Skip to content

Commit

Permalink
[lexical] Bug Fix: Fix registerNodeTransform regression introduced in #…
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepum authored Jan 3, 2025
1 parent 85c08b6 commit bd1ef2a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
29 changes: 29 additions & 0 deletions packages/lexical-code/src/__tests__/unit/LexicalCodeNode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import {
$createCodeHighlightNode,
$createCodeNode,
$isCodeHighlightNode,
registerCodeHighlighting,
Expand Down Expand Up @@ -856,5 +857,33 @@ describe('LexicalCodeNode tests', () => {
}
}
});
describe('initial editor state before transforms', () => {
test('can be registered after initial editor state (regression #7014)', async () => {
const {editor} = testEnv;
await editor.update(
() => {
const root = $getRoot();
const codeBlock = $createCodeNode('javascript');
codeBlock.append(
$createCodeHighlightNode('const lexical = "awesome"'),
);
root.append(codeBlock);
},
{tag: 'history-merge'},
);
// before transform
expect(testEnv.innerHTML).toBe(
'<code spellcheck="false" data-language="javascript" data-highlight-language="javascript" dir="ltr"><span data-lexical-text="true">const lexical = "awesome"</span></code>',
);
registerRichText(editor);
registerTabIndentation(editor);
registerCodeHighlighting(editor);
await Promise.resolve(undefined);
// after transforms
expect(testEnv.innerHTML).toBe(
'<code spellcheck="false" data-language="javascript" data-highlight-language="javascript" dir="ltr" data-gutter="1"><span data-lexical-text="true">const</span><span data-lexical-text="true"> lexical </span><span data-lexical-text="true">=</span><span data-lexical-text="true"> </span><span data-lexical-text="true">"awesome"</span></code>',
);
});
});
});
});
9 changes: 7 additions & 2 deletions packages/lexical/src/LexicalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,13 @@ export function markNodesWithTypesAsDirty(
editor.update(
() => {
for (const nodeMap of dirtyNodeMaps) {
for (const node of nodeMap.values()) {
node.markDirty();
for (const nodeKey of nodeMap.keys()) {
// We are only concerned with nodes that are still in the latest NodeMap,
// if they no longer exist then markDirty would raise an exception
const latest = $getNodeByKey(nodeKey);
if (latest) {
latest.markDirty();
}
}
}
},
Expand Down

0 comments on commit bd1ef2a

Please sign in to comment.