Skip to content

Commit

Permalink
Fix HistoryPlugin selection out of sync (#4390)
Browse files Browse the repository at this point in the history
  • Loading branch information
zurfyx committed Apr 26, 2023
1 parent 30b4187 commit 28fec90
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
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

2 comments on commit 28fec90

@vercel
Copy link

@vercel vercel bot commented on 28fec90 Apr 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical – ./packages/lexical-website

lexical.dev
lexical-git-main-fbopensource.vercel.app
lexical-fbopensource.vercel.app
lexicaljs.com
lexicaljs.org
www.lexical.dev

@vercel
Copy link

@vercel vercel bot commented on 28fec90 Apr 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

lexical-playground – ./packages/lexical-playground

playground.lexical.dev
lexical-playground.vercel.app
lexical-playground-fbopensource.vercel.app
lexical-playground-git-main-fbopensource.vercel.app

Please sign in to comment.