UNPKG

sp-editor

Version:

SpEditor is a HTML5 rich text editor in smartphone browsers

24 lines (23 loc) 27.6 kB
/*! * sp-editor version 1.1.1 * Author: capricorncd * Released under the MIT License * Repository: https://github.com/capricorncd/zx-editor * Copyright © 2018-present, capricorncd/ Xing Zhong. */ (function(E,_){typeof exports=="object"&&typeof module<"u"?_(exports):typeof define=="function"&&define.amd?define(["exports"],_):(E=typeof globalThis<"u"?globalThis:E||self,_(E.SpEditor={}))})(this,function(E){"use strict";var rt=Object.defineProperty;var ot=(E,_,$)=>_ in E?rt(E,_,{enumerable:!0,configurable:!0,writable:!0,value:$}):E[_]=$;var d=(E,_,$)=>(ot(E,typeof _!="symbol"?_+"":_,$),$);class _{constructor(){this._events={}}on(e,i){return!e||!i||typeof i!="function"?this:(this._events[e]||(this._events[e]=[]),this._events[e].push(i),this)}once(e,i){const n=(...s)=>{i.apply(this,s),this.off(e,n)};return this.on(e,n)}emit(e,...i){const n=this._events[e];if(!n)return this;for(let s=0;s<n.length;s++)try{n[s].apply(this,i)}catch(r){this.emit("error",r,"emit")}return this}off(e,i){if(!this._events[e])return this;const n=this._events[e];if(typeof i=="function"){const s=n.findIndex(r=>r===i);s>=0&&n.splice(s,1)}else this._events[e].length=0;return this._removeEmpty(e),this}_removeEmpty(e){this._events[e].length||delete this._events[e]}removeAllListeners(){Object.keys(this._events).forEach(e=>this.off(e))}}/*! * zx-sml version 0.7.5 * Author: Capricorncd <capricorncd@qq.com> * Repository: https://github.com/capricorncd/zx-sml * Released on: 2023-05-17 20:41:58 (GMT+0900) */var $=Object.defineProperty,F=Object.getOwnPropertySymbols,Ee=Object.prototype.hasOwnProperty,_e=Object.prototype.propertyIsEnumerable,U=(t,e,i)=>e in t?$(t,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):t[e]=i,W=(t,e)=>{for(var i in e||(e={}))Ee.call(e,i)&&U(t,i,e[i]);if(F)for(var i of F(e))_e.call(e,i)&&U(t,i,e[i]);return t};function ve(t){return Array.isArray(t)}function z(t){return typeof t=="object"&&t!==null&&!ve(t)}function G(t="",e="-"){return t.replace(/[A-Z]/g,(i,n)=>`${n>0?e:""}${i.toLowerCase()}`)}function V(t="",e=!1){const i=t.replace(/[-_\s](\w)/g,(n,s)=>s.toUpperCase());return e?i.replace(/^\w/,n=>n.toUpperCase()):i}function q(t){return t.replace(/^-?[1-9]\d{0,2}(,\d{3})+/,e=>e.replace(/,/g,""))}function Y(t,e=!1){if(typeof t=="number")return t;if(typeof t=="string"){if(!e&&/^(-?\d+(?:\.\d+)?)\D*/.test(q(t)))return Y(RegExp.$1,!0);const i=Number(t);return isNaN(i)?0:i}return 0}function be(t){if(typeof t=="number")return[t,""];const e=q(t).match(/^(-?\d+(?:\.\d+)?)(.*)$/);return e?[Y(e[1],!0),e[2]]:[0,""]}function K(t){return typeof t=="string"?t:t===null||typeof t>"u"?"":Array.isArray(t)?t.map(K).join(" "):typeof t=="object"?Object.keys(t).filter(e=>t[e]).join(" "):String(t)}function Ne(...t){return t.map(K).filter(e=>!!e).join(" ")}function C(t,e=0){return Array.prototype.slice.call(t,e)}function Z(t={},e=!1){const i=e?V:G,n={};for(const[s,r]of Object.entries(t))n[i(s)]=z(r)?Z(r,e):r;return n}function b(t,e=document){return t?t instanceof HTMLElement?t:e.querySelector(t):null}function L(t,e=document){return C(e.querySelectorAll(t))}function f(t,e={},i){const n=document.createElement(t);for(const[s,r]of Object.entries(e))n.setAttribute(G(s),s==="style"&&z(r)?H(r):String(r));return i&&(Array.isArray(i)||(i=[i]),i.forEach(s=>{if(typeof s=="string"){const r=f("div");r.innerHTML=s,n.append(...r.childNodes)}else n.append(s)})),n}function H(...t){const e=t.reduce((n,s)=>W(W({},n),Z(s)),{}),i=[];for(const[n,s]of Object.entries(e))s===""||typeof s>"u"||s===null||i.push(`${n}:${s}`);return i.join(";")}const A=(t,e="style")=>t?(t.getAttribute(e)||"").split(/\s?;\s?/).reduce((n,s)=>{const[r,o]=s.split(/\s?:\s?/);return r&&(n[V(r)]=o),n},{}):{},O=t=>document.createTextNode(t),J=t=>{if(!t)return null;if(typeof t=="string")return O(t);const{tag:e,attrs:i,child:n}=t;if(!e&&!i&&!n)return null;const s=f(e||"div",i);if(Array.isArray(n)&&n.length){let r;n.forEach(o=>{r=J(o),r&&s.appendChild(r)})}else n&&typeof n=="string"&&s.appendChild(O(n));return s},$e=(t,e)=>{t.classList.add(e)},Ce=(t,e)=>{t.classList.remove(e)},Le="sp-editor__editor",D="is-empty",Q="SECTION",xe="BR",P=[Q,"H1","H2","H3","H4","H5","BLOCKQUOTE","UL","OL"];function x(t,e,i){return t.replace(new RegExp("(^<"+e+")|("+e+">$)","gi"),n=>n.replace(new RegExp(e,"i"),i.toLowerCase()))}function Te(t){return t.replace(/<li[^>]*>(.+)<\/li>/gi,"$1")}function I(t){const e=typeof t=="string"?t:t.nodeName;return/^UL|OL$/i.test(e)}function X(t){if(!t)return!1;const e=C(t.childNodes);return e.length>0&&e.every(i=>i.nodeName==="BR")}function R(t){return typeof t!="string"&&t.nodeName&&(t=t.outerHTML),typeof t=="string"&&/^<(\w+)[^>]*>.*<\/\1>$/.test(t.replace(/\n/g,""))}function ee(t){return["PICTURE","VIDEO","AUDIO","CANVAS"].includes(t.nodeName)}function Se(t){return["IMG"].includes(t.nodeName)||ee(t)}function te(t){if(Se(t))return!0;for(let e=0;e<t.children.length;e++)if(te(t.children[e]))return!0;return!1}const He=(t,e)=>{const i={minHeight:t.minHeight,"--placeholder":JSON.stringify(t.placeholder),"--placeholder-color":t.placeholderColor,"--line-height":t.lineHeight,"--paragraph-spacing":t.paragraphTailSpacing,"--padding-bottom":t.paddingBottom,...t.styles};t.caretColor&&(i.caretColor=t.caretColor),t.textColor&&(i.color=t.textColor);const n={class:`${Le} ${D}`,style:H(i)};return t.editable&&(n.contenteditable="true"),f("div",n,e)},ie=(t,e)=>{var a,l,v,u,c,w,m,h,p;if(!t)return null;const i=t.nodeName,n=e.toUpperCase();if(i===n)return null;const s=f(e),r=t.parentElement;let o;if(i==="LI"&&I(r)){if(s.innerHTML=x(t.outerHTML,i,n),o=s.firstChild,r.childElementCount>1)if(r.firstElementChild===t)(a=r.parentElement)==null||a.insertBefore(o,r);else if(r.lastElementChild===t){const g=(l=r.parentElement)==null?void 0:l.nextElementSibling;g?(v=g.parentElement)==null||v.insertBefore(o,g):(u=r.parentElement)==null||u.append(o)}else{const g=C(r.children),y=f(r.nodeName);let N=g.shift();for(;N&&N!==t;)y.append(N),N=g.shift();(c=r.parentElement)==null||c.insertBefore(y,r),(w=r.parentElement)==null||w.insertBefore(o,r),r.removeChild(t)}else(m=r.parentElement)==null||m.insertBefore(o,r),(h=r.parentElement)==null||h.removeChild(r);return o}if(/UL|OL/.test(n)){const g=t.previousElementSibling,y=t.nextElementSibling;if(g&&I(g)){if(s.innerHTML=x(t.outerHTML,i,"li"),o=s.firstChild,g.append(o),r==null||r.removeChild(t),y&&y.nodeName===g.nodeName){const N=C(y.children);g.append(...N),(p=y.parentElement)==null||p.removeChild(y)}}else y&&I(y)?(s.innerHTML=x(t.outerHTML,i,"li"),o=s.firstChild,y.insertBefore(o,y.firstElementChild),r==null||r.removeChild(t)):(o=s,s.innerHTML=x(t.outerHTML,i,"li"),r==null||r.replaceChild(o,t))}else s.innerHTML=Te(x(t.outerHTML,i,n)),o=s.firstChild,r==null||r.replaceChild(o,t);return o},M=t=>{var e;!((e=t.innerText)!=null&&e.trim())&&!te(t)?t.classList.add(D):t.classList.remove(D)};function Ae(t,e,i=!1){var n;for(;t&&e!==t;){if(!i&&t.nodeName==="LI"&&((n=t.parentElement)==null?void 0:n.parentElement)===e||t.parentElement===e)return t;t=t.parentElement}return e.lastElementChild}const Oe={editable:!0,minHeight:"50vh",paddingBottom:"50vh",placeholder:"请在此输入内容..",placeholderColor:"#999",lineHeight:1.5,childNodeName:Q,allowedNodeNames:P,paragraphTailSpacing:"10px",caretColor:"",textColor:"#333333",customPasteHandler:void 0,insertTextToNewParagraph:!1},at="";class De extends _{constructor(i){super();d(this,"version");d(this,"options");d(this,"$editor");d(this,"_eventHandler");d(this,"allowedNodeNames");d(this,"blankLine");d(this,"range");d(this,"_pasteHandler");const n=typeof i.container=="string"?b(i.container):i.container;if(!n)throw new Error(`Can't found '${i.container}' Node in document!`);this.version="1.1.1",this.options={...Oe,...i},this.allowedNodeNames=(this.options.allowedNodeNames||P).map(r=>r.toUpperCase());const s=this.options.childNodeName.toUpperCase();this.options.childNodeName=s,this.blankLine=`<${s}><br></${s}>`,this.allowedNodeNames.includes(s)||this.allowedNodeNames.push(s),this.$editor=He(this.options,this.blankLine),n.append(this.$editor),this.range=new Range,this.range.setStart(this.$editor.children[0],0),this._eventHandler=r=>{var a;const o=r.type;if(o==="click"){const l=(a=window.getSelection())==null?void 0:a.getRangeAt(0);l&&(this.range=l)}else o==="blur"&&this._verifyChild();this.emit(o==="input"?"change":o,r),M(this.$editor)},this._pasteHandler=r=>{var a;if(typeof this.options.customPasteHandler=="function")return this.options.customPasteHandler(r);r.preventDefault();const o=(a=r.clipboardData)==null?void 0:a.getData("text");this.insert(o)},this._initEvents()}_initEvents(){this.$editor.addEventListener("focus",this._eventHandler),this.$editor.addEventListener("blur",this._eventHandler),this.$editor.addEventListener("input",this._eventHandler),this.$editor.addEventListener("click",this._eventHandler),this.$editor.addEventListener("paste",this._pasteHandler)}setRangeWithNode(i){const n=new Range;return n.setStart(i===this.$editor?this.$editor.childNodes[this.$editor.childNodes.length-1]:i,0),this.range=n,n}use(i,n){typeof i.install=="function"&&i.install(this,n)}setHtml(i){this.$editor.innerHTML=this.blankLine,this.insert(i,!0),M(this.$editor)}getHtml(i){const n=this.$editor.innerHTML;if(i)return n;const s=this.options.childNodeName;return n.replace(new RegExp(`(<${s}><br\\s?\\/?><\\/${s}>)+$`,"i"),"")}insert(i,n=!1){if(!i)return;const{childNodeName:s,insertTextToNewParagraph:r}=this.options;if(i instanceof HTMLElement)this._insertEl(i);else{const o=f("div",{},i),a=C(o.childNodes);if(!n&&!r&&a.every(l=>l.nodeType===Node.TEXT_NODE))return this._insertText(i);a.forEach(l=>{l.nodeType===Node.ELEMENT_NODE?l.nodeName===xe?this._insertEl(f(s,{},"<br/>")):this._insertEl(l):l.textContent&&this._insertEl(f(s,{},l.textContent))})}this._verifyChild(),this._dispatchChange(),M(this.$editor)}_insertEl(i){const n=this.getCursorElement();X(n)?R(i.outerHTML)?this.$editor.insertBefore(i,n):n===this.$editor.children[this.$editor.children.length-1]?this.$editor.insertBefore(f(this.options.childNodeName,{},i),n):(n.innerHTML="",n.append(i)):(R(i.outerHTML)||(i=f(this.options.childNodeName,{},i)),n.nextElementSibling?this.$editor.insertBefore(i,n.nextElementSibling):this.$editor.append(i)),this.setRangeWithNode(i)}_insertText(i){if(!i)return;const n=window.getSelection();if(!(n==null?void 0:n.rangeCount))return this.insert(i,!0);n.deleteFromDocument();const r=O(i);n.getRangeAt(0).insertNode(r),n.setPosition(r,i.length),this.range=n.getRangeAt(0),n.collapseToEnd()}_verifyChild(){const i=this.options.childNodeName;let n,s,r=!1;const o=this.$editor.childNodes;for(let a=0;a<o.length;a++)s=null,n=o[a],r=n===this.range.commonAncestorContainer,n.nodeType===Node.ELEMENT_NODE&&R(n)&&!ee(n)?this.allowedNodeNames.includes(n.nodeName)||(s=ie(n,i)):(s=f(i,{},n.cloneNode(!0)),this.$editor.replaceChild(s,n)),r&&s&&this.setRangeWithNode(s);X(this.$editor.lastElementChild)||this.$editor.append(f(i,{},"<br>"))}changeNodeName(i){if(i=(i||this.options.childNodeName).toUpperCase(),!this.allowedNodeNames.includes(i))return!1;const n=this.getCursorElement(),s=ie(n,i);return s?(this.setRangeWithNode(s),this._dispatchChange(),!0):!1}changeStyles(i,n){const s=this.getCursorElement(!0);if(s){const r=A(s);if(i){const o=typeof i=="string"?{[i]:n}:i;s.setAttribute("style",H(r,o))}else{if(!Object.keys(r).length)return;s.removeAttribute("style")}this._dispatchChange()}}_dispatchChange(){this.$editor.dispatchEvent(new InputEvent("input"))}getStyles(){return A(this.getCursorElement())}getCursorElement(i=!1){return Ae(this.range.commonAncestorContainer,this.$editor,i)}destroy(){this.$editor.removeEventListener("focus",this._eventHandler),this.$editor.removeEventListener("blur",this._eventHandler),this.$editor.removeEventListener("input",this._eventHandler),this.$editor.removeEventListener("paste",this._pasteHandler),this.removeAllListeners()}}const Pe=["#333333","#d0d0d0","#ff583d","#fdaa25","#44c67b","#14b2e0","#b065e2"],Ie={tag:"dl",attrs:{class:"__style-wrapper border-bottom"},child:[{tag:"dd",attrs:{style:"font-weight: 800;","data-style":"fontWeight:800"},child:["B"]},{tag:"dd",attrs:{style:"font-style: italic;","data-style":"fontStyle:italic"},child:["I"]},{tag:"dd",attrs:{style:"text-decoration: line-through;","data-style":"textDecoration:line-through"},child:["abc"]},{tag:"dd",attrs:{style:"","data-style":"textAlign:left",class:"text-align--l"}},{tag:"dd",attrs:{style:"","data-style":"textAlign:center",class:"text-align--c"}},{tag:"dd",attrs:{style:"","data-style":"textAlign:right",class:"text-align--r"}}]},ne={tag:"dl",attrs:{class:"__tag-wrapper"},child:[{tag:"dd",attrs:{class:"__h2","data-tag":"h2"},child:["大标题",{tag:"i"}]},{tag:"dd",attrs:{class:"__h4","data-tag":"h4"},child:["小标题",{tag:"i"}]},{tag:"dd",attrs:{class:"__section active","data-tag":"section"},child:["正文",{tag:"i"}]},{tag:"dd",attrs:{class:"__blockquote","data-tag":"blockquote"},child:["引用",{tag:"i"}]},{tag:"dd",attrs:{class:"__ul","data-tag":"ul"},child:["无序列表",{tag:"i"}]}]},Re=t=>{const e=[];return t.forEach((i,n)=>{/^#\w{3,6}$/.test(i)&&e.push({tag:"dd",attrs:{class:n===0?"active":"","data-color":Me(i.toLowerCase())},child:[{tag:"i",attrs:{style:`background:${i}`}}]})}),e},Me=t=>t.length===7?t:`#${t[1]}${t[1]}${t[2]}${t[2]}${t[3]}${t[3]}`,Be={textStyleColors:[...Pe],textStyleTitle:"Set Style",textStyleHeadLeftBtnText:"Clear"},lt="",T="style-panel",B=`${T}__fade-in`;class ke{constructor(e){d(this,"editorInstance",null);d(this,"$el");d(this,"options");d(this,"_headerSwitchHandler");d(this,"_headerLeftHandler");d(this,"$elMap",new Map);d(this,"_styleHandler");d(this,"_colorHandler");d(this,"_tagHandler");const i={...Be,...e};this.options=i,this.$el=f("div",{class:`${T} border-top`}),this._styleHandler=n=>{const s=this.editorInstance,r=n.currentTarget,o=A(r,"data-style"),a=s.getStyles();Object.keys(o).forEach(l=>{a[l]&&(o[l]="")}),s.changeStyles(o)},this._colorHandler=n=>{const s=n.currentTarget;if(this.updateActiveClassName(s)){const r=this.editorInstance,o=s.getAttribute("data-color");r.changeStyles({color:o})}},this._tagHandler=n=>{const s=n.currentTarget;if(this.updateActiveClassName(s)){const r=this.editorInstance,o=s.getAttribute("data-tag");r.changeNodeName(o)}},this._headerLeftHandler=()=>{const n=this.editorInstance,{textColor:s,childNodeName:r}=n.options;n.changeStyles(),n.changeNodeName(),this.updateActiveClassName(b(`[data-color="${s}"]`,this.$el)),this.updateActiveClassName(b(`[data-tag="${r}"]`,this.$el))},this._headerSwitchHandler=()=>{this.$el.classList.contains(B)?this.hide():this.show()}}_initChildEl(e){const{textColor:i,childNodeName:n}=e,{textStyleTitle:s,textStyleHeadLeftBtnText:r,textStyleColors:o}=this.options,a=f("div",{class:`${T}__header`},s),l=f("div",{class:"__left"},r),v=f("div",{class:"__switch"});a.append(l,v);const u=[Ie],c=o;if(c.length){i&&!c.includes(i)&&c.unshift(i);const p={tag:"dl",attrs:{class:"__color-wrapper border-bottom"},child:Re(c)};u.push(p)}const w={...ne,child:[...ne.child]},m=n.toLowerCase();w.child.forEach(p=>{const g=p.attrs["data-tag"];g==="section"&&g!==m&&(p.attrs["data-tag"]=m)}),u.push(w);const h=J({tag:"div",attrs:{class:`${T}__body`},child:u});this.$el.append(a,h),L(".__style-wrapper dd",h).forEach(p=>{p.addEventListener("click",this._styleHandler)}),L(".__color-wrapper dd",h).forEach(p=>{p.addEventListener("click",this._colorHandler)}),L(".__tag-wrapper dd",h).forEach(p=>{p.addEventListener("click",this._tagHandler)}),l.addEventListener("click",this._headerLeftHandler),v.addEventListener("click",this._headerSwitchHandler),this.$elMap.set(l,this._headerLeftHandler),this.$elMap.set(v,this._headerSwitchHandler)}install(e,i){this.editorInstance=e,i&&i.append(this.$el),this._initChildEl(e.options),e.on("click",()=>{const{textColor:n,childNodeName:s}=e.options,r=e.getStyles();this.updateActiveClassName(b(`[data-color="${r.color||n}"]`,this.$el));const o=e.getCursorElement(!0).nodeName.toLowerCase();this.updateActiveClassName(b(`[data-tag="${o||s}"]`,this.$el))})}show(){this.$el.classList.add(B)}hide(){this.$el.classList.remove(B)}updateActiveClassName(e){return!e||e.classList.contains("active")?!1:(b(".active",e.parentElement).classList.remove("active"),e.classList.add("active"),!0)}destroy(){this.$elMap.forEach((e,i)=>{i.removeEventListener("click",e)})}}const ct="",je={toolbarBeenFixed:!0,toolbarButtons:["choose-picture","text-style"]};class Fe{constructor(e){d(this,"editorInstance",null);d(this,"visible");d(this,"options");d(this,"$el");d(this,"_btnClickHandler");this.options={...je,...e},this.visible=this.options.toolbarBeenFixed;const[i,n]=be(this.options.toolbarHeight||"");this.$el=f("div",{class:"sp-editor__toolbar border-top",style:{"--bar-height":i?`${i}${n}`:null}},'<dl class="inner-wrapper"></dl>'),this._btnClickHandler=s=>{const r=s.currentTarget;this.editorInstance&&r&&this.editorInstance.emit("toolbarButtonOnClick",r.getAttribute("data-name"))},this.options.toolbarButtons.forEach(s=>{this.addButton({name:s})})}install(e,i){this.editorInstance=e,i&&i.append(this.$el),this.visible&&this.show()}show(){$e(this.$el,"__fade-in"),this.visible=!0,this.editorInstance.emit("toolbarShow",!0,this)}hide(){Ce(this.$el,"__fade-in"),this.visible=!1,this.editorInstance.emit("toolbarShow",!1,this)}addButton(e,i){const n={...e.style},s=f("dd",{class:Ne("icon-item",e.className),dataName:e.name,style:n},e.innerHtml),r=L("dd",this.$el),o=b("dl",this.$el);typeof i=="number"&&i>=0&&i<r.length?o.insertBefore(s,r[i]):o.append(s),s.addEventListener("click",this._btnClickHandler)}destroy(){L(".icon-item",this.$el).forEach(e=>{e.removeEventListener("click",this._btnClickHandler)})}}/*! * image-process version 4.3.1 * Author: Xing Zhong <capricorncd@qq.com, zx198401@gmail.com> * Repository: https://github.com/capricorncd/image-process-tools * Released on: 2022-12-30 14:17:09 (GMT+0900) *//*! * zx-sml version 0.6.0 * Author: Xing Zhong<zx198401@gmail.com> * Repository: https://github.com/capricorncd/zx-sml * Released on: 2022-09-09 21:21:26 (GMT+0900) */var Ue=Object.defineProperty,se=Object.getOwnPropertySymbols,We=Object.prototype.hasOwnProperty,ze=Object.prototype.propertyIsEnumerable,re=(t,e,i)=>e in t?Ue(t,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):t[e]=i,oe=(t,e)=>{for(var i in e||(e={}))We.call(e,i)&&re(t,i,e[i]);if(se)for(var i of se(e))ze.call(e,i)&&re(t,i,e[i]);return t};function Ge(t){return Array.isArray(t)}function ae(t){return t!==null&&!Ge(t)&&typeof t=="object"}var Ve=typeof globalThis<"u"?globalThis:typeof window<"u"?window:typeof global<"u"?global:typeof self<"u"?self:{},S={exports:{}};/*! For license information please see date-utils-2020.js.LICENSE.txt */(function(t,e){(function(i,n){t.exports=n()})(typeof self<"u"?self:Ve,function(){return(()=>{var i={949:(s,r)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.toTwoDigits=void 0,r.toTwoDigits=function(o){return o[1]?o:"0"+o}},607:(s,r,o)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.toTwoDigits=r.toDate=r.formatDate=void 0;var a=o(949);Object.defineProperty(r,"toTwoDigits",{enumerable:!0,get:function(){return a.toTwoDigits}});var l={weeks:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"]};function v(u){if(u instanceof Date)return u;if(typeof u=="number")return new Date(u);if(typeof u=="string"){var c=u.trim();if(/^\d+$/.test(c)){var w=c.length;return w===8?new Date([c.substr(0,4),c.substr(4,2),c.substr(6,2)].join("/")):w===6?new Date([c.substr(0,4),c.substr(4,2),"01"].join("/")):w===4?new Date(c+"/01/01"):new Date(parseInt(u))}if(c=c.replace(/[年月日]/g,function(h){return h==="日"?"":"/"}).replace(/[(((].*?[)))]/g," ").replace(/\bam|pm\b/gi," ").replace(/\s+/g," "),/^(\d{4})[-/](\d{1,2})[-/](\d{1,2})$/.test(c))return new Date([RegExp.$1,RegExp.$2,RegExp.$3].join("/"));if(/^(\d{4})[-/](\d{1,2})$/.test(c))return new Date([RegExp.$1,RegExp.$2,"01"].join("/"));var m=new Date(c);return isNaN(m.getFullYear())?null:m}return null}r.formatDate=function(u,c,w){var m,h=v(u);if(!h||!c)return u+"";if(c==="timestamp")return h.getTime().toString();/(y+)/i.test(c)&&(m=RegExp.$1,c=c.replace(m,(h.getFullYear()+"").substr(4-m.length))),w&&Array.isArray(w.weeks)||(w=l);var p={"M+":h.getMonth()+1,"d+":h.getDate(),"h+":h.getHours(),"m+":h.getMinutes(),"s+":h.getSeconds(),"w+":h.getDay(),"W+":w.weeks[h.getDay()],"a+":h.getHours()<12?"am":"pm","A+":h.getHours()<12?"AM":"PM"};for(var g in p)if(new RegExp("("+g+")").test(c)){m=RegExp.$1;var y=p[g]+"";c=c.replace(m,m.length===1?y:a.toTwoDigits(y))}if(/(g)/i.test(c)){var N=h.toString().split(/\s+/).slice(5),st=c.includes("g");c=c.replace(/g/i,st?N[0]:N.join(" "))}return c},r.toDate=v}},n={};return function s(r){if(n[r])return n[r].exports;var o=n[r]={exports:{}};return i[r](o,o.exports,s),o.exports}(607)})()})})(S);function le(t="",e="-"){return t.replace(/[A-Z]/g,(i,n)=>`${n>0?e:""}${i.toLowerCase()}`)}function qe(t="",e=!1){const i=t.replace(/[-_\s](\w)/g,(n,s)=>s.toUpperCase());return e?i.replace(/^\w/,n=>n.toUpperCase()):i}function ce(t={},e=!1){const i=e?qe:le,n={};for(const[s,r]of Object.entries(t))n[i(s)]=ae(r)?ce(r,e):r;return n}function de(t,e=!1,i=2){const n=["KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB"],s=e?1e3:1024;let r=String(t),o="Byte";for(let a=0,l=t/s;l>1;l/=s,a++)r=l.toFixed(i),o=n[a];return e&&(o=o.replace("i","")),{text:r.replace(/\.0+$/,"")+o,value:+r,unit:o,bytes:t}}function he(t,e={},i){const n=document.createElement(t);for(const[s,r]of Object.entries(e))n.setAttribute(le(s),s==="style"&&ae(r)?Ye(r):r);return i&&(Array.isArray(i)||(i=[i]),i.forEach(s=>{if(typeof s=="string"){const r=he("div");r.innerHTML=s,n.append(...r.childNodes)}else n.append(s)})),n}function Ye(...t){const e=t.reduce((n,s)=>oe(oe({},n),ce(s)),{}),i=[];for(const[n,s]of Object.entries(e))s===""||typeof s>"u"||s===null||i.push(`${n}:${s}`);return i.join(";")}function Ke(t){return new Promise((e,i)=>{const n=new FileReader;n.onload=s=>{var r;const o=(r=s.target)==null?void 0:r.result;o?e(o):i(new Error(`FileReader's result is null, ${s.target}`))},n.onerror=i,n.readAsDataURL(t)})}function fe(t){return(window.URL||window.webkitURL).createObjectURL(t)}function ue(t){const e=t.split(",");let i="";return/data:(\w+\/\w+);base64/.test(e[0])&&(i=RegExp.$1),{type:i,data:e[1]}}function ge(t,e){const i=ue(t),n=window.atob(i.data);e=e||i.type;const s=new Uint8Array(n.length);for(let r=0;r<n.length;r++)s[r]=n.charCodeAt(r);return new Blob([s],{type:e})}S.exports.formatDate,S.exports.toDate,S.exports.toTwoDigits;const Ze={enableDevicePixelRatio:!1,isForce:!1,mimeType:"image/jpeg",perResize:500,quality:.9,width:0,height:0,longEdge:0},Je=/^data:(.+?);base64/,Qe=/^image\/.+/;function Xe(t,e){return new Promise((i,n)=>{var s;const r={...Ze,...e,longEdge:(s=e==null?void 0:e.longEdge)!=null?s:e==null?void 0:e.longestSide};typeof t=="string"&&Je.test(t)?pe(t,r,i,n):(t instanceof File||t instanceof Blob)&&Qe.test(t.type)?Ke(t).then(o=>{pe(o,r,i,n)}).catch(n):n(new Error(`Invalid file, ${t}`))})}function pe(t,e,i,n){const{type:s}=ue(t),r=ge(t,s),o=new Image;o.onload=()=>{const a={element:o,blob:r,data:t,url:fe(r),width:o.naturalWidth||o.width,height:o.naturalHeight||o.height,type:s,size:de(r.size)};e.cropInfo&&e.cropInfo.sw&&e.cropInfo.sh?me(a,e,i,n,{...e.cropInfo,dx:0,dy:0,dw:e.cropInfo.sw,dh:e.cropInfo.sh}):e.width&&e.height?me(a,e,i,n,tt(a,e)):e.width||e.height||e.longEdge?et(a,e,i,n):k({...a,raw:a},e,i)},o.onerror=n,o.src=t}function me(t,e,i,n,s){try{Object.prototype.hasOwnProperty.call(s,"enableDevicePixelRatio")||(s.enableDevicePixelRatio=e.enableDevicePixelRatio);const r=j(t.element,{enableDevicePixelRatio:e.enableDevicePixelRatio,sx:s.sx,sy:s.sy,sw:s.sw,sh:s.sh,dx:0,dy:0,dw:s.sw,dh:s.sh});!e.width&&!e.height?e.longEdge?s.sw>s.sh?(e.width=e.longEdge,e.height=s.sh*e.width/s.sw):(e.height=e.longEdge,e.width=s.sw*e.height/s.sh):(e.width=s.sw,e.height=s.sh):e.width?e.height=s.sh*e.width/s.sw:e.width=s.sw*e.height/s.sh,ye(r,t,e,{...s,sx:0,sy:0,sw:r.width,sh:r.height},i)}catch(r){n(r)}}function et(t,e,i,n){try{e.longEdge&&!e.width&&!e.height&&(t.width>=t.height?e.width=e.longEdge:e.height=e.longEdge);const s={enableDevicePixelRatio:e.enableDevicePixelRatio,sx:0,sy:0,sw:t.width,sh:t.height,dx:0,dy:0,dw:e.width,dh:e.height};if(e.width){if(t.width<e.width&&!e.isForce){k({...t,raw:t},e,i);return}s.dh=t.height*e.width/t.width,e.height=s.dh}else{if(t.height<e.height&&!e.isForce){k({...t,raw:t},e,i);return}s.dw=t.width*e.height/t.height,e.width=s.dw}ye(t.element,t,e,s,i)}catch(s){n(s)}}function k(t,e,i){t.type!==e.mimeType?(t.type=e.mimeType,we(t.element,t.raw,e,{enableDevicePixelRatio:e.enableDevicePixelRatio,sx:0,sy:0,sw:t.width,sh:t.height,dx:0,dy:0,dw:t.width,dh:t.height},i)):i(t)}function ye(t,e,i,n,s){let r=e.width>e.height?e.width-n.dw:e.height-n.dh;if(r>i.perResize){const o=e.height/e.width;for(;r>i.perResize;)r-=i.perResize,n.sw=t.width,n.sh=t.height,n.dw=i.width+r,n.dh=n.dw*o,t=j(t,n)}n.sw=t.width,n.sh=t.height,n.dw=i.width,n.dh=i.height,we(t,e,i,n,s)}function we(t,e,i,n,s){const r=j(t,n),o=/^\w+\/\*$/.test(i.mimeType)||!i.mimeType?e.type:i.mimeType,a=r.toDataURL(o,i.quality),l=ge(a,o);s({element:r,type:o,width:r.width,height:r.height,blob:l,data:a,url:fe(l),size:de(l.size),raw:e})}function tt(t,e){const{width:i,height:n}=t,{width:s,height:r}=e;let o;const a=n*s/r;if(i>a)o={sx:(i-a)/2,sy:0,sw:a,sh:n};else{const l=i*r/s;o={sx:0,sy:(n-l)/2,sw:i,sh:l}}return{...o,dx:0,dy:0,dw:s,dh:r}}function j(t,e){const i=e.enableDevicePixelRatio&&window.devicePixelRatio||1,n=he("canvas");n.width=e.dw*i,n.height=e.dh*i;const s=n.getContext("2d");return s.scale(i,i),s.drawImage(t,e.sx,e.sy,e.sw,e.sh,e.dx,e.dy,e.dw,e.dh),n}const it={imageMaxWidth:750,ignoreGif:!0,forceImageResize:!1,chooseFileMultiple:!0,chooseFileAccept:"image/*"},dt="";class nt extends De{constructor(i,n={}){let s=null;if(typeof i=="string"||i instanceof HTMLElement?s=b(i):(n=i||{},typeof n.container=="string"&&(s=b(n.container))),n={...it,...n},!s)throw new Error(`Can't found '${i}' Node in document!`);const r=f("div",{class:"sp-editor"});super({...n,container:r});d(this,"$el");d(this,"stylePanel");d(this,"toolbar");d(this,"fileInput",null);d(this,"_inputChangeHandler");s.append(r),this.$el=r,this.stylePanel=new ke(n),this.use(this.stylePanel,this.$el),this.toolbar=new Fe(n),this.use(this.toolbar,this.$el),this._inputChangeHandler=o=>{const a=o.currentTarget;this.handleImageFile(a.files).then(l=>{l.forEach(v=>{const u=/gif$/i.test(v.raw.type)&&n.ignoreGif;this.insert(`<img src="${u?v.raw.data:v.data}">`)})}).catch(l=>{this.emit("error",l)})},this.on("toolbarButtonOnClick",o=>{switch(o){case"choose-picture":if(typeof n.customPictureHandler=="function")n.customPictureHandler();else if(this.fileInput)this.fileInput.click();else{const a={type:"file",style:{display:"none"},accept:n.chooseFileAccept};n.chooseFileMultiple&&(a.multiple=!0),this.fileInput=f("input",a),this.$el.append(this.fileInput),this.fileInput.addEventListener("change",this._inputChangeHandler),this.fileInput.click()}break;case"text-style":this.stylePanel.show();break}})}handleImageFile(i){return i?new Promise((n,s)=>{Promise.all(C(i).map(this._handleFile)).then(r=>{n(r.sort((o,a)=>o.index-a.index).map(o=>o.data))}).catch(s)}):Promise.resolve([])}_handleFile(i,n){return new Promise((s,r)=>{Xe(i).then(o=>{s({data:o,index:n})}).catch(r)})}addToolbarButton(i,n){this.toolbar.addButton(i,n)}destroy(){var i;super.destroy(),this.stylePanel.destroy(),this.toolbar.destroy(),(i=this.fileInput)==null||i.removeEventListener("change",this._inputChangeHandler)}}E.ALLOWED_NODE_NAMES=P,E.SpEditor=nt,Object.defineProperty(E,Symbol.toStringTag,{value:"Module"})});