modern_spreadsheet/assets/index-a70da1df.js

5 lines
15 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var w=Object.defineProperty;var m=(n,e,t)=>e in n?w(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var o=(n,e,t)=>(m(n,typeof e!="symbol"?e+"":e,t),t);(function(){const e=document.createElement("link").relList;if(e&&e.supports&&e.supports("modulepreload"))return;for(const l of document.querySelectorAll('link[rel="modulepreload"]'))s(l);new MutationObserver(l=>{for(const i of l)if(i.type==="childList")for(const r of i.addedNodes)r.tagName==="LINK"&&r.rel==="modulepreload"&&s(r)}).observe(document,{childList:!0,subtree:!0});function t(l){const i={};return l.integrity&&(i.integrity=l.integrity),l.referrerPolicy&&(i.referrerPolicy=l.referrerPolicy),l.crossOrigin==="use-credentials"?i.credentials="include":l.crossOrigin==="anonymous"?i.credentials="omit":i.credentials="same-origin",i}function s(l){if(l.ep)return;l.ep=!0;const i=t(l);fetch(l.href,i)}})();class a{constructor(e,t){o(this,"x");o(this,"y");o(this,"width");o(this,"height");this.x=this.getXCoord(t.column,e),this.y=this.getYCoord(t.row,e),this.width=e.columns[t.column].width,this.height=e.rows[t.row].height}getXCoord(e,t){let s=0;for(let l=0;l<e;l++)s+=t.columns[l].width;return s}getYCoord(e,t){let s=0;for(let l=0;l<e;l++)s+=t.rows[l].height;return s}}class f{constructor(e){o(this,"element");o(this,"root");o(this,"handleKeydown",e=>{const{key:t}=e;switch(t){case"Escape":{this.hide();break}case"Enter":this.root.changeCellValues(this.root.selection.selectedCell,{value:this.element.value,displayValue:this.element.value}),this.hide()}});o(this,"handleClickOutside",e=>{const t=e.target;this.element.contains(t)||this.hide()});this.root=e;const t=document.createElement("input");t.classList.add("editor"),this.element=t,this.hide()}hide(){this.element.style.display="none",this.element.classList.add("hide"),this.element.blur(),window.removeEventListener("click",this.handleClickOutside),this.element.removeEventListener("keydown",this.handleKeydown),this.root.focusTable()}show(e){const{height:t,width:s,x:l,y:i}=new a(this.root.config,e),r=this.root.getCell(e);this.element.classList.remove("hide"),this.element.style.top=i-this.root.viewport.top+"px",this.element.style.left=l-this.root.viewport.left+"px",this.element.style.width=s+"px",this.element.style.height=t+"px",this.element.style.display="block",window.addEventListener("click",this.handleClickOutside),this.element.addEventListener("keydown",this.handleKeydown),this.element.value=r.value,this.element.focus(),this.element.select()}}class g{constructor(e){o(this,"element");o(this,"root");this.root=e;const t=document.createElement("header");t.classList.add(),this.element=t}}class C{constructor(e){o(this,"element");o(this,"verticalScroller");o(this,"horizontalScroller");o(this,"root");o(this,"isSelecting",!1);o(this,"handleMouseMove",e=>{if(!this.isSelecting)return;const{offsetX:t,offsetY:s}=e,l=this.root.getCellByCoords(t,s);this.root.selection.selectedRange&&(this.root.selection.selectedRange.to=l),this.root.renderSheet()});o(this,"handleMouseUp",()=>{this.isSelecting=!1,this.root.selection.selectedRange&&this.root.selection.selectedRange.from.row===this.root.selection.selectedRange.to.row&&this.root.selection.selectedRange.from.column===this.root.selection.selectedRange.to.column&&(this.root.selection.selectedRange=null),this.root.renderSheet()});o(this,"handleDoubleClick",e=>{e.preventDefault();const t=this.root.getCellByCoords(e.offsetX,e.offsetY);this.root.showEditor(t)});o(this,"handleKeydown",e=>{if(console.log(e),["ArrowLeft","ArrowRight","ArrowUp","ArrowDown"].includes(e.key))switch(e.preventDefault(),this.root.selection.selectedRange=null,e.key){case"ArrowLeft":{this.root.selection.selectedCell&&this.root.selection.selectedCell.column>0&&(console.log("tick"),this.root.selection.selectedCell.column-=1,this.root.renderSheet());break}case"ArrowRight":{this.root.selection.selectedCell&&this.root.selection.selectedCell.column<this.root.config.columns.length-1&&(this.root.selection.selectedCell.column+=1,this.root.renderSheet());break}case"ArrowUp":{this.root.selection.selectedCell&&this.root.selection.selectedCell.row>0&&(this.root.selection.selectedCell.row-=1,this.root.renderSheet());break}case"ArrowDown":{this.root.selection.selectedCell&&this.root.selection.selectedCell.row<this.root.config.rows.length-1&&(this.root.selection.selectedCell.row+=1,this.root.renderSheet());break}}if(!e.metaKey&&!e.ctrlKey&&(e.key==="F2"||/^([a-z]|[а-я])$/.test(e.key.toLowerCase()))){if(e.preventDefault(),!this.root.selection.selectedCell)return;this.root.showEditor(this.root.selection.selectedCell)}e.key==="Delete"&&(e.preventDefault(),this.root.deleteSelectedCellsValues(),this.root.renderSheet())});o(this,"handleClick",e=>{if(e.button!==0)return;const{offsetX:t,offsetY:s}=e,l=this.root.getCellByCoords(t,s);this.isSelecting=!0,this.root.selection.selectedRange={from:l,to:l},this.root.selection.selectedCell=l,this.root.renderSheet()});o(this,"handleScroll",()=>{const e=this.getViewportBoundlingRect();this.root.viewport.updateValues(e),this.root.renderSheet()});this.root=e;const{horizontalScroller:t,scroller:s,verticalScroller:l}=this.buildComponent();this.element=s,this.verticalScroller=l,this.horizontalScroller=t,this.element.style.height=this.root.config.view.height+"px",this.element.style.width=this.root.config.view.width+"px",this.element.tabIndex=-1,this.updateScrollerSize(),this.element.addEventListener("scroll",this.handleScroll),this.element.addEventListener("mousedown",this.handleClick),this.element.addEventListener("mousemove",this.handleMouseMove),this.element.addEventListener("mouseup",this.handleMouseUp),this.element.addEventListener("dblclick",this.handleDoubleClick),this.element.addEventListener("keydown",this.handleKeydown)}getViewportBoundlingRect(){const{scrollTop:e,scrollLeft:t}=this.element,{height:s,width:l}=this.element.getBoundingClientRect(),i=e+s,r=t+l;return{top:e,left:t,bottom:i,right:r}}buildComponent(){const e=document.createElement("div"),t=document.createElement("div"),s=document.createElement("div"),l=document.createElement("div"),i=document.createElement("div");return t.style.width="0px",t.style.pointerEvents="none",s.style.pointerEvents="none",l.style.display="flex",i.appendChild(t),i.appendChild(s),l.appendChild(i),this.verticalScroller=t,this.horizontalScroller=s,e.appendChild(l),e.classList.add("scroller"),{scroller:e,verticalScroller:t,horizontalScroller:s}}getActualHeight(){return this.root.config.rows.reduce((e,t)=>(e+=t.height,e),0)}getActualWidth(){return this.root.config.columns.reduce((e,t)=>(e+=t.width,e),0)}updateScrollerSize(){const e=this.getActualHeight(),t=this.getActualWidth();this.setScrollerHeight(e),this.setScrollerWidth(t)}setScrollerHeight(e){this.verticalScroller.style.height=e+"px"}setScrollerWidth(e){this.horizontalScroller.style.width=e+"px"}}class p{constructor(e){o(this,"fontSize",16);o(this,"fontColor","black");o(this,"background","white");o(this,"borderColor","black");o(this,"selectedBackground","#4287f5");o(this,"selectedFontColor","#ffffff");e&&Object.assign(this,e)}}class y{constructor(e,t){o(this,"row");o(this,"column");this.row=e,this.column=t}}class v{constructor(e){o(this,"value");o(this,"displayValue");o(this,"resultValue");o(this,"position");o(this,"style",new p);this.value=e.value,this.displayValue=e.displayValue,this.resultValue=e.resultValue,this.position=e.position}changeValues(e){Object.assign(this,e)}isCellInRange(e){const{column:t,row:s}=this.position,{selectedRange:l}=e.selection;if(!l)return!1;const i=s>=Math.min(l.from.row,l.to.row)&&s<=Math.max(l.to.row,l.from.row);return t>=Math.min(l.from.column,l.to.column)&&t<=Math.max(l.to.column,l.from.column)&&i}render(e){var d;let{height:t,width:s,x:l,y:i}=new a(e.config,this.position);const{ctx:r}=e,h=((d=e.selection.selectedCell)==null?void 0:d.row)===this.position.row&&e.selection.selectedCell.column===this.position.column,c=this.isCellInRange(e);i-=e.viewport.top,l-=e.viewport.left,r.clearRect(l,i,s,t),r.fillStyle=h||c?this.style.selectedBackground:this.style.background,r.strokeStyle="black",r.fillRect(l,i,s-1,t-1),r.strokeRect(l,i,s,t),r.fillStyle=h||c?this.style.selectedFontColor:this.style.fontColor,r.textAlign="left",r.font=`${this.style.fontSize}px Arial`,r.textBaseline="middle",r.fillText(this.displayValue,l+2,i+t/2,s)}}class x{constructor(e){o(this,"element");o(this,"ctx");o(this,"root");this.root=e;const t=document.createElement("canvas");t.classList.add("sheet"),t.height=this.root.config.view.height,t.width=this.root.config.view.width,t.style.width=this.root.config.view.width+"px",t.style.height=this.root.config.view.height+"px",this.element=t;const s=this.element.getContext("2d");if(!s)throw new Error("Enable hardware acceleration");this.ctx=s}getCellByCoords(e,t){let s=0,l=0;for(;l<=t&&(l+=this.root.config.rows[s].height,!(l>=t));)s++;let i=0,r=0;for(;r<=e&&(r+=this.root.config.columns[i].width,!(r>=e));)i++;return new y(s,i)}renderCell(e){const{column:t,row:s}=e;this.root.data[s][t].render(this.root)}renderSheet(){const e=this.root.viewport.firstRow,t=this.root.viewport.lastCol+3,s=this.root.viewport.lastRow+3,l=this.root.viewport.firstCol;for(let i=e;i<=s;i++)for(let r=l;r<=t&&!(!this.root.config.columns[r]||!this.root.config.rows[i]);r++)this.renderCell({column:r,row:i})}}class S{constructor(e){o(this,"element");o(this,"root");this.root=e;const t=document.createElement("div");t.classList.add("spreadsheet_container"),this.element=t,this.changeElementSizes(this.root.viewProps)}changeElementSizes(e){const{height:t,width:s}=e;this.element.style.width=s+"px",this.element.style.height=t+"px"}}class b{constructor(e){o(this,"element");o(this,"root");this.root=e;const t=document.createElement("div");t.classList.add("toolbar"),this.element=t}}class u{constructor(e){o(this,"rows");o(this,"columns");o(this,"view",{width:800,height:600});this.columns=e.columns,this.rows=e.rows,this.view=e.view}}class R{constructor(){o(this,"selectedCell",null);o(this,"selectedRange",null)}}class E{}class k{constructor(e,t){o(this,"root");o(this,"top");o(this,"left");o(this,"right");o(this,"bottom");o(this,"firstRow");o(this,"lastRow");o(this,"firstCol");o(this,"lastCol");this.root=e,this.top=t.top,this.left=t.left,this.right=t.right,this.bottom=t.bottom,this.firstRow=this.getFirstRow(),this.lastCol=this.getFirstRow();//!Temp
this.firstCol=this.getFirstRow();//!Temp
this.lastRow=this.getLastRow(),this.updateValues({top:0,left:0,right:this.root.viewProps.width,bottom:this.root.viewProps.height})}updateValues(e){this.top=e.top,this.left=e.left,this.right=e.right,this.bottom=e.bottom,this.firstRow=this.getFirstRow(),this.lastRow=this.getLastRow(),this.firstCol=this.getFirstCol(),this.lastCol=this.getLastCol()}getFirstRow(){return this.root.cache.getRowByYCoord(this.top)}getLastRow(){return this.root.cache.getRowByYCoord(this.bottom)}getFirstCol(){return this.root.cache.getColumnByXCoord(this.left)}getLastCol(){return this.root.cache.getColumnByXCoord(this.right)}}class I{constructor(e){o(this,"width");o(this,"title");this.width=e.width,this.title=e.title}}class L{constructor(e){o(this,"height");o(this,"title");this.height=e.height,this.title=e.title}}function V(n,e){const t=[];for(let s=0;s<=n;s++){const l=[];for(let i=0;i<=e;i++){const r=`${s}:${i}`,h=new v({displayValue:r,resultValue:r,value:r,position:{column:i,row:s}});l.push(h)}t.push(l)}return t}function A(n,e){const t=[];for(let i=0;i<=n;i++){const r=new L({height:40,title:String(i)});t.push(r)}const s=[];for(let i=0;i<=e;i++){const r=new I({title:String(i),width:150});s.push(r)}return new u({columns:s,rows:t,view:{height:600,width:800}})}class B{constructor(e){o(this,"xPos");o(this,"colIdx");this.xPos=e.xPos,this.colIdx=e.colIdx}}class M{constructor(e){o(this,"yPos");o(this,"rowIdx");this.yPos=e.yPos,this.rowIdx=e.rowIdx}}class P{constructor(e){o(this,"columns");o(this,"rows");this.columns=e.columns,this.rows=e.rows}getRowByYCoord(e){let t=0;for(let s=0;s<this.rows.length;s++)if(e<=this.rows[s].yPos){t=s;break}return t}getColumnByXCoord(e){let t=0;for(let s=0;s<this.columns.length;s++)if(e<=this.columns[s].xPos){t=s;break}return t}}class T{constructor(e,t){o(this,"table");o(this,"scroller");o(this,"toolbar");o(this,"header");o(this,"sheet");o(this,"editor");o(this,"styles");o(this,"config");o(this,"data");o(this,"viewport");o(this,"selection");o(this,"cache");const s=A(500,500);t!=null&&t.view&&(s.view=t.view),this.config=new u(s),this.sheet=new x(this);const l=V(500,500);this.table=new S(this),this.scroller=new C(this),this.toolbar=new b(this),this.header=new g(this),this.editor=new f(this),this.cache=this.getInitialCache(),this.viewport=new k(this,this.scroller.getViewportBoundlingRect()),this.selection=new R,this.data=l,this.styles=new E,this.buildComponent(),this.appendTableToTarget(e),this.renderSheet()}getInitialCache(){const e=[];let t=0;for(let r=0;r<=this.config.columns.length-1;r++){const h=this.config.columns[r];t+=h.width;const c=new B({xPos:t,colIdx:r});e.push(c)}const s=[];let l=0;for(let r=0;r<=this.config.rows.length-1;r++){const h=this.config.rows[r];l+=h.height;const c=new M({yPos:l,rowIdx:r});s.push(c)}const i=new P({columns:e,rows:s});return console.log("CACHE: ",i),console.log("CONFIG: ",this.config),i}buildComponent(){const e=document.createElement("div");e.appendChild(this.header.element),e.appendChild(this.sheet.element),e.classList.add("content"),this.table.element.appendChild(this.toolbar.element),this.table.element.appendChild(e),this.table.element.appendChild(this.scroller.element),this.table.element.append(this.editor.element)}appendTableToTarget(e){if(typeof e=="string"){const t=document.querySelector(e);if(!t)throw new Error(`Element with selector ${e} is not finded in DOM.
Make sure it exists.`);t==null||t.appendChild(this.table.element)}e instanceof HTMLElement&&e.append(this.table.element)}get ctx(){return this.sheet.ctx}get viewProps(){return this.config.view}focusTable(){this.scroller.element.focus()}getCellByCoords(e,t){return this.sheet.getCellByCoords(e,t)}getCell(e){const{column:t,row:s}=e;return this.data[s][t]}changeCellValues(e,t){const{column:s,row:l}=e;this.data[l][s].changeValues(t),this.renderCell(l,s)}applyActionToRange(e,t){const s=Math.min(e.from.row,e.to.row),l=Math.max(e.from.row,e.to.row),i=Math.min(e.from.column,e.to.column),r=Math.max(e.from.column,e.to.column);for(let h=s;h<=l;h++)for(let c=i;c<=r;c++){const d=this.data[h][c];t(d)}}deleteSelectedCellsValues(){if(this.selection.selectedRange!==null)this.applyActionToRange(this.selection.selectedRange,e=>{this.changeCellValues(e.position,{displayValue:"",resultValue:"",value:""})});else{if(!this.selection.selectedCell)return;this.changeCellValues(this.selection.selectedCell,{displayValue:"",resultValue:"",value:""})}}showEditor(e){this.editor.show(e)}renderSheet(){this.sheet.renderSheet()}renderCell(e,t){this.data[e][t].render(this)}}const z=new T("#spreadsheet",{view:{height:768,width:1366}});console.log(z);