mirror of
				https://github.com/langgenius/dify.git
				synced 2025-10-31 02:42:59 +00:00 
			
		
		
		
	 5b9858a8a3
			
		
	
	
		5b9858a8a3
		
			
		
	
	
	
	
		
			
			Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: JzoNg <jzongcode@gmail.com> Co-authored-by: Gillian97 <jinling.sunshine@gmail.com>
		
			
				
	
	
		
			46 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			46 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import { useEffect } from 'react'
 | |
| import {
 | |
|   $insertNodes,
 | |
|   COMMAND_PRIORITY_EDITOR,
 | |
|   createCommand,
 | |
| } from 'lexical'
 | |
| import { mergeRegister } from '@lexical/utils'
 | |
| import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
 | |
| import { CustomTextNode } from '../custom-text/node'
 | |
| 
 | |
| export const INSERT_VARIABLE_BLOCK_COMMAND = createCommand('INSERT_VARIABLE_BLOCK_COMMAND')
 | |
| export const INSERT_VARIABLE_VALUE_BLOCK_COMMAND = createCommand('INSERT_VARIABLE_VALUE_BLOCK_COMMAND')
 | |
| 
 | |
| const VariableBlock = () => {
 | |
|   const [editor] = useLexicalComposerContext()
 | |
| 
 | |
|   useEffect(() => {
 | |
|     return mergeRegister(
 | |
|       editor.registerCommand(
 | |
|         INSERT_VARIABLE_BLOCK_COMMAND,
 | |
|         () => {
 | |
|           const textNode = new CustomTextNode('{')
 | |
|           $insertNodes([textNode])
 | |
| 
 | |
|           return true
 | |
|         },
 | |
|         COMMAND_PRIORITY_EDITOR,
 | |
|       ),
 | |
|       editor.registerCommand(
 | |
|         INSERT_VARIABLE_VALUE_BLOCK_COMMAND,
 | |
|         (value: string) => {
 | |
|           const textNode = new CustomTextNode(value)
 | |
|           $insertNodes([textNode])
 | |
| 
 | |
|           return true
 | |
|         },
 | |
|         COMMAND_PRIORITY_EDITOR,
 | |
|       ),
 | |
|     )
 | |
|   }, [editor])
 | |
| 
 | |
|   return null
 | |
| }
 | |
| 
 | |
| export default VariableBlock
 |