node.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import type { LexicalNode, NodeKey, SerializedLexicalNode } from 'lexical'
  2. import { DecoratorNode } from 'lexical'
  3. import ContextBlockComponent from './component'
  4. import type { Dataset } from './index'
  5. export type SerializedNode = SerializedLexicalNode & { datasets: Dataset[]; onAddContext: () => void; canNotAddContext: boolean }
  6. export class ContextBlockNode extends DecoratorNode<JSX.Element> {
  7. __datasets: Dataset[]
  8. __onAddContext: () => void
  9. __canNotAddContext: boolean
  10. static getType(): string {
  11. return 'context-block'
  12. }
  13. static clone(node: ContextBlockNode): ContextBlockNode {
  14. return new ContextBlockNode(node.__datasets, node.__onAddContext, node.getKey(), node.__canNotAddContext)
  15. }
  16. isInline(): boolean {
  17. return true
  18. }
  19. constructor(datasets: Dataset[], onAddContext: () => void, key?: NodeKey, canNotAddContext?: boolean) {
  20. super(key)
  21. this.__datasets = datasets
  22. this.__onAddContext = onAddContext
  23. this.__canNotAddContext = canNotAddContext || false
  24. }
  25. createDOM(): HTMLElement {
  26. const div = document.createElement('div')
  27. div.classList.add('inline-flex', 'items-center', 'align-middle')
  28. return div
  29. }
  30. updateDOM(): false {
  31. return false
  32. }
  33. decorate(): JSX.Element {
  34. return (
  35. <ContextBlockComponent
  36. nodeKey={this.getKey()}
  37. datasets={this.getDatasets()}
  38. onAddContext={this.getOnAddContext()}
  39. canNotAddContext={this.getCanNotAddContext()}
  40. />
  41. )
  42. }
  43. getDatasets(): Dataset[] {
  44. const self = this.getLatest()
  45. return self.__datasets
  46. }
  47. getOnAddContext(): () => void {
  48. const self = this.getLatest()
  49. return self.__onAddContext
  50. }
  51. getCanNotAddContext(): boolean {
  52. const self = this.getLatest()
  53. return self.__canNotAddContext
  54. }
  55. static importJSON(serializedNode: SerializedNode): ContextBlockNode {
  56. const node = $createContextBlockNode(serializedNode.datasets, serializedNode.onAddContext, serializedNode.canNotAddContext)
  57. return node
  58. }
  59. exportJSON(): SerializedNode {
  60. return {
  61. type: 'context-block',
  62. version: 1,
  63. datasets: this.getDatasets(),
  64. onAddContext: this.getOnAddContext(),
  65. canNotAddContext: this.getCanNotAddContext(),
  66. }
  67. }
  68. getTextContent(): string {
  69. return '{{#context#}}'
  70. }
  71. }
  72. export function $createContextBlockNode(datasets: Dataset[], onAddContext: () => void, canNotAddContext?: boolean): ContextBlockNode {
  73. return new ContextBlockNode(datasets, onAddContext, undefined, canNotAddContext)
  74. }
  75. export function $isContextBlockNode(
  76. node: ContextBlockNode | LexicalNode | null | undefined,
  77. ): boolean {
  78. return node instanceof ContextBlockNode
  79. }