Skip to content

Commit 2b1064a

Browse files
author
Niklas Kiefer
committed
wip feat(editor): add removeSelection action
Related to #437
1 parent f30dbf7 commit 2b1064a

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

packages/form-js-editor/src/features/editor-actions/FormEditorActions.js

+36-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default class FormEditorActions extends EditorActions {
1717
_registerDefaultActions(injector) {
1818
const commandStack = injector.get('commandStack', false),
1919
formFieldRegistry = injector.get('formFieldRegistry', false),
20+
modeling = injector.get('modeling', false),
2021
selection = injector.get('selection', false);
2122

2223
if (commandStack) {
@@ -48,11 +49,45 @@ export default class FormEditorActions extends EditorActions {
4849
selection.set(formField);
4950
}
5051
});
52+
53+
}
54+
55+
if (modeling && selection) {
56+
57+
// @ts-ignore
58+
this.register('removeSelection', () => {
59+
const selectedFormField = selection.get();
60+
61+
if (!selectedFormField) {
62+
return;
63+
}
64+
65+
const parentField = formFieldRegistry.get(selectedFormField._parent);
66+
67+
const index = getFormFieldIndex(parentField, selectedFormField);
68+
69+
modeling.removeFormField(selectedFormField, parentField, index);
70+
});
5171
}
5272
}
5373
}
5474

5575
FormEditorActions.$inject = [
5676
'eventBus',
5777
'injector'
58-
];
78+
];
79+
80+
81+
// helper ////////////
82+
83+
function getFormFieldIndex(parent, formField) {
84+
let fieldFormIndex = parent.components.length;
85+
86+
parent.components.forEach(({ id }, index) => {
87+
if (id === formField.id) {
88+
fieldFormIndex = index;
89+
}
90+
});
91+
92+
return fieldFormIndex;
93+
}

packages/form-js-editor/src/features/keyboard/FormEditorKeyboardBindings.js

+13
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ export default class FormEditorKeyboardBindings {
5353
}
5454
});
5555

56+
// delete selected field
57+
// DEL
58+
addListener('removeSelection', (context) => {
59+
60+
const { keyEvent } = context;
61+
62+
if (isKey([ 'Backspace', 'Delete', 'Del' ], keyEvent)) {
63+
64+
editorActions.trigger('removeSelection');
65+
66+
return true;
67+
}
68+
});
5669
}
5770
}
5871

0 commit comments

Comments
 (0)