From 9a4f30b811fdf8c58ec5c50c185628eb3158931a Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 25 Aug 2020 21:08:28 +0200 Subject: [PATCH] fix: improve hidecolumns behaviour --- frontend/src/lib/table/table.js | 45 ++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/frontend/src/lib/table/table.js b/frontend/src/lib/table/table.js index a87ef02c8..33c03cc80 100644 --- a/frontend/src/lib/table/table.js +++ b/frontend/src/lib/table/table.js @@ -5,17 +5,26 @@ import * as defer from 'lodash.defer'; class Overhang { colSpan; rowSpan; + cell; - constructor(colSpan, rowSpan) { + constructor(colSpan, rowSpan, cell) { this.colSpan = colSpan; this.rowSpan = rowSpan; + this.cell = cell; if (new.target === Overhang) Object.freeze(this); } nextLine() { - return new Overhang(this.colSpan, Math.max(0, this.rowSpan - 1)); + return new Overhang(this.colSpan, Math.max(0, this.rowSpan - 1), this.cell); + } + + reduceCol(n) { + if (this.colSpan > n) + return new Overhang(this.colSpan - n, this.rowSpan, this.cell); + else + return null; } isHole() { @@ -57,11 +66,10 @@ export class TableIndices { this._table = table; - let overhangs = new Array(); + let currentOverhangs = new Array(); let currentRow = 0; for (const rowParent of this._table.rows) { - let currentOverhangs = Array.from(overhangs); let newOverhangs = new Array(); let cellBefore = 0; @@ -76,18 +84,38 @@ export class TableIndices { break; else newOverhangs.push(overhang.nextLine()); + + if (DEBUG_MODE > 1) + console.log('From overhang', overhang); cellBefore += overhang.colSpan; } currentOverhangs = currentOverhangs.slice(i); + let remCols = this.colSpan(cell); + while (remCols > 0 && currentOverhangs[0]) { + let firstOverhang = currentOverhangs[0].reduceCol(this.colSpan(cell)); + if (firstOverhang) { + if (DEBUG_MODE > 1) + console.log('Replace first overhang', remCols, currentOverhangs[0], firstOverhang); + currentOverhangs[0] = firstOverhang; + break; + } else { + if (DEBUG_MODE > 1) + console.log('Drop first overhang', remCols, currentOverhangs[0], firstOverhang); + remCols -= currentOverhangs[0].colSpan; + currentOverhangs.shift(); + } + } this._cellToIndices.set(cell, { row: currentRow, col: cellBefore }); let rows = range(currentRow, currentRow + this.rowSpan(cell)); let columns = range(cellBefore, cellBefore + this.colSpan(cell)); - if (DEBUG_MODE > 0) { + if (DEBUG_MODE > 1) { + console.log('Result', rows, columns); + cell.dataset.rows = JSON.stringify(rows); cell.dataset.columns = JSON.stringify(columns); } @@ -104,11 +132,14 @@ export class TableIndices { } } - newOverhangs.push(new Overhang(this.colSpan(cell), this.rowSpan(cell) - 1)); + newOverhangs.push(new Overhang(this.colSpan(cell), this.rowSpan(cell) - 1, cell)); + + if (DEBUG_MODE > 1) + console.log('From current cell', this.colSpan(cell)); cellBefore += this.colSpan(cell); } - overhangs = newOverhangs; + currentOverhangs = Array.from(newOverhangs); currentRow++; }