-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmin.main.js
More file actions
1 lines (1 loc) · 9.08 KB
/
min.main.js
File metadata and controls
1 lines (1 loc) · 9.08 KB
1
window.addEventListener("DOMContentLoaded",(()=>{let e,t,n=!1,s=1,i=0,o=0,d=null,l=null;const a=document.getElementById("windowsContainer"),c=document.getElementById("tabs"),r={javascript:["js","mjs","cjs"],"text/x-csrc":["c","h"],"text/x-c++src":["cpp","hpp","cc","cxx"],"text/x-java":["java"],"text/x-csharp":["cs"],"text/x-python":["py"],"text/html":["html","htm"],htmlmixed:["ejs","erb","jsp","php"],"text/css":["css"],"application/json":["json"],"text/xml":["xml"],"text/x-go":["go"],"text/x-ruby":["rb","rake"],"text/plain":["txt","log","md"]};function m(e,t){const n=document.createElement("div");n.classList.add("tab"),n.innerText=e,n._windowElement=t,t._tab=n,n.addEventListener("click",(()=>{n.classList.contains("minimized")?function(e){e.style.display="block",e._tab&&(e._tab.classList.remove("minimized"),e._tab.classList.add("active"),l=e._tab);e.classList.add("active"),d=e;const t=e._container,n=600,i=360,o=e._lastWidth||n,a=e._lastHeight||i;if(t.style.width=o*s+"px",t.style.height=a*s+"px",e._editor){const t=e._editor,n=t.getWrapperElement();n.style.width="100%",n.style.height="100%",n.style.fontSize=14*s+"px",t.refresh()}const c=e.querySelector(".window-header").offsetHeight;e.style.width=t.offsetWidth+"px",e.style.height=t.offsetHeight+c+"px"}(t):(l&&l.classList.remove("active"),n.classList.add("active"),l=n,d!==t&&(d&&d.classList.remove("active"),t.classList.add("active"),d=t))})),c.appendChild(n),l||(n.classList.add("active"),l=n)}function p(e,t){const n=document.createElement("div");n.classList.add("window");const c=document.createElement("div");c.classList.add("window-header"),c.innerText=e;const p=document.createElement("button");p.classList.add("close-btn"),p.innerText="X",p.addEventListener("click",(()=>{n._resizeObserver?.disconnect(),n.remove(),n._tab&&(n._tab.remove(),l===n._tab&&(l=null)),d===n&&(d=null);const e=document.querySelectorAll(".window");if(e.length>0){const t=e[e.length-1];t.classList.add("active"),d=t;const n=t._tab;n&&(n.classList.add("active"),l=n)}}));const u=document.createElement("button");u.classList.add("minimize-btn"),u.innerText="_",u.addEventListener("click",(()=>{const e=n._container;n._lastWidth=e.offsetWidth/s,n._lastHeight=e.offsetHeight/s,n.style.display="none",n._tab&&(n._tab.classList.remove("active"),n._tab.classList.add("minimized")),d===n&&(d=null),l===n._tab&&(l=null)}));n.style.left=-i+(window.innerWidth-600)/2+"px",n.style.top=-o+(window.innerHeight-360)/2+"px";const h=document.createElement("div");h.classList.add("editor-container"),h.style.width=600*s+"px",h.style.height=360*s+"px",h.style.resize="both",h.style.overflow="hidden",n.appendChild(c),n.appendChild(p),n.appendChild(u),n.appendChild(h),a.appendChild(n),m(e,n),d&&d.classList.remove("active"),n.classList.add("active"),d=n,f(n),function(e,t,n=""){const i=function(e){const t=e.split(".").pop().toLowerCase();for(const[e,n]of Object.entries(r))if(n.includes(t))return e;return"text/plain"}(n),o=CodeMirror(t,{mode:i,lineNumbers:!0,value:"",viewportMargin:1/0});o.setOption("theme","isotope");const d=o.getWrapperElement();d.style.width="100%",d.style.height="100%",d.style.fontSize=14*s+"px",o.refresh();const l=new ResizeObserver((()=>{const n=t.getBoundingClientRect(),s=e.querySelector(".window-header").offsetHeight;e.style.width=n.width+"px",e.style.height=n.height+s+"px",o.refresh()}));l.observe(t),e._editor=o,e._container=t,e._resizeObserver=l}(n,h,e),n._editor.setValue(t),n.dataset.scale=s}function u(e){if(e.type&&e.type.startsWith("image/"))return!0;const t=e.name.split(".").pop().toLowerCase();return["png","jpg","jpeg","gif","bmp","webp","svg"].includes(t)}function h(e,t){const n=document.createElement("div");n.classList.add("window");const c=document.createElement("div");c.classList.add("window-header"),c.innerText=e;const r=document.createElement("button");r.classList.add("close-btn"),r.innerText="X",r.addEventListener("click",(()=>{n._resizeObserver?.disconnect(),n.remove(),n._tab&&(n._tab.remove(),l===n._tab&&(l=null)),d===n&&(d=null);const e=document.querySelectorAll(".window");if(e.length>0){const t=e[e.length-1];t.classList.add("active"),d=t;const n=t._tab;n&&(n.classList.add("active"),l=n)}}));const p=document.createElement("button");p.classList.add("minimize-btn"),p.innerText="_",p.addEventListener("click",(()=>{const e=n._container;n._lastWidth=e.offsetWidth/s,n._lastHeight=e.offsetHeight/s,n.style.display="none",n._tab&&(n._tab.classList.remove("active"),n._tab.classList.add("minimized")),d===n&&(d=null),l===n._tab&&(l=null)}));n.style.left=-i+(window.innerWidth-600)/2+"px",n.style.top=-o+(window.innerHeight-360)/2+"px";const u=document.createElement("div");u.classList.add("editor-container"),u.style.width=600*s+"px",u.style.height=360*s+"px",u.style.resize="both",u.style.overflow="hidden",u.style.display="flex",u.style.alignItems="center",u.style.justifyContent="center";const h=document.createElement("img");h.src=t,h.style.width="100%",h.style.height="100%",h.style.objectFit="contain",h.style.display="block",h.draggable=!1,u.appendChild(h),n.appendChild(c),n.appendChild(r),n.appendChild(p),n.appendChild(u),a.appendChild(n),m(e,n),d&&d.classList.remove("active"),n.classList.add("active"),d=n,f(n);const v=new ResizeObserver((()=>{const e=u.getBoundingClientRect(),t=n.querySelector(".window-header").offsetHeight;n.style.width=e.width+"px",n.style.height=e.height+t+"px"}));v.observe(u),n._container=u,n._resizeObserver=v,n._isImageWindow=!0,n._imageElement=h}function f(e){const t=e.querySelector(".window-header");let s,i,o,l,a=!1,c=!1;t.style.cursor="grab",t.addEventListener("mousedown",(r=>{0===r.button&&(c=!0,d!==e&&(e.classList.add("active"),d&&d.classList.remove("active"),d=e),n||(a=!0,s=r.clientX,i=r.clientY,o=e.offsetLeft,l=e.offsetTop,t.style.cursor="grabbing"),r.preventDefault())})),document.addEventListener("mousemove",(t=>{if(!a||n)return;const d=t.clientX-s,c=t.clientY-i;e.style.left=o+d+"px",e.style.top=l+c+"px"})),document.addEventListener("mouseup",(e=>{0===e.button&&(c=!1,a=!1,t.style.cursor="grab")}));e._onPanEnd=(n,r)=>{c&&(a=!0,s=n,i=r,o=e.offsetLeft,l=e.offsetTop,t.style.cursor="grabbing",e.classList.add("active"),d&&d!==e&&d.classList.remove("active"),d=e)}}function v(e){if(e._isImageWindow&&e._imageElement){const t=e.querySelector(".window-header").innerText||"image.png",n=document.createElement("a");n.href=e._imageElement.src,n.download=t,document.body.appendChild(n),n.click(),document.body.removeChild(n)}else{const t=e._editor,n=e.querySelector(".window-header").innerText||"untitled.txt",s=t.getValue(),i=new Blob([s],{type:"text/plain;charset=utf-8"}),o=document.createElement("a");o.href=URL.createObjectURL(i),o.download=n,document.body.appendChild(o),o.click(),document.body.removeChild(o)}}document.addEventListener("wheel",(e=>{if(e.altKey)return;e.preventDefault();const t=e.deltaY>0?.9:1.1;!function(e,t,n){const i=(n=Math.min(Math.max(n,.5),3))/s;s=n,document.querySelectorAll(".window").forEach((n=>{if("none"===n.style.display)return;const o=n._container,d=n.getBoundingClientRect(),l=e-d.left,a=t-d.top,c=o.offsetWidth*i,r=o.offsetHeight*i;if(o.style.width=c+"px",o.style.height=r+"px",!n._isImageWindow){const e=n._editor;if(e){const t=e.getWrapperElement();t.style.width="100%",t.style.height="100%",t.style.fontSize=14*s+"px",e.refresh()}}const m=n.querySelector(".window-header").offsetHeight;n.style.width=o.offsetWidth+"px",n.style.height=o.offsetHeight+m+"px";const p=l*(i-1),u=a*(i-1);n.style.left=n.offsetLeft-p+"px",n.style.top=n.offsetTop-u+"px"}))}(e.clientX,e.clientY,s*t)}),{passive:!1}),document.addEventListener("mousedown",(s=>{1===s.button&&(n=!0,e=s.clientX,t=s.clientY,s.preventDefault())})),document.addEventListener("mousemove",(s=>{if(!n)return;const d=s.clientX-e,l=s.clientY-t;i+=d,o+=l,a.style.transform=`translate(${i}px, ${o}px)`,e=s.clientX,t=s.clientY})),document.addEventListener("mouseup",(s=>{1===s.button&&(n=!1,e=t=null,document.querySelectorAll(".window").forEach((e=>{e._onPanEnd&&e._onPanEnd(s.clientX,s.clientY)})))})),document.getElementById("createWindowBtn").addEventListener("click",(()=>{let e=prompt("Enter file name:");e&&p(e,"")})),document.getElementById("openFileBtn").addEventListener("click",(()=>{const e=document.getElementById("fileInput");e.accept="",e.value="",e.click()})),document.getElementById("fileInput").addEventListener("change",(e=>{const t=e.target.files[0];if(t)if(u(t)){const e=new FileReader;e.onload=e=>h(t.name,e.target.result),e.readAsDataURL(t)}else{const e=new FileReader;e.onload=e=>p(t.name,e.target.result),e.readAsText(t)}})),document.addEventListener("dragover",(e=>e.preventDefault())),document.addEventListener("drop",(e=>{e.preventDefault();Array.from(e.dataTransfer.files).forEach((e=>{if(u(e)){const t=new FileReader;t.onload=t=>h(e.name,t.target.result),t.readAsDataURL(e)}else{const t=new FileReader;t.onload=t=>p(e.name,t.target.result),t.readAsText(e)}}))})),document.getElementById("saveFileBtn").addEventListener("click",(()=>{const e=Array.from(document.querySelectorAll(".window"));if(!e.length)return alert("No open windows to save.");const t=prompt("Enter 1 to save current file or 2 to save all files:","1");if("1"===t||"2"===t)if("1"===t){if(!d)return alert("No active window to save.");v(d)}else e.forEach((e=>v(e)))}))}));