From 9ef322b91b8dca6d218f18e1fa772ff25ee116ad Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Mon, 11 Dec 2023 22:29:45 +0400 Subject: [PATCH] Ensure to deselect with undefined --- src/editor/model/Editor.ts | 8 ++++---- test/specs/grapesjs/index.ts | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/editor/model/Editor.ts b/src/editor/model/Editor.ts index 617363560..3aac98110 100644 --- a/src/editor/model/Editor.ts +++ b/src/editor/model/Editor.ts @@ -488,11 +488,11 @@ export default class EditorModel extends Model { .filter(Boolean) as Component[]; const selected = this.getSelectedAll(); const mltSel = this.getConfig().multipleSelection; - - // If an array is passed remove all selected - // expect those yet to be selected const multiple = isArray(el); - multiple && this.removeSelected(selected.filter(s => !contains(models, s))); + + if (multiple || !el) { + this.removeSelected(selected.filter(s => !contains(models, s))); + } models.forEach(model => { if (model) { diff --git a/test/specs/grapesjs/index.ts b/test/specs/grapesjs/index.ts index 32eab8d04..57747dea1 100644 --- a/test/specs/grapesjs/index.ts +++ b/test/specs/grapesjs/index.ts @@ -505,6 +505,16 @@ describe('GrapesJS', () => { expect(editor.getSelectedAll().length).toBe(0); }); + test('Deselect component', () => { + editor.select(el1); + expect(editor.getSelected()).toBe(el1); + expect(editor.getSelectedAll().length).toBe(1); + // Deselect with undefined + editor.select(); + expect(editor.getSelected()).toBe(undefined); + expect(editor.getSelectedAll().length).toBe(0); + }); + test('Select multiple components', () => { // Select at first el1 and el3 editor.select([el1, el3]);