Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

index.tsx 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 { CurrentBlockType } from '../../types'
  13. import {
  14. $createCurrentBlockNode,
  15. CurrentBlockNode,
  16. } from './node'
  17. export const INSERT_CURRENT_BLOCK_COMMAND = createCommand('INSERT_CURRENT_BLOCK_COMMAND')
  18. export const DELETE_CURRENT_BLOCK_COMMAND = createCommand('DELETE_CURRENT_BLOCK_COMMAND')
  19. const CurrentBlock = memo(({
  20. generatorType,
  21. onInsert,
  22. onDelete,
  23. }: CurrentBlockType) => {
  24. const [editor] = useLexicalComposerContext()
  25. useEffect(() => {
  26. if (!editor.hasNodes([CurrentBlockNode]))
  27. throw new Error('CURRENTBlockPlugin: CURRENTBlock not registered on editor')
  28. return mergeRegister(
  29. editor.registerCommand(
  30. INSERT_CURRENT_BLOCK_COMMAND,
  31. () => {
  32. const currentBlockNode = $createCurrentBlockNode(generatorType)
  33. $insertNodes([currentBlockNode])
  34. if (onInsert)
  35. onInsert()
  36. return true
  37. },
  38. COMMAND_PRIORITY_EDITOR,
  39. ),
  40. editor.registerCommand(
  41. DELETE_CURRENT_BLOCK_COMMAND,
  42. () => {
  43. if (onDelete)
  44. onDelete()
  45. return true
  46. },
  47. COMMAND_PRIORITY_EDITOR,
  48. ),
  49. )
  50. }, [editor, generatorType, onDelete, onInsert])
  51. return null
  52. })
  53. CurrentBlock.displayName = 'CurrentBlock'
  54. export { CurrentBlock }
  55. export { CurrentBlockNode } from './node'
  56. export { default as CurrentBlockReplacementBlock } from './current-block-replacement-block'