BookStack/resources/js/wysiwyg/nodes/horizontal-rule.ts

92 lines
2.4 KiB
TypeScript
Raw Normal View History

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