UNPKG

@opentiny/fluent-editor

Version:

A rich text editor based on Quill 2.0, which extends rich modules and formats on the basis of Quill. It's powerful and out-of-the-box.

95 lines (94 loc) 2.71 kB
import Quill from "quill"; import { isNullOrUndefined } from "../../../config/editor.utils.es.js"; import { CELL_IDENTITY_KEYS, CELL_ATTRIBUTES } from "../table-config.es.js"; const OriginHeader = Quill.import("formats/header"); class Header extends OriginHeader { static create(value) { if (typeof value === "string") { value = { value }; } const node = super.create(value.value); CELL_IDENTITY_KEYS.forEach((key) => { if (value[key]) { node.setAttribute(`data-${key}`, value[key]); } }); CELL_ATTRIBUTES.forEach((key) => { if (value[key]) { node.setAttribute(`data-${key}`, value[key]); } }); return node; } static formats(domNode) { const formats = {}; formats.value = this.tagName.indexOf(domNode.tagName) + 1; return CELL_ATTRIBUTES.concat(CELL_IDENTITY_KEYS).reduce((tableFormats, attribute) => { if (domNode.hasAttribute(`data-${attribute}`)) { tableFormats[attribute] = domNode.getAttribute(`data-${attribute}`) || void 0; } return tableFormats; }, formats); } format(name, value) { if (Object.prototype.toString.call(value) === "[object Object]") { value = value.value; } const headerDom = Header.formats(this.domNode); const { row, cell, rowspan, colspan } = headerDom; if (name === Header.blotName) { if (value) { super.format(name, { value, row, cell, rowspan, colspan }); } else { if (row) { this.replaceWith("table-cell-line", { row, cell, rowspan, colspan }); } else { super.format(name, value); } } } else { super.format(name, value); } } optimize(_context) { const { row, cell, rowspan, colspan } = Header.formats(this.domNode); if (row && this.parent.statics.blotName !== "table") { this.wrap("table", { row, cell, colspan, rowspan }); } this.enforceAllowedChildren(); if (!isNullOrUndefined(this.uiNode) && this.uiNode !== this.domNode.firstChild) { this.domNode.insertBefore(this.uiNode, this.domNode.firstChild); } if (this.children.length === 0) { if (!isNullOrUndefined(this.statics.defaultChild)) { const child = this.scroll.create(this.statics.defaultChild.blotName); this.appendChild(child); } else { this.remove(); } } this.cache = {}; } } Header.blotName = "header"; Header.tagName = ["H1", "H2", "H3", "H4", "H5", "H6"]; export { Header as default }; //# sourceMappingURL=header.es.js.map