Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

index.tsx 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import {
  2. memo,
  3. useEffect,
  4. } from 'react'
  5. import {
  6. $insertNodes,
  7. COMMAND_PRIORITY_EDITOR,
  8. createCommand,
  9. } from 'lexical'
  10. import { mergeRegister } from '@lexical/utils'
  11. import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
  12. import type { ContextBlockType } from '../../types'
  13. import {
  14. $createContextBlockNode,
  15. ContextBlockNode,
  16. } from './node'
  17. import { noop } from 'lodash-es'
  18. export const INSERT_CONTEXT_BLOCK_COMMAND = createCommand('INSERT_CONTEXT_BLOCK_COMMAND')
  19. export const DELETE_CONTEXT_BLOCK_COMMAND = createCommand('DELETE_CONTEXT_BLOCK_COMMAND')
  20. export type Dataset = {
  21. id: string
  22. name: string
  23. type: string
  24. }
  25. const ContextBlock = memo(({
  26. datasets = [],
  27. onAddContext = noop,
  28. onInsert,
  29. onDelete,
  30. canNotAddContext,
  31. }: ContextBlockType) => {
  32. const [editor] = useLexicalComposerContext()
  33. useEffect(() => {
  34. if (!editor.hasNodes([ContextBlockNode]))
  35. throw new Error('ContextBlockPlugin: ContextBlock not registered on editor')
  36. return mergeRegister(
  37. editor.registerCommand(
  38. INSERT_CONTEXT_BLOCK_COMMAND,
  39. () => {
  40. const contextBlockNode = $createContextBlockNode(datasets, onAddContext, canNotAddContext)
  41. $insertNodes([contextBlockNode])
  42. if (onInsert)
  43. onInsert()
  44. return true
  45. },
  46. COMMAND_PRIORITY_EDITOR,
  47. ),
  48. editor.registerCommand(
  49. DELETE_CONTEXT_BLOCK_COMMAND,
  50. () => {
  51. if (onDelete)
  52. onDelete()
  53. return true
  54. },
  55. COMMAND_PRIORITY_EDITOR,
  56. ),
  57. )
  58. }, [editor, datasets, onAddContext, onInsert, onDelete, canNotAddContext])
  59. return null
  60. })
  61. ContextBlock.displayName = 'ContextBlock'
  62. export { ContextBlock }
  63. export { ContextBlockNode } from './node'
  64. export { default as ContextBlockReplacementBlock } from './context-block-replacement-block'