UNPKG

react-es-drager

Version:

A draggable, resizable, rotatable component based on react

1,272 lines (1,269 loc) 43.5 kB
import Oe, { useRef as Te, useEffect as ke, useState as qe, useMemo as Ke, useCallback as Pt } from "react"; var ot = { exports: {} }, je = {}; /** * @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 vt; function At() { if (vt) return je; vt = 1; var r = Oe, n = Symbol.for("react.element"), t = Symbol.for("react.fragment"), o = Object.prototype.hasOwnProperty, a = r.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, x = { key: !0, ref: !0, __self: !0, __source: !0 }; function g(f, u, p) { var y, c = {}, k = null, j = null; p !== void 0 && (k = "" + p), u.key !== void 0 && (k = "" + u.key), u.ref !== void 0 && (j = u.ref); for (y in u) o.call(u, y) && !x.hasOwnProperty(y) && (c[y] = u[y]); if (f && f.defaultProps) for (y in u = f.defaultProps, u) c[y] === void 0 && (c[y] = u[y]); return { $$typeof: n, type: f, key: k, ref: j, props: c, _owner: a.current }; } return je.Fragment = t, je.jsx = g, je.jsxs = g, je; } var Me = {}; /** * @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 pt; function jt() { return pt || (pt = 1, process.env.NODE_ENV !== "production" && function() { var r = Oe, n = Symbol.for("react.element"), t = Symbol.for("react.portal"), o = Symbol.for("react.fragment"), a = Symbol.for("react.strict_mode"), x = Symbol.for("react.profiler"), g = Symbol.for("react.provider"), f = Symbol.for("react.context"), u = Symbol.for("react.forward_ref"), p = Symbol.for("react.suspense"), y = Symbol.for("react.suspense_list"), c = Symbol.for("react.memo"), k = Symbol.for("react.lazy"), j = Symbol.for("react.offscreen"), _ = Symbol.iterator, w = "@@iterator"; function R(e) { if (e === null || typeof e != "object") return null; var i = _ && e[_] || e[w]; return typeof i == "function" ? i : null; } var D = r.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED; function b(e) { { for (var i = arguments.length, s = new Array(i > 1 ? i - 1 : 0), h = 1; h < i; h++) s[h - 1] = arguments[h]; M("error", e, s); } } function M(e, i, s) { { var h = D.ReactDebugCurrentFrame, T = h.getStackAddendum(); T !== "" && (i += "%s", s = s.concat([T])); var P = s.map(function(E) { return String(E); }); P.unshift("Warning: " + i), Function.prototype.apply.call(console[e], console, P); } } var ne = !1, H = !1, O = !1, S = !1, K = !1, C; C = Symbol.for("react.module.reference"); function he(e) { return !!(typeof e == "string" || typeof e == "function" || e === o || e === x || K || e === a || e === p || e === y || S || e === j || ne || H || O || typeof e == "object" && e !== null && (e.$$typeof === k || e.$$typeof === c || e.$$typeof === g || e.$$typeof === f || e.$$typeof === u || // This needs to include all possible module reference object // types supported by any Flight configuration anywhere since // we don't know which Flight build this will end up being used // with. e.$$typeof === C || e.getModuleId !== void 0)); } function ye(e, i, s) { var h = e.displayName; if (h) return h; var T = i.displayName || i.name || ""; return T !== "" ? s + "(" + T + ")" : s; } function me(e) { return e.displayName || "Context"; } function N(e) { if (e == null) return null; if (typeof e.tag == "number" && b("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 o: return "Fragment"; case t: return "Portal"; case x: return "Profiler"; case a: return "StrictMode"; case p: return "Suspense"; case y: return "SuspenseList"; } if (typeof e == "object") switch (e.$$typeof) { case f: var i = e; return me(i) + ".Consumer"; case g: var s = e; return me(s._context) + ".Provider"; case u: return ye(e, e.render, "ForwardRef"); case c: var h = e.displayName || null; return h !== null ? h : N(e.type) || "Memo"; case k: { var T = e, P = T._payload, E = T._init; try { return N(E(P)); } catch { return null; } } } return null; } var ae = Object.assign, ue = 0, we, be, d, v, V, $, Z; function ee() { } ee.__reactDisabledLog = !0; function ge() { { if (ue === 0) { we = console.log, be = console.info, d = console.warn, v = console.error, V = console.group, $ = console.groupCollapsed, Z = console.groupEnd; var e = { configurable: !0, enumerable: !0, value: ee, writable: !0 }; Object.defineProperties(console, { info: e, log: e, warn: e, error: e, group: e, groupCollapsed: e, groupEnd: e }); } ue++; } } function ve() { { if (ue--, ue === 0) { var e = { configurable: !0, enumerable: !0, writable: !0 }; Object.defineProperties(console, { log: ae({}, e, { value: we }), info: ae({}, e, { value: be }), warn: ae({}, e, { value: d }), error: ae({}, e, { value: v }), group: ae({}, e, { value: V }), groupCollapsed: ae({}, e, { value: $ }), groupEnd: ae({}, e, { value: Z }) }); } ue < 0 && b("disabledDepth fell below zero. This is a bug in React. Please file an issue."); } } var Q = D.ReactCurrentDispatcher, l; function A(e, i, s) { { if (l === void 0) try { throw Error(); } catch (T) { var h = T.stack.trim().match(/\n( *(at )?)/); l = h && h[1] || ""; } return ` ` + l + e; } } var L = !1, U; { var le = typeof WeakMap == "function" ? WeakMap : Map; U = new le(); } function B(e, i) { if (!e || L) return ""; { var s = U.get(e); if (s !== void 0) return s; } var h; L = !0; var T = Error.prepareStackTrace; Error.prepareStackTrace = void 0; var P; P = Q.current, Q.current = null, ge(); try { if (i) { var E = function() { throw Error(); }; if (Object.defineProperty(E.prototype, "props", { set: function() { throw Error(); } }), typeof Reflect == "object" && Reflect.construct) { try { Reflect.construct(E, []); } catch (re) { h = re; } Reflect.construct(e, [], E); } else { try { E.call(); } catch (re) { h = re; } e.call(E.prototype); } } else { try { throw Error(); } catch (re) { h = re; } e(); } } catch (re) { if (re && h && typeof re.stack == "string") { for (var m = re.stack.split(` `), W = h.stack.split(` `), Y = m.length - 1, F = W.length - 1; Y >= 1 && F >= 0 && m[Y] !== W[F]; ) F--; for (; Y >= 1 && F >= 0; Y--, F--) if (m[Y] !== W[F]) { if (Y !== 1 || F !== 1) do if (Y--, F--, F < 0 || m[Y] !== W[F]) { var se = ` ` + m[Y].replace(" at new ", " at "); return e.displayName && se.includes("<anonymous>") && (se = se.replace("<anonymous>", e.displayName)), typeof e == "function" && U.set(e, se), se; } while (Y >= 1 && F >= 0); break; } } } finally { L = !1, Q.current = P, ve(), Error.prepareStackTrace = T; } var De = e ? e.displayName || e.name : "", _e = De ? A(De) : ""; return typeof e == "function" && U.set(e, _e), _e; } function q(e, i, s) { return B(e, !1); } function oe(e) { var i = e.prototype; return !!(i && i.isReactComponent); } function G(e, i, s) { if (e == null) return ""; if (typeof e == "function") return B(e, oe(e)); if (typeof e == "string") return A(e); switch (e) { case p: return A("Suspense"); case y: return A("SuspenseList"); } if (typeof e == "object") switch (e.$$typeof) { case u: return q(e.render); case c: return G(e.type, i, s); case k: { var h = e, T = h._payload, P = h._init; try { return G(P(T), i, s); } catch { } } } return ""; } var J = Object.prototype.hasOwnProperty, te = {}, fe = D.ReactDebugCurrentFrame; function z(e) { if (e) { var i = e._owner, s = G(e.type, e._source, i ? i.type : null); fe.setExtraStackFrame(s); } else fe.setExtraStackFrame(null); } function Ye(e, i, s, h, T) { { var P = Function.call.bind(J); for (var E in e) if (P(e, E)) { var m = void 0; try { if (typeof e[E] != "function") { var W = Error((h || "React class") + ": " + s + " type `" + E + "` is invalid; it must be a function, usually from the `prop-types` package, but received `" + typeof e[E] + "`.This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`."); throw W.name = "Invariant Violation", W; } m = e[E](i, E, h, s, null, "SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"); } catch (Y) { m = Y; } m && !(m instanceof Error) && (z(T), b("%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).", h || "React class", s, E, typeof m), z(null)), m instanceof Error && !(m.message in te) && (te[m.message] = !0, z(T), b("Failed %s type: %s", s, m.message), z(null)); } } } var ze = Array.isArray; function Ee(e) { return ze(e); } function ie(e) { { var i = typeof Symbol == "function" && Symbol.toStringTag, s = i && e[Symbol.toStringTag] || e.constructor.name || "Object"; return s; } } function ct(e) { try { return Pe(e), !1; } catch { return !0; } } function Pe(e) { return "" + e; } function Fe(e) { if (ct(e)) return b("The provided key is an unsupported type %s. This value must be coerced to a string before before using it here.", ie(e)), Pe(e); } var xe = D.ReactCurrentOwner, Se = { key: !0, ref: !0, __self: !0, __source: !0 }, Re, Xe, Ce; Ce = {}; function Je(e) { if (J.call(e, "ref")) { var i = Object.getOwnPropertyDescriptor(e, "ref").get; if (i && i.isReactWarning) return !1; } return e.ref !== void 0; } function Ie(e) { if (J.call(e, "key")) { var i = Object.getOwnPropertyDescriptor(e, "key").get; if (i && i.isReactWarning) return !1; } return e.key !== void 0; } function Ze(e, i) { if (typeof e.ref == "string" && xe.current && i && xe.current.stateNode !== i) { var s = N(xe.current.type); Ce[s] || (b('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', N(xe.current.type), e.ref), Ce[s] = !0); } } function Qe(e, i) { { var s = function() { Re || (Re = !0, b("%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)", i)); }; s.isReactWarning = !0, Object.defineProperty(e, "key", { get: s, configurable: !0 }); } } function $e(e, i) { { var s = function() { Xe || (Xe = !0, b("%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)", i)); }; s.isReactWarning = !0, Object.defineProperty(e, "ref", { get: s, configurable: !0 }); } } var Ue = function(e, i, s, h, T, P, E) { var m = { // This tag allows us to uniquely identify this as a React Element $$typeof: n, // Built-in properties that belong on the element type: e, key: i, ref: s, props: E, // Record the component responsible for creating this element. _owner: P }; return m._store = {}, Object.defineProperty(m._store, "validated", { configurable: !1, enumerable: !1, writable: !0, value: !1 }), Object.defineProperty(m, "_self", { configurable: !1, enumerable: !1, writable: !1, value: h }), Object.defineProperty(m, "_source", { configurable: !1, enumerable: !1, writable: !1, value: T }), Object.freeze && (Object.freeze(m.props), Object.freeze(m)), m; }; function Ne(e, i, s, h, T) { { var P, E = {}, m = null, W = null; s !== void 0 && (Fe(s), m = "" + s), Ie(i) && (Fe(i.key), m = "" + i.key), Je(i) && (W = i.ref, Ze(i, T)); for (P in i) J.call(i, P) && !Se.hasOwnProperty(P) && (E[P] = i[P]); if (e && e.defaultProps) { var Y = e.defaultProps; for (P in Y) E[P] === void 0 && (E[P] = Y[P]); } if (m || W) { var F = typeof e == "function" ? e.displayName || e.name || "Unknown" : e; m && Qe(E, F), W && $e(E, F); } return Ue(e, m, W, T, h, xe.current, E); } } var Ae = D.ReactCurrentOwner, Be = D.ReactDebugCurrentFrame; function pe(e) { if (e) { var i = e._owner, s = G(e.type, e._source, i ? i.type : null); Be.setExtraStackFrame(s); } else Be.setExtraStackFrame(null); } var We; We = !1; function He(e) { return typeof e == "object" && e !== null && e.$$typeof === n; } function ut() { { if (Ae.current) { var e = N(Ae.current.type); if (e) return ` Check the render method of \`` + e + "`."; } return ""; } } function bt(e) { return ""; } var lt = {}; function Et(e) { { var i = ut(); if (!i) { var s = typeof e == "string" ? e : e.displayName || e.name; s && (i = ` Check the top-level render call using <` + s + ">."); } return i; } } function ft(e, i) { { if (!e._store || e._store.validated || e.key != null) return; e._store.validated = !0; var s = Et(i); if (lt[s]) return; lt[s] = !0; var h = ""; e && e._owner && e._owner !== Ae.current && (h = " It was passed a child from " + N(e._owner.type) + "."), pe(e), b('Each child in a list should have a unique "key" prop.%s%s See https://reactjs.org/link/warning-keys for more information.', s, h), pe(null); } } function dt(e, i) { { if (typeof e != "object") return; if (Ee(e)) for (var s = 0; s < e.length; s++) { var h = e[s]; He(h) && ft(h, i); } else if (He(e)) e._store && (e._store.validated = !0); else if (e) { var T = R(e); if (typeof T == "function" && T !== e.entries) for (var P = T.call(e), E; !(E = P.next()).done; ) He(E.value) && ft(E.value, i); } } } function xt(e) { { var i = e.type; if (i == null || typeof i == "string") return; var s; if (typeof i == "function") s = i.propTypes; else if (typeof i == "object" && (i.$$typeof === u || // Note: Memo only checks outer props here. // Inner props are checked in the reconciler. i.$$typeof === c)) s = i.propTypes; else return; if (s) { var h = N(i); Ye(s, e.props, "prop", h, e); } else if (i.PropTypes !== void 0 && !We) { We = !0; var T = N(i); b("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?", T || "Unknown"); } typeof i.getDefaultProps == "function" && !i.getDefaultProps.isReactClassApproved && b("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead."); } } function Rt(e) { { for (var i = Object.keys(e.props), s = 0; s < i.length; s++) { var h = i[s]; if (h !== "children" && h !== "key") { pe(e), b("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.", h), pe(null); break; } } e.ref !== null && (pe(e), b("Invalid attribute `ref` supplied to `React.Fragment`."), pe(null)); } } var ht = {}; function gt(e, i, s, h, T, P) { { var E = he(e); if (!E) { var m = ""; (e === void 0 || typeof e == "object" && e !== null && Object.keys(e).length === 0) && (m += " 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 W = bt(); W ? m += W : m += ut(); var Y; e === null ? Y = "null" : Ee(e) ? Y = "array" : e !== void 0 && e.$$typeof === n ? (Y = "<" + (N(e.type) || "Unknown") + " />", m = " Did you accidentally export a JSX literal instead of a component?") : Y = typeof e, b("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s", Y, m); } var F = Ne(e, i, s, T, P); if (F == null) return F; if (E) { var se = i.children; if (se !== void 0) if (h) if (Ee(se)) { for (var De = 0; De < se.length; De++) dt(se[De], e); Object.freeze && Object.freeze(se); } else b("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 dt(se, e); } if (J.call(i, "key")) { var _e = N(e), re = Object.keys(i).filter(function(Dt) { return Dt !== "key"; }), et = re.length > 0 ? "{key: someKey, " + re.join(": ..., ") + ": ...}" : "{key: someKey}"; if (!ht[_e + et]) { var Ct = re.length > 0 ? "{" + re.join(": ..., ") + ": ...}" : "{}"; b(`A props object containing a "key" prop is being spread into JSX: let props = %s; <%s {...props} /> React keys must be passed directly to JSX without using spread: let props = %s; <%s key={someKey} {...props} />`, et, _e, Ct, _e), ht[_e + et] = !0; } } return e === o ? Rt(F) : xt(F), F; } } function _t(e, i, s) { return gt(e, i, s, !0); } function Tt(e, i, s) { return gt(e, i, s, !1); } var kt = Tt, St = _t; Me.Fragment = o, Me.jsx = kt, Me.jsxs = St; }()), Me; } process.env.NODE_ENV === "production" ? ot.exports = At() : ot.exports = jt(); var ce = ot.exports; function st(r, n) { const t = (o) => { n && n(o), document.removeEventListener("mousemove", r), document.removeEventListener("mouseup", t), document.removeEventListener("mouseleave", t), document.removeEventListener("touchmove", r), document.removeEventListener("touchend", t); }; document.addEventListener("mousemove", r), document.addEventListener("mouseup", t), document.addEventListener("mouseleave", t), document.addEventListener("touchmove", r), document.addEventListener("touchend", t); } function Le(r) { let n = 0, t = 0; if (Mt(r)) { const o = r.targetTouches[0]; n = o.pageX, t = o.pageY; } else n = r.clientX, t = r.clientY; return { clientX: n, clientY: t }; } function Mt(r) { const n = Object.prototype.toString.call(r); return n.substring(8, n.length - 1) === "TouchEvent"; } const Ve = (r = 0) => parseInt(r + "") + "px", tt = { n: "top", s: "bottom", e: "right", w: "left", ne: "top-right", nw: "top-left", se: "bottom-right", sw: "bottom-left" }, it = ["n", "ne", "e", "se", "s", "sw", "w", "nw"], Ot = { n: 0, ne: 1, e: 2, se: 3, s: 4, sw: 5, w: 6, nw: 7 }, Lt = { 0: 0, 1: 1, 2: 2, 3: 2, 4: 3, 5: 4, 6: 4, 7: 5, 8: 6, 9: 6, 10: 7, 11: 8 }, Yt = (r, n) => { const t = Lt[Math.floor(r / 30)], a = (Ot[n] + t) % 8; return it[a]; }, yt = (r = 0, n) => { let t = []; for (let o = 0; o < it.length; o++) { const a = it[o], [x, g] = tt[a].split("-"), f = Yt(r, a), u = { [x]: "0%", cursor: f + "-resize", side: tt[a] }; if (g) u[g] = "0%"; else { const p = ["top", "bottom"].includes(x) ? "left" : "top"; u[p] = "50%"; } n ? n.includes(tt[a]) && t.push(u) : t.push(u); } return t; }, at = (r) => r * Math.PI / 180, zt = (r, n) => Math.sqrt(r * r + n * n), X = (r) => Math.cos(at(r)), I = (r) => Math.sin(at(r)), Ft = (r, n, t, o, a, x, g) => { let { width: f, height: u, centerX: p, centerY: y, rotateAngle: c } = n; const k = f < 0 ? -1 : 1, j = u < 0 ? -1 : 1; if (f = Math.abs(f), u = Math.abs(u), ["top-left", "top-right", "bottom-left", "bottom-right"].includes(r)) { r === "top-right" ? o = -o : r === "bottom-left" ? t = -t : r === "top-left" && (t = -t, o = -o); const _ = nt(f, t, x); f = _.width, t = _.deltaW; const w = rt(u, o, g); u = w.height, o = w.deltaH, a && (o = t / a, u = f / a); } switch (r) { case "right": { const _ = nt(f, t, x); f = _.width, t = _.deltaW, a ? (o = t / a, u = f / a, p += t / 2 * X(c) - o / 2 * I(c), y += t / 2 * I(c) + o / 2 * X(c)) : (p += t / 2 * X(c), y += t / 2 * I(c)); break; } case "top-right": { p += t / 2 * X(c) + o / 2 * I(c), y += t / 2 * I(c) - o / 2 * X(c); break; } case "bottom-right": { p += t / 2 * X(c) - o / 2 * I(c), y += t / 2 * I(c) + o / 2 * X(c); break; } case "bottom": { const _ = rt(u, o, g); u = _.height, o = _.deltaH, a ? (t = o * a, f = u * a, p += t / 2 * X(c) - o / 2 * I(c), y += t / 2 * I(c) + o / 2 * X(c)) : (p -= o / 2 * I(c), y += o / 2 * X(c)); break; } case "bottom-left": { p -= t / 2 * X(c) + o / 2 * I(c), y -= t / 2 * I(c) - o / 2 * X(c); break; } case "left": { t = -t; const _ = nt(f, t, x); f = _.width, t = _.deltaW, a ? (u = f / a, o = t / a, p -= t / 2 * X(c) + o / 2 * I(c), y -= t / 2 * I(c) - o / 2 * X(c)) : (p -= t / 2 * X(c), y -= t / 2 * I(c)); break; } case "top-left": { p -= t / 2 * X(c) - o / 2 * I(c), y -= t / 2 * I(c) + o / 2 * X(c); break; } case "top": { o = -o; const _ = rt(u, o, g); u = _.height, o = _.deltaH, a ? (f = u * a, t = o * a, p += t / 2 * X(c) + o / 2 * I(c), y += t / 2 * I(c) - o / 2 * X(c)) : (p += o / 2 * I(c), y -= o / 2 * X(c)); break; } } return { position: { centerX: p, centerY: y }, size: { width: f * k, height: u * j } }; }, rt = (r, n, t) => { const o = r + n; return o > t ? r = o : (n = t - r, r = t), { height: r, deltaH: n }; }, nt = (r, n, t) => { const o = r + n; return o > t ? r = o : (n = t - r, r = t), { width: r, deltaW: n }; }, Xt = ({ centerX: r, centerY: n, width: t, height: o, angle: a }) => ({ top: n - o / 2, left: r - t / 2, width: t, height: o, angle: a }), It = (r, n, t) => { const { width: o, height: a } = r; return { width: Math.abs(o), height: Math.abs(a), left: n - Math.abs(o) / 2, top: t - Math.abs(a) / 2 }; }; function Ge(r, n) { const t = Math.abs(r) % n, o = r > 0 ? n : -n; let a = 0; return t > n / 2 ? a = o * Math.ceil(Math.abs(r) / n) : a = o * Math.floor(Math.abs(r) / n), a; } function $t(r, n, t) { if (!r || !n) return !1; const o = de(r, t), a = de(n, t); return o.left < a.left + a.width && o.left + o.width > a.left && o.top < a.top + a.height && o.top + o.height > a.top; } const de = (r, n) => { var t = r.getBoundingClientRect(); return { ...t, left: t.left / n, top: t.top / n, right: t.right / n, bottom: t.bottom / n, width: t.width / n, height: t.height / n }; }, Ut = ({ dragData: r, element: n, children: t, onRotate: o, onRotateStart: a, onRotateEnd: x }) => { const g = Te(null), f = (u) => { const p = u.nativeEvent; if (!n) { console.warn("[es-drager] rotate component needs drag element property"); return; } p.stopPropagation(); const { width: y, height: c, left: k, top: j } = n.getBoundingClientRect(), _ = k + y / 2, w = j + c / 2; a && a(r); let R = 0; st((M) => { const { clientX: ne, clientY: H } = Le(M), O = _ - ne, S = w - H; R = (Math.atan2(S, O) * 180 / Math.PI - 90 + 360) % 360, o(R); }, () => { x(R); }); }; return /* @__PURE__ */ ce.jsx( "div", { ref: g, className: "es-drager-rotate", onMouseDown: f, onTouchStart: f, children: t || /* @__PURE__ */ ce.jsx("div", { className: "es-drager-rotate-handle", children: /* @__PURE__ */ ce.jsx("svg", { viewBox: "0 0 1024 1024", xmlns: "http://www.w3.org/2000/svg", children: /* @__PURE__ */ ce.jsx( "path", { fill: "currentColor", d: "M784.512 230.272v-50.56a32 32 0 1 1 64 0v149.056a32 32 0 0 1-32 32H667.52a32 32 0 1 1 0-64h92.992A320 320 0 1 0 524.8 833.152a320 320 0 0 0 320-320h64a384 384 0 0 1-384 384 384 384 0 0 1-384-384 384 384 0 0 1 643.712-282.88z" } ) }) }) } ); }, mt = (r) => typeof r == "function"; function Nt(r, n) { const t = n.color || "#3a7afe", o = n.scaleRatio || 1, a = Te(null), x = Te(null), g = Te(null), f = Te(null); ke(() => { r.current && (g.current = r.current.offsetParent || document.body, f.current = de(g.current, o)); }, [r, n.scaleRatio]); const u = Te({ x: null, y: null }), p = () => { n.markline && !mt(n.markline) && (a.current || (a.current = document.querySelector(".es-drager-markline-x") || wt("x", g.current, t)), x.current || (x.current = document.querySelector(".es-drager-markline-y") || wt("y", g.current, t))); }, y = (w = {}) => { if (n.markline) { if (mt(n.markline)) return n.markline(w); w.left === null ? a.current && (a.current.style.display = "none") : a.current && (a.current.style.left = `${w.left}px`, a.current.style.backgroundColor = t, a.current.style.display = "block"), w.top === null ? x.current && (x.current.style.display = "none") : x.current && (x.current.style.top = `${w.top}px`, x.current.style.backgroundColor = t, x.current.style.display = "block"); } }, c = () => { debugger; const w = de(r.current, o), R = Array.from(document.querySelectorAll(".es-drager")); if (n.extraLines) { const b = n.extraLines(w) || []; R.push(...b); } const D = []; for (let b = 0; b < R.length; b++) { const M = R[b]; M.nodeType === 1 ? M !== r.current && D.push(de(M, o)) : M.showTop || M.showTop === 0 || M.showLeft || M.showLeft; } u.current = Bt(D, w); }, k = () => { var b, M, ne, H; const w = { top: null, left: null, diffX: 0, diffY: 0 }, R = de(r.current, o), D = n.snapThreshold ?? 10; if ((b = u.current) != null && b.y) for (let O = 0; O < u.current.y.length; O++) { const { top: S, showTop: K } = u.current.y[O]; if (Math.abs(S - R.top) < D) { w.diffY = S - R.top, w.top = K - (((M = f.current) == null ? void 0 : M.top) ?? 0); break; } } if ((ne = u.current) != null && ne.x) for (let O = 0; O < u.current.x.length; O++) { const { left: S, showLeft: K } = u.current.x[O]; if (Math.abs(S - R.left) < D) { w.diffX = S - R.left, w.left = K - (((H = f.current) == null ? void 0 : H.left) ?? 0); break; } } return y(w), w; }, j = () => { y({ left: null, top: null }); }, _ = (w) => { if (!(!n.snap && !n.markline)) switch (w) { case "drag-start": c(); break; case "drag": return k(); case "drag-end": j(); break; } }; return ke(() => { p(); }, []), { marklineEmit: _ }; } function wt(r = "x", n, t) { const o = document.createElement("div"); return o.classList.add(`es-drager-markline-${r}`), o.style.position = "absolute", o.style.top = "0px", o.style.left = "0px", o.style.zIndex = "9999", o.style.backgroundColor = t, o.style.display = "none", r === "x" ? (o.style.height = "100%", o.style.width = "1px") : (o.style.height = "1px", o.style.width = "100%"), n && n.appendChild(o), o; } function Bt(r, n) { const t = { x: [], y: [] }, { width: o = 0, height: a = 0 } = n; return r.forEach((x) => { const { top: g, left: f, width: u, height: p } = x; t.y.push({ showTop: g, top: g }), t.y.push({ showTop: g, top: g - a }), t.y.push({ showTop: g + p / 2, top: g + p / 2 - a / 2 }), t.y.push({ showTop: g + p, top: g + p }), t.y.push({ showTop: g + p, top: g + p - a }), t.x.push({ showLeft: f, left: f }), t.x.push({ showLeft: f + u, left: f + u }), t.x.push({ showLeft: f + u / 2, left: f + u / 2 - o / 2 }), t.x.push({ showLeft: f + u, left: f + u - o }), t.x.push({ showLeft: f, left: f - o }); }), t; } function Kt(r, n, t, o, { getBoundary: a, fixBoundary: x, checkDragerCollision: g }) { let f = 0, u = 0; return { handleKeyDown: (c) => { let { left: k, top: j } = n; f || (f = k), u || (u = j); const _ = r.gridX ?? 50, w = r.gridY ?? 50; if (["ArrowRight", "ArrowLeft"].includes(c.key)) { const R = c.key === "ArrowRight"; let D = R ? 1 : -1; r.snapToGrid && (D = R ? _ : -_), k = k + D; } else if (["ArrowUp", "ArrowDown"].includes(c.key)) { const R = c.key === "ArrowDown"; let D = R ? 1 : -1; r.snapToGrid && (D = R ? w : -w), j = j + D; } if (r.boundary) { const [R, D, b, M] = a(); [k, j] = x(k, j, R, D, b, M); } !r.size && t((R) => ({ ...R, left: k, top: j })), o("drag", { ...n, left: k, top: j }); }, handleKeyUp: (c) => { ["ArrowRight", "ArrowLeft", "ArrowUp", "ArrowDown"].includes(c.key) && r.checkCollision && g() && (!r.size && t((k) => ({ ...k, left: f, top: u })), o("drag-end", { ...n, left: f, top: u })), f = 0, u = 0; } }; } const Vt = { width: 100, height: 100, left: 0, top: 0, angle: 0 }; function qt(r, n) { var be; const { scaleRatio: t = 1, onDragStart: o, onDrag: a, onDragEnd: x, onChange: g, onResize: f, onResizeStart: u, onResizeEnd: p, onRotate: y, onRotateStart: c, onRotateEnd: k, onFocus: j, onBlur: _ } = n, w = (d, v) => { g == null || g(v), d === "drag" && (a == null || a(v)), d === "drag-start" && (o == null || o(v)), d === "drag-end" && (x == null || x(v)), d === "resize" && (f == null || f(v)), d === "rotate" && (y == null || y(v)), d === "resize-start" && (u == null || u(v)), d === "resize-end" && (p == null || p(v)), d === "rotate-start" && (c == null || c(v)), d === "rotate-end" && (k == null || k(v)); }, R = n.size || n.defaultSize || Vt, [D, b] = qe(!1), [M, ne] = qe(n.selected || !1); ke(() => { ne(n.selected || !1); }, [n.selected]); const [H, O] = qe({ width: R.width || 0, height: R.height || 0, left: R.left || 0, top: R.top || 0, angle: R.angle || 0 }), S = { ...n.size || H, angle: ((be = n.size) == null ? void 0 : be.angle) ?? H.angle // 确保 angle 始终存在 }; ke(() => { }, [D]); const { marklineEmit: K } = Nt(r, n), C = /* @__PURE__ */ new Set(); function he(d) { if (C.add(d.button), n.disabled) return; b(!0), ne(!0); let { clientX: v, clientY: V } = Le(d); const { left: $, top: Z } = S; let ee = 0, ge = 0, ve = 0, Q = 0; n.boundary && ([ee, ge, ve, Q] = ye()), K("drag-start"), w("drag-start", S); let l = S; st((L) => { if (C.size > 1) return; const { clientX: U, clientY: le } = Le(L); let B = (U - v) / t + $, q = (le - V) / t + Z; if (n.snapToGrid) { const { left: oe, top: G } = S, J = B - oe, te = q - G; B = oe + Ge(J, n.gridX || 50), q = G + Ge(te, n.gridY || 50); } n.boundary && ([B, q] = me(B, q, ee, ge, ve, Q)), l = { ...S, left: B, top: q }, !n.size && O(l), w("drag", l); }, (L) => { n.checkCollision && N() && !n.size && O((le) => ({ ...le, top: Z, left: $ })), C.clear(), b(!1), K("drag-end"); }); } ke(() => { if (D) { const d = K("drag"); if (n.snap && d) { if (d.diffX) { const v = { ...S, left: S.left + ((d == null ? void 0 : d.diffX) || 0) }; !n.size && O(v), g == null || g(v); } if (d.diffY) { const v = { ...S, top: S.top + ((d == null ? void 0 : d.diffY) || 0) }; !n.size && O(v), g == null || g(v); } } } }, [n.size, S]); const ye = () => { var L; let d = 0, v = 0; const { left: V, top: $, height: Z, width: ee, angle: ge } = S, ve = ((L = r.current) == null ? void 0 : L.offsetParent) || document.body, Q = de(ve, t); if (ge) { const U = de(r.current, t); d = U.left - Math.floor(V - (U.width - ee) + Q.left), v = U.top - Math.floor($ - (U.height - Z) + Q.top); } const l = Q.width - ee, A = Q.height - Z; return [d, l - d, v, A - v, Q.width, Q.height]; }, me = (d, v, V, $, Z, ee) => (d = d < V ? V : d, d = d > $ ? $ : d, v = v < Z ? Z : v, v = v > ee ? ee : v, [d, v]), N = () => { var V; const d = ((V = r.current) == null ? void 0 : V.offsetParent) || document.body, v = Array.from(d.children).filter(($) => $ !== r.current && $.classList.contains("es-drager")); for (let $ = 0; $ < v.length; $++) { const Z = v[$]; if ($t(r.current, Z, t)) return !0; } return !1; }, ae = () => { ne(!1); }, { handleKeyDown: ue, handleKeyUp: we } = Kt( n, S, O, w, { getBoundary: ye, fixBoundary: me, checkDragerCollision: N } ); return ke(() => { if (r.current) { if (!S.width && !S.height) { const { width: d, height: v } = de(r.current, t); !n.size && O((V) => ({ ...V, width: d || 100, height: v || 100 })); } return r.current.addEventListener("mousedown", he), r.current.addEventListener("touchstart", he, { passive: !0 }), () => { var d, v; (d = r.current) == null || d.removeEventListener("mousedown", he), (v = r.current) == null || v.removeEventListener("touchstart", he); }; } }, [H, n.size]), ke(() => (M && (j && j(M), document.addEventListener("click", ae, { once: !0 }), n.disabledKeyEvent || (document.addEventListener("keydown", ue), document.addEventListener("keyup", we))), () => { _ && _(M), n.disabledKeyEvent || (document.removeEventListener("keydown", ue), document.removeEventListener("keyup", we)); }), [M]), { triggerEvent: w, isMousedown: D, selected: M, setSelected: ne, setDragData: O, currentDragData: S, getBoundary: ye, checkDragerCollision: N }; } const Jt = (r) => { const { type: n = "rect", disabled: t, border: o = !0, resizable: a = !0, rotatable: x, zIndex: g = 1, color: f = "#3a7afe", scaleRatio: u = 1, resizeList: p, minWidth: y = 1, minHeight: c = 1, equalProportion: k, maxWidth: j = 99999, maxHeight: _ = 9999, snapToGrid: w, gridX: R = 50, gridY: D = 50, boundary: b, children: M, checkCollision: ne } = r, H = Te(null), { selected: O, setDragData: S, triggerEvent: K, currentDragData: C, isMousedown: he, getBoundary: ye, checkDragerCollision: me } = qt(H, r), [N, ae] = qe(yt(0, p)), ue = Ke( () => x && !t && O, [a, t, O] ), we = Ke(() => a && !t, [a, t]), be = Ke(() => r.type !== "text" ? N : N.filter((l) => !["top", "bottom"].includes(l.side)), [r.type, N]), d = Ke(() => { var G, J, te, fe, z; const { width: l, height: A, left: L, top: U, angle: le } = C, B = { ...r.style || {} }; B.width = ((G = r.size) == null ? void 0 : G.width) ?? Ve(l); const q = ((J = r.size) == null ? void 0 : J.height) ?? A; q && (r.type === "text" ? B.fontSize = q + "px" : B.height = Ve(q)); let oe = [ `translateX(${Ve(((te = r.size) == null ? void 0 : te.left) ?? L)})`, `translateY(${Ve(((fe = r.size) == null ? void 0 : fe.top) ?? U)})`, `rotate(${((z = r.size) == null ? void 0 : z.angle) ?? le}deg)` ]; return { ...B, zIndex: g, transform: oe.join(" "), "--es-drager-color": f }; }, [C, r.size]), v = (l) => { ae(yt(l, p)), K("rotate-end", { ...C, angle: l }); }, V = Pt( (l, A) => { if (t) return; A.stopPropagation(); const { clientX: L, clientY: U } = Le(A), le = L, B = U, { width: q, height: oe, left: G, top: J } = C, te = G + q / 2, fe = J + oe / 2, z = { width: q, height: oe, centerX: te, centerY: fe, rotateAngle: C.angle }, Ye = l.side; K("resize-start", C); let ze = []; b && (ze = ye()); let Ee = r.aspectRatio; ["text", "image"].includes(n) && Ye.includes("-") && (Ee = z.width / z.height); let ie = null; st((Pe) => { const { clientX: Fe, clientY: xe } = Le(Pe); let Se = (Fe - le) / u, Re = (xe - B) / u; w && (Se = Ge(Se, R), Re = Ge(Re, D)); const Xe = Math.atan2(Re, Se), Ce = zt(Se, Re), Je = Pe.shiftKey, Ie = Xe - at(z.rotateAngle), Ze = Ce * Math.cos(Ie), Qe = Ce * Math.sin(Ie), $e = (k || Je) && !Ee ? z.width / z.height : Ee, { position: { centerX: Ue, centerY: Ne }, size: { width: Ae, height: Be } } = Ft( Ye, { ...z, rotateAngle: z.rotateAngle }, Ze, Qe, $e, y, c ), pe = Xt({ centerX: Ue, centerY: Ne, width: Ae, height: Be, angle: C.angle }); ie = { ...C, ...It(pe, Ue, Ne) }, j > 0 && (ie.width = Math.min(ie.width, j)), _ > 0 && (ie.height = Math.min(ie.height, _)), b && (ie = $(ie, ze, $e)), !r.size && S(ie), K("resize", ie); }, () => { ne && me() && !r.size && S({ ...C, width: q, height: oe, left: G, top: J }), ie && K("resize-end", ie); }); }, [C] ), $ = (l, A, L) => { const [U, le, B, q, oe, G] = A, J = l.left < U, te = l.left + l.width > oe, fe = l.top < B, z = l.top + l.height > G; return J && (l.left = U, l.width = C.width), fe && (l.top = B, l.height = C.height), (te || z) && (te && (l.left = C.left), z && (l.top = C.top), z || (l.width = oe - l.left), te || (l.height = G - l.top)), (z || fe) && L && (l.width = C.width, l.left = C.left), (te || J) && L && (l.height = C.height, l.top = C.top), l; }, [Z, ee, ge] = Oe.Children.toArray(M).reduce( (l, A) => { if (Oe.isValidElement(A)) { const L = A.props.slot; L === "rotate" ? l[2] = A : L === "resize" ? l[1] = A : l[0].push(A); } else l[0].push(A); return l; }, [[], null, null] ), ve = (l) => { !r.size && S({ ...C, angle: l }), K("rotate", { ...C, angle: l }); }, Q = r.tag || "div"; return /* @__PURE__ */ ce.jsxs( Q, { ref: H, className: [ r.className, "es-drager", `es-drager-${n}`, o ? "border" : "", O ? "selected" : "", t ? "disabled" : "", he ? "dragging" : "" ].join(" "), style: d, onClick: (l) => l.stopPropagation(), onMouseDown: (l) => l.stopPropagation(), children: [ Z.map((l, A) => /* @__PURE__ */ ce.jsx(Oe.Fragment, { children: l }, A)), we && /* @__PURE__ */ ce.jsx(ce.Fragment, { children: be.map((l, A) => /* @__PURE__ */ ce.jsx( "div", { className: "es-drager-dot", "data-side": l.side, style: { ...l }, onMouseDown: (L) => V(l, L), onTouchStart: (L) => V(l, L), children: ee || /* @__PURE__ */ ce.jsx("div", { className: "es-drager-dot-handle" }) }, A )) }), ue && /* @__PURE__ */ ce.jsx( Ut, { dragData: C, element: H.current, onRotate: ve, onRotateStart: (l) => K("rotate-start", l), onRotateEnd: v, children: ge } ) ] } ); }; export { Jt as default };