1
2
3
4
5
|
/*!
* CodeJar 3.6.0 (https://github.com/antonmedv/codejar)
* Copyright (c) 2020-2022, Anton Medvedev, MIT
*/
const globalWindow=window;function CodeJar(y,n,e={}){const v=Object.assign({tab:"\t",indentOn:/[({\[]$/,moveToNewLine:/^[)}\]]/,spellcheck:!1,catchTab:!0,preserveIdent:!0,addClosing:!0,history:!0,window:globalWindow},e),o=v.window,N=o.document;let r=[],T=[],b=-1,s=!1,i,E,m=(y.setAttribute("contenteditable","plaintext-only"),y.setAttribute("spellcheck",v.spellcheck?"true":"false"),y.style.outline="none",y.style.overflowWrap="break-word",y.style.overflowY="auto",!(y.style.whiteSpace="pre-wrap"));n(y),(m="plaintext-only"!==y.contentEditable?!0:m)&&y.setAttribute("contenteditable","true");const t=c(()=>{var e=w();n(y,e),O(e)},30);let k=!1;const C=e=>!D(e)&&!S(e)&&"Meta"!==e.key&&"Control"!==e.key&&"Alt"!==e.key&&!e.key.startsWith("Arrow"),a=c(e=>{C(e)&&(A(),k=!1)},300);e=(e,t)=>{r.push([e,t]),y.addEventListener(e,t)};function w(){var e=P();const t={start:0,end:0,dir:void 0};let{anchorNode:n,anchorOffset:r,focusNode:o,focusOffset:s}=e;if(n&&o)return n.nodeType===Node.ELEMENT_NODE&&(e=N.createTextNode(""),n.insertBefore(e,n.childNodes[r]),n=e,r=0),o.nodeType===Node.ELEMENT_NODE&&(e=N.createTextNode(""),o.insertBefore(e,o.childNodes[s]),o=e,s=0),l(y,e=>{if(e===n&&e===o)return t.start+=r,t.end+=s,t.dir=r<=s?"->":"<-","stop";if(e===n){if(t.start+=r,t.dir)return"stop";t.dir="->"}else if(e===o){if(t.end+=s,t.dir)return"stop";t.dir="<-"}e.nodeType===Node.TEXT_NODE&&("->"!=t.dir&&(t.start+=e.nodeValue.length),"<-"!=t.dir&&(t.end+=e.nodeValue.length))}),y.normalize(),t;throw"error1"}function O(n){const e=P();let r,o=0,s,i=0;var t,a;n.dir||(n.dir="->"),n.start<0&&(n.start=0),n.end<0&&(n.end=0),"<-"==n.dir&&({start:t,end:a}=n,n.start=a,n.end=t);let d=0;l(y,e=>{var t;if(e.nodeType===Node.TEXT_NODE)return t=(e.nodeValue||"").length,d+t>n.start&&(r||(r=e,o=n.start-d),d+t>n.end)?(s=e,i=n.end-d,"stop"):void(d+=t)}),r||(r=y,o=y.childNodes.length),s||(s=y,i=y.childNodes.length),"<-"==n.dir&&([r,o,s,i]=[s,i,r,o]),e.setBaseAndExtent(r,o,s,i)}function x(){const e=P();var t=e.getRangeAt(0);const n=N.createRange();return n.selectNodeContents(y),n.setEnd(t.startContainer,t.startOffset),n.toString()}function M(){const e=P();var t=e.getRangeAt(0);const n=N.createRange();return n.selectNodeContents(y),n.setStart(t.endContainer,t.endOffset),n.toString()}function L(e){if(m&&"Enter"===e.key)if(R(e),e.stopPropagation(),""==M()){K("\n ");const t=w();t.start=--t.end,O(t)}else K("\n")}function A(){var e,t,n;s&&(e=y.innerHTML,t=w(),(n=T[b])&&n.html===e&&n.pos.start===t.start&&n.pos.end===t.end||(b++,T[b]={html:e,pos:t},T.splice(b+1),300<b&&(b=300,T.splice(0,1))))}function l(e,t){const n=[];e.firstChild&&n.push(e.firstChild);let r=n.pop();for(;r&&"stop"!==t(r);)r.nextSibling&&n.push(r.nextSibling),r.firstChild&&n.push(r.firstChild),r=n.pop()}function d(e){return e.metaKey||e.ctrlKey}function D(e){return d(e)&&!e.shiftKey&&"KeyZ"===e.code}function S(e){return d(e)&&e.shiftKey&&"KeyZ"===e.code}function K(e){e=e.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""").replace(/'/g,"'"),N.execCommand("insertHTML",!1,e)}function c(t,n){let r=0;return(...e)=>{clearTimeout(r),r=o.setTimeout(()=>t(...e),n)}}function _(e){let t=e.length-1;for(;0<=t&&"\n"!==e[t];)t--;let n=++t;for(;n<e.length&&/[ \t]/.test(e[n]);)n++;return[e.substring(t,n)||"",t,n]}function H(){return y.textContent||""}function R(e){e.preventDefault()}function P(){var e;return((null==(e=y.parentNode)?void 0:e.nodeType)==Node.DOCUMENT_FRAGMENT_NODE?y.parentNode:o).getSelection()}return e("keydown",e=>{if(!e.defaultPrevented){if(E=H(),v.preserveIdent){var t=e;if("Enter"===t.key){var n=x(),r=M(),[o]=_(n);let e=o;v.indentOn.test(n)&&(e+=v.tab),0<e.length?(R(t),t.stopPropagation(),K("\n"+e)):L(t),e!==o&&v.moveToNewLine.test(r)&&(n=w(),K("\n"+o),O(n))}}else L(e);var s,i;if(v.catchTab)if("Tab"===(t=e).key)if(R(t),t.shiftKey){var[t,r]=_(x());if(0<t.length){const a=w();t=Math.min(v.tab.length,t.length);O({start:r,end:r+t}),N.execCommand("delete"),a.start-=t,a.end-=t,O(a)}}else K(v.tab);if(v.addClosing){o=e;const d=`([{'"`,l=`)]}'"`,c=M(),f=x(),u="\\"===f.substr(f.length-1),p=c.substr(0,1);if(l.includes(o.key)&&!u&&p===o.key){const h=w();R(o),h.start=++h.end,O(h)}else if(d.includes(o.key)&&!u&&(`"'`.includes(o.key)||[""," ","\n"].includes(p))){R(o);const g=w();n=g.start==g.end?"":P().toString();K(o.key+n+l[d.indexOf(o.key)]),g.start++,g.end++,O(g)}}v.history&&(D(s=e)&&(R(s),b--,(i=T[b])&&(y.innerHTML=i.html,O(i.pos)),b<0&&(b=0)),S(s)&&(R(s),b++,(i=T[b])&&(y.innerHTML=i.html,O(i.pos)),b>=T.length&&b--),C(e)&&!k&&(A(),k=!0)),m&&O(w())}}),e("keyup",e=>{e.defaultPrevented||e.isComposing||(E!==H()&&t(),a(e),i&&i(H()))}),e("focus",e=>{s=!0}),e("blur",e=>{s=!1}),e("paste",e=>{var t;A(),R(e=e),e=(e.originalEvent||e).clipboardData.getData("text/plain").replace(/\r/g,""),t=w(),K(e),n(y),O({start:Math.min(t.start,t.end)+e.length,end:Math.min(t.start,t.end)+e.length,dir:"<-"}),A(),i&&i(H())}),{updateOptions(e){Object.assign(v,e)},updateCode(e){y.textContent=e,n(y)},onUpdate(e){i=e},toString:H,save:w,restore:O,recordHistory:A,destroy(){for(var[e,t]of r)y.removeEventListener(e,t)}}}
|