Skip to content

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

editor: hide clear completed tasks button in readonly editor (#1498)

Muhammad Ali19a5e78495f7
diff --git a/packages/editor/src/extensions/task-item/component.tsx b/packages/editor/src/extensions/task-item/component.tsx
index 17cad1f73..3ee9fb787 100644
--- a/packages/editor/src/extensions/task-item/component.tsx
+++ b/packages/editor/src/extensions/task-item/component.tsx
@@ -177,29 +177,31 @@ export function TaskItemComponent(
alignItems: "center"
}}
>
- <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;
- });
- }}
- />
+ {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>
diff --git a/packages/editor/src/extensions/task-list/component.tsx b/packages/editor/src/extensions/task-list/component.tsx
index 23ece6655..37b2903f8 100644
--- a/packages/editor/src/extensions/task-list/component.tsx
+++ b/packages/editor/src/extensions/task-list/component.tsx
@@ -134,36 +134,39 @@ export function TaskListComponent(
}}
/>
<Flex sx={{ flexShrink: 0, pr: 2, zIndex: 1 }}>
- <ToolButton
- toggled={false}
- title="Clear completed tasks"
- icon="clear"
- variant="small"
- 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.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)
+ {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
);
- }
- return true;
- });
- }}
- />
+ 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;
+ });
+ }}
+ />
+ ) : null}
<Icon
path={Icons.checkbox}
size={15}