devexpress-richedit
Version:
DevExpress Rich Text Editor is an advanced word-processing tool designed for working with rich text documents.
53 lines (52 loc) • 1.54 kB
JavaScript
export class FocusHelper {
static preventFocusOnClick(element) {
return new FocusBlocker(element);
}
}
class FocusBlocker {
target;
static focusableSelectors = [
'a[href]',
'area[href]',
'input',
'select',
'textarea',
'button',
'iframe',
'object',
'embed',
'[tabindex]:not([tabindex="-1"])',
'[contenteditable]:not([contenteditable="false"])'
];
static focusableSelector = this.focusableSelectors.join(',');
onPointerDownBinded = this.onPointerDown.bind(this);
constructor(target) {
this.target = target;
this.addEventListeners(target);
}
addEventListeners(element) {
element.addEventListener("mousedown", this.onPointerDownBinded);
}
removeEventListeners(element) {
element.removeEventListener("mousedown", this.onPointerDownBinded);
}
onPointerDown(event) {
for (const element of event.composedPath()) {
if (element === this.target) {
event.preventDefault();
break;
}
if (element instanceof Element && this.isFocusableElement(element))
break;
}
}
isFocusableElement(element) {
return element.matches(FocusBlocker.focusableSelector);
}
dispose() {
if (this.target) {
this.removeEventListeners(this.target);
this.target = null;
}
}
}