diff --git a/web/src/components/MemoContent/MemoContentContext.tsx b/web/src/components/MemoContent/MemoContentContext.tsx index e2e36f943..3be3eeb97 100644 --- a/web/src/components/MemoContent/MemoContentContext.tsx +++ b/web/src/components/MemoContent/MemoContentContext.tsx @@ -19,6 +19,9 @@ export interface MemoContentContextType { /** Parent page path (for navigation) */ parentPage?: string; + + /** Reference to the container element for the memo content */ + containerRef?: React.RefObject; } export const MemoContentContext = createContext({ diff --git a/web/src/components/MemoContent/TaskListItem.tsx b/web/src/components/MemoContent/TaskListItem.tsx index e9365a2a5..eeebf1444 100644 --- a/web/src/components/MemoContent/TaskListItem.tsx +++ b/web/src/components/MemoContent/TaskListItem.tsx @@ -44,8 +44,14 @@ export const TaskListItem: React.FC = ({ checked, ...props }) if (taskIndexStr !== null) { taskIndex = parseInt(taskIndexStr); } else { - // Fallback: Calculate index by counting previous task list items - const allTaskItems = listItem.closest("ul, ol")?.querySelectorAll("li.task-list-item") || []; + // Fallback: Calculate index by counting ALL task list items in the memo + // Use the container ref from context for proper scoping + const container = context.containerRef?.current; + if (!container) { + return; + } + + const allTaskItems = container.querySelectorAll("li.task-list-item"); for (let i = 0; i < allTaskItems.length; i++) { if (allTaskItems[i] === listItem) { taskIndex = i; diff --git a/web/src/components/MemoContent/index.tsx b/web/src/components/MemoContent/index.tsx index e1b510ce2..a1483fe9e 100644 --- a/web/src/components/MemoContent/index.tsx +++ b/web/src/components/MemoContent/index.tsx @@ -49,6 +49,7 @@ const MemoContent = observer((props: Props) => { readonly: !allowEdit, disableFilter: props.disableFilter, parentPage: props.parentPage, + containerRef: memoContentContainerRef, }; // Initial compact mode.