{"version":3,"file":"use-calendar.mjs","sources":["../../../../../../packages/components/calendar/src/use-calendar.ts"],"sourcesContent":["import { computed, ref } from 'vue'\nimport dayjs from 'dayjs'\nimport { useLocale } from '@element-plus/hooks'\nimport { debugWarn, isArray, isDate } from '@element-plus/utils'\nimport { INPUT_EVENT, UPDATE_MODEL_EVENT } from '@element-plus/constants'\n\nimport type { ComputedRef, SetupContext } from 'vue'\nimport type { Dayjs } from 'dayjs'\nimport type { CalendarDateType, CalendarEmits, CalendarProps } from './calendar'\n\nconst adjacentMonth = (start: Dayjs, end: Dayjs): [Dayjs, Dayjs][] => {\n const firstMonthLastDay = start.endOf('month')\n const lastMonthFirstDay = end.startOf('month')\n\n // Whether the last day of the first month and the first day of the last month is in the same week\n const isSameWeek = firstMonthLastDay.isSame(lastMonthFirstDay, 'week')\n const lastMonthStartDay = isSameWeek\n ? lastMonthFirstDay.add(1, 'week')\n : lastMonthFirstDay\n\n return [\n [start, firstMonthLastDay],\n [lastMonthStartDay.startOf('week'), end],\n ]\n}\n\nconst threeConsecutiveMonth = (start: Dayjs, end: Dayjs): [Dayjs, Dayjs][] => {\n const firstMonthLastDay = start.endOf('month')\n const secondMonthFirstDay = start.add(1, 'month').startOf('month')\n\n // Whether the last day of the first month and the second month is in the same week\n const secondMonthStartDay = firstMonthLastDay.isSame(\n secondMonthFirstDay,\n 'week'\n )\n ? secondMonthFirstDay.add(1, 'week')\n : secondMonthFirstDay\n\n const secondMonthLastDay = secondMonthStartDay.endOf('month')\n const lastMonthFirstDay = end.startOf('month')\n\n // Whether the last day of the second month and the last day of the last month is in the same week\n const lastMonthStartDay = secondMonthLastDay.isSame(lastMonthFirstDay, 'week')\n ? lastMonthFirstDay.add(1, 'week')\n : lastMonthFirstDay\n\n return [\n [start, firstMonthLastDay],\n [secondMonthStartDay.startOf('week'), secondMonthLastDay],\n [lastMonthStartDay.startOf('week'), end],\n ]\n}\n\nexport const useCalendar = (\n props: CalendarProps,\n emit: SetupContext['emit'],\n componentName: string\n) => {\n const { lang } = useLocale()\n\n const selectedDay = ref()\n const now = dayjs().locale(lang.value)\n\n const realSelectedDay = computed({\n get() {\n if (!props.modelValue) return selectedDay.value\n return date.value\n },\n set(val) {\n if (!val) return\n selectedDay.value = val\n const result = val.toDate()\n\n emit(INPUT_EVENT, result)\n emit(UPDATE_MODEL_EVENT, result)\n },\n })\n\n // if range is valid, we get a two-digit array\n const validatedRange = computed(() => {\n if (\n !props.range ||\n !isArray(props.range) ||\n props.range.length !== 2 ||\n props.range.some((item) => !isDate(item))\n )\n return []\n const rangeArrDayjs = props.range.map((_) => dayjs(_).locale(lang.value))\n const [startDayjs, endDayjs] = rangeArrDayjs\n if (startDayjs.isAfter(endDayjs)) {\n debugWarn(componentName, 'end time should be greater than start time')\n return []\n }\n if (startDayjs.isSame(endDayjs, 'month')) {\n // same month\n return calculateValidatedDateRange(startDayjs, endDayjs)\n } else {\n // two months\n if (startDayjs.add(1, 'month').month() !== endDayjs.month()) {\n debugWarn(\n componentName,\n 'start time and end time interval must not exceed two months'\n )\n return []\n }\n return calculateValidatedDateRange(startDayjs, endDayjs)\n }\n })\n\n const date: ComputedRef = computed(() => {\n if (!props.modelValue) {\n return (\n realSelectedDay.value ||\n (validatedRange.value.length ? validatedRange.value[0][0] : now)\n )\n } else {\n return dayjs(props.modelValue).locale(lang.value)\n }\n })\n const prevMonthDayjs = computed(() => date.value.subtract(1, 'month').date(1))\n const nextMonthDayjs = computed(() => date.value.add(1, 'month').date(1))\n const prevYearDayjs = computed(() => date.value.subtract(1, 'year').date(1))\n const nextYearDayjs = computed(() => date.value.add(1, 'year').date(1))\n\n // https://github.com/element-plus/element-plus/issues/3155\n // Calculate the validate date range according to the start and end dates\n const calculateValidatedDateRange = (\n startDayjs: Dayjs,\n endDayjs: Dayjs\n ): [Dayjs, Dayjs][] => {\n const firstDay = startDayjs.startOf('week')\n const lastDay = endDayjs.endOf('week')\n const firstMonth = firstDay.get('month')\n const lastMonth = lastDay.get('month')\n\n // Current mouth\n if (firstMonth === lastMonth) {\n return [[firstDay, lastDay]]\n }\n // Two adjacent months\n else if ((firstMonth + 1) % 12 === lastMonth) {\n return adjacentMonth(firstDay, lastDay)\n }\n // Three consecutive months (compatible: 2021-01-30 to 2021-02-28)\n else if (\n firstMonth + 2 === lastMonth ||\n (firstMonth + 1) % 11 === lastMonth\n ) {\n return threeConsecutiveMonth(firstDay, lastDay)\n }\n // Other cases\n else {\n debugWarn(\n componentName,\n 'start time and end time interval must not exceed two months'\n )\n return []\n }\n }\n\n const pickDay = (day: Dayjs) => {\n realSelectedDay.value = day\n }\n\n const selectDate = (type: CalendarDateType) => {\n const dateMap: Record = {\n 'prev-month': prevMonthDayjs.value,\n 'next-month': nextMonthDayjs.value,\n 'prev-year': prevYearDayjs.value,\n 'next-year': nextYearDayjs.value,\n today: now,\n }\n\n const day = dateMap[type]\n\n if (!day.isSame(date.value, 'day')) {\n pickDay(day)\n }\n }\n\n return {\n calculateValidatedDateRange,\n date,\n realSelectedDay,\n pickDay,\n selectDate,\n validatedRange,\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;AAKA,MAAM,aAAa,GAAG,CAAC,KAAK,EAAE,GAAG,KAAK;AACtC,EAAE,MAAM,iBAAiB,GAAG,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;AACjD,EAAE,MAAM,iBAAiB,GAAG,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;AACjD,EAAE,MAAM,UAAU,GAAG,iBAAiB,CAAC,MAAM,CAAC,iBAAiB,EAAE,MAAM,CAAC,CAAC;AACzE,EAAE,MAAM,iBAAiB,GAAG,UAAU,GAAG,iBAAiB,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,CAAC,GAAG,iBAAiB,CAAC;AAC9F,EAAE,OAAO;AACT,IAAI,CAAC,KAAK,EAAE,iBAAiB,CAAC;AAC9B,IAAI,CAAC,iBAAiB,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,GAAG,CAAC;AAC5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