X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/ec965f28c09bf18cab2b615716d902d31ff49cfd..refs/pull/5313/head:/resources/js/wysiwyg/nodes/callout.ts diff --git a/resources/js/wysiwyg/nodes/callout.ts b/resources/js/wysiwyg/nodes/callout.ts index b720b5c43..cfe32ec85 100644 --- a/resources/js/wysiwyg/nodes/callout.ts +++ b/resources/js/wysiwyg/nodes/callout.ts @@ -5,22 +5,28 @@ import { ElementNode, LexicalEditor, LexicalNode, - ParagraphNode, SerializedElementNode, Spread + ParagraphNode, Spread } from 'lexical'; import type {EditorConfig} from "lexical/LexicalEditor"; import type {RangeSelection} from "lexical/LexicalSelection"; -import {el} from "../utils/dom"; +import { + CommonBlockAlignment, commonPropertiesDifferent, deserializeCommonBlockNode, + SerializedCommonBlockNode, + setCommonBlockPropsFromElement, + updateElementWithCommonBlockProps +} from "./_common"; export type CalloutCategory = 'info' | 'danger' | 'warning' | 'success'; export type SerializedCalloutNode = Spread<{ category: CalloutCategory; - id: string; -}, SerializedElementNode> +}, SerializedCommonBlockNode> export class CalloutNode extends ElementNode { __id: string = ''; __category: CalloutCategory = 'info'; + __alignment: CommonBlockAlignment = ''; + __inset: number = 0; static getType() { return 'callout'; @@ -29,6 +35,8 @@ export class CalloutNode extends ElementNode { static clone(node: CalloutNode) { const newNode = new CalloutNode(node.__category, node.__key); newNode.__id = node.__id; + newNode.__alignment = node.__alignment; + newNode.__inset = node.__inset; return newNode; } @@ -57,19 +65,36 @@ export class CalloutNode extends ElementNode { 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; + } + createDOM(_config: EditorConfig, _editor: LexicalEditor) { const element = document.createElement('p'); element.classList.add('callout', this.__category || ''); - if (this.__id) { - element.setAttribute('id', this.__id); - } + updateElementWithCommonBlockProps(element, this); return element; } - updateDOM(prevNode: unknown, dom: HTMLElement) { - // Returning false tells Lexical that this node does not need its - // DOM element replacing with a new copy from createDOM. - return false; + updateDOM(prevNode: CalloutNode): boolean { + return prevNode.__category !== this.__category || + commonPropertiesDifferent(prevNode, this); } insertNewAfter(selection: RangeSelection, restoreSelection?: boolean): CalloutNode|ParagraphNode { @@ -106,9 +131,7 @@ export class CalloutNode extends ElementNode { } const node = new CalloutNode(category); - if (element.id) { - node.setId(element.id); - } + setCommonBlockPropsFromElement(element, node); return { node, @@ -129,12 +152,14 @@ export class CalloutNode extends ElementNode { version: 1, category: this.__category, id: this.__id, + alignment: this.__alignment, + inset: this.__inset, }; } static importJSON(serializedNode: SerializedCalloutNode): CalloutNode { const node = $createCalloutNode(serializedNode.category); - node.setId(serializedNode.id); + deserializeCommonBlockNode(serializedNode, node); return node; }