dsh-delete-session 0.3.0 → 0.3.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/README.md CHANGED
@@ -162,6 +162,19 @@ MIT
162
162
 
163
163
  ## 修复记录
164
164
 
165
+ **(v0.3.2)** —— 语言跟随 DSH 的界面语言,而不是操作系统。
166
+
167
+ - **问题**:插件的语言只认 `navigator.language`。OS 是英文、DSH 设成中文的用户,看到的是全英文界面,而且没有任何办法改([#3](https://github.com/DDDMUC/dsh-delete-session/issues/3))。
168
+ - **现在跟随宿主**:先读 `locale` 服务的 `getSnapshot().active`(`dsh-client-locale` 就是这么暴露当前语言的);服务取不到就读 `document.documentElement.lang`(宿主每次切换都会把选择同步到这里);两者都没有才退回浏览器语言。
169
+ - **宿主语言我们没收录时的落点**:DSH 自己还认识 ja/es/fr/de/pt。这些没有对应字典,宿主对它们也只会答英文,所以插件答英文 —— 而不是像原来那样直接从浏览器猜,猜不到就掉回中文。
170
+ - **菜单和底栏不能再分裂**:原来会话菜单对 zh/en 一律交给宿主翻译,只有俄语才用插件字典。跟随宿主语言之后这条规则会让同一种语言出现两个来源 —— DSH 设成英文时菜单英文、底栏和弹窗中文。现在统一先查插件字典,未收录的键才交给宿主。
171
+ - **切换语言即时生效**:订阅 `locale.subscribe()`,宿主改语言时底栏和弹窗跟着重渲染,不需要刷新页面。
172
+ **(v0.3.1)** —— 批量删除也会处理产出文件了。
173
+
174
+ - **批量删除原来完全不碰产出文件**:单删会带上 `quarantine`,批量那条却调 `postDelete(id)` 什么都不传,宿主按缺省当「不动文件」。于是用户明明开着「产出文件默认全部进隔离夹」,批量删时文件**原地留着**,而且没有任何提示——看起来像做了,其实没做。现在两条路径共用 `quarantinePlan()`。
175
+ - **批量也会报文件数**:「已删除 N 个会话,M 个文件已移入隔离夹」;有失败则分开报成功与失败数。原来是干巴巴一句「已删除 N 个会话」,看不出文件去哪了。
176
+ - **询问模式下批量会明说**:勾着「产出文件先询问」时批量删不了清单(100 个会话挨个弹窗不现实),所以文件确实没处理。现在补一句提示,告诉用户要么单个删、要么在插件页改成自动隔离——而不是让它静默过去。
177
+ - 顺带修了测试夹具跨用例串扰:批量成功后排的 2.5 秒 `settlePendingSoon` 定时器会活到下一个用例,用**旧插件的闭包**去装饰**新用例的 DOM**,导致三条毫不相关的用例随机变红。现在每条用例开始前清掉遗留定时器,连跑 5 次稳定。
165
178
  **(v0.3.0)** —— 多选支持按工作区全选,也能一次选中全部会话。
166
179
 
167
180
  - **两个全选框**:`工作区 N` 那一行多一个框,选中**所有**会话;每个工作区标题行(含「未分组」)各多一个框,只选中**该组**。点一下全选,再点一下清空。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-delete-session",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "Delete sessions from the DSH Web sidebar: adds a Delete action to the session row menu with a risk-consent dialog, sends the session log, workspace accounting and live session state to the system trash (or removes them outright), and, when opted in, quarantines the files the conversation produced.",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/client.js CHANGED
@@ -59,6 +59,9 @@ window.__ModuleLoader__.load({
59
59
  deleteSelected: '删除',
60
60
  batchDone: (n) => `已删除 ${n} 个会话`,
61
61
  batchPartial: (ok, fail) => `已删除 ${ok} 个,${fail} 个失败`,
62
+ batchDoneQuarantine: (n, m) => `已删除 ${n} 个会话,${m} 个文件已移入隔离夹`,
63
+ batchDoneQuarantineFailed: (n, ok, fail) => `已删除 ${n} 个会话,隔离 ${ok} 个、${fail} 个失败`,
64
+ batchAskArtifacts: '这批会话的产出文件没有处理:批量删除不列逐文件清单。要处理它们,请单个删除,或在插件页改成「产出文件默认全部进隔离夹」。',
62
65
  batchFailed: '批量删除失败:',
63
66
  pageAsk: '删除时询问',
64
67
  pageRecycle: '删除至回收站',
@@ -165,6 +168,9 @@ window.__ModuleLoader__.load({
165
168
  deleteSelected: 'Delete',
166
169
  batchDone: (n) => `Deleted ${n} session(s)`,
167
170
  batchPartial: (ok, fail) => `Deleted ${ok}, ${fail} failed`,
171
+ batchDoneQuarantine: (n, m) => `Deleted ${n} session(s), ${m} file(s) moved to quarantine`,
172
+ batchDoneQuarantineFailed: (n, ok, fail) => `Deleted ${n} session(s), ${ok} quarantined and ${fail} failed`,
173
+ batchAskArtifacts: 'The produced files of these sessions were left alone: a batch has no per-file checklist. Delete them one by one, or switch the plugin page to "move every produced file to quarantine".',
168
174
  batchFailed: 'Batch delete failed: ',
169
175
  pageAsk: 'Ask before deleting',
170
176
  pageRecycle: 'Delete to the Recycle Bin',
@@ -268,6 +274,9 @@ window.__ModuleLoader__.load({
268
274
  deleteSelected: 'Удалить',
269
275
  batchDone: (n) => `Удалено сессий: ${n}`,
270
276
  batchPartial: (ok, fail) => `Удалено ${ok}, не удалось ${fail}`,
277
+ batchDoneQuarantine: (n, m) => `Удалено сессий: ${n}, файлов в карантине: ${m}`,
278
+ batchDoneQuarantineFailed: (n, ok, fail) => `Удалено сессий: ${n}, в карантине ${ok}, не удалось ${fail}`,
279
+ batchAskArtifacts: 'Файлы этих сессий не обработаны: пакетное удаление не показывает список файлов. Удаляйте по одному или переключите страницу плагина на «все созданные файлы в карантин». ',
271
280
  batchFailed: 'Массовое удаление не удалось: ',
272
281
  pageAsk: 'Спрашивать перед удалением',
273
282
  pageRecycle: 'Удалять в корзину',
@@ -350,11 +359,52 @@ window.__ModuleLoader__.load({
350
359
  let sessionsSvc = null
351
360
  let localeSvc = null
352
361
 
362
+ /** Languages this plugin ships dictionaries for. */
363
+ const SHIPPED = ['zh', 'en', 'ru']
364
+
365
+ /**
366
+ * The language DSH itself is showing, or null when the host offers none.
367
+ *
368
+ * `dsh-client-locale` provides a `locale` service: `getSnapshot()` returns
369
+ * `{ active }` and `subscribe()` fires on every change. It also mirrors the
370
+ * choice onto `document.documentElement.lang`, which covers the case where the
371
+ * service is not reachable but the page has already been localised.
372
+ */
373
+ function hostLang() {
374
+ if (localeSvc !== null && typeof localeSvc.getSnapshot === 'function') {
375
+ try {
376
+ const snapshot = localeSvc.getSnapshot()
377
+ const active = snapshot && typeof snapshot.active === 'string' ? snapshot.active : null
378
+ if (active !== null) return active
379
+ } catch { /* fall through to the document hint */ }
380
+ }
381
+ if (typeof document !== 'undefined' && document.documentElement) {
382
+ const html = document.documentElement.lang
383
+ if (typeof html === 'string' && html !== '') return html
384
+ }
385
+ return null
386
+ }
387
+
388
+ /**
389
+ * Which of our dictionaries to read.
390
+ *
391
+ * The host is authoritative and the browser is the fallback: following
392
+ * `navigator.language` alone meant an English OS rendered an English plugin
393
+ * even with DSH switched to 中文. A host language we do not ship (DSH knows
394
+ * ja/es/fr/de/pt as well) falls back to Russian for ru, English for the rest,
395
+ * because the host would also answer in one of those two.
396
+ */
353
397
  function browserLang() {
398
+ const host = hostLang()
399
+ if (host !== null) {
400
+ const primary = String(host).toLowerCase().split('-')[0]
401
+ if (primary === 'zh' || primary === 'en' || primary === 'ru') return primary
402
+ return primary === 'ru' ? 'ru' : 'en'
403
+ }
354
404
  if (typeof navigator === 'undefined') return 'zh'
355
405
  for (const tag of (navigator.languages || []).concat([navigator.language])) {
356
406
  const primary = String(tag || '').toLowerCase().split('-')[0]
357
- if (primary === 'zh' || primary === 'en' || primary === 'ru') return primary
407
+ if (SHIPPED.indexOf(primary) !== -1) return primary
358
408
  }
359
409
  return 'zh'
360
410
  }
@@ -435,17 +485,18 @@ window.__ModuleLoader__.load({
435
485
  }
436
486
 
437
487
  /**
438
- * Pick the translator the session-menu rows render through.
488
+ * The translator every slot component renders through.
439
489
  *
440
- * The host injects a `t` bound to this namespace and stays the authority for
441
- * the languages DSH itself ships (zh, en): following it is what keeps the menu
442
- * in step when the UI language is changed in the settings. For a Russian
443
- * browser there is nothing to follow - DSH ships no ru locale - so the plugin's
444
- * own dictionary answers first and the rows do not drop back to English.
490
+ * The plugin ships its own dictionaries and follows the host's language
491
+ * (issue #3), so its dictionary is asked first for every language it carries,
492
+ * not just Russian. Deferring to the injected `t` for zh/en would split one
493
+ * language across two authorities: with DSH set to English the menu would say
494
+ * "Delete session" while the bar and the dialog stayed Chinese. Keys this
495
+ * plugin does not ship still go to the host.
445
496
  */
446
497
  function slotTranslator(injected) {
447
498
  const raw = typeof injected === 'function' ? injected : t
448
- return browserLang() === 'ru' ? localizedTranslator(raw) : raw
499
+ return localizedTranslator(raw)
449
500
  }
450
501
 
451
502
  // --- state -----------------------------------------------------------------
@@ -598,6 +649,20 @@ window.__ModuleLoader__.load({
598
649
  return () => { prefListeners.delete(listener) }
599
650
  }
600
651
 
652
+ /**
653
+ * The `quarantine` payload a delete should carry, or undefined for "do not
654
+ * touch any file".
655
+ *
656
+ * Only `auto` mode can answer this without a dialog: asking means handing the
657
+ * user a per-file checklist, which a batch of sessions has nowhere to put.
658
+ * The batch path used to skip this entirely, so a user who had switched the
659
+ * feature on got a silent no-op instead of a quarantine.
660
+ */
661
+ function quarantinePlan() {
662
+ if (artifactsOff() || !artifactsAuto()) return undefined
663
+ return { scope: artifactScope() }
664
+ }
665
+
601
666
  /** Whether the produced-files feature is off. Off unless explicitly enabled. */
602
667
  function artifactsOff() {
603
668
  return readPref(OFF_KEY, '1') !== '0'
@@ -1654,6 +1719,15 @@ window.__ModuleLoader__.load({
1654
1719
  for (const id of ids) state.pending.add(id)
1655
1720
  publish()
1656
1721
 
1722
+ // The same switch the single path honours; without this every request went
1723
+ // out with no `quarantine` field and the files stayed where they were.
1724
+ const quarantine = quarantinePlan()
1725
+ // The feature is on but `auto` is off: the user expects files to be handled
1726
+ // and a batch cannot show the per-file checklist. Say so afterwards rather
1727
+ // than pretending the files were dealt with.
1728
+ const askOnly = !artifactsOff() && !artifactsAuto()
1729
+ let movedFiles = 0
1730
+ let failedFiles = 0
1657
1731
  const failedIds = []
1658
1732
  let cursor = 0
1659
1733
  const worker = async () => {
@@ -1663,7 +1737,12 @@ window.__ModuleLoader__.load({
1663
1737
  if (index >= ids.length) return
1664
1738
  const id = ids[index]
1665
1739
  try {
1666
- await postDelete(id)
1740
+ const result = await postDelete(id, quarantine)
1741
+ const outcome = result && result.quarantine ? result.quarantine : null
1742
+ if (outcome !== null) {
1743
+ movedFiles += outcome.moved.length
1744
+ failedFiles += outcome.failed.length
1745
+ }
1667
1746
  state.batch.done += 1
1668
1747
  } catch {
1669
1748
  state.batch.failed += 1
@@ -1681,12 +1760,22 @@ window.__ModuleLoader__.load({
1681
1760
  const ok = state.batch ? state.batch.done : 0
1682
1761
  state.batch = null
1683
1762
  if (failed === 0) {
1684
- toast(t('batchDone', ok))
1763
+ // The files are reported with the count, so a user who moved a hundred
1764
+ // of them sees that number instead of a bare "deleted".
1765
+ if (failedFiles > 0) {
1766
+ toast(t('batchDoneQuarantineFailed', ok, movedFiles, failedFiles), true)
1767
+ } else if (movedFiles > 0) {
1768
+ toast(t('batchDoneQuarantine', ok, movedFiles))
1769
+ } else {
1770
+ toast(t('batchDone', ok))
1771
+ }
1772
+ if (askOnly) toast(t('batchAskArtifacts'), true)
1685
1773
  exitSelection({ keepPending: true })
1686
1774
  settlePendingSoon()
1687
1775
  } else {
1688
1776
  for (const id of failedIds) state.selected.add(id)
1689
1777
  toast(t('batchPartial', ok, failed), true)
1778
+ if (askOnly) toast(t('batchAskArtifacts'), true)
1690
1779
  publish()
1691
1780
  }
1692
1781
  refreshList()
@@ -1765,7 +1854,7 @@ window.__ModuleLoader__.load({
1765
1854
  // and the menu row forces the dialog in that case. With the feature off no
1766
1855
  // quarantine field is sent at all, keeping the request identical to the
1767
1856
  // version that predates the feature.
1768
- const quarantine = !artifactsOff() && artifactsAuto() ? { scope: artifactScope() } : undefined
1857
+ const quarantine = quarantinePlan()
1769
1858
  postDelete(info.sessionId, quarantine)
1770
1859
  .then((result) => {
1771
1860
  const outcome = result && result.quarantine ? result.quarantine : null
@@ -2694,8 +2783,20 @@ window.__ModuleLoader__.load({
2694
2783
  // printing raw keys ("multiSelect", "menu"). `ctx.effect` ties the
2695
2784
  // registration's lifetime to the plugin, matching ui-workspace.
2696
2785
  let localeRegistered = false
2786
+ let localeSubscribed = false
2697
2787
  const adoptLocale = (svc) => {
2698
2788
  localeSvc = svc || null
2789
+ // The bar, the dialog and the toasts are rendered by this plugin, so they
2790
+ // do not inherit the host's re-render when the language changes in the
2791
+ // settings. Piggy-backing on the service's own notification keeps them in
2792
+ // step with the menu rows, which the host and re-render itself.
2793
+ if (localeSvc !== null && typeof localeSvc.subscribe === 'function' && !localeSubscribed) {
2794
+ localeSubscribed = true
2795
+ try {
2796
+ const off = localeSvc.subscribe(() => { try { publish() } catch { /* not mounted yet */ } })
2797
+ if (typeof ctx.effect === 'function' && typeof off === 'function') ctx.effect(() => off, `${NS}: language changes`)
2798
+ } catch { /* a host without subscribe still renders on its own */ }
2799
+ }
2699
2800
  if (localeRegistered || !localeSvc || typeof localeSvc.register !== 'function') return
2700
2801
  localeRegistered = true
2701
2802
  const register = () => localeSvc.register(NS, { zh, en, ru })