1import {createCommand, ElementNode} from 'lexical'; 2 3export const APPLY_TEXT_CLASS_COMMAND = createCommand('APPLY_TEXT_CLASS_COMMAND'); 4 5export class ClassNode extends ElementNode { 6 __className; 7 8 static getType() { 9 return 'class'; 10 } 11 12 static clone(node) { 13 // noinspection JSValidateTypes 14 return new ClassNode(node.__className, node.__key); 15 } 16 17 constructor(className = '', key) { 18 super(key); 19 this.__className = className; 20 } 21 22 createDOM(config) { 23 const dom = document.createElement('span'); 24 if (this.__className) dom.className = this.__className; 25 // noinspection JSValidateTypes 26 return dom; 27 } 28 29 updateDOM(prev, dom) { 30 // update class when changed 31 if (dom.className !== this.__className) { 32 dom.className = this.__className || ''; 33 } 34 return false; 35 } 36 37 /** 38 * Import DOM -> Lexical node mapping. 39 * If a <span> element has a class attribute, we convert it into a ClassNode 40 * preserving the class value. 41 */ 42 static importDOM() { 43 return { 44 'span': (domNode) => { 45 // Only convert spans that have a non-empty class attribute 46 const classAttr = domNode.getAttribute && domNode.getAttribute('class'); 47 if (!classAttr) { 48 return null; 49 } 50 51 return { 52 conversion: (_lexicalNode) => { 53 // Create a ClassNode with the span's class attribute and 54 // let the import pipeline populate its children. 55 const node = new ClassNode(classAttr); 56 return {node}; 57 }, 58 priority: 1 59 }; 60 } 61 }; 62 } 63 64 static importJSON(serialized) { 65 // noinspection JSValidateTypes 66 return new ClassNode(serialized.className); 67 } 68 69 exportJSON() { 70 return { 71 type: 'class', 72 version: 1, 73 className: this.__className, 74 }; 75 } 76 77 // helper getter/setter 78 getClassName() { 79 return this.__className; 80 } 81 setClassName(name) { 82 this.__className = name; 83 this.markDirty(); 84 } 85 86 isInline() { 87 return true; 88 } 89} 90 91export function $createClassNode(className = '') { 92 return new ClassNode(className); 93} 94 95export function $isClassNode(node) { 96 return node instanceof ClassNode; 97}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.