dify/web/app/components/base/prompt-editor/plugins/query-block/index.tsx
Stephen Zhou a84c2d36a3
style: format with vp fmt (#38803)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-07-12 15:57:46 +00:00

49 lines
1.5 KiB
TypeScript

import type { QueryBlockType } from '../../types'
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
import { mergeRegister } from '@lexical/utils'
import { $insertNodes, COMMAND_PRIORITY_EDITOR, createCommand } from 'lexical'
import { memo, useEffect } from 'react'
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')
const QueryBlock = memo(({ onInsert, onDelete }: QueryBlockType) => {
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
})
QueryBlock.displayName = 'QueryBlock'
export { QueryBlock }
export { QueryBlockNode } from './node'
export { default as QueryBlockReplacementBlock } from './query-block-replacement-block'