Skip to content

Commit d16c2b4

Browse files
committed
feat(editor): implement diagram-js style hover handling
1 parent 8a3a7d4 commit d16c2b4

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ export default class Renderer {
3636
});
3737
});
3838

39+
eventBus.on('element.hover', function() {
40+
if (document.activeElement === document.body) {
41+
container.focus({ preventScroll: true });
42+
}
43+
});
44+
3945
const App = () => {
4046
const [ state, setState ] = useState(formEditor._getState());
4147

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import {
2+
bootstrapFormEditor,
3+
getFormEditor,
4+
inject
5+
} from 'test/TestHelper';
6+
7+
8+
describe('renderer', function() {
9+
10+
beforeEach(bootstrapFormEditor());
11+
12+
afterEach(function() {
13+
getFormEditor().destroy();
14+
});
15+
16+
17+
function getContainer() {
18+
return getFormEditor()._container;
19+
}
20+
21+
describe('focus handling', function() {
22+
23+
it('should be focusable', inject(function(formEditor) {
24+
25+
// given
26+
var container = getContainer();
27+
28+
// when
29+
container.focus();
30+
31+
// then
32+
expect(document.activeElement).to.equal(container);
33+
}));
34+
35+
36+
describe('<hover>', function() {
37+
38+
beforeEach(function() {
39+
document.body.focus();
40+
});
41+
42+
43+
it('should focus if body is focused', inject(function(formEditor, eventBus) {
44+
45+
// given
46+
var container = getContainer();
47+
48+
// when
49+
eventBus.fire('element.hover');
50+
51+
// then
52+
expect(document.activeElement).to.equal(container);
53+
}));
54+
55+
56+
it('should not scroll on focus', inject(function(canvas, eventBus) {
57+
58+
// given
59+
var container = getContainer();
60+
61+
var clientRect = container.getBoundingClientRect();
62+
63+
// when
64+
eventBus.fire('element.hover');
65+
66+
// then
67+
expect(clientRect).to.eql(container.getBoundingClientRect());
68+
}));
69+
70+
71+
it('should not focus on existing document focus', inject(function(canvas, eventBus) {
72+
73+
// given
74+
var inputEl = document.createElement('input');
75+
76+
document.body.appendChild(inputEl);
77+
inputEl.focus();
78+
79+
// assume
80+
expect(document.activeElement).to.equal(inputEl);
81+
82+
// when
83+
eventBus.fire('element.hover');
84+
85+
// then
86+
expect(document.activeElement).to.eql(inputEl);
87+
}));
88+
89+
});
90+
91+
});
92+
93+
});

0 commit comments

Comments
 (0)