UNPKG

smart-compose-react

Version:

输入字符可以智能提醒,按一下 Tab 键位就可以快速输入智能提醒内容

695 lines (691 loc) 23.5 kB
import ke, { useRef as fr, useId as we, useEffect as vr } from "react"; var Y = function() { return Y = Object.assign || function(m) { for (var h, E = 1, T = arguments.length; E < T; E++) { h = arguments[E]; for (var c in h) Object.prototype.hasOwnProperty.call(h, c) && (m[c] = h[c]); } return m; }, Y.apply(this, arguments); }; var re = { exports: {} }, L = {}; /** * @license React * react-jsx-runtime.production.min.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var Oe; function dr() { if (Oe) return L; Oe = 1; var y = ke, m = Symbol.for("react.element"), h = Symbol.for("react.fragment"), E = Object.prototype.hasOwnProperty, T = y.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, c = { key: !0, ref: !0, __self: !0, __source: !0 }; function O(C, g, P) { var b, w = {}, l = null, u = null; P !== void 0 && (l = "" + P), g.key !== void 0 && (l = "" + g.key), g.ref !== void 0 && (u = g.ref); for (b in g) E.call(g, b) && !c.hasOwnProperty(b) && (w[b] = g[b]); if (C && C.defaultProps) for (b in g = C.defaultProps, g) w[b] === void 0 && (w[b] = g[b]); return { $$typeof: m, type: C, key: l, ref: u, props: w, _owner: T.current }; } return L.Fragment = h, L.jsx = O, L.jsxs = O, L; } var N = {}; /** * @license React * react-jsx-runtime.development.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var Pe; function pr() { return Pe || (Pe = 1, process.env.NODE_ENV !== "production" && function() { var y = ke, m = Symbol.for("react.element"), h = Symbol.for("react.portal"), E = Symbol.for("react.fragment"), T = Symbol.for("react.strict_mode"), c = Symbol.for("react.profiler"), O = Symbol.for("react.provider"), C = Symbol.for("react.context"), g = Symbol.for("react.forward_ref"), P = Symbol.for("react.suspense"), b = Symbol.for("react.suspense_list"), w = Symbol.for("react.memo"), l = Symbol.for("react.lazy"), u = Symbol.for("react.offscreen"), _ = Symbol.iterator, k = "@@iterator"; function A(e) { if (e === null || typeof e != "object") return null; var r = _ && e[_] || e[k]; return typeof r == "function" ? r : null; } var p = y.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED; function f(e) { { for (var r = arguments.length, t = new Array(r > 1 ? r - 1 : 0), n = 1; n < r; n++) t[n - 1] = arguments[n]; Ae("error", e, t); } } function Ae(e, r, t) { { var n = p.ReactDebugCurrentFrame, i = n.getStackAddendum(); i !== "" && (r += "%s", t = t.concat([i])); var s = t.map(function(o) { return String(o); }); s.unshift("Warning: " + r), Function.prototype.apply.call(console[e], console, s); } } var De = !1, Fe = !1, Ie = !1, $e = !1, We = !1, te; te = Symbol.for("react.module.reference"); function Le(e) { return !!(typeof e == "string" || typeof e == "function" || e === E || e === c || We || e === T || e === P || e === b || $e || e === u || De || Fe || Ie || typeof e == "object" && e !== null && (e.$$typeof === l || e.$$typeof === w || e.$$typeof === O || e.$$typeof === C || e.$$typeof === g || e.$$typeof === te || e.getModuleId !== void 0)); } function Ne(e, r, t) { var n = e.displayName; if (n) return n; var i = r.displayName || r.name || ""; return i !== "" ? t + "(" + i + ")" : t; } function ne(e) { return e.displayName || "Context"; } function j(e) { if (e == null) return null; if (typeof e.tag == "number" && f("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."), typeof e == "function") return e.displayName || e.name || null; if (typeof e == "string") return e; switch (e) { case E: return "Fragment"; case h: return "Portal"; case c: return "Profiler"; case T: return "StrictMode"; case P: return "Suspense"; case b: return "SuspenseList"; } if (typeof e == "object") switch (e.$$typeof) { case C: var r = e; return ne(r) + ".Consumer"; case O: var t = e; return ne(t._context) + ".Provider"; case g: return Ne(e, e.render, "ForwardRef"); case w: var n = e.displayName || null; return n !== null ? n : j(e.type) || "Memo"; case l: { var i = e, s = i._payload, o = i._init; try { return j(o(s)); } catch { return null; } } } return null; } var D = Object.assign, $ = 0, ae, oe, ie, ue, se, le, ce; function fe() { } fe.__reactDisabledLog = !0; function Ye() { { if ($ === 0) { ae = console.log, oe = console.info, ie = console.warn, ue = console.error, se = console.group, le = console.groupCollapsed, ce = console.groupEnd; var e = { configurable: !0, enumerable: !0, value: fe, writable: !0 }; Object.defineProperties(console, { info: e, log: e, warn: e, error: e, group: e, groupCollapsed: e, groupEnd: e }); } $++; } } function Ve() { { if ($--, $ === 0) { var e = { configurable: !0, enumerable: !0, writable: !0 }; Object.defineProperties(console, { log: D({}, e, { value: ae }), info: D({}, e, { value: oe }), warn: D({}, e, { value: ie }), error: D({}, e, { value: ue }), group: D({}, e, { value: se }), groupCollapsed: D({}, e, { value: le }), groupEnd: D({}, e, { value: ce }) }); } $ < 0 && f("disabledDepth fell below zero. This is a bug in React. Please file an issue."); } } var z = p.ReactCurrentDispatcher, J; function V(e, r, t) { { if (J === void 0) try { throw Error(); } catch (i) { var n = i.stack.trim().match(/\n( *(at )?)/); J = n && n[1] || ""; } return ` ` + J + e; } } var G = !1, M; { var Me = typeof WeakMap == "function" ? WeakMap : Map; M = new Me(); } function ve(e, r) { if (!e || G) return ""; { var t = M.get(e); if (t !== void 0) return t; } var n; G = !0; var i = Error.prepareStackTrace; Error.prepareStackTrace = void 0; var s; s = z.current, z.current = null, Ye(); try { if (r) { var o = function() { throw Error(); }; if (Object.defineProperty(o.prototype, "props", { set: function() { throw Error(); } }), typeof Reflect == "object" && Reflect.construct) { try { Reflect.construct(o, []); } catch (x) { n = x; } Reflect.construct(e, [], o); } else { try { o.call(); } catch (x) { n = x; } e.call(o.prototype); } } else { try { throw Error(); } catch (x) { n = x; } e(); } } catch (x) { if (x && n && typeof x.stack == "string") { for (var a = x.stack.split(` `), R = n.stack.split(` `), v = a.length - 1, d = R.length - 1; v >= 1 && d >= 0 && a[v] !== R[d]; ) d--; for (; v >= 1 && d >= 0; v--, d--) if (a[v] !== R[d]) { if (v !== 1 || d !== 1) do if (v--, d--, d < 0 || a[v] !== R[d]) { var S = ` ` + a[v].replace(" at new ", " at "); return e.displayName && S.includes("<anonymous>") && (S = S.replace("<anonymous>", e.displayName)), typeof e == "function" && M.set(e, S), S; } while (v >= 1 && d >= 0); break; } } } finally { G = !1, z.current = s, Ve(), Error.prepareStackTrace = i; } var I = e ? e.displayName || e.name : "", Ce = I ? V(I) : ""; return typeof e == "function" && M.set(e, Ce), Ce; } function Ue(e, r, t) { return ve(e, !1); } function Be(e) { var r = e.prototype; return !!(r && r.isReactComponent); } function U(e, r, t) { if (e == null) return ""; if (typeof e == "function") return ve(e, Be(e)); if (typeof e == "string") return V(e); switch (e) { case P: return V("Suspense"); case b: return V("SuspenseList"); } if (typeof e == "object") switch (e.$$typeof) { case g: return Ue(e.render); case w: return U(e.type, r, t); case l: { var n = e, i = n._payload, s = n._init; try { return U(s(i), r, t); } catch { } } } return ""; } var B = Object.prototype.hasOwnProperty, de = {}, pe = p.ReactDebugCurrentFrame; function q(e) { if (e) { var r = e._owner, t = U(e.type, e._source, r ? r.type : null); pe.setExtraStackFrame(t); } else pe.setExtraStackFrame(null); } function qe(e, r, t, n, i) { { var s = Function.call.bind(B); for (var o in e) if (s(e, o)) { var a = void 0; try { if (typeof e[o] != "function") { var R = Error((n || "React class") + ": " + t + " type `" + o + "` is invalid; it must be a function, usually from the `prop-types` package, but received `" + typeof e[o] + "`.This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`."); throw R.name = "Invariant Violation", R; } a = e[o](r, o, n, t, null, "SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"); } catch (v) { a = v; } a && !(a instanceof Error) && (q(i), f("%s: type specification of %s `%s` is invalid; the type checker function must return `null` or an `Error` but returned a %s. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument).", n || "React class", t, o, typeof a), q(null)), a instanceof Error && !(a.message in de) && (de[a.message] = !0, q(i), f("Failed %s type: %s", t, a.message), q(null)); } } } var ze = Array.isArray; function H(e) { return ze(e); } function Je(e) { { var r = typeof Symbol == "function" && Symbol.toStringTag, t = r && e[Symbol.toStringTag] || e.constructor.name || "Object"; return t; } } function Ge(e) { try { return ge(e), !1; } catch { return !0; } } function ge(e) { return "" + e; } function me(e) { if (Ge(e)) return f("The provided key is an unsupported type %s. This value must be coerced to a string before before using it here.", Je(e)), ge(e); } var W = p.ReactCurrentOwner, He = { key: !0, ref: !0, __self: !0, __source: !0 }, he, Ee, K; K = {}; function Ke(e) { if (B.call(e, "ref")) { var r = Object.getOwnPropertyDescriptor(e, "ref").get; if (r && r.isReactWarning) return !1; } return e.ref !== void 0; } function Xe(e) { if (B.call(e, "key")) { var r = Object.getOwnPropertyDescriptor(e, "key").get; if (r && r.isReactWarning) return !1; } return e.key !== void 0; } function Ze(e, r) { if (typeof e.ref == "string" && W.current && r && W.current.stateNode !== r) { var t = j(W.current.type); K[t] || (f('Component "%s" contains the string ref "%s". Support for string refs will be removed in a future major release. This case cannot be automatically converted to an arrow function. We ask you to manually fix this case by using useRef() or createRef() instead. Learn more about using refs safely here: https://reactjs.org/link/strict-mode-string-ref', j(W.current.type), e.ref), K[t] = !0); } } function Qe(e, r) { { var t = function() { he || (he = !0, f("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)", r)); }; t.isReactWarning = !0, Object.defineProperty(e, "key", { get: t, configurable: !0 }); } } function er(e, r) { { var t = function() { Ee || (Ee = !0, f("%s: `ref` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)", r)); }; t.isReactWarning = !0, Object.defineProperty(e, "ref", { get: t, configurable: !0 }); } } var rr = function(e, r, t, n, i, s, o) { var a = { $$typeof: m, type: e, key: r, ref: t, props: o, _owner: s }; return a._store = {}, Object.defineProperty(a._store, "validated", { configurable: !1, enumerable: !1, writable: !0, value: !1 }), Object.defineProperty(a, "_self", { configurable: !1, enumerable: !1, writable: !1, value: n }), Object.defineProperty(a, "_source", { configurable: !1, enumerable: !1, writable: !1, value: i }), Object.freeze && (Object.freeze(a.props), Object.freeze(a)), a; }; function tr(e, r, t, n, i) { { var s, o = {}, a = null, R = null; t !== void 0 && (me(t), a = "" + t), Xe(r) && (me(r.key), a = "" + r.key), Ke(r) && (R = r.ref, Ze(r, i)); for (s in r) B.call(r, s) && !He.hasOwnProperty(s) && (o[s] = r[s]); if (e && e.defaultProps) { var v = e.defaultProps; for (s in v) o[s] === void 0 && (o[s] = v[s]); } if (a || R) { var d = typeof e == "function" ? e.displayName || e.name || "Unknown" : e; a && Qe(o, d), R && er(o, d); } return rr(e, a, R, i, n, W.current, o); } } var X = p.ReactCurrentOwner, ye = p.ReactDebugCurrentFrame; function F(e) { if (e) { var r = e._owner, t = U(e.type, e._source, r ? r.type : null); ye.setExtraStackFrame(t); } else ye.setExtraStackFrame(null); } var Z; Z = !1; function Q(e) { return typeof e == "object" && e !== null && e.$$typeof === m; } function be() { { if (X.current) { var e = j(X.current.type); if (e) return ` Check the render method of \`` + e + "`."; } return ""; } } function nr(e) { { if (e !== void 0) { var r = e.fileName.replace(/^.*[\\\/]/, ""), t = e.lineNumber; return ` Check your code at ` + r + ":" + t + "."; } return ""; } } var _e = {}; function ar(e) { { var r = be(); if (!r) { var t = typeof e == "string" ? e : e.displayName || e.name; t && (r = ` Check the top-level render call using <` + t + ">."); } return r; } } function Re(e, r) { { if (!e._store || e._store.validated || e.key != null) return; e._store.validated = !0; var t = ar(r); if (_e[t]) return; _e[t] = !0; var n = ""; e && e._owner && e._owner !== X.current && (n = " It was passed a child from " + j(e._owner.type) + "."), F(e), f('Each child in a list should have a unique "key" prop.%s%s See https://reactjs.org/link/warning-keys for more information.', t, n), F(null); } } function Te(e, r) { { if (typeof e != "object") return; if (H(e)) for (var t = 0; t < e.length; t++) { var n = e[t]; Q(n) && Re(n, r); } else if (Q(e)) e._store && (e._store.validated = !0); else if (e) { var i = A(e); if (typeof i == "function" && i !== e.entries) for (var s = i.call(e), o; !(o = s.next()).done; ) Q(o.value) && Re(o.value, r); } } } function or(e) { { var r = e.type; if (r == null || typeof r == "string") return; var t; if (typeof r == "function") t = r.propTypes; else if (typeof r == "object" && (r.$$typeof === g || r.$$typeof === w)) t = r.propTypes; else return; if (t) { var n = j(r); qe(t, e.props, "prop", n, e); } else if (r.PropTypes !== void 0 && !Z) { Z = !0; var i = j(r); f("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?", i || "Unknown"); } typeof r.getDefaultProps == "function" && !r.getDefaultProps.isReactClassApproved && f("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead."); } } function ir(e) { { for (var r = Object.keys(e.props), t = 0; t < r.length; t++) { var n = r[t]; if (n !== "children" && n !== "key") { F(e), f("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.", n), F(null); break; } } e.ref !== null && (F(e), f("Invalid attribute `ref` supplied to `React.Fragment`."), F(null)); } } function Se(e, r, t, n, i, s) { { var o = Le(e); if (!o) { var a = ""; (e === void 0 || typeof e == "object" && e !== null && Object.keys(e).length === 0) && (a += " You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports."); var R = nr(i); R ? a += R : a += be(); var v; e === null ? v = "null" : H(e) ? v = "array" : e !== void 0 && e.$$typeof === m ? (v = "<" + (j(e.type) || "Unknown") + " />", a = " Did you accidentally export a JSX literal instead of a component?") : v = typeof e, f("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s", v, a); } var d = tr(e, r, t, i, s); if (d == null) return d; if (o) { var S = r.children; if (S !== void 0) if (n) if (H(S)) { for (var I = 0; I < S.length; I++) Te(S[I], e); Object.freeze && Object.freeze(S); } else f("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead."); else Te(S, e); } return e === E ? ir(d) : or(d), d; } } function ur(e, r, t) { return Se(e, r, t, !0); } function sr(e, r, t) { return Se(e, r, t, !1); } var lr = sr, cr = ur; N.Fragment = E, N.jsx = lr, N.jsxs = cr; }()), N; } (function(y) { process.env.NODE_ENV === "production" ? y.exports = dr() : y.exports = pr(); })(re); const je = re.exports.jsx, xe = re.exports.jsxs, gr = "_container_y1zdu_1", mr = "_sugStyle_y1zdu_9", ee = { container: gr, sugStyle: mr }; function hr(y, m) { var h = m.selectionStart, E = m.selectionEnd, T = m.value, c = T.substring(0, h), O = T.substring(E, T.length), C = c + y + O; m.value = C, m.selectionStart = m.selectionEnd = h + y.length; } var yr = function(y) { var m = y.children, h = y.suggestion, E = y.onChange, T = y.sugStyle, c = fr(null), O = we(), C = we(); vr(function() { var l = document.querySelectorAll("textarea"); return c.current = null, setTimeout(function() { for (var u = 0; u < l.length; u++) l[u].addEventListener("focus", g(l[u]), !0), l[u].addEventListener("blur", P, !0); }, 200), function() { c.current = null; for (var u = 0; u < l.length; u++) l[u].removeEventListener("focus", g(l[u]), !0), l[u].removeEventListener("blur", P, !0); }; }, []); var g = function(l) { return function(u) { c.current = l, console.log("\u9009\u4E2D\u8BBE\u7F6E$textarea", c.current); }; }, P = function(l) { c.current = null; }, b = function(l) { var u, _, k; console.log("\u6309\u952E\u4E86", l.key); var A = document.getElementById(O), p = window.getSelection(), f = p == null ? void 0 : p.getRangeAt(0); A && ((u = A.parentNode) === null || u === void 0 || u.removeChild(A)), l.key == "Tab" ? h && (console.log("$textarea.current:", c.current), c.current ? hr(h, c.current) : w(h), l.preventDefault()) : E && (c.current ? E(c.current.value) : ((_ = p == null ? void 0 : p.focusNode) === null || _ === void 0 ? void 0 : _.nodeValue) && f && E((k = p == null ? void 0 : p.focusNode) === null || k === void 0 ? void 0 : k.nodeValue)); }, w = function(l) { var u = null, _; if (window.getSelection && (u = window.getSelection(), (u == null ? void 0 : u.getRangeAt) && u.rangeCount)) { _ = u.getRangeAt(0), _.deleteContents(); var k = document.createElement("div"); k.innerHTML = l; for (var A = document.createDocumentFragment(), p = void 0, f = void 0; p = k.firstChild; ) f = A.appendChild(p); _.insertNode(A), f && (_ = _.cloneRange(), _.setStartAfter(f), _.collapse(!0), u.removeAllRanges(), u.addRange(_)); } }; return xe("div", Y({ className: ee.container }, { children: [h && xe("div", Y({ className: T || ee.sugStyle }, { children: [je("span", { children: " suggestion: " }), h] })), je("div", Y({ className: ee.children, id: C, onKeyDown: b }, { children: m }))] })); }; export { yr as SmartCompose };