mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-05-13 14:48:16 +08:00

Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Yeuoly <admin@srmxy.cn> Co-authored-by: JzoNg <jzongcode@gmail.com> Co-authored-by: StyleZhang <jasonapring2015@outlook.com> Co-authored-by: jyong <jyong@dify.ai> Co-authored-by: nite-knite <nkCoding@gmail.com> Co-authored-by: jyong <718720800@qq.com>
43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import { $insertNodes } from 'lexical'
|
|
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
|
|
import { textToEditorState } from '../utils'
|
|
import { CustomTextNode } from './custom-text/node'
|
|
import { CLEAR_HIDE_MENU_TIMEOUT } from './workflow-variable-block'
|
|
import { useEventEmitterContextContext } from '@/context/event-emitter'
|
|
|
|
export const PROMPT_EDITOR_UPDATE_VALUE_BY_EVENT_EMITTER = 'PROMPT_EDITOR_UPDATE_VALUE_BY_EVENT_EMITTER'
|
|
export const PROMPT_EDITOR_INSERT_QUICKLY = 'PROMPT_EDITOR_INSERT_QUICKLY'
|
|
|
|
type UpdateBlockProps = {
|
|
instanceId?: string
|
|
}
|
|
const UpdateBlock = ({
|
|
instanceId,
|
|
}: UpdateBlockProps) => {
|
|
const { eventEmitter } = useEventEmitterContextContext()
|
|
const [editor] = useLexicalComposerContext()
|
|
|
|
eventEmitter?.useSubscription((v: any) => {
|
|
if (v.type === PROMPT_EDITOR_UPDATE_VALUE_BY_EVENT_EMITTER && v.instanceId === instanceId) {
|
|
const editorState = editor.parseEditorState(textToEditorState(v.payload))
|
|
editor.setEditorState(editorState)
|
|
}
|
|
})
|
|
|
|
eventEmitter?.useSubscription((v: any) => {
|
|
if (v.type === PROMPT_EDITOR_INSERT_QUICKLY && v.instanceId === instanceId) {
|
|
editor.focus()
|
|
editor.update(() => {
|
|
const textNode = new CustomTextNode('/')
|
|
$insertNodes([textNode])
|
|
|
|
editor.dispatchCommand(CLEAR_HIDE_MENU_TIMEOUT, undefined)
|
|
})
|
|
}
|
|
})
|
|
|
|
return null
|
|
}
|
|
|
|
export default UpdateBlock
|