lightview
Version:
Small, simple, powerful web UI and micro front end creation ... Great ideas from Svelte, React, Vue and Riot combined.
8 lines • 5.56 kB
JavaScript
/**
* Minified by jsDelivr using Terser v5.10.0.
* Original file: /npm/codejar@3.6.0/codejar.js
*
* Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
*/
const globalWindow=window;export function CodeJar(t,e,n={}){const o=Object.assign({tab:"\t",indentOn:/[({\[]$/,moveToNewLine:/^[)}\]]/,spellcheck:!1,catchTab:!0,preserveIdent:!0,addClosing:!0,history:!0,window:globalWindow},n),r=o.window,i=r.document;let s,d,l=[],a=[],c=-1,f=!1;t.setAttribute("contenteditable","plaintext-only"),t.setAttribute("spellcheck",o.spellcheck?"true":"false"),t.style.outline="none",t.style.overflowWrap="break-word",t.style.overflowY="auto",t.style.whiteSpace="pre-wrap";let u=!1;e(t),"plaintext-only"!==t.contentEditable&&(u=!0),u&&t.setAttribute("contenteditable","true");const p=L((()=>{const n=T();e(t,n),b(n)}),30);let h=!1;const g=t=>!O(t)&&!x(t)&&"Meta"!==t.key&&"Control"!==t.key&&"Alt"!==t.key&&!t.key.startsWith("Arrow"),y=L((t=>{g(t)&&(C(),h=!1)}),300),N=(e,n)=>{l.push([e,n]),t.addEventListener(e,n)};function T(){const e=K(),n={start:0,end:0,dir:void 0};let{anchorNode:o,anchorOffset:r,focusNode:s,focusOffset:d}=e;if(!o||!s)throw"error1";if(o.nodeType===Node.ELEMENT_NODE){const t=i.createTextNode("");o.insertBefore(t,o.childNodes[r]),o=t,r=0}if(s.nodeType===Node.ELEMENT_NODE){const t=i.createTextNode("");s.insertBefore(t,s.childNodes[d]),s=t,d=0}return v(t,(t=>{if(t===o&&t===s)return n.start+=r,n.end+=d,n.dir=r<=d?"->":"<-","stop";if(t===o){if(n.start+=r,n.dir)return"stop";n.dir="->"}else if(t===s){if(n.end+=d,n.dir)return"stop";n.dir="<-"}t.nodeType===Node.TEXT_NODE&&("->"!=n.dir&&(n.start+=t.nodeValue.length),"<-"!=n.dir&&(n.end+=t.nodeValue.length))})),t.normalize(),n}function b(e){const n=K();let o,r,i=0,s=0;if(e.dir||(e.dir="->"),e.start<0&&(e.start=0),e.end<0&&(e.end=0),"<-"==e.dir){const{start:t,end:n}=e;e.start=n,e.end=t}let d=0;v(t,(t=>{if(t.nodeType!==Node.TEXT_NODE)return;const n=(t.nodeValue||"").length;if(d+n>e.start&&(o||(o=t,i=e.start-d),d+n>e.end))return r=t,s=e.end-d,"stop";d+=n})),o||(o=t,i=t.childNodes.length),r||(r=t,s=t.childNodes.length),"<-"==e.dir&&([o,i,r,s]=[r,s,o,i]),n.setBaseAndExtent(o,i,r,s)}function E(){const e=K().getRangeAt(0),n=i.createRange();return n.selectNodeContents(t),n.setEnd(e.startContainer,e.startOffset),n.toString()}function m(){const e=K().getRangeAt(0),n=i.createRange();return n.selectNodeContents(t),n.setStart(e.endContainer,e.endOffset),n.toString()}function k(t){if(u&&"Enter"===t.key)if(D(t),t.stopPropagation(),""==m()){M("\n ");const t=T();t.start=--t.end,b(t)}else M("\n")}function C(){if(!f)return;const e=t.innerHTML,n=T(),o=a[c];if(o&&o.html===e&&o.pos.start===n.start&&o.pos.end===n.end)return;c++,a[c]={html:e,pos:n},a.splice(c+1);c>300&&(c=300,a.splice(0,1))}function v(t,e){const n=[];t.firstChild&&n.push(t.firstChild);let o=n.pop();for(;o&&"stop"!==e(o);)o.nextSibling&&n.push(o.nextSibling),o.firstChild&&n.push(o.firstChild),o=n.pop()}function w(t){return t.metaKey||t.ctrlKey}function O(t){return w(t)&&!t.shiftKey&&"KeyZ"===t.code}function x(t){return w(t)&&t.shiftKey&&"KeyZ"===t.code}function M(t){t=t.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'"),i.execCommand("insertHTML",!1,t)}function L(t,e){let n=0;return(...o)=>{clearTimeout(n),n=r.setTimeout((()=>t(...o)),e)}}function S(t){let e=t.length-1;for(;e>=0&&"\n"!==t[e];)e--;e++;let n=e;for(;n<t.length&&/[ \t]/.test(t[n]);)n++;return[t.substring(e,n)||"",e,n]}function A(){return t.textContent||""}function D(t){t.preventDefault()}function K(){var e;return(null===(e=t.parentNode)||void 0===e?void 0:e.nodeType)==Node.DOCUMENT_FRAGMENT_NODE?t.parentNode.getSelection():r.getSelection()}return N("keydown",(e=>{e.defaultPrevented||(d=A(),o.preserveIdent?function(t){if("Enter"===t.key){const e=E(),n=m();let[r]=S(e),i=r;if(o.indentOn.test(e)&&(i+=o.tab),i.length>0?(D(t),t.stopPropagation(),M("\n"+i)):k(t),i!==r&&o.moveToNewLine.test(n)){const t=T();M("\n"+r),b(t)}}}(e):k(e),o.catchTab&&function(t){if("Tab"===t.key)if(D(t),t.shiftKey){const t=E();let[e,n]=S(t);if(e.length>0){const t=T(),r=Math.min(o.tab.length,e.length);b({start:n,end:n+r}),i.execCommand("delete"),t.start-=r,t.end-=r,b(t)}}else M(o.tab)}(e),o.addClosing&&function(t){const e="([{'\"",n=")]}'\"",o=m(),r=E(),i="\\"===r.substr(r.length-1),s=o.substr(0,1);if(n.includes(t.key)&&!i&&s===t.key){const e=T();D(t),e.start=++e.end,b(e)}else if(e.includes(t.key)&&!i&&("\"'".includes(t.key)||[""," ","\n"].includes(s))){D(t);const o=T(),r=o.start==o.end?"":K().toString();M(t.key+r+n[e.indexOf(t.key)]),o.start++,o.end++,b(o)}}(e),o.history&&(!function(e){if(O(e)){D(e),c--;const n=a[c];n&&(t.innerHTML=n.html,b(n.pos)),c<0&&(c=0)}if(x(e)){D(e),c++;const n=a[c];n&&(t.innerHTML=n.html,b(n.pos)),c>=a.length&&c--}}(e),g(e)&&!h&&(C(),h=!0)),u&&b(T()))})),N("keyup",(t=>{t.defaultPrevented||t.isComposing||(d!==A()&&p(),y(t),s&&s(A()))})),N("focus",(t=>{f=!0})),N("blur",(t=>{f=!1})),N("paste",(n=>{C(),function(n){D(n);const o=(n.originalEvent||n).clipboardData.getData("text/plain").replace(/\r/g,""),r=T();M(o),e(t),b({start:Math.min(r.start,r.end)+o.length,end:Math.min(r.start,r.end)+o.length,dir:"<-"})}(n),C(),s&&s(A())})),{updateOptions(t){Object.assign(o,t)},updateCode(n){t.textContent=n,e(t)},onUpdate(t){s=t},toString:A,save:T,restore:b,recordHistory:C,destroy(){for(let[e,n]of l)t.removeEventListener(e,n)}}}
//# sourceMappingURL=/sm/96f81945d5ecf71ee083a671465d6081d317ddc1afb05de3cce12b31422fa964.map