import moment from 'moment' ; import { Duration , VerboseFormattingArg , createPlugin , CalendarApi } from '@fullcalendar/common' ; export function toMoment ( date , calendar ) { if ( ! ( calendar instanceof CalendarApi ) ) { throw new Error ( 'must supply a CalendarApi instance' ) ; } let { dateEnv } = calendar . getCurrentData ( ) ; return convertToMoment ( date , dateEnv . timeZone , null , dateEnv . locale . codes [ 0 ] ) ; } export function toMomentDuration ( fcDuration ) { return moment . duration ( fcDuration ) ; } function formatWithCmdStr ( cmdStr , arg ) { let cmd = parseCmdStr ( cmdStr ) ; if ( arg . end ) { let startMom = convertToMoment ( arg . start . array , arg . timeZone , arg . start . timeZoneOffset , arg . localeCodes [ 0 ] ) ; let endMom = convertToMoment ( arg . end . array , arg . timeZone , arg . end . timeZoneOffset , arg . localeCodes [ 0 ] ) ; return formatRange ( cmd , createMomentFormatFunc ( startMom ) , createMomentFormatFunc ( endMom ) , arg . defaultSeparator ) ; } return convertToMoment ( arg . date . array , arg . timeZone , arg . date . timeZoneOffset , arg . localeCodes [ 0 ] ) . format ( cmd . whole ) ; } export default createPlugin ( { cmdFormatter : formatWithCmdStr , } ) ; function createMomentFormatFunc ( mom ) { return ( cmdStr ) => ( cmdStr ? mom . format ( cmdStr ) : '' ) ; } function convertToMoment ( input , timeZone , timeZoneOffset , locale ) { let mom ; if ( timeZone === 'local' ) { mom = moment ( input ) ; } else if ( timeZone === 'UTC' ) { mom = moment . utc ( input ) ; } else if ( ( moment as any ) . tz ) { mom = ( moment as any ) . tz ( input , timeZone ) ; } else { mom = moment . utc ( input ) ; if ( timeZoneOffset != null ) { mom . utcOffset ( timeZoneOffset ) ; } } mom . locale ( locale ) ; return mom ; } interface CmdParts { head : string | null ; middle : CmdParts | null ; tail : string | null ; whole : string ; } function parseCmdStr ( cmdStr ) { let parts = cmdStr . match ( /^(.*?)\{(.*)\}(.*)$/ ) ; if ( parts ) { let middle = parseCmdStr ( parts [ 2 ] ) ; return { head : parts [ 1 ] , middle , tail : parts [ 3 ] , whole : parts [ 1 ] + middle . whole + parts [ 3 ] , } ; } return { head : null , middle : null , tail : null , whole : cmdStr , } ; } function formatRange ( cmd , formatStart , formatEnd , separator ) { if ( cmd . middle ) { let startHead = formatStart ( cmd . head ) ; let startMiddle = formatRange ( cmd . middle , formatStart , formatEnd , separator ) ; let startTail = formatStart ( cmd . tail ) ; let endHead = formatEnd ( cmd . head ) ; let endMiddle = formatRange ( cmd . middle , formatStart , formatEnd , separator ) ; let endTail = formatEnd ( cmd . tail ) ; if ( startHead === endHead && startTail === endTail ) { return startHead + ( startMiddle === endMiddle ? startMiddle : startMiddle + separator + endMiddle ) + startTail ; } } let startWhole = formatStart ( cmd . whole ) ; let endWhole = formatEnd ( cmd . whole ) ; if ( startWhole === endWhole ) { return startWhole ; } return startWhole + separator + endWhole ; }