Kaynağa Gözat

more adjustment to new context, move things out of Calendar

Adam Shaw 5 yıl önce
ebeveyn
işleme
d7791e7f86

+ 1 - 1
packages-premium

@@ -1 +1 @@
-Subproject commit 7ca7da6f52a6e19c8e93ab110f0da28faebf52f3
+Subproject commit c21b2c3e8a206bfa2794b3c8823f62bb3be0936f

+ 8 - 71
packages/core/src/Calendar.tsx

@@ -4,7 +4,7 @@ import { DateInput } from './datelib/env'
 import { DateMarker, startOfDay } from './datelib/marker'
 import { createFormatter } from './datelib/formatting'
 import { createDuration, DurationInput } from './datelib/duration'
-import { parseDateSpan, DateSpanInput, DateSpan, buildDateSpanApi, DateSpanApi, buildDatePointApi, DatePointApi } from './structs/date-span'
+import { parseDateSpan, DateSpanInput, DateSpan, DateSpanApi, DatePointApi } from './structs/date-span'
 import { DateRangeInput } from './datelib/date-range'
 import { EventSourceInput, parseEventSource } from './structs/event-source'
 import { EventInput, parseEvent } from './structs/event'
@@ -29,6 +29,7 @@ import { applyStyleProp } from './util/dom-manip'
 import { CalendarStateReducer } from './reducers/CalendarStateReducer'
 import { getNow } from './reducers/current-date'
 import { ReducerContext } from './reducers/ReducerContext'
+import { triggerDateSelect, triggerDateUnselect } from './calendar-utils'
 
 
 export interface DateClickApi extends DatePointApi {
@@ -42,13 +43,13 @@ export interface DateSelectionApi extends DateSpanApi {
   view: ViewApi
 }
 
-export type DatePointTransform = (dateSpan: DateSpan, calendar: Calendar) => any
-export type DateSpanTransform = (dateSpan: DateSpan, calendar: Calendar) => any
+export type DatePointTransform = (dateSpan: DateSpan, context: ReducerContext) => any
+export type DateSpanTransform = (dateSpan: DateSpan, context: ReducerContext) => any
 
 export type CalendarInteraction = { destroy() }
 export type CalendarInteractionClass = { new(context: ReducerContext): CalendarInteraction }
 
-export type OptionChangeHandler = (propValue: any, calendar: Calendar) => void
+export type OptionChangeHandler = (propValue: any, context: ReducerContext) => void
 export type OptionChangeHandlerMap = { [propName: string]: OptionChangeHandler }
 
 
@@ -293,7 +294,7 @@ export class Calendar {
 
       // special updates
       for (let name in specialUpdates) {
-        changeHandlers[name](specialUpdates[name], this)
+        changeHandlers[name](specialUpdates[name], this.state)
       }
     })
   }
@@ -322,12 +323,6 @@ export class Calendar {
   // -----------------------------------------------------------------------------------------------------------------
 
 
-  // Returns a boolean about whether the view is okay to instantiate at some point
-  isValidViewType(viewType: string): boolean {
-    return Boolean(this.state.viewSpecs[viewType])
-  }
-
-
   changeView(viewType: string, dateOrRange?: DateRangeInput | DateInput) {
     this.batchRendering(() => {
       this.unselect()
@@ -616,7 +611,7 @@ export class Calendar {
 
     if (selection) { // throw parse error otherwise?
       this.dispatch({ type: 'SELECT_DATES', selection })
-      this.triggerDateSelect(selection)
+      triggerDateSelect(selection, null, this.state)
     }
   }
 
@@ -625,66 +620,8 @@ export class Calendar {
   unselect(pev?: PointerDragEvent) {
     if (this.state.dateSelection) {
       this.dispatch({ type: 'UNSELECT_DATES' })
-      this.triggerDateUnselect(pev)
-    }
-  }
-
-
-  triggerDateSelect(selection: DateSpan, pev?: PointerDragEvent) {
-    const arg = {
-      ...this.buildDateSpanApi(selection),
-      jsEvent: pev ? pev.origEvent as MouseEvent : null, // Is this always a mouse event? See #4655
-      view: this.state.viewApi
-    }
-
-    this.emitter.trigger('select', arg)
-  }
-
-
-  triggerDateUnselect(pev?: PointerDragEvent) {
-    this.emitter.trigger('unselect', {
-      jsEvent: pev ? pev.origEvent : null,
-      view: this.state.viewApi
-    })
-  }
-
-
-  // TODO: receive pev?
-  triggerDateClick(dateSpan: DateSpan, dayEl: HTMLElement, view: ViewApi, ev: UIEvent) {
-    const arg = {
-      ...this.buildDatePointApi(dateSpan),
-      dayEl,
-      jsEvent: ev as MouseEvent, // Is this always a mouse event? See #4655
-      view
+      triggerDateUnselect(pev, this.state)
     }
-
-    this.emitter.trigger('dateClick', arg)
-  }
-
-
-  buildDatePointApi(dateSpan: DateSpan) {
-    let props = {} as DatePointApi
-
-    for (let transform of this.state.pluginHooks.datePointTransforms) {
-      __assign(props, transform(dateSpan, this))
-    }
-
-    __assign(props, buildDatePointApi(dateSpan, this.state.dateEnv))
-
-    return props
-  }
-
-
-  buildDateSpanApi(dateSpan: DateSpan) {
-    let props = {} as DateSpanApi
-
-    for (let transform of this.state.pluginHooks.dateSpanTransforms) {
-      __assign(props, transform(dateSpan, this))
-    }
-
-    __assign(props, buildDateSpanApi(dateSpan, this.state.dateEnv))
-
-    return props
   }
 
 

+ 63 - 0
packages/core/src/calendar-utils.ts

@@ -0,0 +1,63 @@
+import { PointerDragEvent } from './interactions/pointer'
+import { buildDateSpanApi, DateSpanApi, DatePointApi, DateSpan, buildDatePointApi } from './structs/date-span'
+import { ReducerContext } from './reducers/ReducerContext'
+import { __assign } from 'tslib'
+import { ViewApi } from './ViewApi'
+
+
+export function triggerDateSelect(selection: DateSpan, pev: PointerDragEvent | null, context: ReducerContext & { viewApi?: ViewApi }) {
+  const arg = {
+    ...buildDateSpanApiWithContext(selection, context),
+    jsEvent: pev ? pev.origEvent as MouseEvent : null, // Is this always a mouse event? See #4655
+    view: context.viewApi || context.calendar.view
+  }
+
+  context.emitter.trigger('select', arg)
+}
+
+
+export function triggerDateUnselect(pev: PointerDragEvent | null, context: ReducerContext & { viewApi?: ViewApi }) {
+  context.emitter.trigger('unselect', {
+    jsEvent: pev ? pev.origEvent : null,
+    view: context.viewApi || context.calendar.view
+  })
+}
+
+
+// TODO: receive pev?
+export function triggerDateClick(dateSpan: DateSpan, dayEl: HTMLElement, ev: UIEvent, context: ReducerContext & { viewApi?: ViewApi }) {
+  const arg = {
+    ...buildDatePointApiWithContext(dateSpan, context),
+    dayEl,
+    jsEvent: ev as MouseEvent, // Is this always a mouse event? See #4655
+    view: context.viewApi || context.calendar.view
+  }
+
+  context.emitter.trigger('dateClick', arg)
+}
+
+
+export function buildDatePointApiWithContext(dateSpan: DateSpan, context: ReducerContext) {
+  let props = {} as DatePointApi
+
+  for (let transform of context.pluginHooks.datePointTransforms) {
+    __assign(props, transform(dateSpan, context))
+  }
+
+  __assign(props, buildDatePointApi(dateSpan, context.dateEnv))
+
+  return props
+}
+
+
+export function buildDateSpanApiWithContext(dateSpan: DateSpan, context: ReducerContext) {
+  let props = {} as DateSpanApi
+
+  for (let transform of context.pluginHooks.dateSpanTransforms) {
+    __assign(props, transform(dateSpan, context))
+  }
+
+  __assign(props, buildDateSpanApi(dateSpan, context.dateEnv))
+
+  return props
+}

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

@@ -201,3 +201,4 @@ export { renderFill, BgEvent, BgEventProps } from './common/bg-fill'
 export { WeekNumberRoot, WeekNumberRootProps } from './common/WeekNumberRoot'
 
 export { ViewRoot, ViewRootProps } from './common/ViewRoot'
+export { triggerDateSelect, triggerDateClick, buildDatePointApiWithContext } from './calendar-utils'

+ 7 - 7
packages/core/src/option-change-handlers.ts

@@ -1,12 +1,12 @@
 import { createPlugin } from './plugin-system'
-import { Calendar } from './main'
 import { hashValuesToArray } from './util/object'
 import { EventSource } from './structs/event-source'
+import { ReducerContext } from 'fullcalendar'
 
 export const changeHandlerPlugin = createPlugin({
   optionChangeHandlers: {
-    events(events, calendar) {
-      handleEventSources([ events ], calendar)
+    events(events, context) {
+      handleEventSources([ events ], context)
     },
     eventSources: handleEventSources
   }
@@ -15,8 +15,8 @@ export const changeHandlerPlugin = createPlugin({
 /*
 BUG: if `event` was supplied, all previously-given `eventSources` will be wiped out
 */
-function handleEventSources(inputs, calendar: Calendar) {
-  let unfoundSources: EventSource[] = hashValuesToArray(calendar.state.eventSources)
+function handleEventSources(inputs, context: ReducerContext) {
+  let unfoundSources: EventSource[] = hashValuesToArray(context.getCurrentState().eventSources)
   let newInputs = []
 
   for (let input of inputs) {
@@ -36,13 +36,13 @@ function handleEventSources(inputs, calendar: Calendar) {
   }
 
   for (let unfoundSource of unfoundSources) {
-    calendar.dispatch({
+    context.dispatch({
       type: 'REMOVE_EVENT_SOURCE',
       sourceId: unfoundSource.sourceId
     })
   }
 
   for (let newInput of newInputs) {
-    calendar.addEventSource(newInput)
+    context.calendar.addEventSource(newInput)
   }
 }

+ 3 - 2
packages/core/src/validation.ts

@@ -9,6 +9,7 @@ import { EventInteractionState } from './interactions/event-interaction-state'
 import { SplittableProps } from './component/event-splitting'
 import { mapHash } from './util/object'
 import { ReducerContext } from './reducers/ReducerContext'
+import { buildDateSpanApiWithContext } from './calendar-utils'
 
 // TODO: rename to "criteria" ?
 export type ConstraintInput = 'businessHours' | string | EventInput | EventInput[]
@@ -154,7 +155,7 @@ function isInteractionPropsValid(state: SplittableProps, context: ReducerContext
       }
 
       if (!subjectAllow(
-        calendar.buildDateSpanApi(subjectDateSpan),
+        buildDateSpanApiWithContext(subjectDateSpan, context),
         eventApi
       )) {
         return false
@@ -217,7 +218,7 @@ function isDateSelectionPropsValid(state: SplittableProps, context: ReducerConte
     let fullDateSpan = { ...dateSpanMeta, ...selection }
 
     if (!selectionAllow(
-      context.calendar.buildDateSpanApi(fullDateSpan),
+      buildDateSpanApiWithContext(fullDateSpan, context),
       null
     )) {
       return false

+ 3 - 2
packages/interaction/src/interactions-external/ExternalElementDragging.ts

@@ -14,7 +14,8 @@ import {
   isInteractionValid,
   ElementDragging,
   ViewApi,
-  ReducerContext
+  ReducerContext,
+  buildDatePointApiWithContext
 } from '@fullcalendar/core'
 import { HitDragging } from '../interactions/HitDragging'
 import { __assign } from 'tslib'
@@ -130,7 +131,7 @@ export class ExternalElementDragging {
       let finalView = finalHit.component.context.viewApi
       let dragMeta = this.dragMeta!
       let arg = {
-        ...receivingContext.calendar.buildDatePointApi(finalHit.dateSpan),
+        ...buildDatePointApiWithContext(finalHit.dateSpan, receivingContext),
         draggedEl: pev.subjectEl as HTMLElement,
         jsEvent: pev.origEvent as MouseEvent, // Is this always a mouse event? See #4655
         view: finalView

+ 4 - 5
packages/interaction/src/interactions/DateClicking.ts

@@ -1,4 +1,4 @@
-import { PointerDragEvent, Interaction, InteractionSettings, interactionSettingsToStore } from '@fullcalendar/core'
+import { PointerDragEvent, Interaction, InteractionSettings, interactionSettingsToStore, triggerDateClick } from '@fullcalendar/core'
 import { FeaturefulElementDragging } from '../dnd/FeaturefulElementDragging'
 import { HitDragging, isHitsEqual } from './HitDragging'
 
@@ -39,18 +39,17 @@ export class DateClicking extends Interaction {
   // won't even fire if moving was ignored
   handleDragEnd = (ev: PointerDragEvent) => {
     let { component } = this
-    let { calendar, viewApi } = component.context
     let { pointer } = this.dragging
 
     if (!pointer.wasTouchScroll) {
       let { initialHit, finalHit } = this.hitDragging
 
       if (initialHit && finalHit && isHitsEqual(initialHit, finalHit)) {
-        calendar.triggerDateClick(
+        triggerDateClick(
           initialHit.dateSpan,
           initialHit.dayEl,
-          viewApi,
-          ev.origEvent
+          ev.origEvent,
+          component.context
         )
       }
     }

+ 3 - 2
packages/interaction/src/interactions/DateSelecting.ts

@@ -1,7 +1,8 @@
 import {
   compareNumbers, enableCursor, disableCursor, DateComponent, Hit,
   DateSpan, PointerDragEvent, dateSelectionJoinTransformer,
-  Interaction, InteractionSettings, interactionSettingsToStore
+  Interaction, InteractionSettings, interactionSettingsToStore,
+  triggerDateSelect
 } from '@fullcalendar/core'
 import { HitDragging } from './HitDragging'
 import { FeaturefulElementDragging } from '../dnd/FeaturefulElementDragging'
@@ -95,7 +96,7 @@ export class DateSelecting extends Interaction {
     if (this.dragSelection) {
 
       // selection is already rendered, so just need to report selection
-      this.component.context.calendar.triggerDateSelect(this.dragSelection, pev)
+      triggerDateSelect(this.dragSelection, pev, this.component.context)
 
       this.dragSelection = null
     }

+ 3 - 2
packages/interaction/src/interactions/EventDragging.ts

@@ -12,7 +12,8 @@ import {
   eventDragMutationMassager,
   Interaction, InteractionSettings, interactionSettingsStore,
   EventDropTransformers,
-  ReducerContext
+  ReducerContext,
+  buildDatePointApiWithContext
 } from '@fullcalendar/core'
 import { HitDragging, isHitsEqual } from './HitDragging'
 import { FeaturefulElementDragging } from '../dnd/FeaturefulElementDragging'
@@ -300,7 +301,7 @@ export class EventDragging extends Interaction { // TODO: rename to EventSelecti
           }
 
           receivingContext.emitter.trigger('drop', {
-            ...receivingContext.calendar.buildDatePointApi(finalHit.dateSpan),
+            ...buildDatePointApiWithContext(finalHit.dateSpan, receivingContext),
             draggedEl: ev.subjectEl as HTMLElement,
             jsEvent: ev.origEvent as MouseEvent, // Is this always a mouse event? See #4655
             view: finalHit.component.context.viewApi