UNPKG

@alkhipce/editorjs-aitext

Version:
13 lines (12 loc) 7.24 kB
(function(c,o){typeof exports=="object"&&typeof module<"u"?module.exports=o():typeof define=="function"&&define.amd?define(o):(c=typeof globalThis<"u"?globalThis:c||self,c.AIText=o())})(this,function(){"use strict";(function(){try{if(typeof document<"u"){var h=document.createElement("style");h.appendChild(document.createTextNode(".ce-paragraph{line-height:1.6em;outline:none}.ce-paragraph[data-placeholder]:empty:before{content:attr(data-placeholder);color:#707684;font-weight:400;opacity:0}.codex-editor--empty .ce-block:first-child .ce-paragraph[data-placeholder]:empty:before{opacity:1}.codex-editor--toolbox-opened .ce-block:first-child .ce-paragraph[data-placeholder]:empty:before,.codex-editor--empty .ce-block:first-child .ce-paragraph[data-placeholder]:empty:focus:before{opacity:0}.ce-paragraph p:first-of-type{margin-top:0}.ce-paragraph p:last-of-type{margin-bottom:0}")),document.head.appendChild(h)}}catch(e){console.error("vite-plugin-css-injected-by-js",e)}})();const c='<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="M8 9V7.2C8 7.08954 8.08954 7 8.2 7L12 7M16 9V7.2C16 7.08954 15.9105 7 15.8 7L12 7M12 7L12 17M12 17H10M12 17H14"/></svg>';/** * Base Paragraph Block for the Editor.js. * Represents a regular text block * * @author CodeX (team@codex.so) * @copyright CodeX 2018 * @license The MIT License (MIT) */class o{static get DEFAULT_PLACEHOLDER(){return""}constructor({data:e,config:t,api:i,readOnly:a}){this.api=i,this.readOnly=a,this._CSS={block:this.api.styles.block,wrapper:"ce-paragraph"},this.readOnly||(this.onKeyUp=this.onKeyUp.bind(this)),this._placeholder=t.placeholder?t.placeholder:o.DEFAULT_PLACEHOLDER,this._data={},this._element=null,this._preserveBlank=t.preserveBlank!==void 0?t.preserveBlank:!1,this.data=e}onKeyUp(e){if(e.code!=="Backspace"&&e.code!=="Delete")return;const{textContent:t}=this._element;t===""&&(this._element.innerHTML="")}drawView(){const e=document.createElement("DIV");return e.classList.add(this._CSS.wrapper,this._CSS.block),e.contentEditable=!1,e.dataset.placeholder=this.api.i18n.t(this._placeholder),this._data.text&&(e.innerHTML=this._data.text),this.readOnly||(e.contentEditable=!0,e.addEventListener("keyup",this.onKeyUp)),e}render(){return this._element=this.drawView(),this._element}merge(e){const t={text:this.data.text+e.text};this.data=t}validate(e){return!(e.text.trim()===""&&!this._preserveBlank)}save(e){return{text:e.innerHTML}}onPaste(e){const t={text:e.detail.data.innerHTML};this.data=t}static get conversionConfig(){return{export:"text",import:"text"}}static get sanitize(){return{text:{br:!0}}}static get isReadOnlySupported(){return!0}get data(){if(this._element!==null){const e=this._element.innerHTML;this._data.text=e}return this._data}set data(e){this._data=e||{},this._element!==null&&this.hydrate()}hydrate(){window.requestAnimationFrame(()=>{this._element.innerHTML=this._data.text||""})}static get pasteConfig(){return{tags:["P"]}}static get toolbox(){return{icon:c,title:"Text"}}}function y(h,e=2e3){let t;return(...i)=>{clearTimeout(t),t=setTimeout(()=>{h.apply(null,i)},e)}}const u=`<svg width="800px" height="800px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> <path d="M8 4V20M17 12V20M6 20H10M15 20H19M13 7V4H3V7M21 14V12H13V14" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/> </svg>`,g=`<svg width="800px" height="800px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> <path d="M12 2.99988V5.99988M12 20.9999V17.9999M4.20577 16.4999L6.80385 14.9999M21 11.9999H18M16.5 19.7941L15 17.196M3 11.9999H6M7.5 4.20565L9 6.80373M7.5 19.7941L9 17.196M19.7942 16.4999L17.1962 14.9999M4.20577 7.49988L6.80385 8.99988" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/> </svg>`,m=class p extends o{constructor({api:e,block:t,config:i,data:a}){if(super({api:e,block:t,config:i,data:a}),this.readOnly=!1,this.onKeyDown=n=>{var s;if(!this.isAcceptKey(n.code))return;const r=(s=this._element)==null?void 0:s.querySelector("#ai-suggestions");r!=null&&r.textContent&&(n.preventDefault(),n.stopPropagation(),this.applySuggestion(r))},this.onKeyUp=n=>{var s,d;if(this.isDeclineKey(n.code)||n.code==="Backspace"){(d=(s=this._element)==null?void 0:s.querySelector("#ai-suggestions"))==null||d.remove();return}if(n.code!=="Backspace"&&n.code!=="Delete"||!this._element)return;const{textContent:r}=this._element;r===""&&(this._element.innerHTML="")},!i.callback)throw new Error("Callback function is required!");this.callback=i.callback,this.acceptKeys=i.acceptKeys??["AltLeft","AltRight","Tab"],this.declineKeys=i.declineKeys??["Escape","Backspace"],this.debounceTimeout=i.debounceTimeout??2e3;const l=this.acceptKeys.filter(n=>this.declineKeys.includes(n));l.length&&console.warn(`AIText: keys [${l.join(", ")}] are in both acceptKeys and declineKeys`),this.loaderIcon=i.loaderIcon??g,p._icon=i.icon??u,this.onInput=y(n=>{var r;(r=this._element)!=null&&r.querySelector("#ai-suggestions")||n.inputType==="deleteContentBackward"||n.inputType==="deleteContentForward"||n.inputType==="insertParagraph"||n.inputType==="insertFromPaste"||n.inputType==="insertFromDrop"||!n.target.innerHTML||this.getAICompletion(n.target.innerHTML)},this.debounceTimeout)}static get toolbox(){return{title:"AI TEXT",icon:p._icon}}static prepare({config:e}){e!=null&&e.icon&&(p._icon=e.icon)}getAICompletion(e){var i,a;if(!e)return;const t=document.createElement("div");t.innerHTML=this.loaderIcon,t.id="ai-suggestions-loader",t.style.display="inline-flex",t.style.alignItems="center",t.style.width="24px",t.style.height="24px",t.style.paddingLeft="4px",t.style.color="lightgray",t.style.position="absolute",t.animate([{transform:"rotate(0deg)"},{transform:"rotate(360deg)"}],{duration:2e3,iterations:1/0}),(i=this._element)==null||i.appendChild(t),(a=this.callback)==null||a.call(this,e).then(l=>{var r,s,d;const n=document.createElement("span");n.innerHTML="",n.id="ai-suggestions",n.style.color="lightgray",n.innerHTML=l,(r=this._element)==null||r.appendChild(n),(d=(s=this._element)==null?void 0:s.querySelector("#ai-suggestions-loader"))==null||d.remove()}).catch(l=>{throw new Error(l)})}isAcceptKey(e){return this.acceptKeys.includes(e)}isDeclineKey(e){return this.declineKeys.includes(e)}applySuggestion(e){var i;const t=e.textContent;t&&((i=this._element)==null||i.appendChild(document.createTextNode(t)),e.remove(),this.moveCursorToEnd())}moveCursorToEnd(){if(!this._element)return;const e=document.createRange(),t=window.getSelection();e.selectNodeContents(this._element),e.collapse(!1),t==null||t.removeAllRanges(),t==null||t.addRange(e)}drawView(){const e=document.createElement("DIV");return e.classList.add(this._CSS.wrapper,this._CSS.block),e.contentEditable="false",e.dataset.placeholder=this.api.i18n.t(this._placeholder),this._data.text&&(e.innerHTML=this._data.text),this.readOnly||(e.contentEditable="true",e.addEventListener("keydown",this.onKeyDown),e.addEventListener("keyup",this.onKeyUp),e.addEventListener("input",this.onInput)),e}_placeholder(e){throw new Error("Method not implemented.")}};return m._icon=u,m});