Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

editor: task list should update when its items are toggled

Abdullah Attaa633f4a3fe87
diff --git a/packages/editor/src/extensions/task-item/component.tsx b/packages/editor/src/extensions/task-item/component.tsx
index 94efccda9..bc1f0697f 100644
--- a/packages/editor/src/extensions/task-item/component.tsx
+++ b/packages/editor/src/extensions/task-item/component.tsx
@@ -21,7 +21,7 @@ import { Flex, Text } from "@theme-ui/components";
import { ReactNodeViewProps } from "../react";
import { Icon } from "../../toolbar/components/icon";
import { Icons } from "../../toolbar/icons";
-import { Node } from "prosemirror-model";
+import { Node as ProsemirrorNode } from "prosemirror-model";
import { Transaction } from "prosemirror-state";
import { findChildren, findChildrenInRange } from "@tiptap/core";
import { useCallback } from "react";
@@ -196,7 +196,7 @@ export function TaskItemComponent(
}
function toggleChildren(
- node: Node,
+ node: ProsemirrorNode,
tr: Transaction,
toggleState: boolean,
parentPos: number
diff --git a/packages/editor/src/extensions/task-list/component.tsx b/packages/editor/src/extensions/task-list/component.tsx
index ee6769133..a5e3b91cf 100644
--- a/packages/editor/src/extensions/task-list/component.tsx
+++ b/packages/editor/src/extensions/task-list/component.tsx
@@ -27,6 +27,7 @@ import { findParentNodeOfTypeClosestToPos } from "../../utils/prosemirror";
import { ReactNodeViewProps } from "../react";
import { TaskItemNode } from "../task-item";
import { TaskListAttributes } from "./task-list";
+import { countCheckedItems } from "./utils";
export function TaskListComponent(
props: ReactNodeViewProps<TaskListAttributes>
@@ -64,14 +65,7 @@ export function TaskListComponent(
}, [editor.commands, editor.state.doc, getParent, node, node.childCount]);
useEffect(() => {
- let checked = 0;
- let total = 0;
- node.forEach((node) => {
- if (node.type.name === TaskItemNode.name) {
- if (node.attrs.checked) checked++;
- total++;
- }
- });
+ const { checked, total } = countCheckedItems(node);
const percentage = Math.round((checked / total) * 100);
setStats({ checked, total, percentage });
}, [isNested, node]);
diff --git a/packages/editor/src/extensions/task-list/task-list.ts b/packages/editor/src/extensions/task-list/task-list.ts
index 356f56dcc..47851822f 100644
--- a/packages/editor/src/extensions/task-list/task-list.ts
+++ b/packages/editor/src/extensions/task-list/task-list.ts
@@ -25,6 +25,7 @@ import { Plugin, PluginKey, NodeSelection } from "prosemirror-state";
import TaskItem from "@tiptap/extension-task-item";
import { dropPoint } from "prosemirror-transform";
import { findChildrenByType, hasSameAttributes } from "../../utils/prosemirror";
+import { countCheckedItems } from "./utils";
export type TaskListAttributes = {
title: string;
@@ -110,7 +111,8 @@ export const TaskListNode = TaskList.extend({
shouldUpdate: (prev, next) => {
return (
!hasSameAttributes(prev.attrs, next.attrs) ||
- prev.childCount !== next.childCount
+ prev.childCount !== next.childCount ||
+ countCheckedItems(prev).checked !== countCheckedItems(next).checked
);
}
});
diff --git a/packages/editor/src/extensions/task-list/utils.ts b/packages/editor/src/extensions/task-list/utils.ts
new file mode 100644
index 000000000..c9166da82
--- /dev/null
+++ b/packages/editor/src/extensions/task-list/utils.ts
@@ -0,0 +1,33 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import { Fragment, Node as ProsemirrorNode } from "prosemirror-model";
+import { TaskItemNode } from "../task-item";
+
+export function countCheckedItems(node: ProsemirrorNode) {
+ let checked = 0;
+ let total = 0;
+ node.descendants((node) => {
+ if (node.type.name === TaskItemNode.name) {
+ if (node.attrs.checked) checked++;
+ total++;
+ }
+ });
+ return { checked, total };
+}