UNPKG

@orca-fe/antd-plus

Version:
352 lines (345 loc) 16.2 kB
var _excluded = ["className", "current", "data", "precision", "children", "renderPlaceholder", "showNow", "showNowColor", "onDataClick", "onEmptyClick", "mode", "placeholder", "checked", "timelineHeader", "renderHeader"]; 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(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); } 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); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure 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 _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 _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } import { useEventListener, useInterval, useSetState } from 'ahooks'; import moment from 'moment'; import React, { useEffect, useMemo, useRef, useState } from 'react'; import Scrollbars from 'react-custom-scrollbars-2'; import cn from 'classnames'; import LunarDate from "./LunarDate/LunarDate"; import Task from "./Task/Task"; import TimeTip from "./TimeTip/TimeTip"; import useScheduleData, { hourPercent, schedulePositions } from "./useScheduleData"; import useStyles from "./WeeklyCalendar.style"; import { jsx as _jsx } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; import { Fragment as _Fragment } from "react/jsx-runtime"; var eArr = []; var ef = () => {}; var hours = new Array(24).fill(0).map((_, i) => moment().hour(i).minute(0)); var WeeklyCalendar = props => { var _useState = useState(() => moment()), _useState2 = _slicedToArray(_useState, 1), defaultNow = _useState2[0]; var styles = useStyles(); var _props$className = props.className, className = _props$className === void 0 ? '' : _props$className, _props$current = props.current, _current = _props$current === void 0 ? defaultNow : _props$current, _props$data = props.data, data = _props$data === void 0 ? eArr : _props$data, _props$precision = props.precision, precision = _props$precision === void 0 ? 15 : _props$precision, _props$children = props.children, children = _props$children === void 0 ? () => null : _props$children, _props$renderPlacehol = props.renderPlaceholder, renderPlaceholder = _props$renderPlacehol === void 0 ? () => null : _props$renderPlacehol, _props$showNow = props.showNow, showNow = _props$showNow === void 0 ? true : _props$showNow, _props$showNowColor = props.showNowColor, showNowColor = _props$showNowColor === void 0 ? 'red' : _props$showNowColor, _props$onDataClick = props.onDataClick, onDataClick = _props$onDataClick === void 0 ? ef : _props$onDataClick, _props$onEmptyClick = props.onEmptyClick, onEmptyClick = _props$onEmptyClick === void 0 ? ef : _props$onEmptyClick, _props$mode = props.mode, mode = _props$mode === void 0 ? 'week' : _props$mode, placeholder = props.placeholder, checked = props.checked, _props$timelineHeader = props.timelineHeader, timelineHeader = _props$timelineHeader === void 0 ? '时间段' : _props$timelineHeader, _props$renderHeader = props.renderHeader, renderHeader = _props$renderHeader === void 0 ? a => a : _props$renderHeader, otherProps = _objectWithoutProperties(props, _excluded); var current = useMemo(() => _current.clone().second(0).millisecond(0), [_current]); var weekMode = mode === 'week'; var bodyRef = useRef(null); var viewRef = useRef(null); var currentWeekday = useMemo(() => current.weekday(), [current]); var placeholderMoment = useMemo(() => placeholder ? { start: moment(placeholder.start), end: moment(placeholder.end) } : undefined, [placeholder]); // 占位日程的尺寸 var placeholderPosition = useMemo(() => placeholderMoment ? schedulePositions(placeholderMoment.start, placeholderMoment.end, current, precision, mode) : undefined, [placeholderMoment, current, precision, mode]); var _useState3 = useState(-1), _useState4 = _slicedToArray(_useState3, 2), hoverIndex = _useState4[0], setHoverIndex = _useState4[1]; /** 记录当前 hover 的日期 */ var _useSetState = useSetState({ day: 0, visible: false }), _useSetState2 = _slicedToArray(_useSetState, 2), dayHover = _useSetState2[0], setDayHover = _useSetState2[1]; /** 记录当前 hover 的时间 */ var _useSetState3 = useSetState({ time: 0, visible: false }), _useSetState4 = _slicedToArray(_useSetState3, 2), timeHover = _useSetState4[0], setTimeHover = _useSetState4[1]; // 当前时间 var _useState5 = useState(() => { var now = moment(); if (moment.isMoment(showNow)) { now = showNow; } return hourPercent(now) / 100; }), _useState6 = _slicedToArray(_useState5, 2), nowTime = _useState6[0], setNowTime = _useState6[1]; useEffect(() => { if (moment.isMoment(showNow)) { setNowTime(hourPercent(showNow) / 100); } }, [showNow]); // 计时器,用于更新当前时间 useInterval(() => { var newNowTime = hourPercent(moment()) / 100; if (nowTime !== newNowTime) setNowTime(newNowTime); }, showNow === true ? 1000 : undefined, { immediate: true }); var todayStr = useMemo(() => (typeof showNow === 'boolean' ? defaultNow : showNow).format('YYYY-MM-DD'), [showNow]); // 计算出这周每天的日期 var dayListOfWeek = useMemo(() => { if (mode === 'day') { return [{ day: current.clone(), isToday: todayStr === current.format('YYYY-MM-DD') }]; } return [0, 1, 2, 3, 4, 5, 6].map(index => { var day = current.clone().weekday(index); return { day, isToday: todayStr === day.format('YYYY-MM-DD') }; }); }, [current, mode, todayStr]); var _useScheduleData = useScheduleData({ current, data, mode, precision }), positionInfo = _useScheduleData.positionInfo, dataInRange = _useScheduleData.dataInRange; useEventListener('click', e => { if (e.target === e.currentTarget) { // 点在了空白区域,根据鼠标位置计算出当前点击的时间信息 onEmptyClick(current.clone().weekday(dayHover.day).hour(0).minute(0).second(0).add(timeHover.time * 24 * 60, 'minute')); } }, { target: viewRef }); // 监听视图鼠标事件 useEventListener('mousemove', e => { var view = viewRef.current; if (view) { var _getBoundingClientRec = e.currentTarget.getBoundingClientRect(), top = _getBoundingClientRec.top, height = _getBoundingClientRec.height; var _view$getBoundingClie = view.getBoundingClientRect(), left = _view$getBoundingClie.left, width = _view$getBoundingClie.width; var x = e.clientX - left; var y = e.clientY - top; var day = mode === 'week' ? Math.floor(x / width * 7) : currentWeekday; var _time = y / height; if (day >= 0) { setDayHover({ day, visible: true }); } setTimeHover({ time: _time, visible: true }); } }, { target: bodyRef }); useEventListener('mouseleave', e => { setDayHover({ visible: false }); }, { target: viewRef }); useEventListener('mouseleave', e => { setTimeHover({ visible: false }); }, { target: bodyRef }); var showNowProps = useMemo(() => { if (showNow === false) { return { visible: false }; } var now = moment.isMoment(showNow) ? showNow : moment(); var nowDay = now.clone().hour(0).minute(0).second(0).millisecond(0); var currentDay = current.clone().hour(0).minute(0).second(0).millisecond(0); var firstDay = current.clone().weekday(0).hour(0).minute(0).second(0).millisecond(0); var dateDiff = nowDay.diff(firstDay, 'day'); // 不是今天(日视图)或不在本周范围内,则看不见 if (mode === 'day' && nowDay.diff(currentDay, 'day') !== 0 || mode !== 'day' && (dateDiff < 0 || dateDiff > 6)) { return { visible: false }; } if (mode === 'day') { return { visible: true, lineLeft: 0, lineWidth: '100%' }; } return { visible: true, lineLeft: `${100 * dateDiff / 7}%`, lineWidth: `${100 / 7}%` }; }, [showNow, current, mode]); return /*#__PURE__*/_jsxs("div", _objectSpread(_objectSpread({ className: cn(styles.root, { [styles.dayMode]: mode === 'day', [styles.weekMode]: mode === 'week' }, className) }, otherProps), {}, { children: [weekMode && /*#__PURE__*/_jsx("div", { className: styles.hoverContainer, children: /*#__PURE__*/_jsx("div", { className: cn(styles.hoverDay, { [styles.visible]: dayHover.visible }), style: { left: `${dayHover.day * 100 / 7}%` } }) }), /*#__PURE__*/_jsxs("div", { className: styles.header, children: [/*#__PURE__*/_jsx("div", { className: cn(styles.timeline, styles.timelineTitle), children: timelineHeader }), /*#__PURE__*/_jsx("div", { className: styles.calendarContainer, children: dayListOfWeek.map(({ day, isToday }, index) => /*#__PURE__*/_jsx("div", { className: cn(styles.column, { [styles.today]: isToday }), children: renderHeader( /*#__PURE__*/_jsxs(_Fragment, { children: [/*#__PURE__*/_jsx("div", { className: styles.weekdayName, children: day.format('ddd') }), /*#__PURE__*/_jsxs("div", { className: styles.date, children: [/*#__PURE__*/_jsx("div", { className: styles.dateValue, children: day.date() === 1 ? day.format('MMM') : day.format('DD') }), /*#__PURE__*/_jsx(LunarDate, { className: styles.lunarDate, year: day.year(), month: day.month() + 1, date: day.date(), format: "month-date" })] })] }), day, { isToday }) }, index)) })] }), /*#__PURE__*/_jsx(Scrollbars, { className: styles.body, autoHide: true, children: /*#__PURE__*/_jsxs("div", { ref: bodyRef, className: styles.bodyContent, children: [/*#__PURE__*/_jsx("div", { className: styles.timeline, children: hours.map((hour, index) => /*#__PURE__*/_jsx("div", { className: styles.hour, children: /*#__PURE__*/_jsx("div", { className: styles.hourTimeline, children: index > 0 ? hour.format('HH:mm') : '' }) }, index)) }), /*#__PURE__*/_jsxs("div", { ref: viewRef, className: styles.calendarContainer, children: [dayListOfWeek.map((day, index) => /*#__PURE__*/_jsx("div", { className: styles.column, children: hours.map((hour, index) => /*#__PURE__*/_jsx("div", { className: cn(styles.hour, styles.hourCalendar) }, index)) }, index)), dataInRange.map((item, index) => { var position = positionInfo.get(item); if (!position) return null; var concurrency = position.concurrency, order = position.order; return position.positions.map(position => /*#__PURE__*/_jsx(Task, { className: styles.task, concurrency: concurrency, order: order, position: position, style: { backgroundColor: item.color }, onClick: () => { onDataClick(item, item._index); }, mode: mode, checked: item._index === checked, hover: item._index === hoverIndex, onMouseEnter: () => { setHoverIndex(item._index); }, onMouseLeave: () => { setHoverIndex(-1); }, children: children(item, item._index) }, `task_${index}_${position.day}`)); }), placeholderMoment && (placeholderPosition === null || placeholderPosition === void 0 ? void 0 : placeholderPosition.map((p, index) => /*#__PURE__*/_jsx(Task, { isPlaceholder: true, position: p, mode: mode, children: renderPlaceholder(placeholderMoment) }, `placeholder_${index}`))), showNow !== false && /*#__PURE__*/_jsx(TimeTip, _objectSpread({ time: nowTime, color: showNowColor, textLeft: -60 }, showNowProps))] }), /*#__PURE__*/_jsx(TimeTip, { time: timeHover.time, visible: timeHover.visible, textLeft: 20 })] }) })] })); }; export default WeeklyCalendar;