| 
									
										
										
										
											2024-06-27 22:48:06 +08:00
										 |  |  | import { | 
					
						
							|  |  |  |     DOMConversion, | 
					
						
							|  |  |  |     DOMConversionMap, DOMConversionOutput, | 
					
						
							|  |  |  |     ElementNode, | 
					
						
							|  |  |  |     LexicalEditor, | 
					
						
							|  |  |  |     LexicalNode, | 
					
						
							| 
									
										
										
										
											2024-08-11 23:08:51 +08:00
										 |  |  |     SerializedElementNode, Spread, | 
					
						
							| 
									
										
										
										
											2024-06-27 22:48:06 +08:00
										 |  |  | } from 'lexical'; | 
					
						
							|  |  |  | import type {EditorConfig} from "lexical/LexicalEditor"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-11 23:08:51 +08:00
										 |  |  | export type SerializedHorizontalRuleNode = Spread<{ | 
					
						
							|  |  |  |     id: string; | 
					
						
							|  |  |  | }, SerializedElementNode> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-27 22:48:06 +08:00
										 |  |  | export class HorizontalRuleNode extends ElementNode { | 
					
						
							| 
									
										
										
										
											2024-08-11 23:08:51 +08:00
										 |  |  |     __id: string = ''; | 
					
						
							| 
									
										
										
										
											2024-06-27 22:48:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     static getType() { | 
					
						
							|  |  |  |         return 'horizontal-rule'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-11 23:08:51 +08:00
										 |  |  |     setId(id: string) { | 
					
						
							|  |  |  |         const self = this.getWritable(); | 
					
						
							|  |  |  |         self.__id = id; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     getId(): string { | 
					
						
							|  |  |  |         const self = this.getLatest(); | 
					
						
							|  |  |  |         return self.__id; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-27 22:48:06 +08:00
										 |  |  |     static clone(node: HorizontalRuleNode): HorizontalRuleNode { | 
					
						
							| 
									
										
										
										
											2024-08-11 23:08:51 +08:00
										 |  |  |         const newNode = new HorizontalRuleNode(node.__key); | 
					
						
							|  |  |  |         newNode.__id = node.__id; | 
					
						
							|  |  |  |         return newNode; | 
					
						
							| 
									
										
										
										
											2024-06-27 22:48:06 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-11 23:08:51 +08:00
										 |  |  |     createDOM(_config: EditorConfig, _editor: LexicalEditor): HTMLElement { | 
					
						
							|  |  |  |         const el = document.createElement('hr'); | 
					
						
							|  |  |  |         if (this.__id) { | 
					
						
							|  |  |  |             el.setAttribute('id', this.__id); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return el; | 
					
						
							| 
									
										
										
										
											2024-06-27 22:48:06 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-11 23:08:51 +08:00
										 |  |  |     updateDOM(prevNode: HorizontalRuleNode, dom: HTMLElement) { | 
					
						
							|  |  |  |         return prevNode.__id !== this.__id; | 
					
						
							| 
									
										
										
										
											2024-06-27 22:48:06 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static importDOM(): DOMConversionMap|null { | 
					
						
							|  |  |  |         return { | 
					
						
							|  |  |  |             hr(node: HTMLElement): DOMConversion|null { | 
					
						
							|  |  |  |                 return { | 
					
						
							|  |  |  |                     conversion: (element: HTMLElement): DOMConversionOutput|null => { | 
					
						
							| 
									
										
										
										
											2024-08-11 23:08:51 +08:00
										 |  |  |                         const node = new HorizontalRuleNode(); | 
					
						
							|  |  |  |                         if (element.id) { | 
					
						
							|  |  |  |                             node.setId(element.id); | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         return {node}; | 
					
						
							| 
									
										
										
										
											2024-06-27 22:48:06 +08:00
										 |  |  |                     }, | 
					
						
							|  |  |  |                     priority: 3, | 
					
						
							|  |  |  |                 }; | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-11 23:08:51 +08:00
										 |  |  |     exportJSON(): SerializedHorizontalRuleNode { | 
					
						
							| 
									
										
										
										
											2024-06-27 22:48:06 +08:00
										 |  |  |         return { | 
					
						
							|  |  |  |             ...super.exportJSON(), | 
					
						
							|  |  |  |             type: 'horizontal-rule', | 
					
						
							|  |  |  |             version: 1, | 
					
						
							| 
									
										
										
										
											2024-08-11 23:08:51 +08:00
										 |  |  |             id: this.__id, | 
					
						
							| 
									
										
										
										
											2024-06-27 22:48:06 +08:00
										 |  |  |         }; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-11 23:08:51 +08:00
										 |  |  |     static importJSON(serializedNode: SerializedHorizontalRuleNode): HorizontalRuleNode { | 
					
						
							|  |  |  |         const node = $createHorizontalRuleNode(); | 
					
						
							|  |  |  |         node.setId(serializedNode.id); | 
					
						
							|  |  |  |         return node; | 
					
						
							| 
									
										
										
										
											2024-06-27 22:48:06 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-11 23:08:51 +08:00
										 |  |  | export function $createHorizontalRuleNode(): HorizontalRuleNode { | 
					
						
							| 
									
										
										
										
											2024-06-27 22:48:06 +08:00
										 |  |  |     return new HorizontalRuleNode(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-11 23:08:51 +08:00
										 |  |  | export function $isHorizontalRuleNode(node: LexicalNode | null | undefined): node is HorizontalRuleNode { | 
					
						
							| 
									
										
										
										
											2024-06-27 22:48:06 +08:00
										 |  |  |     return node instanceof HorizontalRuleNode; | 
					
						
							|  |  |  | } |