Adam Shaw 7 роки тому
батько
коміт
678fa6b37b
5 змінених файлів з 35 додано та 14 видалено
  1. 8 6
      src/Calendar.ts
  2. 8 1
      src/datelib/env.ts
  3. 15 6
      src/datelib/formatting-cmd.ts
  4. 2 1
      src/datelib/formatting.ts
  5. 2 0
      src/options.ts

+ 8 - 6
src/Calendar.ts

@@ -508,7 +508,8 @@ export default class Calendar {
       options.timeZoneImpl,
       options.firstDay,
       options.weekNumberCalculation,
-      options.weekLabel
+      options.weekLabel,
+      options.cmdFormatter
     )
 
     this.viewSpecManager.clearCache()
@@ -1242,15 +1243,16 @@ EmitterMixin.mixInto(Calendar)
 // -----------------------------------------------------------------------------------------------------------------
 
 
-function buildDateEnv(locale, timeZone, timeZoneImpl, firstDay, weekNumberCalculation, weekLabel) {
+function buildDateEnv(locale, timeZone, timeZoneImpl, firstDay, weekNumberCalculation, weekLabel, cmdFormatter) {
   return new DateEnv({
-    calendarSystem: 'gregory',
+    calendarSystem: 'gregory', // TODO: make this a setting
     timeZone,
     timeZoneImpl,
     locale: getLocale(locale),
-    weekNumberCalculation: weekNumberCalculation,
-    firstDay: firstDay,
-    weekLabel: weekLabel
+    weekNumberCalculation,
+    firstDay,
+    weekLabel,
+    cmdFormatter
   })
 }
 

+ 8 - 1
src/datelib/env.ts

@@ -11,6 +11,7 @@ import { Duration, asRoughYears, asRoughMonths, asRoughDays, asRoughMs } from '.
 import { DateFormatter, buildIsoString } from './formatting'
 import { parse } from './parsing'
 import { isInt } from '../util/misc'
+import { CmdFormatterFunc, getCmdFormatter } from './formatting-cmd'
 
 export interface DateEnvSettings {
   timeZone: string
@@ -19,7 +20,8 @@ export interface DateEnvSettings {
   locale: Locale
   weekNumberCalculation?: any
   firstDay?: any,
-  weekLabel?: string
+  weekLabel?: string,
+  cmdFormatter?: string
 }
 
 export type DateInput = Date | string | number | number[]
@@ -37,6 +39,7 @@ export class DateEnv {
   weekDoy: number
   weekNumberFunc: any
   weekLabel: string // DON'T LIKE how options are confused with local
+  cmdFormatter: CmdFormatterFunc
 
 
   constructor(settings: DateEnvSettings) {
@@ -66,6 +69,10 @@ export class DateEnv {
     }
 
     this.weekLabel = settings.weekLabel != null ? settings.weekLabel : settings.locale.options.weekLabel
+
+    if (typeof settings.cmdFormatter === 'string') {
+      this.cmdFormatter = getCmdFormatter(settings.cmdFormatter)
+    }
   }
 
 

+ 15 - 6
src/datelib/formatting-cmd.ts

@@ -6,15 +6,24 @@ import {
 export type CmdFormatterFunc = (cmd: string, arg: VerboseFormattingArg) => string
 
 
-let soleCmdFunc: CmdFormatterFunc = null
+let cmdFormatters: { [name: string]: CmdFormatterFunc } = {}
 
 export function registerCmdFormatter(name, input: CmdFormatterFunc) {
-  if (!soleCmdFunc) {
-    soleCmdFunc = input
-  }
+  cmdFormatters[name] = input
+}
+
+export function getCmdFormatter(name: string): CmdFormatterFunc | null {
+  return cmdFormatters[name]
 }
 
+/*
+TODO: fix the terminology of "formatter" vs "formatting func"
+*/
 
+/*
+At the time of instantiation, this object does not know which cmd-formatting system it will use.
+It receives this at the time of formatting, as a setting.
+*/
 export class CmdFormatter implements DateFormatter {
 
   cmdStr: string
@@ -26,11 +35,11 @@ export class CmdFormatter implements DateFormatter {
   }
 
   format(date: ZonedMarker, context: DateFormattingContext) {
-    return soleCmdFunc(this.cmdStr, createVerboseFormattingArg(date, null, context, this.separator))
+    return context.cmdFormatter(this.cmdStr, createVerboseFormattingArg(date, null, context, this.separator))
   }
 
   formatRange(start: ZonedMarker, end: ZonedMarker, context: DateFormattingContext) {
-    return soleCmdFunc(this.cmdStr, createVerboseFormattingArg(start, end, context, this.separator))
+    return context.cmdFormatter(this.cmdStr, createVerboseFormattingArg(start, end, context, this.separator))
   }
 
 }

+ 2 - 1
src/datelib/formatting.ts

@@ -3,7 +3,7 @@ import { DateMarker } from './marker'
 import { CalendarSystem } from './calendar-system'
 import { Locale } from './locale'
 import { NativeFormatter } from './formatting-native'
-import { CmdFormatter } from './formatting-cmd'
+import { CmdFormatter, CmdFormatterFunc } from './formatting-cmd'
 import { FuncFormatter, FuncFormatterFunc } from './formatting-func'
 import { assignTo } from '../util/object'
 
@@ -38,6 +38,7 @@ export interface DateFormattingContext {
   calendarSystem: CalendarSystem
   computeWeekNumber: (d: DateMarker) => number
   weekLabel: string
+  cmdFormatter: CmdFormatterFunc
 }
 
 export interface DateFormatter {

+ 2 - 0
src/options.ts

@@ -40,6 +40,8 @@ export const globalDefaults = {
 
   timeZone: 'UTC', // TODO: throw error if given falsy value?
 
+  //cmdFormatter: null,
+
   // allDayDefault: undefined,
 
   // locale