dsh-delete-session 0.3.2 → 0.3.3
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 +6 -0
- package/package.json +1 -1
- package/src/client.js +52 -3
- package/test/checkbox.dom.test.js +90 -1
package/README.md
CHANGED
|
@@ -162,6 +162,12 @@ MIT
|
|
|
162
162
|
|
|
163
163
|
## 修复记录
|
|
164
164
|
|
|
165
|
+
**(v0.3.3)** —— 删除当前会话不再顺手新建一个会话。
|
|
166
|
+
|
|
167
|
+
- **问题**:删掉正在看的那个会话之后,`leaveIfCurrent()` 会去侧边栏点「新建会话」按钮,好让主视图别停在那个禁用的合成器上。可那个按钮是**用户**新建会话的入口,凡是拦截「新建会话」的插件(命名弹窗、模板选择、预设工作区)都会跟着触发 —— 结果是「删了一个会话」被做成了「新建一个会话」([#5](https://github.com/DDDMUC/dsh-delete-session/issues/5))。
|
|
168
|
+
- **改用宿主的 `uiWorkspace.clearMain()`**:放掉主引用、释放 retain、把持久化的 `dsh.sessions.current` 一并复位、清空面板。宿主自己就用它表达「没有当前会话」—— 归档当前会话走它,`startSession` 拿不到工作区也走它。
|
|
169
|
+
- **顺带去掉两个副作用**:不再需要 `localStorage.removeItem('dsh.sessions.current')`(`clearMain` 已经复位),也不再需要 `location.reload()`。
|
|
170
|
+
- **旧宿主仍有兜底**:`uiWorkspace` 拿不到(或没有 `clearMain`)时才退回原来的按钮点击 + 重置指针 + reload,保证视图不会停在已删的会话上。服务本身经 `ctx.get` 查一次、拿不到再 `ctx.inject` 补挂。
|
|
165
171
|
**(v0.3.2)** —— 语言跟随 DSH 的界面语言,而不是操作系统。
|
|
166
172
|
|
|
167
173
|
- **问题**:插件的语言只认 `navigator.language`。OS 是英文、DSH 设成中文的用户,看到的是全英文界面,而且没有任何办法改([#3](https://github.com/DDDMUC/dsh-delete-session/issues/3))。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-delete-session",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
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
|
@@ -358,6 +358,10 @@ window.__ModuleLoader__.load({
|
|
|
358
358
|
|
|
359
359
|
let sessionsSvc = null
|
|
360
360
|
let localeSvc = null
|
|
361
|
+
/** `uiWorkspace`: the host's own "this conversation is closed" face. */
|
|
362
|
+
let navigationSvc = null
|
|
363
|
+
/** Kept so the navigation service can be looked up lazily, outside apply(). */
|
|
364
|
+
let hostCtx = null
|
|
361
365
|
|
|
362
366
|
/** Languages this plugin ships dictionaries for. */
|
|
363
367
|
const SHIPPED = ['zh', 'en', 'ru']
|
|
@@ -920,13 +924,34 @@ window.__ModuleLoader__.load({
|
|
|
920
924
|
try { window.location.reload() } catch { /* ignore */ }
|
|
921
925
|
}
|
|
922
926
|
|
|
923
|
-
// Deleting the session the main view is showing
|
|
924
|
-
//
|
|
925
|
-
// which
|
|
927
|
+
// Deleting the session the main view is showing has to leave the app with no
|
|
928
|
+
// current conversation. It used to click the sidebar's "new session" button,
|
|
929
|
+
// which is wrong twice over: that button is the USER entry point, so every
|
|
930
|
+
// plugin that intercepts a new session (a naming dialog, a template picker,
|
|
931
|
+
// a preset workspace) fires on a delete; and the visible result of a delete
|
|
932
|
+
// became "a new conversation was created".
|
|
933
|
+
//
|
|
934
|
+
// The host has a way to express exactly this: `uiWorkspace.clearMain()`
|
|
935
|
+
// drops the main reference, releases its retain, resets the persisted
|
|
936
|
+
// `dsh.sessions.current` pointer and clears the panel. Archiving the current
|
|
937
|
+
// conversation and a failed `startSession` both go through it, so it is the
|
|
938
|
+
// sanctioned "nothing is open" state rather than a side effect of ours.
|
|
926
939
|
function deletedVariants(id) {
|
|
927
940
|
return id.startsWith('session-') ? [id, id.slice('session-'.length)] : [id, `session-${id}`]
|
|
928
941
|
}
|
|
929
942
|
|
|
943
|
+
/** The navigation service, looked up lazily so a late one is still found. */
|
|
944
|
+
function navigationApi() {
|
|
945
|
+
if (navigationSvc !== null) return navigationSvc
|
|
946
|
+
const holder = hostCtx
|
|
947
|
+
if (holder === null || typeof holder.get !== 'function') return navigationSvc
|
|
948
|
+
try {
|
|
949
|
+
const svc = holder.get('uiWorkspace')
|
|
950
|
+
if (svc !== null && svc !== undefined) navigationSvc = svc
|
|
951
|
+
} catch { /* optional service */ }
|
|
952
|
+
return navigationSvc
|
|
953
|
+
}
|
|
954
|
+
|
|
930
955
|
function leaveIfCurrent(id) {
|
|
931
956
|
let current = null
|
|
932
957
|
try {
|
|
@@ -936,6 +961,18 @@ window.__ModuleLoader__.load({
|
|
|
936
961
|
return
|
|
937
962
|
}
|
|
938
963
|
if (!current || !deletedVariants(id).includes(current)) return
|
|
964
|
+
|
|
965
|
+
// Preferred: tell the host, which owns the pointer and the retain.
|
|
966
|
+
const api = navigationApi()
|
|
967
|
+
if (api !== null && typeof api.clearMain === 'function') {
|
|
968
|
+
try {
|
|
969
|
+
api.clearMain()
|
|
970
|
+
return
|
|
971
|
+
} catch { /* older build: fall through */ }
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
// No such service (a host that predates it): the old route, kept so the
|
|
975
|
+
// view is never left pointing at a conversation that no longer exists.
|
|
939
976
|
const btn = document.querySelector(
|
|
940
977
|
'button[class*="newSession"], button[aria-label*="New session" i], button[aria-label*="新会话"]',
|
|
941
978
|
)
|
|
@@ -2770,11 +2807,23 @@ window.__ModuleLoader__.load({
|
|
|
2770
2807
|
}
|
|
2771
2808
|
}
|
|
2772
2809
|
|
|
2810
|
+
hostCtx = ctx
|
|
2773
2811
|
sessionsSvc = ctx.get('sessions') || null
|
|
2774
2812
|
if (!sessionsSvc) {
|
|
2775
2813
|
try { ctx.inject(['sessions'], (sub) => { sessionsSvc = sub.sessions }) } catch { /* optional */ }
|
|
2776
2814
|
}
|
|
2777
2815
|
localeSvc = ctx.get('locale') || null
|
|
2816
|
+
// `uiWorkspace` is a service, not a module: `ctx.get` is an optional lookup,
|
|
2817
|
+
// and a host that provides it later is covered by the inject below.
|
|
2818
|
+
try {
|
|
2819
|
+
const nav = ctx.get('uiWorkspace')
|
|
2820
|
+
if (nav !== null && nav !== undefined) navigationSvc = nav
|
|
2821
|
+
} catch { /* optional service */ }
|
|
2822
|
+
if (navigationSvc === null) {
|
|
2823
|
+
try {
|
|
2824
|
+
ctx.inject(['uiWorkspace'], (sub) => { navigationSvc = sub.uiWorkspace || null })
|
|
2825
|
+
} catch { /* optional service */ }
|
|
2826
|
+
}
|
|
2778
2827
|
|
|
2779
2828
|
// Slot components are handed a `t` bound to this namespace, so the
|
|
2780
2829
|
// dictionaries must reach the host - on BOTH arrival paths. If `locale`
|
|
@@ -188,6 +188,12 @@ function walk(root) {
|
|
|
188
188
|
}
|
|
189
189
|
|
|
190
190
|
function matches(node, selector) {
|
|
191
|
+
// A comma list is an OR in the DOM, and the plugin uses one to find the
|
|
192
|
+
// "new session" button. Without this the stub silently answered "no match"
|
|
193
|
+
// for every alternative, which hid the fallback path entirely.
|
|
194
|
+
if (selector.includes(',')) {
|
|
195
|
+
return selector.split(',').some((part) => matches(node, part.trim()))
|
|
196
|
+
}
|
|
191
197
|
// `:scope > .dsdel-check` - a DIRECT child. The plugin uses this to tell its
|
|
192
198
|
// own row checkbox apart from anything nested, so depth has to be respected.
|
|
193
199
|
const scoped = /^:scope > \.([\w-]+)$/.exec(selector)
|
|
@@ -201,6 +207,23 @@ function matches(node, selector) {
|
|
|
201
207
|
return node._classes.has(selector.slice(1))
|
|
202
208
|
}
|
|
203
209
|
|
|
210
|
+
// `button[class*="newSession"]` - tag-qualified, used by the fallback that
|
|
211
|
+
// finds the host's "new session" button after a delete.
|
|
212
|
+
const tagClassContains = /^([A-Za-z][\w-]*)\[class\*="([^"]*)"\]$/.exec(selector)
|
|
213
|
+
if (tagClassContains !== null) {
|
|
214
|
+
if (String(node.tagName || '').toLowerCase() !== tagClassContains[1].toLowerCase()) return false
|
|
215
|
+
return String(node.className ?? '').includes(tagClassContains[2])
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// `button[aria-label*="New session" i]` - a case-insensitive attribute match.
|
|
219
|
+
const tagAttrContains = /^([A-Za-z][\w-]*)\[([\w-]+)\*="([^"]*)"(?: i)?\]$/.exec(selector)
|
|
220
|
+
if (tagAttrContains !== null) {
|
|
221
|
+
if (String(node.tagName || '').toLowerCase() !== tagAttrContains[1].toLowerCase()) return false
|
|
222
|
+
const value = node.attributes[tagAttrContains[2]]
|
|
223
|
+
if (typeof value !== 'string') return false
|
|
224
|
+
return value.toLowerCase().includes(tagAttrContains[3].toLowerCase())
|
|
225
|
+
}
|
|
226
|
+
|
|
204
227
|
// `[class*="_footArea"]` - how the bar finds the host's sidebar footer.
|
|
205
228
|
const classContains = /^\[class\*="([^"]*)"\]$/.exec(selector)
|
|
206
229
|
if (classContains !== null) return node.className.includes(classContains[1])
|
|
@@ -457,12 +480,16 @@ async function loadPlugin(options = {}) {
|
|
|
457
480
|
// `lateLocale` models a host where the locale service is only reachable
|
|
458
481
|
// through ctx.inject, which is the path that used to skip registration.
|
|
459
482
|
if (name === 'locale') return (options.lateLocale || options.noLocale) ? null : localeStub
|
|
483
|
+
if (name === 'uiWorkspace') return options.noNavigation ? null : (options.navigation || null)
|
|
460
484
|
return null
|
|
461
485
|
},
|
|
462
486
|
inject: (names, cb) => {
|
|
463
487
|
if (typeof cb === 'function') {
|
|
464
488
|
const late = options.lateLocale && !options.noLocale
|
|
465
|
-
|
|
489
|
+
const bag = { slots: slotsStub }
|
|
490
|
+
if (late) bag.locale = localeStub
|
|
491
|
+
if (options.navigation && !options.noNavigation) bag.uiWorkspace = options.navigation
|
|
492
|
+
cb(bag)
|
|
466
493
|
}
|
|
467
494
|
},
|
|
468
495
|
effect: (fn) => { const dispose = fn(); return () => { if (typeof dispose === 'function') dispose() } },
|
|
@@ -874,6 +901,68 @@ test('shift-click forces the dialog even after asking was turned off', async ()
|
|
|
874
901
|
|
|
875
902
|
// --- the regression ---------------------------------------------------------
|
|
876
903
|
|
|
904
|
+
test('deleting the current session clears the main view instead of opening a new one (issue #5)', async () => {
|
|
905
|
+
let cleared = 0
|
|
906
|
+
const harness = await loadPlugin({
|
|
907
|
+
navigation: { clearMain: () => { cleared += 1 } },
|
|
908
|
+
})
|
|
909
|
+
mountRow(harness, SESSION_A)
|
|
910
|
+
localStorage.setItem('dsh.sessions.current', JSON.stringify({ sessionId: SESSION_A.id }))
|
|
911
|
+
|
|
912
|
+
// The host's own session menu row drives the delete; asking is off so it goes
|
|
913
|
+
// straight through, like the reporter did.
|
|
914
|
+
localStorage.setItem(SKIP_KEY, '1')
|
|
915
|
+
const { element } = deleteRow(harness)
|
|
916
|
+
element.props.onSelect({ shiftKey: false })
|
|
917
|
+
await tick()
|
|
918
|
+
await flush()
|
|
919
|
+
|
|
920
|
+
// Issue #5: this used to click the sidebar's "new session" button, which every
|
|
921
|
+
// plugin that intercepts session creation shares (a naming dialog, a template
|
|
922
|
+
// picker). Deleting a conversation must not create one.
|
|
923
|
+
assert.equal(cleared, 1, 'the host was told to drop the main reference')
|
|
924
|
+
assert.equal(harness.requests.filter((c) => c.url === '/dsh-delete-session/delete').length, 1, 'the delete still went out')
|
|
925
|
+
})
|
|
926
|
+
|
|
927
|
+
test('a delete that is not the current session leaves navigation alone', async () => {
|
|
928
|
+
let cleared = 0
|
|
929
|
+
const harness = await loadPlugin({
|
|
930
|
+
navigation: { clearMain: () => { cleared += 1 } },
|
|
931
|
+
})
|
|
932
|
+
mountRow(harness, SESSION_A)
|
|
933
|
+
localStorage.setItem('dsh.sessions.current', JSON.stringify({ sessionId: SESSION_B.id }))
|
|
934
|
+
localStorage.setItem(SKIP_KEY, '1')
|
|
935
|
+
|
|
936
|
+
const { element } = deleteRow(harness)
|
|
937
|
+
element.props.onSelect({ shiftKey: false })
|
|
938
|
+
await tick()
|
|
939
|
+
await flush()
|
|
940
|
+
|
|
941
|
+
assert.equal(cleared, 0, 'nothing to clear: the deleted one was not on screen')
|
|
942
|
+
})
|
|
943
|
+
|
|
944
|
+
test('with no navigation service the sidebar button is the last resort', async () => {
|
|
945
|
+
const harness = await loadPlugin({ noNavigation: true })
|
|
946
|
+
mountRow(harness, SESSION_A)
|
|
947
|
+
localStorage.setItem('dsh.sessions.current', JSON.stringify({ sessionId: SESSION_A.id }))
|
|
948
|
+
localStorage.setItem(SKIP_KEY, '1')
|
|
949
|
+
|
|
950
|
+
// A host that predates `clearMain` still has to end up with no current
|
|
951
|
+
// conversation, so the old fallback stays. It is only reached when the service
|
|
952
|
+
// is genuinely absent.
|
|
953
|
+
let clicked = 0
|
|
954
|
+
const btn = harness.document.createElement('button')
|
|
955
|
+
btn.className = 'newSession'
|
|
956
|
+
btn.click = () => { clicked += 1 }
|
|
957
|
+
harness.document.body.appendChild(btn)
|
|
958
|
+
|
|
959
|
+
const { element } = deleteRow(harness)
|
|
960
|
+
element.props.onSelect({ shiftKey: false })
|
|
961
|
+
await tick()
|
|
962
|
+
await flush()
|
|
963
|
+
|
|
964
|
+
assert.equal(clicked, 1, 'the fallback still runs without the service')
|
|
965
|
+
})
|
|
877
966
|
test('a single delete plants no checkbox on any row (the v0.1.8 regression)', async () => {
|
|
878
967
|
const harness = await loadPlugin()
|
|
879
968
|
mountRow(harness, SESSION_A)
|