tiptap: fix lock node

This commit is contained in:
fantasticit 2022-04-27 14:43:38 +08:00
parent c65a519945
commit a9eefd3c37
1 changed files with 20 additions and 14 deletions

View File

@ -6,11 +6,11 @@ type CollaborationCursorStorage = {
users: { clientId: number; [key: string]: any }[];
};
export function findNodeAt(state: EditorState, from, to) {
export function findNodeAt(editor, state: EditorState, from, to) {
let target = null;
let pos = -1;
if (state && state.doc) {
if (editor && !editor.isDestroyed) {
state.doc.nodesBetween(from, to, (node, p) => {
target = node;
pos = p;
@ -61,6 +61,10 @@ const awarenessStatesToArray = (states: Map<number, Record<string, any>>) => {
const lockCollaborationUserEditingNodes = (extensionThis, users) => {
const { editor, options } = extensionThis;
if (!editor || editor.isDestroyed) {
return;
}
while (options.lockedDOMNodes.length) {
const dom = options.lockedDOMNodes.shift();
dom && dom.classList && dom.classList.remove(options.lockClassName);
@ -82,9 +86,10 @@ const lockCollaborationUserEditingNodes = (extensionThis, users) => {
users.forEach((user) => {
if (user.name === options.user.name) return;
try {
const cursor = user.cursor;
if (cursor) {
const { node, pos } = findNodeAt(editor.state, cursor.originAnchor, cursor.originHead);
const { node, pos } = findNodeAt(editor, editor.state, cursor.originAnchor, cursor.originHead);
if (node && node.isAtom) {
const dom = editor.view.nodeDOM(pos) as HTMLElement;
@ -96,6 +101,7 @@ const lockCollaborationUserEditingNodes = (extensionThis, users) => {
options.collaborationUserCursorCache.set(user.clientId, { user, cursor });
}
}
} catch (e) {}
});
}
};