Commit 5396c126 authored by Johnny's avatar Johnny

chore: extract task list class names to constants

- Add TASK_LIST_CLASS and TASK_LIST_ITEM_CLASS constants
- Replace hardcoded 'contains-task-list' and 'task-list-item' strings
- Improve maintainability and prevent typos
parent 97ba1545
...@@ -3,6 +3,7 @@ import { Checkbox } from "@/components/ui/checkbox"; ...@@ -3,6 +3,7 @@ import { Checkbox } from "@/components/ui/checkbox";
import { useUpdateMemo } from "@/hooks/useMemoQueries"; import { useUpdateMemo } from "@/hooks/useMemoQueries";
import { toggleTaskAtIndex } from "@/utils/markdown-manipulation"; import { toggleTaskAtIndex } from "@/utils/markdown-manipulation";
import { useMemoViewContext, useMemoViewDerived } from "../MemoView/MemoViewContext"; import { useMemoViewContext, useMemoViewDerived } from "../MemoView/MemoViewContext";
import { TASK_LIST_CLASS, TASK_LIST_ITEM_CLASS } from "./constants";
import type { ReactMarkdownProps } from "./markdown/types"; import type { ReactMarkdownProps } from "./markdown/types";
interface TaskListItemProps extends React.InputHTMLAttributes<HTMLInputElement>, ReactMarkdownProps { interface TaskListItemProps extends React.InputHTMLAttributes<HTMLInputElement>, ReactMarkdownProps {
...@@ -37,7 +38,7 @@ export const TaskListItem: React.FC<TaskListItemProps> = ({ checked, node: _node ...@@ -37,7 +38,7 @@ export const TaskListItem: React.FC<TaskListItemProps> = ({ checked, node: _node
// Fallback: Calculate index by counting task list items // Fallback: Calculate index by counting task list items
// Walk up to find the parent element with all task items // Walk up to find the parent element with all task items
let searchRoot = listItem.parentElement; let searchRoot = listItem.parentElement;
while (searchRoot && !searchRoot.classList.contains("contains-task-list")) { while (searchRoot && !searchRoot.classList.contains(TASK_LIST_CLASS)) {
searchRoot = searchRoot.parentElement; searchRoot = searchRoot.parentElement;
} }
...@@ -46,7 +47,7 @@ export const TaskListItem: React.FC<TaskListItemProps> = ({ checked, node: _node ...@@ -46,7 +47,7 @@ export const TaskListItem: React.FC<TaskListItemProps> = ({ checked, node: _node
searchRoot = document.body; searchRoot = document.body;
} }
const allTaskItems = searchRoot.querySelectorAll("li.task-list-item"); const allTaskItems = searchRoot.querySelectorAll(`li.${TASK_LIST_ITEM_CLASS}`);
for (let i = 0; i < allTaskItems.length; i++) { for (let i = 0; i < allTaskItems.length; i++) {
if (allTaskItems[i] === listItem) { if (allTaskItems[i] === listItem) {
taskIndex = i; taskIndex = i;
......
import { defaultSchema } from "rehype-sanitize"; import { defaultSchema } from "rehype-sanitize";
// Class names added by remark-gfm for task lists
export const TASK_LIST_CLASS = "contains-task-list";
export const TASK_LIST_ITEM_CLASS = "task-list-item";
// Compact mode display settings // Compact mode display settings
export const COMPACT_MODE_CONFIG = { export const COMPACT_MODE_CONFIG = {
maxHeightVh: 60, // 60% of viewport height maxHeightVh: 60, // 60% of viewport height
......
import { cn } from "@/lib/utils"; import { cn } from "@/lib/utils";
import { TASK_LIST_CLASS, TASK_LIST_ITEM_CLASS } from "../constants";
import type { ReactMarkdownProps } from "./types"; import type { ReactMarkdownProps } from "./types";
interface ListProps extends React.HTMLAttributes<HTMLUListElement | HTMLOListElement>, ReactMarkdownProps { interface ListProps extends React.HTMLAttributes<HTMLUListElement | HTMLOListElement>, ReactMarkdownProps {
...@@ -12,7 +13,7 @@ interface ListProps extends React.HTMLAttributes<HTMLUListElement | HTMLOListEle ...@@ -12,7 +13,7 @@ interface ListProps extends React.HTMLAttributes<HTMLUListElement | HTMLOListEle
*/ */
export const List = ({ ordered, children, className, node: _node, ...domProps }: ListProps) => { export const List = ({ ordered, children, className, node: _node, ...domProps }: ListProps) => {
const Component = ordered ? "ol" : "ul"; const Component = ordered ? "ol" : "ul";
const isTaskList = className?.includes("contains-task-list"); const isTaskList = className?.includes(TASK_LIST_CLASS);
return ( return (
<Component <Component
...@@ -38,7 +39,7 @@ interface ListItemProps extends React.LiHTMLAttributes<HTMLLIElement>, ReactMark ...@@ -38,7 +39,7 @@ interface ListItemProps extends React.LiHTMLAttributes<HTMLLIElement>, ReactMark
* Applies specialized styling for task checkboxes * Applies specialized styling for task checkboxes
*/ */
export const ListItem = ({ children, className, node: _node, ...domProps }: ListItemProps) => { export const ListItem = ({ children, className, node: _node, ...domProps }: ListItemProps) => {
const isTaskListItem = className?.includes("task-list-item"); const isTaskListItem = className?.includes(TASK_LIST_ITEM_CLASS);
if (isTaskListItem) { if (isTaskListItem) {
return ( return (
...@@ -48,7 +49,7 @@ export const ListItem = ({ children, className, node: _node, ...domProps }: List ...@@ -48,7 +49,7 @@ export const ListItem = ({ children, className, node: _node, ...domProps }: List
// Task item styles: checkbox margins, inline paragraph, nested list indent // Task item styles: checkbox margins, inline paragraph, nested list indent
"[&>button]:mr-2 [&>button]:align-middle", "[&>button]:mr-2 [&>button]:align-middle",
"[&>p]:inline [&>p]:m-0", "[&>p]:inline [&>p]:m-0",
"[&>.contains-task-list]:pl-6", `[&>.${TASK_LIST_CLASS}]:pl-6`,
className, className,
)} )}
{...domProps} {...domProps}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment