From 2798c50b50e308546c8f43e332579697bc1cd145 Mon Sep 17 00:00:00 2001 From: Zhurbin Date: Wed, 15 May 2019 17:19:39 +0300 Subject: [PATCH] Handle shift modifier to add/subtract selected cells. --- lib/DataCell.js | 2 +- lib/DataSheet.js | 24 ++++++++++++++++++++---- src/DataCell.js | 2 +- src/DataSheet.js | 23 +++++++++++++++++++---- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/lib/DataCell.js b/lib/DataCell.js index 1e381d2..53ef0d5 100644 --- a/lib/DataCell.js +++ b/lib/DataCell.js @@ -155,7 +155,7 @@ var DataCell = function (_PureComponent) { cell = _props2.cell; if (!cell.disableEvents) { - onMouseDown(row, col); + onMouseDown(row, col, e); } } }, { diff --git a/lib/DataSheet.js b/lib/DataSheet.js index ec05908..e70f706 100644 --- a/lib/DataSheet.js +++ b/lib/DataSheet.js @@ -438,14 +438,23 @@ var DataSheet = function (_PureComponent) { if (offsets && (offsets.i || offsets.j)) { var _getState6 = this.getState(), - start = _getState6.start; + start = _getState6.start, + end = _getState6.end; var data = this.props.data; + var oldStartLocation = { i: start.i, j: start.j }; + var newEndLocation = { i: end.i + offsets.i, j: end.j + offsets.j }; + var newLocation = { i: start.i + offsets.i, j: start.j + offsets.j }; + var updateLocation = function updateLocation() { if (data[newLocation.i] && typeof data[newLocation.i][newLocation.j] !== 'undefined') { - _this3._setState({ start: newLocation, end: newLocation, editing: {} }); + _this3._setState({ + start: e.shiftKey && !jumpRow ? oldStartLocation : newLocation, + end: e.shiftKey && !jumpRow ? newEndLocation : newLocation, + editing: {} + }); e.preventDefault(); return true; } @@ -515,9 +524,16 @@ var DataSheet = function (_PureComponent) { } }, { key: 'onMouseDown', - value: function onMouseDown(i, j) { + value: function onMouseDown(i, j, e) { var editing = isEmpty(this.state.editing) || this.state.editing.i !== i || this.state.editing.j !== j ? {} : this.state.editing; - this._setState({ selecting: true, start: { i: i, j: j }, end: { i: i, j: j }, editing: editing, forceEdit: false }); + + this._setState({ + selecting: true, + start: e.shiftKey ? this.state.start : { i: i, j: j }, + end: { i: i, j: j }, + editing: editing, + forceEdit: false + }); // Keep listening to mouse if user releases the mouse (dragging outside) document.addEventListener('mouseup', this.onMouseUp); diff --git a/src/DataCell.js b/src/DataCell.js index 7bb0257..26516dd 100644 --- a/src/DataCell.js +++ b/src/DataCell.js @@ -89,7 +89,7 @@ export default class DataCell extends PureComponent { handleMouseDown (e) { const {row, col, onMouseDown, cell} = this.props if (!cell.disableEvents) { - onMouseDown(row, col) + onMouseDown(row, col, e) } } diff --git a/src/DataSheet.js b/src/DataSheet.js index 31a1c1b..ca081bf 100644 --- a/src/DataSheet.js +++ b/src/DataSheet.js @@ -323,12 +323,20 @@ export default class DataSheet extends PureComponent { handleNavigate (e, offsets, jumpRow) { if (offsets && (offsets.i || offsets.j)) { - const {start} = this.getState() + const {start, end} = this.getState() const {data} = this.props + const oldStartLocation = {i: start.i, j: start.j} + const newEndLocation = {i: end.i + offsets.i, j: end.j + offsets.j} + let newLocation = {i: start.i + offsets.i, j: start.j + offsets.j} + const updateLocation = () => { if (data[newLocation.i] && typeof (data[newLocation.i][newLocation.j]) !== 'undefined') { - this._setState({start: newLocation, end: newLocation, editing: {}}) + this._setState({ + start: e.shiftKey && !jumpRow ? oldStartLocation : newLocation, + end: e.shiftKey && !jumpRow ? newEndLocation : newLocation, + editing: {} + }) e.preventDefault() return true } @@ -385,10 +393,17 @@ export default class DataSheet extends PureComponent { } } - onMouseDown (i, j) { + onMouseDown (i, j, e) { let editing = (isEmpty(this.state.editing) || this.state.editing.i !== i || this.state.editing.j !== j) ? {} : this.state.editing - this._setState({selecting: true, start: {i, j}, end: {i, j}, editing: editing, forceEdit: false}) + + this._setState({ + selecting: true, + start: e.shiftKey ? this.state.start : {i, j}, + end: {i, j}, + editing: editing, + forceEdit: false + }) // Keep listening to mouse if user releases the mouse (dragging outside) document.addEventListener('mouseup', this.onMouseUp)