X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/ec965f28c09bf18cab2b615716d902d31ff49cfd..refs/pull/5312/head:/resources/js/wysiwyg/nodes/custom-quote.ts diff --git a/resources/js/wysiwyg/nodes/custom-quote.ts b/resources/js/wysiwyg/nodes/custom-quote.ts index 58c62f769..39ae7bf8a 100644 --- a/resources/js/wysiwyg/nodes/custom-quote.ts +++ b/resources/js/wysiwyg/nodes/custom-quote.ts @@ -1,19 +1,25 @@ import { DOMConversionMap, - DOMConversionOutput, ElementFormatType, + DOMConversionOutput, LexicalNode, Spread } from "lexical"; import {EditorConfig} from "lexical/LexicalEditor"; import {QuoteNode, SerializedQuoteNode} from "@lexical/rich-text"; +import { + CommonBlockAlignment, commonPropertiesDifferent, deserializeCommonBlockNode, + SerializedCommonBlockNode, + setCommonBlockPropsFromElement, + updateElementWithCommonBlockProps +} from "./_common"; -export type SerializedCustomQuoteNode = Spread<{ - id: string; -}, SerializedQuoteNode> +export type SerializedCustomQuoteNode = Spread export class CustomQuoteNode extends QuoteNode { __id: string = ''; + __alignment: CommonBlockAlignment = ''; + __inset: number = 0; static getType() { return 'custom-quote'; @@ -29,33 +35,58 @@ export class CustomQuoteNode extends QuoteNode { return self.__id; } + setAlignment(alignment: CommonBlockAlignment) { + const self = this.getWritable(); + self.__alignment = alignment; + } + + getAlignment(): CommonBlockAlignment { + const self = this.getLatest(); + return self.__alignment; + } + + setInset(size: number) { + const self = this.getWritable(); + self.__inset = size; + } + + getInset(): number { + const self = this.getLatest(); + return self.__inset; + } + static clone(node: CustomQuoteNode) { const newNode = new CustomQuoteNode(node.__key); newNode.__id = node.__id; + newNode.__alignment = node.__alignment; + newNode.__inset = node.__inset; return newNode; } createDOM(config: EditorConfig): HTMLElement { const dom = super.createDOM(config); - if (this.__id) { - dom.setAttribute('id', this.__id); - } - + updateElementWithCommonBlockProps(dom, this); return dom; } + updateDOM(prevNode: CustomQuoteNode): boolean { + return commonPropertiesDifferent(prevNode, this); + } + exportJSON(): SerializedCustomQuoteNode { return { ...super.exportJSON(), type: 'custom-quote', version: 1, id: this.__id, + alignment: this.__alignment, + inset: this.__inset, }; } static importJSON(serializedNode: SerializedCustomQuoteNode): CustomQuoteNode { const node = $createCustomQuoteNode(); - node.setId(serializedNode.id); + deserializeCommonBlockNode(serializedNode, node); return node; } @@ -71,12 +102,7 @@ export class CustomQuoteNode extends QuoteNode { function $convertBlockquoteElement(element: HTMLElement): DOMConversionOutput { const node = $createCustomQuoteNode(); - if (element.style !== null) { - node.setFormat(element.style.textAlign as ElementFormatType); - } - if (element.id) { - node.setId(element.id); - } + setCommonBlockPropsFromElement(element, node); return {node}; }