| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  | import type { LexicalNode, NodeKey, SerializedLexicalNode } from 'lexical' | 
					
						
							|  |  |  | import { DecoratorNode } from 'lexical' | 
					
						
							|  |  |  | import ContextBlockComponent from './component' | 
					
						
							|  |  |  | import type { Dataset } from './index' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | export type SerializedNode = SerializedLexicalNode & { datasets: Dataset[]; onAddContext: () => void; canNotAddContext: boolean } | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export class ContextBlockNode extends DecoratorNode<JSX.Element> { | 
					
						
							|  |  |  |   __datasets: Dataset[] | 
					
						
							|  |  |  |   __onAddContext: () => void | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   __canNotAddContext: boolean | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   static getType(): string { | 
					
						
							|  |  |  |     return 'context-block' | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   static clone(node: ContextBlockNode): ContextBlockNode { | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     return new ContextBlockNode(node.__datasets, node.__onAddContext, node.getKey(), node.__canNotAddContext) | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   isInline(): boolean { | 
					
						
							|  |  |  |     return true | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   constructor(datasets: Dataset[], onAddContext: () => void, key?: NodeKey, canNotAddContext?: boolean) { | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |     super(key) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     this.__datasets = datasets | 
					
						
							|  |  |  |     this.__onAddContext = onAddContext | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     this.__canNotAddContext = canNotAddContext || false | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   createDOM(): HTMLElement { | 
					
						
							|  |  |  |     const div = document.createElement('div') | 
					
						
							|  |  |  |     div.classList.add('inline-flex', 'items-center', 'align-middle') | 
					
						
							|  |  |  |     return div | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   updateDOM(): false { | 
					
						
							|  |  |  |     return false | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   decorate(): JSX.Element { | 
					
						
							|  |  |  |     return ( | 
					
						
							|  |  |  |       <ContextBlockComponent | 
					
						
							|  |  |  |         nodeKey={this.getKey()} | 
					
						
							|  |  |  |         datasets={this.getDatasets()} | 
					
						
							|  |  |  |         onAddContext={this.getOnAddContext()} | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |         canNotAddContext={this.getCanNotAddContext()} | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |       /> | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   getDatasets(): Dataset[] { | 
					
						
							|  |  |  |     const self = this.getLatest() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return self.__datasets | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   getOnAddContext(): () => void { | 
					
						
							|  |  |  |     const self = this.getLatest() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return self.__onAddContext | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   getCanNotAddContext(): boolean { | 
					
						
							|  |  |  |     const self = this.getLatest() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return self.__canNotAddContext | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |   static importJSON(serializedNode: SerializedNode): ContextBlockNode { | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     const node = $createContextBlockNode(serializedNode.datasets, serializedNode.onAddContext, serializedNode.canNotAddContext) | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return node | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   exportJSON(): SerializedNode { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       type: 'context-block', | 
					
						
							|  |  |  |       version: 1, | 
					
						
							|  |  |  |       datasets: this.getDatasets(), | 
					
						
							|  |  |  |       onAddContext: this.getOnAddContext(), | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       canNotAddContext: this.getCanNotAddContext(), | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   getTextContent(): string { | 
					
						
							|  |  |  |     return '{{#context#}}' | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | export function $createContextBlockNode(datasets: Dataset[], onAddContext: () => void, canNotAddContext?: boolean): ContextBlockNode { | 
					
						
							|  |  |  |   return new ContextBlockNode(datasets, onAddContext, undefined, canNotAddContext) | 
					
						
							| 
									
										
										
										
											2023-10-12 23:14:28 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function $isContextBlockNode( | 
					
						
							|  |  |  |   node: ContextBlockNode | LexicalNode | null | undefined, | 
					
						
							|  |  |  | ): boolean { | 
					
						
							|  |  |  |   return node instanceof ContextBlockNode | 
					
						
							|  |  |  | } |