UNPKG

@wordpress/block-editor

Version:
235 lines (218 loc) 7.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; exports.useMouseMoveTypingReset = useMouseMoveTypingReset; exports.useTypingObserver = useTypingObserver; var _compose = require("@wordpress/compose"); var _data = require("@wordpress/data"); var _dom = require("@wordpress/dom"); var _keycodes = require("@wordpress/keycodes"); var _store = require("../../store"); var _jsxRuntime = require("react/jsx-runtime"); /** * WordPress dependencies */ /** * Internal dependencies */ /** * Set of key codes upon which typing is to be initiated on a keydown event. * * @type {Set<number>} */const KEY_DOWN_ELIGIBLE_KEY_CODES = new Set([_keycodes.UP, _keycodes.RIGHT, _keycodes.DOWN, _keycodes.LEFT, _keycodes.ENTER, _keycodes.BACKSPACE]); /** * Returns true if a given keydown event can be inferred as intent to start * typing, or false otherwise. A keydown is considered eligible if it is a * text navigation without shift active. * * @param {KeyboardEvent} event Keydown event to test. * * @return {boolean} Whether event is eligible to start typing. */ function isKeyDownEligibleForStartTyping(event) { const { keyCode, shiftKey } = event; return !shiftKey && KEY_DOWN_ELIGIBLE_KEY_CODES.has(keyCode); } /** * Removes the `isTyping` flag when the mouse moves in the document of the given * element. */ function useMouseMoveTypingReset() { const isTyping = (0, _data.useSelect)(select => select(_store.store).isTyping(), []); const { stopTyping } = (0, _data.useDispatch)(_store.store); return (0, _compose.useRefEffect)(node => { if (!isTyping) { return; } const { ownerDocument } = node; let lastClientX; let lastClientY; /** * On mouse move, unset typing flag if user has moved cursor. * * @param {MouseEvent} event Mousemove event. */ function stopTypingOnMouseMove(event) { const { clientX, clientY } = event; // We need to check that the mouse really moved because Safari // triggers mousemove events when shift or ctrl are pressed. if (lastClientX && lastClientY && (lastClientX !== clientX || lastClientY !== clientY)) { stopTyping(); } lastClientX = clientX; lastClientY = clientY; } ownerDocument.addEventListener('mousemove', stopTypingOnMouseMove); return () => { ownerDocument.removeEventListener('mousemove', stopTypingOnMouseMove); }; }, [isTyping, stopTyping]); } /** * Sets and removes the `isTyping` flag based on user actions: * * - Sets the flag if the user types within the given element. * - Removes the flag when the user selects some text, focuses a non-text * field, presses ESC or TAB, or moves the mouse in the document. */ function useTypingObserver() { const { isTyping } = (0, _data.useSelect)(select => { const { isTyping: _isTyping } = select(_store.store); return { isTyping: _isTyping() }; }, []); const { startTyping, stopTyping } = (0, _data.useDispatch)(_store.store); const ref1 = useMouseMoveTypingReset(); const ref2 = (0, _compose.useRefEffect)(node => { const { ownerDocument } = node; const { defaultView } = ownerDocument; const selection = defaultView.getSelection(); // Listeners to stop typing should only be added when typing. // Listeners to start typing should only be added when not typing. if (isTyping) { let timerId; /** * Stops typing when focus transitions to a non-text field element. * * @param {FocusEvent} event Focus event. */ function stopTypingOnNonTextField(event) { const { target } = event; // Since focus to a non-text field via arrow key will trigger // before the keydown event, wait until after current stack // before evaluating whether typing is to be stopped. Otherwise, // typing will re-start. timerId = defaultView.setTimeout(() => { if (!(0, _dom.isTextField)(target)) { stopTyping(); } }); } /** * Unsets typing flag if user presses Escape while typing flag is * active. * * @param {KeyboardEvent} event Keypress or keydown event to * interpret. */ function stopTypingOnEscapeKey(event) { const { keyCode } = event; if (keyCode === _keycodes.ESCAPE || keyCode === _keycodes.TAB) { stopTyping(); } } /** * On selection change, unset typing flag if user has made an * uncollapsed (shift) selection. */ function stopTypingOnSelectionUncollapse() { if (!selection.isCollapsed) { stopTyping(); } } node.addEventListener('focus', stopTypingOnNonTextField); node.addEventListener('keydown', stopTypingOnEscapeKey); ownerDocument.addEventListener('selectionchange', stopTypingOnSelectionUncollapse); return () => { defaultView.clearTimeout(timerId); node.removeEventListener('focus', stopTypingOnNonTextField); node.removeEventListener('keydown', stopTypingOnEscapeKey); ownerDocument.removeEventListener('selectionchange', stopTypingOnSelectionUncollapse); }; } /** * Handles a keypress or keydown event to infer intention to start * typing. * * @param {KeyboardEvent} event Keypress or keydown event to interpret. */ function startTypingInTextField(event) { const { type, target } = event; // Abort early if already typing, or key press is incurred outside a // text field (e.g. arrow-ing through toolbar buttons). // Ignore typing if outside the current DOM container if (!(0, _dom.isTextField)(target) || !node.contains(target)) { return; } // Special-case keydown because certain keys do not emit a keypress // event. Conversely avoid keydown as the canonical event since // there are many keydown which are explicitly not targeted for // typing. if (type === 'keydown' && !isKeyDownEligibleForStartTyping(event)) { return; } startTyping(); } node.addEventListener('keypress', startTypingInTextField); node.addEventListener('keydown', startTypingInTextField); return () => { node.removeEventListener('keypress', startTypingInTextField); node.removeEventListener('keydown', startTypingInTextField); }; }, [isTyping, startTyping, stopTyping]); return (0, _compose.useMergeRefs)([ref1, ref2]); } function ObserveTyping({ children }) { return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", { ref: useTypingObserver(), children: children }); } /** * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/observe-typing/README.md */ var _default = exports.default = ObserveTyping; //# sourceMappingURL=index.js.map