node.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import type {
  2. EditorConfig,
  3. LexicalNode,
  4. SerializedTextNode,
  5. } from 'lexical'
  6. import {
  7. $applyNodeReplacement,
  8. TextNode,
  9. } from 'lexical'
  10. export class VariableValueBlockNode extends TextNode {
  11. static getType(): string {
  12. return 'variable-value-block'
  13. }
  14. static clone(node: VariableValueBlockNode): VariableValueBlockNode {
  15. return new VariableValueBlockNode(node.__text, node.__key)
  16. }
  17. // constructor(text: string, key?: NodeKey) {
  18. // super(text, key)
  19. // }
  20. createDOM(config: EditorConfig): HTMLElement {
  21. const element = super.createDOM(config)
  22. element.classList.add('inline-flex', 'items-center', 'px-0.5', 'h-[22px]', 'text-text-accent', 'rounded-[5px]', 'align-middle')
  23. return element
  24. }
  25. static importJSON(serializedNode: SerializedTextNode): TextNode {
  26. const node = $createVariableValueBlockNode(serializedNode.text)
  27. node.setFormat(serializedNode.format)
  28. node.setDetail(serializedNode.detail)
  29. node.setMode(serializedNode.mode)
  30. node.setStyle(serializedNode.style)
  31. return node
  32. }
  33. exportJSON(): SerializedTextNode {
  34. return {
  35. detail: this.getDetail(),
  36. format: this.getFormat(),
  37. mode: this.getMode(),
  38. style: this.getStyle(),
  39. text: this.getTextContent(),
  40. type: 'variable-value-block',
  41. version: 1,
  42. }
  43. }
  44. canInsertTextBefore(): boolean {
  45. return false
  46. }
  47. }
  48. export function $createVariableValueBlockNode(text = ''): VariableValueBlockNode {
  49. return $applyNodeReplacement(new VariableValueBlockNode(text))
  50. }
  51. export function $isVariableValueNodeBlock(
  52. node: LexicalNode | null | undefined,
  53. ): node is VariableValueBlockNode {
  54. return node instanceof VariableValueBlockNode
  55. }