UNPKG

@wordpress/block-editor

Version:
169 lines (167 loc) 5.96 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/block-editor/src/components/observe-typing/index.js var observe_typing_exports = {}; __export(observe_typing_exports, { default: () => observe_typing_default, useMouseMoveTypingReset: () => useMouseMoveTypingReset, useTypingObserver: () => useTypingObserver }); module.exports = __toCommonJS(observe_typing_exports); var import_compose = require("@wordpress/compose"); var import_data = require("@wordpress/data"); var import_dom = require("@wordpress/dom"); var import_keycodes = require("@wordpress/keycodes"); var import_store = require("../../store"); var import_jsx_runtime = require("react/jsx-runtime"); var KEY_DOWN_ELIGIBLE_KEY_CODES = /* @__PURE__ */ new Set([ import_keycodes.UP, import_keycodes.RIGHT, import_keycodes.DOWN, import_keycodes.LEFT, import_keycodes.ENTER, import_keycodes.BACKSPACE ]); function isKeyDownEligibleForStartTyping(event) { const { keyCode, shiftKey } = event; return !shiftKey && KEY_DOWN_ELIGIBLE_KEY_CODES.has(keyCode); } function useMouseMoveTypingReset() { const isTyping = (0, import_data.useSelect)( (select) => select(import_store.store).isTyping(), [] ); const { stopTyping } = (0, import_data.useDispatch)(import_store.store); return (0, import_compose.useRefEffect)( (node) => { if (!isTyping) { return; } const { ownerDocument } = node; let lastClientX; let lastClientY; function stopTypingOnMouseMove(event) { const { clientX, clientY } = event; if (lastClientX && lastClientY && (lastClientX !== clientX || lastClientY !== clientY)) { stopTyping(); } lastClientX = clientX; lastClientY = clientY; } ownerDocument.addEventListener( "mousemove", stopTypingOnMouseMove ); return () => { ownerDocument.removeEventListener( "mousemove", stopTypingOnMouseMove ); }; }, [isTyping, stopTyping] ); } function useTypingObserver() { const { isTyping } = (0, import_data.useSelect)((select) => { const { isTyping: _isTyping } = select(import_store.store); return { isTyping: _isTyping() }; }, []); const { startTyping, stopTyping } = (0, import_data.useDispatch)(import_store.store); const ref1 = useMouseMoveTypingReset(); const ref2 = (0, import_compose.useRefEffect)( (node) => { const { ownerDocument } = node; const { defaultView } = ownerDocument; const selection = defaultView.getSelection(); if (isTyping) { let stopTypingOnNonTextField2 = function(event) { const { target } = event; timerId = defaultView.setTimeout(() => { if (!(0, import_dom.isTextField)(target)) { stopTyping(); } }); }, stopTypingOnEscapeKey2 = function(event) { const { keyCode } = event; if (keyCode === import_keycodes.ESCAPE || keyCode === import_keycodes.TAB) { stopTyping(); } }, stopTypingOnSelectionUncollapse2 = function() { if (!selection.isCollapsed) { stopTyping(); } }; var stopTypingOnNonTextField = stopTypingOnNonTextField2, stopTypingOnEscapeKey = stopTypingOnEscapeKey2, stopTypingOnSelectionUncollapse = stopTypingOnSelectionUncollapse2; let timerId; node.addEventListener("focus", stopTypingOnNonTextField2); node.addEventListener("keydown", stopTypingOnEscapeKey2); ownerDocument.addEventListener( "selectionchange", stopTypingOnSelectionUncollapse2 ); return () => { defaultView.clearTimeout(timerId); node.removeEventListener( "focus", stopTypingOnNonTextField2 ); node.removeEventListener( "keydown", stopTypingOnEscapeKey2 ); ownerDocument.removeEventListener( "selectionchange", stopTypingOnSelectionUncollapse2 ); }; } function startTypingInTextField(event) { const { type, target } = event; if (!(0, import_dom.isTextField)(target) || !node.contains(target)) { return; } 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, import_compose.useMergeRefs)([ref1, ref2]); } function ObserveTyping({ children }) { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { ref: useTypingObserver(), children }); } var observe_typing_default = ObserveTyping; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { useMouseMoveTypingReset, useTypingObserver }); //# sourceMappingURL=index.js.map