mirror of
				https://github.com/langgenius/dify.git
				synced 2025-11-04 04:43:09 +00:00 
			
		
		
		
	Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: JzoNg <jzongcode@gmail.com> Co-authored-by: Gillian97 <jinling.sunshine@gmail.com>
		
			
				
	
	
		
			63 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			63 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import type { FC } from 'react'
 | 
						|
import { useEffect } from 'react'
 | 
						|
import {
 | 
						|
  $insertNodes,
 | 
						|
  COMMAND_PRIORITY_EDITOR,
 | 
						|
  createCommand,
 | 
						|
} from 'lexical'
 | 
						|
import { mergeRegister } from '@lexical/utils'
 | 
						|
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
 | 
						|
import {
 | 
						|
  $createQueryBlockNode,
 | 
						|
  QueryBlockNode,
 | 
						|
} from './node'
 | 
						|
 | 
						|
export const INSERT_QUERY_BLOCK_COMMAND = createCommand('INSERT_QUERY_BLOCK_COMMAND')
 | 
						|
export const DELETE_QUERY_BLOCK_COMMAND = createCommand('DELETE_QUERY_BLOCK_COMMAND')
 | 
						|
 | 
						|
export type QueryBlockProps = {
 | 
						|
  onInsert?: () => void
 | 
						|
  onDelete?: () => void
 | 
						|
}
 | 
						|
const QueryBlock: FC<QueryBlockProps> = ({
 | 
						|
  onInsert,
 | 
						|
  onDelete,
 | 
						|
}) => {
 | 
						|
  const [editor] = useLexicalComposerContext()
 | 
						|
 | 
						|
  useEffect(() => {
 | 
						|
    if (!editor.hasNodes([QueryBlockNode]))
 | 
						|
      throw new Error('QueryBlockPlugin: QueryBlock not registered on editor')
 | 
						|
 | 
						|
    return mergeRegister(
 | 
						|
      editor.registerCommand(
 | 
						|
        INSERT_QUERY_BLOCK_COMMAND,
 | 
						|
        () => {
 | 
						|
          const contextBlockNode = $createQueryBlockNode()
 | 
						|
 | 
						|
          $insertNodes([contextBlockNode])
 | 
						|
          if (onInsert)
 | 
						|
            onInsert()
 | 
						|
 | 
						|
          return true
 | 
						|
        },
 | 
						|
        COMMAND_PRIORITY_EDITOR,
 | 
						|
      ),
 | 
						|
      editor.registerCommand(
 | 
						|
        DELETE_QUERY_BLOCK_COMMAND,
 | 
						|
        () => {
 | 
						|
          if (onDelete)
 | 
						|
            onDelete()
 | 
						|
 | 
						|
          return true
 | 
						|
        },
 | 
						|
        COMMAND_PRIORITY_EDITOR,
 | 
						|
      ),
 | 
						|
    )
 | 
						|
  }, [editor, onInsert, onDelete])
 | 
						|
 | 
						|
  return null
 | 
						|
}
 | 
						|
 | 
						|
export default QueryBlock
 |