Skip to content

Commit

Permalink
remove cursors from dom when reload (facebook#1449)
Browse files Browse the repository at this point in the history
* remove cursors from dom when reload

* address feedback
  • Loading branch information
btezzxxt authored and acywatson committed Apr 9, 2022
1 parent be98b6c commit 3fee5aa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ module.exports = {
'<rootDir>/packages/lexical-react/src/useLexicalEditor.js',
'^@lexical/react/withSubscriptions$':
'<rootDir>/packages/lexical-react/src/withSubscriptions.js',
'^@lexical/selection$': '<rootDir>/packages/lexical-selection/src/index.js',
'^@lexical/selection$':
'<rootDir>/packages/lexical-selection/src/index.js',
'^@lexical/table$': '<rootDir>/packages/lexical-table/src/index.js',
'^@lexical/text$': '<rootDir>/packages/lexical-text/src/index.js',
'^@lexical/yjs$': '<rootDir>/packages/lexical-yjs/src/index.js',
Expand Down
27 changes: 25 additions & 2 deletions packages/lexical-react/src/shared/useYjsCollaboration.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function useYjsCollaboration(
);

const onProviderDocReload = (ydoc) => {
clearEditorSkipCollab(editor);
clearEditorSkipCollab(editor, binding);
setDoc(ydoc);
docMap.set(id, ydoc);
isReloadingDoc.current = true;
Expand Down Expand Up @@ -278,7 +278,8 @@ function initializeEditor(editor: LexicalEditor): void {
);
}

function clearEditorSkipCollab(editor) {
function clearEditorSkipCollab(editor, binding) {
// reset editor state
editor.update(
() => {
const root = $getRoot();
Expand All @@ -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]);
}
}
}
}

0 comments on commit 3fee5aa

Please sign in to comment.