UNPKG

@plebjs/editorjs-nowrap-tool

Version:
95 lines (94 loc) 2.71 kB
(function(){"use strict";try{if(typeof document<"u"){var e=document.createElement("style");e.appendChild(document.createTextNode(".cdx-nowrap{display:inline;white-space:nowrap}")),document.head.appendChild(e)}}catch(n){console.error("vite-plugin-css-injected-by-js",n)}})(); class s { /** * Class name for term-tag * * @type {string} */ static get CSS() { return "cdx-nowrap"; } /** * @param {{api: object}} - Editor.js API */ constructor({ api: t }) { this.api = t, this.button = null, this.tag = "SPAN", this.iconClasses = { base: this.api.styles.inlineToolButton, active: this.api.styles.inlineToolButtonActive }; } /** * Specifies Tool as Inline Toolbar Tool * * @return {boolean} */ static get isInline() { return !0; } /** * Create button element for Toolbar * * @return {HTMLElement} */ render() { return this.button = document.createElement("button"), this.button.type = "button", this.button.classList.add(this.iconClasses.base), this.button.innerHTML = this.toolboxIcon, this.button; } /** * Wrap/Unwrap selected fragment * * @param {Range} range - selected fragment */ surround(t) { if (!t) return; let e = this.api.selection.findParentTag(this.tag, s.CSS); e ? this.unwrap(e) : this.wrap(t); } /** * Wrap selection with term-tag * * @param {Range} range - selected fragment */ wrap(t) { let e = document.createElement(this.tag); e.classList.add(s.CSS), e.appendChild(t.extractContents()), t.insertNode(e), this.api.selection.expandToTag(e); } /** * Unwrap term-tag * * @param {HTMLElement} termWrapper - term wrapper tag */ unwrap(t) { this.api.selection.expandToTag(t); let e = window.getSelection(), n = e.getRangeAt(0), i = n.extractContents(); t.parentNode.removeChild(t), n.insertNode(i), e.removeAllRanges(), e.addRange(n); } /** * Check and change Term's state for current selection */ checkState() { const t = this.api.selection.findParentTag(this.tag, s.CSS); this.button.classList.toggle(this.iconClasses.active, !!t); } /** * Get Tool icon's SVG * @return {string} */ get toolboxIcon() { return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"><path d="M4 6l10 0"></path> <path d="M4 18l10 0"></path><path d="M4 12h17l-3 -3m0 6l3 -3"></path></svg>'; } /** * Sanitizer rule * @return {{nowrap: {class: string}}} */ static get sanitize() { return { span: { class: s.CSS } }; } } export { s as default };