dsh-lite 0.0.0-stage → 0.6.2
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/CHANGELOG.md +183 -0
- package/LICENSE +21 -0
- package/README.en.md +250 -0
- package/README.md +289 -2
- package/cordis.patch.yml +15 -0
- package/docs/design-system.md +163 -0
- package/docs/lite-plus-plan.md +142 -0
- package/docs/networking.md +144 -0
- package/docs/parity.md +92 -0
- package/lib/client.js +68 -0
- package/lib/index.js +2261 -0
- package/package.json +72 -4
- package/public/app.js +3895 -0
- package/public/favicon.svg +8 -0
- package/public/i18n.js +386 -0
- package/public/icon-192.png +0 -0
- package/public/icon-512.png +0 -0
- package/public/index.html +196 -0
- package/public/manifest.webmanifest +28 -0
- package/public/question-answers.js +45 -0
- package/public/style.css +3250 -0
- package/public/sw.js +76 -0
- package/public/theme.css +2153 -0
- package/public/theme.js +372 -0
package/public/theme.js
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-lite theme runtime.
|
|
3
|
+
*
|
|
4
|
+
* Paints `<html>` with two attributes consumed by `theme.css`:
|
|
5
|
+
*
|
|
6
|
+
* <html data-theme-family="claude|codex|mac" data-theme="light|dark" data-theme-mode="light|dark|auto">
|
|
7
|
+
*
|
|
8
|
+
* - `data-theme-family` selects one of the three visual families.
|
|
9
|
+
* - `data-theme` is always a *resolved* mode (never "auto"), so CSS only ever
|
|
10
|
+
* sees light or dark and `prefers-color-scheme` fallbacks in style.css stay
|
|
11
|
+
* dormant.
|
|
12
|
+
* - `data-theme-mode` keeps the user's choice (including "auto") for UI that
|
|
13
|
+
* wants to show which option is selected.
|
|
14
|
+
*
|
|
15
|
+
* Storage is best-effort: private mode / disabled storage must not break paint.
|
|
16
|
+
* Persistence keys:
|
|
17
|
+
* - `dsh-lite:theme-family` — family id.
|
|
18
|
+
* - `dsh-lite:theme` — explicit light/dark; absent means "auto".
|
|
19
|
+
* (Same key the legacy app uses, so both stay in sync.)
|
|
20
|
+
*
|
|
21
|
+
* The module self-initializes when imported, so a single
|
|
22
|
+
* `<script type="module" src="theme.js">` after app.js is enough. Calling
|
|
23
|
+
* `initTheme()` again is idempotent.
|
|
24
|
+
*
|
|
25
|
+
* CSP note: nothing here injects <style> or inline style attributes; only
|
|
26
|
+
* data attributes, meta tags and CSS custom properties are touched.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/** @typedef {'claude' | 'codex' | 'mac'} ThemeFamily */
|
|
30
|
+
/** @typedef {'light' | 'dark' | 'auto'} ColorMode */
|
|
31
|
+
/** @typedef {'light' | 'dark'} ResolvedMode */
|
|
32
|
+
|
|
33
|
+
/** @typedef {Object} ThemeInfo
|
|
34
|
+
* @property {ThemeFamily} family Selected visual family.
|
|
35
|
+
* @property {ColorMode} mode User-facing choice, may be "auto".
|
|
36
|
+
* @property {ResolvedMode} resolvedMode The light/dark actually painted.
|
|
37
|
+
* @property {boolean} prefersDark Current OS preference (false without matchMedia).
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
const FAMILY_ATTRIBUTE = 'data-theme-family'
|
|
41
|
+
const MODE_ATTRIBUTE = 'data-theme'
|
|
42
|
+
const MODE_CHOICE_ATTRIBUTE = 'data-theme-mode'
|
|
43
|
+
|
|
44
|
+
/** Storage keys. `mode` intentionally mirrors the legacy app key. */
|
|
45
|
+
export const THEME_STORAGE_KEYS = Object.freeze({
|
|
46
|
+
family: 'dsh-lite:theme-family',
|
|
47
|
+
mode: 'dsh-lite:theme',
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
/** Event fired on `document` after every applied change. */
|
|
51
|
+
export const THEME_CHANGE_EVENT = 'dsh-lite:themechange'
|
|
52
|
+
|
|
53
|
+
/** @type {ThemeFamily} */
|
|
54
|
+
export const DEFAULT_THEME_FAMILY = 'mac'
|
|
55
|
+
|
|
56
|
+
/** @type {ColorMode} */
|
|
57
|
+
export const DEFAULT_COLOR_MODE = 'auto'
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Family metadata. Labels are deliberately not trademarks of the products the
|
|
61
|
+
* palettes are inspired by; descriptions describe the visual feel.
|
|
62
|
+
* @type {ReadonlyArray<{ id: ThemeFamily, label: { zh: string, en: string }, description: { zh: string, en: string } }>}
|
|
63
|
+
*/
|
|
64
|
+
export const THEME_FAMILIES = Object.freeze([
|
|
65
|
+
{
|
|
66
|
+
id: 'claude',
|
|
67
|
+
label: { zh: 'Claude', en: 'Claude' },
|
|
68
|
+
description: {
|
|
69
|
+
zh: 'Claude 风格:暖纸与墨色、赤陶强调,编辑式留白与柔和的圆角。',
|
|
70
|
+
en: 'Claude style: warm paper and ink, terracotta accents, editorial spacing and soft rounding.',
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
id: 'codex',
|
|
75
|
+
label: { zh: 'Codex', en: 'Codex' },
|
|
76
|
+
description: {
|
|
77
|
+
zh: 'Codex 风格:高对比石墨、紧凑密度、青绿强调,元信息偏等宽。',
|
|
78
|
+
en: 'Codex style: high-contrast graphite, compact density, emerald accents, monospace-leaning metadata.',
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
id: 'mac',
|
|
83
|
+
label: { zh: 'macOS', en: 'macOS' },
|
|
84
|
+
description: {
|
|
85
|
+
zh: 'macOS 风格:系统应用的半透明层次、蓝色强调与细腻分隔线。',
|
|
86
|
+
en: 'macOS style: system-app chrome, translucent layers, blue accents, hairline separators.',
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
])
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Mode metadata. `auto` follows `prefers-color-scheme`.
|
|
93
|
+
* @type {ReadonlyArray<{ id: ColorMode, label: { zh: string, en: string } }>}
|
|
94
|
+
*/
|
|
95
|
+
export const COLOR_MODES = Object.freeze([
|
|
96
|
+
{ id: 'light', label: { zh: '浅色', en: 'Light' } },
|
|
97
|
+
{ id: 'dark', label: { zh: '深色', en: 'Dark' } },
|
|
98
|
+
{ id: 'auto', label: { zh: '跟随系统', en: 'Auto' } },
|
|
99
|
+
])
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Browser chrome colors per family/mode, matching each theme's `--ds-bg`.
|
|
103
|
+
* Used for `<meta name="theme-color">` so the address bar matches the app.
|
|
104
|
+
* @type {Readonly<Record<ThemeFamily, Record<ResolvedMode, string>>>}
|
|
105
|
+
*/
|
|
106
|
+
export const THEME_META_COLORS = Object.freeze({
|
|
107
|
+
claude: { light: '#faf9f5', dark: '#1b1916' },
|
|
108
|
+
codex: { light: '#f6f6f7', dark: '#0b0c0e' },
|
|
109
|
+
mac: { light: '#f2f2f7', dark: '#1c1c1e' },
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
const FAMILY_IDS = THEME_FAMILIES.map((entry) => entry.id)
|
|
113
|
+
const MODE_IDS = COLOR_MODES.map((entry) => entry.id)
|
|
114
|
+
const DARK_QUERY = '(prefers-color-scheme: dark)'
|
|
115
|
+
|
|
116
|
+
/** @type {{ family: ThemeFamily, mode: ColorMode } | null} */
|
|
117
|
+
let current = null
|
|
118
|
+
/** @type {MediaQueryList | null} */
|
|
119
|
+
let darkQuery = null
|
|
120
|
+
/** @type {boolean} */
|
|
121
|
+
let listenersAttached = false
|
|
122
|
+
/** @type {boolean} */
|
|
123
|
+
let autoInitScheduled = false
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* @param {unknown} value
|
|
127
|
+
* @returns {value is ThemeFamily}
|
|
128
|
+
*/
|
|
129
|
+
export function isThemeFamily(value) {
|
|
130
|
+
return typeof value === 'string' && FAMILY_IDS.includes(/** @type {ThemeFamily} */ (value))
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @param {unknown} value
|
|
135
|
+
* @returns {value is ColorMode}
|
|
136
|
+
*/
|
|
137
|
+
export function isColorMode(value) {
|
|
138
|
+
return typeof value === 'string' && MODE_IDS.includes(/** @type {ColorMode} */ (value))
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @param {unknown} value
|
|
143
|
+
* @returns {value is ResolvedMode}
|
|
144
|
+
*/
|
|
145
|
+
function isResolvedMode(value) {
|
|
146
|
+
return value === 'light' || value === 'dark'
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** @param {string} key @returns {string | null} */
|
|
150
|
+
function readStore(key) {
|
|
151
|
+
try {
|
|
152
|
+
return typeof window === 'undefined' ? null : window.localStorage.getItem(key)
|
|
153
|
+
} catch {
|
|
154
|
+
return null
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** @param {string} key @param {string | null} value */
|
|
159
|
+
function writeStore(key, value) {
|
|
160
|
+
try {
|
|
161
|
+
if (typeof window === 'undefined') return
|
|
162
|
+
if (value === null) window.localStorage.removeItem(key)
|
|
163
|
+
else window.localStorage.setItem(key, value)
|
|
164
|
+
} catch {
|
|
165
|
+
// Storage can be unavailable (private mode, quota, policy). Non-fatal.
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** @returns {ColorMode} */
|
|
170
|
+
function storedMode() {
|
|
171
|
+
const value = readStore(THEME_STORAGE_KEYS.mode)
|
|
172
|
+
return isResolvedMode(value) ? value : DEFAULT_COLOR_MODE
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** @returns {ThemeFamily} */
|
|
176
|
+
function storedFamily() {
|
|
177
|
+
const value = readStore(THEME_STORAGE_KEYS.family)
|
|
178
|
+
return isThemeFamily(value) ? value : DEFAULT_THEME_FAMILY
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** @returns {boolean} */
|
|
182
|
+
function prefersDark() {
|
|
183
|
+
if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') return false
|
|
184
|
+
if (darkQuery === null) darkQuery = window.matchMedia(DARK_QUERY)
|
|
185
|
+
return darkQuery.matches
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* @param {ColorMode} mode
|
|
190
|
+
* @returns {ResolvedMode}
|
|
191
|
+
*/
|
|
192
|
+
function resolveMode(mode) {
|
|
193
|
+
if (mode === 'light' || mode === 'dark') return mode
|
|
194
|
+
return prefersDark() ? 'dark' : 'light'
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** @returns {HTMLElement | null} */
|
|
198
|
+
function rootElement() {
|
|
199
|
+
return typeof document === 'undefined' ? null : document.documentElement
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Push the current selection into the DOM, storage and (optionally) meta tags.
|
|
204
|
+
* @param {{ persistFamily?: boolean, persistMode?: boolean, notify?: boolean }} options
|
|
205
|
+
* @returns {ThemeInfo}
|
|
206
|
+
*/
|
|
207
|
+
function apply({ persistFamily = false, persistMode = false, notify = true } = {}) {
|
|
208
|
+
if (current === null) current = { family: storedFamily(), mode: storedMode() }
|
|
209
|
+
if (persistFamily) writeStore(THEME_STORAGE_KEYS.family, current.family)
|
|
210
|
+
if (persistMode) writeStore(THEME_STORAGE_KEYS.mode, current.mode === 'auto' ? null : current.mode)
|
|
211
|
+
|
|
212
|
+
const root = rootElement()
|
|
213
|
+
const resolved = resolveMode(current.mode)
|
|
214
|
+
if (root !== null) {
|
|
215
|
+
root.setAttribute(FAMILY_ATTRIBUTE, current.family)
|
|
216
|
+
root.setAttribute(MODE_ATTRIBUTE, resolved)
|
|
217
|
+
root.setAttribute(MODE_CHOICE_ATTRIBUTE, current.mode)
|
|
218
|
+
}
|
|
219
|
+
paintChrome(current.family, resolved)
|
|
220
|
+
|
|
221
|
+
const info = { family: current.family, mode: current.mode, resolvedMode: resolved, prefersDark: prefersDark() }
|
|
222
|
+
if (notify && typeof document !== 'undefined' && typeof CustomEvent === 'function') {
|
|
223
|
+
document.dispatchEvent(new CustomEvent(THEME_CHANGE_EVENT, { detail: info }))
|
|
224
|
+
}
|
|
225
|
+
return info
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Keep browser chrome in sync: every `<meta name="theme-color">` gets the
|
|
230
|
+
* family background color, and the `media` attribute is dropped so the value
|
|
231
|
+
* does not depend on the OS setting (the app paints an explicit mode).
|
|
232
|
+
* @param {ThemeFamily} family
|
|
233
|
+
* @param {ResolvedMode} mode
|
|
234
|
+
*/
|
|
235
|
+
function paintChrome(family, mode) {
|
|
236
|
+
if (typeof document === 'undefined') return
|
|
237
|
+
const color = THEME_META_COLORS[family][mode]
|
|
238
|
+
for (const node of document.querySelectorAll('meta[name="theme-color"]')) {
|
|
239
|
+
node.removeAttribute('media')
|
|
240
|
+
node.setAttribute('content', color)
|
|
241
|
+
}
|
|
242
|
+
const scheme = document.querySelector('meta[name="color-scheme"]')
|
|
243
|
+
if (scheme !== null) scheme.setAttribute('content', mode === 'dark' ? 'dark light' : 'light dark')
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Attach the OS-preference and cross-tab listeners once.
|
|
248
|
+
* `storage` events only fire in *other* tabs, which is exactly what we want.
|
|
249
|
+
*/
|
|
250
|
+
function attachListeners() {
|
|
251
|
+
if (listenersAttached || typeof window === 'undefined') return
|
|
252
|
+
listenersAttached = true
|
|
253
|
+
if (typeof window.matchMedia === 'function') {
|
|
254
|
+
darkQuery = window.matchMedia(DARK_QUERY)
|
|
255
|
+
const onChange = () => {
|
|
256
|
+
if (current !== null && current.mode === 'auto') apply({ notify: true })
|
|
257
|
+
}
|
|
258
|
+
if (typeof darkQuery.addEventListener === 'function') darkQuery.addEventListener('change', onChange)
|
|
259
|
+
else if (typeof darkQuery.addListener === 'function') darkQuery.addListener(onChange)
|
|
260
|
+
}
|
|
261
|
+
window.addEventListener('storage', (event) => {
|
|
262
|
+
if (event.key !== THEME_STORAGE_KEYS.family && event.key !== THEME_STORAGE_KEYS.mode) return
|
|
263
|
+
current = { family: storedFamily(), mode: storedMode() }
|
|
264
|
+
apply({ notify: true })
|
|
265
|
+
})
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Resolve initial state and paint it.
|
|
270
|
+
*
|
|
271
|
+
* Precedence: explicit option > previously stored value > attribute already
|
|
272
|
+
* present in the document > defaults.
|
|
273
|
+
*
|
|
274
|
+
* @param {{ family?: ThemeFamily, mode?: ColorMode, persist?: boolean, listen?: boolean }} [options]
|
|
275
|
+
* @returns {ThemeInfo}
|
|
276
|
+
*/
|
|
277
|
+
export function initTheme(options = {}) {
|
|
278
|
+
const { family, mode, persist = false, listen = true } = options
|
|
279
|
+
const root = rootElement()
|
|
280
|
+
|
|
281
|
+
/** @type {ThemeFamily} */
|
|
282
|
+
let nextFamily = storedFamily()
|
|
283
|
+
if (isThemeFamily(family)) nextFamily = family
|
|
284
|
+
else if (root !== null && isThemeFamily(root.getAttribute(FAMILY_ATTRIBUTE))) {
|
|
285
|
+
nextFamily = /** @type {ThemeFamily} */ (root.getAttribute(FAMILY_ATTRIBUTE))
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** @type {ColorMode} */
|
|
289
|
+
let nextMode = storedMode()
|
|
290
|
+
if (isColorMode(mode)) nextMode = mode
|
|
291
|
+
else if (root !== null && isColorMode(root.getAttribute(MODE_CHOICE_ATTRIBUTE))) {
|
|
292
|
+
nextMode = /** @type {ColorMode} */ (root.getAttribute(MODE_CHOICE_ATTRIBUTE))
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
current = { family: nextFamily, mode: nextMode }
|
|
296
|
+
if (listen) attachListeners()
|
|
297
|
+
return apply({ persistFamily: persist, persistMode: persist, notify: true })
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Switch the visual family.
|
|
302
|
+
* @param {ThemeFamily} family
|
|
303
|
+
* @param {{ persist?: boolean }} [options]
|
|
304
|
+
* @returns {ThemeInfo}
|
|
305
|
+
*/
|
|
306
|
+
export function setThemeFamily(family, { persist = true } = {}) {
|
|
307
|
+
if (!isThemeFamily(family)) throw new TypeError(`Unknown theme family: ${String(family)}`)
|
|
308
|
+
if (current === null) current = { family: storedFamily(), mode: storedMode() }
|
|
309
|
+
current = { family, mode: current.mode }
|
|
310
|
+
return apply({ persistFamily: persist, notify: true })
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Choose light, dark, or auto (follow the OS).
|
|
315
|
+
* @param {ColorMode} mode
|
|
316
|
+
* @param {{ persist?: boolean }} [options]
|
|
317
|
+
* @returns {ThemeInfo}
|
|
318
|
+
*/
|
|
319
|
+
export function setColorMode(mode, { persist = true } = {}) {
|
|
320
|
+
if (!isColorMode(mode)) throw new TypeError(`Unknown color mode: ${String(mode)}`)
|
|
321
|
+
if (current === null) current = { family: storedFamily(), mode: storedMode() }
|
|
322
|
+
current = { family: current.family, mode }
|
|
323
|
+
return apply({ persistMode: persist, notify: true })
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Flip between light and dark. The result is an explicit choice (mode becomes
|
|
328
|
+
* light/dark, not auto), which matches how the legacy ◐ button behaved.
|
|
329
|
+
* @returns {ThemeInfo}
|
|
330
|
+
*/
|
|
331
|
+
export function toggleColorMode() {
|
|
332
|
+
if (current === null) current = { family: storedFamily(), mode: storedMode() }
|
|
333
|
+
const next = resolveMode(current.mode) === 'dark' ? 'light' : 'dark'
|
|
334
|
+
current = { family: current.family, mode: next }
|
|
335
|
+
return apply({ persistMode: true, notify: true })
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* @returns {ThemeInfo}
|
|
340
|
+
*/
|
|
341
|
+
export function getTheme() {
|
|
342
|
+
if (current === null) current = { family: storedFamily(), mode: storedMode() }
|
|
343
|
+
return {
|
|
344
|
+
family: current.family,
|
|
345
|
+
mode: current.mode,
|
|
346
|
+
resolvedMode: resolveMode(current.mode),
|
|
347
|
+
prefersDark: prefersDark(),
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Family/mode metadata lookup for UI builders.
|
|
353
|
+
* @param {ThemeFamily} family
|
|
354
|
+
*/
|
|
355
|
+
export function getThemeFamily(family) {
|
|
356
|
+
return THEME_FAMILIES.find((entry) => entry.id === family) ?? null
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** @param {ColorMode} mode */
|
|
360
|
+
export function getColorMode(mode) {
|
|
361
|
+
return COLOR_MODES.find((entry) => entry.id === mode) ?? null
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/* Self-initialize: the module is inert until imported, then paints immediately. */
|
|
365
|
+
if (typeof document !== 'undefined' && !autoInitScheduled) {
|
|
366
|
+
autoInitScheduled = true
|
|
367
|
+
if (document.readyState === 'loading') {
|
|
368
|
+
document.addEventListener('DOMContentLoaded', () => initTheme(), { once: true })
|
|
369
|
+
} else {
|
|
370
|
+
initTheme()
|
|
371
|
+
}
|
|
372
|
+
}
|