Skip to content

Commit

Permalink
editor: fix task list state & readonly bugs (streetwriters#7117)
Browse files Browse the repository at this point in the history
* fix task list complete state not showing on first load
* fix task list allowing paste when readonly

Signed-off-by: 01zulfi <85733202+01zulfi@users.noreply.github.com>
  • Loading branch information
01zulfi authored Dec 24, 2024
1 parent 68eb502 commit 4bebf5a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
3 changes: 3 additions & 0 deletions packages/editor/src/extensions/task-list/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ export function TaskListComponent(
ref={forwardRef}
dir={textDirection}
contentEditable={editor.isEditable && !readonly}
onPaste={(e) => {
if (readonly) e.preventDefault();
}}
sx={{
ul: {
display: "block",
Expand Down
37 changes: 15 additions & 22 deletions packages/editor/src/extensions/task-list/task-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ import {
findParentNodeClosestToPos,
getExactChangedNodes
} from "../../utils/prosemirror.js";
import { countCheckedItems, findRootTaskList, toggleChildren } from "./utils.js";
import {
countCheckedItems,
findRootTaskList,
toggleChildren
} from "./utils.js";
import { Node as ProsemirrorNode } from "@tiptap/pm/model";
import { TaskItemNode } from "../task-item/index.js";

Expand All @@ -49,7 +53,16 @@ export const TaskListNode = TaskList.extend({
return {
stats: {
default: { checked: 0, total: 0 },
rendered: false
rendered: false,
parseHTML: (element) => {
// do not update stats for nested task lists
if (!!element.closest("ul")) return { checked: 0, total: 0 };
const total = element.querySelectorAll("li.checklist--item").length;
const checked = element.querySelectorAll(
"li.checklist--item.checked"
).length;
return { checked, total };
}
},
title: {
default: null,
Expand Down Expand Up @@ -222,26 +235,6 @@ export const TaskListNode = TaskList.extend({
// the task list.
new Plugin({
key: new PluginKey("task-list-state-management"),
view(view) {
const { tr } = view.state;
tr.doc.descendants((node, pos) => {
if (node.type.name === TaskList.name) {
tr.setNodeMarkup(pos, undefined, {
...node.attrs,
stats: countCheckedItems(node)
});
return false;
}
});
tr.setMeta("preventUpdate", true);
tr.setMeta("addToHistory", false);
try {
view.dispatch(tr);
} catch (e) {
// ignore
}
return {};
},
appendTransaction(transactions, oldState, newState) {
if (!transactions[0].docChanged) return;

Expand Down

0 comments on commit 4bebf5a

Please sign in to comment.