refactor: extract date.js, event-factory.js, split.js; use shared date helpers in recur/organize
- date.js: single source of truth for ISO date formatting/arithmetic (toISODate, parseISODate, addDays, daysBetween, todayISO, nowCompactTimestamp) - event-factory.js: createEvent/cloneEvent (structuredClone with JSON fallback) - split.js: splitRecurringEvent pure function using intervalDays from recur.js - recur.js: replaced local toISO() and inline Date math with date.js helpers - organize.js: replaced inline date formatting and gap math with date.js helpers, extracted buildGrid() helper and byDate comparator
This commit is contained in:
41
src/lib/event-factory.js
Normal file
41
src/lib/event-factory.js
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* Event model factory and helpers.
|
||||
*
|
||||
* The EventModel is the plain-JS shape used throughout the app (see
|
||||
* docs/superpowers/specs/... §4.2). This module centralizes creation
|
||||
* and cloning so that useCalendar.js stays focused on state management.
|
||||
*/
|
||||
import { todayISO, nowCompactTimestamp } from './date.js'
|
||||
|
||||
/**
|
||||
* Create a new single-occurrence event with sensible defaults.
|
||||
* @param {object} [overrides] - partial fields to override
|
||||
* @returns {object} a fresh EventModel
|
||||
*/
|
||||
export function createEvent(overrides = {}) {
|
||||
return {
|
||||
uid: crypto.randomUUID(),
|
||||
summary: '新日程',
|
||||
location: '',
|
||||
description: '',
|
||||
dtstartDate: todayISO(),
|
||||
dtstartTime: '09:00',
|
||||
dtendTime: '10:00',
|
||||
tzid: 'UTC',
|
||||
rrule: null,
|
||||
exdates: [],
|
||||
_raw: { dtstamp: nowCompactTimestamp() },
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deep-clone an event via structuredClone (available in modern browsers
|
||||
* and Node 17+). Falls back to JSON round-trip for older environments.
|
||||
*/
|
||||
export function cloneEvent(ev) {
|
||||
if (typeof structuredClone === 'function') {
|
||||
return structuredClone(ev)
|
||||
}
|
||||
return JSON.parse(JSON.stringify(ev))
|
||||
}
|
||||
Reference in New Issue
Block a user