Formatted & Linted

Dist update
Updated version of spreadsheet
This commit is contained in:
Eugene 2023-07-27 12:45:15 +03:00
parent 1e2847f7f2
commit cddfc134f8
14 changed files with 212 additions and 163 deletions

View File

@ -10,5 +10,8 @@ export declare class Sheet {
constructor(root: Spreadsheet); constructor(root: Spreadsheet);
getCellByCoords(x: number, y: number): Position; getCellByCoords(x: number, y: number): Position;
renderCell(position: Position): void; renderCell(position: Position): void;
private getSelectionRange;
private renderSelectionRange;
renderSelection(): void;
renderSheet(): void; renderSheet(): void;
} }

10
dist/main.cjs vendored

File diff suppressed because one or more lines are too long

2
dist/main.cjs.map vendored

File diff suppressed because one or more lines are too long

1
dist/main.d.ts vendored
View File

@ -62,6 +62,7 @@ export default class Spreadsheet {
deleteSelectedCellsValues(): void; deleteSelectedCellsValues(): void;
showEditor(position: Position, initialString?: string): void; showEditor(position: Position, initialString?: string): void;
renderSheet(): void; renderSheet(): void;
renderSelection(): void;
renderColumnsBar(): void; renderColumnsBar(): void;
renderRowsBar(): void; renderRowsBar(): void;
renderCell(row: number, col: number): void; renderCell(row: number, col: number): void;

241
dist/main.js vendored
View File

@ -1,10 +1,10 @@
var I = Object.defineProperty; var I = Object.defineProperty;
var A = (r, t, e) => t in r ? I(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e; var A = (r, t, e) => t in r ? I(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e;
var s = (r, t, e) => (A(r, typeof t != "symbol" ? t + "" : t, e), e); var s = (r, t, e) => (A(r, typeof t != "symbol" ? t + "" : t, e), e);
var d; var u;
(function(r) { (function(r) {
r.CELL_CLICK = "CELL_CLICK", r.SELECTION_CHANGE = "CHANGE_SELECTION", r.CELL_CHANGE = "CELL_CHANGE", r.COPY_CELLS = "COPY_CELLS"; r.CELL_CLICK = "CELL_CLICK", r.SELECTION_CHANGE = "CHANGE_SELECTION", r.CELL_CHANGE = "CELL_CHANGE", r.COPY_CELLS = "COPY_CELLS";
})(d || (d = {})); })(u || (u = {}));
class P { class P {
constructor(t) { constructor(t) {
s(this, "root"); s(this, "root");
@ -30,22 +30,22 @@ class P {
} }
dispatch(t) { dispatch(t) {
switch (t.type) { switch (t.type) {
case d.CELL_CLICK: { case u.CELL_CLICK: {
const { event: e, scroller: o } = t; const { event: e, scroller: o } = t;
this.cellClick(e, o); this.cellClick(e, o);
break; break;
} }
case d.SELECTION_CHANGE: { case u.SELECTION_CHANGE: {
const { selection: e, enableCallback: o } = t; const { selection: e, enableCallback: o } = t;
this.changeSelection(e, o); this.changeSelection(e, o);
break; break;
} }
case d.CELL_CHANGE: { case u.CELL_CHANGE: {
const { cell: e, enableCallback: o } = t; const { cell: e, enableCallback: o } = t;
this.changeCellValues(e, o); this.changeCellValues(e, o);
break; break;
} }
case d.COPY_CELLS: { case u.COPY_CELLS: {
const { data: e, dataAsString: o, range: l } = t; const { data: e, dataAsString: o, range: l } = t;
this.copy(l, e, o); this.copy(l, e, o);
break; break;
@ -57,7 +57,7 @@ class P {
e && ((l = (o = this.root.config).onCellChange) == null || l.call(o, t)); e && ((l = (o = this.root.config).onCellChange) == null || l.call(o, t));
} }
} }
class y { class g {
constructor(t, e) { constructor(t, e) {
s(this, "x"); s(this, "x");
s(this, "y"); s(this, "y");
@ -96,9 +96,9 @@ class T {
value: this.element.value, value: this.element.value,
displayValue: this.element.value displayValue: this.element.value
}), this.root.events.dispatch({ }), this.root.events.dispatch({
type: d.CELL_CHANGE, type: u.CELL_CHANGE,
cell: this.root.getCell(this.root.selection.selectedCell) cell: this.root.getCell(this.root.selection.selectedCell)
}), this.hide(); }), this.hide(), this.root.renderSelection();
} }
} }
}); });
@ -108,17 +108,17 @@ class T {
}); });
this.root = t; this.root = t;
const e = document.createElement("input"); const e = document.createElement("input");
e.classList.add(m + "editor"), this.element = e, this.hide(); e.classList.add(f + "editor"), this.element = e, this.hide();
} }
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(); 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(t, e) { show(t, e) {
const { height: o, width: l, x: i, y: n } = new y(this.root.config, t), h = this.root.getCell(t); const { height: o, width: l, x: i, y: n } = new g(this.root.config, t), h = this.root.getCell(t);
this.element.classList.remove("hide"), this.element.style.top = n - this.root.viewport.top + this.root.columnsBarHeight + "px", this.element.style.left = i - this.root.viewport.left + this.root.rowsBarWidth + "px", this.element.style.width = l + "px", this.element.style.height = o + "px", this.element.style.display = "block", window.addEventListener("click", this.handleClickOutside), this.element.addEventListener("keydown", this.handleKeydown), this.element.value = e || h.value, this.element.focus(), e || this.element.select(); this.element.classList.remove("hide"), this.element.style.top = n - this.root.viewport.top + this.root.columnsBarHeight + "px", this.element.style.left = i - this.root.viewport.left + this.root.rowsBarWidth + "px", this.element.style.width = l + "px", this.element.style.height = o + "px", this.element.style.display = "block", window.addEventListener("click", this.handleClickOutside), this.element.addEventListener("keydown", this.handleKeydown), this.element.value = e || h.value, this.element.focus(), e || this.element.select();
} }
} }
function b(r, t) { function S(r, t) {
return r.column === t.column && r.row === t.row; return r.column === t.column && r.row === t.row;
} }
class H { class H {
@ -133,8 +133,8 @@ class H {
return; return;
const { offsetX: e, offsetY: o } = t, l = this.root.getCellByCoords(e, o); const { offsetX: e, offsetY: o } = t, l = this.root.getCellByCoords(e, o);
let i = !1; let i = !1;
this.root.selection.selectedRange && (i = !b(this.root.selection.selectedRange.to, l), i && (this.root.selection.selectedRange.to = l, this.root.events.dispatch({ this.root.selection.selectedRange && (i = !S(this.root.selection.selectedRange.to, l), i && (this.root.selection.selectedRange.to = l, this.root.events.dispatch({
type: d.SELECTION_CHANGE, type: u.SELECTION_CHANGE,
selection: this.root.selection, selection: this.root.selection,
enableCallback: !0 enableCallback: !0
}))); })));
@ -142,8 +142,8 @@ class H {
s(this, "handleMouseUp", () => { s(this, "handleMouseUp", () => {
this.isSelecting = !1; this.isSelecting = !1;
const t = { ...this.root.selection }; const t = { ...this.root.selection };
this.root.selection.selectedRange && b(this.root.selection.selectedRange.from, this.root.selection.selectedRange.to) && (t.selectedRange = null, this.root.events.dispatch({ this.root.selection.selectedRange && S(this.root.selection.selectedRange.from, this.root.selection.selectedRange.to) && (t.selectedRange = null, this.root.events.dispatch({
type: d.SELECTION_CHANGE, type: u.SELECTION_CHANGE,
selection: t, selection: t,
enableCallback: !1 enableCallback: !1
})), this.root.renderSheet(), this.root.renderColumnsBar(), this.root.renderRowsBar(); })), this.root.renderSheet(), this.root.renderColumnsBar(), this.root.renderRowsBar();
@ -174,7 +174,7 @@ class H {
} }
} }
this.root.events.dispatch({ this.root.events.dispatch({
type: d.SELECTION_CHANGE, type: u.SELECTION_CHANGE,
selection: this.root.selection, selection: this.root.selection,
enableCallback: !0 enableCallback: !0
}); });
@ -189,7 +189,7 @@ class H {
} }
} }
if (t.key === "Delete" && (t.preventDefault(), this.root.deleteSelectedCellsValues(), this.root.renderSheet()), t.metaKey || t.ctrlKey) { if (t.key === "Delete" && (t.preventDefault(), this.root.deleteSelectedCellsValues(), this.root.renderSheet()), t.metaKey || t.ctrlKey) {
if (console.log(t.code), t.code === "KeyC") { if (t.code === "KeyC") {
let o; let o;
const l = new v(); const l = new v();
if (this.root.selection.selectedRange) { if (this.root.selection.selectedRange) {
@ -211,7 +211,7 @@ class H {
}); });
s(this, "handleClick", (t) => { s(this, "handleClick", (t) => {
this.root.events.dispatch({ this.root.events.dispatch({
type: d.CELL_CLICK, type: u.CELL_CLICK,
event: t, event: t,
scroller: this scroller: this
}); });
@ -240,7 +240,7 @@ class H {
} }
buildComponent() { buildComponent() {
const t = document.createElement("div"), e = document.createElement("div"), o = document.createElement("div"), l = document.createElement("div"), i = document.createElement("div"); const t = document.createElement("div"), e = document.createElement("div"), o = document.createElement("div"), l = document.createElement("div"), i = document.createElement("div");
return e.style.width = "0px", e.style.pointerEvents = "none", o.style.pointerEvents = "none", l.style.display = "flex", i.appendChild(e), i.appendChild(o), l.appendChild(i), this.verticalScroller = e, this.horizontalScroller = o, t.appendChild(l), t.contentEditable = "false", t.classList.add(m + "scroller"), { scroller: t, verticalScroller: e, horizontalScroller: o }; return e.style.width = "0px", e.style.pointerEvents = "none", o.style.pointerEvents = "none", l.style.display = "flex", i.appendChild(e), i.appendChild(o), l.appendChild(i), this.verticalScroller = e, this.horizontalScroller = o, t.appendChild(l), t.contentEditable = "false", t.classList.add(f + "scroller"), { scroller: t, verticalScroller: e, horizontalScroller: o };
} }
getActualHeight() { getActualHeight() {
return this.root.config.rows.reduce((t, e) => (t += e.height, t), 0); return this.root.config.rows.reduce((t, e) => (t += e.height, t), 0);
@ -270,14 +270,14 @@ class R {
t && Object.assign(this, t); t && Object.assign(this, t);
} }
} }
class M { class D {
constructor(t, e) { constructor(t, e) {
s(this, "row"); s(this, "row");
s(this, "column"); s(this, "column");
this.row = t, this.column = e; this.row = t, this.column = e;
} }
} }
class _ { class M {
constructor(t) { constructor(t) {
s(this, "value"); s(this, "value");
s(this, "displayValue"); s(this, "displayValue");
@ -287,7 +287,7 @@ class _ {
this.value = t.value, this.displayValue = t.displayValue, this.resultValue = t.resultValue, this.position = t.position, this.style = t.style; this.value = t.value, this.displayValue = t.displayValue, this.resultValue = t.resultValue, this.position = t.position, this.style = t.style;
} }
} }
class x { class p {
constructor(t) { constructor(t) {
/** True value (data) */ /** True value (data) */
s(this, "value"); s(this, "value");
@ -300,7 +300,7 @@ class x {
this.value = t.value, this.displayValue = t.displayValue, this.resultValue = t.resultValue, this.position = t.position, this.style = t.style; this.value = t.value, this.displayValue = t.displayValue, this.resultValue = t.resultValue, this.position = t.position, this.style = t.style;
} }
getSerializableCell() { getSerializableCell() {
return new _({ return new M({
displayValue: this.displayValue, displayValue: this.displayValue,
position: this.position, position: this.position,
resultValue: this.resultValue, resultValue: this.resultValue,
@ -314,31 +314,35 @@ class x {
changeValues(t) { changeValues(t) {
Object.assign(this, t); Object.assign(this, t);
} }
isCellInRange(t) { // private isCellInRange(root: Spreadsheet): boolean {
const { column: e, row: o } = this.position, { selectedRange: l } = t.selection; // const { column, row } = this.position;
if (!l) // const { selectedRange } = root.selection;
return !1; // if (!selectedRange) return false;
const i = o >= Math.min(l.from.row, l.to.row) && o <= Math.max(l.to.row, l.from.row); // const isCellInRow =
return e >= Math.min(l.from.column, l.to.column) && e <= Math.max(l.to.column, l.from.column) && i; // row >= Math.min(selectedRange.from.row, selectedRange.to.row) &&
} // row <= Math.max(selectedRange.to.row, selectedRange.from.row);
// const isCellInCol =
// column >= Math.min(selectedRange.from.column, selectedRange.to.column) &&
// column <= Math.max(selectedRange.to.column, selectedRange.from.column);
// return isCellInCol && isCellInRow;
// }
render(t) { render(t) {
var C; const e = new g(t.config, this.position);
const e = new y(t.config, this.position);
let { x: o, y: l } = e; let { x: o, y: l } = e;
const { height: i, width: n } = e, { ctx: h } = t, c = ((C = t.selection.selectedCell) == null ? void 0 : C.row) === this.position.row && t.selection.selectedCell.column === this.position.column, a = this.isCellInRange(t); const { height: i, width: n } = e, { ctx: h } = t;
l -= t.viewport.top, o -= t.viewport.left; l -= t.viewport.top, o -= t.viewport.left;
const u = this.style ?? t.styles.cells; const c = this.style ?? t.styles.cells;
h.clearRect(o, l, n, i), h.fillStyle = c || a ? u.selectedBackground : u.background, h.strokeStyle = "black", h.fillRect(o, l, n - 1, i - 1), h.strokeRect(o, l, n, i), h.fillStyle = c || a ? u.selectedFontColor : u.fontColor, h.textAlign = "left", h.font = `${u.fontSize}px Arial`, h.textBaseline = "middle", h.fillText(this.displayValue, o + 2, l + i / 2); h.clearRect(o, l, n, i), h.fillStyle = c.background, h.strokeStyle = "black", h.fillRect(o, l, n - 1, i - 1), h.strokeRect(o, l, n, i), h.fillStyle = c.fontColor, h.textAlign = "left", h.font = `${c.fontSize}px Arial`, h.textBaseline = "middle", h.fillText(this.displayValue, o + 2, l + i / 2);
} }
} }
class D { class _ {
constructor(t) { constructor(t) {
s(this, "element"); s(this, "element");
s(this, "ctx"); s(this, "ctx");
s(this, "root"); s(this, "root");
this.root = t; this.root = t;
const e = document.createElement("canvas"); const e = document.createElement("canvas");
e.classList.add(m + "sheet"), e.height = this.root.config.view.height, e.width = this.root.config.view.width, e.style.width = this.root.config.view.width + "px", e.style.height = this.root.config.view.height + "px", e.style.left = "0px", this.element = e; e.classList.add(f + "sheet"), e.height = this.root.config.view.height, e.width = this.root.config.view.width, e.style.width = this.root.config.view.width + "px", e.style.height = this.root.config.view.height + "px", e.style.left = "0px", this.element = e;
const o = this.element.getContext("2d"); const o = this.element.getContext("2d");
if (!o) if (!o)
throw new Error("Enable hardware acceleration"); throw new Error("Enable hardware acceleration");
@ -351,17 +355,51 @@ class D {
let i = 0, n = 0; let i = 0, n = 0;
for (; n <= t && (n += this.root.config.columns[i].width, !(n >= t)); ) for (; n <= t && (n += this.root.config.columns[i].width, !(n >= t)); )
i++; i++;
return new M(o, i); return new D(o, i);
} }
renderCell(t) { renderCell(t) {
const { column: e, row: o } = t; const { column: e, row: o } = t;
this.root.data[o][e].render(this.root); this.root.data[o][e].render(this.root);
} }
getSelectionRange() {
const { selectedCell: t, selectedRange: e } = this.root.selection;
if (!(!t && !e)) {
if (e) {
const o = Math.min(e.from.row, e.to.row), l = Math.min(e.from.column, e.to.column), i = Math.max(e.from.row, e.to.row), n = Math.max(e.from.column, e.to.column), h = new g(this.root.config, {
row: o,
column: l
});
let c = 0;
for (let d = l; d <= n; d++)
c += this.root.config.columns[d].width;
let a = 0;
for (let d = o; d <= i; d++)
a += this.root.config.rows[d].height;
const m = h.x - this.root.viewport.left, C = h.y - this.root.viewport.top;
return { x: m, y: C, height: a, width: c };
}
if (!e && t) {
const o = new g(this.root.config, t);
return o.x -= this.root.viewport.left, o.y -= this.root.viewport.top, o;
}
}
}
renderSelectionRange(t, e, o, l) {
this.ctx.save(), this.ctx.strokeStyle = "#47d1ff", this.ctx.lineWidth = 3, this.ctx.strokeRect(t, e, o, l), this.ctx.fillStyle = "#7da8ff50", this.ctx.fillRect(t, e, o, l), this.ctx.restore();
}
renderSelection() {
const t = this.getSelectionRange();
if (!t)
return;
const { height: e, width: o, x: l, y: i } = t;
this.renderSelectionRange(l, i, o, e);
}
renderSheet() { renderSheet() {
const t = this.root.viewport.firstRow, e = this.root.viewport.lastCol + 3, o = this.root.viewport.lastRow + 3, l = this.root.viewport.firstCol; const t = this.root.viewport.firstRow, e = this.root.viewport.lastCol + 3, o = this.root.viewport.lastRow + 3, l = this.root.viewport.firstCol;
for (let i = t; i <= o; i++) for (let i = t; i <= o; i++)
for (let n = l; n <= e && !(!this.root.config.columns[n] || !this.root.config.rows[i]); n++) for (let n = l; n <= e && !(!this.root.config.columns[n] || !this.root.config.rows[i]); n++)
this.renderCell({ column: n, row: i }); this.renderCell({ column: n, row: i });
this.renderSelection();
} }
} }
class z { class z {
@ -370,24 +408,24 @@ class z {
s(this, "root"); s(this, "root");
this.root = t; this.root = t;
const e = document.createElement("div"); const e = document.createElement("div");
e.classList.add(m + "spreadsheet_container"), this.element = e, this.changeElementSizes(this.root.viewProps); e.classList.add(f + "spreadsheet_container"), this.element = e, this.changeElementSizes(this.root.viewProps);
} }
changeElementSizes(t) { changeElementSizes(t) {
const { height: e, width: o } = t; const { height: e, width: o } = t;
this.element.style.width = o + this.root.rowsBarWidth + "px", this.element.style.height = e + this.root.columnsBarHeight + "px"; this.element.style.width = o + this.root.rowsBarWidth + "px", this.element.style.height = e + this.root.columnsBarHeight + "px";
} }
} }
class N { class O {
constructor(t) { constructor(t) {
s(this, "element"); s(this, "element");
s(this, "root"); s(this, "root");
s(this, "height", 0); s(this, "height", 0);
this.root = t; this.root = t;
const e = document.createElement("div"); const e = document.createElement("div");
e.classList.add(m + "toolbar"), this.element = e; e.classList.add(f + "toolbar"), this.element = e;
} }
} }
class p { class x {
constructor(t) { constructor(t) {
s(this, "rows"); s(this, "rows");
s(this, "columns"); s(this, "columns");
@ -408,13 +446,13 @@ class v {
s(this, "selectedRange", null); s(this, "selectedRange", null);
} }
} }
class O { class N {
constructor() { constructor() {
s(this, "cells"); s(this, "cells");
this.cells = new R(); this.cells = new R();
} }
} }
class S { class b {
constructor(t, e) { constructor(t, e) {
s(this, "root"); s(this, "root");
s(this, "top"); s(this, "top");
@ -467,12 +505,12 @@ class k {
this.height = t.height, this.title = t.title; this.height = t.height, this.title = t.title;
} }
} }
function B(r, t, e = !1) { function L(r, t, e = !1) {
const o = []; const o = [];
for (let l = 0; l <= r; l++) { for (let l = 0; l <= r; l++) {
const i = []; const i = [];
for (let n = 0; n <= t; n++) { for (let n = 0; n <= t; n++) {
const h = e ? `${l}:${n}` : "", c = new x({ const h = e ? `${l}:${n}` : "", c = new p({
displayValue: h, displayValue: h,
resultValue: h, resultValue: h,
value: h, value: h,
@ -505,7 +543,7 @@ function K(r, t) {
}); });
o.push(n); o.push(n);
} }
return new p({ return new x({
columns: o, columns: o,
rows: e, rows: e,
view: { view: {
@ -515,17 +553,17 @@ function K(r, t) {
}); });
} }
function U(r, t) { function U(r, t) {
const e = B(r, t), o = K(r, t); const e = L(r, t), o = K(r, t);
return { data: e, config: o }; return { data: e, config: o };
} }
class F { class W {
constructor(t) { constructor(t) {
s(this, "xPos"); s(this, "xPos");
s(this, "colIdx"); s(this, "colIdx");
this.xPos = t.xPos, this.colIdx = t.colIdx; this.xPos = t.xPos, this.colIdx = t.colIdx;
} }
} }
class W { class F {
constructor(t) { constructor(t) {
s(this, "yPos"); s(this, "yPos");
s(this, "rowIdx"); s(this, "rowIdx");
@ -596,12 +634,12 @@ class G {
} }
renderRect(t, e) { renderRect(t, e) {
const { width: o, x: l } = e, i = this.isColumnSelected(t); const { width: o, x: l } = e, i = this.isColumnSelected(t);
this.ctx.fillStyle = i ? this.root.styles.cells.selectedBackground : "white", this.ctx.strokeStyle = "black", this.ctx.lineWidth = 1; this.ctx.fillStyle = i ? "#c7ebff" : "white", this.ctx.strokeStyle = "black", this.ctx.lineWidth = 1;
const n = l - this.root.viewport.left; const n = l - this.root.viewport.left;
this.ctx.fillRect(n - 1, 0, o, this.height), this.ctx.strokeRect(n - 1, 0, o, this.height); this.ctx.fillRect(n - 1, 0, o, this.height), this.ctx.strokeRect(n - 1, 0, o, this.height);
} }
renderSingleColumn(t) { renderSingleColumn(t) {
const e = new y(this.root.config, { const e = new g(this.root.config, {
row: 0, row: 0,
column: t column: t
}); });
@ -645,12 +683,12 @@ class X {
} }
renderRect(t, e) { renderRect(t, e) {
const { y: o, height: l } = e, i = this.isRowSelected(t); const { y: o, height: l } = e, i = this.isRowSelected(t);
this.ctx.fillStyle = i ? this.root.styles.cells.selectedBackground : "white", this.ctx.strokeStyle = "black", this.ctx.lineWidth = this.resizerHeight; this.ctx.fillStyle = i ? "#c7ebff" : "white", this.ctx.strokeStyle = "black", this.ctx.lineWidth = this.resizerHeight;
const n = o - this.root.viewport.top; const n = o - this.root.viewport.top;
this.ctx.fillRect(0, n - 1, this.width, l), this.ctx.strokeRect(0, n - 1, this.width, l); this.ctx.fillRect(0, n - 1, this.width, l), this.ctx.strokeRect(0, n - 1, this.width, l);
} }
renderSingleRow(t) { renderSingleRow(t) {
const e = new y(this.root.config, { const e = new g(this.root.config, {
column: 0, column: 0,
row: t row: t
}); });
@ -673,7 +711,7 @@ class j {
const o = t.map((l) => l.map((i) => i.displayValue).join(" ")).join(` const o = t.map((l) => l.map((i) => i.displayValue).join(" ")).join(`
`); `);
this.saved = t, navigator.clipboard.writeText(o), this.root.events.dispatch({ this.saved = t, navigator.clipboard.writeText(o), this.root.events.dispatch({
type: d.COPY_CELLS, type: u.COPY_CELLS,
data: t, data: t,
dataAsString: o, dataAsString: o,
range: e range: e
@ -686,40 +724,41 @@ class j {
const h = l.clipboardData.getData("text"); const h = l.clipboardData.getData("text");
try { try {
const a = h.split(` const a = h.split(`
`).map((w) => w.split(" ")).map((w) => w.map((g) => { `).map((d) => d.split(" ")).map((d) => d.map((w) => {
const f = { const y = {
displayValue: g, displayValue: w,
position: { position: {
column: e, column: e,
row: o row: o
}, },
resultValue: g, resultValue: w,
style: new R(), style: new R(),
value: g value: w
}; };
return new x(f); return new p(y);
})), u = a.length, C = a[0] ? a[0].length : 0; })), m = a.length, C = a[0] ? a[0].length : 0;
for (let w = 0; w < u; w++) for (let d = 0; d < m; d++)
for (let g = 0; g < C; g++) { for (let w = 0; w < C; w++) {
const f = a[w][g], L = { const y = a[d][w], B = {
column: e + g, column: e + w,
row: o + w row: o + d
}, V = { }, V = {
displayValue: f.displayValue, displayValue: y.displayValue,
value: f.value, value: y.value,
style: f.style style: y.style
}; };
t.changeCellValues(L, V, !1); t.changeCellValues(B, V, !1);
} }
} catch (c) { } catch (c) {
console.error("Cannot read clipboard. ", c); console.error("Cannot read clipboard. ", c);
} }
t.renderSheet();
return; return;
} }
const i = this.saved.length, n = this.saved[0] ? this.saved[0].length : 0; const i = this.saved.length, n = this.saved[0] ? this.saved[0].length : 0;
for (let h = 0; h < i; h++) for (let h = 0; h < i; h++)
for (let c = 0; c < n; c++) { for (let c = 0; c < n; c++) {
const a = this.saved[h][c], u = { const a = this.saved[h][c], m = {
column: e + c, column: e + c,
row: o + h row: o + h
}, C = { }, C = {
@ -727,11 +766,11 @@ class j {
value: a.value, value: a.value,
style: a.style style: a.style
}; };
t.changeCellValues(u, C, !1); t.changeCellValues(m, C, !1);
} }
} }
} }
const m = "modern_sc_"; const f = "modern_sc_";
class q { class q {
constructor(t, e) { constructor(t, e) {
s(this, "table"); s(this, "table");
@ -749,8 +788,8 @@ class q {
s(this, "cache"); s(this, "cache");
s(this, "events"); s(this, "events");
s(this, "clipboard"); s(this, "clipboard");
const o = B(40, 40), l = this.makeConfigFromData(o, (e == null ? void 0 : e.view) ?? { height: 600, width: 800 }); const o = L(40, 40), l = this.makeConfigFromData(o, (e == null ? void 0 : e.view) ?? { height: 600, width: 800 });
e != null && e.view && (l.view = e.view), this.config = new p(l), this.config.onCellClick = (e == null ? void 0 : e.onCellClick) ?? null, this.config.onSelectonChange = (e == null ? void 0 : e.onSelectionChange) ?? null, this.config.onCellChange = (e == null ? void 0 : e.onCellChange) ?? null, this.config.onCopy = (e == null ? void 0 : e.onCopy) ?? null, this.rowsBar = new X(this), this.columnsBar = new G(this), this.sheet = new D(this), this.table = new z(this), this.scroller = new H(this), this.toolbar = new N(this), this.editor = new T(this), this.cache = this.getInitialCache(), this.viewport = new S(this, this.scroller.getViewportBoundlingRect()), this.selection = new v(), this.events = new P(this), this.clipboard = new j(this), this.data = o, this.styles = new O(), this.buildComponent(), this.setElementsPositions(), this.appendTableToTarget(t), this.renderSheet(), this.renderColumnsBar(), this.renderRowsBar(); e != null && e.view && (l.view = e.view), this.config = new x(l), this.config.onCellClick = (e == null ? void 0 : e.onCellClick) ?? null, this.config.onSelectonChange = (e == null ? void 0 : e.onSelectionChange) ?? null, this.config.onCellChange = (e == null ? void 0 : e.onCellChange) ?? null, this.config.onCopy = (e == null ? void 0 : e.onCopy) ?? null, this.rowsBar = new X(this), this.columnsBar = new G(this), this.sheet = new _(this), this.table = new z(this), this.scroller = new H(this), this.toolbar = new O(this), this.editor = new T(this), this.cache = this.getInitialCache(), this.viewport = new b(this, this.scroller.getViewportBoundlingRect()), this.selection = new v(), this.events = new P(this), this.clipboard = new j(this), this.data = o, this.styles = new N(), this.buildComponent(), this.setElementsPositions(), this.appendTableToTarget(t), this.renderSheet(), this.renderColumnsBar(), this.renderRowsBar();
} }
setRowsBarPosition() { setRowsBarPosition() {
const t = this.columnsBar.height + this.toolbar.height, e = 0; const t = this.columnsBar.height + this.toolbar.height, e = 0;
@ -758,7 +797,7 @@ class q {
} }
setColumnsBarPosition() { setColumnsBarPosition() {
const t = this.toolbar.height, e = this.rowsBar.width; const t = this.toolbar.height, e = this.rowsBar.width;
console.log(t, e), this.columnsBar.setElementPosition(t, e); this.columnsBar.setElementPosition(t, e);
} }
setElementsPositions() { setElementsPositions() {
this.setRowsBarPosition(), this.setColumnsBarPosition(); this.setRowsBarPosition(), this.setColumnsBarPosition();
@ -769,7 +808,7 @@ class q {
for (let n = 0; n <= this.config.columns.length - 1; n++) { for (let n = 0; n <= this.config.columns.length - 1; n++) {
const h = this.config.columns[n]; const h = this.config.columns[n];
e += h.width; e += h.width;
const c = new F({ const c = new W({
xPos: e, xPos: e,
colIdx: n colIdx: n
}); });
@ -780,21 +819,20 @@ class q {
for (let n = 0; n <= this.config.rows.length - 1; n++) { for (let n = 0; n <= this.config.rows.length - 1; n++) {
const h = this.config.rows[n]; const h = this.config.rows[n];
l += h.height; l += h.height;
const c = new W({ const c = new F({
yPos: l, yPos: l,
rowIdx: n rowIdx: n
}); });
o.push(c); o.push(c);
} }
const i = new Y({ return new Y({
columns: t, columns: t,
rows: o rows: o
}); });
return console.log("CACHE: ", i), console.log("CONFIG: ", this.config), i;
} }
buildComponent() { buildComponent() {
const t = document.createElement("div"); const t = document.createElement("div");
t.style.top = this.columnsBarHeight + "px", t.style.left = this.rowsBarWidth + "px", t.appendChild(this.sheet.element), t.classList.add(m + "content"), this.table.element.appendChild(this.toolbar.element), this.table.element.appendChild(this.rowsBar.element), this.table.element.appendChild(this.columnsBar.element), this.table.element.appendChild(t), this.table.element.appendChild(this.scroller.element), this.table.element.append(this.editor.element); t.style.top = this.columnsBarHeight + "px", t.style.left = this.rowsBarWidth + "px", t.appendChild(this.sheet.element), t.classList.add(f + "content"), this.table.element.appendChild(this.toolbar.element), this.table.element.appendChild(this.rowsBar.element), this.table.element.appendChild(this.columnsBar.element), this.table.element.appendChild(t), this.table.element.appendChild(this.scroller.element), this.table.element.append(this.editor.element);
} }
/**Destroy spreadsheet DOM element. /**Destroy spreadsheet DOM element.
* *
@ -846,7 +884,7 @@ class q {
changeCellValues(t, e, o = !0) { changeCellValues(t, e, o = !0) {
const { column: l, row: i } = t; const { column: l, row: i } = t;
this.data[i][l].changeValues(e), this.events.dispatch({ this.data[i][l].changeValues(e), this.events.dispatch({
type: d.CELL_CHANGE, type: u.CELL_CHANGE,
cell: this.data[i][l], cell: this.data[i][l],
enableCallback: o enableCallback: o
}), this.renderCell(i, l); }), this.renderCell(i, l);
@ -888,6 +926,9 @@ class q {
renderSheet() { renderSheet() {
this.sheet.renderSheet(); this.sheet.renderSheet();
} }
renderSelection() {
this.sheet.renderSelection();
}
renderColumnsBar() { renderColumnsBar() {
this.columnsBar.renderBar(); this.columnsBar.renderBar();
} }
@ -898,14 +939,14 @@ class q {
this.data[t][e].render(this); this.data[t][e].render(this);
} }
loadData(t) { loadData(t) {
const e = t.length, o = t[0] ? this.data[0].length : 0; const e = t.length, o = t[0] ? t[0].length : 0;
this.data = []; console.log("!!FORMATTED DATA", e, o, t[0]), this.data = [];
const l = []; const l = [];
for (let i = 0; i < e; i++) { for (let i = 0; i < e; i++) {
const n = []; const n = [];
for (let h = 0; h < o; h++) { for (let h = 0; h < o; h++) {
const c = t[i][h]; const c = t[i][h];
n.push(new x({ n.push(new p({
displayValue: c.displayValue, displayValue: c.displayValue,
position: c.position, position: c.position,
resultValue: c.resultValue, resultValue: c.resultValue,
@ -915,7 +956,7 @@ class q {
} }
l.push(n); l.push(n);
} }
return this.data = l, this.selection.selectedCell = null, this.selection.selectedRange = null, this.config = this.makeConfigFromData(l, this.config.view), this.cache = this.getInitialCache(), this.scroller.updateScrollerSize(), this.viewport = new S(this, this.scroller.getViewportBoundlingRect()), this.renderSheet(), this; return this.data = l, this.selection.selectedCell = null, this.selection.selectedRange = null, this.config = this.makeConfigFromData(l, this.config.view), this.cache = this.getInitialCache(), this.scroller.updateScrollerSize(), this.viewport = new b(this, this.scroller.getViewportBoundlingRect()), this.renderSheet(), this;
} }
makeConfigFromData(t, e) { makeConfigFromData(t, e) {
const o = t.length - 1, l = t[0] ? t[0].length : 0, i = []; const o = t.length - 1, l = t[0] ? t[0].length : 0, i = [];
@ -930,7 +971,7 @@ class q {
width: 150, width: 150,
title: String(c) title: String(c)
})); }));
return new p({ return new x({
view: e, view: e,
rows: i, rows: i,
columns: n, columns: n,
@ -949,23 +990,23 @@ class q {
} }
} }
export { export {
m as CSS_PREFIX, f as CSS_PREFIX,
Y as Cache, Y as Cache,
F as CachedColumn, W as CachedColumn,
W as CachedRow, F as CachedRow,
x as Cell, p as Cell,
R as CellStyles, R as CellStyles,
E as Column, E as Column,
p as Config, x as Config,
M as Position, D as Position,
y as RenderBox, g as RenderBox,
k as Row, k as Row,
v as Selection, v as Selection,
_ as SerializableCell, M as SerializableCell,
O as Styles, N as Styles,
S as Viewport, b as Viewport,
K as createSampleConfig, K as createSampleConfig,
B as createSampleData, L as createSampleData,
q as default, q as default,
U as makeSpreadsheetConfigAndData U as makeSpreadsheetConfigAndData
}; };

2
dist/main.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -49,7 +49,6 @@ export declare class Cell {
getSerializableCell(): SerializableCell; getSerializableCell(): SerializableCell;
changeStyles(styles: CellStyles): void; changeStyles(styles: CellStyles): void;
changeValues(values: Partial<Omit<CellConstructorProps, "position">>): void; changeValues(values: Partial<Omit<CellConstructorProps, "position">>): void;
private isCellInRange;
render(root: Spreadsheet): void; render(root: Spreadsheet): void;
} }
export {}; export {};

View File

@ -1,7 +1,7 @@
{ {
"name": "modern_spreadsheet", "name": "modern_spreadsheet",
"private": false, "private": false,
"version": "0.0.31", "version": "0.0.32",
"exports": { "exports": {
".": { ".": {
"import": "./dist/main.js", "import": "./dist/main.js",

View File

@ -82,9 +82,7 @@ export class ColumnsBar {
const isColSelected = this.isColumnSelected(column); const isColSelected = this.isColumnSelected(column);
this.ctx.fillStyle = isColSelected this.ctx.fillStyle = isColSelected ? "#c7ebff" : "white";
? "#c7ebff"
: "white";
this.ctx.strokeStyle = "black"; this.ctx.strokeStyle = "black";
this.ctx.lineWidth = 1; this.ctx.lineWidth = 1;

View File

@ -66,7 +66,7 @@ export class Editor {
}); });
this.hide(); this.hide();
this.root.renderSelection() this.root.renderSelection();
} }
} }
}; };

View File

@ -69,9 +69,7 @@ export class RowsBar {
const isRowSeleted = this.isRowSelected(column); const isRowSeleted = this.isRowSelected(column);
this.ctx.fillStyle = isRowSeleted this.ctx.fillStyle = isRowSeleted ? "#c7ebff" : "white";
? "#c7ebff"
: "white";
this.ctx.strokeStyle = "black"; this.ctx.strokeStyle = "black";
this.ctx.lineWidth = this.resizerHeight; this.ctx.lineWidth = this.resizerHeight;

View File

@ -53,58 +53,69 @@ export class Sheet {
} }
private getSelectionRange() { private getSelectionRange() {
const { selectedCell, selectedRange } = this.root.selection const { selectedCell, selectedRange } = this.root.selection;
if (!selectedCell && !selectedRange) return; if (!selectedCell && !selectedRange) return;
if (selectedRange) { if (selectedRange) {
const startRow = Math.min(selectedRange.from.row, selectedRange.to.row);
const startCol = Math.min(
selectedRange.from.column,
selectedRange.to.column,
);
const lastRow = Math.max(selectedRange.from.row, selectedRange.to.row);
const lastCol = Math.max(
selectedRange.from.column,
selectedRange.to.column,
);
const startRow = Math.min(selectedRange.from.row, selectedRange.to.row) const startCellBox = new RenderBox(this.root.config, {
const startCol = Math.min(selectedRange.from.column, selectedRange.to.column) row: startRow,
const lastRow = Math.max(selectedRange.from.row, selectedRange.to.row) column: startCol,
const lastCol = Math.max(selectedRange.from.column, selectedRange.to.column) });
const startCellBox = new RenderBox(this.root.config, {row: startRow, column: startCol}) let width = 0;
let width = 0
for (let col = startCol; col <= lastCol; col++) { for (let col = startCol; col <= lastCol; col++) {
width += this.root.config.columns[col].width width += this.root.config.columns[col].width;
} }
let height = 0 let height = 0;
for (let row = startRow; row <= lastRow; row++) { for (let row = startRow; row <= lastRow; row++) {
height += this.root.config.rows[row].height height += this.root.config.rows[row].height;
} }
const x = startCellBox.x - this.root.viewport.left const x = startCellBox.x - this.root.viewport.left;
const y = startCellBox.y - this.root.viewport.top const y = startCellBox.y - this.root.viewport.top;
return { x, y, height, width } return { x, y, height, width };
} }
if (!selectedRange && selectedCell) { if (!selectedRange && selectedCell) {
const box = new RenderBox(this.root.config, selectedCell) const box = new RenderBox(this.root.config, selectedCell);
box.x -= this.root.viewport.left box.x -= this.root.viewport.left;
box.y -= this.root.viewport.top box.y -= this.root.viewport.top;
return box return box;
} }
} }
private renderSelectionRange(x: number, y: number, width: number, height: number) { private renderSelectionRange(
x: number,
this.ctx.save() y: number,
this.ctx.strokeStyle = '#47d1ff' width: number,
this.ctx.lineWidth = 3 height: number,
this.ctx.strokeRect(x, y, width, height) ) {
this.ctx.fillStyle = '#7da8ff50' this.ctx.save();
this.ctx.fillRect(x, y, width, height) this.ctx.strokeStyle = "#47d1ff";
this.ctx.restore() this.ctx.lineWidth = 3;
this.ctx.strokeRect(x, y, width, height);
this.ctx.fillStyle = "#7da8ff50";
this.ctx.fillRect(x, y, width, height);
this.ctx.restore();
} }
renderSelection() { renderSelection() {
const box = this.getSelectionRange() const box = this.getSelectionRange();
if (!box) return; if (!box) return;
const {height, width, x, y} = box const { height, width, x, y } = box;
this.renderSelectionRange(x, y, width, height) this.renderSelectionRange(x, y, width, height);
} }
renderSheet() { renderSheet() {
@ -113,7 +124,6 @@ export class Sheet {
const lastRowIdx = this.root.viewport.lastRow + 3; const lastRowIdx = this.root.viewport.lastRow + 3;
const firstColIdx = this.root.viewport.firstCol; const firstColIdx = this.root.viewport.firstCol;
for (let row = firstRowIdx; row <= lastRowIdx; row++) { for (let row = firstRowIdx; row <= lastRowIdx; row++) {
for (let col = firstColIdx; col <= lastColIdx; col++) { for (let col = firstColIdx; col <= lastColIdx; col++) {
if (!this.root.config.columns[col] || !this.root.config.rows[row]) if (!this.root.config.columns[col] || !this.root.config.rows[row])
@ -122,7 +132,6 @@ export class Sheet {
this.renderCell({ column: col, row }); this.renderCell({ column: col, row });
} }
} }
this.renderSelection() this.renderSelection();
} }
} }

View File

@ -96,21 +96,21 @@ export class Cell {
Object.assign(this, values); Object.assign(this, values);
} }
private isCellInRange(root: Spreadsheet): boolean { // private isCellInRange(root: Spreadsheet): boolean {
const { column, row } = this.position; // const { column, row } = this.position;
const { selectedRange } = root.selection; // const { selectedRange } = root.selection;
if (!selectedRange) return false; // if (!selectedRange) return false;
const isCellInRow = // const isCellInRow =
row >= Math.min(selectedRange.from.row, selectedRange.to.row) && // row >= Math.min(selectedRange.from.row, selectedRange.to.row) &&
row <= Math.max(selectedRange.to.row, selectedRange.from.row); // row <= Math.max(selectedRange.to.row, selectedRange.from.row);
const isCellInCol = // const isCellInCol =
column >= Math.min(selectedRange.from.column, selectedRange.to.column) && // column >= Math.min(selectedRange.from.column, selectedRange.to.column) &&
column <= Math.max(selectedRange.to.column, selectedRange.from.column); // column <= Math.max(selectedRange.to.column, selectedRange.from.column);
return isCellInCol && isCellInRow; // return isCellInCol && isCellInRow;
} // }
render(root: Spreadsheet) { render(root: Spreadsheet) {
const renderBox = new RenderBox(root.config, this.position); const renderBox = new RenderBox(root.config, this.position);

View File

@ -77,7 +77,7 @@ export class Clipboard {
} catch (err) { } catch (err) {
console.error("Cannot read clipboard. ", err); console.error("Cannot read clipboard. ", err);
} }
root.renderSheet() root.renderSheet();
return; return;
} }