-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.min.js
More file actions
1 lines (1 loc) · 1.72 KB
/
script.min.js
File metadata and controls
1 lines (1 loc) · 1.72 KB
1
document.addEventListener("DOMContentLoaded",()=>{const u=document.getElementById("codeInput");const e=document.getElementById("container");const n=document.createElement("pre");n.classList.add("mirror");e.appendChild(n);const t=()=>{const t=window.getComputedStyle(u);["border","fontFamily","fontSize","fontWeight","lineHeight","padding","borderRadius","wordWrap","whiteSpace"].forEach(e=>n.style[e]=t[e]);n.style.borderColor="transparent"};const m=()=>{n.innerHTML=Prism.highlight(u.value+"\n",Prism.languages.sql,"sql");n.scrollTop=u.scrollTop};const h=e=>{document.execCommand("insertText",false,e)};const c=()=>{const e=u.selectionStart;const t=u.selectionEnd;const n=u.value;const s=n.substring(0,e);console.log("beforeCursor:",s);const o=n.substring(t);const l=s.lastIndexOf("\n")+1;const c=o.indexOf("\n")!==-1?t+o.indexOf("\n"):n.length;const i=n.substring(l,c).split("\n");const r=i.every(e=>e.trim().startsWith("--"));let a="";let d=0;if(r){a=i.map(e=>e.replace(/^--\s?/,"")).join("\n");d=-3}else{a=i.map(e=>`-- ${e}`).join("\n");d=3}const g=i.length;u.setSelectionRange(l,c);h(a);const f=e+d;const p=t+d*g;u.setSelectionRange(f,p);m()};u.addEventListener("keydown",e=>{if((e.ctrlKey||e.metaKey)&&e.key==="/"){e.preventDefault();c()}if(e.key==="Tab"&&!e.shiftKey){e.preventDefault();const t=u.selectionStart;const n=u.selectionEnd;h(" ");u.setSelectionRange(t+4,t+4)}if(e.key==="Tab"&&e.shiftKey){e.preventDefault();const t=u.selectionStart;const s=u.value;const o=s.lastIndexOf("\n",t-1)+1;const l=s.substring(o,t);if(l.endsWith(" ")){u.setSelectionRange(o+l.length-4,t);h("");u.setSelectionRange(t-4,t-4)}}});u.addEventListener("input",m);u.addEventListener("scroll",()=>{n.scrollTop=u.scrollTop;n.scrollLeft=u.scrollLeft;m()});t();m()});