From b8df1e0959b9f950bfb39e7731dcab2ad1b8a43b Mon Sep 17 00:00:00 2001 From: Baptiste Jamin Date: Thu, 15 May 2025 20:46:35 +0200 Subject: [PATCH] [lexical-code] Bug Fix: Allow Code Highlighter to be working in headless mode (#7538) Co-authored-by: Baptiste Jamin --- packages/lexical-code/src/CodeHighlighter.ts | 41 +++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/packages/lexical-code/src/CodeHighlighter.ts b/packages/lexical-code/src/CodeHighlighter.ts index 1fcc5b227..c019496b9 100644 --- a/packages/lexical-code/src/CodeHighlighter.ts +++ b/packages/lexical-code/src/CodeHighlighter.ts @@ -813,23 +813,32 @@ export function registerCodeHighlighting( tokenizer = PrismTokenizer; } - return mergeRegister( - editor.registerMutationListener( - CodeNode, - (mutations) => { - editor.update(() => { - for (const [key, type] of mutations) { - if (type !== 'destroyed') { - const node = $getNodeByKey(key); - if (node !== null) { - updateCodeGutter(node as CodeNode, editor); + const registrations = []; + + // Only register the mutation listener if not in headless mode + if (editor._headless !== true) { + registrations.push( + editor.registerMutationListener( + CodeNode, + (mutations) => { + editor.update(() => { + for (const [key, type] of mutations) { + if (type !== 'destroyed') { + const node = $getNodeByKey(key); + if (node !== null) { + updateCodeGutter(node as CodeNode, editor); + } } } - } - }); - }, - {skipInitialization: false}, - ), + }); + }, + {skipInitialization: false}, + ), + ); + } + + // Add the rest of the registrations + registrations.push( editor.registerNodeTransform(CodeNode, (node) => codeNodeTransform(node, editor, tokenizer as Tokenizer), ), @@ -937,4 +946,6 @@ export function registerCodeHighlighting( COMMAND_PRIORITY_LOW, ), ); + + return mergeRegister(...registrations); }