Skip to content

Commit

Permalink
fix(core/Canvas): explicitly enable autoFocus
Browse files Browse the repository at this point in the history
We'll otherwise break embedding with existing applications, cf.
bpmn-io/bpmn-js-examples#302.
  • Loading branch information
nikku committed Dec 19, 2024
1 parent 88e15c5 commit 724be04
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/core/Canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { createMatrix as createMatrix } from 'tiny-svg';
* deferUpdate?: boolean;
* width?: number;
* height?: number;
* autoFocus?: boolean;
* } } CanvasConfig
* @typedef { {
* group: SVGElement;
Expand Down Expand Up @@ -235,7 +236,7 @@ Canvas.prototype._init = function(config) {

domAttr(svg, 'tabindex', 0);

eventBus.on('element.hover', () => {
config.autoFocus && eventBus.on('element.hover', () => {
this.restoreFocus();
});

Expand Down
37 changes: 34 additions & 3 deletions test/spec/core/CanvasSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { getChildren as getChildrenGfx } from 'lib/util/GraphicsUtil';


describe('Canvas', function() {
describe('core/Canvas', function() {

var container;

Expand Down Expand Up @@ -112,7 +112,7 @@ describe('Canvas', function() {
});


it('should focus if body is focused', inject(function(canvas, eventBus) {
it('should NOT focus if body is focused', inject(function(canvas, eventBus) {

// given
var svg = container.querySelector('svg');
Expand All @@ -124,7 +124,7 @@ describe('Canvas', function() {
});

// then
expect(document.activeElement).to.equal(svg);
expect(document.activeElement).to.not.equal(svg);
}));


Expand Down Expand Up @@ -174,6 +174,37 @@ describe('Canvas', function() {
});


describe('focus handling <config.autoFocus>', function() {

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

beforeEach(createDiagram({
canvas: {
autoFocus: true
}
}));


it('should focus if body is focused', inject(function(canvas, eventBus) {

// given
var svg = container.querySelector('svg');

// when
eventBus.fire('element.hover', {
element: canvas.getRootElement(),
gfx: svg
});

// then
expect(document.activeElement).to.equal(svg);
}));

});


describe('events', function() {

beforeEach(function() {
Expand Down

0 comments on commit 724be04

Please sign in to comment.