2024-08-11 23:08:51 +08:00
|
|
|
import {
|
|
|
|
DOMConversionMap,
|
2024-08-18 23:51:08 +08:00
|
|
|
DOMConversionOutput,
|
2024-08-11 23:08:51 +08:00
|
|
|
LexicalNode,
|
|
|
|
Spread
|
|
|
|
} from "lexical";
|
|
|
|
import {EditorConfig} from "lexical/LexicalEditor";
|
|
|
|
import {QuoteNode, SerializedQuoteNode} from "@lexical/rich-text";
|
2024-08-18 23:51:08 +08:00
|
|
|
import {
|
|
|
|
CommonBlockAlignment, commonPropertiesDifferent,
|
|
|
|
SerializedCommonBlockNode,
|
|
|
|
setCommonBlockPropsFromElement,
|
|
|
|
updateElementWithCommonBlockProps
|
|
|
|
} from "./_common";
|
2024-08-11 23:08:51 +08:00
|
|
|
|
|
|
|
|
2024-08-18 23:51:08 +08:00
|
|
|
export type SerializedCustomQuoteNode = Spread<SerializedCommonBlockNode, SerializedQuoteNode>
|
2024-08-11 23:08:51 +08:00
|
|
|
|
|
|
|
export class CustomQuoteNode extends QuoteNode {
|
|
|
|
__id: string = '';
|
2024-08-18 23:51:08 +08:00
|
|
|
__alignment: CommonBlockAlignment = '';
|
2024-08-11 23:08:51 +08:00
|
|
|
|
|
|
|
static getType() {
|
|
|
|
return 'custom-quote';
|
|
|
|
}
|
|
|
|
|
|
|
|
setId(id: string) {
|
|
|
|
const self = this.getWritable();
|
|
|
|
self.__id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
getId(): string {
|
|
|
|
const self = this.getLatest();
|
|
|
|
return self.__id;
|
|
|
|
}
|
|
|
|
|
2024-08-18 23:51:08 +08:00
|
|
|
setAlignment(alignment: CommonBlockAlignment) {
|
|
|
|
const self = this.getWritable();
|
|
|
|
self.__alignment = alignment;
|
|
|
|
}
|
|
|
|
|
|
|
|
getAlignment(): CommonBlockAlignment {
|
|
|
|
const self = this.getLatest();
|
|
|
|
return self.__alignment;
|
|
|
|
}
|
|
|
|
|
2024-08-11 23:08:51 +08:00
|
|
|
static clone(node: CustomQuoteNode) {
|
|
|
|
const newNode = new CustomQuoteNode(node.__key);
|
|
|
|
newNode.__id = node.__id;
|
2024-08-18 23:51:08 +08:00
|
|
|
newNode.__alignment = node.__alignment;
|
2024-08-11 23:08:51 +08:00
|
|
|
return newNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
createDOM(config: EditorConfig): HTMLElement {
|
|
|
|
const dom = super.createDOM(config);
|
2024-08-18 23:51:08 +08:00
|
|
|
updateElementWithCommonBlockProps(dom, this);
|
2024-08-11 23:08:51 +08:00
|
|
|
return dom;
|
|
|
|
}
|
|
|
|
|
2024-08-18 23:51:08 +08:00
|
|
|
updateDOM(prevNode: CustomQuoteNode): boolean {
|
|
|
|
return commonPropertiesDifferent(prevNode, this);
|
|
|
|
}
|
|
|
|
|
2024-08-11 23:08:51 +08:00
|
|
|
exportJSON(): SerializedCustomQuoteNode {
|
|
|
|
return {
|
|
|
|
...super.exportJSON(),
|
|
|
|
type: 'custom-quote',
|
|
|
|
version: 1,
|
|
|
|
id: this.__id,
|
2024-08-18 23:51:08 +08:00
|
|
|
alignment: this.__alignment,
|
2024-08-11 23:08:51 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static importJSON(serializedNode: SerializedCustomQuoteNode): CustomQuoteNode {
|
|
|
|
const node = $createCustomQuoteNode();
|
|
|
|
node.setId(serializedNode.id);
|
2024-08-18 23:51:08 +08:00
|
|
|
node.setAlignment(serializedNode.alignment);
|
2024-08-11 23:08:51 +08:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
static importDOM(): DOMConversionMap | null {
|
|
|
|
return {
|
|
|
|
blockquote: (node: Node) => ({
|
|
|
|
conversion: $convertBlockquoteElement,
|
|
|
|
priority: 0,
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function $convertBlockquoteElement(element: HTMLElement): DOMConversionOutput {
|
|
|
|
const node = $createCustomQuoteNode();
|
2024-08-18 23:51:08 +08:00
|
|
|
setCommonBlockPropsFromElement(element, node);
|
2024-08-11 23:08:51 +08:00
|
|
|
return {node};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function $createCustomQuoteNode() {
|
|
|
|
return new CustomQuoteNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
export function $isCustomQuoteNode(node: LexicalNode | null | undefined): node is CustomQuoteNode {
|
|
|
|
return node instanceof CustomQuoteNode;
|
|
|
|
}
|