Przeglądaj źródła

change how we do global plugins/locales

Adam Shaw 6 lat temu
rodzic
commit
10a8b07ac0

+ 3 - 3
packages/bundle/src/main.ts

@@ -6,17 +6,17 @@ export * from '@fullcalendar/timegrid'
 export * from '@fullcalendar/list'
 export * from '@fullcalendar/bootstrap'
 
-import { addDefaultPlugins } from '@fullcalendar/core'
+import { globalPlugins } from '@fullcalendar/core'
 import interactionPlugin from '@fullcalendar/interaction'
 import dayGridPlugin from '@fullcalendar/daygrid'
 import timeGridPlugin from '@fullcalendar/timegrid'
 import listPlugin from '@fullcalendar/list'
 import bootstrapPlugin from '@fullcalendar/bootstrap'
 
-addDefaultPlugins([
+globalPlugins.push(
   interactionPlugin,
   dayGridPlugin,
   timeGridPlugin,
   listPlugin,
   bootstrapPlugin
-])
+)

+ 5 - 5
packages/core/src/Calendar.tsx

@@ -2,7 +2,7 @@ import { default as EmitterMixin, EmitterInterface } from './common/EmitterMixin
 import OptionsManager from './OptionsManager'
 import View from './View'
 import { OptionsInput, EventHandlerName, EventHandlerArgs } from './types/input-types'
-import { buildLocale, parseRawLocales, RawLocaleMap } from './datelib/locale'
+import { buildLocale, organizeRawLocales, RawLocaleMap } from './datelib/locale'
 import { DateEnv, DateInput } from './datelib/env'
 import { DateMarker, startOfDay, diffWholeDays } from './datelib/marker'
 import { createFormatter } from './datelib/formatting'
@@ -35,7 +35,7 @@ import { render, h, createRef, flushToDom } from './vdom'
 import { TaskRunner, DelayedRunner } from './util/runner'
 import ViewApi from './ViewApi'
 import NowTimer, { NowTimerCallback } from './NowTimer'
-import { defaultPlugins } from './default-plugins'
+import { globalPlugins } from './global-plugins'
 
 
 export interface DateClickApi extends DatePointApi {
@@ -81,7 +81,7 @@ export default class Calendar {
 
   // derived state
   // TODO: make these all private
-  private parseRawLocales = memoize(parseRawLocales)
+  private organizeRawLocales = memoize(organizeRawLocales)
   private buildDateEnv = memoize(buildDateEnv)
   private computeTitle = memoize(computeTitle)
   private buildTheme = memoize(buildTheme)
@@ -140,7 +140,7 @@ export default class Calendar {
 
     // only do once. don't do in onOptionsChange. because can't remove plugins
     this.addPluginDefs(
-      defaultPlugins.concat(optionsManager.computed.plugins || [])
+      globalPlugins.concat(optionsManager.computed.plugins || [])
     )
 
     this.onOptionsChange()
@@ -499,7 +499,7 @@ export default class Calendar {
     let pluginHooks = this.pluginSystem.hooks
     let rawOptions = this.optionsManager.computed
 
-    let availableLocaleData = this.parseRawLocales(rawOptions.locales)
+    let availableLocaleData = this.organizeRawLocales(rawOptions.locales)
     let dateEnv = this.buildDateEnv(rawOptions, pluginHooks, availableLocaleData)
 
     this.availableRawLocales = availableLocaleData.map

+ 2 - 2
packages/core/src/OptionsManager.ts

@@ -1,6 +1,6 @@
 import { firstDefined } from './util/misc'
 import { globalDefaults, rtlDefaults, mergeOptions } from './options'
-import { parseRawLocales, buildLocale } from './datelib/locale'
+import { organizeRawLocales, buildLocale } from './datelib/locale'
 import { __assign } from 'tslib'
 
 
@@ -53,7 +53,7 @@ export default class OptionsManager {
       this.overrides.locale,
       globalDefaults.locale
     )
-    let available = parseRawLocales(locales)
+    let available = organizeRawLocales(locales) // also done in Calendar :(
     let localeDefaults = buildLocale(locale || available.defaultCode, available.map).options
 
     let dir = firstDefined( // based on options computed so far, is direction RTL?

+ 10 - 8
packages/core/src/datelib/locale.ts

@@ -1,4 +1,5 @@
-import { mergeProps, hashValuesToArray } from '../util/object'
+import { mergeProps } from '../util/object'
+import { getGlobalRawLocales } from '../global-locales' // weird to be importing this
 import { __assign } from 'tslib'
 
 export type LocaleCodeArg = string | string[]
@@ -49,14 +50,11 @@ const RAW_EN_LOCALE = {
   noEventsMessage: 'No events to display'
 }
 
-export function parseRawLocales(explicitRawLocales: RawLocale[]): RawLocaleInfo {
+
+export function organizeRawLocales(explicitRawLocales: RawLocale[]): RawLocaleInfo {
   let defaultCode = explicitRawLocales.length > 0 ? explicitRawLocales[0].code : 'en'
-  let globalArray = window['FullCalendarLocalesAll'] || [] // from locales-all.js
-  let globalObject = window['FullCalendarLocales'] || {} // from locales/*.js. keys are meaningless
-  let allRawLocales = globalArray.concat( // globalArray is low prio
-    hashValuesToArray(globalObject), // medium prio
-    explicitRawLocales // highest prio
-  )
+  let globalRawLocales = getGlobalRawLocales()
+  let allRawLocales = globalRawLocales.concat(explicitRawLocales)
   let rawLocaleMap = {
     en: RAW_EN_LOCALE // necessary?
   }
@@ -71,6 +69,7 @@ export function parseRawLocales(explicitRawLocales: RawLocale[]): RawLocaleInfo
   }
 }
 
+
 export function buildLocale(inputSingular: LocaleSingularArg, available: RawLocaleMap) {
   if (typeof inputSingular === 'object' && !Array.isArray(inputSingular)) {
     return parseLocale(
@@ -83,6 +82,7 @@ export function buildLocale(inputSingular: LocaleSingularArg, available: RawLoca
   }
 }
 
+
 function queryLocale(codeArg: LocaleCodeArg, available: RawLocaleMap): Locale {
   let codes = [].concat(codeArg || []) // will convert to array
   let raw = queryRawLocale(codes, available) || RAW_EN_LOCALE
@@ -90,6 +90,7 @@ function queryLocale(codeArg: LocaleCodeArg, available: RawLocaleMap): Locale {
   return parseLocale(codeArg, codes, raw)
 }
 
+
 function queryRawLocale(codes: string[], available: RawLocaleMap): RawLocale {
   for (let i = 0; i < codes.length; i++) {
     let parts = codes[i].toLocaleLowerCase().split('-')
@@ -105,6 +106,7 @@ function queryRawLocale(codes: string[], available: RawLocaleMap): RawLocale {
   return null
 }
 
+
 function parseLocale(codeArg: LocaleCodeArg, codes: string[], raw: RawLocale): Locale {
   let merged = mergeProps([ RAW_EN_LOCALE, raw ], [ 'buttonText' ])
 

+ 2 - 2
packages/core/src/formatting-api.ts

@@ -1,6 +1,6 @@
 import { DateEnv, DateInput } from './datelib/env'
 import { createFormatter } from './datelib/formatting'
-import { parseRawLocales, buildLocale } from './datelib/locale'
+import { organizeRawLocales, buildLocale } from './datelib/locale'
 import { globalDefaults } from './options'
 
 export function formatDate(dateInput: DateInput, settings = {}) {
@@ -40,7 +40,7 @@ export function formatRange(
 
 // TODO: more DRY and optimized
 function buildDateEnv(settings) {
-  let locale = buildLocale(settings.locale || 'en', parseRawLocales([]).map) // TODO: don't hardcode 'en' everywhere
+  let locale = buildLocale(settings.locale || 'en', organizeRawLocales([]).map) // TODO: don't hardcode 'en' everywhere
 
   // ensure required settings
   settings = {

+ 12 - 0
packages/core/src/global-locales.ts

@@ -0,0 +1,12 @@
+import { RawLocale } from './datelib/locale'
+import { hashValuesToArray } from './util/object'
+
+
+export function getGlobalRawLocales(): RawLocale[] {
+
+  // NOTE: make sure this global variable name is in-sync with the rollup bundle locale script
+  let globalStore = window['FullCalendarLocales']
+
+  return Array.isArray(globalStore) ? globalStore : // assigned by locales-all
+    hashValuesToArray(globalStore) // assigned by individual locale file(s)
+}

+ 5 - 7
packages/core/src/default-plugins.ts → packages/core/src/global-plugins.ts

@@ -6,16 +6,14 @@ import JsonFeedEventSourcePlugin from './event-sources/json-feed-event-source'
 import SimpleRecurrencePlugin from './structs/recurring-event-simple'
 import DefaultOptionChangeHandlers from './option-change-handlers'
 
-
-export let defaultPlugins: PluginDef[] = [
+/*
+this array is exposed on the root namespace so that UMD plugins can add to it.
+see the rollup-bundles script.
+*/
+export let globalPlugins: PluginDef[] = [
   ArrayEventSourcePlugin,
   FuncEventSourcePlugin,
   JsonFeedEventSourcePlugin,
   SimpleRecurrencePlugin,
   DefaultOptionChangeHandlers
 ]
-
-
-export function addDefaultPlugins(pluginDefs: PluginDef[]) { // TODO: redo
-  defaultPlugins.push(...pluginDefs)
-}

+ 1 - 1
packages/core/src/main.ts

@@ -185,4 +185,4 @@ export { default as RefMap } from './util/RefMap'
 export { getIsRtlScrollbarOnLeft } from './util/scrollbar-side'
 
 export { default as NowTimer, NowTimerCallback } from './NowTimer'
-export { addDefaultPlugins } from './default-plugins'
+export { globalPlugins } from './global-plugins'

+ 3 - 1
packages/core/src/util/RefMap.ts

@@ -1,3 +1,5 @@
+import { hashValuesToArray } from './object'
+
 
 export default class RefMap<RefType, OtherArgs extends any[] = []> {
 
@@ -36,7 +38,7 @@ export default class RefMap<RefType, OtherArgs extends any[] = []> {
 
 
   getCurrents() {
-    return Object.values(this.currentMap)
+    return hashValuesToArray(this.currentMap)
   }
 
 }

+ 1 - 1
packages/core/src/util/object.ts

@@ -89,7 +89,7 @@ export function arrayToHash(a): { [key: string]: true } {
 }
 
 
-export function hashValuesToArray(obj) {
+export function hashValuesToArray(obj) { // can't use Object.values yet because of no IE support
   let a = []
 
   for (let key in obj) {

+ 16 - 7
scripts/lib/rollup-bundles.js

@@ -61,6 +61,11 @@ function buildBundleConfig(pkgStruct, isDev) {
 }
 
 
+/*
+if the exports object has the globalPlugins property, that means the exports object
+is the same as the root fullcalendar namespace, which means we're executing as browser globals.
+in this case, register the plugin globally. also, undo attaching a useless `default` export.
+*/
 const OUTRO = `
 if (exports.globalPlugins) {
   exports.globalPlugins.push(exports.default)
@@ -113,14 +118,18 @@ function buildLocaleConfigs() { // for EACH
   let bundleStruct = getNonPremiumBundle()
   let localePaths = glob.sync('locales/*.js', { cwd: corePkg.tscDir })
 
-  return localePaths.map((localePath, i) => ({
-    input: path.join(corePkg.tscDir, localePath),
-    output: {
-      format: 'umd',
-      name: 'FullCalendarLocales.add',
-      file: path.join(bundleStruct.distDir, localePath)
+  return localePaths.map((localePath) => {
+    let localeCode = path.basename(localePath).replace(/\.[^.]*$/, '')
+
+    return {
+      input: path.join(corePkg.tscDir, localePath),
+      output: {
+        format: 'umd',
+        name: 'FullCalendarLocales.' + localeCode, // code isn't used, but needs to be unique
+        file: path.join(bundleStruct.distDir, localePath)
+      }
     }
-  }))
+  })
 }