node.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import type { EditorConfig, NodeKey, SerializedTextNode } from 'lexical'
  2. import { $createTextNode, TextNode } from 'lexical'
  3. export class CustomTextNode extends TextNode {
  4. static getType() {
  5. return 'custom-text'
  6. }
  7. static clone(node: CustomTextNode) {
  8. return new CustomTextNode(node.__text, node.__key)
  9. }
  10. constructor(text: string, key?: NodeKey) {
  11. super(text, key)
  12. }
  13. createDOM(config: EditorConfig) {
  14. const dom = super.createDOM(config)
  15. dom.classList.add('align-middle')
  16. return dom
  17. }
  18. static importJSON(serializedNode: SerializedTextNode): TextNode {
  19. const node = $createTextNode(serializedNode.text)
  20. node.setFormat(serializedNode.format)
  21. node.setDetail(serializedNode.detail)
  22. node.setMode(serializedNode.mode)
  23. node.setStyle(serializedNode.style)
  24. return node
  25. }
  26. exportJSON(): SerializedTextNode {
  27. return {
  28. detail: this.getDetail(),
  29. format: this.getFormat(),
  30. mode: this.getMode(),
  31. style: this.getStyle(),
  32. text: this.getTextContent(),
  33. type: 'custom-text',
  34. version: 1,
  35. }
  36. }
  37. isSimpleText() {
  38. return (
  39. (this.__type === 'text' || this.__type === 'custom-text') && this.__mode === 0)
  40. }
  41. }
  42. export function $createCustomTextNode(text: string): CustomTextNode {
  43. return new CustomTextNode(text)
  44. }