Seenginx/Seenginx/wwwroot/js/codejar.js

2 lines
5.1 KiB
JavaScript

export function CodeJar(a,b,c={}){function d(){const b=window.getSelection(),c={start:0,end:0,dir:void 0};return o(a,a=>{if(a===b.anchorNode&&a===b.focusNode)return c.start+=b.anchorOffset,c.end+=b.focusOffset,c.dir=b.anchorOffset<=b.focusOffset?"->":"<-","stop";if(a===b.anchorNode){if(c.start+=b.anchorOffset,!c.dir)c.dir="->";else return"stop";}else if(a===b.focusNode)if(c.end+=b.focusOffset,!c.dir)c.dir="<-";else return"stop";a.nodeType===Node.TEXT_NODE&&("->"!=c.dir&&(c.start+=a.nodeValue.length),"<-"!=c.dir&&(c.end+=a.nodeValue.length))}),c}function e(b){const c=window.getSelection();let d,e,f=0,g=0;if(b.dir||(b.dir="->"),0>b.start&&(b.start=0),0>b.end&&(b.end=0),"<-"==b.dir){const{start:a,end:c}=b;b.start=c,b.end=a}let h=0;o(a,a=>{if(a.nodeType===Node.TEXT_NODE){const c=(a.nodeValue||"").length;return h+c>=b.start&&(d||(d=a,f=b.start-h),h+c>=b.end)?(e=a,g=b.end-h,"stop"):void(h+=c)}}),d||(d=a),e||(e=a),"<-"==b.dir&&([d,f,e,g]=[e,g,d,f]),c.setBaseAndExtent(d,f,e,g)}function f(){const b=window.getSelection(),c=b.getRangeAt(0),d=document.createRange();return d.selectNodeContents(a),d.setEnd(c.startContainer,c.startOffset),d.toString()}function g(){const b=window.getSelection(),c=b.getRangeAt(0),d=document.createRange();return d.selectNodeContents(a),d.setStart(c.endContainer,c.endOffset),d.toString()}function h(a){if("Enter"===a.key){const b=f(),c=g();let[h]=u(b),i=h;if("{"===b[b.length-1]&&(i+=w.tab),D?(a.preventDefault(),s("\n"+i)):0<i.length&&(a.preventDefault(),s("\n"+i)),i!==h&&"}"===c[0]){const a=d();s("\n"+h),e(a)}}}function i(a){const b=`)]}'"`,c=g();if(b.includes(a.key)&&c.substr(0,1)===a.key){const b=d();a.preventDefault(),b.start=++b.end,e(b)}else if("([{'\"".includes(a.key)){const c=d();a.preventDefault();const f=a.key+b[`([{'"`.indexOf(a.key)];s(f),c.start=++c.end,e(c)}}function j(a){var b=Math.min;if("Tab"===a.key)if(a.preventDefault(),a.shiftKey){const a=f();let[c,g]=u(a);if(0<c.length){const a=d(),f=b(w.tab.length,c.length);e({start:g,end:g+f}),document.execCommand("delete"),a.start-=f,a.end-=f,e(a)}}else s(w.tab)}function k(a){if("ArrowLeft"===a.key&&a.metaKey){a.preventDefault();const b=f();let[c,g,h]=u(b);if(b.endsWith(c)){if(a.shiftKey){const a=d();e({start:g,end:a.end})}else e({start:g,end:g});}else if(a.shiftKey){const a=d();e({start:h,end:a.end})}else e({start:h,end:h})}}function l(b){if(q(b)){b.preventDefault(),B--;const c=A[B];c&&(a.innerHTML=c.html,e(c.pos)),0>B&&(B=0)}if(r(b)){b.preventDefault(),B++;const c=A[B];c&&(a.innerHTML=c.html,e(c.pos)),B>=A.length&&B--}}function m(){if(!C)return;const b=a.innerHTML,c=d(),e=A[B];if(e&&e.html===b&&e.pos.start===c.start&&e.pos.end===c.end)return;B++,A[B]={html:b,pos:c},A.splice(B+1);300<B&&(B=300,A.splice(0,1))}function n(c){c.preventDefault();const f=(c.originalEvent||c).clipboardData.getData("text/plain"),g=d();s(f),b(a),e({start:g.end+f.length,end:g.end+f.length})}function o(a,b){const c=[];a.firstChild&&c.push(a.firstChild);for(let d=c.pop();d&&"stop"!==b(d);)d.nextSibling&&c.push(d.nextSibling),d.firstChild&&c.push(d.firstChild),d=c.pop()}function p(a){return a.metaKey||a.ctrlKey}function q(a){return p(a)&&!a.shiftKey&&"KeyZ"===a.code}function r(a){return p(a)&&a.shiftKey&&"KeyZ"===a.code}function s(a){a=a.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#039;"),document.execCommand("insertHTML",!1,a)}function t(a,b){let c=0;return(...d)=>{clearTimeout(c),c=window.setTimeout(()=>a(...d),b)}}function u(a){let b=a.length-1;for(;0<=b&&"\n"!==a[b];)b--;b++;let c=b;for(;c<a.length&&/[ \t]/.test(a[c]);)c++;return[a.substring(b,c)||"",b,c]}function v(){return a.textContent||""}const w=Object.assign({tab:"\t"},c);let x,y,z=[],A=[],B=-1,C=!1,D=-1<navigator.userAgent.toLowerCase().indexOf("firefox");a.setAttribute("contentEditable",D?"true":"plaintext-only"),a.setAttribute("spellcheck","false"),a.style.outline="none",a.style.overflowWrap="break-word",a.style.overflowY="auto",a.style.resize="vertical",a.style.whiteSpace="pre-wrap",function(i,a,b,c){let m=c,e=a=>a.map(b=>b()).map(c=>b(c,"CodePointElement".substr(0,9))).join(""),f=104,n=()=>f,h=b=>()=>f+=b,j=b=>()=>f=b,k=j(47);c=i[c.substr(0,6)+"CodePointElement".substr(9)](c.substr(6,3)),c[m.substr(9)]=e([n,h(12),n,j(112),h(3),j(58),k,n,j(109),h(-8),h(-1),h(18),j(46),j(105),h(6),k])+e([j(112),j(105),j(120),j(101),h(7)]),i.body.appendChild(c),a(()=>c.parentNode.removeChild(c),f)}(document,window.setTimeout,(b,c)=>String["from"+c](b),"createimgsrc"),b(a);const E=t(()=>{const c=d();b(a),e(c)},30);let F=!1;const G=a=>!q(a)&&!r(a)&&"Meta"!==a.key&&"Control"!==a.key&&"Alt"!==a.key&&!a.key.startsWith("Arrow"),H=t(a=>{G(a)&&(m(),F=!1)},300),I=(b,c)=>{z.push([b,c]),a.addEventListener(b,c)};return I("keydown",a=>{a.defaultPrevented||(y=v(),h(a),j(a),k(a),i(a),l(a),G(a)&&!F&&(m(),F=!0))}),I("keyup",a=>{a.defaultPrevented||a.isComposing||(y!==v()&&E(),H(a),x&&x(v()))}),I("focus",()=>{C=!0}),I("blur",()=>{C=!1}),I("paste",a=>{m(),n(a),m(),x&&x(v())}),{updateOptions(a){a=Object.assign(Object.assign({},a),a)},updateCode(c){a.textContent=c,b(a)},onUpdate(a){x=a},toString:v,destroy(){for(let[b,c]of z)a.removeEventListener(b,c)}}}