|
| 1 | +/* eslint-disable @typescript-eslint/no-use-before-define */ |
| 2 | + |
| 3 | +import {setUISelection} from '../../document' |
| 4 | +import { |
| 5 | + focus, |
| 6 | + getTabDestination, |
| 7 | + getValue, |
| 8 | + hasOwnSelection, |
| 9 | + input, |
| 10 | + isContentEditable, |
| 11 | + isEditable, |
| 12 | + isElementType, |
| 13 | + moveSelection, |
| 14 | + selectAll, |
| 15 | + setSelectionRange, |
| 16 | +} from '../../utils' |
| 17 | +import {BehaviorPlugin} from '.' |
| 18 | +import {behavior} from './registry' |
| 19 | + |
| 20 | +behavior.keydown = (event, target, config) => { |
| 21 | + return ( |
| 22 | + keydownBehavior[event.key]?.(event, target, config) ?? |
| 23 | + combinationBehavior(event, target, config) |
| 24 | + ) |
| 25 | +} |
| 26 | + |
| 27 | +const keydownBehavior: { |
| 28 | + [key: string]: BehaviorPlugin<'keydown'> | undefined |
| 29 | +} = { |
| 30 | + ArrowLeft: (event, target) => () => moveSelection(target, -1), |
| 31 | + ArrowRight: (event, target) => () => moveSelection(target, 1), |
| 32 | + Backspace: (event, target, config) => { |
| 33 | + if (isEditable(target)) { |
| 34 | + return () => { |
| 35 | + input(config, target, '', 'deleteContentBackward') |
| 36 | + } |
| 37 | + } |
| 38 | + }, |
| 39 | + Delete: (event, target, config) => { |
| 40 | + if (isEditable(target)) { |
| 41 | + return () => { |
| 42 | + input(config, target, '', 'deleteContentForward') |
| 43 | + } |
| 44 | + } |
| 45 | + }, |
| 46 | + End: (event, target) => { |
| 47 | + if ( |
| 48 | + isElementType(target, ['input', 'textarea']) || |
| 49 | + isContentEditable(target) |
| 50 | + ) { |
| 51 | + return () => { |
| 52 | + const newPos = getValue(target)?.length ?? /* istanbul ignore next */ 0 |
| 53 | + setSelectionRange(target, newPos, newPos) |
| 54 | + } |
| 55 | + } |
| 56 | + }, |
| 57 | + Home: (event, target) => { |
| 58 | + if ( |
| 59 | + isElementType(target, ['input', 'textarea']) || |
| 60 | + isContentEditable(target) |
| 61 | + ) { |
| 62 | + return () => { |
| 63 | + setSelectionRange(target, 0, 0) |
| 64 | + } |
| 65 | + } |
| 66 | + }, |
| 67 | + PageDown: (event, target) => { |
| 68 | + if (isElementType(target, ['input'])) { |
| 69 | + return () => { |
| 70 | + const newPos = getValue(target).length |
| 71 | + setSelectionRange(target, newPos, newPos) |
| 72 | + } |
| 73 | + } |
| 74 | + }, |
| 75 | + PageUp: (event, target) => { |
| 76 | + if (isElementType(target, ['input'])) { |
| 77 | + return () => { |
| 78 | + setSelectionRange(target, 0, 0) |
| 79 | + } |
| 80 | + } |
| 81 | + }, |
| 82 | + Tab: (event, target, {keyboardState}) => { |
| 83 | + return () => { |
| 84 | + const dest = getTabDestination(target, keyboardState.modifiers.Shift) |
| 85 | + focus(dest) |
| 86 | + if (hasOwnSelection(dest)) { |
| 87 | + setUISelection(dest, { |
| 88 | + anchorOffset: 0, |
| 89 | + focusOffset: dest.value.length, |
| 90 | + }) |
| 91 | + } |
| 92 | + } |
| 93 | + }, |
| 94 | +} |
| 95 | + |
| 96 | +const combinationBehavior: BehaviorPlugin<'keydown'> = ( |
| 97 | + event, |
| 98 | + target, |
| 99 | + config, |
| 100 | +) => { |
| 101 | + if (event.code === 'KeyA' && config.keyboardState.modifiers.Control) { |
| 102 | + return () => selectAll(target) |
| 103 | + } |
| 104 | +} |
0 commit comments