UNPKG

editorjs-inline-formatter

Version:

Inline formatter tool for editorjs

3 lines (2 loc) 4.15 kB
(function(){"use strict";try{if(typeof document<"u"){var e=document.createElement("style");e.appendChild(document.createTextNode(".speech-word[data-italic=true]{font-style:italic}.speech-word[data-bold=true]{font-weight:700}.speech-word[data-underline=true]{text-decoration:underline}")),document.head.appendChild(e)}}catch(t){console.error("vite-plugin-css-injected-by-js",t)}})(); (function(s,l){typeof exports=="object"&&typeof module<"u"?l(exports):typeof define=="function"&&define.amd?define(["exports"],l):(s=typeof globalThis<"u"?globalThis:s||self,l(s.EditorjsInlineFormatter={}))})(this,function(s){"use strict";const l=({shortcut:N,formatStyle:g,toolboxIcon:C})=>{var a;return a=class{constructor({api:t}){this.button=null,this.api=t,this.button=null,this.formatStyle=g,this.attribute=`data-${this.formatStyle}`.toLowerCase(),this.iconClasses={base:this.api.styles.inlineToolButton,active:this.api.styles.inlineToolButtonActive}}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}surround(t){if(!t)return;this.expandSelectionToNode(t);const n=t.extractContents(),o=Array.from(n.querySelectorAll("span")),i=o.some(e=>e.getAttribute(this.attribute));o.forEach(e=>{i?e.removeAttribute(this.attribute):e.setAttribute(this.attribute,"true")}),t.insertNode(n)}expandSelectionToNode(t){var r,u;const n=t.startContainer,o=t.endContainer,e=t.toString().split(/\s+/).filter(c=>!!c);if(e.length>0){const c=e[0],d=e[e.length-1];n.parentNode&&n.nodeType===Node.TEXT_NODE&&((r=n.textContent)!=null&&r.includes(c))&&t.setStartBefore(n.parentNode),o.parentNode&&o.nodeType===Node.TEXT_NODE&&((u=o.textContent)!=null&&u.includes(d))&&t.setEndAfter(o.parentNode)}}checkState(t){var i,e;const o=this.getNodesFromSelection(t).some(r=>r.nodeType===Node.ELEMENT_NODE&&r.getAttribute(this.attribute)==="true");return(e=(i=this.button)==null?void 0:i.classList)==null||e.toggle(this.iconClasses.active,o),o}getNodesFromSelection(t){if(!t||!t.anchorNode||!t.focusNode)return[];const n=t.getRangeAt(0),{commonAncestorContainer:o,startContainer:i,endContainer:e}=n;if(o.nodeType===Node.TEXT_NODE)return o.parentNode?[o.parentNode]:[];if(i.nodeType===Node.TEXT_NODE&&e.nodeType===Node.TEXT_NODE){const r=i.parentNode,u=e.parentNode;if(!(r instanceof Element)||!(u instanceof Element))return[];const c=[r];let d=r.nextElementSibling;for(;d&&d!==u.nextElementSibling;)c.push(d),d=d.nextElementSibling;return c}return Array.from(i.childNodes).slice(n.startOffset,n.endOffset)}get toolboxIcon(){return C}get shortcut(){return N}},a.isInline=!0,a},h='<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-width="2" d="M9 12L9 7.1C9 7.04477 9.04477 7 9.1 7H10.4C11.5 7 14 7.1 14 9.5C14 9.5 14 12 11 12M9 12V16.8C9 16.9105 9.08954 17 9.2 17H12.5C14 17 15 16 15 14.5C15 11.7046 11 12 11 12M9 12H11"/></svg>',f='<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-width="2" d="M13.34 10C12.4223 12.7337 11 17 11 17"/><path stroke="currentColor" stroke-linecap="round" stroke-width="2" d="M14.21 7H14.2"/></svg>',p='<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 7.5V11.5C9 12.2956 9.31607 13.0587 9.87868 13.6213C10.4413 14.1839 11.2044 14.5 12 14.5C12.7956 14.5 13.5587 14.1839 14.1213 13.6213C14.6839 13.0587 15 12.2956 15 11.5V7.5"/><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7.71429 18H16.2857"/></svg>',m=l({shortcut:"CMD+I",formatStyle:"italic",toolboxIcon:f}),T=l({shortcut:"CMD+B",formatStyle:"bold",toolboxIcon:h}),b=l({formatStyle:"underline",toolboxIcon:p});s.ItalicInlineTool=m,s.StrongInlineTool=T,s.UnderlineInlineTool=b,s.default=l,Object.defineProperties(s,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});