Skip to content

Commit

Permalink
refactor: use new diagram-js binding
Browse files Browse the repository at this point in the history
  • Loading branch information
vsgoulart authored and Skaiir committed Oct 2, 2024
1 parent 7b103d2 commit fe4d7ff
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 39 deletions.
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.2",
"diagram-js": "^15.0.0-alpha-keyboard",
"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) {
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)) {
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

0 comments on commit fe4d7ff

Please sign in to comment.