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 { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
||||
import { CodeNode } from '@lexical/code';
|
||||
import { $isCodeNode } from '@lexical/code';
|
||||
|
||||
export function CodeLanguagePlugin() {
|
||||
const [editor] = useLexicalComposerContext();
|
||||
|
||||
useEffect(() => {
|
||||
// Update data-language attribute on code blocks
|
||||
const updateCodeLanguages = () => {
|
||||
// Use Lexical's MutationListener to safely update DOM when CodeNode changes
|
||||
const removeMutationListener = editor.registerMutationListener(CodeNode, (mutatedNodes) => {
|
||||
editor.getEditorState().read(() => {
|
||||
const codeBlocks = document.querySelectorAll('[contenteditable="true"] pre, .lexical-viewer pre');
|
||||
codeBlocks.forEach((block) => {
|
||||
if (block instanceof HTMLElement) {
|
||||
// Get the language from the CodeNode if available
|
||||
const codeElement = block.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-', '');
|
||||
block.setAttribute('data-language', language);
|
||||
} else if (!block.hasAttribute('data-language')) {
|
||||
block.setAttribute('data-language', 'plain text');
|
||||
}
|
||||
} else if (!block.hasAttribute('data-language')) {
|
||||
block.setAttribute('data-language', 'plain text');
|
||||
for (const [nodeKey, mutation] of mutatedNodes) {
|
||||
// Handle created or updated nodes
|
||||
if (mutation === 'created' || mutation === 'updated') {
|
||||
const node = editor.getElementByKey(nodeKey);
|
||||
if (node instanceof HTMLElement) {
|
||||
|
||||
const language = node.getAttribute("data-highlight-language"); //className.replace('data-highlight-language', '');
|
||||
node.setAttribute('data-language', language!);
|
||||
/**
|
||||
* 더 좋은 방법이 있을 때까지..
|
||||
*/
|
||||
// const codeElement = node.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-', '');
|
||||
// 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]);
|
||||
|
||||
return null;
|
||||
|
||||
Reference in new issue
Block a user