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); // src/useIMEComposition/index.tsx var useIMEComposition_exports = {}; __export(useIMEComposition_exports, { default: () => useIMEComposition }); module.exports = __toCommonJS(useIMEComposition_exports); var import_react = require("react"); var import_func = require("@zhst/func"); var EMIT_CHANGE_AFTER_COMPOSITION_END = (0, import_func.isChrome)(); var defaultGetEventValue = (e) => e.target.value; function useIMEComposition(propValue, onChangeProp, getEventValueProp, onCompositionStartProp, onCompositionEndProp) { const getEventValue = getEventValueProp || defaultGetEventValue; const isCompositionRef = (0, import_react.useRef)(false); const [compositionValue, setCompositionValue] = (0, import_react.useState)(propValue); const compositionValueRef = (0, import_react.useRef)(compositionValue); const onChangeRef = (0, import_react.useRef)(onChangeProp); const onCompositionStartRef = (0, import_react.useRef)(onCompositionStartProp); const onCompositionEndRef = (0, import_react.useRef)(onCompositionEndProp); compositionValueRef.current = compositionValue; onChangeRef.current = onChangeProp; onCompositionStartRef.current = onCompositionStartProp; onCompositionEndRef.current = onCompositionEndProp; const onCompositionValueChange = (0, import_react.useCallback)( (...args) => { var _a; if (isCompositionRef.current) { setCompositionValue(getEventValue(...args)); return; } return (_a = onChangeRef.current) == null ? void 0 : _a.call(onChangeRef, ...args); }, [onChangeRef] ); const onCompositionStart = (0, import_react.useCallback)( (e) => { var _a; isCompositionRef.current = true; (_a = onCompositionStartRef.current) == null ? void 0 : _a.call(onCompositionStartRef, e); }, [onCompositionStartRef] ); const onCompositionEnd = (0, import_react.useCallback)( (e) => { var _a, _b; isCompositionRef.current = false; (_a = onCompositionEndRef.current) == null ? void 0 : _a.call(onCompositionEndRef, e); if (EMIT_CHANGE_AFTER_COMPOSITION_END) { e.type = "change"; (_b = onChangeRef.current) == null ? void 0 : _b.call(onChangeRef, compositionValueRef.current, e); } }, [onCompositionEndRef, onChangeRef] ); const isControlled = propValue !== void 0; const passCompositionHandler = isControlled; const passCompositionValue = isControlled && isCompositionRef.current; return { value: passCompositionValue ? compositionValue : propValue, onChange: passCompositionHandler ? onCompositionValueChange : onChangeProp, onCompositionStart: passCompositionHandler ? onCompositionStart : onCompositionStartProp, onCompositionEnd: passCompositionHandler ? onCompositionEnd : onCompositionEndProp }; }