Skip to content

Commit d3b4db9

Browse files
committed
Improved hack for focus in Dialog
closes #28
1 parent d225b81 commit d3b4db9

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

src/main/java/org/vaadin/tinymce/TinyMce.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,8 @@ public void focus() {
241241
// Dialog has timing issues...
242242
getElement().executeJs("""
243243
const el = this;
244-
const isInShadowRoot = document.body.contains(el);
245-
if(isInShadowRoot) {
244+
if(el.$connector.isInDialog()) {
246245
setTimeout(() => {
247-
debugger;
248246
el.$connector.focus()
249247
}, 150);
250248
} else {

src/main/resources/META-INF/resources/frontend/tinymceConnector.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,19 @@ window.Vaadin.Flow.tinymceConnector = {
3434
readonlyTimeout = setTimeout(() => {
3535
this.editor.mode.set(enabled ? 'design' : 'readonly');
3636
}, 20);
37+
},
38+
isInDialog: function() {
39+
let inDialog = false;
40+
let parent = c.parentElement;
41+
while(parent != null) {
42+
if(parent.tagName.toLowerCase().indexOf("vaadin-dialog") == 0) {
43+
inDialog = true;
44+
break;
45+
}
46+
parent = parent.parentElement;
47+
}
48+
return inDialog;
3749
}
38-
3950
};
4051
}
4152

@@ -83,6 +94,18 @@ window.Vaadin.Flow.tinymceConnector = {
8394
c.dispatchEvent(event);
8495
});
8596

97+
if(c.$connector.isInDialog()) {
98+
// This is inside a shadowroot (Dialog in Vaadin)
99+
// and needs some hacks to make this nagigateable with keyboard
100+
if(!c.tabIndex) {
101+
// make the wrapping element also focusable
102+
c.setAttribute("tabindex", 0);
103+
}
104+
// on focus to wrapping element, pass forward to editor
105+
c.addEventListener("focus", e=> {
106+
ed.focus();
107+
});
108+
}
86109
};
87110

88111
ta.innerHTML = initialContent;

src/test/java/org/vaadin/tinymce/PreserveOnRefreshBug27.java

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public class PreserveOnRefreshBug27 extends VerticalLayout {
1616
public PreserveOnRefreshBug27() {
1717
Dialog dialog = new Dialog();
1818
TinyMce tinyMce = new TinyMce();
19-
tinyMce.setTabIndex(1);
2019
tinyMce.configure("branding", false);
2120
tinyMce.configure("statusbar", false);
2221
tinyMce.setValue("<h2>Hallo Leute,</h2>");

0 commit comments

Comments
 (0)