refactor: 移除重做/应用按钮, 自动应用更改, 删除死代码, 修复性能问题

UI 变更:
- 移除重做按钮及 redo/canRedo/redoStack 相关逻辑 (useCalendar, HeaderBar)
- 移除应用更改按钮, EventDetail 改为 watch form 自动应用 (deep watch)
- 用 syncing 标志防止 syncForm 回写时触发自动应用的循环

死代码清理:
- weekday.js: 移除未使用的 BYDAY_TO_INDEX 和 dowLabel
- date.js: 移除未使用的 toHHMM
- App.vue: 移除未使用的 .empty CSS
- DropZone.vue: 移除空的 .icon 容器及 CSS
- test/weekday.test.js: 移除对应死代码的 2 个测试用例

性能修复:
- WeekPreview: layoutEvents 从模板内联调用改为 computed (laidOutByDate),
  避免每次渲染重新计算布局
- MonthPreview: occByDate 过滤为仅网格内 42 天, 避免展开全部 occurrence

代码质量:
- useCalendar: prevMonth/nextMonth 合并为 shiftMonth(delta), 消除重复
- WeekPreview/MonthPreview: 移除未使用导入 (toISODate, toDate)
This commit is contained in:
zikai
2026-07-22 04:18:05 +00:00
parent add471eb44
commit 1c6ecf9d86
10 changed files with 111 additions and 136 deletions

View File

@@ -3,7 +3,7 @@ import { parseICS, serializeICS } from '../lib/ical-io.js'
import { organize as organizeEvents } from '../lib/organize.js'
import { createEvent, cloneEvent } from '../lib/event-factory.js'
import { splitRecurringEvent } from '../lib/split.js'
import { todayISO, addDays, startOfMonth, startOfWeek, parseISODate, pad2 } from '../lib/date.js'
import { todayISO, addDays, startOfMonth, parseISODate, pad2 } from '../lib/date.js'
const MAX_HISTORY = 50
@@ -13,7 +13,6 @@ const state = reactive({
events: [],
selectedUid: null,
history: [],
redoStack: [],
fileName: null,
dirty: false,
viewMode: 'edit', // 'edit' | 'month' | 'week'
@@ -34,7 +33,6 @@ function snapshot() {
function pushHistory() {
state.history.push(snapshot())
if (state.history.length > MAX_HISTORY) state.history.shift()
state.redoStack = []
}
function restore(snap) {
@@ -44,7 +42,7 @@ function restore(snap) {
}
// ------------------------------------------------------------------ //
// 操作
// 文件加载
// ------------------------------------------------------------------ //
function loadFile(file) {
return file.text().then((text) => {
@@ -53,12 +51,14 @@ function loadFile(file) {
state.events = parsed.events
state.selectedUid = null
state.history = []
state.redoStack = []
state.fileName = file.name
state.dirty = false
})
}
// ------------------------------------------------------------------ //
// 事件操作
// ------------------------------------------------------------------ //
function selectEvent(uid) {
state.selectedUid = uid
}
@@ -72,6 +72,10 @@ function organize() {
return result.stats
}
/**
* 更新指定事件的字段 (自动记录历史)。
* 供 EventDetail 的自动应用 (watch) 调用。
*/
function updateEvent(uid, patch) {
pushHistory()
const ev = state.events.find((e) => e.uid === uid)
@@ -127,16 +131,12 @@ function toggleOccurrence(uid, date) {
function undo() {
if (state.history.length === 0) return
state.redoStack.push(snapshot())
restore(state.history.pop())
}
function redo() {
if (state.redoStack.length === 0) return
state.history.push(snapshot())
restore(state.redoStack.pop())
}
// ------------------------------------------------------------------ //
// 序列化
// ------------------------------------------------------------------ //
function serialize() {
return serializeICS({ meta: state.meta, events: state.events })
}
@@ -152,16 +152,18 @@ function setViewMode(mode) {
state.viewMode = mode
}
function prevMonth() {
function shiftMonth(delta) {
const { year, month } = parseISODate(startOfMonth(state.previewDate))
const d = new Date(year, month - 2, 1) // month-2 = 上个月 (0 基)
const d = new Date(year, month - 1 + delta, 1)
state.previewDate = `${d.getFullYear()}-${pad2(d.getMonth() + 1)}-01`
}
function prevMonth() {
shiftMonth(-1)
}
function nextMonth() {
const { year, month } = parseISODate(startOfMonth(state.previewDate))
const d = new Date(year, month, 1) // month = 下个月 (0 基)
state.previewDate = `${d.getFullYear()}-${pad2(d.getMonth() + 1)}-01`
shiftMonth(1)
}
function prevWeek() {
@@ -180,7 +182,6 @@ const selectedEvent = computed(() =>
)
const canUndo = computed(() => state.history.length > 0)
const canRedo = computed(() => state.redoStack.length > 0)
const isLoaded = computed(() => state.events.length > 0)
const stats = computed(() => {
const nRecur = state.events.filter((e) => e.rrule !== null).length
@@ -192,7 +193,6 @@ export function useCalendar() {
state,
selectedEvent,
canUndo,
canRedo,
isLoaded,
stats,
loadFile,
@@ -204,7 +204,6 @@ export function useCalendar() {
splitEvent,
toggleOccurrence,
undo,
redo,
serialize,
markSaved,
setViewMode,