node.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import type { LexicalNode, NodeKey, SerializedLexicalNode } from 'lexical'
  2. import { DecoratorNode } from 'lexical'
  3. import HistoryBlockComponent from './component'
  4. import type { RoleName } from './index'
  5. export type SerializedNode = SerializedLexicalNode & { roleName: RoleName; onEditRole: () => void }
  6. export class HistoryBlockNode extends DecoratorNode<JSX.Element> {
  7. __roleName: RoleName
  8. __onEditRole: () => void
  9. static getType(): string {
  10. return 'history-block'
  11. }
  12. static clone(node: HistoryBlockNode): HistoryBlockNode {
  13. return new HistoryBlockNode(node.__roleName, node.__onEditRole)
  14. }
  15. constructor(roleName: RoleName, onEditRole: () => void, key?: NodeKey) {
  16. super(key)
  17. this.__roleName = roleName
  18. this.__onEditRole = onEditRole
  19. }
  20. isInline(): boolean {
  21. return true
  22. }
  23. createDOM(): HTMLElement {
  24. const div = document.createElement('div')
  25. div.classList.add('inline-flex', 'items-center', 'align-middle')
  26. return div
  27. }
  28. updateDOM(): false {
  29. return false
  30. }
  31. decorate(): JSX.Element {
  32. return (
  33. <HistoryBlockComponent
  34. nodeKey={this.getKey()}
  35. roleName={this.getRoleName()}
  36. onEditRole={this.getOnEditRole()}
  37. />
  38. )
  39. }
  40. getRoleName(): RoleName {
  41. const self = this.getLatest()
  42. return self.__roleName
  43. }
  44. getOnEditRole(): () => void {
  45. const self = this.getLatest()
  46. return self.__onEditRole
  47. }
  48. static importJSON(serializedNode: SerializedNode): HistoryBlockNode {
  49. const node = $createHistoryBlockNode(serializedNode.roleName, serializedNode.onEditRole)
  50. return node
  51. }
  52. exportJSON(): SerializedNode {
  53. return {
  54. type: 'history-block',
  55. version: 1,
  56. roleName: this.getRoleName(),
  57. onEditRole: this.getOnEditRole,
  58. }
  59. }
  60. getTextContent(): string {
  61. return '{{#histories#}}'
  62. }
  63. }
  64. export function $createHistoryBlockNode(roleName: RoleName, onEditRole: () => void): HistoryBlockNode {
  65. return new HistoryBlockNode(roleName, onEditRole)
  66. }
  67. export function $isHistoryBlockNode(
  68. node: HistoryBlockNode | LexicalNode | null | undefined,
  69. ): node is HistoryBlockNode {
  70. return node instanceof HistoryBlockNode
  71. }