From a4ab6caaa152f07ef284dab3a1a4d1d9ef1645ef Mon Sep 17 00:00:00 2001 From: sunshinesmilelk Date: Tue, 17 Dec 2024 20:38:53 +0800 Subject: [PATCH] fix(core): fix selecting multiple cells to move --- packages/libro-core/src/libro-model.ts | 2 +- packages/libro-core/src/libro-view.tsx | 27 ++++++++++++++------------ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/packages/libro-core/src/libro-model.ts b/packages/libro-core/src/libro-model.ts index 05c6377e..79aa1c8f 100644 --- a/packages/libro-core/src/libro-model.ts +++ b/packages/libro-core/src/libro-model.ts @@ -640,7 +640,7 @@ export class LibroModel implements NotebookModel, DndListModel { //往上交换cell if (startIndex === this.activeIndex) { //active在头 - this.activeIndex = targetIndex - source.length; + this.activeIndex = targetIndex; } else { //active在尾 this.activeIndex = targetIndex + 1; diff --git a/packages/libro-core/src/libro-view.tsx b/packages/libro-core/src/libro-view.tsx index deb531f5..ce449a83 100644 --- a/packages/libro-core/src/libro-view.tsx +++ b/packages/libro-core/src/libro-view.tsx @@ -742,13 +742,15 @@ export class LibroView extends BaseView implements NotebookView { this.collapseCell(previousCell, false); } if (this.model.selections.length !== 0 && this.isSelected(cell)) { - for (const selectedCell of this.model.selections) { - const selectIndex = this.findCellIndex(selectedCell); - if (selectIndex === 0) { - return; - } - this.model.exchangeCells(this.model.selections, selectIndex - 1); + const startIndex = this.findCellIndex(this.model.selections[0]); + const endIndex = this.findCellIndex( + this.model.selections[this.model.selections.length - 1], + ); + const index = Math.min(startIndex, endIndex); + if (startIndex === 0) { + return; } + this.model.exchangeCells(this.model.selections, index - 1); } else { const sourceIndex = this.findCellIndex(cell); if (sourceIndex > -1) { @@ -764,13 +766,14 @@ export class LibroView extends BaseView implements NotebookView { this.collapseCell(nextCell, false); } if (this.model.selections.length !== 0 && this.isSelected(cell)) { - for (let i = this.model.selections.length - 1; i > -1; i--) { - const selectIndex = this.findCellIndex(this.model.selections[i]); - if (selectIndex === this.model.cells.length - 1) { - return; - } - this.model.exchangeCells(this.model.selections, selectIndex + 1); + const startIndex = this.findCellIndex(this.model.selections[0]) + 1; + const endIndex = + this.findCellIndex(this.model.selections[this.model.selections.length - 1]) + 1; + const index = Math.max(startIndex, endIndex); + if (index === this.model.cells.length) { + return; } + this.model.exchangeCells(this.model.selections, index + 1); } else { const sourceIndex = this.findCellIndex(cell); if (sourceIndex > -1) {