dsh-session-relocate 0.0.0-stage → 0.2.0
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/LICENSE +21 -0
- package/README.md +263 -2
- package/cordis.patch.yml +23 -0
- package/docs/DESIGN.md +278 -0
- package/lib/client.js +1189 -0
- package/package.json +62 -4
- package/scripts/drag-live-check.mjs +284 -0
- package/scripts/drag-verify.mjs +270 -0
- package/scripts/e2e.mjs +209 -0
- package/src/paths.js +123 -0
- package/src/plugin.js +884 -0
- package/src/relocate.js +860 -0
- package/src/zstd.js +165 -0
package/lib/client.js
ADDED
|
@@ -0,0 +1,1189 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-session-relocate —— 网页端。
|
|
3
|
+
*
|
|
4
|
+
* 往侧边栏「会话行 -> ... 」菜单里注入一项「移动到」:点开列出其它工作区,
|
|
5
|
+
* 选中后调用主机侧桥接 /api/dsh-session-relocate/move 完成迁移。
|
|
6
|
+
*
|
|
7
|
+
* 形态是 DSH 客户端的 lazy-CJS 包:只注册一个工厂,模块体副作用在首次物化时才跑。
|
|
8
|
+
* 唯一的外部依赖是 react(平台基座模块),因此不需要打包器。
|
|
9
|
+
*
|
|
10
|
+
* 有意只用 <button role="menuitem"> 作为树的节点(不套 div):菜单的键盘遍历读 DOM,
|
|
11
|
+
* 这样每一项都能正常加入,也不会破坏既有菜单结构。
|
|
12
|
+
*/
|
|
13
|
+
window.__ModuleLoader__.load({
|
|
14
|
+
id: 'dsh-session-relocate',
|
|
15
|
+
factory: function (require) {
|
|
16
|
+
var module = { exports: {} };
|
|
17
|
+
var exports = module.exports;
|
|
18
|
+
var react = require('react');
|
|
19
|
+
|
|
20
|
+
var STYLE_ID = 'dsh-session-relocate-styles';
|
|
21
|
+
var CSS = [
|
|
22
|
+
'.dshsm-item{display:block;width:100%;box-sizing:border-box;text-align:left;font:inherit;font-size:13px;line-height:1.45;padding:6px 10px;border:0;border-radius:6px;background:0 0;color:var(--dsw-alias-label-primary,inherit);cursor:pointer;white-space:normal;overflow-wrap:anywhere}',
|
|
23
|
+
'.dshsm-item:hover:not(:disabled){background:var(--dsw-alias-fill-secondary,rgba(127,127,127,.12))}',
|
|
24
|
+
'.dshsm-item:disabled{color:var(--dsw-alias-label-caption,#8b8b8b);cursor:default}',
|
|
25
|
+
'.dshsm-target{padding-left:18px}',
|
|
26
|
+
'.dshsm-meta{display:block;font-size:11px;color:var(--dsw-alias-label-caption,#8b8b8b);overflow-wrap:anywhere}',
|
|
27
|
+
'.dshsm-bad{color:#ff453a}',
|
|
28
|
+
// 拖动时给工作区行加的高亮(官方行本身没有 drop 语义,所以这个类只由本插件加)
|
|
29
|
+
'.dshsm-drop{outline:2px solid #4c8dff;outline-offset:-2px;border-radius:6px}',
|
|
30
|
+
// 多选模式:行首勾选圈 + 底部操作条 + 目标工作区面板
|
|
31
|
+
'.dshsm-check{display:inline-flex;align-items:center;justify-content:center;width:18px;height:18px;margin-right:6px;border-radius:50%;border:1.5px solid var(--dsw-alias-border-l3,rgba(128,128,128,.5));background:0 0;flex:none}',
|
|
32
|
+
'.dshsm-check-on{border-color:#4c8dff;background:#4c8dff;box-shadow:inset 0 0 0 3px var(--dsw-alias-fill-elevated,#fff)}',
|
|
33
|
+
'.dshsm-bar{position:fixed;left:12px;bottom:12px;z-index:2147483600;display:flex;align-items:center;gap:10px;padding:10px 12px;border-radius:12px;border:1px solid var(--dsw-alias-border-l3,rgba(128,128,128,.3));background:var(--dsw-alias-fill-elevated,#1f232b);color:var(--dsw-alias-label-primary,#fff);box-shadow:0 8px 28px rgba(0,0,0,.28)}',
|
|
34
|
+
'.dshsm-bar-count{font-size:13px;white-space:nowrap}',
|
|
35
|
+
'.dshsm-bar-btn{appearance:none;min-height:30px;padding:0 12px;border-radius:8px;border:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.4));background:0 0;color:inherit;font:inherit;font-size:13px;cursor:pointer}',
|
|
36
|
+
'.dshsm-bar-btn:hover:not(:disabled){background:var(--dsw-alias-fill-secondary,rgba(127,127,127,.16))}',
|
|
37
|
+
'.dshsm-bar-btn:disabled{opacity:.5;cursor:default}',
|
|
38
|
+
'.dshsm-bar-primary{border-color:#4c8dff;background:#4c8dff;color:#fff}',
|
|
39
|
+
'.dshsm-picker-backdrop{position:fixed;inset:0;z-index:2147483602;display:flex;align-items:center;justify-content:center;background:rgba(0,0,0,.28)}',
|
|
40
|
+
'.dshsm-picker{width:min(440px,92vw);max-height:70vh;overflow:auto;display:flex;flex-direction:column;gap:10px;padding:16px;border-radius:14px;background:var(--dsw-alias-fill-elevated,#1f232b);color:var(--dsw-alias-label-primary,#fff);border:1px solid var(--dsw-alias-border-l3,rgba(128,128,128,.3));box-shadow:0 18px 48px rgba(0,0,0,.36)}',
|
|
41
|
+
'.dshsm-picker-title{font-size:14px;font-weight:600}',
|
|
42
|
+
'.dshsm-picker-list{display:flex;flex-direction:column;gap:6px}',
|
|
43
|
+
'.dshsm-picker-item{display:flex;flex-direction:column;gap:2px;width:100%;text-align:left;padding:8px 10px;border-radius:8px;border:1px solid transparent;background:0 0;color:inherit;font:inherit;font-size:13px;cursor:pointer}',
|
|
44
|
+
'.dshsm-picker-item:hover{background:var(--dsw-alias-fill-secondary,rgba(127,127,127,.16))}',
|
|
45
|
+
'.dshsm-picker-name{font-weight:500}',
|
|
46
|
+
'.dshsm-picker-path{font-size:11px;color:var(--dsw-alias-label-caption,#8b8b8b);overflow-wrap:anywhere}',
|
|
47
|
+
'.dshsm-picker-empty{font-size:12px;color:var(--dsw-alias-label-caption,#8b8b8b)}',
|
|
48
|
+
'.dshsm-picker-actions{display:flex;justify-content:flex-end}',
|
|
49
|
+
'.dshsm-picker-btn{appearance:none;min-height:30px;padding:0 12px;border-radius:8px;border:1px solid var(--dsw-alias-border-l2,rgba(128,128,128,.4));background:0 0;color:inherit;font:inherit;font-size:13px;cursor:pointer}',
|
|
50
|
+
].join('');
|
|
51
|
+
|
|
52
|
+
function ensureStyles() {
|
|
53
|
+
if (typeof document === 'undefined') return;
|
|
54
|
+
if (document.querySelector('style[data-plugin-css="' + STYLE_ID + '"]') !== null) return;
|
|
55
|
+
var tag = document.createElement('style');
|
|
56
|
+
tag.dataset.plugin = 'dsh-session-relocate';
|
|
57
|
+
tag.dataset.pluginCss = STYLE_ID;
|
|
58
|
+
tag.textContent = CSS;
|
|
59
|
+
document.head.appendChild(tag);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 引导全局。0.1.0 起叫 __DSH_SESSION_RELOCATE__;**改名过渡期**里主机侧可能还是旧版
|
|
64
|
+
* (注入的是 __DSH_SESSION_MOVE__),所以两个名字都认 —— 否则「客户端已改名、主机还没重启」
|
|
65
|
+
* 这半截状态下,页面刷新后菜单和拖动会去请求一个主机根本不服务的前缀。
|
|
66
|
+
*/
|
|
67
|
+
function bootGlobal() {
|
|
68
|
+
if (typeof globalThis === 'undefined') return null;
|
|
69
|
+
var current = globalThis.__DSH_SESSION_RELOCATE__;
|
|
70
|
+
if (current !== null && current !== undefined) return current;
|
|
71
|
+
var legacy = globalThis.__DSH_SESSION_MOVE__;
|
|
72
|
+
if (legacy !== null && legacy !== undefined) return legacy;
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 桥接前缀由主机侧通过引导全局下发;取不到就用默认值。 */
|
|
77
|
+
function bridgePrefix() {
|
|
78
|
+
var boot = bootGlobal();
|
|
79
|
+
if (boot !== null && typeof boot === 'object' && typeof boot.prefix === 'string' && boot.prefix.length > 0) {
|
|
80
|
+
return boot.prefix;
|
|
81
|
+
}
|
|
82
|
+
return '/api/dsh-session-relocate';
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function isChinese() {
|
|
86
|
+
var language = globalThis.navigator !== undefined ? globalThis.navigator.language : '';
|
|
87
|
+
return typeof language === 'string' && /^zh/i.test(language);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
var ZH = {
|
|
91
|
+
open: '移动到',
|
|
92
|
+
loading: '正在读取工作区…',
|
|
93
|
+
pick: '移动到:',
|
|
94
|
+
noTargets: '没有其它工作区',
|
|
95
|
+
chooseDir: '其它目录…',
|
|
96
|
+
chooseDirPrompt: '输入目标目录的绝对路径(不存在会被创建并注册成新工作区):',
|
|
97
|
+
moving: '正在移动…',
|
|
98
|
+
done: '已移动',
|
|
99
|
+
pending: '文件已搬移;工作区归属会在下次启动 DSH 时自动补齐',
|
|
100
|
+
cannot: '这个对话无法移动',
|
|
101
|
+
failed: '移动失败',
|
|
102
|
+
needsReload: '(刷新页面后生效)',
|
|
103
|
+
dragMoved: '已移动到「{ws}」',
|
|
104
|
+
dragFailed: '移动失败:',
|
|
105
|
+
bulk: '多选',
|
|
106
|
+
bulkMove: '移动',
|
|
107
|
+
bulkCancel: '取消',
|
|
108
|
+
bulkSelected: '已选 {n} 个',
|
|
109
|
+
bulkPicking: '移动 {n} 个对话到:',
|
|
110
|
+
bulkProgress: '正在移动 {d}/{t}…',
|
|
111
|
+
bulkDone: '已移动 {ok} 个',
|
|
112
|
+
bulkPartial: '已移动 {ok} 个,{fail} 个失败',
|
|
113
|
+
bulkSkipped: '({n} 个本来就在目标工作区)',
|
|
114
|
+
bulkFailed: '批量移动失败:',
|
|
115
|
+
noteReleaseRetries: '(释放重试 {n} 次)',
|
|
116
|
+
noteSampled: '(大会话:抽样校验)',
|
|
117
|
+
};
|
|
118
|
+
var EN = {
|
|
119
|
+
open: 'Move to',
|
|
120
|
+
loading: 'Loading workspaces…',
|
|
121
|
+
pick: 'Move to:',
|
|
122
|
+
noTargets: 'No other workspace',
|
|
123
|
+
chooseDir: 'Other directory…',
|
|
124
|
+
chooseDirPrompt: 'Absolute path of the target directory (it is created and registered as a new workspace if missing):',
|
|
125
|
+
moving: 'Moving…',
|
|
126
|
+
done: 'Moved',
|
|
127
|
+
pending: 'Files moved; the workspace accounting is completed on the next DSH start',
|
|
128
|
+
cannot: 'This conversation cannot be moved',
|
|
129
|
+
failed: 'Move failed',
|
|
130
|
+
needsReload: ' (reload the page to apply)',
|
|
131
|
+
dragMoved: 'Moved to “{ws}”',
|
|
132
|
+
dragFailed: 'Move failed: ',
|
|
133
|
+
bulk: 'Select multiple',
|
|
134
|
+
bulkMove: 'Move',
|
|
135
|
+
bulkCancel: 'Cancel',
|
|
136
|
+
bulkSelected: '{n} selected',
|
|
137
|
+
bulkPicking: 'Move {n} conversations to:',
|
|
138
|
+
bulkProgress: 'Moving {d}/{t}…',
|
|
139
|
+
bulkDone: 'Moved {ok}',
|
|
140
|
+
bulkPartial: 'Moved {ok}, {fail} failed',
|
|
141
|
+
bulkSkipped: ' ({n} already there)',
|
|
142
|
+
bulkFailed: 'Bulk move failed: ',
|
|
143
|
+
noteReleaseRetries: ' (release retried {n}×)',
|
|
144
|
+
noteSampled: ' (large session: sampled verify)',
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
function text() {
|
|
148
|
+
return isChinese() ? ZH : EN;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function post(path, body) {
|
|
152
|
+
var options = { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body !== undefined ? body : {}) };
|
|
153
|
+
return fetch(bridgePrefix() + path, options).then(function (response) {
|
|
154
|
+
return response.json().then(function (payload) { return payload; }, function () {
|
|
155
|
+
return { ok: false, code: 'bad-response', message: 'HTTP ' + String(response.status) };
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function errorText(error) {
|
|
161
|
+
if (error !== null && typeof error === 'object' && typeof error.message === 'string' && error.message.length > 0) {
|
|
162
|
+
return error.message;
|
|
163
|
+
}
|
|
164
|
+
return String(error);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* 把回执里值得说一句的信息拼成后缀:释放重试了几次、大会话是不是只做了抽样校验。
|
|
169
|
+
* 没有要说的就返回空串。
|
|
170
|
+
*/
|
|
171
|
+
function receiptNotes(result) {
|
|
172
|
+
if (result === null || typeof result !== 'object') return '';
|
|
173
|
+
var strings = text();
|
|
174
|
+
var notes = [];
|
|
175
|
+
if (Number.isSafeInteger(result.releaseAttempts) && result.releaseAttempts > 1) {
|
|
176
|
+
notes.push(strings.noteReleaseRetries.replace('{n}', String(result.releaseAttempts)));
|
|
177
|
+
}
|
|
178
|
+
if (result.verification === 'sampled') notes.push(strings.noteSampled);
|
|
179
|
+
return notes.length === 0 ? '' : notes.join('');
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** 渲染一个菜单按钮。 */
|
|
183
|
+
function button(key, label, options) {
|
|
184
|
+
var props = Object.assign({ key: key, type: 'button', role: 'menuitem', className: 'dshsm-item' }, options || {});
|
|
185
|
+
return react.createElement('button', props, label);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** 带标题 + 灰色副标题的两行按钮内容。 */
|
|
189
|
+
function twoLine(title, meta) {
|
|
190
|
+
return [title, react.createElement('span', { key: 'meta', className: 'dshsm-meta' }, meta)];
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* 会话行菜单项。
|
|
195
|
+
* props: sessionId / displayTitle / useMenuOpenState(由 slot 提供)。
|
|
196
|
+
*/
|
|
197
|
+
function Entry(props) {
|
|
198
|
+
var sessionId = props.sessionId;
|
|
199
|
+
var useMenuOpenState = props.useMenuOpenState;
|
|
200
|
+
|
|
201
|
+
// 必须在顶层无条件调用(hooks 规则)。
|
|
202
|
+
var menuState = null;
|
|
203
|
+
if (typeof useMenuOpenState === 'function') {
|
|
204
|
+
try {
|
|
205
|
+
menuState = useMenuOpenState();
|
|
206
|
+
} catch (error) {
|
|
207
|
+
menuState = null;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
var setMenuOpen = menuState !== null && Array.isArray(menuState) && typeof menuState[1] === 'function' ? menuState[1] : null;
|
|
211
|
+
|
|
212
|
+
var stateHook = react.useState({ phase: 'idle' });
|
|
213
|
+
var state = stateHook[0];
|
|
214
|
+
var setState = stateHook[1];
|
|
215
|
+
var strings = text();
|
|
216
|
+
|
|
217
|
+
function open() {
|
|
218
|
+
setState({ phase: 'loading' });
|
|
219
|
+
post('/describe', { sessionId: sessionId }).then(function (result) {
|
|
220
|
+
if (result === null || typeof result !== 'object' || result.ok !== true) {
|
|
221
|
+
setState({ phase: 'error', message: (result !== null && typeof result === 'object' && result.message !== undefined) ? result.message : strings.failed });
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
var session = result.session;
|
|
225
|
+
var all = Array.isArray(result.workspaces) ? result.workspaces : [];
|
|
226
|
+
var targets = all.filter(function (workspace) { return workspace.current !== true; });
|
|
227
|
+
if (session === null || typeof session !== 'object' || session.canMove !== true) {
|
|
228
|
+
setState({ phase: 'blocked', message: (session !== null && typeof session === 'object' && session.reason !== undefined && session.reason !== null) ? session.reason : strings.cannot });
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
setState({ phase: 'picking', targets: targets });
|
|
232
|
+
}, function (error) {
|
|
233
|
+
setState({ phase: 'error', message: errorText(error) });
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function run(request) {
|
|
238
|
+
setState({ phase: 'moving' });
|
|
239
|
+
post('/move', Object.assign({ sessionId: sessionId }, request)).then(function (result) {
|
|
240
|
+
if (result === null || typeof result !== 'object' || result.ok !== true) {
|
|
241
|
+
setState({ phase: 'error', message: (result !== null && typeof result === 'object' && result.message !== undefined) ? result.message : strings.failed });
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
setState({
|
|
245
|
+
phase: 'done',
|
|
246
|
+
title: result.targetTitle !== undefined ? result.targetTitle : result.to,
|
|
247
|
+
applied: result.applied,
|
|
248
|
+
note: receiptNotes(result),
|
|
249
|
+
});
|
|
250
|
+
// 成功后排这一行会被工作区列表刷新带走;这里顺手关掉菜单更干净。
|
|
251
|
+
if (setMenuOpen !== null) setMenuOpen(false);
|
|
252
|
+
}, function (error) {
|
|
253
|
+
setState({ phase: 'error', message: errorText(error) });
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function chooseDirectory() {
|
|
258
|
+
var picked = globalThis.prompt(strings.chooseDirPrompt, '');
|
|
259
|
+
if (typeof picked !== 'string' || picked.trim().length === 0) return;
|
|
260
|
+
run({ targetPath: picked.trim() });
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
if (state.phase === 'idle') {
|
|
264
|
+
return button('open', strings.open, { onClick: open });
|
|
265
|
+
}
|
|
266
|
+
if (state.phase === 'loading') {
|
|
267
|
+
return button('loading', strings.loading, { disabled: true });
|
|
268
|
+
}
|
|
269
|
+
if (state.phase === 'moving') {
|
|
270
|
+
return button('moving', strings.moving, { disabled: true });
|
|
271
|
+
}
|
|
272
|
+
if (state.phase === 'blocked') {
|
|
273
|
+
return button('blocked', state.message, { disabled: true });
|
|
274
|
+
}
|
|
275
|
+
if (state.phase === 'error') {
|
|
276
|
+
return button('error', strings.failed + ':' + state.message, { disabled: true, className: 'dshsm-item dshsm-bad' });
|
|
277
|
+
}
|
|
278
|
+
if (state.phase === 'done') {
|
|
279
|
+
var suffix = state.applied === 'pending' ? strings.pending : (state.applied === 'live' ? '' : strings.needsReload);
|
|
280
|
+
var note = typeof state.note === 'string' ? state.note : '';
|
|
281
|
+
return button('done', strings.done + ' ' + String(state.title) + note + (suffix.length > 0 ? ';' + suffix : ''), { disabled: true });
|
|
282
|
+
}
|
|
283
|
+
// picking
|
|
284
|
+
var nodes = [button('caption', strings.pick + (state.targets.length === 0 ? ' ' + strings.noTargets : ''), { disabled: true })];
|
|
285
|
+
for (var i = 0; i < state.targets.length; i++) {
|
|
286
|
+
(function (workspace) {
|
|
287
|
+
nodes.push(button('ws-' + workspace.id, twoLine(workspace.title, workspace.path), {
|
|
288
|
+
className: 'dshsm-item dshsm-target',
|
|
289
|
+
onClick: function () { run({ targetWorkspaceId: workspace.id }); },
|
|
290
|
+
}));
|
|
291
|
+
})(state.targets[i]);
|
|
292
|
+
}
|
|
293
|
+
nodes.push(button('choose-dir', strings.chooseDir, { className: 'dshsm-item dshsm-target', onClick: chooseDirectory }));
|
|
294
|
+
return react.createElement(react.Fragment, null, nodes);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// ── 拖动搬移 ────────────────────────────────────────────────────────────
|
|
298
|
+
//
|
|
299
|
+
// 官方侧边栏给每一行都写了稳定的 data-row-key(client-ui-workspace):
|
|
300
|
+
// 会话行 data-row-key="session:<sessionId>" (client.js:2250)
|
|
301
|
+
// 工作区行 data-row-key="workspace:<workspaceId>" (client.js:1945,key 就是 workspaceId)
|
|
302
|
+
// 所以这里**不依赖任何未公开的拖拽载荷**(dataTransfer 在 dragover 阶段本来就取不到
|
|
303
|
+
// 数据),只读这两个属性。
|
|
304
|
+
//
|
|
305
|
+
// 官方自己的拖动只做排序:会话拖到别的会话行上 = 在**本工作区**内重排
|
|
306
|
+
// (commitSessionDrag → setSessionOrder(accountKey, …),accountKey 就是源工作区),
|
|
307
|
+
// 拖到工作区行上官方什么都不做。跨工作区搬移是官方没有的语义,所以由本插件接管。
|
|
308
|
+
//
|
|
309
|
+
// 关键:按**分组归属**决定介不介入,而不是按「指针下那一行是不是文件夹行」。
|
|
310
|
+
// 官方每个工作区渲染成一个分组容器(renderGroup → div.groupSection:先文件夹行、再它的会话行),
|
|
311
|
+
// 于是:
|
|
312
|
+
// - 指针在**别的**分组里(文件夹行 / 它的任意一条会话行 / 分组里的空隙)→ 接管,搬过去;
|
|
313
|
+
// - 指针在**它自己所在**的那个分组里 → 完全让路,官方的工作区内排序照旧。
|
|
314
|
+
// 这样「拖到对应工作区那块区域就能换」成立,官方的组内重排一点也没被抢。
|
|
315
|
+
var ROW_KEY_ATTR = 'data-row-key';
|
|
316
|
+
var SESSION_PREFIX = 'session:';
|
|
317
|
+
var WORKSPACE_PREFIX = 'workspace:';
|
|
318
|
+
var DROP_CLASS = 'dshsm-drop';
|
|
319
|
+
var TOAST_ATTR = 'data-dshsm-toast';
|
|
320
|
+
var ROW_WALK_LIMIT = 8;
|
|
321
|
+
|
|
322
|
+
var dragState = {
|
|
323
|
+
sessionId: null,
|
|
324
|
+
sessionIds: null,
|
|
325
|
+
sourceWorkspaceId: null,
|
|
326
|
+
targetSection: null,
|
|
327
|
+
hostSelected: false,
|
|
328
|
+
};
|
|
329
|
+
var dragInstallRefs = 0;
|
|
330
|
+
var toastNode = null;
|
|
331
|
+
var toastTimer = null;
|
|
332
|
+
|
|
333
|
+
/** 从事件目标向上找最近的 data-row-key 元素(会话行 / 工作区行都不是彼此的祖先)。 */
|
|
334
|
+
function nearestRow(target) {
|
|
335
|
+
var node = target;
|
|
336
|
+
for (var depth = 0; depth < ROW_WALK_LIMIT && node !== null && node !== undefined; depth += 1) {
|
|
337
|
+
if (typeof node.getAttribute === 'function') {
|
|
338
|
+
var key = node.getAttribute(ROW_KEY_ATTR);
|
|
339
|
+
if (typeof key === 'string' && key.length > 0) return node;
|
|
340
|
+
}
|
|
341
|
+
node = node.parentElement;
|
|
342
|
+
}
|
|
343
|
+
return null;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
var WORKSPACE_ROW_SELECTOR = '[' + ROW_KEY_ATTR + '^="' + WORKSPACE_PREFIX + '"]';
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* 指针下这个位置属于哪个工作区分组 —— **整块分组区域都算投放目标**,不再只认那 34px 的文件夹行。
|
|
350
|
+
*
|
|
351
|
+
* 官方侧边栏把每个工作区渲染成一个分组容器(renderGroup → div.groupSection:先放工作区行,
|
|
352
|
+
* 再放它自己的会话行)。所以「最近的、恰好只含一个工作区行的祖先」就是这个分组:
|
|
353
|
+
* - 指针落在文件夹行上 → 就是它自己那一层;
|
|
354
|
+
* - 指针落在它下面任意一条会话行上(或「展开其余 N 个会话」上、或分组里的空隙上)→ 向上走到同一个容器;
|
|
355
|
+
* - 再往上整棵 role="tree" 列表含多个工作区行,说明指针不在任何分组里 → null。
|
|
356
|
+
*
|
|
357
|
+
* 拿不到分组(老版本 DSH / 结构变了)就返回 null,自动退化成「只认工作区行」的老行为。
|
|
358
|
+
*/
|
|
359
|
+
function workspaceGroupOf(target) {
|
|
360
|
+
var node = target;
|
|
361
|
+
var best = null;
|
|
362
|
+
for (var depth = 0; depth < ROW_WALK_LIMIT && node !== null && node !== undefined; depth += 1) {
|
|
363
|
+
// 列表根(role="tree")之上就再没有分组了,别把整棵列表当成分组。
|
|
364
|
+
if (typeof node.getAttribute === 'function' && node.getAttribute('role') === 'tree') break;
|
|
365
|
+
if (typeof node.querySelectorAll === 'function') {
|
|
366
|
+
var rows = node.querySelectorAll(WORKSPACE_ROW_SELECTOR);
|
|
367
|
+
if (rows !== null && rows !== undefined && typeof rows.length === 'number') {
|
|
368
|
+
// 含多个工作区行 = 已经越过分组这一层(整棵列表 / 滚动容器),停。
|
|
369
|
+
if (rows.length > 1) break;
|
|
370
|
+
// 恰好一个 = 就是「这个分组」(或行外面那层 wrapper)→ 继续向上取最高的那一层。
|
|
371
|
+
if (rows.length === 1) best = { section: node, row: rows[0], workspaceId: workspaceIdOf(rows[0]) };
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
node = node.parentElement;
|
|
375
|
+
}
|
|
376
|
+
return best !== null && best.workspaceId !== null ? best : null;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function workspaceIdOf(row) {
|
|
380
|
+
if (row === null || row === undefined || typeof row.getAttribute !== 'function') return null;
|
|
381
|
+
var key = row.getAttribute(ROW_KEY_ATTR);
|
|
382
|
+
if (typeof key !== 'string' || key.indexOf(WORKSPACE_PREFIX) !== 0) return null;
|
|
383
|
+
var id = key.slice(WORKSPACE_PREFIX.length);
|
|
384
|
+
return id.length > 0 ? id : null;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function toggleDropClass(row, on) {
|
|
388
|
+
if (row === null || row === undefined) return;
|
|
389
|
+
var list = row.classList;
|
|
390
|
+
if (list === undefined || list === null || typeof list.add !== 'function') return;
|
|
391
|
+
if (on) list.add(DROP_CLASS);
|
|
392
|
+
else list.remove(DROP_CLASS);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/** 高亮**整块分组**(不是那一行):让人一眼看出「松手就搬到这个工作区」。 */
|
|
396
|
+
function highlightGroup(section) {
|
|
397
|
+
if (dragState.targetSection === section) return;
|
|
398
|
+
clearHighlight();
|
|
399
|
+
dragState.targetSection = section;
|
|
400
|
+
toggleDropClass(section, true);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
function clearHighlight() {
|
|
404
|
+
toggleDropClass(dragState.targetSection, false);
|
|
405
|
+
dragState.targetSection = null;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
function resetDrag() {
|
|
409
|
+
clearHighlight();
|
|
410
|
+
dragState.sessionId = null;
|
|
411
|
+
dragState.sessionIds = null;
|
|
412
|
+
dragState.sourceWorkspaceId = null;
|
|
413
|
+
dragState.hostSelected = false;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/** 解析投放目标:没有正在拖的会话、或指针不在任何工作区分组里,就返回 null。 */
|
|
417
|
+
function resolveDrop(event) {
|
|
418
|
+
if (dragState.sessionId === null) return null;
|
|
419
|
+
var target = event !== null && event !== undefined ? event.target : null;
|
|
420
|
+
var group = workspaceGroupOf(target);
|
|
421
|
+
if (group === null) {
|
|
422
|
+
// 退化路径:读不到分组结构(老版本 DSH / 结构变了)时,回到「只认工作区行」的老行为,
|
|
423
|
+
// 绝不因为读不到就去乱接管(那会破坏官方的工作区内排序)。
|
|
424
|
+
var row = nearestRow(target);
|
|
425
|
+
var fallbackId = row === null ? null : workspaceIdOf(row);
|
|
426
|
+
if (fallbackId === null) return null;
|
|
427
|
+
if (dragState.sourceWorkspaceId !== null && dragState.sourceWorkspaceId === fallbackId) return null;
|
|
428
|
+
return { sessionId: dragState.sessionId, targetWorkspaceId: fallbackId, section: row };
|
|
429
|
+
}
|
|
430
|
+
// 落在**它自己所在的那个分组**里 = 官方的工作区内排序区,让路(官方照旧重排)。
|
|
431
|
+
if (dragState.sourceWorkspaceId !== null && dragState.sourceWorkspaceId === group.workspaceId) return null;
|
|
432
|
+
return { sessionId: dragState.sessionId, targetWorkspaceId: group.workspaceId, section: group.section };
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* 当前多选中的会话 id。两个来源:
|
|
437
|
+
* - 我们自己的多选模式(bulkState);
|
|
438
|
+
* - dsh-delete-session 的多选底栏:它给选中行的复选框挂 `.dsdel-check-on`
|
|
439
|
+
* (那是它的 DOM 契约,我们只读、不改)。
|
|
440
|
+
* 没有多选时返回空数组。
|
|
441
|
+
*/
|
|
442
|
+
function selectedIdsForDrag() {
|
|
443
|
+
if (bulkState.selecting === true) return bulkSelectedIds();
|
|
444
|
+
if (typeof document === 'undefined' || typeof document.querySelectorAll !== 'function') return [];
|
|
445
|
+
var marks = document.querySelectorAll('.dsdel-check-on');
|
|
446
|
+
var ids = [];
|
|
447
|
+
for (var i = 0; marks !== null && marks !== undefined && i < marks.length; i += 1) {
|
|
448
|
+
var row = nearestRow(marks[i]);
|
|
449
|
+
var id = row === null ? null : sessionIdFromRow(row);
|
|
450
|
+
if (id !== null && ids.indexOf(id) < 0) ids.push(id);
|
|
451
|
+
}
|
|
452
|
+
return ids;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
function onDragStart(event) {
|
|
456
|
+
var row = nearestRow(event !== null && event !== undefined ? event.target : null);
|
|
457
|
+
if (row === null || typeof row.getAttribute !== 'function') return;
|
|
458
|
+
var key = row.getAttribute(ROW_KEY_ATTR);
|
|
459
|
+
if (typeof key !== 'string' || key.indexOf(SESSION_PREFIX) !== 0) return;
|
|
460
|
+
var sessionId = key.slice(SESSION_PREFIX.length);
|
|
461
|
+
if (sessionId.length === 0) return;
|
|
462
|
+
resetDrag();
|
|
463
|
+
dragState.sessionId = sessionId;
|
|
464
|
+
// 多选拖动:宿主底栏(或我们自己的多选)里选中的整批一起搬。
|
|
465
|
+
var selectedIds = selectedIdsForDrag();
|
|
466
|
+
var multi = selectedIds.length > 1 && selectedIds.indexOf(sessionId) >= 0;
|
|
467
|
+
dragState.sessionIds = multi ? selectedIds : [sessionId];
|
|
468
|
+
dragState.hostSelected = bulkState.selecting !== true && selectedIds.indexOf(sessionId) >= 0;
|
|
469
|
+
// 源工作区**每次**从 DOM 现读(同步、稳):拖起来的那一行就在它自己的分组里。
|
|
470
|
+
// 不能跨拖动缓存 —— 会话可能刚被搬走,旧分组已经不是它的家了。
|
|
471
|
+
// 读不到再问主机;还是拿不到也无所谓 —— 服务端会用 already-there 兜住。
|
|
472
|
+
var own = workspaceGroupOf(row);
|
|
473
|
+
if (own !== null) {
|
|
474
|
+
dragState.sourceWorkspaceId = own.workspaceId;
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
post('/describe', { sessionId: sessionId }).then(function (result) {
|
|
478
|
+
var session = result !== null && result !== undefined ? result.session : null;
|
|
479
|
+
if (session === null || session === undefined) return;
|
|
480
|
+
if (dragState.sessionId === sessionId) dragState.sourceWorkspaceId = session.currentWorkspaceId;
|
|
481
|
+
}, function () { /* 探测失败就让服务端兜 */ });
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function onDragOver(event) {
|
|
485
|
+
if (dragState.sessionId === null) return;
|
|
486
|
+
var drop = resolveDrop(event);
|
|
487
|
+
if (drop === null) {
|
|
488
|
+
clearHighlight();
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
if (typeof event.preventDefault === 'function') event.preventDefault();
|
|
492
|
+
var transfer = event.dataTransfer;
|
|
493
|
+
if (transfer !== undefined && transfer !== null) {
|
|
494
|
+
try {
|
|
495
|
+
transfer.dropEffect = 'move';
|
|
496
|
+
} catch (error) { /* 只读实现就算了 */ }
|
|
497
|
+
}
|
|
498
|
+
highlightGroup(drop.section);
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
function onDrop(event) {
|
|
502
|
+
if (dragState.sessionId === null) return;
|
|
503
|
+
var drop = resolveDrop(event);
|
|
504
|
+
if (drop === null) {
|
|
505
|
+
clearHighlight();
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
// 必须吃掉这个事件:官方在会话行上还有一套排序用的 drop 处理。
|
|
509
|
+
if (typeof event.stopPropagation === 'function') event.stopPropagation();
|
|
510
|
+
if (typeof event.preventDefault === 'function') event.preventDefault();
|
|
511
|
+
var sessionId = drop.sessionId;
|
|
512
|
+
var targetWorkspaceId = drop.targetWorkspaceId;
|
|
513
|
+
var ids = dragState.sessionIds !== null && dragState.sessionIds.length > 1 ? dragState.sessionIds : [sessionId];
|
|
514
|
+
var hostSelected = dragState.hostSelected === true;
|
|
515
|
+
resetDrag();
|
|
516
|
+
if (ids.length > 1) moveManyByDrag(ids, targetWorkspaceId, hostSelected);
|
|
517
|
+
else moveByDrag(sessionId, targetWorkspaceId, hostSelected);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function onDragEnd() {
|
|
521
|
+
resetDrag();
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
function onDragLeave(event) {
|
|
525
|
+
var related = event !== null && event !== undefined ? event.relatedTarget : null;
|
|
526
|
+
if (related === null || related === undefined) {
|
|
527
|
+
clearHighlight();
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
if (typeof document !== 'undefined' && document.body !== undefined && document.body !== null
|
|
531
|
+
&& typeof document.body.contains === 'function' && document.body.contains(related) === false) {
|
|
532
|
+
clearHighlight();
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/** 拖完立刻搬(不再问一次;搬错了再拖回去就行)。 */
|
|
537
|
+
function moveByDrag(sessionId, targetWorkspaceId, hostSelected) {
|
|
538
|
+
var strings = text();
|
|
539
|
+
toast(strings.moving);
|
|
540
|
+
post('/move', { sessionId: sessionId, targetWorkspaceId: targetWorkspaceId }).then(function (result) {
|
|
541
|
+
if (result !== null && result !== undefined && result.ok === true) {
|
|
542
|
+
var title = result.targetTitle;
|
|
543
|
+
var label = typeof title === 'string' && title.length > 0 ? title : targetWorkspaceId;
|
|
544
|
+
toast(strings.dragMoved.replace('{ws}', label) + receiptNotes(result));
|
|
545
|
+
if (bulkState.selecting === true) exitBulk();
|
|
546
|
+
else if (hostSelected === true) dismissHostSelection();
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
var message = result !== null && result !== undefined && typeof result.message === 'string' ? result.message : '';
|
|
550
|
+
toast(strings.dragFailed + message);
|
|
551
|
+
}, function (error) {
|
|
552
|
+
toast(strings.dragFailed + errorText(error));
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
/** 多选拖动:整批搬到同一个目标(目标就是松手的分组,不再弹选择面板)。 */
|
|
557
|
+
function moveManyByDrag(ids, targetWorkspaceId, hostSelected) {
|
|
558
|
+
var strings = text();
|
|
559
|
+
toast(strings.moving);
|
|
560
|
+
runBulkMove(ids, targetWorkspaceId, {
|
|
561
|
+
onProgress: function (done, total) {
|
|
562
|
+
toast(strings.bulkProgress.replace('{d}', String(done)).replace('{t}', String(total)));
|
|
563
|
+
},
|
|
564
|
+
}).then(function (result) {
|
|
565
|
+
var summary = result.failed === 0
|
|
566
|
+
? strings.bulkDone.replace('{ok}', String(result.moved))
|
|
567
|
+
: strings.bulkPartial.replace('{ok}', String(result.moved)).replace('{fail}', String(result.failed));
|
|
568
|
+
if (result.skipped > 0) summary += strings.bulkSkipped.replace('{n}', String(result.skipped));
|
|
569
|
+
summary += receiptNotes(result);
|
|
570
|
+
toast(summary);
|
|
571
|
+
if (bulkState.selecting === true) exitBulk();
|
|
572
|
+
else if (hostSelected === true) dismissHostSelection();
|
|
573
|
+
refreshSidebar();
|
|
574
|
+
}, function (error) {
|
|
575
|
+
toast(strings.bulkFailed + errorText(error));
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/**
|
|
580
|
+
* 收起宿主(dsh-delete-session)的多选态:它监听 document 的 Escape(capture),
|
|
581
|
+
* 我们只发一个 Escape,不碰它的内部状态。
|
|
582
|
+
*/
|
|
583
|
+
function dismissHostSelection() {
|
|
584
|
+
if (typeof document === 'undefined' || typeof document.dispatchEvent !== 'function') return;
|
|
585
|
+
var event = typeof KeyboardEvent === 'function'
|
|
586
|
+
? new KeyboardEvent('keydown', { key: 'Escape', bubbles: true })
|
|
587
|
+
: { type: 'keydown', key: 'Escape', bubbles: true, stopPropagation: function () {}, preventDefault: function () {} };
|
|
588
|
+
try { document.dispatchEvent(event); } catch (error) { /* ignore */ }
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/** 自绘 toast(不依赖官方是否暴露通知服务)。 */
|
|
592
|
+
function toast(message) {
|
|
593
|
+
if (typeof document === 'undefined' || document.body === undefined || document.body === null) return;
|
|
594
|
+
if (toastNode === null || toastNode.parentNode === null || toastNode.parentNode === undefined) {
|
|
595
|
+
toastNode = document.createElement('div');
|
|
596
|
+
toastNode.setAttribute(TOAST_ATTR, '');
|
|
597
|
+
toastNode.style.cssText = 'position:fixed;left:50%;bottom:28px;transform:translateX(-50%);z-index:10000;'
|
|
598
|
+
+ 'max-width:70vw;padding:8px 14px;border-radius:8px;background:rgba(28,32,40,.92);color:#fff;'
|
|
599
|
+
+ 'font-size:13px;line-height:1.5;overflow-wrap:anywhere;pointer-events:none;opacity:0;'
|
|
600
|
+
+ 'box-shadow:0 6px 24px rgba(0,0,0,.28);transition:opacity .22s ease';
|
|
601
|
+
document.body.appendChild(toastNode);
|
|
602
|
+
}
|
|
603
|
+
toastNode.textContent = message;
|
|
604
|
+
toastNode.style.opacity = '1';
|
|
605
|
+
if (toastTimer !== null) clearTimeout(toastTimer);
|
|
606
|
+
toastTimer = setTimeout(function () {
|
|
607
|
+
toastTimer = null;
|
|
608
|
+
if (toastNode !== null) toastNode.style.opacity = '0';
|
|
609
|
+
}, 2600);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/** 拖动搬移是否启用(主机侧 config.dragMove,默认开)。 */
|
|
613
|
+
function dragEnabled() {
|
|
614
|
+
var boot = bootGlobal();
|
|
615
|
+
if (boot !== null && typeof boot === 'object' && boot.drag === false) return false;
|
|
616
|
+
return true;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* 挂上拖动搬移,返回卸载函数。环境没有 document 就整个不启用。
|
|
621
|
+
*
|
|
622
|
+
* 引用计数:客户端可能被重复 apply(热重载 / 换 fiber)。DOM 里同一个监听函数
|
|
623
|
+
* 重复 add 是 no-op,但 remove 一次就真删掉了 —— 不计数的话,旧的 disposer 一跑
|
|
624
|
+
* 就会把还在用的监听整套撤掉(菜单还在、拖动却全没了)。
|
|
625
|
+
*/
|
|
626
|
+
function installDrag() {
|
|
627
|
+
if (typeof document === 'undefined' || typeof document.addEventListener !== 'function') {
|
|
628
|
+
return function () {};
|
|
629
|
+
}
|
|
630
|
+
var capture = true; // 捕获阶段先于官方(React)的处理
|
|
631
|
+
if (dragInstallRefs === 0) {
|
|
632
|
+
document.addEventListener('dragstart', onDragStart, capture);
|
|
633
|
+
document.addEventListener('dragover', onDragOver, capture);
|
|
634
|
+
document.addEventListener('drop', onDrop, capture);
|
|
635
|
+
document.addEventListener('dragend', onDragEnd, capture);
|
|
636
|
+
document.addEventListener('dragleave', onDragLeave, capture);
|
|
637
|
+
}
|
|
638
|
+
dragInstallRefs += 1;
|
|
639
|
+
var disposed = false;
|
|
640
|
+
return function () {
|
|
641
|
+
if (disposed) return;
|
|
642
|
+
disposed = true;
|
|
643
|
+
dragInstallRefs -= 1;
|
|
644
|
+
if (dragInstallRefs > 0) return;
|
|
645
|
+
document.removeEventListener('dragstart', onDragStart, capture);
|
|
646
|
+
document.removeEventListener('dragover', onDragOver, capture);
|
|
647
|
+
document.removeEventListener('drop', onDrop, capture);
|
|
648
|
+
document.removeEventListener('dragend', onDragEnd, capture);
|
|
649
|
+
document.removeEventListener('dragleave', onDragLeave, capture);
|
|
650
|
+
resetDrag();
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
// ── 批量搬移(多选)───────────────────────────────────────────────────────
|
|
655
|
+
//
|
|
656
|
+
// 两条路,靠 dsh-delete-session 显式暴露的能力位决定走哪条:
|
|
657
|
+
// A. 它装了且支持批量动作(window.__DSH_DELETE_SESSION__.supportsBulkMove)→ 它那根
|
|
658
|
+
// 底栏上出现「移动」,它调我们暴露的 window.__DSH_SESSION_RELOCATE__.moveSelection(ids, hooks)。
|
|
659
|
+
// B. 只有我们 → 菜单里多一项「多选」,自己画勾选圈 + 底栏(已选 N / 取消 / 移动)。
|
|
660
|
+
// 判断只认能力位、不猜 DOM:它没给能力位就按 B 走(宁可多一个入口,也不静默丢功能)。
|
|
661
|
+
var CHECK_CLASS = 'dshsm-check';
|
|
662
|
+
var CHECK_ON_CLASS = 'dshsm-check-on';
|
|
663
|
+
var bulkState = { selecting: false, selected: {}, bar: null, countNode: null, moveNode: null, raf: null, moveSelectionFn: null };
|
|
664
|
+
|
|
665
|
+
/** 宿主(dsh-delete-session)是否支持把「移动」放进它那根底栏。 */
|
|
666
|
+
function hostSupportsBulkMove() {
|
|
667
|
+
var host = globalThis.__DSH_DELETE_SESSION__;
|
|
668
|
+
return host !== null && typeof host === 'object' && host.supportsBulkMove === true;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
/** 搬完让侧边栏重新取一次(官方一般会因会话事件自己刷新,这里补一次兜底)。 */
|
|
672
|
+
function refreshSidebar() {
|
|
673
|
+
try { globalThis.dispatchEvent(new Event('resize')); } catch (error) { /* 没有 Event 的环境就算了 */ }
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
/**
|
|
677
|
+
* 纯逻辑:串行把一批会话搬到同一个目标工作区。不碰 DOM,方便单测。
|
|
678
|
+
*
|
|
679
|
+
* 两段式:先逐条 `/move` + `deferRegistry`(每条只做文件事务,进度能实时显示),
|
|
680
|
+
* 最后一次性 `/move-commit` 把整批归属写进注册表 —— N 条会话只付 2 次注册表写,
|
|
681
|
+
* 而不是 2N 次(实测这是单条搬移里最贵的部分)。
|
|
682
|
+
*/
|
|
683
|
+
function runBulkMove(sessionIds, targetWorkspaceId, hooks) {
|
|
684
|
+
var ids = Array.isArray(sessionIds) ? sessionIds.filter(function (id) { return typeof id === 'string' && id.length > 0; }) : [];
|
|
685
|
+
if (ids.length === 0) return Promise.resolve({ canceled: false, moved: 0, failed: 0, skipped: 0, total: 0, results: [] });
|
|
686
|
+
// 主机支不支持两段式,问一次 /status 并缓存:不支持就退回逐条单发(老主机照常能用)。
|
|
687
|
+
return hostSupportsDefer().then(function (twoPhase) {
|
|
688
|
+
return runBulkMoveInner(ids, targetWorkspaceId, hooks, twoPhase);
|
|
689
|
+
});
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
var bulkCapability = null;
|
|
693
|
+
function hostSupportsDefer() {
|
|
694
|
+
if (bulkCapability !== null) return Promise.resolve(bulkCapability);
|
|
695
|
+
return post('/status').then(function (result) {
|
|
696
|
+
bulkCapability = result !== null && typeof result === 'object' && result.bulkDefer === true;
|
|
697
|
+
return bulkCapability;
|
|
698
|
+
}, function () {
|
|
699
|
+
bulkCapability = false;
|
|
700
|
+
return false;
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
function runBulkMoveInner(ids, targetWorkspaceId, hooks, twoPhase) {
|
|
705
|
+
var byId = {};
|
|
706
|
+
var deferred = [];
|
|
707
|
+
var moved = 0;
|
|
708
|
+
var failed = 0;
|
|
709
|
+
var skipped = 0;
|
|
710
|
+
var sampled = false;
|
|
711
|
+
var releaseRetries = 0;
|
|
712
|
+
function record(entry) {
|
|
713
|
+
byId[entry.sessionId] = entry;
|
|
714
|
+
}
|
|
715
|
+
function finish() {
|
|
716
|
+
// results 按传入顺序输出(commit 会把成功项推到后面,这里统一回填)。
|
|
717
|
+
var ordered = [];
|
|
718
|
+
for (var i = 0; i < ids.length; i += 1) if (byId[ids[i]] !== undefined) ordered.push(byId[ids[i]]);
|
|
719
|
+
return {
|
|
720
|
+
canceled: false,
|
|
721
|
+
moved: moved,
|
|
722
|
+
failed: failed,
|
|
723
|
+
skipped: skipped,
|
|
724
|
+
total: ids.length,
|
|
725
|
+
results: ordered,
|
|
726
|
+
verification: sampled ? 'sampled' : 'full',
|
|
727
|
+
releaseAttempts: releaseRetries,
|
|
728
|
+
};
|
|
729
|
+
}
|
|
730
|
+
/** 第二段:一次提交,把 defer 过的归属一次性写进注册表。 */
|
|
731
|
+
function commit(pending) {
|
|
732
|
+
return post('/move-commit', { sessionIds: pending }).then(function (result) {
|
|
733
|
+
var okCommit = result !== null && typeof result === 'object' && result.ok === true;
|
|
734
|
+
var committed = result !== null && typeof result === 'object' && Array.isArray(result.results) ? result.results : [];
|
|
735
|
+
var byId = {};
|
|
736
|
+
for (var i = 0; i < committed.length; i += 1) {
|
|
737
|
+
var entry = committed[i];
|
|
738
|
+
if (entry !== null && typeof entry === 'object' && typeof entry.sessionId === 'string') byId[entry.sessionId] = entry;
|
|
739
|
+
}
|
|
740
|
+
for (var j = 0; j < pending.length; j += 1) {
|
|
741
|
+
var id = pending[j];
|
|
742
|
+
var one = byId[id];
|
|
743
|
+
if (okCommit && one !== undefined && one.status === 'moved') {
|
|
744
|
+
moved += 1;
|
|
745
|
+
record({ sessionId: id, status: 'moved', targetWorkspaceId: targetWorkspaceId });
|
|
746
|
+
} else {
|
|
747
|
+
failed += 1;
|
|
748
|
+
record({
|
|
749
|
+
sessionId: id,
|
|
750
|
+
status: 'failed',
|
|
751
|
+
code: one !== undefined && typeof one.status === 'string' ? one.status : 'commit-failed',
|
|
752
|
+
message: one !== undefined && typeof one.message === 'string'
|
|
753
|
+
? one.message
|
|
754
|
+
: (result !== null && typeof result === 'object' && typeof result.message === 'string' ? result.message : ''),
|
|
755
|
+
});
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
return finish();
|
|
759
|
+
}, function (error) {
|
|
760
|
+
for (var k = 0; k < pending.length; k += 1) {
|
|
761
|
+
failed += 1;
|
|
762
|
+
record({ sessionId: pending[k], status: 'failed', code: 'commit-failed', message: errorText(error) });
|
|
763
|
+
}
|
|
764
|
+
return finish();
|
|
765
|
+
});
|
|
766
|
+
}
|
|
767
|
+
function step(index) {
|
|
768
|
+
if (index >= ids.length) return twoPhase && deferred.length > 0 ? commit(deferred) : Promise.resolve(finish());
|
|
769
|
+
var sessionId = ids[index];
|
|
770
|
+
var done = post('/move', { sessionId: sessionId, targetWorkspaceId: targetWorkspaceId, deferRegistry: twoPhase }).then(function (result) {
|
|
771
|
+
if (result !== null && typeof result === 'object' && result.ok === true) {
|
|
772
|
+
if (result.verification === 'sampled') sampled = true;
|
|
773
|
+
if (Number.isSafeInteger(result.releaseAttempts) && result.releaseAttempts > releaseRetries) releaseRetries = result.releaseAttempts;
|
|
774
|
+
if (twoPhase) deferred.push(sessionId);
|
|
775
|
+
else {
|
|
776
|
+
moved += 1;
|
|
777
|
+
record({ sessionId: sessionId, status: 'moved', targetWorkspaceId: targetWorkspaceId });
|
|
778
|
+
}
|
|
779
|
+
} else if (result !== null && typeof result === 'object' && result.code === 'already-there') {
|
|
780
|
+
skipped += 1;
|
|
781
|
+
record({ sessionId: sessionId, status: 'skipped', code: 'already-there', message: typeof result.message === 'string' ? result.message : '' });
|
|
782
|
+
} else {
|
|
783
|
+
failed += 1;
|
|
784
|
+
record({
|
|
785
|
+
sessionId: sessionId,
|
|
786
|
+
status: 'failed',
|
|
787
|
+
code: result !== null && typeof result === 'object' && typeof result.code === 'string' ? result.code : 'failed',
|
|
788
|
+
message: result !== null && typeof result === 'object' && typeof result.message === 'string' ? result.message : '',
|
|
789
|
+
});
|
|
790
|
+
}
|
|
791
|
+
}, function (error) {
|
|
792
|
+
failed += 1;
|
|
793
|
+
record({ sessionId: sessionId, status: 'failed', code: 'network', message: errorText(error) });
|
|
794
|
+
});
|
|
795
|
+
return done.then(function () {
|
|
796
|
+
if (hooks !== null && hooks !== undefined && typeof hooks.onProgress === 'function') {
|
|
797
|
+
try { hooks.onProgress(index + 1, ids.length); } catch (error) { /* 回调抛错不能打断批量 */ }
|
|
798
|
+
}
|
|
799
|
+
return step(index + 1);
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
return step(0);
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
/** 弹一个居中的目标工作区面板;取消返回 { canceled: true }。 */
|
|
806
|
+
function pickBulkTarget(count) {
|
|
807
|
+
return new Promise(function (resolve) {
|
|
808
|
+
var strings = text();
|
|
809
|
+
var backdrop = document.createElement('div');
|
|
810
|
+
backdrop.className = 'dshsm-picker-backdrop';
|
|
811
|
+
var panel = document.createElement('div');
|
|
812
|
+
panel.className = 'dshsm-picker';
|
|
813
|
+
panel.setAttribute('role', 'dialog');
|
|
814
|
+
var title = document.createElement('div');
|
|
815
|
+
title.className = 'dshsm-picker-title';
|
|
816
|
+
title.textContent = strings.bulkPicking.replace('{n}', String(count));
|
|
817
|
+
panel.appendChild(title);
|
|
818
|
+
var list = document.createElement('div');
|
|
819
|
+
list.className = 'dshsm-picker-list';
|
|
820
|
+
panel.appendChild(list);
|
|
821
|
+
var actions = document.createElement('div');
|
|
822
|
+
actions.className = 'dshsm-picker-actions';
|
|
823
|
+
var cancel = document.createElement('button');
|
|
824
|
+
cancel.type = 'button';
|
|
825
|
+
cancel.className = 'dshsm-picker-btn';
|
|
826
|
+
cancel.textContent = strings.bulkCancel;
|
|
827
|
+
actions.appendChild(cancel);
|
|
828
|
+
panel.appendChild(actions);
|
|
829
|
+
backdrop.appendChild(panel);
|
|
830
|
+
document.body.appendChild(backdrop);
|
|
831
|
+
|
|
832
|
+
var settled = false;
|
|
833
|
+
function done(value) {
|
|
834
|
+
if (settled) return;
|
|
835
|
+
settled = true;
|
|
836
|
+
if (typeof backdrop.remove === 'function') backdrop.remove();
|
|
837
|
+
document.removeEventListener('keydown', onKey, true);
|
|
838
|
+
resolve(value);
|
|
839
|
+
}
|
|
840
|
+
function onKey(event) {
|
|
841
|
+
if (event.key !== 'Escape') return;
|
|
842
|
+
event.stopPropagation();
|
|
843
|
+
done({ canceled: true });
|
|
844
|
+
}
|
|
845
|
+
cancel.addEventListener('click', function () { done({ canceled: true }); });
|
|
846
|
+
backdrop.addEventListener('mousedown', function (event) { if (event.target === backdrop) done({ canceled: true }); });
|
|
847
|
+
document.addEventListener('keydown', onKey, true);
|
|
848
|
+
|
|
849
|
+
var loading = document.createElement('div');
|
|
850
|
+
loading.className = 'dshsm-picker-empty';
|
|
851
|
+
loading.textContent = strings.loading;
|
|
852
|
+
list.appendChild(loading);
|
|
853
|
+
|
|
854
|
+
post('/describe', {}).then(function (result) {
|
|
855
|
+
if (settled) return;
|
|
856
|
+
if (typeof list.removeChild === 'function') list.removeChild(loading);
|
|
857
|
+
var spaces = result !== null && typeof result === 'object' && Array.isArray(result.workspaces) ? result.workspaces : [];
|
|
858
|
+
if (spaces.length === 0) {
|
|
859
|
+
var empty = document.createElement('div');
|
|
860
|
+
empty.className = 'dshsm-picker-empty';
|
|
861
|
+
empty.textContent = strings.noTargets;
|
|
862
|
+
list.appendChild(empty);
|
|
863
|
+
return;
|
|
864
|
+
}
|
|
865
|
+
for (var i = 0; i < spaces.length; i += 1) {
|
|
866
|
+
(function (workspace) {
|
|
867
|
+
var item = document.createElement('button');
|
|
868
|
+
item.type = 'button';
|
|
869
|
+
item.className = 'dshsm-picker-item';
|
|
870
|
+
var name = document.createElement('span');
|
|
871
|
+
name.className = 'dshsm-picker-name';
|
|
872
|
+
name.textContent = workspace.title;
|
|
873
|
+
item.appendChild(name);
|
|
874
|
+
if (typeof workspace.path === 'string' && workspace.path.length > 0) {
|
|
875
|
+
var path = document.createElement('span');
|
|
876
|
+
path.className = 'dshsm-picker-path';
|
|
877
|
+
path.textContent = workspace.path;
|
|
878
|
+
item.appendChild(path);
|
|
879
|
+
}
|
|
880
|
+
item.addEventListener('click', function () { done({ workspace: workspace }); });
|
|
881
|
+
list.appendChild(item);
|
|
882
|
+
})(spaces[i]);
|
|
883
|
+
}
|
|
884
|
+
}, function (error) {
|
|
885
|
+
if (settled) return;
|
|
886
|
+
if (typeof list.removeChild === 'function') list.removeChild(loading);
|
|
887
|
+
var failedNode = document.createElement('div');
|
|
888
|
+
failedNode.className = 'dshsm-picker-empty dshsm-bad';
|
|
889
|
+
failedNode.textContent = strings.failed + ':' + errorText(error);
|
|
890
|
+
list.appendChild(failedNode);
|
|
891
|
+
});
|
|
892
|
+
});
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
/**
|
|
896
|
+
* 对外契约(dsh-delete-session 会用):选目标 → 串行搬 → 汇总。
|
|
897
|
+
* 返回 { canceled?, moved, failed, skipped, total, results[] };
|
|
898
|
+
* hooks.onProgress(done, total) 的 done = 已处理条数(成功 + 失败 + 跳过)。
|
|
899
|
+
*/
|
|
900
|
+
function moveSelection(sessionIds, hooks) {
|
|
901
|
+
var ids = Array.isArray(sessionIds) ? sessionIds.filter(function (id) { return typeof id === 'string' && id.length > 0; }) : [];
|
|
902
|
+
if (ids.length === 0) {
|
|
903
|
+
return Promise.resolve({ canceled: false, moved: 0, failed: 0, skipped: 0, total: 0, results: [] });
|
|
904
|
+
}
|
|
905
|
+
return pickBulkTarget(ids.length).then(function (picked) {
|
|
906
|
+
if (picked === null || picked === undefined || picked.workspace === undefined) {
|
|
907
|
+
return { canceled: true, moved: 0, failed: 0, skipped: 0, total: ids.length, results: [] };
|
|
908
|
+
}
|
|
909
|
+
return runBulkMove(ids, picked.workspace.id, hooks);
|
|
910
|
+
});
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
/** 行 → 会话 id(只认会话行;工作区行不是会话)。 */
|
|
914
|
+
function sessionIdFromRow(row) {
|
|
915
|
+
var node = row;
|
|
916
|
+
for (var depth = 0; depth < ROW_WALK_LIMIT && node !== null && node !== undefined; depth += 1) {
|
|
917
|
+
if (typeof node.getAttribute === 'function') {
|
|
918
|
+
var key = node.getAttribute(ROW_KEY_ATTR);
|
|
919
|
+
if (typeof key === 'string' && key.indexOf(SESSION_PREFIX) === 0 && key.length > SESSION_PREFIX.length) {
|
|
920
|
+
return key.slice(SESSION_PREFIX.length);
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
node = node.parentElement;
|
|
924
|
+
}
|
|
925
|
+
return null;
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
function bulkDecorate() {
|
|
929
|
+
bulkState.raf = null;
|
|
930
|
+
if (typeof document === 'undefined' || typeof document.querySelectorAll !== 'function') return;
|
|
931
|
+
var rows = Array.prototype.slice.call(document.querySelectorAll('[role="treeitem"]'));
|
|
932
|
+
for (var i = 0; i < rows.length; i += 1) {
|
|
933
|
+
var row = rows[i];
|
|
934
|
+
var id = sessionIdFromRow(row);
|
|
935
|
+
if (id === null) continue;
|
|
936
|
+
var check = typeof row.querySelector === 'function' ? row.querySelector(':scope > .' + CHECK_CLASS) : null;
|
|
937
|
+
if (bulkState.selecting !== true) {
|
|
938
|
+
if (check !== null && typeof check.remove === 'function') check.remove();
|
|
939
|
+
continue;
|
|
940
|
+
}
|
|
941
|
+
if (check === null) {
|
|
942
|
+
check = document.createElement('span');
|
|
943
|
+
check.className = CHECK_CLASS;
|
|
944
|
+
check.setAttribute('aria-hidden', 'true');
|
|
945
|
+
row.insertBefore(check, row.firstChild);
|
|
946
|
+
}
|
|
947
|
+
if (bulkState.selected[id] === true) check.classList.add(CHECK_ON_CLASS);
|
|
948
|
+
else check.classList.remove(CHECK_ON_CLASS);
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
function scheduleBulkDecorate() {
|
|
953
|
+
if (bulkState.raf !== null) return;
|
|
954
|
+
bulkState.raf = typeof globalThis.requestAnimationFrame === 'function'
|
|
955
|
+
? globalThis.requestAnimationFrame(bulkDecorate)
|
|
956
|
+
: setTimeout(bulkDecorate, 16);
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
function bulkSelectedIds() {
|
|
960
|
+
var ids = [];
|
|
961
|
+
for (var key in bulkState.selected) if (bulkState.selected[key] === true) ids.push(key);
|
|
962
|
+
return ids;
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
function bulkUpdateBar() {
|
|
966
|
+
var count = bulkSelectedIds().length;
|
|
967
|
+
if (bulkState.countNode !== null) bulkState.countNode.textContent = text().bulkSelected.replace('{n}', String(count));
|
|
968
|
+
if (bulkState.moveNode !== null) bulkState.moveNode.disabled = count === 0;
|
|
969
|
+
return count;
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
function buildBulkBar() {
|
|
973
|
+
var strings = text();
|
|
974
|
+
var bar = document.createElement('div');
|
|
975
|
+
bar.className = 'dshsm-bar';
|
|
976
|
+
bar.setAttribute('role', 'toolbar');
|
|
977
|
+
var count = document.createElement('span');
|
|
978
|
+
count.className = 'dshsm-bar-count';
|
|
979
|
+
var cancel = document.createElement('button');
|
|
980
|
+
cancel.type = 'button';
|
|
981
|
+
cancel.className = 'dshsm-bar-btn';
|
|
982
|
+
cancel.textContent = strings.bulkCancel;
|
|
983
|
+
cancel.addEventListener('click', function (event) { if (event) event.stopPropagation(); exitBulk(); });
|
|
984
|
+
var move = document.createElement('button');
|
|
985
|
+
move.type = 'button';
|
|
986
|
+
move.className = 'dshsm-bar-btn dshsm-bar-primary';
|
|
987
|
+
move.textContent = strings.bulkMove;
|
|
988
|
+
move.disabled = true;
|
|
989
|
+
move.addEventListener('click', function (event) {
|
|
990
|
+
if (event) event.stopPropagation();
|
|
991
|
+
bulkMoveSelected();
|
|
992
|
+
});
|
|
993
|
+
bar.appendChild(count);
|
|
994
|
+
bar.appendChild(cancel);
|
|
995
|
+
bar.appendChild(move);
|
|
996
|
+
document.body.appendChild(bar);
|
|
997
|
+
bulkState.bar = bar;
|
|
998
|
+
bulkState.countNode = count;
|
|
999
|
+
bulkState.moveNode = move;
|
|
1000
|
+
bulkUpdateBar();
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
function bulkMoveSelected() {
|
|
1004
|
+
var strings = text();
|
|
1005
|
+
var ids = bulkSelectedIds();
|
|
1006
|
+
if (ids.length === 0) return;
|
|
1007
|
+
if (bulkState.moveNode !== null) bulkState.moveNode.disabled = true;
|
|
1008
|
+
var run = typeof bulkState.moveSelectionFn === 'function' ? bulkState.moveSelectionFn : moveSelection;
|
|
1009
|
+
run(ids, {
|
|
1010
|
+
onProgress: function (done, total) {
|
|
1011
|
+
if (bulkState.countNode !== null) {
|
|
1012
|
+
bulkState.countNode.textContent = strings.bulkProgress.replace('{d}', String(done)).replace('{t}', String(total));
|
|
1013
|
+
}
|
|
1014
|
+
},
|
|
1015
|
+
}).then(function (result) {
|
|
1016
|
+
if (result.canceled === true) { bulkUpdateBar(); return; }
|
|
1017
|
+
var summary = result.failed === 0
|
|
1018
|
+
? strings.bulkDone.replace('{ok}', String(result.moved))
|
|
1019
|
+
: strings.bulkPartial.replace('{ok}', String(result.moved)).replace('{fail}', String(result.failed));
|
|
1020
|
+
if (result.skipped > 0) summary += strings.bulkSkipped.replace('{n}', String(result.skipped));
|
|
1021
|
+
summary += receiptNotes(result);
|
|
1022
|
+
toast(summary);
|
|
1023
|
+
exitBulk();
|
|
1024
|
+
refreshSidebar();
|
|
1025
|
+
}, function (error) {
|
|
1026
|
+
toast(strings.bulkFailed + errorText(error));
|
|
1027
|
+
bulkUpdateBar();
|
|
1028
|
+
});
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
function onBulkRowClick(event) {
|
|
1032
|
+
if (bulkState.selecting !== true) return;
|
|
1033
|
+
var target = event !== null && event !== undefined ? event.target : null;
|
|
1034
|
+
var row = target !== null && target !== undefined && typeof target.closest === 'function' ? target.closest('[role="treeitem"]') : null;
|
|
1035
|
+
if (row === null || row === undefined) return;
|
|
1036
|
+
var id = sessionIdFromRow(row);
|
|
1037
|
+
if (id === null) return;
|
|
1038
|
+
if (typeof event.preventDefault === 'function') event.preventDefault();
|
|
1039
|
+
if (typeof event.stopPropagation === 'function') event.stopPropagation();
|
|
1040
|
+
if (bulkState.selected[id] === true) delete bulkState.selected[id];
|
|
1041
|
+
else bulkState.selected[id] = true;
|
|
1042
|
+
scheduleBulkDecorate();
|
|
1043
|
+
bulkUpdateBar();
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
function onBulkKeydown(event) {
|
|
1047
|
+
if (bulkState.selecting !== true) return;
|
|
1048
|
+
if (event.key !== 'Escape') return;
|
|
1049
|
+
event.stopPropagation();
|
|
1050
|
+
exitBulk();
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
function enterBulk() {
|
|
1054
|
+
if (typeof document === 'undefined' || document.body === undefined || document.body === null) return;
|
|
1055
|
+
if (bulkState.selecting === true) return;
|
|
1056
|
+
bulkState.selecting = true;
|
|
1057
|
+
bulkState.selected = {};
|
|
1058
|
+
buildBulkBar();
|
|
1059
|
+
document.addEventListener('click', onBulkRowClick, true);
|
|
1060
|
+
document.addEventListener('keydown', onBulkKeydown, true);
|
|
1061
|
+
scheduleBulkDecorate();
|
|
1062
|
+
// 关掉菜单(和官方一样:在别处点一下)
|
|
1063
|
+
try { document.dispatchEvent(new MouseEvent('mousedown', { bubbles: true })); } catch (error) { /* ignore */ }
|
|
1064
|
+
try { document.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true })); } catch (error) { /* ignore */ }
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
function exitBulk() {
|
|
1068
|
+
if (bulkState.selecting !== true) return;
|
|
1069
|
+
bulkState.selecting = false;
|
|
1070
|
+
bulkState.selected = {};
|
|
1071
|
+
if (bulkState.raf !== null) {
|
|
1072
|
+
try {
|
|
1073
|
+
if (typeof globalThis.cancelAnimationFrame === 'function') globalThis.cancelAnimationFrame(bulkState.raf);
|
|
1074
|
+
else clearTimeout(bulkState.raf);
|
|
1075
|
+
} catch (error) { /* ignore */ }
|
|
1076
|
+
bulkState.raf = null;
|
|
1077
|
+
}
|
|
1078
|
+
if (typeof document !== 'undefined') {
|
|
1079
|
+
document.removeEventListener('click', onBulkRowClick, true);
|
|
1080
|
+
document.removeEventListener('keydown', onBulkKeydown, true);
|
|
1081
|
+
}
|
|
1082
|
+
if (bulkState.bar !== null) {
|
|
1083
|
+
if (typeof bulkState.bar.remove === 'function') bulkState.bar.remove();
|
|
1084
|
+
bulkState.bar = null;
|
|
1085
|
+
bulkState.countNode = null;
|
|
1086
|
+
bulkState.moveNode = null;
|
|
1087
|
+
}
|
|
1088
|
+
bulkDecorate();
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
/** 菜单里的「多选」入口:宿主支持批量动作时让位给它那根底栏。 */
|
|
1092
|
+
function BulkEntry(props) {
|
|
1093
|
+
var useMenuOpenState = props !== null && props !== undefined ? props.useMenuOpenState : null;
|
|
1094
|
+
var menuState = null;
|
|
1095
|
+
if (typeof useMenuOpenState === 'function') {
|
|
1096
|
+
try { menuState = useMenuOpenState(); } catch (error) { menuState = null; }
|
|
1097
|
+
}
|
|
1098
|
+
if (hostSupportsBulkMove()) return null;
|
|
1099
|
+
return button('bulk', text().bulk, {
|
|
1100
|
+
onClick: function () {
|
|
1101
|
+
if (menuState !== null && Array.isArray(menuState) && typeof menuState[1] === 'function') {
|
|
1102
|
+
try { menuState[1](false); } catch (error) { /* ignore */ }
|
|
1103
|
+
}
|
|
1104
|
+
enterBulk();
|
|
1105
|
+
},
|
|
1106
|
+
});
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
/** 需要 slots 服务。 */
|
|
1110
|
+
var inject = ['slots'];
|
|
1111
|
+
|
|
1112
|
+
function apply(ctx) {
|
|
1113
|
+
ensureStyles();
|
|
1114
|
+
// 对外契约:dsh-delete-session 会检测这个全局,把「移动」放进它那根底栏。
|
|
1115
|
+
var boot = bootGlobal();
|
|
1116
|
+
var version = boot !== null && typeof boot === 'object' && typeof boot.version === 'string' ? boot.version : '0.0.0';
|
|
1117
|
+
// 注意:这个全局**同时**是主机注入的引导信息(prefix / version / drag)和客户端对外契约,
|
|
1118
|
+
// 所以只能合并、不能整个覆盖 —— 覆盖掉 prefix 会让桥接请求打到一个不存在的地址。
|
|
1119
|
+
var contract = globalThis.__DSH_SESSION_RELOCATE__;
|
|
1120
|
+
if (contract === null || typeof contract !== 'object') contract = {};
|
|
1121
|
+
contract.version = version;
|
|
1122
|
+
contract.supportsBulkMove = true;
|
|
1123
|
+
contract.moveSelection = moveSelection;
|
|
1124
|
+
globalThis.__DSH_SESSION_RELOCATE__ = contract;
|
|
1125
|
+
try {
|
|
1126
|
+
document.dispatchEvent(new CustomEvent('dsh-session-relocate:ready', { detail: { version: version } }));
|
|
1127
|
+
} catch (error) { /* 没有 CustomEvent 的环境就算了 */ }
|
|
1128
|
+
// 拖动搬移:与菜单那条路并存(拖不动就点菜单)。
|
|
1129
|
+
if (!dragEnabled()) {
|
|
1130
|
+
// 主机侧关掉了:什么都不挂。
|
|
1131
|
+
} else if (typeof ctx.effect === 'function') {
|
|
1132
|
+
ctx.effect(installDrag, 'dsh-session-relocate:drag');
|
|
1133
|
+
} else {
|
|
1134
|
+
installDrag();
|
|
1135
|
+
}
|
|
1136
|
+
return ctx.slots.inject('sidebar.workspaces.session.menu.item', function () {
|
|
1137
|
+
var disposers = [];
|
|
1138
|
+
// 「多选」入口:dsh-delete-session 支持批量动作时该组件渲染 null(入口交给它那根底栏)。
|
|
1139
|
+
disposers.push(ctx.slots.register(
|
|
1140
|
+
{ name: 'sidebar.workspaces.session.menu.item', id: 'session-relocate-bulk', order: 351 },
|
|
1141
|
+
BulkEntry,
|
|
1142
|
+
));
|
|
1143
|
+
disposers.push(ctx.slots.register(
|
|
1144
|
+
{ name: 'sidebar.workspaces.session.menu.item', id: 'session-relocate', order: 350 },
|
|
1145
|
+
Entry,
|
|
1146
|
+
));
|
|
1147
|
+
return function () {
|
|
1148
|
+
for (var i = 0; i < disposers.length; i += 1) {
|
|
1149
|
+
if (typeof disposers[i] === 'function') {
|
|
1150
|
+
try { disposers[i](); } catch (error) { /* ignore */ }
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
};
|
|
1154
|
+
});
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
exports.apply = apply;
|
|
1158
|
+
exports.inject = inject;
|
|
1159
|
+
exports.__entry = Entry;
|
|
1160
|
+
exports.__bulkEntry = BulkEntry;
|
|
1161
|
+
// 给测试用的内部入口(浏览器里没人用)。
|
|
1162
|
+
exports.__bulk = {
|
|
1163
|
+
moveSelection: moveSelection,
|
|
1164
|
+
runBulkMove: runBulkMove,
|
|
1165
|
+
pickBulkTarget: pickBulkTarget,
|
|
1166
|
+
hostSupportsBulkMove: hostSupportsBulkMove,
|
|
1167
|
+
enterBulk: enterBulk,
|
|
1168
|
+
exitBulk: exitBulk,
|
|
1169
|
+
bulkDecorate: bulkDecorate,
|
|
1170
|
+
bulkMoveSelected: bulkMoveSelected,
|
|
1171
|
+
state: bulkState,
|
|
1172
|
+
sessionIdFromRow: sessionIdFromRow,
|
|
1173
|
+
};
|
|
1174
|
+
exports.__drag = {
|
|
1175
|
+
installDrag: installDrag,
|
|
1176
|
+
onDragStart: onDragStart,
|
|
1177
|
+
onDragOver: onDragOver,
|
|
1178
|
+
onDrop: onDrop,
|
|
1179
|
+
onDragEnd: onDragEnd,
|
|
1180
|
+
onDragLeave: onDragLeave,
|
|
1181
|
+
state: dragState,
|
|
1182
|
+
reset: resetDrag,
|
|
1183
|
+
nearestRow: nearestRow,
|
|
1184
|
+
workspaceGroupOf: workspaceGroupOf,
|
|
1185
|
+
workspaceIdOf: workspaceIdOf,
|
|
1186
|
+
};
|
|
1187
|
+
return module.exports;
|
|
1188
|
+
},
|
|
1189
|
+
});
|