From b51de2ac4c9d4618449987bd0438942a421b5532 Mon Sep 17 00:00:00 2001 From: L-Sun Date: Tue, 31 Dec 2024 08:05:13 +0000 Subject: [PATCH] fix(editor): add index checking to selected elements array (#9453) Close https://toeverything.sentry.io/issues/6187666983/?alert_rule_id=15031714&alert_type=issue¬ification_uuid=fe3f4b9d-1e86-420e-a715-c3ca0fa932e4&project=4506307500179456&referrer=slack --- .../widgets/element-toolbar/lock-button.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/blocksuite/blocks/src/root-block/widgets/element-toolbar/lock-button.ts b/blocksuite/blocks/src/root-block/widgets/element-toolbar/lock-button.ts index 2b8a65d71952c..c9c4b2012f774 100644 --- a/blocksuite/blocks/src/root-block/widgets/element-toolbar/lock-button.ts +++ b/blocksuite/blocks/src/root-block/widgets/element-toolbar/lock-button.ts @@ -33,8 +33,6 @@ export class EdgelessLockButton extends SignalWatcher( private _lock() { const { service, doc, std } = this.edgeless; - doc.captureSync(); - // get most top selected elements(*) from tree, like in a tree below // G0 // / \ @@ -46,12 +44,16 @@ export class EdgelessLockButton extends SignalWatcher( // return [E1] const selectedElements = this._selectedElements; + if (selectedElements.length === 0) return; + const levels = selectedElements.map(element => element.groups.length); const topElement = selectedElements[levels.indexOf(Math.min(...levels))]; const otherElements = selectedElements.filter( element => element !== topElement ); + doc.captureSync(); + // release other elements from their groups and group with top element otherElements.forEach(element => { // oxlint-disable-next-line unicorn/prefer-dom-node-remove @@ -97,9 +99,13 @@ export class EdgelessLockButton extends SignalWatcher( private _unlock() { const { service, doc } = this.edgeless; + + const selectedElements = this._selectedElements; + if (selectedElements.length === 0) return; + doc.captureSync(); - this._selectedElements.forEach(element => { + selectedElements.forEach(element => { if (element instanceof GroupElementModel) { service.ungroup(element); } else {