仰望星辰工作室

better-staridc-MNBT

Z zfhsh first commit 2 个月前
1/*!
2FullCalendar Moment Plugin v4.3.0
3Docs & License: https://fullcalendar.io/
4(c) 2019 Adam Shaw
5*/
6
7import * as momentNs from 'moment';
8import { createPlugin, Calendar } from '@fullcalendar/core';
9
10var moment = momentNs; // the directly callable function
11function toMoment(date, calendar) {
12 if (!(calendar instanceof Calendar)) {
13 throw new Error('must supply a Calendar instance');
14 }
15 return convertToMoment(date, calendar.dateEnv.timeZone, null, calendar.dateEnv.locale.codes[0]);
16}
17function toDuration(fcDuration) {
18 return moment.duration(fcDuration); // moment accepts all the props that fc.Duration already has!
19}
20function formatWithCmdStr(cmdStr, arg) {
21 var cmd = parseCmdStr(cmdStr);
22 if (arg.end) {
23 var startMom = convertToMoment(arg.start.array, arg.timeZone, arg.start.timeZoneOffset, arg.localeCodes[0]);
24 var endMom = convertToMoment(arg.end.array, arg.timeZone, arg.end.timeZoneOffset, arg.localeCodes[0]);
25 return formatRange(cmd, createMomentFormatFunc(startMom), createMomentFormatFunc(endMom), arg.separator);
26 }
27 return convertToMoment(arg.date.array, arg.timeZone, arg.date.timeZoneOffset, arg.localeCodes[0]).format(cmd.whole); // TODO: test for this
28}
29var main = createPlugin({
30 cmdFormatter: formatWithCmdStr
31});
32function createMomentFormatFunc(mom) {
33 return function (cmdStr) {
34 return cmdStr ? mom.format(cmdStr) : ''; // because calling with blank string results in ISO8601 :(
35 };
36}
37function convertToMoment(input, timeZone, timeZoneOffset, locale) {
38 var mom;
39 if (timeZone === 'local') {
40 mom = moment(input);
41 }
42 else if (timeZone === 'UTC') {
43 mom = moment.utc(input);
44 }
45 else if (moment.tz) {
46 mom = moment.tz(input, timeZone);
47 }
48 else {
49 mom = moment.utc(input);
50 if (timeZoneOffset != null) {
51 mom.utcOffset(timeZoneOffset);
52 }
53 }
54 mom.locale(locale);
55 return mom;
56}
57function parseCmdStr(cmdStr) {
58 var parts = cmdStr.match(/^(.*?)\{(.*)\}(.*)$/); // TODO: lookbehinds for escape characters
59 if (parts) {
60 var middle = parseCmdStr(parts[2]);
61 return {
62 head: parts[1],
63 middle: middle,
64 tail: parts[3],
65 whole: parts[1] + middle.whole + parts[3]
66 };
67 }
68 else {
69 return {
70 head: null,
71 middle: null,
72 tail: null,
73 whole: cmdStr
74 };
75 }
76}
77function formatRange(cmd, formatStart, formatEnd, separator) {
78 if (cmd.middle) {
79 var startHead = formatStart(cmd.head);
80 var startMiddle = formatRange(cmd.middle, formatStart, formatEnd, separator);
81 var startTail = formatStart(cmd.tail);
82 var endHead = formatEnd(cmd.head);
83 var endMiddle = formatRange(cmd.middle, formatStart, formatEnd, separator);
84 var endTail = formatEnd(cmd.tail);
85 if (startHead === endHead && startTail === endTail) {
86 return startHead +
87 (startMiddle === endMiddle ? startMiddle : startMiddle + separator + endMiddle) +
88 startTail;
89 }
90 }
91 var startWhole = formatStart(cmd.whole);
92 var endWhole = formatEnd(cmd.whole);
93 if (startWhole === endWhole) {
94 return startWhole;
95 }
96 else {
97 return startWhole + separator + endWhole;
98 }
99}
100
101export default main;
102export { toDuration, toMoment };