Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implicit keyboard binding / migrate to diagram-js@15 #1269

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 36 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"cross-env": "^7.0.3",
"css-loader": "^7.1.2",
"del-cli": "^5.1.0",
"diagram-js": "^14.11.3",
"diagram-js": "^15.2.2",
"didi": "^10.2.2",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/form-js-editor/src/FormEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class FormEditor {
*/
this._container = createFormContainer();

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { isCmd, isKey, isShift } from 'diagram-js/lib/features/keyboard/KeyboardUtil';

import { KEYS_REDO, KEYS_UNDO } from 'diagram-js/lib/features/keyboard/KeyboardBindings';
import { isUndo, isRedo } from 'diagram-js/lib/features/keyboard/KeyboardUtil';

const LOW_PRIORITY = 500;

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

if (isCmd(keyEvent) && !isShift(keyEvent) && isKey(KEYS_UNDO, keyEvent)) {
if (isUndo(keyEvent)) {
editorActions.trigger('undo');

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

if (isCmd(keyEvent) && (isKey(KEYS_REDO, keyEvent) || (isKey(KEYS_UNDO, keyEvent) && isShift(keyEvent)))) {
if (isRedo(keyEvent)) {
editorActions.trigger('redo');

return true;
Expand Down
25 changes: 25 additions & 0 deletions packages/form-js-editor/src/render/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,31 @@ export class Renderer {
constructor(renderConfig, eventBus, formEditor, injector) {
const { container, compact = false } = renderConfig;

eventBus.on('form.init', function () {
// emit <canvas.init> so dependent components can hook in
// this is required to register keyboard bindings
eventBus.fire('canvas.init', {
svg: container,
viewport: null,
});
});

// focus container on over if no selection
container.addEventListener('mouseover', function () {
if (document.activeElement === document.body) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could / should use Canvas#restoreFocus() here.

container.focus({ preventScroll: true });
}
});

// ensure we focus the container if the users clicks
// inside; this follows input focus handling closely
container.addEventListener('click', function (event) {
// force focus when clicking container
if (!container.contains(document.activeElement)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could / should use Canvas#restoreFocus() here.

container.focus({ preventScroll: true });
}
});

const App = () => {
const [state, setState] = useState(formEditor._getState());

Expand Down
21 changes: 15 additions & 6 deletions packages/form-js-editor/src/render/components/FormEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,23 @@ function Element(props) {
}
}, [selection, field]);

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

selection.toggle(field);
if (!fieldEl) {
return;
}

// properly focus on field
ref.current.focus();
}
const id = fieldEl.dataset.id;

if (id === field.id) {
selection.toggle(field);
}
},
[field, selection],
);

const isSelected = selection.isSelected(field);

Expand Down
21 changes: 0 additions & 21 deletions packages/form-js-editor/test/spec/FormEditor.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ describe('FormEditor', function () {
await bootstrapFormEditor({
container,
schema,
keyboard: {
bindTo: document,
},
});

formEditor.on('changed', (event) => {
Expand All @@ -70,9 +67,6 @@ describe('FormEditor', function () {
await bootstrapFormEditor({
container,
schema: schemaRows,
keyboard: {
bindTo: document,
},
debugColumns: true,
});

Expand All @@ -93,9 +87,6 @@ describe('FormEditor', function () {
await bootstrapFormEditor({
container,
schema,
keyboard: {
bindTo: document,
},
});

// then
Expand All @@ -111,9 +102,6 @@ describe('FormEditor', function () {
await bootstrapFormEditor({
container,
schema,
keyboard: {
bindTo: document,
},
});

// when
Expand All @@ -132,9 +120,6 @@ describe('FormEditor', function () {
renderer: {
compact: true,
},
keyboard: {
bindTo: document,
},
});

// then
Expand All @@ -153,9 +138,6 @@ describe('FormEditor', function () {
type: 'default',
},
debounce: true,
keyboard: {
bindTo: document,
},
});

// then
Expand All @@ -181,9 +163,6 @@ describe('FormEditor', function () {
],
},
debounce: true,
keyboard: {
bindTo: document,
},
});

// then
Expand Down
3 changes: 0 additions & 3 deletions packages/form-js-viewer/test/spec/Form.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,6 @@ describe('Form', function () {
await bootstrapForm({
container,
schema,
keyboard: {
bindTo: document,
},
});

// when
Expand Down
Loading