node.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import type { LexicalNode, NodeKey, SerializedLexicalNode } from 'lexical'
  2. import { DecoratorNode } from 'lexical'
  3. import type { WorkflowVariableBlockType } from '../../types'
  4. import WorkflowVariableBlockComponent from './component'
  5. export type WorkflowNodesMap = WorkflowVariableBlockType['workflowNodesMap']
  6. export type SerializedNode = SerializedLexicalNode & {
  7. variables: string[]
  8. workflowNodesMap: WorkflowNodesMap
  9. }
  10. export class WorkflowVariableBlockNode extends DecoratorNode<JSX.Element> {
  11. __variables: string[]
  12. __workflowNodesMap: WorkflowNodesMap
  13. static getType(): string {
  14. return 'workflow-variable-block'
  15. }
  16. static clone(node: WorkflowVariableBlockNode): WorkflowVariableBlockNode {
  17. return new WorkflowVariableBlockNode(node.__variables, node.__workflowNodesMap)
  18. }
  19. isInline(): boolean {
  20. return true
  21. }
  22. constructor(variables: string[], workflowNodesMap: WorkflowNodesMap, key?: NodeKey) {
  23. super(key)
  24. this.__variables = variables
  25. this.__workflowNodesMap = workflowNodesMap
  26. }
  27. createDOM(): HTMLElement {
  28. const div = document.createElement('div')
  29. div.classList.add('inline-flex', 'items-center', 'align-middle')
  30. return div
  31. }
  32. updateDOM(): false {
  33. return false
  34. }
  35. decorate(): JSX.Element {
  36. return (
  37. <WorkflowVariableBlockComponent
  38. nodeKey={this.getKey()}
  39. variables={this.__variables}
  40. workflowNodesMap={this.__workflowNodesMap}
  41. />
  42. )
  43. }
  44. static importJSON(serializedNode: SerializedNode): WorkflowVariableBlockNode {
  45. const node = $createWorkflowVariableBlockNode(serializedNode.variables, serializedNode.workflowNodesMap)
  46. return node
  47. }
  48. exportJSON(): SerializedNode {
  49. return {
  50. type: 'workflow-variable-block',
  51. version: 1,
  52. variables: this.getVariables(),
  53. workflowNodesMap: this.getWorkflowNodesMap(),
  54. }
  55. }
  56. getVariables(): string[] {
  57. const self = this.getLatest()
  58. return self.__variables
  59. }
  60. getWorkflowNodesMap(): WorkflowNodesMap {
  61. const self = this.getLatest()
  62. return self.__workflowNodesMap
  63. }
  64. getTextContent(): string {
  65. return `{{#${this.getVariables().join('.')}#}}`
  66. }
  67. }
  68. export function $createWorkflowVariableBlockNode(variables: string[], workflowNodesMap: WorkflowNodesMap): WorkflowVariableBlockNode {
  69. return new WorkflowVariableBlockNode(variables, workflowNodesMap)
  70. }
  71. export function $isWorkflowVariableBlockNode(
  72. node: WorkflowVariableBlockNode | LexicalNode | null | undefined,
  73. ): node is WorkflowVariableBlockNode {
  74. return node instanceof WorkflowVariableBlockNode
  75. }