UNPKG

@orca-fe/antd-plus

Version:
233 lines (227 loc) 11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = useScheduleData; exports.hourPercent = hourPercent; exports.schedulePositions = schedulePositions; var _tools = require("@orca-fe/tools"); var _moment = _interopRequireDefault(require("moment")); var _react = require("react"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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); } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } 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."); } 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); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } 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; } 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; } 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; } 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; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } function hourPercent(m) { return 100 * (m.hour() * 60 + m.minute()) / (24 * 60); } function createIsInRange(current, range) { // 起始日期 var startDate = current.clone().hour(0).minute(0).second(0).millisecond(0); // 结束日期 var endDate = current.clone().hour(23).minute(59).second(59).millisecond(999); if (range === 'week') { startDate.weekday(0); endDate.weekday(6); } // 日期是否处于范围内 var isInRange = function isInRange(start, end) { // 起始日期比结束范围要晚 if ((0, _moment.default)(start).diff(endDate) > 0) { return false; } // 结束日期比起始范围要早 if ((0, _moment.default)(end).diff(startDate) < 0) { return false; } return true; }; return { isInRange: isInRange, startDate: startDate, endDate: endDate }; } function zeroOClock(m) { return (0, _moment.default)(m).clone().hour(0).minute(0).second(0).millisecond(0); } /** * 根据日程的起止时间,计算日程的位置信息 */ function schedulePositions(start, end, current, precision, mode) { var positions = []; var currentDate = zeroOClock(current); var currentWeekday = current.weekday(); var startWeekday = currentWeekday + zeroOClock(start).diff(currentDate, 'day'); var endWeekday = currentWeekday + zeroOClock(end).diff(currentDate, 'day'); var floor = (0, _tools.floorBy)(precision); var round = (0, _tools.roundBy)(precision); for (var i = 0; i <= endWeekday - startWeekday; i++) { if (mode === 'day' && i + startWeekday !== currentWeekday) { continue; } // 针对结束时间是0点的跨天日程情况,将不在最后一天生成位置信息 if (i === endWeekday && endWeekday !== startWeekday && end.format('HH:mm:ss') === '00:00:00') { continue; } var startTime = floor(start.toDate().getTime()); var startPercent = i === 0 ? hourPercent((0, _moment.default)(startTime)) : 0; var endTime = round(end.toDate().getTime()); // 结束时间取四舍五入,如果结束时间四舍五入后和 if (endTime <= startTime) endTime += precision; var endPercent = i + startWeekday === endWeekday ? hourPercent((0, _moment.default)(endTime)) : 100; positions.push({ day: startWeekday + i, startPercent: startPercent, endPercent: endPercent }); } return positions; } /** * 根据当前日期、原始日程数据、时间精度等配置,处理日程数据 * 将会得到本周/本日的日程数据及日程在视图中的位置信息 */ function useScheduleData(options) { var current = options.current, data = options.data, precision = options.precision, mode = options.mode; // 获得任务的并发信息 var _useMemo = (0, _react.useMemo)(function () { var percisionTimestamp = precision * 60 * 1000; var floor = (0, _tools.floorBy)(percisionTimestamp); var round = (0, _tools.roundBy)(percisionTimestamp); // 判断某日期是否处于本周范围 var _createIsInRange = createIsInRange(current, mode), isInRange = _createIsInRange.isInRange, endDate = _createIsInRange.endDate, startDate = _createIsInRange.startDate; // 筛选出本周的数据 var dataInRange = data.map(function (item, index) { return _objectSpread(_objectSpread({}, item), {}, { start: (0, _moment.default)(item.start), end: (0, _moment.default)(item.end), _index: index }); }).filter(function (item) { var r = isInRange(item.start, item.end); // if (!r) { // console.log( // 'out', // item['title'], // item.start.format('MM-DD HH:mm'), // item.end.format('MM-DD HH:mm'), // ); // } return r; }); var startCache = {}; var endCache = {}; // 遍历的起始时间点 var startTimestamp = floor(startDate.toDate().getTime()); // 遍历的结束时间点 var endTimestamp = round(endDate.toDate().getTime()); // 生成起始、结束时间缓存 dataInRange.forEach(function (item) { var _startCache$startTime, _endCache$endTime; var startTime = floor(item.start.toDate().getTime()); startTimestamp = Math.min(startTimestamp, startTime); var endTime = round(item.end.toDate().getTime()); if (endTime <= startTime) endTime += percisionTimestamp; if (!startCache[startTime]) startCache[startTime] = new Set(); if (!endCache[endTime]) endCache[endTime] = new Set(); (_startCache$startTime = startCache[startTime]) === null || _startCache$startTime === void 0 || _startCache$startTime.add(item); (_endCache$endTime = endCache[endTime]) === null || _endCache$endTime === void 0 || _endCache$endTime.add(item); }); // 开始遍历数据,生成位置信息 var positionInfo = new Map(); var count = 0; var arr = []; var history = []; var _loop = function _loop() { var changed = false; var startDataList = _toConsumableArray(startCache[t] || []); var endDataSet = endCache[t]; if (endDataSet) { changed = true; arr = arr.map(function (item) { if (item && endDataSet.has(item)) { count -= 1; history.push(item); return undefined; } return item; }); } if (count === 0) { arr = []; history = []; } if (startDataList.length > 0) { changed = true; for (var i = 0; i < arr.length; i++) { if (arr[i] == null) { var item = startDataList.shift(); if (item) { if (endDataSet !== null && endDataSet !== void 0 && endDataSet.has(item)) continue; arr[i] = item; count += 1; } } if (startDataList.length <= 0) break; } startDataList.forEach(function (item) { if (endDataSet !== null && endDataSet !== void 0 && endDataSet.has(item)) return; count += 1; arr.push(item); }); } if (changed) { // console.log( // moment(t).format('YYYY-MM-DD HH:mm'), // arr.map(item => item?.['title'] || 'null').join(', '), // ); [].concat(_toConsumableArray(arr), _toConsumableArray(history)).forEach(function (item, order) { if (item) { var o = positionInfo.get(item) || { concurrency: 0, order: order, positions: [] }; // 任务并发数 o.concurrency = Math.max(o.concurrency, arr.length); // 生成位置信息 if (o.positions.length === 0) { var _o$positions; (_o$positions = o.positions).push.apply(_o$positions, _toConsumableArray(schedulePositions(item.start, item.end, current, percisionTimestamp, mode))); } positionInfo.set(item, o); } }); } }; for (var t = startTimestamp; t <= endTimestamp; t += percisionTimestamp) { _loop(); } return { positionInfo: positionInfo, dataInRange: dataInRange }; }, [current, data, precision, mode]), positionInfo = _useMemo.positionInfo, dataInRange = _useMemo.dataInRange; return { positionInfo: positionInfo, dataInRange: dataInRange }; }