package.dist.header.mjs Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of header Show documentation
Show all versions of header Show documentation
Heading Tool for Editor.js
The newest version!
(function(){"use strict";try{if(typeof document<"u"){var e=document.createElement("style");e.appendChild(document.createTextNode(".ce-header{padding:.6em 0 3px;margin:0;line-height:1.25em;outline:none}.ce-header p,.ce-header div{padding:0!important;margin:0!important}")),document.head.appendChild(e)}}catch(n){console.error("vite-plugin-css-injected-by-js",n)}})();
const a = '', l = '', o = '', h = '', d = '', u = '', g = '';
/**
* Header block for the Editor.js.
*
* @author CodeX ([email protected])
* @copyright CodeX 2018
* @license MIT
* @version 2.0.0
*/
class v {
constructor({ data: e, config: t, api: s, readOnly: r }) {
this.api = s, this.readOnly = r, this._settings = t, this._data = this.normalizeData(e), this._element = this.getTag();
}
/**
* Styles
*/
get _CSS() {
return {
block: this.api.styles.block,
wrapper: "ce-header"
};
}
/**
* Check if data is valid
*
* @param {any} data - data to check
* @returns {data is HeaderData}
* @private
*/
isHeaderData(e) {
return e.text !== void 0;
}
/**
* Normalize input data
*
* @param {HeaderData} data - saved data to process
*
* @returns {HeaderData}
* @private
*/
normalizeData(e) {
const t = { text: "", level: this.defaultLevel.number };
return this.isHeaderData(e) && (t.text = e.text || "", e.level !== void 0 && !isNaN(parseInt(e.level.toString())) && (t.level = parseInt(e.level.toString()))), t;
}
/**
* Return Tool's view
*
* @returns {HTMLHeadingElement}
* @public
*/
render() {
return this._element;
}
/**
* Returns header block tunes config
*
* @returns {Array}
*/
renderSettings() {
return this.levels.map((e) => ({
icon: e.svg,
label: this.api.i18n.t(`Heading ${e.number}`),
onActivate: () => this.setLevel(e.number),
closeOnActivate: !0,
isActive: this.currentLevel.number === e.number,
render: () => document.createElement("div")
}));
}
/**
* Callback for Block's settings buttons
*
* @param {number} level - level to set
*/
setLevel(e) {
this.data = {
level: e,
text: this.data.text
};
}
/**
* Method that specified how to merge two Text blocks.
* Called by Editor.js by backspace at the beginning of the Block
*
* @param {HeaderData} data - saved data to merger with current block
* @public
*/
merge(e) {
this._element.insertAdjacentHTML("beforeend", e.text);
}
/**
* Validate Text block data:
* - check for emptiness
*
* @param {HeaderData} blockData — data received after saving
* @returns {boolean} false if saved data is not correct, otherwise true
* @public
*/
validate(e) {
return e.text.trim() !== "";
}
/**
* Extract Tool's data from the view
*
* @param {HTMLHeadingElement} toolsContent - Text tools rendered view
* @returns {HeaderData} - saved data
* @public
*/
save(e) {
return {
text: e.innerHTML,
level: this.currentLevel.number
};
}
/**
* Allow Header to be converted to/from other blocks
*/
static get conversionConfig() {
return {
export: "text",
// use 'text' property for other blocks
import: "text"
// fill 'text' property from other block's export string
};
}
/**
* Sanitizer Rules
*/
static get sanitize() {
return {
level: !1,
text: {}
};
}
/**
* Returns true to notify core that read-only is supported
*
* @returns {boolean}
*/
static get isReadOnlySupported() {
return !0;
}
/**
* Get current Tools`s data
*
* @returns {HeaderData} Current data
* @private
*/
get data() {
return this._data.text = this._element.innerHTML, this._data.level = this.currentLevel.number, this._data;
}
/**
* Store data in plugin:
* - at the this._data property
* - at the HTML
*
* @param {HeaderData} data — data to set
* @private
*/
set data(e) {
if (this._data = this.normalizeData(e), e.level !== void 0 && this._element.parentNode) {
const t = this.getTag();
t.innerHTML = this._element.innerHTML, this._element.parentNode.replaceChild(t, this._element), this._element = t;
}
e.text !== void 0 && (this._element.innerHTML = this._data.text || "");
}
/**
* Get tag for target level
* By default returns second-leveled header
*
* @returns {HTMLElement}
*/
getTag() {
const e = document.createElement(this.currentLevel.tag);
return e.innerHTML = this._data.text || "", e.classList.add(this._CSS.wrapper), e.contentEditable = this.readOnly ? "false" : "true", e.dataset.placeholder = this.api.i18n.t(this._settings.placeholder || ""), e;
}
/**
* Get current level
*
* @returns {level}
*/
get currentLevel() {
let e = this.levels.find((t) => t.number === this._data.level);
return e || (e = this.defaultLevel), e;
}
/**
* Return default level
*
* @returns {level}
*/
get defaultLevel() {
if (this._settings.defaultLevel) {
const e = this.levels.find((t) => t.number === this._settings.defaultLevel);
if (e)
return e;
console.warn("(ง'̀-'́)ง Heading Tool: the default level specified was not found in available levels");
}
return this.levels[1];
}
/**
* @typedef {object} level
* @property {number} number - level number
* @property {string} tag - tag corresponds with level number
* @property {string} svg - icon
*/
/**
* Available header levels
*
* @returns {level[]}
*/
get levels() {
const e = [
{
number: 1,
tag: "H1",
svg: a
},
{
number: 2,
tag: "H2",
svg: l
},
{
number: 3,
tag: "H3",
svg: o
},
{
number: 4,
tag: "H4",
svg: h
},
{
number: 5,
tag: "H5",
svg: d
},
{
number: 6,
tag: "H6",
svg: u
}
];
return this._settings.levels ? e.filter(
(t) => this._settings.levels.includes(t.number)
) : e;
}
/**
* Handle H1-H6 tags on paste to substitute it with header Tool
*
* @param {PasteEvent} event - event with pasted content
*/
onPaste(e) {
const t = e.detail;
if ("data" in t) {
const s = t.data;
let r = this.defaultLevel.number;
switch (s.tagName) {
case "H1":
r = 1;
break;
case "H2":
r = 2;
break;
case "H3":
r = 3;
break;
case "H4":
r = 4;
break;
case "H5":
r = 5;
break;
case "H6":
r = 6;
break;
}
this._settings.levels && (r = this._settings.levels.reduce((n, i) => Math.abs(i - r) < Math.abs(n - r) ? i : n)), this.data = {
level: r,
text: s.innerHTML
};
}
}
/**
* Used by Editor.js paste handling API.
* Provides configuration to handle H1-H6 tags.
*
* @returns {{handler: (function(HTMLElement): {text: string}), tags: string[]}}
*/
static get pasteConfig() {
return {
tags: ["H1", "H2", "H3", "H4", "H5", "H6"]
};
}
/**
* Get Tool toolbox settings
* icon - Tool icon's SVG
* title - title to show in toolbox
*
* @returns {{icon: string, title: string}}
*/
static get toolbox() {
return {
icon: g,
title: "Heading"
};
}
}
export {
v as default
};
© 2015 - 2024 Weber Informatics LLC | Privacy Policy