From 0608c394259146682e59ab69bdee6b10bd82d993 Mon Sep 17 00:00:00 2001 From: Evgeni B <26095528+devgeni@users.noreply.github.com> Date: Mon, 25 Oct 2021 19:37:15 +0300 Subject: [PATCH] feat: insert new row on enter at the last row --- src/component/sheet.js | 7 +++++++ src/core/data_proxy.js | 14 ++++++++++++++ src/core/row.js | 18 ++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/src/component/sheet.js b/src/component/sheet.js index 918e023a..a8487e10 100644 --- a/src/component/sheet.js +++ b/src/component/sheet.js @@ -505,6 +505,8 @@ function insertDeleteRowColumn(type) { if (data.settings.mode === 'read') return; if (type === 'insert-row') { data.insert('row'); + } else if (type === 'insert-row-below') { + data.insertRowBelow(); } else if (type === 'delete-row') { data.delete('row'); } else if (type === 'insert-column') { @@ -831,6 +833,11 @@ function sheetInitEvents() { break; case 13: // enter editor.clear(); + const { eri } = this.selector.range; + const rows = this.data.rows; + if (eri === rows.len - 1 && !shiftKey) { + insertDeleteRowColumn.call(this, 'insert-row-below'); + } // shift + enter => move up // enter => move down selectorMove.call(this, false, shiftKey ? 'up' : 'down'); diff --git a/src/core/data_proxy.js b/src/core/data_proxy.js index a59ef6ef..ed80fd5e 100644 --- a/src/core/data_proxy.js +++ b/src/core/data_proxy.js @@ -848,6 +848,20 @@ export default class DataProxy { }); } + insertRowBelow(n = 1) { + this.changeData(() => { + const { sri } = this.selector.range; + const { rows, merges } = this; + let si = sri; + rows.insertBelow(sri, n); + merges.shift('row', si, n, (ri, ci, rn, cn) => { + const cell = rows.getCell(ri, ci); + cell.merge[0] += rn; + cell.merge[1] += cn; + }); + }); + } + // type: row | column delete(type) { this.changeData(() => { diff --git a/src/core/row.js b/src/core/row.js index dc3243a7..c2dff3fb 100644 --- a/src/core/row.js +++ b/src/core/row.js @@ -225,6 +225,24 @@ class Rows { this.len += n; } + insertBelow(sri, n = 1) { + const ndata = {}; + this.each((ri, row) => { + let nri = parseInt(ri, 10); + if (nri > sri) { + nri += n; + this.eachCells(ri, (ci, cell) => { + if (cell.text && cell.text[0] === '=') { + cell.text = cell.text.replace(/[a-zA-Z]{1,3}\d+/g, word => expr2expr(word, 0, n, (x, y) => y >= sri)); + } + }); + } + ndata[nri] = row; + }); + this._ = ndata; + this.len += n; + } + delete(sri, eri) { const n = eri - sri + 1; const ndata = {};