dsh-reminder-notify 0.8.1 → 0.8.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/client.js +84 -17
- package/index.js +78 -0
- package/package.json +1 -1
- package/publish/index.json +2 -2
package/client.js
CHANGED
|
@@ -16,12 +16,16 @@ window.__ModuleLoader__.load({
|
|
|
16
16
|
const NS = 'dsh-reminder-notify'
|
|
17
17
|
// 与 index.js 里 Host 注册的路由保持一致(同源 GET,返回 { dir, files, error? })。
|
|
18
18
|
const AUDIO_ROUTE = '/dsh-reminder-notify/audio'
|
|
19
|
+
const CONFIG_ROUTE = '/dsh-reminder-notify/config'
|
|
19
20
|
const AUDIO_KEYS = ['doneAudio', 'decideAudio', 'rawAudio']
|
|
20
21
|
|
|
21
22
|
const zh = {
|
|
22
23
|
title: '桌面提醒设置',
|
|
23
|
-
intro: '
|
|
24
|
+
intro: '调整弹窗停留时间、位置、提示音和卡片配色。改完点保存,立刻生效,不用重启。',
|
|
24
25
|
unavailable: '这个页面拿不到插件配置(可能是宿主版本较旧),请改用 cordis.patch.yml 修改。',
|
|
26
|
+
loading: '正在读取插件配置…',
|
|
27
|
+
loadFailed: '读取配置失败:',
|
|
28
|
+
retry: '重试',
|
|
25
29
|
readonly: '当前配置由更高优先级的层覆盖,这里只能查看。',
|
|
26
30
|
groupBasic: '基本',
|
|
27
31
|
groupAudio: '提示音',
|
|
@@ -95,8 +99,11 @@ window.__ModuleLoader__.load({
|
|
|
95
99
|
|
|
96
100
|
const en = {
|
|
97
101
|
title: 'Desktop reminder settings',
|
|
98
|
-
intro: 'Tune the popup duration, position, sounds and card colours. Saving
|
|
102
|
+
intro: 'Tune the popup duration, position, sounds and card colours. Saving applies immediately, no restart.',
|
|
99
103
|
unavailable: 'This page cannot read the plugin config (older host). Edit cordis.patch.yml instead.',
|
|
104
|
+
loading: 'Reading the plugin config…',
|
|
105
|
+
loadFailed: 'Could not read the config: ',
|
|
106
|
+
retry: 'Retry',
|
|
100
107
|
readonly: 'A higher-priority layer overrides this config; it is read-only here.',
|
|
101
108
|
groupBasic: 'Basics',
|
|
102
109
|
groupAudio: 'Sounds',
|
|
@@ -265,17 +272,50 @@ window.__ModuleLoader__.load({
|
|
|
265
272
|
|
|
266
273
|
function ReminderConfigPanel(props) {
|
|
267
274
|
const t = props.t
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
const
|
|
271
|
-
const
|
|
272
|
-
const
|
|
275
|
+
// 配置读写走插件自己注册的同源 HTTP 路由(Host 端用 configEditor 落盘),
|
|
276
|
+
// 不依赖宿主是否把 ConfigPageForm 传进槽位——那条链路在部分宿主版本里拿不到。
|
|
277
|
+
const [state, setState] = React.useState({ status: 'loading', values: {}, writable: true, error: null })
|
|
278
|
+
const [reload, setReload] = React.useState(0)
|
|
279
|
+
const values = state.values
|
|
280
|
+
const writable = state.writable !== false
|
|
273
281
|
const [draft, setDraft] = React.useState(null)
|
|
274
282
|
const [busy, setBusy] = React.useState(false)
|
|
275
283
|
const [picking, setPicking] = React.useState(false)
|
|
276
284
|
const [message, setMessage] = React.useState(null)
|
|
277
285
|
const [audio, setAudio] = React.useState({ dir: '', files: [], error: null })
|
|
278
286
|
|
|
287
|
+
React.useEffect(() => {
|
|
288
|
+
if (typeof fetch !== 'function') {
|
|
289
|
+
setState({ status: 'error', values: {}, writable: false, error: 'fetch is unavailable' })
|
|
290
|
+
return undefined
|
|
291
|
+
}
|
|
292
|
+
let cancelled = false
|
|
293
|
+
Promise.resolve()
|
|
294
|
+
.then(() => fetch(CONFIG_ROUTE, { headers: { accept: 'application/json' } }))
|
|
295
|
+
.then((response) => {
|
|
296
|
+
const status = response ? response.status : 0
|
|
297
|
+
const body = response && typeof response.json === 'function' ? response.json() : Promise.resolve(null)
|
|
298
|
+
return Promise.resolve(body)
|
|
299
|
+
.catch(() => null)
|
|
300
|
+
.then((data) => {
|
|
301
|
+
if (response && response.ok && data && data.ok !== false) return data
|
|
302
|
+
const detail = data && data.error ? data.error : 'HTTP ' + status
|
|
303
|
+
throw new Error(detail)
|
|
304
|
+
})
|
|
305
|
+
})
|
|
306
|
+
.then((data) => {
|
|
307
|
+
if (cancelled) return
|
|
308
|
+
setState({ status: 'ready', values: (data && data.values) || {}, writable: (data && data.writable) !== false, error: null })
|
|
309
|
+
})
|
|
310
|
+
.catch((error) => {
|
|
311
|
+
if (cancelled) return
|
|
312
|
+
setState({ status: 'error', values: {}, writable: false, error: String((error && error.message) || error) })
|
|
313
|
+
})
|
|
314
|
+
return () => {
|
|
315
|
+
cancelled = true
|
|
316
|
+
}
|
|
317
|
+
}, [reload])
|
|
318
|
+
|
|
279
319
|
// 音频候选:Host 端注册了只读路由 /dsh-reminder-notify/audio,页面同源 fetch 拿目录清单。
|
|
280
320
|
// 拿不到(旧宿主 / 路由被拦)就静默降级成手填文件名,不打扰用户。
|
|
281
321
|
const scanDir = (draft && typeof draft.audioDir === 'string' && draft.audioDir !== '' ? draft.audioDir : values.audioDir) || ''
|
|
@@ -306,8 +346,21 @@ window.__ModuleLoader__.load({
|
|
|
306
346
|
return h('span', null, `${tr('summaryPrefix')}${tr(posKey)} · ${values.seconds === undefined ? 60 : values.seconds}${tr('secondsUnit')} · ${sound}`)
|
|
307
347
|
}
|
|
308
348
|
|
|
309
|
-
if (
|
|
310
|
-
return h('div', { className: 'drn-panel' }, h('div', { className: 'drn-hint' }, typeof t === 'function' ? t('
|
|
349
|
+
if (state.status === 'loading') {
|
|
350
|
+
return h('div', { className: 'drn-panel' }, h('div', { className: 'drn-hint' }, typeof t === 'function' ? t('loading') : 'loading…'))
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (state.status === 'error') {
|
|
354
|
+
return h(
|
|
355
|
+
'div',
|
|
356
|
+
{ className: 'drn-panel' },
|
|
357
|
+
h('div', { className: 'drn-hint error' }, (typeof t === 'function' ? t('loadFailed') : 'cannot read config: ') + (state.error || '')),
|
|
358
|
+
h(
|
|
359
|
+
'div',
|
|
360
|
+
{ className: 'drn-actions' },
|
|
361
|
+
h('button', { type: 'button', className: 'drn-btn', onClick: () => setReload(reload + 1) }, typeof t === 'function' ? t('retry') : 'Retry'),
|
|
362
|
+
),
|
|
363
|
+
)
|
|
311
364
|
}
|
|
312
365
|
|
|
313
366
|
const effective = draft === null ? values : Object.assign({}, values, draft)
|
|
@@ -322,27 +375,41 @@ window.__ModuleLoader__.load({
|
|
|
322
375
|
setDraft(Object.assign({}, draft || {}, { [key]: value }))
|
|
323
376
|
}
|
|
324
377
|
|
|
325
|
-
function
|
|
326
|
-
const
|
|
378
|
+
function changedPatch() {
|
|
379
|
+
const patch = {}
|
|
327
380
|
for (const field of FIELDS) {
|
|
328
381
|
const current = effective[field.key]
|
|
329
382
|
const saved = values[field.key]
|
|
330
383
|
if (sameValue(current, saved)) continue
|
|
331
|
-
|
|
384
|
+
patch[field.key] = current === undefined ? '' : current
|
|
332
385
|
}
|
|
333
|
-
return
|
|
386
|
+
return patch
|
|
334
387
|
}
|
|
335
388
|
|
|
336
|
-
const
|
|
337
|
-
const dirty =
|
|
389
|
+
const patch = changedPatch()
|
|
390
|
+
const dirty = Object.keys(patch).length > 0
|
|
338
391
|
|
|
339
392
|
function save() {
|
|
340
393
|
if (!dirty || busy) return
|
|
341
394
|
setBusy(true)
|
|
342
395
|
setMessage(null)
|
|
343
|
-
Promise.resolve(
|
|
344
|
-
.then(() =>
|
|
396
|
+
Promise.resolve()
|
|
397
|
+
.then(() =>
|
|
398
|
+
fetch(CONFIG_ROUTE, {
|
|
399
|
+
method: 'POST',
|
|
400
|
+
headers: { 'content-type': 'application/json', accept: 'application/json' },
|
|
401
|
+
body: JSON.stringify({ values: patch }),
|
|
402
|
+
}),
|
|
403
|
+
)
|
|
404
|
+
.then((response) =>
|
|
405
|
+
response && typeof response.json === 'function'
|
|
406
|
+
? response.json().then((data) => ({ ok: !!(response.ok && data && data.ok !== false), data }))
|
|
407
|
+
: Promise.reject(new Error('HTTP ' + (response ? response.status : '?'))),
|
|
408
|
+
)
|
|
409
|
+
.then(({ ok, data }) => {
|
|
345
410
|
setBusy(false)
|
|
411
|
+
if (!ok) throw new Error((data && data.error) || 'save rejected')
|
|
412
|
+
setState({ status: 'ready', values: (data && data.values) || effective, writable: (data && data.writable) !== false, error: null })
|
|
346
413
|
setDraft(null)
|
|
347
414
|
setMessage({ ok: true, text: typeof t === 'function' ? t('saved') : 'saved' })
|
|
348
415
|
})
|
package/index.js
CHANGED
|
@@ -434,6 +434,7 @@ function toolDefinition(getSettings) {
|
|
|
434
434
|
|
|
435
435
|
const AUDIO_EXTENSIONS = ['.wav', '.mp3', '.ogg', '.m4a', '.flac', '.aac', '.wma']
|
|
436
436
|
const AUDIO_ROUTE = '/dsh-reminder-notify/audio'
|
|
437
|
+
const CONFIG_ROUTE = '/dsh-reminder-notify/config'
|
|
437
438
|
|
|
438
439
|
// 面板要「把音频目录里的文件列成下拉」,但 Client 侧没有任何读文件能力,
|
|
439
440
|
// 所以由 Host 注册一条只读的 GET 路由,页面用同源 fetch 取这个 JSON。
|
|
@@ -487,8 +488,85 @@ function registerAudioRoute(ctx, getSettings) {
|
|
|
487
488
|
}, 'dsh-reminder-notify: audio list route')
|
|
488
489
|
}
|
|
489
490
|
|
|
491
|
+
function readJsonBody(req, limit = 256 * 1024) {
|
|
492
|
+
return new Promise((resolve, reject) => {
|
|
493
|
+
let size = 0
|
|
494
|
+
const chunks = []
|
|
495
|
+
req.on('data', (chunk) => {
|
|
496
|
+
size += chunk.length
|
|
497
|
+
if (size > limit) {
|
|
498
|
+
reject(new Error('request body is too large'))
|
|
499
|
+
req.destroy()
|
|
500
|
+
return
|
|
501
|
+
}
|
|
502
|
+
chunks.push(chunk)
|
|
503
|
+
})
|
|
504
|
+
req.on('end', () => {
|
|
505
|
+
const text = Buffer.concat(chunks).toString('utf8').trim()
|
|
506
|
+
if (text === '') return resolve({})
|
|
507
|
+
try {
|
|
508
|
+
resolve(JSON.parse(text))
|
|
509
|
+
} catch (error) {
|
|
510
|
+
reject(error)
|
|
511
|
+
}
|
|
512
|
+
})
|
|
513
|
+
req.on('error', reject)
|
|
514
|
+
})
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
function registerConfigRoute(ctx, getSettings) {
|
|
518
|
+
const web = typeof ctx.get === 'function' ? ctx.get('webServer') : undefined
|
|
519
|
+
if (web === undefined || web === null || typeof web.register !== 'function') {
|
|
520
|
+
ctx.logger?.warn?.('dsh-reminder-notify: webServer is unavailable, the settings panel stays read-only')
|
|
521
|
+
return
|
|
522
|
+
}
|
|
523
|
+
const editable = new Set(Object.keys(settingsOf({})))
|
|
524
|
+
const reply = (res, status, payload) => {
|
|
525
|
+
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
526
|
+
res.end(JSON.stringify(payload))
|
|
527
|
+
}
|
|
528
|
+
const route = {
|
|
529
|
+
kind: 'exact',
|
|
530
|
+
path: CONFIG_ROUTE,
|
|
531
|
+
handler: async (req, res) => {
|
|
532
|
+
try {
|
|
533
|
+
if ((req.method ?? 'GET').toUpperCase() === 'GET') {
|
|
534
|
+
return reply(res, 200, { ok: true, writable: true, values: getSettings() })
|
|
535
|
+
}
|
|
536
|
+
const body = await readJsonBody(req)
|
|
537
|
+
const patch = body !== null && typeof body === 'object' && body.values !== null && typeof body.values === 'object'
|
|
538
|
+
? body.values
|
|
539
|
+
: body
|
|
540
|
+
const entry = ctx.fiber?.entry
|
|
541
|
+
const editor = typeof ctx.get === 'function' ? ctx.get('configEditor') : undefined
|
|
542
|
+
if (entry === undefined || editor === undefined || typeof editor.edit !== 'function') {
|
|
543
|
+
return reply(res, 409, { ok: false, error: 'configuration cannot be saved in this profile' })
|
|
544
|
+
}
|
|
545
|
+
const changes = {}
|
|
546
|
+
for (const [key, value] of Object.entries(patch ?? {})) {
|
|
547
|
+
if (editable.has(key)) changes[key] = value
|
|
548
|
+
}
|
|
549
|
+
if (Object.keys(changes).length === 0) {
|
|
550
|
+
return reply(res, 400, { ok: false, error: 'no editable field in the request' })
|
|
551
|
+
}
|
|
552
|
+
await editor.edit(entry, (current) => ({ ...current, ...changes }))
|
|
553
|
+
return reply(res, 200, { ok: true, writable: true, values: getSettings() })
|
|
554
|
+
} catch (error) {
|
|
555
|
+
return reply(res, 500, { ok: false, error: String(error?.message ?? error) })
|
|
556
|
+
}
|
|
557
|
+
},
|
|
558
|
+
}
|
|
559
|
+
ctx.effect(() => {
|
|
560
|
+
const dispose = web.register(route)
|
|
561
|
+
return () => {
|
|
562
|
+
if (typeof dispose === 'function') dispose()
|
|
563
|
+
}
|
|
564
|
+
}, 'dsh-reminder-notify: settings route')
|
|
565
|
+
}
|
|
566
|
+
|
|
490
567
|
export function apply(ctx, config) {
|
|
491
568
|
registerAudioRoute(ctx, () => settingsOf(config))
|
|
569
|
+
registerConfigRoute(ctx, () => settingsOf(config))
|
|
492
570
|
const tools = typeof ctx.get === 'function' ? ctx.get('tools') : ctx.tools
|
|
493
571
|
if (tools === undefined || tools === null || typeof tools.register !== 'function') {
|
|
494
572
|
ctx.logger?.warn?.('dsh-reminder-notify: tools service is unavailable, notify_user was not registered')
|
package/package.json
CHANGED