Skip to content

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

editor: reduce no. of nested nodes in task list for perf

Abdullah Atta88f047c3124b
diff --git a/packages/editor/src/extensions/task-item/component.tsx b/packages/editor/src/extensions/task-item/component.tsx
index 7e89064de..94efccda9 100644
--- a/packages/editor/src/extensions/task-item/component.tsx
+++ b/packages/editor/src/extensions/task-item/component.tsx
@@ -75,137 +75,122 @@ export function TaskItemComponent(
return (
<>
- <Flex
- data-drag-image
- sx={{
- bg: "background",
- borderRadius: "default",
- gap: 2,
- ":hover > .dragHandle": {
- opacity: editor.isEditable ? 1 : 0
- },
- ":hover > .taskItemTools": {
- opacity: 1
- }
- }}
- >
- {editor.isEditable && (
- <Icon
- className="dragHandle"
- draggable="true"
- // NOTE: Turning this off somehow makes drag/drop stop working
- // properly on touch devices.
- // contentEditable={false}
- data-drag-handle
- path={Icons.dragHandle}
- sx={{
- opacity: [1, 1, 0],
- alignSelf: "start",
- bg: "transparent",
- cursor: "grab",
- ".icon:hover path": {
- fill: "var(--checked) !important"
- },
- mt: "1px"
- }}
- size={isMobile ? 24 : 20}
- />
- )}
+ {editor.isEditable && (
<Icon
- path={checked ? Icons.check : ""}
- stroke="1px"
- contentEditable={false}
- tabIndex={1}
+ className="dragHandle"
+ draggable="true"
+ // NOTE: Turning this off somehow makes drag/drop stop working
+ // properly on touch devices.
+ // contentEditable={false}
+ data-drag-handle
+ path={Icons.dragHandle}
sx={{
- border: "2px solid",
- borderColor: checked ? "checked" : "icon",
- borderRadius: "default",
+ opacity: [1, 1, 0],
alignSelf: "start",
- p: "1px",
- mt: "2px",
- cursor: editor.isEditable ? "pointer" : "unset",
- ":hover": {
- borderColor: "checked"
- },
- ":hover .icon path": {
+ bg: "transparent",
+ cursor: "grab",
+ ".icon:hover path": {
fill: "var(--checked) !important"
- }
- }}
- onMouseDown={(e) => {
- if (useIsKeyboardOpen.current) {
- e.preventDefault();
- }
- toggle();
- }}
- onTouchEnd={(e) => {
- if (useIsKeyboardOpen.current || isiOS()) {
- e.preventDefault();
- toggle();
- }
+ },
+ mt: "1px"
}}
- color={checked ? "checked" : "icon"}
- size={isMobile ? 16 : 14}
+ size={isMobile ? 24 : 20}
/>
+ )}
+ <Icon
+ path={checked ? Icons.check : ""}
+ stroke="1px"
+ contentEditable={false}
+ tabIndex={1}
+ sx={{
+ border: "2px solid",
+ borderColor: checked ? "checked" : "icon",
+ borderRadius: "default",
+ alignSelf: "start",
+ p: "1px",
+ mt: "2px",
+ cursor: editor.isEditable ? "pointer" : "unset",
+ ":hover": {
+ borderColor: "checked"
+ },
+ ":hover .icon path": {
+ fill: "var(--checked) !important"
+ }
+ }}
+ onMouseDown={(e) => {
+ if (useIsKeyboardOpen.current) {
+ e.preventDefault();
+ }
+ toggle();
+ }}
+ onTouchEnd={(e) => {
+ if (useIsKeyboardOpen.current || isiOS()) {
+ e.preventDefault();
+ toggle();
+ }
+ }}
+ color={checked ? "checked" : "icon"}
+ size={isMobile ? 16 : 14}
+ />
- <Text
- as="div"
- ref={forwardRef}
- sx={{
- "> .taskitem-content-wrapper > p": {
- textDecorationLine: checked ? "line-through" : "none",
- opacity: checked ? 0.8 : 1
+ <Text
+ as="div"
+ ref={forwardRef}
+ sx={{
+ "> .taskitem-content-wrapper > p": {
+ textDecorationLine: checked ? "line-through" : "none",
+ opacity: checked ? 0.8 : 1
+ },
+ // FIXME: this is quite fragile and will break if the structure
+ // changes. We should probably find a better & more robust
+ // solution for this.
+ "> .taskitem-content-wrapper > p:hover ~ div > div.task-list-tools .toggleSublist":
+ {
+ opacity: 1
},
- // FIXME: this is quite fragile and will break if the structure
- // changes. We should probably find a better & more robust
- // solution for this.
- "> .taskitem-content-wrapper > p:hover ~ div > div.task-list-tools .toggleSublist":
- {
- opacity: 1
- },
- flex: 1,
- mt: "1px"
+ flex: 1,
+ mt: "1px"
+ }}
+ />
+ <DesktopOnly>
+ <Flex
+ className="taskItemTools"
+ sx={{
+ bg: "background",
+ opacity: 0,
+ position: "absolute",
+ top: 1,
+ right: 0,
+ alignItems: "center"
}}
- />
- <DesktopOnly>
- <Flex
- className="taskItemTools"
- sx={{
- bg: "background",
- opacity: 0,
- position: "absolute",
- top: 1,
- right: 0,
- alignItems: "center"
- }}
- >
- {editor.isEditable && (
- <Icon
- className="deleleTaskItem"
- title="Delete this task item"
- path={Icons.close}
- size={18}
- sx={{
- cursor: "pointer"
- }}
- onClick={() => {
- 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.commands.command(({ tr }) => {
- tr.deleteRange(pos, pos + node.nodeSize);
- return true;
- });
- }}
- />
- )}
- </Flex>
- </DesktopOnly>
- </Flex>
+ >
+ {editor.isEditable && (
+ <Icon
+ className="deleleTaskItem"
+ title="Delete this task item"
+ path={Icons.close}
+ size={18}
+ sx={{
+ cursor: "pointer"
+ }}
+ onClick={() => {
+ 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.commands.command(({ tr }) => {
+ tr.deleteRange(pos, pos + node.nodeSize);
+ return true;
+ });
+ }}
+ />
+ )}
+ </Flex>
+ </DesktopOnly>
</>
);
}
diff --git a/packages/editor/src/extensions/task-item/task-item.ts b/packages/editor/src/extensions/task-item/task-item.ts
index 4396f0115..f76543949 100644
--- a/packages/editor/src/extensions/task-item/task-item.ts
+++ b/packages/editor/src/extensions/task-item/task-item.ts
@@ -83,7 +83,11 @@ export const TaskItemNode = TaskItem.extend({
addNodeView() {
return createNodeView(TaskItemComponent, {
contentDOMFactory: true,
- wrapperFactory: () => document.createElement("li"),
+ wrapperFactory: () => {
+ const li = document.createElement("li");
+ li.dataset.dragImage = "true";
+ return li;
+ },
shouldUpdate: ({ attrs: prev }, { attrs: next }) => {
return prev.checked !== next.checked;
}
diff --git a/packages/editor/src/extensions/task-list/component.tsx b/packages/editor/src/extensions/task-list/component.tsx
index 0c53a0c2a..ee6769133 100644
--- a/packages/editor/src/extensions/task-list/component.tsx
+++ b/packages/editor/src/extensions/task-list/component.tsx
@@ -78,101 +78,103 @@ export function TaskListComponent(
return (
<>
- <Flex
- sx={{
- flexDirection: "column"
- }}
- className="task-list-tools"
- dir={textDirection}
- >
- {!isNested && (
- <Flex
+ {!isNested && (
+ <Flex
+ sx={{
+ position: "relative",
+ bg: "bgSecondary",
+ py: 1,
+ borderRadius: "default",
+ mb: 2,
+ alignItems: "center",
+ justifyContent: "end",
+ overflow: "hidden"
+ }}
+ className="task-list-tools"
+ dir={textDirection}
+ contentEditable={false}
+ >
+ <Box
sx={{
- position: "relative",
- bg: "bgSecondary",
- py: "5px",
- borderRadius: "default",
- mb: 2,
- alignItems: "center",
- justifyContent: "end",
- overflow: "hidden"
+ height: "100%",
+ width: `${stats.percentage}%`,
+ position: "absolute",
+ bg: "border",
+
+ zIndex: 0,
+ left: 0,
+ transition: "width 250ms ease-out"
}}
- contentEditable={false}
- >
- <Box
- sx={{
- height: "100%",
- width: `${stats.percentage}%`,
- position: "absolute",
- bg: "border",
-
- zIndex: 0,
- left: 0,
- transition: "width 250ms ease-out"
- }}
- />
- <Input
- readOnly={!editor.isEditable}
- value={title || ""}
- variant={"clean"}
+ />
+ <Input
+ readOnly={!editor.isEditable}
+ value={title || ""}
+ variant={"clean"}
+ sx={{
+ p: 0,
+ px: 2,
+ zIndex: 1,
+ color: "fontTertiary",
+ fontSize: "title"
+ }}
+ placeholder="Untitled"
+ onChange={(e) => {
+ updateAttributes(
+ { title: e.target.value },
+ { addToHistory: true, preventUpdate: false }
+ );
+ }}
+ />
+ {editor.isEditable && (
+ <ToolButton
+ toggled={false}
+ title="Clear completed tasks"
+ icon="clear"
+ variant="small"
sx={{
- p: 0,
- px: 2,
- zIndex: 1,
- color: "fontTertiary",
- fontSize: "title"
+ zIndex: 1
}}
- placeholder="Untitled"
- onChange={(e) => {
- updateAttributes(
- { title: e.target.value },
- { addToHistory: true, preventUpdate: false }
- );
+ 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;
+ });
}}
/>
- <Flex
- sx={{ flexShrink: 0, pr: 2, zIndex: 1, alignItems: "center" }}
- >
- {editor.isEditable && (
- <ToolButton
- toggled={false}
- title="Clear completed tasks"
- icon="clear"
- variant="small"
- 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;
- });
- }}
- />
- )}
- <Text variant={"body"} sx={{ ml: 1, color: "fontTertiary" }}>
- {stats.checked}/{stats.total}
- </Text>
- </Flex>
- </Flex>
- )}
- </Flex>
+ )}
+ <Text
+ variant={"body"}
+ sx={{
+ ml: 1,
+ mr: 2,
+ color: "fontTertiary",
+ flexShrink: 0,
+ zIndex: 1
+ }}
+ >
+ {stats.checked}/{stats.total}
+ </Text>
+ </Flex>
+ )}
<Text
as={"div"}
ref={forwardRef}
@@ -188,7 +190,18 @@ export function TaskListComponent(
li: {
listStyleType: "none",
position: "relative",
- marginBottom: isNested ? 1 : [2, 1]
+ marginBottom: isNested ? 1 : [2, 1],
+
+ display: "flex",
+ bg: "background",
+ borderRadius: "default",
+ gap: 2,
+ ":hover > .dragHandle": {
+ opacity: editor.isEditable ? 1 : 0
+ },
+ ":hover > .taskItemTools": {
+ opacity: 1
+ }
}
}}
/>