| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   memo, | 
					
						
							|  |  |  |   useEffect, | 
					
						
							|  |  |  | } from 'react' | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |   $insertNodes, | 
					
						
							|  |  |  |   COMMAND_PRIORITY_EDITOR, | 
					
						
							|  |  |  |   createCommand, | 
					
						
							|  |  |  | } from 'lexical' | 
					
						
							|  |  |  | import { mergeRegister } from '@lexical/utils' | 
					
						
							|  |  |  | import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import type { QueryBlockType } from '../../types' | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  | 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 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | const QueryBlock = memo(({ | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |   onInsert, | 
					
						
							|  |  |  |   onDelete, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | }: QueryBlockType) => { | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |   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 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | }) | 
					
						
							|  |  |  | QueryBlock.displayName = 'QueryBlock' | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | export { QueryBlock } | 
					
						
							|  |  |  | export { QueryBlockNode } from './node' | 
					
						
							|  |  |  | export { default as QueryBlockReplacementBlock } from './query-block-replacement-block' |