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

[lexical] Bug Fix: getCachedTypeToNodeMap should handle a empty and writable EditorState #6444

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/lexical/src/LexicalEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ export class LexicalEditor {
klass: Klass<LexicalNode>,
): void {
const prevEditorState = this._editorState;
const nodeMap = getCachedTypeToNodeMap(this._editorState).get(
const nodeMap = getCachedTypeToNodeMap(prevEditorState).get(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

not a meaningful change but should optimize better

klass.getType(),
);
if (!nodeMap) {
Expand Down
6 changes: 6 additions & 0 deletions packages/lexical/src/LexicalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1710,9 +1710,15 @@ export type TypeToNodeMap = Map<string, NodeMap>;
* Compute a cached Map of node type to nodes for a frozen EditorState
*/
const cachedNodeMaps = new WeakMap<EditorState, TypeToNodeMap>();
const EMPTY_TYPE_TO_NODE_MAP: TypeToNodeMap = new Map();
export function getCachedTypeToNodeMap(
editorState: EditorState,
): TypeToNodeMap {
// If this is a new Editor it may have a writable this._editorState
// with only a 'root' entry.
if (!editorState._readOnly && editorState.isEmpty()) {
return EMPTY_TYPE_TO_NODE_MAP;
}
invariant(
editorState._readOnly,
'getCachedTypeToNodeMap called with a writable EditorState',
Expand Down
10 changes: 9 additions & 1 deletion packages/lexical/src/__tests__/unit/LexicalEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
COMMAND_PRIORITY_EDITOR,
COMMAND_PRIORITY_LOW,
createCommand,
createEditor,
EditorState,
ElementNode,
type Klass,
Expand Down Expand Up @@ -1807,7 +1808,14 @@ describe('LexicalEditor tests', () => {
expect(textNodeMutation2[0].get(textNodeKeys[1])).toBe('destroyed');
expect(textNodeMutation2[0].get(textNodeKeys[2])).toBe('destroyed');
});

it('mutation listener on newly initialized editor', async () => {
editor = createEditor();
const textNodeMutations = jest.fn();
editor.registerMutationListener(TextNode, textNodeMutations, {
skipInitialization: false,
});
expect(textNodeMutations.mock.calls.length).toBe(0);
});
it('mutation listener with setEditorState', async () => {
init();

Expand Down
Loading