Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix HistoryPlugin selection out of sync #4390

Merged
merged 4 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*
*/

import {createEmptyHistoryState, registerHistory} from '@lexical/history';
import {useLexicalComposerContext} from '@lexical/react/src/LexicalComposerContext';
import {ContentEditable} from '@lexical/react/src/LexicalContentEditable';
import LexicalErrorBoundary from '@lexical/react/src/LexicalErrorBoundary';
Expand All @@ -14,7 +15,9 @@ import {RichTextPlugin} from '@lexical/react/src/LexicalRichTextPlugin';
import {$createQuoteNode} from '@lexical/rich-text/src';
import {$setBlocksType} from '@lexical/selection/src';
import {
$createNodeSelection,
$createRangeSelection,
$isNodeSelection,
CAN_REDO_COMMAND,
CAN_UNDO_COMMAND,
CLEAR_HISTORY_COMMAND,
Expand All @@ -25,7 +28,7 @@ import {
SerializedTextNode,
UNDO_COMMAND,
} from 'lexical/src';
import {TestComposer} from 'lexical/src/__tests__/utils';
import {createTestEditor, TestComposer} from 'lexical/src/__tests__/utils';
import {$getRoot, $setSelection} from 'lexical/src/LexicalUtils';
import {$createParagraphNode} from 'lexical/src/nodes/LexicalParagraphNode';
import {$createTextNode} from 'lexical/src/nodes/LexicalTextNode';
Expand Down Expand Up @@ -265,6 +268,50 @@ describe('LexicalHistory tests', () => {
expect(canRedo).toBe(false);
expect(canUndo).toBe(true);
});

test('undoStack selection points to the same editor', async () => {
const editor_ = createTestEditor({namespace: 'parent'});
const sharedHistory = createEmptyHistoryState();
registerHistory(editor_, sharedHistory, 1000);
await editor_.update(() => {
const root = $getRoot();
const paragraph = $createParagraphNode();
root.append(paragraph);
});
await editor_.update(() => {
const root = $getRoot();
const paragraph = $createParagraphNode();
root.append(paragraph);
const nodeSelection = $createNodeSelection();
nodeSelection.add(paragraph.getKey());
$setSelection(nodeSelection);
});
const nestedEditor = createTestEditor({namespace: 'nested'});
await nestedEditor.update(
() => {
const root = $getRoot();
const paragraph = $createParagraphNode();
root.append(paragraph);
paragraph.selectEnd();
},
{
tag: 'history-merge',
},
);
nestedEditor._parentEditor = editor_;
registerHistory(nestedEditor, sharedHistory, 1000);

await nestedEditor.update(() => {
const root = $getRoot();
const paragraph = $createParagraphNode();
root.append(paragraph);
paragraph.selectEnd();
});

expect(sharedHistory.undoStack.length).toBe(2);
await editor_.dispatchCommand(UNDO_COMMAND, undefined);
expect($isNodeSelection(editor_.getEditorState()._selection)).toBe(true);
});
});

const createParagraphNode = (text: string) => {
Expand Down
22 changes: 4 additions & 18 deletions packages/lexical-history/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,10 @@
*
*/

import type {
EditorState,
GridSelection,
LexicalEditor,
LexicalNode,
NodeKey,
NodeSelection,
RangeSelection,
} from 'lexical';
import type {EditorState, LexicalEditor, LexicalNode, NodeKey} from 'lexical';

import {mergeRegister} from '@lexical/utils';
import {
$getSelection,
$isRangeSelection,
$isRootNode,
$isTextNode,
Expand Down Expand Up @@ -47,7 +38,6 @@ const DELETE_CHARACTER_AFTER_SELECTION = 4;
export type HistoryStateEntry = {
editor: LexicalEditor;
editorState: EditorState;
undoSelection?: RangeSelection | NodeSelection | GridSelection | null;
};
export type HistoryState = {
current: null | HistoryStateEntry;
Expand Down Expand Up @@ -374,12 +364,9 @@ function undo(editor: LexicalEditor, historyState: HistoryState): void {
historyState.current = historyStateEntry || null;

if (historyStateEntry) {
historyStateEntry.editor.setEditorState(
historyStateEntry.editorState.clone(historyStateEntry.undoSelection),
{
tag: 'historic',
},
);
historyStateEntry.editor.setEditorState(historyStateEntry.editorState, {
tag: 'historic',
});
}
}
}
Expand Down Expand Up @@ -446,7 +433,6 @@ export function registerHistory(
if (current !== null) {
undoStack.push({
...current,
undoSelection: prevEditorState.read($getSelection),
});
editor.dispatchCommand(CAN_UNDO_COMMAND, true);
}
Expand Down