|
1 | 1 | import store from '../../store.js' |
2 | 2 | import NonFocusedType from '../errors/NonFocusedType.js' |
3 | 3 |
|
4 | | -export async function checkFocusBeforeType(helper) { |
5 | | - const isStrict = helper.options.strict |
6 | | - if (!isStrict && !store.debugMode) return |
| 4 | +const EDITING_KEYS = new Set(['a', 'c', 'x', 'v', 'z', 'y']) |
7 | 5 |
|
8 | | - const noFocus = await helper.executeScript(() => { |
| 6 | +async function isNoElementFocused(helper) { |
| 7 | + return helper.executeScript(() => { |
9 | 8 | const ae = document.activeElement |
10 | 9 | return !ae || ae === document.documentElement || (ae === document.body && !ae.isContentEditable) |
11 | 10 | }) |
| 11 | +} |
| 12 | + |
| 13 | +export async function checkFocusBeforeType(helper) { |
| 14 | + if (!helper.options.strict && !store.debugMode) return |
| 15 | + if (!await isNoElementFocused(helper)) return |
| 16 | + |
| 17 | + const message = 'No element is in focus. Use I.click() or I.focus() to activate an element before typing.' |
| 18 | + if (helper.options.strict) throw new NonFocusedType(message) |
| 19 | + helper.debugSection('Warning', message) |
| 20 | +} |
| 21 | + |
| 22 | +export async function checkFocusBeforePressKey(helper, modifiers, key) { |
| 23 | + if (!helper.options.strict && !store.debugMode) return |
12 | 24 |
|
13 | | - if (!noFocus) return |
| 25 | + const hasCtrlOrMeta = modifiers.some(m => m === 'Control' || m === 'Meta' |
| 26 | + || m === 'ControlLeft' || m === 'ControlRight' || m === 'MetaLeft' || m === 'MetaRight') |
| 27 | + if (!hasCtrlOrMeta || !EDITING_KEYS.has(key.toLowerCase())) return |
14 | 28 |
|
15 | | - if (isStrict) { |
16 | | - throw new NonFocusedType() |
17 | | - } |
| 29 | + if (!await isNoElementFocused(helper)) return |
18 | 30 |
|
19 | | - helper.debugSection('Warning', 'No element is in focus. Use I.click() or I.focus() to activate an element before typing.') |
| 31 | + const message = `No element is in focus. Key combination with "${key}" may not work as expected. Use I.click() or I.focus() first.` |
| 32 | + if (helper.options.strict) throw new NonFocusedType(message) |
| 33 | + helper.debugSection('Warning', message) |
20 | 34 | } |
0 commit comments