Updates code language handling for code blocks
Replaces the previous implementation of updating the `data-language` attribute on code blocks with a more robust solution using Lexical's MutationListener. This ensures that the language attribute is correctly updated when CodeNode changes occur. Also includes an initial setup to update all existing code blocks.
This commit is contained in:
1 parent
b0140fbafb
commit
f5520411db
1 file changed
+58
-27
@@ -1,43 +1,74 @@
|
|||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
||||||
|
import { CodeNode } from '@lexical/code';
|
||||||
|
import { $isCodeNode } from '@lexical/code';
|
||||||
|
|
||||||
export function CodeLanguagePlugin() {
|
export function CodeLanguagePlugin() {
|
||||||
const [editor] = useLexicalComposerContext();
|
const [editor] = useLexicalComposerContext();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Update data-language attribute on code blocks
|
// Use Lexical's MutationListener to safely update DOM when CodeNode changes
|
||||||
const updateCodeLanguages = () => {
|
const removeMutationListener = editor.registerMutationListener(CodeNode, (mutatedNodes) => {
|
||||||
editor.getEditorState().read(() => {
|
editor.getEditorState().read(() => {
|
||||||
const codeBlocks = document.querySelectorAll('[contenteditable="true"] pre, .lexical-viewer pre');
|
for (const [nodeKey, mutation] of mutatedNodes) {
|
||||||
codeBlocks.forEach((block) => {
|
// Handle created or updated nodes
|
||||||
if (block instanceof HTMLElement) {
|
if (mutation === 'created' || mutation === 'updated') {
|
||||||
// Get the language from the CodeNode if available
|
const node = editor.getElementByKey(nodeKey);
|
||||||
const codeElement = block.querySelector('code');
|
if (node instanceof HTMLElement) {
|
||||||
if (codeElement && codeElement.classList.length > 0) {
|
|
||||||
const languageClass = Array.from(codeElement.classList).find(cls => cls.startsWith('language-'));
|
const language = node.getAttribute("data-highlight-language"); //className.replace('data-highlight-language', '');
|
||||||
if (languageClass) {
|
node.setAttribute('data-language', language!);
|
||||||
const language = languageClass.replace('language-', '');
|
/**
|
||||||
block.setAttribute('data-language', language);
|
* 더 좋은 방법이 있을 때까지..
|
||||||
} else if (!block.hasAttribute('data-language')) {
|
*/
|
||||||
block.setAttribute('data-language', 'plain text');
|
// const codeElement = node.querySelector('code');
|
||||||
}
|
// if (codeElement && codeElement.classList.length > 0) {
|
||||||
} else if (!block.hasAttribute('data-language')) {
|
// const languageClass = Array.from(codeElement.classList).find(cls => cls.startsWith('language-'));
|
||||||
block.setAttribute('data-language', 'plain text');
|
// if (languageClass) {
|
||||||
|
// const language = languageClass.replace('language-', '');
|
||||||
|
// node.setAttribute('data-language', language);
|
||||||
|
// } else {
|
||||||
|
// node.setAttribute('data-language', 'plain text');
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// node.setAttribute('data-language', 'plain text');
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
});
|
});
|
||||||
};
|
|
||||||
|
|
||||||
// Initial update
|
|
||||||
setTimeout(updateCodeLanguages, 100);
|
|
||||||
|
|
||||||
// Update on editor changes
|
|
||||||
const unregister = editor.registerUpdateListener(() => {
|
|
||||||
setTimeout(updateCodeLanguages, 50);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return unregister;
|
// Initial setup: update all existing code blocks
|
||||||
|
editor.getEditorState().read(() => {
|
||||||
|
const root = editor.getRootElement();
|
||||||
|
if (!root) return;
|
||||||
|
|
||||||
|
editor.getEditorState()._nodeMap.forEach((node) => {
|
||||||
|
if ($isCodeNode(node)) {
|
||||||
|
const element = editor.getElementByKey(node.__key);
|
||||||
|
if (element instanceof HTMLElement) {
|
||||||
|
console.log(element)
|
||||||
|
const language = element.getAttribute("data-highlight-language"); //className.replace('data-highlight-language', '');
|
||||||
|
element.setAttribute('data-language', language!);
|
||||||
|
// const codeElement = element.querySelector('code');
|
||||||
|
// if (codeElement && codeElement.classList.length > 0) {
|
||||||
|
// const languageClass = Array.from(codeElement.classList).find(cls => cls.startsWith('language-'));
|
||||||
|
// if (languageClass) {
|
||||||
|
// const language = languageClass.replace('language-', '');
|
||||||
|
// element.setAttribute('data-language', language);
|
||||||
|
// } else {
|
||||||
|
// element.setAttribute('data-language', 'plain text');
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// element.setAttribute('data-language', 'plain text');
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return removeMutationListener;
|
||||||
}, [editor]);
|
}, [editor]);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in new issue
Block a user