aboutsummaryrefslogtreecommitdiff
path: root/src/content/presenters
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2019-05-11 19:43:56 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2019-05-18 17:28:11 +0900
commitefc48dc7421e3bd48534bc94f84e2b0bd47ae47c (patch)
treedfe80ebc368911c385e6c36aa1096af619b1616b /src/content/presenters
parenta88324acd9fe626b59637541975abe1ee6041aa7 (diff)
Keymaps as a clean architecture [WIP]
Diffstat (limited to 'src/content/presenters')
-rw-r--r--src/content/presenters/FocusPresenter.ts25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/content/presenters/FocusPresenter.ts b/src/content/presenters/FocusPresenter.ts
new file mode 100644
index 0000000..4cef5bf
--- /dev/null
+++ b/src/content/presenters/FocusPresenter.ts
@@ -0,0 +1,25 @@
+import * as doms from '../../shared/utils/dom';
+
+export default interface FocusPresenter {
+ focusFirstElement(): boolean;
+
+ // eslint-disable-next-line semi
+}
+
+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;
+ }
+}
+