@sunr/dsh-reminder 0.0.0-stage → 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +188 -2
- package/bundles/cordis.patch.yml +5 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +53 -4
- package/src/almanac.js +80 -0
- package/src/client.js +984 -0
- package/src/export.js +111 -0
- package/src/index.js +296 -0
- package/src/lunar.js +180 -0
- package/src/remind.js +40 -0
- package/src/repeat.js +171 -0
- package/src/retrograde.js +67 -0
- package/src/service.js +170 -0
- package/src/store.js +116 -0
- package/src/tools.js +512 -0
- package/src/view.js +107 -0
package/src/client.js
ADDED
|
@@ -0,0 +1,984 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 浏览器面:手写的 CJS 工厂,不经过构建。
|
|
3
|
+
*
|
|
4
|
+
* 形状必须与 dsh.client 包的 ./client 导出一致:
|
|
5
|
+
* window.__ModuleLoader__.load({ id, factory: (require) => module.exports })
|
|
6
|
+
* 只有 react / react-dom / ui-primitives 是平台外置的,这里只用到 react。
|
|
7
|
+
*/
|
|
8
|
+
window.__ModuleLoader__.load({
|
|
9
|
+
id: '@sunr/dsh-reminder',
|
|
10
|
+
factory: (require) => {
|
|
11
|
+
var module = { exports: {} }
|
|
12
|
+
var exports = module.exports
|
|
13
|
+
|
|
14
|
+
const React = require('react')
|
|
15
|
+
const h = React.createElement
|
|
16
|
+
|
|
17
|
+
const ROUTE = '/api/dsh-reminder/items'
|
|
18
|
+
const ACCENT = 'var(--dsw-accent, #4f6ef7)'
|
|
19
|
+
const BORDER = 'var(--dsw-border, rgba(127,127,127,.25))'
|
|
20
|
+
const MUTED = 'var(--dsw-text-secondary, #888)'
|
|
21
|
+
const DANGER = 'var(--dsw-danger, #d33)'
|
|
22
|
+
|
|
23
|
+
const KIND_OPTIONS = [['anniversary', '纪念日'], ['todo', '待办'], ['memo', '备忘']]
|
|
24
|
+
const REPEAT_OPTIONS = [
|
|
25
|
+
['none', '不重复'], ['yearly', '每年'], ['monthly', '每月'], ['weekly', '每周'],
|
|
26
|
+
['daily', '每天'], ['biweeklyOdd', '单周'], ['biweeklyEven', '双周'], ['customDays', '每 N 天'],
|
|
27
|
+
]
|
|
28
|
+
const WEEKDAYS = ['日', '一', '二', '三', '四', '五', '六']
|
|
29
|
+
const PALETTE = ['#4f6ef7', '#e5484d', '#f5a524', '#30a46c', '#8e4ec6', '#0ea5e9', '#d6409f', '#f76b15']
|
|
30
|
+
|
|
31
|
+
function colorOf(id) {
|
|
32
|
+
let hash = 0
|
|
33
|
+
for (let index = 0; index < id.length; index += 1) hash = (hash * 31 + id.charCodeAt(index)) >>> 0
|
|
34
|
+
return PALETTE[hash % PALETTE.length]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const styles = {
|
|
38
|
+
wrap: { padding: '20px 24px 32px', overflow: 'auto', height: '100%', boxSizing: 'border-box' },
|
|
39
|
+
header: { display: 'flex', alignItems: 'center', gap: 10, margin: '0 0 12px', flexWrap: 'wrap' },
|
|
40
|
+
title: { margin: 0, fontSize: 18, fontWeight: 600, marginRight: 4 },
|
|
41
|
+
table: { width: '100%', borderCollapse: 'collapse', fontSize: 13 },
|
|
42
|
+
th: { textAlign: 'left', padding: '8px 10px', color: MUTED, fontWeight: 500, borderBottom: `1px solid ${BORDER}` },
|
|
43
|
+
td: { padding: '8px 10px', borderBottom: `1px solid ${BORDER}`, verticalAlign: 'baseline' },
|
|
44
|
+
badge: { display: 'inline-block', padding: '1px 6px', marginLeft: 6, borderRadius: 4, fontSize: 11, background: 'rgba(127,127,127,.14)', color: MUTED },
|
|
45
|
+
soon: { color: ACCENT, fontWeight: 600 },
|
|
46
|
+
overdue: { color: DANGER, fontWeight: 600 },
|
|
47
|
+
muted: { padding: 16, color: MUTED, fontSize: 13 },
|
|
48
|
+
error: { padding: '10px 12px', margin: '0 0 12px', color: DANGER, fontSize: 12, border: `1px solid ${DANGER}`, borderRadius: 6 },
|
|
49
|
+
button: { border: `1px solid ${BORDER}`, background: 'transparent', color: 'inherit', borderRadius: 6, padding: '4px 10px', fontSize: 12, cursor: 'pointer' },
|
|
50
|
+
primary: { border: 'none', background: ACCENT, color: '#fff', borderRadius: 6, padding: '5px 12px', fontSize: 12, cursor: 'pointer' },
|
|
51
|
+
linkButton: { border: 'none', background: 'transparent', color: MUTED, fontSize: 12, cursor: 'pointer', padding: '2px 6px' },
|
|
52
|
+
form: { border: `1px solid ${BORDER}`, borderRadius: 10, padding: 16, margin: '0 0 16px', display: 'grid', gap: 10, maxWidth: 640 },
|
|
53
|
+
field: { display: 'grid', gap: 4 },
|
|
54
|
+
fieldLabel: { fontSize: 12, color: MUTED },
|
|
55
|
+
input: { padding: '6px 8px', borderRadius: 6, border: `1px solid ${BORDER}`, background: 'transparent', color: 'inherit', fontSize: 13, fontFamily: 'inherit' },
|
|
56
|
+
row: { display: 'flex', gap: 8, alignItems: 'center' },
|
|
57
|
+
chip: { border: `1px solid ${BORDER}`, background: 'transparent', color: 'inherit', borderRadius: 999, padding: '2px 9px', fontSize: 12, cursor: 'pointer' },
|
|
58
|
+
chipOn: { border: 'none', background: ACCENT, color: '#fff', borderRadius: 999, padding: '2px 9px', fontSize: 12, cursor: 'pointer' },
|
|
59
|
+
actions: { display: 'flex', gap: 4, justifyContent: 'flex-end' },
|
|
60
|
+
calBar: { display: 'flex', alignItems: 'center', gap: 10, margin: '0 0 8px' },
|
|
61
|
+
calTitle: { fontSize: 14, fontWeight: 600, minWidth: 110 },
|
|
62
|
+
grid: { display: 'grid', gridTemplateColumns: 'repeat(7, minmax(0, 1fr))', gap: 4, maxWidth: 820 },
|
|
63
|
+
weekGrid: { display: 'grid', gridTemplateColumns: '46px repeat(7, minmax(0, 1fr))', gap: 4, maxWidth: 720 },
|
|
64
|
+
calRow: { display: 'flex', gap: 12, alignItems: 'stretch', flexWrap: 'wrap', maxWidth: 1080 },
|
|
65
|
+
calCol: { flex: '2 1 540px', minWidth: 0 },
|
|
66
|
+
sideCol: { flex: '1 1 260px', minWidth: 0, display: 'flex', flexDirection: 'column', gap: 10, alignSelf: 'stretch' },
|
|
67
|
+
retroMark: { position: 'absolute', top: 3, right: 4, fontSize: 9, color: '#e5484d', fontWeight: 700 },
|
|
68
|
+
cardSide: { flex: 'none' },
|
|
69
|
+
strip: { display: 'flex', alignItems: 'baseline', gap: 10, padding: '5px 10px', border: `1px solid ${BORDER}`, borderRadius: 8, marginBottom: 8, maxWidth: 720 },
|
|
70
|
+
compactHead: { display: 'flex', alignItems: 'baseline', gap: 8, marginBottom: 6 },
|
|
71
|
+
compactScroll: { maxHeight: 300, overflow: 'auto', display: 'grid', gap: 2 },
|
|
72
|
+
compactRow: { display: 'flex', alignItems: 'center', gap: 6, padding: '4px 0', borderTop: `1px solid ${BORDER}`, minWidth: 0 },
|
|
73
|
+
compactTitle: { fontSize: 12, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' },
|
|
74
|
+
compactMeta: { fontSize: 10, color: MUTED, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' },
|
|
75
|
+
tiny: { border: 'none', background: 'transparent', color: MUTED, fontSize: 11, cursor: 'pointer', padding: '0 2px' },
|
|
76
|
+
weekLabel: { display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 11, fontWeight: 600, borderRight: `1px solid ${BORDER}` },
|
|
77
|
+
dow: { fontSize: 11, color: MUTED, textAlign: 'center', padding: '2px 0' },
|
|
78
|
+
cell: { minHeight: 62, border: `1px solid ${BORDER}`, borderRadius: 6, padding: '4px 6px 5px', display: 'flex', flexDirection: 'column', gap: 2, cursor: 'pointer', boxSizing: 'border-box', overflow: 'hidden' },
|
|
79
|
+
cellWeekend: { background: 'rgba(229,72,77,.05)' },
|
|
80
|
+
cellToday: { borderColor: ACCENT, background: 'rgba(79,110,247,.08)' },
|
|
81
|
+
cellSelected: { boxShadow: `0 0 0 1.5px ${ACCENT} inset` },
|
|
82
|
+
dayNum: { fontSize: 12, color: 'inherit' },
|
|
83
|
+
lunarDay: { fontSize: 10, color: MUTED, whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' },
|
|
84
|
+
jieQi: { fontSize: 10, color: '#e5484d', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' },
|
|
85
|
+
dots: { display: 'flex', gap: 3, flexWrap: 'wrap', marginTop: 'auto' },
|
|
86
|
+
dot: { width: 6, height: 6, borderRadius: 3, display: 'inline-block' },
|
|
87
|
+
more: { fontSize: 10, color: MUTED },
|
|
88
|
+
almanac: { marginTop: 10, border: `1px solid ${BORDER}`, borderRadius: 10, padding: '10px 14px', maxWidth: 820, display: 'grid', gap: 6 },
|
|
89
|
+
cardsRow: { display: 'flex', gap: 12, flexWrap: 'wrap', marginTop: 10, maxWidth: 860 },
|
|
90
|
+
card: { flex: '1 1 320px', border: `1px solid ${BORDER}`, borderRadius: 10, padding: '8px 12px', display: 'grid', gap: 3, boxSizing: 'border-box', minWidth: 0, alignContent: 'start' },
|
|
91
|
+
cardTitle: { fontSize: 12, fontWeight: 600, color: MUTED },
|
|
92
|
+
infoRow: { display: 'flex', gap: 8, alignItems: 'baseline', fontSize: 12, lineHeight: 1.45 },
|
|
93
|
+
infoLabel: { color: MUTED, minWidth: 40 },
|
|
94
|
+
infoValue: { flex: 1, minWidth: 0 },
|
|
95
|
+
zodiacSign: { fontSize: 16, fontWeight: 600, display: 'flex', alignItems: 'baseline', gap: 8 },
|
|
96
|
+
zodiacElement: { fontSize: 11, color: ACCENT, fontWeight: 500 },
|
|
97
|
+
almanacHead: { display: 'flex', gap: 10, alignItems: 'baseline', flexWrap: 'wrap' },
|
|
98
|
+
almanacDate: { fontSize: 13, fontWeight: 600 },
|
|
99
|
+
almanacMeta: { fontSize: 12, color: MUTED },
|
|
100
|
+
yiJi: { display: 'flex', gap: 8, alignItems: 'baseline', flexWrap: 'wrap', fontSize: 12 },
|
|
101
|
+
yiTag: { color: '#30a46c', fontWeight: 600 },
|
|
102
|
+
jiTag: { color: '#e5484d', fontWeight: 600 },
|
|
103
|
+
pengZu: { fontSize: 11, color: MUTED },
|
|
104
|
+
listBar: { display: 'flex', alignItems: 'baseline', gap: 10, margin: '4px 0 8px', maxWidth: 820 },
|
|
105
|
+
separator: { height: 1, background: BORDER, margin: '22px 0 18px', maxWidth: 860 },
|
|
106
|
+
dayPanel: { marginTop: 12, border: `1px solid ${BORDER}`, borderRadius: 10, padding: '10px 14px', maxWidth: 820 },
|
|
107
|
+
dayHead: { display: 'flex', alignItems: 'baseline', gap: 10, marginBottom: 6 },
|
|
108
|
+
dayTitle: { fontSize: 13, fontWeight: 600 },
|
|
109
|
+
dayItem: { display: 'flex', alignItems: 'center', gap: 6, padding: '5px 0', fontSize: 13, borderTop: `1px solid ${BORDER}` },
|
|
110
|
+
doneRow: { opacity: 0.5 },
|
|
111
|
+
doneTitle: { textDecoration: 'line-through' },
|
|
112
|
+
toast: { position: 'fixed', right: 24, bottom: 24, display: 'flex', alignItems: 'center', gap: 12, padding: '10px 14px', borderRadius: 8, background: 'var(--dsw-surface, #222)', color: 'var(--dsw-text, #fff)', boxShadow: '0 6px 20px rgba(0,0,0,.25)', fontSize: 12, zIndex: 20 },
|
|
113
|
+
search: { padding: '4px 8px', borderRadius: 6, border: `1px solid ${BORDER}`, background: 'transparent', color: 'inherit', fontSize: 12, width: 160 },
|
|
114
|
+
cellCompact: { minHeight: 44, padding: '3px 4px' },
|
|
115
|
+
wrapCompact: { padding: '14px 12px 24px' },
|
|
116
|
+
zone: { padding: '4px 6px', borderRadius: 6, border: `1px solid ${BORDER}`, background: 'transparent', color: MUTED, fontSize: 12 },
|
|
117
|
+
listTitle: { fontSize: 13, fontWeight: 600 },
|
|
118
|
+
colorBar: { width: 3, height: 14, borderRadius: 2, display: 'inline-block', marginRight: 8, verticalAlign: '-2px' },
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function api(action, payload) {
|
|
122
|
+
return fetch(ROUTE, {
|
|
123
|
+
method: 'POST',
|
|
124
|
+
headers: { 'content-type': 'application/json' },
|
|
125
|
+
body: JSON.stringify(Object.assign({ action }, payload || {})),
|
|
126
|
+
}).then(function (response) {
|
|
127
|
+
return response.json().catch(function () { return null }).then(function (data) {
|
|
128
|
+
if (!data || !data.ok) {
|
|
129
|
+
const message = (data && data.error && data.error.message) || ('HTTP ' + response.status)
|
|
130
|
+
throw new Error(message)
|
|
131
|
+
}
|
|
132
|
+
return data
|
|
133
|
+
})
|
|
134
|
+
})
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function when(row) {
|
|
138
|
+
if (row.inDays === 0) return '今天'
|
|
139
|
+
if (row.inDays > 0) return row.inDays === 1 ? '明天' : row.inDays + ' 天后'
|
|
140
|
+
return '已过 ' + (-row.inDays) + ' 天'
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function weekLabel(weekNo) {
|
|
144
|
+
return weekNo % 2 === 1 ? '单周' : '双周'
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function kindLabel(kind) {
|
|
148
|
+
for (const entry of KIND_OPTIONS) if (entry[0] === kind) return entry[1]
|
|
149
|
+
return kind
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function emptyForm(today) {
|
|
153
|
+
return {
|
|
154
|
+
title: '', kind: 'anniversary', calendar: 'gregorian', date: today,
|
|
155
|
+
lunarMonth: 1, lunarDay: 1, lunarLeap: false,
|
|
156
|
+
repeat: 'yearly', every: 3, weekdayMask: 0, tags: '', advance: '', remindAt: '',
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function formFromRow(row) {
|
|
161
|
+
return {
|
|
162
|
+
title: row.title, kind: row.kind, calendar: row.calendar,
|
|
163
|
+
date: row.anchor || row.date,
|
|
164
|
+
lunarMonth: row.lunarMonth || 1, lunarDay: row.lunarDay || 1, lunarLeap: row.lunarLeap,
|
|
165
|
+
repeat: row.repeat, every: row.every || 3, weekdayMask: row.weekdayMask || 0,
|
|
166
|
+
tags: (row.tags || []).join(', '),
|
|
167
|
+
advance: (row.advance || []).join(','), remindAt: row.remindAt || '',
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function Field(props) {
|
|
172
|
+
return h('label', { style: styles.field }, [
|
|
173
|
+
h('span', { key: 'label', style: styles.fieldLabel }, props.label),
|
|
174
|
+
props.children,
|
|
175
|
+
])
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function Form(props) {
|
|
179
|
+
const form = props.form
|
|
180
|
+
const setForm = props.setForm
|
|
181
|
+
const set = (patch) => setForm(Object.assign({}, form, patch))
|
|
182
|
+
const lunar = form.calendar === 'lunar'
|
|
183
|
+
|
|
184
|
+
const children = [
|
|
185
|
+
h(Field, { key: 'title', label: '标题' },
|
|
186
|
+
h('input', {
|
|
187
|
+
style: styles.input, value: form.title, autoFocus: true,
|
|
188
|
+
placeholder: '比如:妈妈的生日',
|
|
189
|
+
onChange: (event) => set({ title: event.target.value }),
|
|
190
|
+
})),
|
|
191
|
+
|
|
192
|
+
h('div', { key: 'kind', style: styles.row }, [
|
|
193
|
+
h('span', { key: 'l', style: styles.fieldLabel }, '类型'),
|
|
194
|
+
...KIND_OPTIONS.map((entry) => h('button', {
|
|
195
|
+
key: entry[0],
|
|
196
|
+
type: 'button',
|
|
197
|
+
style: form.kind === entry[0] ? styles.chipOn : styles.chip,
|
|
198
|
+
onClick: () => set({ kind: entry[0], repeat: entry[0] === 'anniversary' ? 'yearly' : form.repeat }),
|
|
199
|
+
}, entry[1])),
|
|
200
|
+
]),
|
|
201
|
+
|
|
202
|
+
h('div', { key: 'calendar', style: styles.row }, [
|
|
203
|
+
h('span', { key: 'l', style: styles.fieldLabel }, '历法'),
|
|
204
|
+
...['gregorian', 'lunar'].map((value) => h('button', {
|
|
205
|
+
key: value,
|
|
206
|
+
type: 'button',
|
|
207
|
+
style: form.calendar === value ? styles.chipOn : styles.chip,
|
|
208
|
+
onClick: () => set({ calendar: value }),
|
|
209
|
+
}, value === 'lunar' ? '农历' : '公历')),
|
|
210
|
+
]),
|
|
211
|
+
]
|
|
212
|
+
|
|
213
|
+
if (lunar) {
|
|
214
|
+
const months = Array.from({ length: 12 }, (_, index) => index + 1)
|
|
215
|
+
const days = Array.from({ length: 30 }, (_, index) => index + 1)
|
|
216
|
+
children.push(h('div', { key: 'lunar', style: styles.row }, [
|
|
217
|
+
h('span', { key: 'l', style: styles.fieldLabel }, '农历'),
|
|
218
|
+
h('select', {
|
|
219
|
+
style: styles.input, value: form.lunarMonth,
|
|
220
|
+
onChange: (event) => set({ lunarMonth: Number(event.target.value) }),
|
|
221
|
+
}, months.map((month) => h('option', { key: month, value: month }, month + ' 月'))),
|
|
222
|
+
h('select', {
|
|
223
|
+
style: styles.input, value: form.lunarDay,
|
|
224
|
+
onChange: (event) => set({ lunarDay: Number(event.target.value) }),
|
|
225
|
+
}, days.map((day) => h('option', { key: day, value: day }, day + ' 日'))),
|
|
226
|
+
h('label', { key: 'leap', style: styles.row }, [
|
|
227
|
+
h('input', {
|
|
228
|
+
key: 'c', type: 'checkbox', checked: form.lunarLeap,
|
|
229
|
+
onChange: (event) => set({ lunarLeap: event.target.checked }),
|
|
230
|
+
}),
|
|
231
|
+
h('span', { key: 't', style: styles.fieldLabel }, '闰月'),
|
|
232
|
+
]),
|
|
233
|
+
]))
|
|
234
|
+
} else {
|
|
235
|
+
children.push(h('div', { key: 'date', style: styles.row }, [
|
|
236
|
+
h('span', { key: 'l', style: styles.fieldLabel }, '日期'),
|
|
237
|
+
h('input', {
|
|
238
|
+
key: 'd', type: 'date', style: styles.input, value: form.date || '',
|
|
239
|
+
onChange: (event) => set({ date: event.target.value }),
|
|
240
|
+
}),
|
|
241
|
+
]))
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
children.push(h('div', { key: 'repeat', style: styles.row }, [
|
|
245
|
+
h('span', { key: 'l', style: styles.fieldLabel }, '重复'),
|
|
246
|
+
h('select', {
|
|
247
|
+
style: styles.input, value: form.repeat,
|
|
248
|
+
onChange: (event) => set({ repeat: event.target.value }),
|
|
249
|
+
}, REPEAT_OPTIONS.map((entry) => h('option', { key: entry[0], value: entry[0] }, entry[1]))),
|
|
250
|
+
form.repeat === 'customDays'
|
|
251
|
+
? h('input', {
|
|
252
|
+
key: 'n', type: 'number', min: 2, style: Object.assign({}, styles.input, { width: 70 }),
|
|
253
|
+
value: form.every,
|
|
254
|
+
onChange: (event) => set({ every: Number(event.target.value) }),
|
|
255
|
+
})
|
|
256
|
+
: null,
|
|
257
|
+
]))
|
|
258
|
+
|
|
259
|
+
if (form.repeat === 'weekly') {
|
|
260
|
+
children.push(h('div', { key: 'weekday', style: styles.row }, [
|
|
261
|
+
h('span', { key: 'l', style: styles.fieldLabel }, '每周几'),
|
|
262
|
+
...WEEKDAYS.map((label, index) => h('button', {
|
|
263
|
+
key: label,
|
|
264
|
+
type: 'button',
|
|
265
|
+
style: (form.weekdayMask >> index) & 1 ? styles.chipOn : styles.chip,
|
|
266
|
+
onClick: () => set({ weekdayMask: form.weekdayMask ^ (1 << index) }),
|
|
267
|
+
}, label)),
|
|
268
|
+
]))
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
children.push(h('div', { key: 'advance', style: styles.row }, [
|
|
272
|
+
h('span', { key: 'l', style: styles.fieldLabel }, '提前提醒'),
|
|
273
|
+
h('input', {
|
|
274
|
+
key: 'n', type: 'text', style: Object.assign({}, styles.input, { width: 90 }),
|
|
275
|
+
placeholder: '7,1',
|
|
276
|
+
value: form.advance, onChange: (event) => set({ advance: event.target.value }),
|
|
277
|
+
}),
|
|
278
|
+
h('span', { key: 'u', style: styles.fieldLabel }, '天前'),
|
|
279
|
+
h('input', {
|
|
280
|
+
key: 't', type: 'time', style: Object.assign({}, styles.input, { width: 110 }),
|
|
281
|
+
value: form.remindAt, title: '提醒时刻,留空用全局设置',
|
|
282
|
+
onChange: (event) => set({ remindAt: event.target.value }),
|
|
283
|
+
}),
|
|
284
|
+
h('span', { key: 'hint', style: styles.fieldLabel }, '(天数可多个;时刻留空用全局)'),
|
|
285
|
+
]))
|
|
286
|
+
|
|
287
|
+
children.push(h('div', { key: 'tags', style: styles.row }, [
|
|
288
|
+
h('span', { key: 'l', style: styles.fieldLabel }, '标签'),
|
|
289
|
+
h('input', {
|
|
290
|
+
style: Object.assign({}, styles.input, { flex: 1 }),
|
|
291
|
+
value: form.tags, placeholder: '逗号分隔,如:家人, 生日',
|
|
292
|
+
onChange: (event) => set({ tags: event.target.value }),
|
|
293
|
+
}),
|
|
294
|
+
]))
|
|
295
|
+
|
|
296
|
+
children.push(h('div', { key: 'actions', style: styles.actions }, [
|
|
297
|
+
h('button', { key: 'cancel', type: 'button', style: styles.button, onClick: props.onCancel }, '取消'),
|
|
298
|
+
h('button', { key: 'save', type: 'button', style: styles.primary, onClick: props.onSubmit },
|
|
299
|
+
props.mode === 'edit' ? '保存' : '添加'),
|
|
300
|
+
]))
|
|
301
|
+
|
|
302
|
+
return h('div', { style: styles.form }, children)
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const GRID_DAYS = ['一', '二', '三', '四', '五', '六', '日']
|
|
306
|
+
const ZONES = ['Asia/Shanghai', 'Asia/Tokyo', 'Asia/Singapore', 'Europe/London', 'Europe/Berlin', 'America/New_York', 'America/Los_Angeles', 'UTC']
|
|
307
|
+
|
|
308
|
+
function CalendarGrid(props) {
|
|
309
|
+
const month = props.month
|
|
310
|
+
if (!month) return h('div', { style: styles.muted }, '加载月历…')
|
|
311
|
+
|
|
312
|
+
const lead = typeof month.firstWeekdayMonday === 'number'
|
|
313
|
+
? month.firstWeekdayMonday
|
|
314
|
+
: (month.firstWeekday + 6) % 7
|
|
315
|
+
|
|
316
|
+
const padded = []
|
|
317
|
+
for (let index = 0; index < lead; index += 1) padded.push(null)
|
|
318
|
+
for (const day of month.days) padded.push(day)
|
|
319
|
+
while (padded.length % 7 !== 0) padded.push(null)
|
|
320
|
+
|
|
321
|
+
const cells = []
|
|
322
|
+
const headerCells = [h('div', { key: 'dow-empty', style: styles.dow }, '周')]
|
|
323
|
+
for (let index = 0; index < GRID_DAYS.length; index += 1) {
|
|
324
|
+
const label = GRID_DAYS[index]
|
|
325
|
+
headerCells.push(h('div', {
|
|
326
|
+
key: 'dow-' + label,
|
|
327
|
+
style: (index === 5 || index === 6)
|
|
328
|
+
? Object.assign({}, styles.dow, { color: '#e5484d' })
|
|
329
|
+
: styles.dow,
|
|
330
|
+
}, label))
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
for (let row = 0; row * 7 < padded.length; row += 1) {
|
|
334
|
+
const week = padded.slice(row * 7, row * 7 + 7)
|
|
335
|
+
const firstReal = week.find(function (day) { return day !== null })
|
|
336
|
+
const weekNo = firstReal ? (firstReal.weekNo || 1) : 1
|
|
337
|
+
const odd = weekNo % 2 === 1
|
|
338
|
+
|
|
339
|
+
cells.push(h('div', {
|
|
340
|
+
key: 'wk-' + row,
|
|
341
|
+
style: Object.assign({}, styles.weekLabel, { color: odd ? '#4f6ef7' : '#8e4ec6' }),
|
|
342
|
+
title: 'ISO 第 ' + weekNo + ' 周',
|
|
343
|
+
}, odd ? '单周' : '双周'))
|
|
344
|
+
|
|
345
|
+
for (const day of week) {
|
|
346
|
+
if (day === null) {
|
|
347
|
+
cells.push(h('div', { key: 'blank-' + row + '-' + cells.length }))
|
|
348
|
+
continue
|
|
349
|
+
}
|
|
350
|
+
const date = new Date(day.date + 'T00:00:00Z')
|
|
351
|
+
const weekday = date.getUTCDay()
|
|
352
|
+
const weekend = weekday === 0 || weekday === 6
|
|
353
|
+
const isToday = day.date === month.today
|
|
354
|
+
const isSelected = day.date === props.selected
|
|
355
|
+
const cellStyle = Object.assign({},
|
|
356
|
+
styles.cell,
|
|
357
|
+
props.compact ? styles.cellCompact : {},
|
|
358
|
+
weekend ? styles.cellWeekend : {},
|
|
359
|
+
isToday ? styles.cellToday : {},
|
|
360
|
+
isSelected ? styles.cellSelected : {})
|
|
361
|
+
|
|
362
|
+
const marks = day.items.slice(0, 4).map(function (item, index) {
|
|
363
|
+
return h('span', {
|
|
364
|
+
key: index,
|
|
365
|
+
style: Object.assign({}, styles.dot, { background: colorOf(item.id) }),
|
|
366
|
+
title: item.title,
|
|
367
|
+
})
|
|
368
|
+
})
|
|
369
|
+
|
|
370
|
+
cells.push(h('div', {
|
|
371
|
+
key: day.date,
|
|
372
|
+
style: cellStyle,
|
|
373
|
+
onClick: function () {
|
|
374
|
+
props.onSelect(day.date === props.selected ? null : day.date)
|
|
375
|
+
},
|
|
376
|
+
}, [
|
|
377
|
+
day.mercuryRetrograde ? h('span', { key: 'rx', style: styles.retroMark, title: '水逆' }, '逆') : null,
|
|
378
|
+
h('div', { key: 'n', style: isToday ? Object.assign({}, styles.dayNum, { color: ACCENT, fontWeight: 700 }) : styles.dayNum }, day.day),
|
|
379
|
+
props.compact
|
|
380
|
+
? null
|
|
381
|
+
: day.almanac && day.almanac.jieQi
|
|
382
|
+
? h('div', { key: 'j', style: styles.jieQi }, day.almanac.jieQi)
|
|
383
|
+
: h('div', { key: 'l', style: styles.lunarDay }, day.almanac ? day.almanac.lunarDay : ''),
|
|
384
|
+
h('div', { key: 'd', style: styles.dots }, [
|
|
385
|
+
...marks,
|
|
386
|
+
day.items.length > 4 ? h('span', { key: 'm', style: styles.more }, '+' + (day.items.length - 4)) : null,
|
|
387
|
+
]),
|
|
388
|
+
]))
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
return h('div', null, [
|
|
393
|
+
h('div', { key: 'bar', style: styles.calBar }, [
|
|
394
|
+
h('button', { key: 'p', type: 'button', style: styles.button, onClick: props.onPrev }, '‹'),
|
|
395
|
+
h('span', { key: 't', style: styles.calTitle }, month.year + ' 年 ' + month.month + ' 月'),
|
|
396
|
+
h('button', { key: 'n', type: 'button', style: styles.button, onClick: props.onNext }, '›'),
|
|
397
|
+
h('button', { key: 'today', type: 'button', style: styles.button, onClick: props.onToday }, '今天'),
|
|
398
|
+
]),
|
|
399
|
+
h('div', { key: 'head', style: styles.weekGrid }, headerCells),
|
|
400
|
+
h('div', { key: 'grid', style: props.compact ? Object.assign({}, styles.weekGrid, { maxWidth: '100%' }) : styles.weekGrid }, cells),
|
|
401
|
+
])
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function Card(props) {
|
|
405
|
+
return h('div', { style: Object.assign({}, styles.card, props.style || {}) }, props.children)
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function InfoRow(props) {
|
|
409
|
+
return h('div', { style: styles.infoRow }, [
|
|
410
|
+
h('span', { key: 'l', style: props.color ? Object.assign({}, styles.infoLabel, { color: props.color }) : styles.infoLabel }, props.label),
|
|
411
|
+
h('span', { key: 'v', style: styles.infoValue }, props.value),
|
|
412
|
+
])
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function AlmanacCard(props) {
|
|
416
|
+
const [open, setOpen] = React.useState(Boolean(props.defaultOpen))
|
|
417
|
+
const day = props.day
|
|
418
|
+
if (!day || !day.almanac) return null
|
|
419
|
+
const a = day.almanac
|
|
420
|
+
const jiShi = (a.jiShi || []).map(function (slot) { return slot.zhi + ' ' + slot.from })
|
|
421
|
+
|
|
422
|
+
return h(Card, null, [
|
|
423
|
+
h('div', { key: 'h', style: styles.cardTitle }, '老黄历'),
|
|
424
|
+
h(InfoRow, { key: 'l', label: '农历', value: a.lunarText + (a.ganzhi ? ' · ' + a.ganzhi : '') }),
|
|
425
|
+
h(InfoRow, { key: 'j', label: '节气', value: a.jieQi || '—' }),
|
|
426
|
+
h(InfoRow, { key: 'z', label: '建除', value: a.zhiXing + '日' }),
|
|
427
|
+
h(InfoRow, { key: 'y', label: '宜', value: a.yi.length ? a.yi.join(' · ') : '—', color: '#30a46c' }),
|
|
428
|
+
h(InfoRow, { key: 'x', label: '忌', value: a.ji.length ? a.ji.join(' · ') : '—', color: '#e5484d' }),
|
|
429
|
+
h(InfoRow, { key: 'c', label: '冲煞', value: (a.chong || '—') + ' · 煞' + (a.sha || '—') }),
|
|
430
|
+
h(InfoRow, { key: 's', label: '值神', value: (a.tianShen || '—') + (a.tianShenType ? ' · ' + a.tianShenType + '日' : '') }),
|
|
431
|
+
h(InfoRow, { key: 'u', label: '星宿', value: (a.xiu || '—') + (a.xiuLuck ? ' · ' + a.xiuLuck : '') }),
|
|
432
|
+
h(InfoRow, { key: 'n', label: '纳音', value: a.naYin || '—' }),
|
|
433
|
+
h(InfoRow, {
|
|
434
|
+
key: 'p', label: '方位',
|
|
435
|
+
value: a.positions ? `喜神 ${a.positions.xi} · 福神 ${a.positions.fu} · 财神 ${a.positions.cai}` : '—',
|
|
436
|
+
}),
|
|
437
|
+
h(InfoRow, { key: 't', label: '吉时', value: jiShi.length ? jiShi.join('、') : '—' }),
|
|
438
|
+
open ? h(InfoRow, { key: 'js', label: '吉神', value: (a.jiShen || []).join(' · ') || '—' }) : null,
|
|
439
|
+
open ? h(InfoRow, { key: 'xs', label: '凶煞', value: (a.xiongSha || []).join(' · ') || '—' }) : null,
|
|
440
|
+
a.pengZu ? h('div', { key: 'pz', style: styles.pengZu }, '彭祖百忌:' + a.pengZu) : null,
|
|
441
|
+
h('button', {
|
|
442
|
+
key: 'toggle', type: 'button',
|
|
443
|
+
style: Object.assign({}, styles.tiny, { justifySelf: 'start' }),
|
|
444
|
+
onClick: function () { setOpen(!open) },
|
|
445
|
+
}, open ? '收起' : '展开'),
|
|
446
|
+
])
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function ZodiacCard(props) {
|
|
450
|
+
const day = props.day
|
|
451
|
+
if (!day || !day.almanac || !day.almanac.zodiac) return null
|
|
452
|
+
const a = day.almanac
|
|
453
|
+
const z = a.zodiac
|
|
454
|
+
return h(Card, { style: props.grow ? { flex: 1 } : undefined }, [
|
|
455
|
+
h('div', { key: 'h', style: styles.cardTitle }, '星座 · 月相'),
|
|
456
|
+
h('div', { key: 'sign', style: styles.zodiacSign }, [
|
|
457
|
+
z.sign + '座',
|
|
458
|
+
h('span', { key: 'e', style: styles.zodiacElement }, z.element + (z.ruler ? ' · 守护星 ' + z.ruler : '')),
|
|
459
|
+
]),
|
|
460
|
+
h(InfoRow, { key: 'r', label: '日期', value: day.date + ' 星期' + a.week + (z.range ? ' · ' + z.range : '') }),
|
|
461
|
+
h(InfoRow, { key: 'm', label: '月相', value: a.yueXiang || '—' }),
|
|
462
|
+
])
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
function MercuryStrip(props) {
|
|
467
|
+
const month = props.month
|
|
468
|
+
if (!month) return null
|
|
469
|
+
const today = props.today || month.today
|
|
470
|
+
const ranges = month.retrograde || []
|
|
471
|
+
const current = ranges.find(function (range) { return today >= range.start && today <= range.end })
|
|
472
|
+
const next = ranges.filter(function (range) { return range.start > today }).sort(function (a, b) { return a.start < b.start ? -1 : 1 })[0]
|
|
473
|
+
|
|
474
|
+
const gap = function (from, to) {
|
|
475
|
+
return Math.round((Date.parse(to + 'T00:00:00Z') - Date.parse(from + 'T00:00:00Z')) / 86_400_000)
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
let status
|
|
479
|
+
if (current) {
|
|
480
|
+
const left = gap(today, current.end)
|
|
481
|
+
status = '水逆中 · ' + current.start + ' → ' + current.end + (left > 0 ? '(还有 ' + left + ' 天)' : '(最后一天)')
|
|
482
|
+
} else if (next) {
|
|
483
|
+
status = '下次 ' + next.start + ' 开始(' + gap(today, next.start) + ' 天后)'
|
|
484
|
+
} else {
|
|
485
|
+
status = '近期没有'
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
return h('div', { style: styles.strip }, [
|
|
489
|
+
h('span', { key: 'tag', style: Object.assign({}, styles.stripTag, current ? { color: '#e5484d' } : {}) }, '水星逆行'),
|
|
490
|
+
h('span', { key: 's', style: { fontSize: 12 } }, status),
|
|
491
|
+
ranges.length
|
|
492
|
+
? h('span', { key: 'r', style: { fontSize: 11, color: MUTED, marginLeft: 'auto' } },
|
|
493
|
+
ranges.map(function (range) { return range.start.slice(5) + '–' + range.end.slice(5) }).join('、'))
|
|
494
|
+
: null,
|
|
495
|
+
])
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
class Boundary extends React.Component {
|
|
499
|
+
constructor(props) {
|
|
500
|
+
super(props)
|
|
501
|
+
this.state = { error: null }
|
|
502
|
+
}
|
|
503
|
+
static getDerivedStateFromError(error) { return { error } }
|
|
504
|
+
componentDidCatch(error) { console.error('[dsh-reminder] render failed', error) }
|
|
505
|
+
render() {
|
|
506
|
+
if (this.state.error) {
|
|
507
|
+
return h('div', { style: { padding: 24, fontSize: 13, color: '#e5484d', whiteSpace: 'pre-wrap' } },
|
|
508
|
+
'记日渲染出错:' + String(this.state.error && this.state.error.message ? this.state.error.message : this.state.error))
|
|
509
|
+
}
|
|
510
|
+
return this.props.children
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
function PageBoundary(props) {
|
|
515
|
+
return h(Boundary, null, h(Page, props))
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
function Page() {
|
|
519
|
+
const [state, setState] = React.useState({ loading: true, rows: [], today: '', error: '' })
|
|
520
|
+
const [month, setMonth] = React.useState(null)
|
|
521
|
+
const [selected, setSelected] = React.useState(null)
|
|
522
|
+
const [editing, setEditing] = React.useState(null)
|
|
523
|
+
const [form, setForm] = React.useState(emptyForm(''))
|
|
524
|
+
const [busy, setBusy] = React.useState(false)
|
|
525
|
+
const [reminder, setReminder] = React.useState({ loading: true, on: false, supported: false })
|
|
526
|
+
const [query, setQuery] = React.useState('')
|
|
527
|
+
const [zone, setZone] = React.useState('')
|
|
528
|
+
const [remindTime, setRemindTime] = React.useState('09:00')
|
|
529
|
+
const [width, setWidth] = React.useState(typeof window === 'undefined' ? 1200 : window.innerWidth)
|
|
530
|
+
const [tag, setTag] = React.useState('')
|
|
531
|
+
const [lastDeleted, setLastDeleted] = React.useState(null)
|
|
532
|
+
const formRef = React.useRef(null)
|
|
533
|
+
const fileRef = React.useRef(null)
|
|
534
|
+
const [listHidden, setListHidden] = React.useState(function () {
|
|
535
|
+
try { return window.localStorage.getItem('dsh-reminder:list-hidden') === '1' } catch (error) { return false }
|
|
536
|
+
})
|
|
537
|
+
|
|
538
|
+
function toggleList() {
|
|
539
|
+
setListHidden(function (prev) {
|
|
540
|
+
const next = !prev
|
|
541
|
+
try { window.localStorage.setItem('dsh-reminder:list-hidden', next ? '1' : '0') } catch (error) { /* 忽略 */ }
|
|
542
|
+
return next
|
|
543
|
+
})
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
function reportError(error) {
|
|
547
|
+
setState(function (prev) { return Object.assign({}, prev, { error: String(error.message || error) }) })
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
const loadAll = React.useCallback(function (year, monthNo) {
|
|
551
|
+
return Promise.all([
|
|
552
|
+
api('list'),
|
|
553
|
+
year && monthNo ? api('month', { year, month: monthNo }) : null,
|
|
554
|
+
]).then(function (results) {
|
|
555
|
+
const list = results[0]
|
|
556
|
+
const monthData = results[1]
|
|
557
|
+
setState({ loading: false, rows: list.rows || [], today: list.today || '', error: '' })
|
|
558
|
+
if (monthData) setMonth(monthData.month)
|
|
559
|
+
return list
|
|
560
|
+
})
|
|
561
|
+
}, [])
|
|
562
|
+
|
|
563
|
+
const loadMonthOnly = React.useCallback(function (year, monthNo) {
|
|
564
|
+
return api('month', { year, month: monthNo }).then(function (data) {
|
|
565
|
+
setMonth(data.month)
|
|
566
|
+
return data
|
|
567
|
+
})
|
|
568
|
+
}, [])
|
|
569
|
+
|
|
570
|
+
React.useEffect(function () {
|
|
571
|
+
let alive = true
|
|
572
|
+
const today = new Date()
|
|
573
|
+
loadAll(today.getFullYear(), today.getMonth() + 1)
|
|
574
|
+
.catch(function (error) { if (alive) reportError(error) })
|
|
575
|
+
api('settings.get')
|
|
576
|
+
.then(function (data) {
|
|
577
|
+
if (!alive) return
|
|
578
|
+
if (data.timeZone) setZone(data.timeZone)
|
|
579
|
+
if (data.reminderTime) setRemindTime(data.reminderTime)
|
|
580
|
+
})
|
|
581
|
+
.catch(function () { /* 老版本没有这个路由,忽略 */ })
|
|
582
|
+
const onResize = function () { setWidth(window.innerWidth) }
|
|
583
|
+
window.addEventListener('resize', onResize)
|
|
584
|
+
api('remind.status')
|
|
585
|
+
.then(function (data) {
|
|
586
|
+
if (alive) setReminder({ loading: false, on: Boolean(data.reminder), supported: Boolean(data.supported) })
|
|
587
|
+
})
|
|
588
|
+
.catch(function () { if (alive) setReminder({ loading: false, on: false, supported: false }) })
|
|
589
|
+
return function () {
|
|
590
|
+
alive = false
|
|
591
|
+
window.removeEventListener('resize', onResize)
|
|
592
|
+
}
|
|
593
|
+
}, [loadAll])
|
|
594
|
+
|
|
595
|
+
function cursor() {
|
|
596
|
+
const base = state.today || ''
|
|
597
|
+
if (month) return { year: month.year, month: month.month }
|
|
598
|
+
const now = new Date()
|
|
599
|
+
return { year: now.getFullYear(), month: now.getMonth() + 1 }
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
function shiftMonth(delta) {
|
|
603
|
+
if (!month) return
|
|
604
|
+
let year = month.year
|
|
605
|
+
let monthNo = month.month + delta
|
|
606
|
+
if (monthNo < 1) { monthNo = 12; year -= 1 }
|
|
607
|
+
if (monthNo > 12) { monthNo = 1; year += 1 }
|
|
608
|
+
loadMonthOnly(year, monthNo).catch(reportError)
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
function run(action, payload) {
|
|
612
|
+
setBusy(true)
|
|
613
|
+
api(action, payload)
|
|
614
|
+
.then(function (data) {
|
|
615
|
+
if (action === 'delete' && data.removed) setLastDeleted(data.removed)
|
|
616
|
+
setEditing(null)
|
|
617
|
+
const at = cursor()
|
|
618
|
+
return loadAll(at.year, at.month)
|
|
619
|
+
})
|
|
620
|
+
.catch(reportError)
|
|
621
|
+
.then(function () { setBusy(false) })
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
function undoDelete() {
|
|
625
|
+
if (!lastDeleted) return
|
|
626
|
+
api('restore', { item: lastDeleted })
|
|
627
|
+
.then(function () { setLastDeleted(null); return refreshAll() })
|
|
628
|
+
.catch(reportError)
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
function toggleDone(row) {
|
|
632
|
+
const action = row.doneNow ? 'undone' : 'done'
|
|
633
|
+
api(action, { id: row.id, occurrence: row.date })
|
|
634
|
+
.then(function () { return refreshAll() })
|
|
635
|
+
.catch(reportError)
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
function scrollToForm() {
|
|
639
|
+
setTimeout(function () {
|
|
640
|
+
if (formRef.current && formRef.current.scrollIntoView) {
|
|
641
|
+
formRef.current.scrollIntoView({ behavior: 'smooth', block: 'start' })
|
|
642
|
+
}
|
|
643
|
+
}, 0)
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
function startNew() {
|
|
647
|
+
setForm(emptyForm(state.today))
|
|
648
|
+
setEditing({ mode: 'new', row: null })
|
|
649
|
+
scrollToForm()
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
function startEdit(row) {
|
|
653
|
+
setForm(formFromRow(row))
|
|
654
|
+
setEditing({ mode: 'edit', row })
|
|
655
|
+
scrollToForm()
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
function refreshAll() {
|
|
659
|
+
const at = cursor()
|
|
660
|
+
return loadAll(at.year, at.month).catch(reportError)
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
function downloadText(filename, text, mime) {
|
|
664
|
+
const blob = new Blob([text], { type: mime || 'text/plain' })
|
|
665
|
+
const url = URL.createObjectURL(blob)
|
|
666
|
+
const link = document.createElement('a')
|
|
667
|
+
link.href = url
|
|
668
|
+
link.download = filename
|
|
669
|
+
document.body.appendChild(link)
|
|
670
|
+
link.click()
|
|
671
|
+
link.remove()
|
|
672
|
+
setTimeout(function () { URL.revokeObjectURL(url) }, 1000)
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
function exportData(format) {
|
|
676
|
+
api('export', { format })
|
|
677
|
+
.then(function (data) { downloadText(data.filename, data.data, data.mime) })
|
|
678
|
+
.catch(reportError)
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
function importFile(event) {
|
|
682
|
+
const picked = event.target.files && event.target.files[0]
|
|
683
|
+
event.target.value = ''
|
|
684
|
+
if (!picked) return
|
|
685
|
+
picked.text()
|
|
686
|
+
.then(function (text) { return api('import', { data: text }) })
|
|
687
|
+
.then(function (data) {
|
|
688
|
+
const extra = data.errors && data.errors.length ? `,${data.errors.length} 条有误` : ''
|
|
689
|
+
window.alert(`导入完成:新增 ${data.added} 条,跳过 ${data.skipped} 条${extra}`)
|
|
690
|
+
return refreshAll()
|
|
691
|
+
})
|
|
692
|
+
.catch(reportError)
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
function changeZone(next) {
|
|
696
|
+
api('settings.set', { timeZone: next })
|
|
697
|
+
.then(function (data) { setZone(data.timeZone); return refreshAll() })
|
|
698
|
+
.catch(reportError)
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
function changeRemindTime(next) {
|
|
702
|
+
setRemindTime(next)
|
|
703
|
+
api('settings.set', { reminderTime: next })
|
|
704
|
+
.then(function (data) {
|
|
705
|
+
if (data.reminder) setReminder({ loading: false, on: true, supported: true })
|
|
706
|
+
})
|
|
707
|
+
.catch(function (error) {
|
|
708
|
+
setRemindTime(remindTime)
|
|
709
|
+
reportError(error)
|
|
710
|
+
})
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
function toggleReminder() {
|
|
714
|
+
api(reminder.on ? 'remind.off' : 'remind.on')
|
|
715
|
+
.then(function (data) {
|
|
716
|
+
setReminder({ loading: false, on: Boolean(data.reminder), supported: true })
|
|
717
|
+
})
|
|
718
|
+
.catch(reportError)
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
function submit() {
|
|
722
|
+
if (!form.title.trim()) { reportError(new Error('标题不能为空')); return }
|
|
723
|
+
if (!form.date && form.calendar !== 'lunar') { reportError(new Error('请选择日期')); return }
|
|
724
|
+
if (editing.mode === 'new') run('add', { item: form })
|
|
725
|
+
else run('update', { id: editing.row.id, fields: form })
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
if (state.loading) return h('div', { style: styles.muted }, '加载中…')
|
|
729
|
+
|
|
730
|
+
const dayOf = function (date) {
|
|
731
|
+
if (!month || !date) return null
|
|
732
|
+
return month.days.find(function (day) { return day.date === date }) || null
|
|
733
|
+
}
|
|
734
|
+
const selectedDay = dayOf(selected)
|
|
735
|
+
const todayDay = dayOf(state.today)
|
|
736
|
+
const almanacDay = selectedDay || todayDay
|
|
737
|
+
const allTags = Array.from(new Set(state.rows.reduce(function (acc, row) {
|
|
738
|
+
return acc.concat(row.tags || [])
|
|
739
|
+
}, [])))
|
|
740
|
+
const keyword = query.trim().toLowerCase()
|
|
741
|
+
const listRows = state.rows.filter(function (row) {
|
|
742
|
+
if (tag && (row.tags || []).indexOf(tag) === -1) return false
|
|
743
|
+
if (!keyword) return true
|
|
744
|
+
if (row.title.toLowerCase().indexOf(keyword) !== -1) return true
|
|
745
|
+
return (row.tags || []).some(function (item) { return item.toLowerCase().indexOf(keyword) !== -1 })
|
|
746
|
+
})
|
|
747
|
+
const listTitle = '已录入的事项'
|
|
748
|
+
const compact = width < 780
|
|
749
|
+
const wide = width >= 1080
|
|
750
|
+
|
|
751
|
+
const toast = lastDeleted
|
|
752
|
+
? h('div', { key: 'toast', style: styles.toast }, [
|
|
753
|
+
h('span', { key: 't' }, '已删除「' + lastDeleted.title + '」'),
|
|
754
|
+
h('button', { key: 'u', type: 'button', style: Object.assign({}, styles.linkButton, { color: ACCENT }), onClick: undoDelete }, '撤销'),
|
|
755
|
+
h('button', { key: 'x', type: 'button', style: styles.linkButton, onClick: function () { setLastDeleted(null) } }, '×'),
|
|
756
|
+
])
|
|
757
|
+
: null
|
|
758
|
+
|
|
759
|
+
return h('div', { style: compact ? Object.assign({}, styles.wrap, styles.wrapCompact) : styles.wrap }, [
|
|
760
|
+
h('div', { key: 'header', style: styles.header }, [
|
|
761
|
+
h('h1', { key: 't', style: styles.title }, '记日'),
|
|
762
|
+
h('button', { key: 'add', type: 'button', style: styles.primary, onClick: startNew, disabled: busy }, '+ 新增'),
|
|
763
|
+
selected
|
|
764
|
+
? h('button', {
|
|
765
|
+
key: 'clear', type: 'button', style: styles.button,
|
|
766
|
+
onClick: function () { setSelected(null) },
|
|
767
|
+
}, '显示全部')
|
|
768
|
+
: null,
|
|
769
|
+
h('span', { key: 's', style: { fontSize: 12, color: MUTED } }, state.today ? '今天 ' + state.today : ''),
|
|
770
|
+
h('span', { key: 'spacer', style: { flex: 1 } }),
|
|
771
|
+
h('select', {
|
|
772
|
+
key: 'zone',
|
|
773
|
+
style: styles.zone,
|
|
774
|
+
value: zone,
|
|
775
|
+
title: '时区:影响「今天」与农历换算',
|
|
776
|
+
onChange: function (event) { changeZone(event.target.value) },
|
|
777
|
+
}, (zone && ZONES.indexOf(zone) === -1 ? [zone] : []).concat(ZONES).map(function (name) {
|
|
778
|
+
return h('option', { key: name, value: name }, name)
|
|
779
|
+
})),
|
|
780
|
+
h('button', { key: 'exp', type: 'button', style: styles.button, onClick: function () { exportData('json') } }, '导出'),
|
|
781
|
+
h('button', { key: 'imp', type: 'button', style: styles.button, onClick: function () { fileRef.current && fileRef.current.click() } }, '导入'),
|
|
782
|
+
h('input', {
|
|
783
|
+
key: 'file', ref: fileRef, type: 'file', accept: '.json,application/json',
|
|
784
|
+
style: { display: 'none' }, onChange: importFile,
|
|
785
|
+
}),
|
|
786
|
+
h('input', {
|
|
787
|
+
key: 'time', type: 'time', value: remindTime, style: styles.zone,
|
|
788
|
+
title: '每日提醒时间',
|
|
789
|
+
onChange: function (event) { changeRemindTime(event.target.value) },
|
|
790
|
+
}),
|
|
791
|
+
h('button', {
|
|
792
|
+
key: 'rem',
|
|
793
|
+
type: 'button',
|
|
794
|
+
style: reminder.on ? styles.chipOn : styles.button,
|
|
795
|
+
title: reminder.supported ? '每天 09:00 在会话里汇报今天与未来 3 天' : '当前 profile 没有 schedule 服务',
|
|
796
|
+
disabled: reminder.loading || !reminder.supported,
|
|
797
|
+
onClick: toggleReminder,
|
|
798
|
+
}, reminder.on ? '每日提醒 已开' : '每日提醒'),
|
|
799
|
+
]),
|
|
800
|
+
|
|
801
|
+
state.error ? h('div', { key: 'error', style: styles.error }, state.error) : null,
|
|
802
|
+
|
|
803
|
+
editing
|
|
804
|
+
? h('div', { key: 'formWrap', ref: formRef },
|
|
805
|
+
h(Form, { form, setForm, onSubmit: submit, onCancel: () => setEditing(null), mode: editing.mode }))
|
|
806
|
+
: null,
|
|
807
|
+
|
|
808
|
+
h('div', { key: 'listSection' }, [
|
|
809
|
+
h('div', { key: 'listBar', style: styles.listBar }, [
|
|
810
|
+
h('span', { key: 't', style: styles.listTitle }, listTitle),
|
|
811
|
+
h('span', { key: 'c', style: { fontSize: 12, color: MUTED } }, listRows.length ? listRows.length + ' 条' : ''),
|
|
812
|
+
h('input', {
|
|
813
|
+
key: 'q', style: styles.search, value: query, placeholder: '搜索标题 / 标签',
|
|
814
|
+
onChange: function (event) { setQuery(event.target.value) },
|
|
815
|
+
}),
|
|
816
|
+
h('button', {
|
|
817
|
+
key: 'toggle',
|
|
818
|
+
type: 'button',
|
|
819
|
+
style: Object.assign({}, styles.linkButton, { marginLeft: 'auto' }),
|
|
820
|
+
title: listHidden ? '展开列表' : '收起列表',
|
|
821
|
+
onClick: toggleList,
|
|
822
|
+
}, listHidden ? '显示' : '隐藏'),
|
|
823
|
+
]),
|
|
824
|
+
|
|
825
|
+
allTags.length
|
|
826
|
+
? h('div', { key: 'tags', style: Object.assign({}, styles.row, { margin: '0 0 8px', flexWrap: 'wrap' }) }, [
|
|
827
|
+
h('button', { key: 'all', type: 'button', style: tag ? styles.chip : styles.chipOn, onClick: function () { setTag('') } }, '全部'),
|
|
828
|
+
...allTags.map(function (name) {
|
|
829
|
+
return h('button', {
|
|
830
|
+
key: name, type: 'button', style: tag === name ? styles.chipOn : styles.chip,
|
|
831
|
+
onClick: function () { setTag(tag === name ? '' : name) },
|
|
832
|
+
}, name)
|
|
833
|
+
}),
|
|
834
|
+
])
|
|
835
|
+
: null,
|
|
836
|
+
|
|
837
|
+
listHidden
|
|
838
|
+
? null
|
|
839
|
+
: listRows.length === 0
|
|
840
|
+
? h('div', { key: 'empty', style: styles.muted }, '还没有记录。点「+ 新增」,或者在对话里说一句。')
|
|
841
|
+
: h('table', { key: 'table', style: styles.table }, [
|
|
842
|
+
h('thead', { key: 'head' }, h('tr', null, [
|
|
843
|
+
h('th', { key: 'w', style: styles.th }, '单双周'),
|
|
844
|
+
h('th', { key: 'd', style: styles.th }, '日期'),
|
|
845
|
+
compact ? null : h('th', { key: 'i', style: styles.th }, '距今'),
|
|
846
|
+
h('th', { key: 't', style: styles.th }, '事项'),
|
|
847
|
+
h('th', { key: 'a', style: Object.assign({}, styles.th, { textAlign: 'right' }) }, ''),
|
|
848
|
+
])),
|
|
849
|
+
h('tbody', { key: 'body' }, listRows.map(function (row) {
|
|
850
|
+
const tone = row.overdue ? styles.overdue : row.inDays <= 3 ? styles.soon : undefined
|
|
851
|
+
return h('tr', { key: row.id, style: row.doneNow ? styles.doneRow : undefined }, [
|
|
852
|
+
h('td', { key: 'w', style: Object.assign({}, styles.td, { color: MUTED, fontSize: 12 }) }, weekLabel(row.weekNo || 1)),
|
|
853
|
+
h('td', { key: 'd', style: Object.assign({}, styles.td, tone || {}) }, row.date),
|
|
854
|
+
compact ? null : h('td', { key: 'i', style: Object.assign({}, styles.td, tone || {}) }, when(row)),
|
|
855
|
+
h('td', { key: 't', style: styles.td }, [
|
|
856
|
+
h('span', { key: 'c', style: Object.assign({}, styles.colorBar, { background: colorOf(row.id) }) }),
|
|
857
|
+
h('span', { key: 'tt', style: row.doneNow ? styles.doneTitle : undefined }, row.title),
|
|
858
|
+
row.lunar ? h('span', { key: 'l', style: styles.badge }, row.lunar) : null,
|
|
859
|
+
row.repeat !== 'none' ? h('span', { key: 'r', style: styles.badge }, kindLabel(row.kind)) : null,
|
|
860
|
+
(row.advance || []).length ? h('span', { key: 'a', style: styles.badge }, '提前' + row.advance.join('/') + '天' + (row.remindAt ? ' · ' + row.remindAt : '')) : null,
|
|
861
|
+
(row.tags || []).map(function (tag) {
|
|
862
|
+
return h('span', { key: 'tag-' + tag, style: styles.badge }, tag)
|
|
863
|
+
}),
|
|
864
|
+
]),
|
|
865
|
+
h('td', { key: 'a', style: Object.assign({}, styles.td, styles.actions) }, [
|
|
866
|
+
h('button', {
|
|
867
|
+
key: 'done', type: 'button', style: styles.linkButton, disabled: busy,
|
|
868
|
+
onClick: function () { toggleDone(row) },
|
|
869
|
+
}, row.doneNow ? '撤销' : (row.today ? '完成' : '打卡')),
|
|
870
|
+
h('button', {
|
|
871
|
+
key: 'edit', type: 'button', style: styles.linkButton,
|
|
872
|
+
onClick: function () { startEdit(row) },
|
|
873
|
+
}, '编辑'),
|
|
874
|
+
h('button', {
|
|
875
|
+
key: 'del', type: 'button', style: Object.assign({}, styles.linkButton, { color: DANGER }),
|
|
876
|
+
onClick: function () {
|
|
877
|
+
if (window.confirm('删除「' + row.title + '」?')) run('delete', { id: row.id })
|
|
878
|
+
},
|
|
879
|
+
}, '删除'),
|
|
880
|
+
]),
|
|
881
|
+
])
|
|
882
|
+
})),
|
|
883
|
+
]),
|
|
884
|
+
]),
|
|
885
|
+
|
|
886
|
+
h('div', { key: 'sep', style: styles.separator }),
|
|
887
|
+
|
|
888
|
+
h('div', { key: 'calRow', style: styles.calRow }, [
|
|
889
|
+
h('div', { key: 'calCol', style: styles.calCol }, [
|
|
890
|
+
h(MercuryStrip, { key: 'mercury', month, today: state.today }),
|
|
891
|
+
h(CalendarGrid, {
|
|
892
|
+
key: 'cal',
|
|
893
|
+
month,
|
|
894
|
+
compact,
|
|
895
|
+
selected,
|
|
896
|
+
onSelect: setSelected,
|
|
897
|
+
onPrev: () => shiftMonth(-1),
|
|
898
|
+
onNext: () => shiftMonth(1),
|
|
899
|
+
onToday: () => {
|
|
900
|
+
const now = new Date()
|
|
901
|
+
const iso = now.getFullYear() + '-' + String(now.getMonth() + 1).padStart(2, '0') + '-' + String(now.getDate()).padStart(2, '0')
|
|
902
|
+
setSelected(iso)
|
|
903
|
+
loadMonthOnly(now.getFullYear(), now.getMonth() + 1).catch(reportError)
|
|
904
|
+
},
|
|
905
|
+
}),
|
|
906
|
+
]),
|
|
907
|
+
h('div', { key: 'sideCol', style: styles.sideCol }, [
|
|
908
|
+
h(AlmanacCard, { key: 'almanac', day: almanacDay }),
|
|
909
|
+
h(ZodiacCard, { key: 'zodiac', day: almanacDay, grow: true }),
|
|
910
|
+
]),
|
|
911
|
+
]),
|
|
912
|
+
|
|
913
|
+
|
|
914
|
+
selectedDay
|
|
915
|
+
? h('div', { key: 'day', style: styles.dayPanel }, [
|
|
916
|
+
h('div', { key: 'h', style: styles.dayHead }, [
|
|
917
|
+
h('span', { key: 't', style: styles.dayTitle }, selected + ' 的安排'),
|
|
918
|
+
h('span', { key: 'c', style: { fontSize: 12, color: MUTED } }, selectedDay.items.length + ' 条'),
|
|
919
|
+
h('button', {
|
|
920
|
+
key: 'x', type: 'button', style: Object.assign({}, styles.linkButton, { marginLeft: 'auto' }),
|
|
921
|
+
onClick: function () { setSelected(null) },
|
|
922
|
+
}, '关闭'),
|
|
923
|
+
]),
|
|
924
|
+
selectedDay.items.length === 0
|
|
925
|
+
? h('div', { key: 'e', style: { fontSize: 12, color: MUTED, padding: '4px 0' } }, '这天没有安排。')
|
|
926
|
+
: selectedDay.items.map(function (row) {
|
|
927
|
+
return h('div', { key: row.id, style: styles.dayItem }, [
|
|
928
|
+
h('span', { key: 'c', style: Object.assign({}, styles.colorBar, { background: colorOf(row.id) }) }),
|
|
929
|
+
h('span', { key: 't', style: { flex: 1 } }, row.title),
|
|
930
|
+
row.lunar ? h('span', { key: 'l', style: styles.badge }, row.lunar) : null,
|
|
931
|
+
row.repeat !== 'none' ? h('span', { key: 'r', style: styles.badge }, kindLabel(row.kind)) : null,
|
|
932
|
+
(row.advance || []).length ? h('span', { key: 'a', style: styles.badge }, '提前' + row.advance.join('/') + '天' + (row.remindAt ? ' · ' + row.remindAt : '')) : null,
|
|
933
|
+
h('button', {
|
|
934
|
+
key: 'e', type: 'button', style: styles.linkButton,
|
|
935
|
+
onClick: function () { startEdit(row) },
|
|
936
|
+
}, '编辑'),
|
|
937
|
+
h('button', {
|
|
938
|
+
key: 'd', type: 'button', style: Object.assign({}, styles.linkButton, { color: DANGER }),
|
|
939
|
+
onClick: function () {
|
|
940
|
+
if (window.confirm('删除「' + row.title + '」?')) run('delete', { id: row.id })
|
|
941
|
+
},
|
|
942
|
+
}, '删除'),
|
|
943
|
+
])
|
|
944
|
+
}),
|
|
945
|
+
])
|
|
946
|
+
: null,
|
|
947
|
+
|
|
948
|
+
toast,
|
|
949
|
+
])
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
function CalendarIcon(props) {
|
|
953
|
+
const size = typeof props.size === 'number' ? props.size : 18
|
|
954
|
+
return h('svg', {
|
|
955
|
+
width: size, height: size, viewBox: '0 0 24 24', fill: 'none',
|
|
956
|
+
stroke: 'currentColor', strokeWidth: 1.8, strokeLinecap: 'round', strokeLinejoin: 'round',
|
|
957
|
+
}, [
|
|
958
|
+
h('rect', { key: 'r', x: 3, y: 5, width: 18, height: 16, rx: 2 }),
|
|
959
|
+
h('path', { key: 'l1', d: 'M3 10h18' }),
|
|
960
|
+
h('path', { key: 'l2', d: 'M8 3v4M16 3v4' }),
|
|
961
|
+
])
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
module.exports = {
|
|
965
|
+
name: 'dsh-reminder',
|
|
966
|
+
inject: ['slots'],
|
|
967
|
+
apply(ctx) {
|
|
968
|
+
ctx.slots.inject('main', () => ctx.slots.register({ name: 'main', key: 'dsh-reminder' }, PageBoundary))
|
|
969
|
+
ctx.slots.inject('sidebar.panellist', () => ctx.slots.register({
|
|
970
|
+
name: 'sidebar.panellist',
|
|
971
|
+
id: 'dsh-reminder',
|
|
972
|
+
order: 60,
|
|
973
|
+
label: () => '记日',
|
|
974
|
+
}, CalendarIcon))
|
|
975
|
+
},
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
if (globalThis.__DSH_JIRI_TEST__) {
|
|
979
|
+
module.exports.__test = { Page, Form, CalendarGrid, AlmanacCard, ZodiacCard, MercuryStrip, colorOf, when, weekLabel }
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
return module.exports
|
|
983
|
+
},
|
|
984
|
+
})
|