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>
		
			
				
	
	
		
			53 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			53 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import {
 | |
|   useCallback,
 | |
|   useEffect,
 | |
| } from 'react'
 | |
| import type { TextNode } from 'lexical'
 | |
| import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
 | |
| import { useLexicalTextEntity } from '../../hooks'
 | |
| import {
 | |
|   $createVariableValueBlockNode,
 | |
|   VariableValueBlockNode,
 | |
| } from './node'
 | |
| import { getHashtagRegexString } from './utils'
 | |
| 
 | |
| const REGEX = new RegExp(getHashtagRegexString(), 'i')
 | |
| 
 | |
| const VariableValueBlock = () => {
 | |
|   const [editor] = useLexicalComposerContext()
 | |
| 
 | |
|   useEffect(() => {
 | |
|     if (!editor.hasNodes([VariableValueBlockNode]))
 | |
|       throw new Error('VariableValueBlockPlugin: VariableValueNode not registered on editor')
 | |
|   }, [editor])
 | |
| 
 | |
|   const createVariableValueBlockNode = useCallback((textNode: TextNode): VariableValueBlockNode => {
 | |
|     return $createVariableValueBlockNode(textNode.getTextContent())
 | |
|   }, [])
 | |
| 
 | |
|   const getVariableValueMatch = useCallback((text: string) => {
 | |
|     const matchArr = REGEX.exec(text)
 | |
| 
 | |
|     if (matchArr === null)
 | |
|       return null
 | |
| 
 | |
|     const hashtagLength = matchArr[3].length + 4
 | |
|     const startOffset = matchArr.index
 | |
|     const endOffset = startOffset + hashtagLength
 | |
|     return {
 | |
|       end: endOffset,
 | |
|       start: startOffset,
 | |
|     }
 | |
|   }, [])
 | |
| 
 | |
|   useLexicalTextEntity<VariableValueBlockNode>(
 | |
|     getVariableValueMatch,
 | |
|     VariableValueBlockNode,
 | |
|     createVariableValueBlockNode,
 | |
|   )
 | |
| 
 | |
|   return null
 | |
| }
 | |
| 
 | |
| export default VariableValueBlock
 |