This repository has been archived on 2020-04-04. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Vim-Vixen/src/content/presenters/FocusPresenter.ts
2019-05-22 20:30:20 +09:00

23 lines
729 B
TypeScript

import * as doms from '../../shared/utils/dom';
export default interface FocusPresenter {
focusFirstElement(): boolean;
}
export class FocusPresenterImpl implements FocusPresenter {
focusFirstElement(): boolean {
let inputTypes = ['email', 'number', 'search', 'tel', 'text', 'url'];
let inputSelector = inputTypes.map(type => `input[type=${type}]`).join(',');
let targets = window.document.querySelectorAll(inputSelector + ',textarea');
let target = Array.from(targets).find(doms.isVisible);
if (target instanceof HTMLInputElement) {
target.focus();
return true;
} else if (target instanceof HTMLTextAreaElement) {
target.focus();
return true;
}
return false;
}
}