A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
editor: optimize clear all completed tasks action
Abdullah Attab948dcee224e
diff --git a/packages/editor/src/extensions/task-list/component.tsx b/packages/editor/src/extensions/task-list/component.tsxindex a5e3b91cf..609a0fbcc 100644--- a/packages/editor/src/extensions/task-list/component.tsx+++ b/packages/editor/src/extensions/task-list/component.tsx@@ -27,7 +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";+import { countCheckedItems, deleteCheckedItems } from "./utils";export function TaskListComponent(props: ReactNodeViewProps<TaskListAttributes>@@ -120,40 +120,26 @@ export function TaskListComponent(}}/>{editor.isEditable && (- <ToolButton- toggled={false}- title="Clear completed tasks"- icon="clear"- variant="small"- sx={{- zIndex: 1- }}- onClick={() => {- if (!editor.isEditable) return;- if (!editor.current) return;- const pos = getPos();- // we need to get a fresh instance of the task list instead- // of using the one we got via props.- const node = editor.current.state.doc.nodeAt(pos);- if (!node) return;-- editor.current?.commands.command(({ tr }) => {- const taskItems = findChildren(- node,- (n) => n.type.name === TaskItem.name && n.attrs.checked- );- const mapping = tr.mapping;- for (const item of taskItems) {- const childPos = pos + item.pos + 1;- tr.deleteRange(- mapping.map(childPos),- mapping.map(childPos + item.node.nodeSize)- );- }- return true;- });- }}- />+ <ToolButton+ toggled={false}+ title="Clear completed tasks"+ icon="clear"+ variant="small"+ sx={{+ zIndex: 1+ }}+ onClick={() => {+ const pos = getPos();++ editor.current+ ?.chain()+ .focus()+ .command(({ tr }) => {+ return !!deleteCheckedItems(tr, pos);+ })+ .run();+ }}+ />)}<Textvariant={"body"}diff --git a/packages/editor/src/extensions/task-list/utils.ts b/packages/editor/src/extensions/task-list/utils.tsindex c9166da82..0d02ab644 100644--- a/packages/editor/src/extensions/task-list/utils.ts+++ b/packages/editor/src/extensions/task-list/utils.ts@@ -17,8 +17,11 @@ You should have received a copy of the GNU General Public Licensealong with this program. If not, see <http://www.gnu.org/licenses/>.*/+import TaskList from "@tiptap/extension-task-list";+import { Transaction } from "@tiptap/pm/state";import { Fragment, Node as ProsemirrorNode } from "prosemirror-model";import { TaskItemNode } from "../task-item";+import { NodeWithPos } from "@tiptap/core";export function countCheckedItems(node: ProsemirrorNode) {let checked = 0;@@ -31,3 +34,38 @@ export function countCheckedItems(node: ProsemirrorNode) {});return { checked, total };}++export function deleteCheckedItems(tr: Transaction, pos: number) {+ const node = tr.doc.nodeAt(pos);++ const parent = node ? { node, pos } : null;+ if (!parent || parent.node.type.name !== TaskList.name) return;++ const sublists: NodeWithPos[] = [];+ parent.node.descendants((node, nodePos) => {+ if (node.type.name === TaskList.name)+ sublists.push({ node, pos: pos + nodePos + 1 });+ });+ if (sublists.length > 1) sublists.reverse();+ sublists.push(parent);++ for (const list of sublists) {+ const listNode = tr.doc.nodeAt(tr.mapping.map(list.pos));+ if (!listNode) continue;++ const children: ProsemirrorNode[] = [];+ listNode.forEach((node, _, index) => {+ if (!node.attrs.checked) children.push(listNode.child(index));+ });++ tr.replaceWith(+ tr.mapping.map(list.pos),+ tr.mapping.map(list.pos + list.node.nodeSize),+ listNode.copy(Fragment.from(children))+ );+ }++ if (!tr.steps.length) return null;+ return tr;+}+