-
-
Notifications
You must be signed in to change notification settings - Fork 754
Expand file tree
/
Copy pathfocusCheck.js
More file actions
43 lines (35 loc) · 1.57 KB
/
Copy pathfocusCheck.js
File metadata and controls
43 lines (35 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import store from '../../store.js'
import NonFocusedType from '../errors/NonFocusedType.js'
const MODIFIER_PATTERN = /^(control|ctrl|meta|cmd|command|commandorcontrol|ctrlorcommand)/i
const EDITING_KEYS = new Set(['a', 'c', 'x', 'v', 'z', 'y'])
async function isNoElementFocused(helper) {
return helper.executeScript(() => {
const ae = document.activeElement
return !ae || ae === document.documentElement || (ae === document.body && !ae.isContentEditable)
})
}
export async function checkFocusBeforeType(helper) {
if (!helper.options.strict && !store.debugMode) return
if (!await isNoElementFocused(helper)) return
const message = 'No element is in focus. Use I.click() or I.focus() to activate an element before typing.'
if (helper.options.strict) throw new NonFocusedType(message)
helper.debugSection('Warning', message)
}
export async function checkFocusBeforePressKey(helper, originalKey) {
if (!helper.options.strict && !store.debugMode) return
if (!Array.isArray(originalKey)) return
let hasCtrlOrMeta = false
let actionKey = null
for (const k of originalKey) {
if (MODIFIER_PATTERN.test(k)) {
hasCtrlOrMeta = true
} else {
actionKey = k
}
}
if (!hasCtrlOrMeta || !actionKey || !EDITING_KEYS.has(actionKey.toLowerCase())) return
if (!await isNoElementFocused(helper)) return
const message = `No element is in focus. Key combination with "${originalKey.join('+')}" may not work as expected. Use I.click() or I.focus() first.`
if (helper.options.strict) throw new NonFocusedType(message)
helper.debugSection('Warning', message)
}