From 2b1064a495cf55d130c81e23d3bf73fee600ea03 Mon Sep 17 00:00:00 2001 From: Niklas Kiefer Date: Tue, 29 Nov 2022 16:21:12 +0100 Subject: [PATCH] wip feat(editor): add `removeSelection` action Related to #437 --- .../editor-actions/FormEditorActions.js | 37 ++++++++++++++++++- .../keyboard/FormEditorKeyboardBindings.js | 13 +++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/packages/form-js-editor/src/features/editor-actions/FormEditorActions.js b/packages/form-js-editor/src/features/editor-actions/FormEditorActions.js index 8ed8dc221..5bc1f0b4e 100644 --- a/packages/form-js-editor/src/features/editor-actions/FormEditorActions.js +++ b/packages/form-js-editor/src/features/editor-actions/FormEditorActions.js @@ -17,6 +17,7 @@ export default class FormEditorActions extends EditorActions { _registerDefaultActions(injector) { const commandStack = injector.get('commandStack', false), formFieldRegistry = injector.get('formFieldRegistry', false), + modeling = injector.get('modeling', false), selection = injector.get('selection', false); if (commandStack) { @@ -48,6 +49,25 @@ export default class FormEditorActions extends EditorActions { selection.set(formField); } }); + + } + + if (modeling && selection) { + + // @ts-ignore + this.register('removeSelection', () => { + const selectedFormField = selection.get(); + + if (!selectedFormField) { + return; + } + + const parentField = formFieldRegistry.get(selectedFormField._parent); + + const index = getFormFieldIndex(parentField, selectedFormField); + + modeling.removeFormField(selectedFormField, parentField, index); + }); } } } @@ -55,4 +75,19 @@ export default class FormEditorActions extends EditorActions { FormEditorActions.$inject = [ 'eventBus', 'injector' -]; \ No newline at end of file +]; + + +// helper //////////// + +function getFormFieldIndex(parent, formField) { + let fieldFormIndex = parent.components.length; + + parent.components.forEach(({ id }, index) => { + if (id === formField.id) { + fieldFormIndex = index; + } + }); + + return fieldFormIndex; +} \ No newline at end of file diff --git a/packages/form-js-editor/src/features/keyboard/FormEditorKeyboardBindings.js b/packages/form-js-editor/src/features/keyboard/FormEditorKeyboardBindings.js index 4825b79b1..4be2656f2 100644 --- a/packages/form-js-editor/src/features/keyboard/FormEditorKeyboardBindings.js +++ b/packages/form-js-editor/src/features/keyboard/FormEditorKeyboardBindings.js @@ -53,6 +53,19 @@ export default class FormEditorKeyboardBindings { } }); + // delete selected field + // DEL + addListener('removeSelection', (context) => { + + const { keyEvent } = context; + + if (isKey([ 'Backspace', 'Delete', 'Del' ], keyEvent)) { + + editorActions.trigger('removeSelection'); + + return true; + } + }); } }