Skip to content

Commit 89799a3

Browse files
vsgoulartSkaiir
authored andcommitted
refactor: use new diagram-js binding
1 parent c403a86 commit 89799a3

File tree

6 files changed

+81
-39
lines changed

6 files changed

+81
-39
lines changed

package-lock.json

Lines changed: 36 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
"cross-env": "^7.0.3",
9292
"css-loader": "^7.1.2",
9393
"del-cli": "^5.1.0",
94-
"diagram-js": "^14.11.2",
94+
"diagram-js": "^15.0.0-alpha-keyboard",
9595
"didi": "^10.2.2",
9696
"eslint": "^8.57.0",
9797
"eslint-config-prettier": "^9.1.0",

packages/form-js-editor/src/FormEditor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class FormEditor {
6363
*/
6464
this._container = createFormContainer();
6565

66-
this._container.setAttribute('input-handle-modified-keys', 'z,y');
66+
this._container.setAttribute('tabindex', '0');
6767

6868
const { container, exporter, injector = this._createInjector(options, this._container), properties = {} } = options;
6969

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { isCmd, isKey, isShift } from 'diagram-js/lib/features/keyboard/KeyboardUtil';
2-
3-
import { KEYS_REDO, KEYS_UNDO } from 'diagram-js/lib/features/keyboard/KeyboardBindings';
1+
import { isUndo, isRedo } from 'diagram-js/lib/features/keyboard/KeyboardUtil';
42

53
const LOW_PRIORITY = 500;
64

@@ -25,7 +23,7 @@ export class FormEditorKeyboardBindings {
2523
addListener('undo', (context) => {
2624
const { keyEvent } = context;
2725

28-
if (isCmd(keyEvent) && !isShift(keyEvent) && isKey(KEYS_UNDO, keyEvent)) {
26+
if (isUndo(keyEvent)) {
2927
editorActions.trigger('undo');
3028

3129
return true;
@@ -38,7 +36,7 @@ export class FormEditorKeyboardBindings {
3836
addListener('redo', (context) => {
3937
const { keyEvent } = context;
4038

41-
if (isCmd(keyEvent) && (isKey(KEYS_REDO, keyEvent) || (isKey(KEYS_UNDO, keyEvent) && isShift(keyEvent)))) {
39+
if (isRedo(keyEvent)) {
4240
editorActions.trigger('redo');
4341

4442
return true;

packages/form-js-editor/src/render/Renderer.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,31 @@ export class Renderer {
2222
constructor(renderConfig, eventBus, formEditor, injector) {
2323
const { container, compact = false } = renderConfig;
2424

25+
eventBus.on('form.init', function () {
26+
// emit <canvas.init> so dependent components can hook in
27+
// this is required to register keyboard bindings
28+
eventBus.fire('canvas.init', {
29+
svg: container,
30+
viewport: null,
31+
});
32+
});
33+
34+
// focus container on over if no selection
35+
container.addEventListener('mouseover', function () {
36+
if (document.activeElement === document.body) {
37+
container.focus({ preventScroll: true });
38+
}
39+
});
40+
41+
// ensure we focus the container if the users clicks
42+
// inside; this follows input focus handling closely
43+
container.addEventListener('click', function (event) {
44+
// force focus when clicking container
45+
if (!container.contains(document.activeElement)) {
46+
container.focus({ preventScroll: true });
47+
}
48+
});
49+
2550
const App = () => {
2651
const [state, setState] = useState(formEditor._getState());
2752

packages/form-js-editor/src/render/components/FormEditor.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,23 @@ function Element(props) {
121121
}
122122
}, [selection, field]);
123123

124-
function onClick(event) {
125-
event.stopPropagation();
124+
const onClick = useCallback(
125+
(event) => {
126+
// TODO(nikku): refactor this to use proper DOM delegation
127+
const fieldEl = event.target.closest('[data-id]');
126128

127-
selection.toggle(field);
129+
if (!fieldEl) {
130+
return;
131+
}
128132

129-
// properly focus on field
130-
ref.current.focus();
131-
}
133+
const id = fieldEl.dataset.id;
134+
135+
if (id === field.id) {
136+
selection.toggle(field);
137+
}
138+
},
139+
[field, selection],
140+
);
132141

133142
const isSelected = selection.isSelected(field);
134143

0 commit comments

Comments
 (0)