@chaomingd/hooks 0.0.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +23 -0
- package/dist/esm/index.d.ts +10 -0
- package/dist/esm/index.js +10 -0
- package/dist/esm/useDragAndDrop/index.d.ts +35 -0
- package/dist/esm/useDragAndDrop/index.js +193 -0
- package/dist/esm/useDragAndDrop/index.less +29 -0
- package/dist/esm/useEditModal/index.d.ts +56 -0
- package/dist/esm/useEditModal/index.js +166 -0
- package/dist/esm/useEffectWithTarget/index.d.ts +2 -0
- package/dist/esm/useEffectWithTarget/index.js +36 -0
- package/dist/esm/useLatestState/index.d.ts +8 -0
- package/dist/esm/useLatestState/index.js +36 -0
- package/dist/esm/useListLayout/index.d.ts +20 -0
- package/dist/esm/useListLayout/index.js +112 -0
- package/dist/esm/usePagination/constant.d.ts +2 -0
- package/dist/esm/usePagination/constant.js +2 -0
- package/dist/esm/usePagination/index.d.ts +14 -0
- package/dist/esm/usePagination/index.js +184 -0
- package/dist/esm/usePagination/type.d.ts +68 -0
- package/dist/esm/usePagination/type.js +1 -0
- package/dist/esm/usePagination/utils.d.ts +1 -0
- package/dist/esm/usePagination/utils.js +8 -0
- package/dist/esm/useResizer/index.d.ts +37 -0
- package/dist/esm/useResizer/index.js +173 -0
- package/dist/esm/useResponsive/index.d.ts +44 -0
- package/dist/esm/useResponsive/index.js +94 -0
- package/dist/esm/useSelectionList/index.d.ts +13 -0
- package/dist/esm/useSelectionList/index.js +49 -0
- package/dist/lib/index.d.ts +10 -0
- package/dist/lib/index.js +112 -0
- package/dist/lib/useDragAndDrop/index.d.ts +35 -0
- package/dist/lib/useDragAndDrop/index.js +191 -0
- package/dist/lib/useDragAndDrop/index.less +29 -0
- package/dist/lib/useEditModal/index.d.ts +56 -0
- package/dist/lib/useEditModal/index.js +160 -0
- package/dist/lib/useEffectWithTarget/index.d.ts +2 -0
- package/dist/lib/useEffectWithTarget/index.js +37 -0
- package/dist/lib/useLatestState/index.d.ts +8 -0
- package/dist/lib/useLatestState/index.js +29 -0
- package/dist/lib/useListLayout/index.d.ts +20 -0
- package/dist/lib/useListLayout/index.js +116 -0
- package/dist/lib/usePagination/constant.d.ts +2 -0
- package/dist/lib/usePagination/constant.js +8 -0
- package/dist/lib/usePagination/index.d.ts +14 -0
- package/dist/lib/usePagination/index.js +157 -0
- package/dist/lib/usePagination/type.d.ts +68 -0
- package/dist/lib/usePagination/type.js +5 -0
- package/dist/lib/usePagination/utils.d.ts +1 -0
- package/dist/lib/usePagination/utils.js +14 -0
- package/dist/lib/useResizer/index.d.ts +37 -0
- package/dist/lib/useResizer/index.js +168 -0
- package/dist/lib/useResponsive/index.d.ts +44 -0
- package/dist/lib/useResponsive/index.js +88 -0
- package/dist/lib/useSelectionList/index.d.ts +13 -0
- package/dist/lib/useSelectionList/index.js +45 -0
- package/package.json +39 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { useLatest, useUnmount } from 'ahooks';
|
|
2
|
+
import { useEffectWithTarget } from "../useEffectWithTarget";
|
|
3
|
+
import { useRef } from 'react';
|
|
4
|
+
import { listen, resizeObserver, px, getScrollBarSize, getScrollContainer } from '@chaomingd/utils';
|
|
5
|
+
import { useModel } from '@chaomingd/store';
|
|
6
|
+
var defaultTransform = px;
|
|
7
|
+
export function setDefaultTransform(transform) {
|
|
8
|
+
defaultTransform = transform;
|
|
9
|
+
}
|
|
10
|
+
function getCount(base, gap, length) {
|
|
11
|
+
var count = Math.floor(length / base);
|
|
12
|
+
while (count) {
|
|
13
|
+
if (count * base + gap * (count - 1) <= length) break;
|
|
14
|
+
count--;
|
|
15
|
+
}
|
|
16
|
+
return count;
|
|
17
|
+
}
|
|
18
|
+
var scrollBarSize = getScrollBarSize();
|
|
19
|
+
export function useListLayout(config) {
|
|
20
|
+
var configRef = useLatest(config);
|
|
21
|
+
var timerRef = useRef();
|
|
22
|
+
var model = useModel({
|
|
23
|
+
state: {
|
|
24
|
+
col: 0,
|
|
25
|
+
row: 0,
|
|
26
|
+
width: 0,
|
|
27
|
+
height: 0
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
var layoutState = model.useGetState();
|
|
31
|
+
useEffectWithTarget(function () {
|
|
32
|
+
var _config$containerRef2;
|
|
33
|
+
var offResize;
|
|
34
|
+
var pxTransform = function pxTransform(base, width) {
|
|
35
|
+
var _configRef$current;
|
|
36
|
+
if ((_configRef$current = configRef.current) !== null && _configRef$current !== void 0 && _configRef$current.pxTransform) {
|
|
37
|
+
var _configRef$current2;
|
|
38
|
+
return (_configRef$current2 = configRef.current) === null || _configRef$current2 === void 0 ? void 0 : _configRef$current2.pxTransform(base, width);
|
|
39
|
+
}
|
|
40
|
+
return defaultTransform(base);
|
|
41
|
+
};
|
|
42
|
+
var onResize = function onResize() {
|
|
43
|
+
clearTimeout(timerRef.current);
|
|
44
|
+
timerRef.current = setTimeout(function () {
|
|
45
|
+
var _config$containerRef, _configRef$current3, _configRef$current3$o;
|
|
46
|
+
var _ref = configRef.current || {},
|
|
47
|
+
gap = _ref.gap,
|
|
48
|
+
baseHeight = _ref.baseHeight,
|
|
49
|
+
baseWidth = _ref.baseWidth;
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
51
|
+
var config = configRef.current;
|
|
52
|
+
var width;
|
|
53
|
+
var height;
|
|
54
|
+
if (config !== null && config !== void 0 && (_config$containerRef = config.containerRef) !== null && _config$containerRef !== void 0 && _config$containerRef.current) {
|
|
55
|
+
var container = config.containerRef.current;
|
|
56
|
+
var scrollContainer = getScrollContainer(config.containerRef.current);
|
|
57
|
+
width = container.offsetWidth;
|
|
58
|
+
height = container.offsetHeight;
|
|
59
|
+
if (scrollContainer !== container && config.removeScrollBarWidth !== false) {
|
|
60
|
+
width += scrollBarSize.width;
|
|
61
|
+
height += scrollBarSize.height;
|
|
62
|
+
}
|
|
63
|
+
} else {
|
|
64
|
+
width = window.innerWidth;
|
|
65
|
+
height = window.innerHeight;
|
|
66
|
+
}
|
|
67
|
+
var colGap = 0;
|
|
68
|
+
var rowGap = 0;
|
|
69
|
+
if (gap) {
|
|
70
|
+
if (Array.isArray(gap)) {
|
|
71
|
+
colGap = pxTransform(gap[0], width);
|
|
72
|
+
rowGap = pxTransform(gap[1], height);
|
|
73
|
+
} else {
|
|
74
|
+
colGap = rowGap = pxTransform(gap, width);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
var newState = {};
|
|
78
|
+
var col = Math.abs(model.getState().width - width) === scrollBarSize.width ? model.getState().col : getCount(pxTransform(baseWidth || 0, width), colGap, width);
|
|
79
|
+
newState.col = col;
|
|
80
|
+
newState.row = 0;
|
|
81
|
+
newState.width = width;
|
|
82
|
+
newState.height = height;
|
|
83
|
+
newState.windowWidth = window.innerWidth;
|
|
84
|
+
newState.windowHeight = window.innerHeight;
|
|
85
|
+
if (baseHeight) {
|
|
86
|
+
var row = Math.abs(model.getState().height - height) === scrollBarSize.height ? model.getState().row : getCount(pxTransform(baseHeight, height), rowGap, height) + 1;
|
|
87
|
+
newState.row = row;
|
|
88
|
+
}
|
|
89
|
+
var oldState = model.getState();
|
|
90
|
+
if (newState.col === oldState.col && newState.row === oldState.row) return;
|
|
91
|
+
model.setState(newState);
|
|
92
|
+
(_configRef$current3 = configRef.current) === null || _configRef$current3 === void 0 || (_configRef$current3$o = _configRef$current3.onChange) === null || _configRef$current3$o === void 0 || _configRef$current3$o.call(_configRef$current3, model.getState());
|
|
93
|
+
}, 17);
|
|
94
|
+
};
|
|
95
|
+
onResize();
|
|
96
|
+
if (config !== null && config !== void 0 && (_config$containerRef2 = config.containerRef) !== null && _config$containerRef2 !== void 0 && _config$containerRef2.current) {
|
|
97
|
+
offResize = resizeObserver({
|
|
98
|
+
el: config.containerRef.current,
|
|
99
|
+
onResize: onResize
|
|
100
|
+
});
|
|
101
|
+
} else {
|
|
102
|
+
offResize = listen(window, 'resize', onResize);
|
|
103
|
+
}
|
|
104
|
+
return function () {
|
|
105
|
+
offResize();
|
|
106
|
+
};
|
|
107
|
+
}, config === null || config === void 0 ? void 0 : config.containerRef);
|
|
108
|
+
useUnmount(function () {
|
|
109
|
+
clearTimeout(timerRef.current);
|
|
110
|
+
});
|
|
111
|
+
return layoutState;
|
|
112
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { TService, TMediaQueryItem, UsePaginationParams, UsePaginationReturnValue, UsePaginationReturnValueWidthResponsive, UsePaginationReturnValueWidthListLayout, IData } from './type';
|
|
2
|
+
import { UseListLayoutConfig } from '../useListLayout';
|
|
3
|
+
export declare function usePagination<TData extends IData = any, TFormValues extends Record<string, any> = Record<string, any>>(service: TService<TData, TFormValues>, params?: UsePaginationParams): UsePaginationReturnValue<TData>;
|
|
4
|
+
export declare function usePaginationWithResponsive<TData extends IData = any, TFormValues extends Record<string, any> = Record<string, any>>(service: TService<TData, TFormValues>, params: UsePaginationParams & {
|
|
5
|
+
mediaQuery: TMediaQueryItem[];
|
|
6
|
+
}): UsePaginationReturnValueWidthResponsive<TData>;
|
|
7
|
+
/**
|
|
8
|
+
* 列表布局
|
|
9
|
+
*/
|
|
10
|
+
export declare function usePaginationWithListLayout<TData extends IData = any, TFormValues extends Record<string, any> = Record<string, any>>(service: TService<TData, TFormValues>, params: UsePaginationParams & {
|
|
11
|
+
listLayoutConfig: UseListLayoutConfig & {
|
|
12
|
+
row?: number;
|
|
13
|
+
};
|
|
14
|
+
}): UsePaginationReturnValueWidthListLayout<TData>;
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
3
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
4
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
5
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
6
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
7
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
8
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
9
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
10
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
11
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
12
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
13
|
+
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw new Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, catch: function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; }
|
|
14
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
15
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
16
|
+
import { useLatest } from 'ahooks';
|
|
17
|
+
import { createAsyncEffect, useModel } from '@chaomingd/store';
|
|
18
|
+
import { useResponsive } from "../useResponsive";
|
|
19
|
+
import { useEffect } from 'react';
|
|
20
|
+
import { DEFAULT_PAGE_SIZE, DEFAULT_PAGE_SIZE_OPTIONS } from "./constant";
|
|
21
|
+
import { useListLayout } from "../useListLayout";
|
|
22
|
+
import { gerationPageSizeOptions } from "./utils";
|
|
23
|
+
export function usePagination(service, params) {
|
|
24
|
+
var serviceRef = useLatest(service);
|
|
25
|
+
var paramsRef = useLatest(params);
|
|
26
|
+
var model = useModel({
|
|
27
|
+
state: {
|
|
28
|
+
items: (params === null || params === void 0 ? void 0 : params.defaultData) || [],
|
|
29
|
+
current: (params === null || params === void 0 ? void 0 : params.defaultCurrent) || 1,
|
|
30
|
+
pageSize: (params === null || params === void 0 ? void 0 : params.defaultPageSize) || DEFAULT_PAGE_SIZE,
|
|
31
|
+
loading: false,
|
|
32
|
+
totalCount: 0,
|
|
33
|
+
pageSizeOptions: DEFAULT_PAGE_SIZE_OPTIONS,
|
|
34
|
+
total: 0,
|
|
35
|
+
list: []
|
|
36
|
+
},
|
|
37
|
+
effects: {
|
|
38
|
+
fetchData: createAsyncEffect( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
|
39
|
+
var _paramsRef$current, _serviceRef$current;
|
|
40
|
+
var form, _model$getState, current, pageSize, res;
|
|
41
|
+
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
42
|
+
while (1) switch (_context.prev = _context.next) {
|
|
43
|
+
case 0:
|
|
44
|
+
form = (_paramsRef$current = paramsRef.current) === null || _paramsRef$current === void 0 ? void 0 : _paramsRef$current.form;
|
|
45
|
+
_model$getState = model.getState(), current = _model$getState.current, pageSize = _model$getState.pageSize;
|
|
46
|
+
if (pageSize) {
|
|
47
|
+
_context.next = 4;
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
return _context.abrupt("return", null);
|
|
51
|
+
case 4:
|
|
52
|
+
_context.next = 6;
|
|
53
|
+
return (_serviceRef$current = serviceRef.current) === null || _serviceRef$current === void 0 ? void 0 : _serviceRef$current.call(serviceRef, current, pageSize, form ? form.getFieldsValue() : {}).catch(function (err) {
|
|
54
|
+
return console.log(err);
|
|
55
|
+
});
|
|
56
|
+
case 6:
|
|
57
|
+
res = _context.sent;
|
|
58
|
+
if (!res) {
|
|
59
|
+
_context.next = 9;
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
return _context.abrupt("return", {
|
|
63
|
+
items: res.list || res.items || [],
|
|
64
|
+
totalCount: res.total || res.totalCount || 0,
|
|
65
|
+
data: res
|
|
66
|
+
});
|
|
67
|
+
case 9:
|
|
68
|
+
return _context.abrupt("return", null);
|
|
69
|
+
case 10:
|
|
70
|
+
case "end":
|
|
71
|
+
return _context.stop();
|
|
72
|
+
}
|
|
73
|
+
}, _callee);
|
|
74
|
+
})), {
|
|
75
|
+
loadingKey: 'loading'
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
useEffect(function () {
|
|
80
|
+
var _paramsRef$current2;
|
|
81
|
+
if (((_paramsRef$current2 = paramsRef.current) === null || _paramsRef$current2 === void 0 ? void 0 : _paramsRef$current2.manu) !== true) {
|
|
82
|
+
model.getEffect('fetchData')();
|
|
83
|
+
}
|
|
84
|
+
}, (params === null || params === void 0 ? void 0 : params.refreshDeps) || []);
|
|
85
|
+
var state = model.useGetState();
|
|
86
|
+
var returnValue = {
|
|
87
|
+
model: model,
|
|
88
|
+
items: state.items,
|
|
89
|
+
list: state.list,
|
|
90
|
+
data: state.data,
|
|
91
|
+
loading: state.loading,
|
|
92
|
+
refresh: function refresh(options) {
|
|
93
|
+
return model.getEffect('fetchData')(options);
|
|
94
|
+
},
|
|
95
|
+
search: function search(options) {
|
|
96
|
+
model.setState({
|
|
97
|
+
current: 1
|
|
98
|
+
}, {
|
|
99
|
+
silent: true
|
|
100
|
+
});
|
|
101
|
+
return model.getEffect('fetchData')(options);
|
|
102
|
+
},
|
|
103
|
+
reset: function reset(options) {
|
|
104
|
+
var _paramsRef$current3;
|
|
105
|
+
model.setState({
|
|
106
|
+
current: 1
|
|
107
|
+
}, {
|
|
108
|
+
silent: true
|
|
109
|
+
});
|
|
110
|
+
var form = (_paramsRef$current3 = paramsRef.current) === null || _paramsRef$current3 === void 0 ? void 0 : _paramsRef$current3.form;
|
|
111
|
+
if (form) {
|
|
112
|
+
form.resetFields();
|
|
113
|
+
}
|
|
114
|
+
return model.getEffect('fetchData')(options);
|
|
115
|
+
},
|
|
116
|
+
paginationProps: {
|
|
117
|
+
current: state.current,
|
|
118
|
+
pageSize: state.pageSize,
|
|
119
|
+
pageSizeOptions: state.pageSizeOptions,
|
|
120
|
+
total: state.totalCount,
|
|
121
|
+
onChange: function onChange(current, pageSize) {
|
|
122
|
+
model.setState({
|
|
123
|
+
current: current,
|
|
124
|
+
pageSize: pageSize
|
|
125
|
+
}, {
|
|
126
|
+
silent: true
|
|
127
|
+
});
|
|
128
|
+
model.getEffect('fetchData')();
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
return returnValue;
|
|
133
|
+
}
|
|
134
|
+
export function usePaginationWithResponsive(service, params) {
|
|
135
|
+
var paginationValue = usePagination(service, params);
|
|
136
|
+
var _useResponsive = useResponsive({
|
|
137
|
+
mediaQuery: params.mediaQuery,
|
|
138
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
139
|
+
onChange: function onChange(mediaQueryMatchedItem) {
|
|
140
|
+
var pageSize = mediaQueryMatchedItem.data.col * mediaQueryMatchedItem.data.row;
|
|
141
|
+
var pageSizeOptions = gerationPageSizeOptions(pageSize);
|
|
142
|
+
paginationValue.model.setState({
|
|
143
|
+
pageSizeOptions: pageSizeOptions,
|
|
144
|
+
pageSize: pageSize
|
|
145
|
+
}, {
|
|
146
|
+
silent: true
|
|
147
|
+
});
|
|
148
|
+
paginationValue.refresh();
|
|
149
|
+
}
|
|
150
|
+
}),
|
|
151
|
+
mediaQueryMatchedItem = _useResponsive.mediaQueryMatchedItem,
|
|
152
|
+
matchMedia = _useResponsive.matchMedia;
|
|
153
|
+
paginationValue.mediaQueryMatchedItem = mediaQueryMatchedItem;
|
|
154
|
+
paginationValue.matchMedia = matchMedia;
|
|
155
|
+
return paginationValue;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 列表布局
|
|
160
|
+
*/
|
|
161
|
+
export function usePaginationWithListLayout(service, params) {
|
|
162
|
+
var paginationValue = usePagination(service, _objectSpread(_objectSpread({}, params || {}), {}, {
|
|
163
|
+
manu: true
|
|
164
|
+
}));
|
|
165
|
+
var listLayout = useListLayout(params.listLayoutConfig);
|
|
166
|
+
var row = Math.max(params.listLayoutConfig.row || 0, listLayout.row);
|
|
167
|
+
useEffect(function () {
|
|
168
|
+
if (row) {
|
|
169
|
+
var pageSize = row * listLayout.col;
|
|
170
|
+
var pageSizeOptions = gerationPageSizeOptions(pageSize);
|
|
171
|
+
paginationValue.model.setState({
|
|
172
|
+
pageSizeOptions: pageSizeOptions,
|
|
173
|
+
pageSize: pageSize,
|
|
174
|
+
current: 1
|
|
175
|
+
}, {
|
|
176
|
+
silent: true
|
|
177
|
+
});
|
|
178
|
+
paginationValue.refresh();
|
|
179
|
+
}
|
|
180
|
+
}, [listLayout].concat(_toConsumableArray(params.refreshDeps || [])));
|
|
181
|
+
Object.assign(paginationValue, listLayout);
|
|
182
|
+
paginationValue.row = row;
|
|
183
|
+
return paginationValue;
|
|
184
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Nullable } from '@chaomingd/utils';
|
|
2
|
+
import { FormInstance } from 'antd';
|
|
3
|
+
import { DependencyList } from 'react';
|
|
4
|
+
import { Model } from '@chaomingd/store';
|
|
5
|
+
import { IResponsiveConfigItem } from '../useResponsive';
|
|
6
|
+
export type TMediaQueryItem = IResponsiveConfigItem<{
|
|
7
|
+
col: number;
|
|
8
|
+
row: number;
|
|
9
|
+
}>;
|
|
10
|
+
export interface UsePaginationParams<TDataItem extends any = any> {
|
|
11
|
+
defaultData?: TDataItem[];
|
|
12
|
+
defaultPageSize?: number;
|
|
13
|
+
defaultCurrent?: number;
|
|
14
|
+
refreshDeps?: DependencyList;
|
|
15
|
+
form?: FormInstance;
|
|
16
|
+
manu?: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface IData {
|
|
19
|
+
totalCount?: number;
|
|
20
|
+
items?: any[];
|
|
21
|
+
total?: number;
|
|
22
|
+
list?: any[];
|
|
23
|
+
}
|
|
24
|
+
export type TService<TData extends IData = any, TFormValues extends Record<string, any> = Record<string, any>> = (current: number, pageSize: number, formValues: TFormValues, ...args: any[]) => Promise<Nullable<TData>>;
|
|
25
|
+
export interface fetchDataOptions {
|
|
26
|
+
showLoading?: boolean;
|
|
27
|
+
}
|
|
28
|
+
export interface UsePaginationReturnValue<TData extends IData = any> {
|
|
29
|
+
model: Model<IUsePaginationState<TData>, UsePaginationEffects>;
|
|
30
|
+
items: TData['items'];
|
|
31
|
+
list: TData['list'];
|
|
32
|
+
data?: TData;
|
|
33
|
+
loading: boolean;
|
|
34
|
+
paginationProps: {
|
|
35
|
+
current: number;
|
|
36
|
+
pageSize: number;
|
|
37
|
+
total: number;
|
|
38
|
+
onChange: (current: number, pageSize: number) => void;
|
|
39
|
+
pageSizeOptions: number[];
|
|
40
|
+
};
|
|
41
|
+
refresh: (options?: fetchDataOptions, ...args: any[]) => Promise<Nullable<TData>>;
|
|
42
|
+
reset: (options?: fetchDataOptions, ...args: any[]) => Promise<Nullable<TData>>;
|
|
43
|
+
search: (options?: fetchDataOptions, ...args: any[]) => Promise<Nullable<TData>>;
|
|
44
|
+
}
|
|
45
|
+
export interface UsePaginationReturnValueWidthResponsive<TData extends IData = any> extends UsePaginationReturnValue<TData> {
|
|
46
|
+
mediaQueryMatchedItem: TMediaQueryItem;
|
|
47
|
+
matchMedia: () => void;
|
|
48
|
+
}
|
|
49
|
+
export interface UsePaginationReturnValueWidthListLayout<TData extends IData = any> extends UsePaginationReturnValue<TData> {
|
|
50
|
+
col: number;
|
|
51
|
+
row: number;
|
|
52
|
+
}
|
|
53
|
+
export interface UsePaginationEffects<TData extends IData = any> {
|
|
54
|
+
fetchData: (options?: {
|
|
55
|
+
showLoading?: boolean;
|
|
56
|
+
}, ...args: any[]) => Promise<Nullable<TData>>;
|
|
57
|
+
}
|
|
58
|
+
export interface IUsePaginationState<TData extends IData = any> {
|
|
59
|
+
items: TData['items'];
|
|
60
|
+
loading: boolean;
|
|
61
|
+
current: number;
|
|
62
|
+
pageSize: number;
|
|
63
|
+
totalCount: number;
|
|
64
|
+
pageSizeOptions: number[];
|
|
65
|
+
data?: TData;
|
|
66
|
+
total: number;
|
|
67
|
+
list: TData['items'];
|
|
68
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function gerationPageSizeOptions(pageSize: number): number[];
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { MutableRefObject } from 'react';
|
|
2
|
+
declare class ResizerStore {
|
|
3
|
+
updator: (state: IResizeStoreState) => void;
|
|
4
|
+
state: IResizeStoreState;
|
|
5
|
+
constructor(updator: (state: IResizeStoreState) => void, baseDelta?: IResizerStoreConfig);
|
|
6
|
+
setState(state: Partial<IResizeStoreState>): void;
|
|
7
|
+
}
|
|
8
|
+
type TMinMaxDelta = Pick<IConfig, 'minDeltaX' | 'maxDeltaX' | 'minDeltaY' | 'maxDeltaY'>;
|
|
9
|
+
export interface IConfig {
|
|
10
|
+
firstUpdate?: boolean;
|
|
11
|
+
baseDeltaX?: number;
|
|
12
|
+
baseDeltaY?: number;
|
|
13
|
+
minDeltaX?: number;
|
|
14
|
+
maxDeltaX?: number;
|
|
15
|
+
minDeltaY?: number;
|
|
16
|
+
maxDeltaY?: number;
|
|
17
|
+
getMinMaxDelta?: (state: IResizeStoreState) => Partial<TMinMaxDelta>;
|
|
18
|
+
direction?: 'left' | 'top' | 'right' | 'bottom';
|
|
19
|
+
elRef: MutableRefObject<HTMLElement | null>;
|
|
20
|
+
resizerStore?: ReturnType<typeof useResizerStore>;
|
|
21
|
+
updator?: (state: IResizeStoreState) => any;
|
|
22
|
+
resizingChange?: (resizing: boolean) => void;
|
|
23
|
+
}
|
|
24
|
+
interface IDeltaRect {
|
|
25
|
+
deltaX: number;
|
|
26
|
+
deltaY: number;
|
|
27
|
+
}
|
|
28
|
+
export declare function useResizer(config: IConfig): ResizerStore;
|
|
29
|
+
export interface IResizeStoreState extends IDeltaRect {
|
|
30
|
+
resizing: boolean;
|
|
31
|
+
}
|
|
32
|
+
export interface IResizerStoreConfig {
|
|
33
|
+
baseDeltaX?: number;
|
|
34
|
+
baseDeltaY?: number;
|
|
35
|
+
}
|
|
36
|
+
export declare function useResizerStore(baseDelta: IResizerStoreConfig): ResizerStore;
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
3
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
4
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
5
|
+
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
6
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
7
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
8
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
9
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
10
|
+
import { listen } from '@chaomingd/utils';
|
|
11
|
+
import { pick } from '@chaomingd/utils';
|
|
12
|
+
import { requestAnimationFrameThrottle } from '@chaomingd/utils';
|
|
13
|
+
import { useCreation, useUpdate } from 'ahooks';
|
|
14
|
+
import { useRef } from 'react';
|
|
15
|
+
import { useEffectWithTarget } from "../useEffectWithTarget";
|
|
16
|
+
var ResizerStore = /*#__PURE__*/function () {
|
|
17
|
+
function ResizerStore(updator, baseDelta) {
|
|
18
|
+
_classCallCheck(this, ResizerStore);
|
|
19
|
+
_defineProperty(this, "state", {
|
|
20
|
+
deltaX: 0,
|
|
21
|
+
deltaY: 0,
|
|
22
|
+
resizing: false
|
|
23
|
+
});
|
|
24
|
+
this.updator = updator;
|
|
25
|
+
if (baseDelta) {
|
|
26
|
+
this.state.deltaX = baseDelta.baseDeltaX || 0;
|
|
27
|
+
this.state.deltaY = baseDelta.baseDeltaY || 0;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
_createClass(ResizerStore, [{
|
|
31
|
+
key: "setState",
|
|
32
|
+
value: function setState(state) {
|
|
33
|
+
this.state = _objectSpread(_objectSpread({}, this.state), state);
|
|
34
|
+
this.updator(this.state);
|
|
35
|
+
}
|
|
36
|
+
}]);
|
|
37
|
+
return ResizerStore;
|
|
38
|
+
}();
|
|
39
|
+
function getMinOrMaxDelta(config, state) {
|
|
40
|
+
var delta = _objectSpread({}, config);
|
|
41
|
+
if (config.getMinMaxDelta) {
|
|
42
|
+
Object.assign(delta, config.getMinMaxDelta(state) || {});
|
|
43
|
+
}
|
|
44
|
+
return delta;
|
|
45
|
+
}
|
|
46
|
+
function strictDelta(delta, minMaxDelta) {
|
|
47
|
+
if (minMaxDelta.maxDeltaX !== undefined) {
|
|
48
|
+
if (delta.deltaX !== undefined) {
|
|
49
|
+
delta.deltaX = Math.min(delta.deltaX, minMaxDelta.maxDeltaX);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (minMaxDelta.maxDeltaY !== undefined) {
|
|
53
|
+
if (delta.deltaY !== undefined) {
|
|
54
|
+
delta.deltaY = Math.min(delta.deltaY, minMaxDelta.maxDeltaY);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (minMaxDelta.minDeltaX !== undefined) {
|
|
58
|
+
if (delta.deltaX !== undefined) {
|
|
59
|
+
delta.deltaX = Math.max(delta.deltaX, minMaxDelta.minDeltaX);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (minMaxDelta.minDeltaY !== undefined) {
|
|
63
|
+
if (delta.deltaY !== undefined) {
|
|
64
|
+
delta.deltaY = Math.max(delta.deltaY, minMaxDelta.minDeltaY);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return delta;
|
|
68
|
+
}
|
|
69
|
+
export function useResizer(config) {
|
|
70
|
+
var _config$baseDeltaX = config.baseDeltaX,
|
|
71
|
+
baseDeltaX = _config$baseDeltaX === void 0 ? 0 : _config$baseDeltaX,
|
|
72
|
+
_config$baseDeltaY = config.baseDeltaY,
|
|
73
|
+
baseDeltaY = _config$baseDeltaY === void 0 ? 0 : _config$baseDeltaY,
|
|
74
|
+
direction = config.direction,
|
|
75
|
+
resizerStore = config.resizerStore,
|
|
76
|
+
elRef = config.elRef;
|
|
77
|
+
var updator = useUpdate();
|
|
78
|
+
var innerResizerStore = useCreation(function () {
|
|
79
|
+
return resizerStore || new ResizerStore(config.updator || updator, config);
|
|
80
|
+
}, []);
|
|
81
|
+
innerResizerStore.updator = config.updator || updator;
|
|
82
|
+
var configRef = useRef(config);
|
|
83
|
+
configRef.current = config;
|
|
84
|
+
var mousedownClientRef = useRef({
|
|
85
|
+
clientX: 0,
|
|
86
|
+
clientY: 0
|
|
87
|
+
});
|
|
88
|
+
var deltaRef = useRef({
|
|
89
|
+
deltaX: baseDeltaX,
|
|
90
|
+
deltaY: baseDeltaY
|
|
91
|
+
});
|
|
92
|
+
useEffectWithTarget(function () {
|
|
93
|
+
var offMouseDown;
|
|
94
|
+
var cancelThrottleFunc;
|
|
95
|
+
if (elRef.current) {
|
|
96
|
+
offMouseDown = listen(elRef.current, 'mousedown', function (e) {
|
|
97
|
+
cancelThrottleFunc = handlerResizerMouseDown({
|
|
98
|
+
e: e,
|
|
99
|
+
mousedownClientRef: mousedownClientRef,
|
|
100
|
+
store: innerResizerStore,
|
|
101
|
+
deltaRef: deltaRef,
|
|
102
|
+
configRef: configRef
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
// 第一次调用
|
|
107
|
+
if (configRef.current.firstUpdate) {
|
|
108
|
+
innerResizerStore.setState({});
|
|
109
|
+
}
|
|
110
|
+
return function () {
|
|
111
|
+
offMouseDown && offMouseDown();
|
|
112
|
+
cancelThrottleFunc && cancelThrottleFunc();
|
|
113
|
+
};
|
|
114
|
+
}, elRef, [baseDeltaX, baseDeltaY, innerResizerStore, direction, resizerStore], true);
|
|
115
|
+
return resizerStore || innerResizerStore;
|
|
116
|
+
}
|
|
117
|
+
function handlerResizerMouseDown(_ref) {
|
|
118
|
+
var _configRef$current$re, _configRef$current;
|
|
119
|
+
var e = _ref.e,
|
|
120
|
+
configRef = _ref.configRef,
|
|
121
|
+
mousedownClientRef = _ref.mousedownClientRef,
|
|
122
|
+
store = _ref.store,
|
|
123
|
+
deltaRef = _ref.deltaRef;
|
|
124
|
+
mousedownClientRef.current = pick(e, ['clientX', 'clientY']);
|
|
125
|
+
var deltaX = store.state.deltaX;
|
|
126
|
+
var deltaY = store.state.deltaY;
|
|
127
|
+
var direction = configRef.current.direction || 'left';
|
|
128
|
+
var isHorizantal = direction === 'left' || direction === 'right';
|
|
129
|
+
var dir = ['left', 'top'].indexOf(direction) !== -1 ? -1 : 1;
|
|
130
|
+
store.setState({
|
|
131
|
+
resizing: true
|
|
132
|
+
});
|
|
133
|
+
var minMaxDelta = getMinOrMaxDelta(configRef.current, store.state);
|
|
134
|
+
(_configRef$current$re = (_configRef$current = configRef.current).resizingChange) === null || _configRef$current$re === void 0 || _configRef$current$re.call(_configRef$current, store.state.resizing);
|
|
135
|
+
var throttleMouseMoveRes = requestAnimationFrameThrottle({
|
|
136
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
137
|
+
service: function service(e) {
|
|
138
|
+
e.preventDefault();
|
|
139
|
+
if (isHorizantal) {
|
|
140
|
+
deltaRef.current.deltaX = e.clientX - mousedownClientRef.current.clientX;
|
|
141
|
+
} else {
|
|
142
|
+
deltaRef.current.deltaY = e.clientY - mousedownClientRef.current.clientY;
|
|
143
|
+
}
|
|
144
|
+
var newDelta = strictDelta(isHorizantal ? {
|
|
145
|
+
deltaX: deltaX + deltaRef.current.deltaX * dir
|
|
146
|
+
} : {
|
|
147
|
+
deltaY: deltaY + deltaRef.current.deltaY * dir
|
|
148
|
+
}, minMaxDelta);
|
|
149
|
+
store.setState(newDelta);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
var cancelThrottleFunc = throttleMouseMoveRes.cancel;
|
|
153
|
+
var offMouseMove = listen(document, 'mousemove', throttleMouseMoveRes.run);
|
|
154
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
155
|
+
var offMouseUp = listen(document, 'mouseup', function (e) {
|
|
156
|
+
var _configRef$current$re2, _configRef$current2;
|
|
157
|
+
e.preventDefault();
|
|
158
|
+
offMouseMove();
|
|
159
|
+
offMouseUp();
|
|
160
|
+
store.setState({
|
|
161
|
+
resizing: false
|
|
162
|
+
});
|
|
163
|
+
(_configRef$current$re2 = (_configRef$current2 = configRef.current).resizingChange) === null || _configRef$current$re2 === void 0 || _configRef$current$re2.call(_configRef$current2, store.state.resizing);
|
|
164
|
+
});
|
|
165
|
+
return cancelThrottleFunc;
|
|
166
|
+
}
|
|
167
|
+
export function useResizerStore(baseDelta) {
|
|
168
|
+
var updator = useUpdate();
|
|
169
|
+
var resizerStore = useCreation(function () {
|
|
170
|
+
return new ResizerStore(updator, baseDelta);
|
|
171
|
+
}, []);
|
|
172
|
+
return resizerStore;
|
|
173
|
+
}
|