From 3fee5aa29ac8a3b66c321c176ef13797625675f8 Mon Sep 17 00:00:00 2001 From: Tong Xi Date: Mon, 14 Mar 2022 10:53:40 -0400 Subject: [PATCH] remove cursors from dom when reload (#1449) * remove cursors from dom when reload * address feedback --- jest.config.js | 3 ++- .../src/shared/useYjsCollaboration.js | 27 +++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/jest.config.js b/jest.config.js index 46f1db478c0..f0f75113fba 100644 --- a/jest.config.js +++ b/jest.config.js @@ -60,7 +60,8 @@ module.exports = { '/packages/lexical-react/src/useLexicalEditor.js', '^@lexical/react/withSubscriptions$': '/packages/lexical-react/src/withSubscriptions.js', - '^@lexical/selection$': '/packages/lexical-selection/src/index.js', + '^@lexical/selection$': + '/packages/lexical-selection/src/index.js', '^@lexical/table$': '/packages/lexical-table/src/index.js', '^@lexical/text$': '/packages/lexical-text/src/index.js', '^@lexical/yjs$': '/packages/lexical-yjs/src/index.js', diff --git a/packages/lexical-react/src/shared/useYjsCollaboration.js b/packages/lexical-react/src/shared/useYjsCollaboration.js index fa0e246a41d..e1077886426 100644 --- a/packages/lexical-react/src/shared/useYjsCollaboration.js +++ b/packages/lexical-react/src/shared/useYjsCollaboration.js @@ -95,7 +95,7 @@ export function useYjsCollaboration( ); const onProviderDocReload = (ydoc) => { - clearEditorSkipCollab(editor); + clearEditorSkipCollab(editor, binding); setDoc(ydoc); docMap.set(id, ydoc); isReloadingDoc.current = true; @@ -278,7 +278,8 @@ function initializeEditor(editor: LexicalEditor): void { ); } -function clearEditorSkipCollab(editor) { +function clearEditorSkipCollab(editor, binding) { + // reset editor state editor.update( () => { const root = $getRoot(); @@ -289,4 +290,26 @@ function clearEditorSkipCollab(editor) { tag: 'skip-collab', }, ); + + if (binding.cursors == null) { + return; + } + + const cursorsContainer = binding.cursorsContainer; + if (cursorsContainer == null) { + return; + } + + // reset cursors in dom + const cursors = Array.from(binding.cursors.values()); + for (let i = 0; i < cursors.length; i++) { + const cursor = cursors[i]; + const selection = cursor.selection; + if (selection && selection.selections != null) { + const selections = selection.selections; + for (let j = 0; j < selections.length; j++) { + cursorsContainer.removeChild(selections[i]); + } + } + } }