Skip to content

Commit

Permalink
fix: port implicit keyboard binding configuration to decision editors
Browse files Browse the repository at this point in the history
Closes #920
  • Loading branch information
jarekdanielak authored Nov 15, 2024
1 parent 734f9d2 commit 1d2bc3e
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 31 deletions.
31 changes: 25 additions & 6 deletions packages/dmn-js-decision-table/src/features/keyboard/Keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import {
isShift
} from './KeyboardUtil';

var compatMessage =
'Keyboard binding is now implicit; explicit binding to an element got removed. ' +
'For more information, see https://github.com/bpmn-io/diagram-js/pull/662';


/**
* A keyboard abstraction that may be activated and
Expand All @@ -27,17 +31,18 @@ import {
*
* All events contain the fields (node, listeners).
*
* A default binding for the keyboard may be specified via the
* `keyboard.bindTo` configuration option.
* Specify the initial keyboard binding state via the
* `keyboard.bind=true|false` configuration option.
*
* @param {Config} config
* @param {EventBus} eventBus
* @param {EditorActions} editorActions
* @param {CellSelection} cellSelection
* @param {Renderer} renderer
*/
export default class Keyboard {

constructor(config, eventBus, editorActions, cellSelection) {
constructor(config, eventBus, editorActions, cellSelection, renderer) {

this._config = config || {};
this._editorActions = editorActions;
Expand All @@ -52,7 +57,16 @@ export default class Keyboard {
eventBus.on('attach', () => {

if (this._config.bindTo) {
this.bind(config.bindTo);
console.error('unsupported configuration <keyboard.bindTo>',
new Error(compatMessage));
}

this._target = renderer.getContainer();

var bind = this._config && this._config.bind !== false;

if (bind) {
this.bind();
}
});

Expand Down Expand Up @@ -96,10 +110,14 @@ export default class Keyboard {

bind(node) {

if (node) {
console.error('unsupported argument <node>', new Error(compatMessage));
}

// make sure that the keyboard is only bound once to the DOM
this.unbind();

this._node = node;
node = this._node = this._target;

// bind key events
domEvent.bind(node, 'keydown', this._keyHandler);
Expand Down Expand Up @@ -241,7 +259,8 @@ Keyboard.$inject = [
'config.keyboard',
'eventBus',
'editorActions',
'cellSelection'
'cellSelection',
'renderer'
];


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import diagramXML from './copy-cut-paste-key-bindings.dmn';

describe('features/copy-cut-paste/key-bindings', function() {

const keyboardTarget = document.createElement('div');

beforeEach(bootstrapModeler(diagramXML, {
modules: [
Expand All @@ -41,17 +40,16 @@ describe('features/copy-cut-paste/key-bindings', function() {
DecisionRulesEditorModule,
CopyCutPasteKeyBindingsModule,
SelectionModule
],
keyboard: {
bindTo: keyboardTarget
}
]
}));


let testContainer;
let keyboardTarget;

beforeEach(function() {
testContainer = TestContainer.get(this);
keyboardTarget = testContainer.querySelector('.dmn-decision-table-container');
});


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global sinon */

import {
bootstrapModeler,
inject
Expand Down Expand Up @@ -26,7 +28,6 @@ import {

describe('features/keyboard', function() {

const keyboardTarget = document.createElement('div');

beforeEach(bootstrapModeler(diagramXML, {
modules: [
Expand All @@ -37,27 +38,42 @@ describe('features/keyboard', function() {
DecisionRulesEditorModule,
TablePropertiesEditorModule,
SelectionModule
],
keyboard: {
bindTo: keyboardTarget
}
]
}));

describe('keyboard binding', function() {

let testContainer;

beforeEach(function() {
testContainer = TestContainer.get(this);
});

it('should integrate with <attach> + <detach> events', inject(
function(keyboard, eventBus) {
function(eventBus) {

// given
const keyboardTarget =
testContainer.querySelector('.dmn-decision-table-container');

// assume
expect(keyboard._node).to.eql(keyboardTarget);
const bindSpy = sinon.spy();
eventBus.on('keyboard.bind', ({ node }) => bindSpy(node));

// when
eventBus.fire('attach');

// then
expect(bindSpy).to.have.been.calledOnceWith(keyboardTarget);

// and given
const unbindSpy = sinon.spy();
eventBus.on('keyboard.unbind', ({ node }) => unbindSpy(node));

// when
eventBus.fire('detach');
expect(keyboard._node).not.to.exist;

// but when
eventBus.fire('attach');
expect(keyboard._node).to.eql(keyboardTarget);
// then
expect(unbindSpy).to.have.been.calledOnceWith(keyboardTarget);
}
));

Expand All @@ -73,7 +89,7 @@ describe('features/keyboard', function() {
});

beforeEach(inject(function(keyboard) {
keyboard.bind(testContainer);
keyboard.bind();
}));

it('should select cell below on <ENTER>', inject(function(cellSelection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import {
isShift
} from './KeyboardUtil';

var compatMessage =
'Keyboard binding is now implicit; explicit binding to an element got removed. ' +
'For more information, see https://github.com/bpmn-io/diagram-js/pull/662';


/**
* A keyboard abstraction that may be activated and
* deactivated by users at will, consuming key events
Expand All @@ -22,16 +27,17 @@ import {
*
* All events contain the fields (node, listeners).
*
* A default binding for the keyboard may be specified via the
* `keyboard.bindTo` configuration option.
* Specify the initial keyboard binding state via the
* `keyboard.bind=true|false` configuration option.
*
* @param {Config} config
* @param {EventBus} eventBus
* @param {EditorActions} editorActions
* @param {Renderer} renderer
*/
export default class Keyboard {

constructor(config, eventBus, editorActions) {
constructor(config, eventBus, editorActions, renderer) {

this._config = config || {};
this._eventBus = eventBus;
Expand All @@ -45,7 +51,16 @@ export default class Keyboard {
eventBus.on('attach', () => {

if (this._config.bindTo) {
this.bind(config.bindTo);
console.error('unsupported configuration <keyboard.bindTo>',
new Error(compatMessage));
}

this._target = renderer.getContainer();

var bind = this._config && this._config.bind !== false;

if (bind) {
this.bind();
}
});

Expand Down Expand Up @@ -89,10 +104,14 @@ export default class Keyboard {

bind(node) {

if (node) {
console.error('unsupported argument <node>', new Error(compatMessage));
}

// make sure that the keyboard is only bound once to the DOM
this.unbind();

this._node = node;
node = this._node = this._target;

// bind key events
domEvent.bind(node, 'keydown', this._keyHandler, true);
Expand Down Expand Up @@ -185,5 +204,6 @@ export default class Keyboard {
Keyboard.$inject = [
'config.keyboard',
'eventBus',
'editorActions'
'editorActions',
'renderer'
];
2 changes: 1 addition & 1 deletion packages/dmn-js/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ___Note:__ Yet to be released changes appear here._

### Breaking Changes

* Keyboard (DRD) is now implicit, and canvas is focusable, cf. [bpmn-io/diagram-js#662](https://github.com/bpmn-io/diagram-js/pull/662)
* Keyboard (DRD) is now implicit, and canvas is focusable, cf. ([bpmn-io/diagram-js#662](https://github.com/bpmn-io/diagram-js/pull/662))

## 16.8.2

Expand Down

0 comments on commit 1d2bc3e

Please sign in to comment.