refactor: high-cohesion low-coupling cleanup across store and components

ical-io.js:
- Import pad2/parseISODate from date.js (remove local duplicates)
- Rename ICAL.Time helpers to icalTimeTo* for clarity

useCalendar.js:
- Delegate addEvent to createEvent() factory
- Delegate splitEvent to splitRecurringEvent() pure function
- Use cloneEvent() instead of JSON.parse(JSON.stringify())
- Add selectEvent() action (components no longer mutate state.selectedUid directly)

Components:
- EventList: use selectEvent action + toDate() from date.js
- HeaderBar: destructure loadFile at top level (was re-calling useCalendar in handler)
- EventDetail: import DOW from weekday.js (was inlining the array)
- OccurrenceGrid: use toDate() from date.js
This commit is contained in:
timeTable2 dev
2026-07-13 20:06:00 +08:00
parent 02cfdda900
commit b352c4e116
6 changed files with 32 additions and 74 deletions

View File

@@ -1,7 +1,7 @@
<script setup>
import { ref, watch } from 'vue'
import { useCalendar } from '../composables/useCalendar.js'
import { DOW_EN } from '../lib/weekday.js'
import { DOW, DOW_EN } from '../lib/weekday.js'
import OccurrenceGrid from './OccurrenceGrid.vue'
const { selectedEvent, updateEvent, deleteEvent, splitEvent, toggleOccurrence } = useCalendar()
@@ -142,7 +142,7 @@ function onToggleOcc(date) {
<label>星期 (BYDAY)</label>
<select v-model="form.byday">
<option v-for="(d, i) in DOW_EN" :key="d" :value="d">
{{ ['周日','周一','周二','周三','周四','周五','周六'][i] }}
{{ DOW[i] }}
</option>
</select>
</div>

View File

@@ -1,15 +1,12 @@
<script setup>
import { useCalendar } from '../composables/useCalendar.js'
import { DOW } from '../lib/weekday.js'
import { toDate } from '../lib/date.js'
const { state, stats, addEvent } = useCalendar()
function selectEvent(uid) {
state.selectedUid = uid
}
const { state, stats, addEvent, selectEvent } = useCalendar()
function dowFor(ev) {
return DOW[new Date(ev.dtstartDate + 'T00:00:00').getDay()]
return DOW[toDate(ev.dtstartDate).getDay()]
}
</script>

View File

@@ -2,7 +2,7 @@
import { useCalendar } from '../composables/useCalendar.js'
import { ref } from 'vue'
const { state, canUndo, canRedo, organize, undo, redo, serialize, markSaved } = useCalendar()
const { state, canUndo, canRedo, organize, undo, redo, serialize, markSaved, loadFile } = useCalendar()
const fileInput = ref(null)
function onOrganize() {
@@ -19,7 +19,6 @@ function onImport() {
function onFilePicked(e) {
const file = e.target.files[0]
if (!file) return
const { loadFile } = useCalendar()
loadFile(file).catch((err) => alert('解析失败:' + (err.message || err)))
e.target.value = ''
}

View File

@@ -1,6 +1,7 @@
<script setup>
import { computed } from 'vue'
import { DOW } from '../lib/weekday.js'
import { toDate } from '../lib/date.js'
import { expandOccurrences } from '../lib/recur.js'
const props = defineProps({
@@ -11,7 +12,7 @@ const emit = defineEmits(['toggle'])
const occurrences = computed(() => expandOccurrences(props.event))
function dowFor(dateStr) {
return DOW[new Date(dateStr + 'T00:00:00').getDay()]
return DOW[toDate(dateStr).getDay()]
}
</script>