dsh-side-window 0.0.0-stage → 0.5.8
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 +62 -2
- package/client.js +1476 -0
- package/cordis.patch.yml +3 -0
- package/docs/DETAIL.md +1 -0
- package/docs/preview.png +0 -0
- package/docs/settings.png +0 -0
- package/icon.svg +19 -0
- package/index.js +1080 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +37 -5
- package/shell/focus.ps1 +139 -0
- package/shell/main.js +923 -0
- package/shell/marker.js +38 -0
- package/shell/package.json +7 -0
package/client.js
ADDED
|
@@ -0,0 +1,1476 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-side-window 客户端半侧:设置页(`settings.section` 插槽,id 为 dsh-side-window)。
|
|
3
|
+
*
|
|
4
|
+
* 做四件事:读写 /dsh-side-window/config、显示 /dsh-side-window/runtime 状态并可触发下载、
|
|
5
|
+
* 调 /dsh-side-window/restart;另外主窗口每 700ms 上报当前对话、侧栏窗口每 900ms 跟随。
|
|
6
|
+
*
|
|
7
|
+
* 坑(已踩过):组件必须用 React.createElement 建元素,不能用 react/jsx-runtime 的 jsx
|
|
8
|
+
* 代替 createElement。jsx(type, null, child) 会让 React 内部读 config.key 时抛
|
|
9
|
+
* “Cannot read properties of null (reading 'key')”,slot 运行时捕获后把整块设置页渲染成空白
|
|
10
|
+
* (导航项还在,因为注册本身是成功的)。
|
|
11
|
+
*/
|
|
12
|
+
window.__ModuleLoader__.load({
|
|
13
|
+
id: 'dsh-side-window',
|
|
14
|
+
factory(require) {
|
|
15
|
+
const React = require('react')
|
|
16
|
+
const h = React.createElement
|
|
17
|
+
const { useCallback, useEffect, useRef, useState } = React
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 默认值只在宿主(`index.js` 的 `/config` 路由)定义一份。
|
|
21
|
+
*
|
|
22
|
+
* 客户端不再声明任何默认值:宿主每次都返回完整配置,页面在拿到之前显示「正在读取」。
|
|
23
|
+
* 以前这里抄了一份,已经和宿主漂过一次(`enabled` 一边 false、一边「有文件就 true」)。
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
/** 全部取主题变量,浅色/深色跟随 DSH 自身。 */
|
|
27
|
+
const C = {
|
|
28
|
+
label: 'var(--dsw-alias-label-primary, #0f1115)',
|
|
29
|
+
secondary: 'var(--dsw-alias-label-secondary, #5a6070)',
|
|
30
|
+
tertiary: 'var(--dsw-alias-label-tertiary, #8a90a0)',
|
|
31
|
+
border: 'var(--dsw-alias-border-l2, rgba(128,128,128,.28))',
|
|
32
|
+
fill: 'var(--dsw-alias-bg-layer-1, rgba(128,128,128,.06))',
|
|
33
|
+
hover: 'var(--dsw-alias-interactive-bg-hover, rgba(128,128,128,.12))',
|
|
34
|
+
brand: 'var(--dsw-alias-brand-primary, #4d6bfe)',
|
|
35
|
+
danger: 'var(--dsw-alias-state-error-primary, #d92d20)',
|
|
36
|
+
success: 'var(--dsw-alias-state-success-primary, #12b76a)',
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const S = {
|
|
40
|
+
root: {
|
|
41
|
+
display: 'flex',
|
|
42
|
+
flexDirection: 'column',
|
|
43
|
+
gap: '16px',
|
|
44
|
+
padding: '8px 0',
|
|
45
|
+
fontSize: '13px',
|
|
46
|
+
lineHeight: '22px',
|
|
47
|
+
color: C.label,
|
|
48
|
+
},
|
|
49
|
+
intro: { color: C.secondary },
|
|
50
|
+
card: {
|
|
51
|
+
display: 'flex',
|
|
52
|
+
flexDirection: 'column',
|
|
53
|
+
border: '0.5px solid ' + C.border,
|
|
54
|
+
borderRadius: 'var(--dsw-radius-xl, 12px)',
|
|
55
|
+
overflow: 'hidden',
|
|
56
|
+
},
|
|
57
|
+
row: {
|
|
58
|
+
display: 'flex',
|
|
59
|
+
alignItems: 'center',
|
|
60
|
+
justifyContent: 'space-between',
|
|
61
|
+
gap: '16px',
|
|
62
|
+
padding: '12px 16px',
|
|
63
|
+
},
|
|
64
|
+
rowStacked: { display: 'flex', flexDirection: 'column', gap: '8px', padding: '12px 16px' },
|
|
65
|
+
rowText: { display: 'flex', flexDirection: 'column', minWidth: 0, gap: '2px' },
|
|
66
|
+
hint: { color: C.tertiary, fontSize: '12px', lineHeight: '18px' },
|
|
67
|
+
value: { color: C.secondary, fontVariantNumeric: 'tabular-nums', flex: 'none' },
|
|
68
|
+
footer: { display: 'flex', alignItems: 'center', gap: '8px', flexWrap: 'wrap' },
|
|
69
|
+
button: {
|
|
70
|
+
appearance: 'none',
|
|
71
|
+
border: '0.5px solid ' + C.border,
|
|
72
|
+
background: C.fill,
|
|
73
|
+
color: C.label,
|
|
74
|
+
fontSize: '13px',
|
|
75
|
+
lineHeight: '20px',
|
|
76
|
+
padding: '5px 14px',
|
|
77
|
+
borderRadius: 'var(--dsw-radius-md, 8px)',
|
|
78
|
+
cursor: 'pointer',
|
|
79
|
+
fontFamily: 'inherit',
|
|
80
|
+
},
|
|
81
|
+
buttonPrimary: {
|
|
82
|
+
appearance: 'none',
|
|
83
|
+
border: '0.5px solid transparent',
|
|
84
|
+
background: C.brand,
|
|
85
|
+
color: 'var(--dsw-alias-label-primary-foreground, #fff)',
|
|
86
|
+
fontSize: '13px',
|
|
87
|
+
lineHeight: '20px',
|
|
88
|
+
padding: '5px 14px',
|
|
89
|
+
borderRadius: 'var(--dsw-radius-md, 8px)',
|
|
90
|
+
cursor: 'pointer',
|
|
91
|
+
fontFamily: 'inherit',
|
|
92
|
+
},
|
|
93
|
+
buttonDisabled: { opacity: 0.5, cursor: 'default' },
|
|
94
|
+
status: { fontSize: '12px', lineHeight: '18px' },
|
|
95
|
+
track: {
|
|
96
|
+
position: 'relative',
|
|
97
|
+
display: 'flex',
|
|
98
|
+
alignItems: 'center',
|
|
99
|
+
gap: '10px',
|
|
100
|
+
},
|
|
101
|
+
range: { flex: '1 1 auto', minWidth: '120px', accentColor: C.brand, cursor: 'pointer' },
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** 一行开关:右侧是一个 role="switch" 的按钮,避免自己写 checkbox 样式。 */
|
|
105
|
+
function Toggle(props) {
|
|
106
|
+
const on = props.checked === true
|
|
107
|
+
return h(
|
|
108
|
+
'div',
|
|
109
|
+
{ style: S.row },
|
|
110
|
+
h(
|
|
111
|
+
'div',
|
|
112
|
+
{ style: S.rowText },
|
|
113
|
+
h('span', null, props.label),
|
|
114
|
+
props.hint === undefined ? null : h('span', { style: S.hint }, props.hint),
|
|
115
|
+
),
|
|
116
|
+
h(
|
|
117
|
+
'button',
|
|
118
|
+
{
|
|
119
|
+
type: 'button',
|
|
120
|
+
role: 'switch',
|
|
121
|
+
'aria-checked': on ? 'true' : 'false',
|
|
122
|
+
'aria-label': props.label,
|
|
123
|
+
disabled: props.disabled === true,
|
|
124
|
+
onClick: () => {
|
|
125
|
+
if (props.disabled !== true) props.onChange(!on)
|
|
126
|
+
},
|
|
127
|
+
style: {
|
|
128
|
+
position: 'relative',
|
|
129
|
+
flex: 'none',
|
|
130
|
+
width: '34px',
|
|
131
|
+
height: '20px',
|
|
132
|
+
padding: 0,
|
|
133
|
+
border: 'none',
|
|
134
|
+
borderRadius: '10px',
|
|
135
|
+
cursor: props.disabled === true ? 'default' : 'pointer',
|
|
136
|
+
background: on ? C.brand : C.border,
|
|
137
|
+
transition: 'background .15s ease',
|
|
138
|
+
opacity: props.disabled === true ? 0.5 : 1,
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
h('span', {
|
|
142
|
+
style: {
|
|
143
|
+
position: 'absolute',
|
|
144
|
+
top: '2px',
|
|
145
|
+
left: on ? '16px' : '2px',
|
|
146
|
+
width: '16px',
|
|
147
|
+
height: '16px',
|
|
148
|
+
borderRadius: '50%',
|
|
149
|
+
background: 'var(--dsw-alias-switch-thumb, #fff)',
|
|
150
|
+
transition: 'left .15s ease',
|
|
151
|
+
},
|
|
152
|
+
}),
|
|
153
|
+
),
|
|
154
|
+
)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** 一行滑块:目前只有窗口宽度用。 */
|
|
158
|
+
function Slider(props) {
|
|
159
|
+
return h(
|
|
160
|
+
'div',
|
|
161
|
+
{ style: S.rowStacked },
|
|
162
|
+
h(
|
|
163
|
+
'div',
|
|
164
|
+
{ style: { display: 'flex', alignItems: 'baseline', justifyContent: 'space-between', gap: '16px' } },
|
|
165
|
+
h('span', null, props.label),
|
|
166
|
+
h('span', { style: S.value }, props.display),
|
|
167
|
+
),
|
|
168
|
+
h(
|
|
169
|
+
'div',
|
|
170
|
+
{ style: S.track },
|
|
171
|
+
h('input', {
|
|
172
|
+
type: 'range',
|
|
173
|
+
min: props.min,
|
|
174
|
+
max: props.max,
|
|
175
|
+
step: props.step,
|
|
176
|
+
value: props.value,
|
|
177
|
+
'aria-label': props.label,
|
|
178
|
+
disabled: props.disabled === true,
|
|
179
|
+
onChange: (event) => {
|
|
180
|
+
props.onChange(Number(event.target.value))
|
|
181
|
+
},
|
|
182
|
+
style: S.range,
|
|
183
|
+
}),
|
|
184
|
+
),
|
|
185
|
+
props.hint === undefined ? null : h('span', { style: S.hint }, props.hint),
|
|
186
|
+
)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* 读一份 JSON 响应,失败时抛带宿主原文的错。
|
|
191
|
+
*
|
|
192
|
+
* 必须先读 body 再判状态码:宿主拒绝非法输入时会给 400 + 一句能照做的中文
|
|
193
|
+
* (「至少要有一个修饰键…」),只报 HTTP 400 等于把那句话吞了。
|
|
194
|
+
* @returns 解析后的响应体。
|
|
195
|
+
*/
|
|
196
|
+
async function jsonOrThrow(response) {
|
|
197
|
+
const body = await response.json().catch(() => null)
|
|
198
|
+
if (!response.ok) throw new Error((body && body.message) || 'HTTP ' + response.status)
|
|
199
|
+
if (!body || body.ok !== true) throw new Error((body && body.message) || '宿主返回异常')
|
|
200
|
+
return body
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** 读一次宿主配置。 */
|
|
204
|
+
async function readConfig() {
|
|
205
|
+
return (await jsonOrThrow(await fetch('/dsh-side-window/config', { cache: 'no-store' }))).config
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** 把整份配置写回宿主(宿主按 merge 处理)。 */
|
|
209
|
+
async function writeConfig(config) {
|
|
210
|
+
const response = await fetch('/dsh-side-window/config', {
|
|
211
|
+
method: 'POST',
|
|
212
|
+
headers: { 'content-type': 'application/json' },
|
|
213
|
+
body: JSON.stringify(config),
|
|
214
|
+
})
|
|
215
|
+
return (await jsonOrThrow(response)).config
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* 录制期间让外壳临时卸掉全局热键,结束后装回去。
|
|
220
|
+
*
|
|
221
|
+
* 不卸的话按「当前生效的组合」会被操作系统截走:页面收不到 keydown(录不到),
|
|
222
|
+
* 还会把侧栏窗口叫出来抢走焦点(录制态看起来假死)。失败就算了 —— 外壳有 2 分钟兜底。
|
|
223
|
+
*/
|
|
224
|
+
function setHotkeyPaused(paused) {
|
|
225
|
+
fetch('/dsh-side-window/hotkey-pause', {
|
|
226
|
+
method: 'POST',
|
|
227
|
+
headers: { 'content-type': 'application/json' },
|
|
228
|
+
body: JSON.stringify({ paused }),
|
|
229
|
+
}).catch(() => {
|
|
230
|
+
/* 宿主还是旧版 / 网络抖动:最多这次录制按旧组合没反应 */
|
|
231
|
+
})
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** DSH 把「当前看的是哪个对话」持久化在这个键里(dsh-client-ui-workspace 的 selection store)。 */
|
|
235
|
+
const SESSION_KEY = 'dsh.sessions.current'
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* 本窗口是不是侧栏那一侧。
|
|
239
|
+
*
|
|
240
|
+
* 主窗口和侧栏窗口加载同一个页面、同一个源,URL 里的 token 启动后还会被抹掉,
|
|
241
|
+
* 所以由 dsh-shell 的 preload 注入标记(`window.__dshSidebar`);调试时也认 URL 参数。
|
|
242
|
+
* @returns 是否侧栏窗口。
|
|
243
|
+
*/
|
|
244
|
+
function isSidebarWindow() {
|
|
245
|
+
if (typeof window !== 'undefined' && window.__dshSidebar !== undefined && window.__dshSidebar.marker === true) {
|
|
246
|
+
return true
|
|
247
|
+
}
|
|
248
|
+
try {
|
|
249
|
+
return new URLSearchParams(window.location.search).has('dshSidebar')
|
|
250
|
+
} catch {
|
|
251
|
+
return false
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* 读本窗口当前的对话 id。
|
|
257
|
+
* @returns session id;读不到时返回空串。
|
|
258
|
+
*/
|
|
259
|
+
function readCurrentSession() {
|
|
260
|
+
try {
|
|
261
|
+
const raw = window.localStorage.getItem(SESSION_KEY)
|
|
262
|
+
if (raw === null) return ''
|
|
263
|
+
const parsed = JSON.parse(raw)
|
|
264
|
+
return parsed !== null && typeof parsed === 'object' && typeof parsed.sessionId === 'string' ? parsed.sessionId : ''
|
|
265
|
+
} catch {
|
|
266
|
+
return ''
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* 主窗口侧:把「当前对话」上报给宿主。
|
|
272
|
+
* 每 700ms 看一眼 localStorage,只在变化时发一次请求。
|
|
273
|
+
* @returns 清理函数。
|
|
274
|
+
*/
|
|
275
|
+
function startSessionPublisher() {
|
|
276
|
+
if (isSidebarWindow()) return () => {}
|
|
277
|
+
let sent = ''
|
|
278
|
+
const timer = setInterval(() => {
|
|
279
|
+
const sessionId = readCurrentSession()
|
|
280
|
+
if (sessionId === '' || sessionId === sent) return
|
|
281
|
+
sent = sessionId
|
|
282
|
+
fetch('/dsh-side-window/active-session', {
|
|
283
|
+
method: 'POST',
|
|
284
|
+
headers: { 'content-type': 'application/json' },
|
|
285
|
+
body: JSON.stringify({ sessionId }),
|
|
286
|
+
})
|
|
287
|
+
.then((response) => {
|
|
288
|
+
if (!response.ok) sent = ''
|
|
289
|
+
})
|
|
290
|
+
.catch(() => {
|
|
291
|
+
sent = ''
|
|
292
|
+
})
|
|
293
|
+
}, 700)
|
|
294
|
+
return () => clearInterval(timer)
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* 侧栏侧:跟随主窗口切换对话。
|
|
299
|
+
*
|
|
300
|
+
* 只在宿主上报的 id **发生变化**时动作,所以在侧栏里手动切对话不会被拉回去
|
|
301
|
+
* (侧栏切换不改变宿主的 id,因为只有主窗口上报)。
|
|
302
|
+
* @param ctx - 插件上下文。
|
|
303
|
+
* @returns 清理函数。
|
|
304
|
+
*/
|
|
305
|
+
function startSessionFollower(ctx) {
|
|
306
|
+
if (!isSidebarWindow()) return () => {}
|
|
307
|
+
let lastHost = null
|
|
308
|
+
let warned = false
|
|
309
|
+
const timer = setInterval(async () => {
|
|
310
|
+
let body
|
|
311
|
+
try {
|
|
312
|
+
const response = await fetch('/dsh-side-window/active-session', { cache: 'no-store' })
|
|
313
|
+
if (!response.ok) return
|
|
314
|
+
body = await response.json()
|
|
315
|
+
} catch {
|
|
316
|
+
return
|
|
317
|
+
}
|
|
318
|
+
if (!body || body.ok !== true || body.follow !== true) return
|
|
319
|
+
const target = typeof body.sessionId === 'string' ? body.sessionId : ''
|
|
320
|
+
if (target === '' || target === lastHost) return
|
|
321
|
+
lastHost = target
|
|
322
|
+
if (target === readCurrentSession()) return
|
|
323
|
+
const workspace = ctx.get('uiWorkspace')
|
|
324
|
+
if (!workspace || typeof workspace.openSession !== 'function') {
|
|
325
|
+
if (!warned) {
|
|
326
|
+
warned = true
|
|
327
|
+
console.warn('[dsh-side-window] uiWorkspace 不可用,跟随切换对话失效')
|
|
328
|
+
}
|
|
329
|
+
return
|
|
330
|
+
}
|
|
331
|
+
console.log('[dsh-side-window] follow -> ' + target)
|
|
332
|
+
workspace.openSession(target)
|
|
333
|
+
// openSession 是同步发起、异步生效的,过一会儿回头确认一下,失败要看得到
|
|
334
|
+
window.setTimeout(() => {
|
|
335
|
+
console.log('[dsh-side-window] follow ' + (readCurrentSession() === target ? 'ok' : 'failed') + ' -> ' + target)
|
|
336
|
+
}, 1500)
|
|
337
|
+
}, 900)
|
|
338
|
+
return () => clearInterval(timer)
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* 文件资源地址:`dsh-resource://file/session/<sid>/<path>`。
|
|
343
|
+
*
|
|
344
|
+
* 编码规则跟着 DSH 的 `file-address` 走:路径先按 `/` 分段再逐段编码(`/` 必须保持字面),
|
|
345
|
+
* 段里的 `:` 保持字面(Windows 盘符 `E:`),反斜杠一律折成 `/`。
|
|
346
|
+
* 自己拼而不是引内部包:这段格式是稳定的公开字面量,少一个依赖少一处版本耦合。
|
|
347
|
+
* @param sessionId - 文件所属对话。
|
|
348
|
+
* @param path - 工作区相对路径或绝对路径。
|
|
349
|
+
* @returns 资源地址。
|
|
350
|
+
*/
|
|
351
|
+
function fileResourceAddress(sessionId, path) {
|
|
352
|
+
const normalized = String(path).replace(/\\/g, '/').replace(/^(?:\.\/)+/, '')
|
|
353
|
+
const encoded = normalized
|
|
354
|
+
.split('/')
|
|
355
|
+
.map((segment) => encodeURIComponent(segment).replace(/%3A/gi, ':'))
|
|
356
|
+
.join('/')
|
|
357
|
+
return 'dsh-resource://file/session/' + encodeURIComponent(String(sessionId)) + '/' + encoded
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* 等一个客户端服务就位再动手。
|
|
362
|
+
*
|
|
363
|
+
* 为什么要等:`dsh-client-hmr` 热替换时,我们这个模块可能先于 contextMenus / sidebarRight 加载,
|
|
364
|
+
* 那一瞬间 `ctx.get()` 是 undefined。等 5 秒就放弃并告警,不让一个可选功能拖垮整个插件。
|
|
365
|
+
* @param ctx - 插件上下文。
|
|
366
|
+
* @param name - 服务名(`contextMenus` / `sidebarRight`)。
|
|
367
|
+
* @param onReady - 服务就位时调用,返回值当作 disposer。
|
|
368
|
+
* @returns 清理函数。
|
|
369
|
+
*/
|
|
370
|
+
function whenService(ctx, name, onReady) {
|
|
371
|
+
let attempts = 0
|
|
372
|
+
let timer = null
|
|
373
|
+
let disposed = false
|
|
374
|
+
let offReady = null
|
|
375
|
+
const attempt = () => {
|
|
376
|
+
if (disposed) return
|
|
377
|
+
const service = ctx.get(name)
|
|
378
|
+
if (service !== undefined && service !== null) {
|
|
379
|
+
// 注册失败只告警:这个是可选功能,不能把设置页一起拖死
|
|
380
|
+
try {
|
|
381
|
+
offReady = onReady(service)
|
|
382
|
+
} catch (error) {
|
|
383
|
+
console.warn('[dsh-side-window] register into ' + name + ' failed: ' + (error && error.message ? error.message : error))
|
|
384
|
+
}
|
|
385
|
+
return
|
|
386
|
+
}
|
|
387
|
+
attempts += 1
|
|
388
|
+
if (attempts >= 20) {
|
|
389
|
+
// 日志一律 ASCII:页面 console 会经外壳转发进 shell.log,中文在那里会变乱码
|
|
390
|
+
console.warn('[dsh-side-window] service ' + name + ' unavailable; open-in-side-window stays off')
|
|
391
|
+
return
|
|
392
|
+
}
|
|
393
|
+
timer = window.setTimeout(attempt, 250)
|
|
394
|
+
}
|
|
395
|
+
attempt()
|
|
396
|
+
return () => {
|
|
397
|
+
disposed = true
|
|
398
|
+
if (timer !== null) window.clearTimeout(timer)
|
|
399
|
+
if (typeof offReady === 'function') offReady()
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* 把一条打开请求交给宿主,由外壳转给侧栏窗口。
|
|
405
|
+
*
|
|
406
|
+
* 刻意不报错、不兜底:侧栏没在跑的时候,点这一项就等于什么也没发生。
|
|
407
|
+
* 菜单不 await 它,失败只在 Console 留一行。
|
|
408
|
+
* @param payload - `{ address, sessionId, line? }`(文件)或 `{ url, sessionId }`(链接)。
|
|
409
|
+
*/
|
|
410
|
+
function requestSideWindowOpen(payload) {
|
|
411
|
+
fetch('/dsh-side-window/open-resource', {
|
|
412
|
+
method: 'POST',
|
|
413
|
+
headers: { 'content-type': 'application/json' },
|
|
414
|
+
body: JSON.stringify(payload),
|
|
415
|
+
})
|
|
416
|
+
.then((response) => {
|
|
417
|
+
// 404(宿主还是旧版本)也算「没发生」:不报错、不打假日志
|
|
418
|
+
if (response.ok) console.log('[dsh-side-window] open in side window -> ' + (payload.address || payload.url))
|
|
419
|
+
})
|
|
420
|
+
.catch(() => {
|
|
421
|
+
/* 宿主是旧版本、或者刚好不在:静默,不打扰 */
|
|
422
|
+
})
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/** 两个右键类别共用的一句话:侧栏没在跑时点它等于没点。 */
|
|
426
|
+
const OPEN_IN_WINDOW = '在侧边窗口打开'
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* 主窗口侧:往右键菜单里加「在侧边窗口打开」。
|
|
430
|
+
*
|
|
431
|
+
* 两类 target 各注册一条,都只需要一次:
|
|
432
|
+
* - `file`:@文件 chip、工具行(读/改/office)、交付物卡片、文档预览标题;
|
|
433
|
+
* - `url`:消息里的链接、web 工具行的链接。
|
|
434
|
+
*
|
|
435
|
+
* 这里**不判断内容**:DSH 早就分好档了(Markdown 里的本地文件链接给的是 `file` 目标,
|
|
436
|
+
* 只有它认定是网址的才给 `url`)。我们只负责把 kind 对应成自己的搬运格式:
|
|
437
|
+
* 文件 → 资源地址,链接 → href 原样。猜内容会猜错,而且猜错是静默的。
|
|
438
|
+
* @param ctx - 插件上下文。
|
|
439
|
+
* @returns 清理函数。
|
|
440
|
+
*/
|
|
441
|
+
function registerOpenMenus(ctx) {
|
|
442
|
+
if (isSidebarWindow()) return () => {}
|
|
443
|
+
return whenService(ctx, 'contextMenus', (menus) => {
|
|
444
|
+
if (typeof menus.register !== 'function') return undefined
|
|
445
|
+
const disposers = [
|
|
446
|
+
menus.register({
|
|
447
|
+
id: 'dsh-side-window.open-file-in-window',
|
|
448
|
+
target: 'file',
|
|
449
|
+
// 排在「打开方式(100)/下载(200)」前面:这是这几项里唯一跟本插件相关的
|
|
450
|
+
order: 50,
|
|
451
|
+
resolve: (target) => [{
|
|
452
|
+
id: 'openFileInSideWindow',
|
|
453
|
+
label: OPEN_IN_WINDOW,
|
|
454
|
+
run: () => requestSideWindowOpen({
|
|
455
|
+
address: fileResourceAddress(target.sessionId, target.path),
|
|
456
|
+
sessionId: target.sessionId,
|
|
457
|
+
line: target.line,
|
|
458
|
+
}),
|
|
459
|
+
}],
|
|
460
|
+
}),
|
|
461
|
+
menus.register({
|
|
462
|
+
id: 'dsh-side-window.open-url-in-window',
|
|
463
|
+
target: 'url',
|
|
464
|
+
// 排在 DSH 自己的「在侧边栏打开」旁边(那条也是 100)
|
|
465
|
+
order: 50,
|
|
466
|
+
resolve: (target) => [{
|
|
467
|
+
id: 'openUrlInSideWindow',
|
|
468
|
+
label: OPEN_IN_WINDOW,
|
|
469
|
+
run: () => requestSideWindowOpen({ url: target.url, sessionId: target.sessionId }),
|
|
470
|
+
}],
|
|
471
|
+
}),
|
|
472
|
+
]
|
|
473
|
+
return () => {
|
|
474
|
+
for (const off of disposers) if (typeof off === 'function') off()
|
|
475
|
+
}
|
|
476
|
+
})
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* 等侧栏窗口切到某个对话(切完 openResource 才不会落到别的 Session 的停靠面上)。
|
|
481
|
+
* @param sessionId - 目标对话。
|
|
482
|
+
* @param timeoutMs - 最多等多久。
|
|
483
|
+
* @returns 是否切过去了。
|
|
484
|
+
*/
|
|
485
|
+
async function waitForSession(sessionId, timeoutMs) {
|
|
486
|
+
const deadline = Date.now() + timeoutMs
|
|
487
|
+
while (Date.now() < deadline) {
|
|
488
|
+
if (readCurrentSession() === sessionId) return true
|
|
489
|
+
await new Promise((resolve) => window.setTimeout(resolve, 100))
|
|
490
|
+
}
|
|
491
|
+
return false
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* 侧栏侧:收到请求 →(必要时切对话)→ 在自己的右栏打开它。
|
|
496
|
+
*
|
|
497
|
+
* 两种东西走同一个入口:
|
|
498
|
+
* - 文件 → `openResource(dsh-resource://file/…)`,侧栏宽 460 < 768 时右栏自动全屏;
|
|
499
|
+
* - 链接 → `openTabIn(sessionId, 'browser', { params: { url } })`,就是 DSH 自己
|
|
500
|
+
* 那条「在侧边栏打开」用的同一个 tab 类型,只是开在这个窗口里。
|
|
501
|
+
* @param ctx - 插件上下文。
|
|
502
|
+
* @param payload - 外壳转来的 `{ address, sessionId, line? }` 或 `{ url, sessionId }`。
|
|
503
|
+
*/
|
|
504
|
+
async function showResourceInSidebar(ctx, payload) {
|
|
505
|
+
if (payload === null || typeof payload !== 'object') return
|
|
506
|
+
const address = typeof payload.address === 'string' ? payload.address : ''
|
|
507
|
+
const url = typeof payload.url === 'string' ? payload.url : ''
|
|
508
|
+
if (address === '' && url === '') return
|
|
509
|
+
const sessionId = typeof payload.sessionId === 'string' ? payload.sessionId : ''
|
|
510
|
+
if (sessionId !== '' && readCurrentSession() !== sessionId) {
|
|
511
|
+
const workspace = ctx.get('uiWorkspace')
|
|
512
|
+
if (workspace && typeof workspace.openSession === 'function') {
|
|
513
|
+
workspace.openSession(sessionId)
|
|
514
|
+
await waitForSession(sessionId, 2500)
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
const right = ctx.get('sidebarRight')
|
|
518
|
+
if (!right) {
|
|
519
|
+
console.warn('[dsh-side-window] sidebarRight unavailable; cannot open it here')
|
|
520
|
+
return
|
|
521
|
+
}
|
|
522
|
+
if (url !== '') {
|
|
523
|
+
if (sessionId === '') return
|
|
524
|
+
const tabs = ctx.get('sidebarRightTabs')
|
|
525
|
+
if (tabs && typeof tabs.get === 'function' && tabs.get('browser') === undefined) {
|
|
526
|
+
console.warn('[dsh-side-window] browser tab type unavailable; cannot open the link')
|
|
527
|
+
return
|
|
528
|
+
}
|
|
529
|
+
right.openTabIn(sessionId, 'browser', { params: { url } })
|
|
530
|
+
console.log('[dsh-side-window] opened link in side window -> ' + url)
|
|
531
|
+
return
|
|
532
|
+
}
|
|
533
|
+
if (typeof right.openResource !== 'function') {
|
|
534
|
+
console.warn('[dsh-side-window] sidebarRight.openResource unavailable')
|
|
535
|
+
return
|
|
536
|
+
}
|
|
537
|
+
const line = Number.isInteger(payload.line) ? payload.line : undefined
|
|
538
|
+
right.openResource(address, line === undefined ? undefined : { params: { line } })
|
|
539
|
+
console.log('[dsh-side-window] opened in side window -> ' + address)
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* 侧栏侧:订阅外壳转来的打开请求,并在页面就绪时取一次「还没送到的」。
|
|
544
|
+
* @param ctx - 插件上下文。
|
|
545
|
+
* @returns 清理函数。
|
|
546
|
+
*/
|
|
547
|
+
function startSidebarOpener(ctx) {
|
|
548
|
+
if (!isSidebarWindow()) return () => {}
|
|
549
|
+
const bridge = window.__dshSidebar
|
|
550
|
+
if (!bridge || typeof bridge.onOpenResource !== 'function') {
|
|
551
|
+
// preload 改动 HMR 带不过去:这里必须重启侧栏窗口(设置页「保存并重启」)
|
|
552
|
+
console.warn('[dsh-side-window] preload has no onOpenResource; restart the shell window to enable it')
|
|
553
|
+
return () => {}
|
|
554
|
+
}
|
|
555
|
+
let queue = Promise.resolve()
|
|
556
|
+
const handle = (payload) => {
|
|
557
|
+
queue = queue
|
|
558
|
+
.then(() => showResourceInSidebar(ctx, payload))
|
|
559
|
+
.catch((error) => {
|
|
560
|
+
console.warn('[dsh-side-window] open in side window failed: ' + (error && error.message ? error.message : error))
|
|
561
|
+
})
|
|
562
|
+
}
|
|
563
|
+
const off = bridge.onOpenResource(handle)
|
|
564
|
+
if (typeof bridge.takePendingOpen === 'function') {
|
|
565
|
+
bridge.takePendingOpen()
|
|
566
|
+
.then((payload) => { if (payload !== null && payload !== undefined) handle(payload) })
|
|
567
|
+
.catch(() => { /* 没有就没取到,不影响监听 */ })
|
|
568
|
+
}
|
|
569
|
+
return () => { if (typeof off === 'function') off() }
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/**
|
|
573
|
+
* 把宿主返回的运行时状态说成人话。
|
|
574
|
+
* @param runtime - `/dsh-side-window/runtime` 里的 runtime 字段;还没读到时为 null。
|
|
575
|
+
* @returns 一行状态文本。
|
|
576
|
+
*/
|
|
577
|
+
function runtimeText(runtime) {
|
|
578
|
+
if (runtime === null || runtime === undefined) return '检查中…'
|
|
579
|
+
if (runtime.ready === true) {
|
|
580
|
+
if (runtime.source === 'env') return '已就绪(来自 DSH_SIDE_WINDOW_ELECTRON)'
|
|
581
|
+
if (runtime.source === 'shared') {
|
|
582
|
+
return '已就绪(复用已有的 Electron' + (runtime.version === null ? '' : ' ' + runtime.version) + ')'
|
|
583
|
+
}
|
|
584
|
+
return '已就绪(' + runtime.expectedVersion + ')'
|
|
585
|
+
}
|
|
586
|
+
if (runtime.state === 'downloading') return '正在下载 Electron(约 138 MiB)… 装完会自动拉起侧栏窗口'
|
|
587
|
+
if (runtime.state === 'error') return '不可用:' + (runtime.message === '' ? '未知错误' : runtime.message)
|
|
588
|
+
return '未安装:首次使用需要下载一次 Electron(约 138 MiB)'
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/**
|
|
592
|
+
* 录制热键:DOM 键盘事件 → Electron accelerator 的按键名。
|
|
593
|
+
*
|
|
594
|
+
* 用 `event.code`(物理键位)而不是 `event.key`:按住 Shift 时 `event.key` 会变成 `L`/`!`
|
|
595
|
+
* 这种"已带修饰效果的字符",拼出来的 accelerator 反而错。宿主 `normalizeHotkey` 认的
|
|
596
|
+
* 就是下面这套名字,两边必须一致(改一边就要改另一边)。
|
|
597
|
+
*/
|
|
598
|
+
const KEY_NAME_BY_CODE = {
|
|
599
|
+
Space: 'Space',
|
|
600
|
+
Enter: 'Enter',
|
|
601
|
+
Tab: 'Tab',
|
|
602
|
+
Backspace: 'Backspace',
|
|
603
|
+
Delete: 'Delete',
|
|
604
|
+
Insert: 'Insert',
|
|
605
|
+
Home: 'Home',
|
|
606
|
+
End: 'End',
|
|
607
|
+
PageUp: 'PageUp',
|
|
608
|
+
PageDown: 'PageDown',
|
|
609
|
+
Escape: 'Escape',
|
|
610
|
+
ArrowUp: 'Up',
|
|
611
|
+
ArrowDown: 'Down',
|
|
612
|
+
ArrowLeft: 'Left',
|
|
613
|
+
ArrowRight: 'Right',
|
|
614
|
+
Minus: '-',
|
|
615
|
+
Equal: '=',
|
|
616
|
+
Comma: ',',
|
|
617
|
+
Period: '.',
|
|
618
|
+
Slash: '/',
|
|
619
|
+
Semicolon: ';',
|
|
620
|
+
Quote: "'",
|
|
621
|
+
BracketLeft: '[',
|
|
622
|
+
BracketRight: ']',
|
|
623
|
+
Backslash: '\\',
|
|
624
|
+
Backquote: '`',
|
|
625
|
+
NumpadDecimal: 'numdec',
|
|
626
|
+
NumpadAdd: 'numadd',
|
|
627
|
+
NumpadSubtract: 'numsub',
|
|
628
|
+
NumpadMultiply: 'nummult',
|
|
629
|
+
NumpadDivide: 'numdiv',
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* @param event - 键盘事件。
|
|
634
|
+
* @returns Electron accelerator 里的按键名;不认识的键返回空串。
|
|
635
|
+
*/
|
|
636
|
+
function keyNameFromEvent(event) {
|
|
637
|
+
const code = typeof event.code === 'string' ? event.code : ''
|
|
638
|
+
const named = KEY_NAME_BY_CODE[code]
|
|
639
|
+
if (named !== undefined) return named
|
|
640
|
+
const letter = /^Key([A-Z])$/.exec(code)
|
|
641
|
+
if (letter !== null) return letter[1]
|
|
642
|
+
const digit = /^Digit([0-9])$/.exec(code)
|
|
643
|
+
if (digit !== null) return digit[1]
|
|
644
|
+
const numpad = /^Numpad([0-9])$/.exec(code)
|
|
645
|
+
if (numpad !== null) return 'num' + numpad[1]
|
|
646
|
+
if (/^F([1-9]|1[0-9]|2[0-4])$/.test(code)) return code
|
|
647
|
+
// code 缺失(合成事件 / 虚拟键盘)时退回 key:只认单字符字母数字
|
|
648
|
+
const key = typeof event.key === 'string' ? event.key : ''
|
|
649
|
+
if (/^[a-zA-Z]$/.test(key)) return key.toUpperCase()
|
|
650
|
+
if (/^[0-9]$/.test(key)) return key
|
|
651
|
+
return ''
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
/**
|
|
655
|
+
* 一行「呼出快捷键」:点「录制」后在页面上抓组合键。
|
|
656
|
+
*
|
|
657
|
+
* 只在按下合法组合时写进表单(真正生效要等「保存」,外壳 500ms 内热切换)。
|
|
658
|
+
* 不提供 Win 键:Win 的组合基本被系统占着,最吓人的 Win+L 会直接锁屏。
|
|
659
|
+
*/
|
|
660
|
+
function HotkeyField(props) {
|
|
661
|
+
const [recording, setRecording] = useState(false)
|
|
662
|
+
const [held, setHeld] = useState('')
|
|
663
|
+
const [localError, setLocalError] = useState('')
|
|
664
|
+
// 回调放 ref:Section 被 1.5s 轮询推着重渲染,依赖里带 onChange 会让监听器跟着拆装
|
|
665
|
+
const onChangeRef = useRef(props.onChange)
|
|
666
|
+
onChangeRef.current = props.onChange
|
|
667
|
+
|
|
668
|
+
const stop = useCallback(() => {
|
|
669
|
+
setRecording(false)
|
|
670
|
+
setHeld('')
|
|
671
|
+
setLocalError('')
|
|
672
|
+
}, [])
|
|
673
|
+
|
|
674
|
+
useEffect(() => {
|
|
675
|
+
if (!recording) return undefined
|
|
676
|
+
// 开始录制:让外壳把全局热键卸掉(否则按当前组合会被系统截走、还会叫出侧栏窗口)
|
|
677
|
+
setHotkeyPaused(true)
|
|
678
|
+
const onKeyDown = (event) => {
|
|
679
|
+
// 录制期间吃掉所有按键:不然 Ctrl+F 之类会顺手触发页面/浏览器自己的行为
|
|
680
|
+
event.preventDefault()
|
|
681
|
+
event.stopPropagation()
|
|
682
|
+
if (event.key === 'Escape') {
|
|
683
|
+
// Esc 一律当取消:用户想放弃时手里常常还按着 Ctrl,不能录成 Ctrl+Escape
|
|
684
|
+
stop()
|
|
685
|
+
return
|
|
686
|
+
}
|
|
687
|
+
if (event.key === 'Control' || event.key === 'Shift' || event.key === 'Alt' || event.key === 'Meta') {
|
|
688
|
+
const parts = []
|
|
689
|
+
if (event.ctrlKey || event.key === 'Control') parts.push('Ctrl')
|
|
690
|
+
if (event.altKey || event.key === 'Alt') parts.push('Alt')
|
|
691
|
+
if (event.shiftKey || event.key === 'Shift') parts.push('Shift')
|
|
692
|
+
setHeld(parts.length === 0 ? '…' : parts.join(' + ') + ' + …')
|
|
693
|
+
return
|
|
694
|
+
}
|
|
695
|
+
if (event.metaKey) {
|
|
696
|
+
setLocalError('不支持 Win 键组合(会被系统占用),请用 Ctrl / Alt / Shift')
|
|
697
|
+
return
|
|
698
|
+
}
|
|
699
|
+
const modifiers = []
|
|
700
|
+
if (event.ctrlKey) modifiers.push('Ctrl')
|
|
701
|
+
if (event.altKey) modifiers.push('Alt')
|
|
702
|
+
if (event.shiftKey) modifiers.push('Shift')
|
|
703
|
+
if (modifiers.length === 0) {
|
|
704
|
+
setLocalError('要带上 Ctrl / Alt / Shift 之一')
|
|
705
|
+
return
|
|
706
|
+
}
|
|
707
|
+
const key = keyNameFromEvent(event)
|
|
708
|
+
if (key === '') {
|
|
709
|
+
setLocalError('这个键不能用作快捷键')
|
|
710
|
+
return
|
|
711
|
+
}
|
|
712
|
+
onChangeRef.current(modifiers.join('+') + '+' + key)
|
|
713
|
+
stop()
|
|
714
|
+
}
|
|
715
|
+
// 焦点跑掉就结束录制:别停在「监听器还挂着但再也收不到键」的假死态
|
|
716
|
+
const onBlur = () => stop()
|
|
717
|
+
window.addEventListener('keydown', onKeyDown, true)
|
|
718
|
+
window.addEventListener('blur', onBlur)
|
|
719
|
+
return () => {
|
|
720
|
+
window.removeEventListener('keydown', onKeyDown, true)
|
|
721
|
+
window.removeEventListener('blur', onBlur)
|
|
722
|
+
// 结束录制(正常录完、取消、失焦、组件卸载):把热键装回去
|
|
723
|
+
setHotkeyPaused(false)
|
|
724
|
+
}
|
|
725
|
+
}, [recording, stop])
|
|
726
|
+
|
|
727
|
+
const parts = String(props.value === undefined || props.value === null ? '' : props.value)
|
|
728
|
+
.split('+')
|
|
729
|
+
.filter((part) => part !== '')
|
|
730
|
+
const state = props.state
|
|
731
|
+
// 只认「当前表单里这个值」的注册结果:改过还没保存时,上一次失败的信息已经过期了
|
|
732
|
+
const stateMatches = state !== null && state !== undefined && state.requested === props.value
|
|
733
|
+
const stateText =
|
|
734
|
+
localError !== ''
|
|
735
|
+
? localError
|
|
736
|
+
: stateMatches && state.ok === false
|
|
737
|
+
? '注册失败:' + (state.message === '' ? '该组合可能被其它程序占用' : state.message)
|
|
738
|
+
: ''
|
|
739
|
+
|
|
740
|
+
return h(
|
|
741
|
+
'div',
|
|
742
|
+
{ style: S.row },
|
|
743
|
+
h(
|
|
744
|
+
'div',
|
|
745
|
+
{ style: S.rowText },
|
|
746
|
+
h('span', null, '收拉快捷键'),
|
|
747
|
+
h(
|
|
748
|
+
'span',
|
|
749
|
+
{ style: stateText === '' ? S.hint : { ...S.hint, color: C.danger } },
|
|
750
|
+
recording
|
|
751
|
+
? '请按下组合键(不含 Win 键);Esc 取消'
|
|
752
|
+
: stateText !== ''
|
|
753
|
+
? stateText
|
|
754
|
+
: '点击拉出/收起侧栏窗口。点击「录制」可进行自定义(组合键),保存后生效。',
|
|
755
|
+
),
|
|
756
|
+
),
|
|
757
|
+
h(
|
|
758
|
+
'div',
|
|
759
|
+
{
|
|
760
|
+
style: {
|
|
761
|
+
display: 'flex',
|
|
762
|
+
alignItems: 'center',
|
|
763
|
+
justifyContent: 'flex-end',
|
|
764
|
+
flexWrap: 'wrap',
|
|
765
|
+
gap: '8px',
|
|
766
|
+
flex: 'none',
|
|
767
|
+
minHeight: '20px',
|
|
768
|
+
},
|
|
769
|
+
},
|
|
770
|
+
recording
|
|
771
|
+
? h('span', { style: { ...S.hint, color: C.brand } }, held === '' ? '…' : held)
|
|
772
|
+
: h(
|
|
773
|
+
'div',
|
|
774
|
+
{ style: { display: 'flex', alignItems: 'center', gap: '4px' } },
|
|
775
|
+
parts.length === 0
|
|
776
|
+
? h('span', { style: S.hint }, '未设置')
|
|
777
|
+
: parts.map((part, index) =>
|
|
778
|
+
h(
|
|
779
|
+
'kbd',
|
|
780
|
+
{
|
|
781
|
+
key: index + '-' + part,
|
|
782
|
+
style: {
|
|
783
|
+
minWidth: '18px',
|
|
784
|
+
padding: '1px 6px',
|
|
785
|
+
textAlign: 'center',
|
|
786
|
+
border: '0.5px solid ' + C.border,
|
|
787
|
+
borderRadius: 'var(--dsw-radius-md, 8px)',
|
|
788
|
+
background: C.fill,
|
|
789
|
+
color: C.label,
|
|
790
|
+
fontSize: '12px',
|
|
791
|
+
lineHeight: '18px',
|
|
792
|
+
fontFamily: 'inherit',
|
|
793
|
+
},
|
|
794
|
+
},
|
|
795
|
+
part,
|
|
796
|
+
),
|
|
797
|
+
),
|
|
798
|
+
),
|
|
799
|
+
h(
|
|
800
|
+
'button',
|
|
801
|
+
{
|
|
802
|
+
type: 'button',
|
|
803
|
+
disabled: props.disabled === true,
|
|
804
|
+
onClick: () => {
|
|
805
|
+
if (recording) {
|
|
806
|
+
stop()
|
|
807
|
+
return
|
|
808
|
+
}
|
|
809
|
+
setLocalError('')
|
|
810
|
+
setHeld('')
|
|
811
|
+
setRecording(true)
|
|
812
|
+
},
|
|
813
|
+
style: { ...S.button, ...(props.disabled === true ? S.buttonDisabled : {}) },
|
|
814
|
+
},
|
|
815
|
+
recording ? '取消' : '录制',
|
|
816
|
+
),
|
|
817
|
+
),
|
|
818
|
+
)
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
/**
|
|
822
|
+
* 把 `#rgb` / `#rrggbb` / `rgb()` / `rgba()` 解析成 `[r, g, b]`。
|
|
823
|
+
* @returns 认不出时返回 null。
|
|
824
|
+
*/
|
|
825
|
+
function parseCssColor(text) {
|
|
826
|
+
const hex = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(text)
|
|
827
|
+
if (hex !== null) {
|
|
828
|
+
const h = hex[1].length === 3 ? hex[1].split('').map((c) => c + c).join('') : hex[1]
|
|
829
|
+
return [parseInt(h.slice(0, 2), 16), parseInt(h.slice(2, 4), 16), parseInt(h.slice(4, 6), 16)]
|
|
830
|
+
}
|
|
831
|
+
const match = /^rgba?\(([^)]+)\)$/.exec(text)
|
|
832
|
+
if (match === null) return null
|
|
833
|
+
const parts = match[1].split(',').map((v) => Number(v.trim()))
|
|
834
|
+
return parts.length >= 3 && parts.slice(0, 3).every((v) => Number.isFinite(v)) ? parts.slice(0, 3) : null
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
/**
|
|
838
|
+
* 侧栏窗口的外观效果:画布透明度 + 强制隐藏第三方背景层。
|
|
839
|
+
*
|
|
840
|
+
* **画布透明度**:把 `--dsw-alias-bg-base` 换成带 alpha 的 rgba,别的 token 不动。两处关键
|
|
841
|
+
* (都是照开源实现定的):
|
|
842
|
+
* - 必须写在 **body** 上,不能写 html:DSH 的 theme-presenter 把主题 token 以 body 内联样式
|
|
843
|
+
* 下发,底色样式表也按 `body[data-ds-dark-theme]` 取值 —— 写在 html 上会被 body 的声明压住
|
|
844
|
+
* (实测 documentElement 上读到的是空串,窗口就"没透")。
|
|
845
|
+
* - 先去掉自己的覆盖再读当前主题色,否则第二次会把自己上一轮写的 rgba 当源色,越叠越黑。
|
|
846
|
+
* 画布是 `AppFrame.module.css` 的 `.frame { background: var(--dsw-alias-bg-base) }`;
|
|
847
|
+
* 左侧栏另用 `--dsw-specific-sidebar-fill`,这里故意不碰 —— 只透画布,面板保持实心。
|
|
848
|
+
*
|
|
849
|
+
* **第三方背景层跟随不透明度**(默认关):外观/壁纸插件会在 `body` 上挂一层铺满窗口、负 z-index
|
|
850
|
+
* 的背景层(`#dsw-wallpaper-engine-layer`、`.we-scrim`…),它们盖在桌面和我们之间。这里**按结构
|
|
851
|
+
* 识别、不认插件名**:body 直接子元素 + `position:fixed` + 铺满视口 + `z-index < 0` + 不是 `#root`。
|
|
852
|
+
* 命中后把它们的 `opacity` 调成**和画布同一个值**(`!important` 内联,压过插件自己的声明)——
|
|
853
|
+
* 不是删掉:壁纸还在,只是半透明,桌面能透出来。关掉开关立即恢复成 1。
|
|
854
|
+
* 这是"用户明知代价才开"的能力:壁纸会跟着变淡;对方升级换了结构可能不再命中。
|
|
855
|
+
* @returns 清理函数。
|
|
856
|
+
*/
|
|
857
|
+
function startCanvasEffects() {
|
|
858
|
+
if (!isSidebarWindow()) return () => {}
|
|
859
|
+
const token = '--dsw-alias-bg-base'
|
|
860
|
+
let alpha = 1
|
|
861
|
+
let forceLayers = false
|
|
862
|
+
let alive = true
|
|
863
|
+
/** 被我们调过透明度的第三方背景层(恢复时逐个摘掉 opacity)。 */
|
|
864
|
+
const matchedLayers = new Set()
|
|
865
|
+
const restoreLayers = () => {
|
|
866
|
+
for (const element of matchedLayers) element.style.removeProperty('opacity')
|
|
867
|
+
matchedLayers.clear()
|
|
868
|
+
}
|
|
869
|
+
const applyLayerOpacity = () => {
|
|
870
|
+
if (!forceLayers || document.body === null) return
|
|
871
|
+
const value = alpha < 1 ? String(alpha) : ''
|
|
872
|
+
const viewportWidth = window.innerWidth
|
|
873
|
+
const viewportHeight = window.innerHeight
|
|
874
|
+
for (const element of document.body.children) {
|
|
875
|
+
if (element.id === 'root') continue
|
|
876
|
+
if (element.tagName === 'SCRIPT' || element.tagName === 'STYLE' || element.tagName === 'LINK') continue
|
|
877
|
+
const style = getComputedStyle(element)
|
|
878
|
+
if (style.position !== 'fixed') continue
|
|
879
|
+
const depth = Number(style.zIndex)
|
|
880
|
+
if (!Number.isFinite(depth) || depth >= 0) continue
|
|
881
|
+
const rect = element.getBoundingClientRect()
|
|
882
|
+
if (rect.width < viewportWidth * 0.9 || rect.height < viewportHeight * 0.9) continue
|
|
883
|
+
if (value === '') element.style.removeProperty('opacity')
|
|
884
|
+
else element.style.setProperty('opacity', value, 'important')
|
|
885
|
+
if (!matchedLayers.has(element)) {
|
|
886
|
+
matchedLayers.add(element)
|
|
887
|
+
const name = typeof element.className === 'string' && element.className !== '' ? '.' + element.className.trim().split(/\s+/)[0] : ''
|
|
888
|
+
console.log('[dsh-side-window] third-party bg layer ' + element.tagName.toLowerCase() + (element.id ? '#' + element.id : '') + name + ' z=' + depth)
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
const paint = () => {
|
|
893
|
+
const host = document.body
|
|
894
|
+
if (host === null) return
|
|
895
|
+
host.style.removeProperty(token)
|
|
896
|
+
if (alpha < 1) {
|
|
897
|
+
const rgb = parseCssColor(getComputedStyle(host).getPropertyValue(token).trim())
|
|
898
|
+
if (rgb !== null) host.style.setProperty(token, 'rgba(' + rgb.join(',') + ',' + alpha + ')')
|
|
899
|
+
}
|
|
900
|
+
applyLayerOpacity()
|
|
901
|
+
}
|
|
902
|
+
const load = () => {
|
|
903
|
+
fetch('/dsh-side-window/config', { cache: 'no-store' })
|
|
904
|
+
.then((response) => (response.ok ? response.json() : null))
|
|
905
|
+
.then((body) => {
|
|
906
|
+
if (!alive || body === null || body.ok !== true) return
|
|
907
|
+
const config = body.config
|
|
908
|
+
const next = Math.max(0.4, Math.min(1, Number(config.bgAlpha) || 1))
|
|
909
|
+
const nextForce = config.forceTransparentLayers === true
|
|
910
|
+
const alphaChanged = next !== alpha
|
|
911
|
+
const forceChanged = nextForce !== forceLayers
|
|
912
|
+
alpha = next
|
|
913
|
+
forceLayers = nextForce
|
|
914
|
+
if (alphaChanged) console.log('[dsh-side-window] canvas alpha -> ' + alpha)
|
|
915
|
+
if (forceChanged && !forceLayers) restoreLayers()
|
|
916
|
+
if (alphaChanged || forceChanged) paint()
|
|
917
|
+
})
|
|
918
|
+
.catch(() => {})
|
|
919
|
+
}
|
|
920
|
+
// 插件晚挂载/重建:body 子节点一变就补扫一次(不监听属性,避免自己写 opacity 触发循环)
|
|
921
|
+
const mutation = new MutationObserver(() => applyLayerOpacity())
|
|
922
|
+
if (document.body !== null) mutation.observe(document.body, { childList: true })
|
|
923
|
+
// 深浅色切换时主题色变了:用新值重算一遍
|
|
924
|
+
const themeWatch = new MutationObserver(paint)
|
|
925
|
+
themeWatch.observe(document.body, { attributes: true, attributeFilter: ['data-ds-dark-theme'] })
|
|
926
|
+
load()
|
|
927
|
+
// 宿主写的另一份文件:1 秒看一眼 —— 设置页在主窗口,改完只能靠这条轮询送过来
|
|
928
|
+
const timer = setInterval(load, 1000)
|
|
929
|
+
return () => {
|
|
930
|
+
alive = false
|
|
931
|
+
clearInterval(timer)
|
|
932
|
+
mutation.disconnect()
|
|
933
|
+
themeWatch.disconnect()
|
|
934
|
+
restoreLayers()
|
|
935
|
+
if (document.body !== null) document.body.style.removeProperty(token)
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
/**
|
|
940
|
+
* 顶部把手 + 小工具条(只在侧栏窗口里出现)。
|
|
941
|
+
*
|
|
942
|
+
* 窗口没有标题栏,这就是它唯一的窗口级控件:位置在顶部正中、**底色全透明**。
|
|
943
|
+
* 收起时是一道很扁的 ∧,点一下翻成 ∨ 并摊开「固定 / 退出」两个图标键。
|
|
944
|
+
*
|
|
945
|
+
* 两个必须做对的地方:
|
|
946
|
+
* - 展开期间要外壳软固定(hold):否则手一离开窗口,失焦收起会把窗口连同工具条一起带走;
|
|
947
|
+
* - 窗口被收起(热键 / 退出)时页面会变 hidden,工具条要自己收掉,否则下次呼出来还是摊开的、
|
|
948
|
+
* 而外壳那边 hold 已经松了 —— 状态就对不上了。
|
|
949
|
+
*
|
|
950
|
+
* 关闭只有两种:再点把手、点外面。不接管 Esc。
|
|
951
|
+
*/
|
|
952
|
+
const TOOLBAR_CSS = [
|
|
953
|
+
'[data-sw-tool]{background:transparent;border:0;padding:0;cursor:pointer;display:flex;',
|
|
954
|
+
'align-items:center;justify-content:center;color:var(--dsw-alias-label-secondary,#5a6070);',
|
|
955
|
+
'opacity:.82;transition:opacity .15s ease,color .15s ease;outline:none}',
|
|
956
|
+
'[data-sw-tool]:hover{opacity:1;color:var(--dsw-alias-label-primary,#0f1115)}',
|
|
957
|
+
'[data-sw-tool][data-active="true"]{opacity:1;color:var(--dsw-alias-brand-primary,#4d6bfe)}',
|
|
958
|
+
'[data-sw-tool][data-sw-handle]{width:44px;height:14px;color:var(--dsw-alias-label-tertiary,#8a90a0);opacity:.5}',
|
|
959
|
+
'[data-sw-tool][data-sw-handle]:hover{opacity:1}',
|
|
960
|
+
'[data-sw-tool][data-sw-action]{width:26px;height:26px}',
|
|
961
|
+
'[data-sw-chev]{transition:transform .18s ease}',
|
|
962
|
+
].join('')
|
|
963
|
+
|
|
964
|
+
/** 一个 16×16 的描边图标;paths 是若干条 path 的 d。 */
|
|
965
|
+
function strokeIcon(paths, size) {
|
|
966
|
+
return h(
|
|
967
|
+
'svg',
|
|
968
|
+
{
|
|
969
|
+
width: size,
|
|
970
|
+
height: size,
|
|
971
|
+
viewBox: '0 0 16 16',
|
|
972
|
+
fill: 'none',
|
|
973
|
+
stroke: 'currentColor',
|
|
974
|
+
strokeWidth: 1.6,
|
|
975
|
+
strokeLinecap: 'round',
|
|
976
|
+
strokeLinejoin: 'round',
|
|
977
|
+
},
|
|
978
|
+
paths.map((d, index) => h('path', { key: index, d })),
|
|
979
|
+
)
|
|
980
|
+
}
|
|
981
|
+
|
|
982
|
+
/** 固定:图钉。 */
|
|
983
|
+
const PIN_PATHS = ['M5 2.5h6', 'M6.2 2.5 5.4 6.2h5.2L9.8 2.5', 'M4.2 6.2h7.6', 'M8 6.2V14']
|
|
984
|
+
/** 退出:往右边框里收(就是窗口实际的动作)。 */
|
|
985
|
+
const EXIT_PATHS = ['M3 8h6.2', 'M6.8 5.4 9.4 8l-2.6 2.6', 'M12.2 3.8v8.4']
|
|
986
|
+
|
|
987
|
+
function WindowToolbar() {
|
|
988
|
+
const [open, setOpen] = useState(false)
|
|
989
|
+
const [pinned, setPinned] = useState(false)
|
|
990
|
+
const rootRef = useRef(null)
|
|
991
|
+
const api = typeof window === 'undefined' ? undefined : window.__dshSidebar
|
|
992
|
+
const ready = api !== undefined && typeof api.setPinned === 'function'
|
|
993
|
+
|
|
994
|
+
// 初始状态 + 外壳广播(别处改了固定,按钮跟着变)
|
|
995
|
+
useEffect(() => {
|
|
996
|
+
if (!ready) return () => {}
|
|
997
|
+
let alive = true
|
|
998
|
+
api
|
|
999
|
+
.getPinned()
|
|
1000
|
+
.then((value) => {
|
|
1001
|
+
if (alive) setPinned(value === true)
|
|
1002
|
+
})
|
|
1003
|
+
.catch(() => {})
|
|
1004
|
+
const off = api.onPinnedChange((value) => setPinned(value === true))
|
|
1005
|
+
return () => {
|
|
1006
|
+
alive = false
|
|
1007
|
+
off()
|
|
1008
|
+
}
|
|
1009
|
+
}, [ready])
|
|
1010
|
+
|
|
1011
|
+
// 只在卸载时释放一次;展开/收起用下面那个 effect,免得每次切换都先松后紧
|
|
1012
|
+
useEffect(() => {
|
|
1013
|
+
if (!ready) return () => {}
|
|
1014
|
+
return () => {
|
|
1015
|
+
api.setHold(false).catch(() => {})
|
|
1016
|
+
}
|
|
1017
|
+
}, [ready])
|
|
1018
|
+
|
|
1019
|
+
// 展开 = 软固定
|
|
1020
|
+
useEffect(() => {
|
|
1021
|
+
if (!ready) return
|
|
1022
|
+
api.setHold(open).catch(() => {})
|
|
1023
|
+
}, [open, ready])
|
|
1024
|
+
|
|
1025
|
+
// 点外面关闭:捕获阶段,避免被页面自己的 pointerdown 吃掉
|
|
1026
|
+
useEffect(() => {
|
|
1027
|
+
if (!open) return () => {}
|
|
1028
|
+
const onPointerDown = (event) => {
|
|
1029
|
+
if (rootRef.current !== null && rootRef.current.contains(event.target)) return
|
|
1030
|
+
setOpen(false)
|
|
1031
|
+
}
|
|
1032
|
+
document.addEventListener('pointerdown', onPointerDown, true)
|
|
1033
|
+
return () => document.removeEventListener('pointerdown', onPointerDown, true)
|
|
1034
|
+
}, [open])
|
|
1035
|
+
|
|
1036
|
+
// 窗口收起 → 页面 hidden:把工具条收掉,别让下次呼出来还是摊开的
|
|
1037
|
+
useEffect(() => {
|
|
1038
|
+
const onVisibility = () => {
|
|
1039
|
+
if (document.hidden) setOpen(false)
|
|
1040
|
+
}
|
|
1041
|
+
document.addEventListener('visibilitychange', onVisibility)
|
|
1042
|
+
return () => document.removeEventListener('visibilitychange', onVisibility)
|
|
1043
|
+
}, [])
|
|
1044
|
+
|
|
1045
|
+
if (!ready) return null
|
|
1046
|
+
|
|
1047
|
+
const togglePin = () => {
|
|
1048
|
+
const next = !pinned
|
|
1049
|
+
setPinned(next)
|
|
1050
|
+
api
|
|
1051
|
+
.setPinned(next)
|
|
1052
|
+
.then((value) => setPinned(value === true))
|
|
1053
|
+
.catch(() => setPinned(!next))
|
|
1054
|
+
}
|
|
1055
|
+
const leave = () => {
|
|
1056
|
+
setOpen(false)
|
|
1057
|
+
api.hide().catch(() => {})
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
return h(
|
|
1061
|
+
'div',
|
|
1062
|
+
{
|
|
1063
|
+
ref: rootRef,
|
|
1064
|
+
style: {
|
|
1065
|
+
position: 'fixed',
|
|
1066
|
+
top: '4px',
|
|
1067
|
+
left: '50%',
|
|
1068
|
+
transform: 'translateX(-50%)',
|
|
1069
|
+
display: 'flex',
|
|
1070
|
+
flexDirection: 'column',
|
|
1071
|
+
alignItems: 'center',
|
|
1072
|
+
gap: '2px',
|
|
1073
|
+
zIndex: 2147483000,
|
|
1074
|
+
pointerEvents: 'auto',
|
|
1075
|
+
background: 'transparent',
|
|
1076
|
+
},
|
|
1077
|
+
},
|
|
1078
|
+
h('style', { key: 'css' }, TOOLBAR_CSS),
|
|
1079
|
+
h(
|
|
1080
|
+
'button',
|
|
1081
|
+
{
|
|
1082
|
+
key: 'handle',
|
|
1083
|
+
type: 'button',
|
|
1084
|
+
title: open ? '收起工具条' : '窗口工具',
|
|
1085
|
+
'aria-expanded': open,
|
|
1086
|
+
'data-sw-tool': '',
|
|
1087
|
+
'data-sw-handle': '',
|
|
1088
|
+
onClick: () => setOpen(!open),
|
|
1089
|
+
},
|
|
1090
|
+
h(
|
|
1091
|
+
'svg',
|
|
1092
|
+
{
|
|
1093
|
+
width: 22,
|
|
1094
|
+
height: 8,
|
|
1095
|
+
viewBox: '0 0 22 8',
|
|
1096
|
+
fill: 'none',
|
|
1097
|
+
stroke: 'currentColor',
|
|
1098
|
+
strokeWidth: 1.3,
|
|
1099
|
+
strokeLinecap: 'round',
|
|
1100
|
+
'data-sw-chev': '',
|
|
1101
|
+
style: { transform: open ? 'rotate(180deg)' : 'none' },
|
|
1102
|
+
},
|
|
1103
|
+
h('path', { d: 'M2 7 11 2 20 7' }),
|
|
1104
|
+
),
|
|
1105
|
+
),
|
|
1106
|
+
open
|
|
1107
|
+
? h(
|
|
1108
|
+
'div',
|
|
1109
|
+
{
|
|
1110
|
+
key: 'row',
|
|
1111
|
+
style: { display: 'flex', alignItems: 'center', gap: '2px', background: 'transparent' },
|
|
1112
|
+
},
|
|
1113
|
+
h(
|
|
1114
|
+
'button',
|
|
1115
|
+
{
|
|
1116
|
+
type: 'button',
|
|
1117
|
+
title: pinned ? '已固定(不自动收起 + 置顶)' : '固定(不自动收起 + 置顶)',
|
|
1118
|
+
'aria-pressed': pinned,
|
|
1119
|
+
'data-sw-tool': '',
|
|
1120
|
+
'data-sw-action': '',
|
|
1121
|
+
'data-active': pinned ? 'true' : 'false',
|
|
1122
|
+
onClick: togglePin,
|
|
1123
|
+
},
|
|
1124
|
+
strokeIcon(PIN_PATHS, 16),
|
|
1125
|
+
),
|
|
1126
|
+
h(
|
|
1127
|
+
'button',
|
|
1128
|
+
{
|
|
1129
|
+
type: 'button',
|
|
1130
|
+
title: '退出侧栏(热键可再叫回来)',
|
|
1131
|
+
'data-sw-tool': '',
|
|
1132
|
+
'data-sw-action': '',
|
|
1133
|
+
onClick: leave,
|
|
1134
|
+
},
|
|
1135
|
+
strokeIcon(EXIT_PATHS, 16),
|
|
1136
|
+
),
|
|
1137
|
+
)
|
|
1138
|
+
: null,
|
|
1139
|
+
)
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
/** 设置页本体。 */
|
|
1143
|
+
function Section() {
|
|
1144
|
+
const [config, setConfig] = useState(null)
|
|
1145
|
+
const [error, setError] = useState('')
|
|
1146
|
+
const [status, setStatus] = useState(null)
|
|
1147
|
+
const [busy, setBusy] = useState(false)
|
|
1148
|
+
const [runtime, setRuntime] = useState(null)
|
|
1149
|
+
const [hotkeyState, setHotkeyState] = useState(null)
|
|
1150
|
+
/** 上一次「宿主确认写盘」的配置:用来判断表单里有没有未保存的改动。 */
|
|
1151
|
+
const [saved, setSaved] = useState(null)
|
|
1152
|
+
|
|
1153
|
+
useEffect(() => {
|
|
1154
|
+
let alive = true
|
|
1155
|
+
readConfig()
|
|
1156
|
+
.then((next) => {
|
|
1157
|
+
if (!alive) return
|
|
1158
|
+
setConfig(next)
|
|
1159
|
+
setSaved(next)
|
|
1160
|
+
})
|
|
1161
|
+
.catch((cause) => {
|
|
1162
|
+
if (alive) setError('读取侧栏设置失败:' + String((cause && cause.message) || cause))
|
|
1163
|
+
})
|
|
1164
|
+
return () => {
|
|
1165
|
+
alive = false
|
|
1166
|
+
}
|
|
1167
|
+
}, [])
|
|
1168
|
+
|
|
1169
|
+
// 运行时状态:外壳要一份独立的 Electron,首次使用才下载。轮询而不是只读一次,
|
|
1170
|
+
// 因为下载在宿主后台跑,用户在这里能看着它变成「已就绪」。
|
|
1171
|
+
useEffect(() => {
|
|
1172
|
+
let alive = true
|
|
1173
|
+
const tick = () => {
|
|
1174
|
+
fetch('/dsh-side-window/runtime', { cache: 'no-store' })
|
|
1175
|
+
.then((response) => {
|
|
1176
|
+
// 宿主半侧还是旧代码时这条路由不存在:说清楚,而不是一直「检查中…」
|
|
1177
|
+
if (response.status === 404) {
|
|
1178
|
+
return { ok: true, runtime: { ready: false, state: 'error', message: '宿主的运行时接口还没生效:重组一次 profile 或重启 DSH' } }
|
|
1179
|
+
}
|
|
1180
|
+
return response.ok ? response.json() : null
|
|
1181
|
+
})
|
|
1182
|
+
.then((body) => {
|
|
1183
|
+
if (alive && body !== null && body.ok === true) {
|
|
1184
|
+
setRuntime(body.runtime === undefined ? null : body.runtime)
|
|
1185
|
+
// 外壳写下的注册结果;旧宿主没有这个字段,当"不知道"处理
|
|
1186
|
+
setHotkeyState(body.hotkey === undefined ? null : body.hotkey)
|
|
1187
|
+
}
|
|
1188
|
+
})
|
|
1189
|
+
.catch(() => {})
|
|
1190
|
+
}
|
|
1191
|
+
tick()
|
|
1192
|
+
const timer = setInterval(tick, 1500)
|
|
1193
|
+
return () => {
|
|
1194
|
+
alive = false
|
|
1195
|
+
clearInterval(timer)
|
|
1196
|
+
}
|
|
1197
|
+
}, [])
|
|
1198
|
+
|
|
1199
|
+
/** 手动下载/重试:宿主立即返回,下载完成会把外壳拉起来。 */
|
|
1200
|
+
const installRuntime = useCallback(() => {
|
|
1201
|
+
fetch('/dsh-side-window/runtime/install', { method: 'POST' })
|
|
1202
|
+
.then((response) => response.json().catch(() => null))
|
|
1203
|
+
.then((body) => {
|
|
1204
|
+
if (body !== null && body.ok === true) setRuntime(body.runtime)
|
|
1205
|
+
})
|
|
1206
|
+
.catch(() => {})
|
|
1207
|
+
}, [])
|
|
1208
|
+
|
|
1209
|
+
const patch = useCallback((next) => {
|
|
1210
|
+
setConfig((previous) => (previous === null ? previous : { ...previous, ...next }))
|
|
1211
|
+
setStatus(null)
|
|
1212
|
+
}, [])
|
|
1213
|
+
|
|
1214
|
+
/**
|
|
1215
|
+
* 画布不透明度的自动保存(防抖 250ms)。
|
|
1216
|
+
*
|
|
1217
|
+
* 这一项是「拖着看效果」的控件,所以不跟其它设置一样等「保存」:拖动即写盘,
|
|
1218
|
+
* 侧栏窗口 1 秒轮询拿到新值。只 POST `{bgAlpha}` 一个字段(宿主按 merge 处理),
|
|
1219
|
+
* 不会把表单里其它没保存的改动一起写进去。
|
|
1220
|
+
*/
|
|
1221
|
+
const bgSaveTimer = useRef(null)
|
|
1222
|
+
const saveBgAlpha = useCallback((value) => {
|
|
1223
|
+
if (bgSaveTimer.current !== null) clearTimeout(bgSaveTimer.current)
|
|
1224
|
+
bgSaveTimer.current = setTimeout(() => {
|
|
1225
|
+
bgSaveTimer.current = null
|
|
1226
|
+
fetch('/dsh-side-window/config', {
|
|
1227
|
+
method: 'POST',
|
|
1228
|
+
headers: { 'content-type': 'application/json' },
|
|
1229
|
+
body: JSON.stringify({ bgAlpha: value }),
|
|
1230
|
+
})
|
|
1231
|
+
.then((response) => response.json().catch(() => null))
|
|
1232
|
+
.then((body) => {
|
|
1233
|
+
if (body === null || body.ok !== true) return
|
|
1234
|
+
// 只同步这一个字段:整份替换会把用户没保存的其它改动冲掉
|
|
1235
|
+
setSaved((previous) => (previous === null ? previous : { ...previous, bgAlpha: body.config.bgAlpha }))
|
|
1236
|
+
})
|
|
1237
|
+
.catch(() => {})
|
|
1238
|
+
}, 250)
|
|
1239
|
+
}, [])
|
|
1240
|
+
useEffect(
|
|
1241
|
+
() => () => {
|
|
1242
|
+
if (bgSaveTimer.current !== null) clearTimeout(bgSaveTimer.current)
|
|
1243
|
+
},
|
|
1244
|
+
[],
|
|
1245
|
+
)
|
|
1246
|
+
|
|
1247
|
+
const save = useCallback(async () => {
|
|
1248
|
+
if (config === null || busy) return
|
|
1249
|
+
setBusy(true)
|
|
1250
|
+
setStatus(null)
|
|
1251
|
+
try {
|
|
1252
|
+
const written = await writeConfig(config)
|
|
1253
|
+
setConfig(written)
|
|
1254
|
+
setSaved(written)
|
|
1255
|
+
setStatus({ kind: 'ok', text: '已保存。快捷键、置顶、跟随立即生效;宽度 / 透明度 / 自动收起点「保存并重启」。' })
|
|
1256
|
+
} catch (cause) {
|
|
1257
|
+
setStatus({ kind: 'error', text: '保存失败:' + String((cause && cause.message) || cause) })
|
|
1258
|
+
} finally {
|
|
1259
|
+
setBusy(false)
|
|
1260
|
+
}
|
|
1261
|
+
}, [config, busy])
|
|
1262
|
+
|
|
1263
|
+
/** 保存 + 重启:设置改完一般都要重启才生效,合成一个按钮少点一次。 */
|
|
1264
|
+
const saveAndRestart = useCallback(async () => {
|
|
1265
|
+
if (config === null || busy) return
|
|
1266
|
+
setBusy(true)
|
|
1267
|
+
setStatus(null)
|
|
1268
|
+
try {
|
|
1269
|
+
const written = await writeConfig(config)
|
|
1270
|
+
setConfig(written)
|
|
1271
|
+
setSaved(written)
|
|
1272
|
+
const response = await fetch('/dsh-side-window/restart', { method: 'POST' })
|
|
1273
|
+
if (response.status === 404 || response.status === 405) {
|
|
1274
|
+
setStatus({ kind: 'error', text: '已保存,但宿主还没有重启接口:重启一次 DSH 后即可一键重启。' })
|
|
1275
|
+
return
|
|
1276
|
+
}
|
|
1277
|
+
const body = await response.json().catch(() => null)
|
|
1278
|
+
if (!response.ok || !body || body.ok !== true) {
|
|
1279
|
+
throw new Error((body && body.message) || 'HTTP ' + response.status)
|
|
1280
|
+
}
|
|
1281
|
+
setStatus({ kind: 'ok', text: '已保存,侧栏窗口已重启。' })
|
|
1282
|
+
} catch (cause) {
|
|
1283
|
+
setStatus({ kind: 'error', text: '重启失败:' + String((cause && cause.message) || cause) })
|
|
1284
|
+
} finally {
|
|
1285
|
+
setBusy(false)
|
|
1286
|
+
}
|
|
1287
|
+
}, [config, busy])
|
|
1288
|
+
|
|
1289
|
+
if (error !== '') {
|
|
1290
|
+
return h('div', { style: { ...S.root, color: C.danger } }, error)
|
|
1291
|
+
}
|
|
1292
|
+
if (config === null) {
|
|
1293
|
+
return h('div', { style: { ...S.root, color: C.tertiary } }, '正在读取侧栏设置…')
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
// 纯渲染保护:宿主正常时这两个值一定存在;只有半途换了旧宿主才会走到 `||` 后面。
|
|
1297
|
+
// 真实默认值在宿主里,这里不复制一份。
|
|
1298
|
+
const width = Number(config.width) || 460
|
|
1299
|
+
const bgPercent = Math.round(Math.max(0.4, Math.min(1, Number(config.bgAlpha) || 1)) * 100)
|
|
1300
|
+
const hotkeyText =
|
|
1301
|
+
config.hotkey === undefined || config.hotkey === null || String(config.hotkey).trim() === ''
|
|
1302
|
+
? 'Ctrl+Shift+L'
|
|
1303
|
+
: String(config.hotkey)
|
|
1304
|
+
const disabled = busy
|
|
1305
|
+
// 有未保存改动时,底部别把表单值说成「当前生效的值」
|
|
1306
|
+
const dirty = saved !== null && JSON.stringify(saved) !== JSON.stringify(config)
|
|
1307
|
+
|
|
1308
|
+
return h(
|
|
1309
|
+
'div',
|
|
1310
|
+
{ style: S.root },
|
|
1311
|
+
h(
|
|
1312
|
+
'div',
|
|
1313
|
+
{ style: S.intro },
|
|
1314
|
+
'窗口宽度、自动收起需要重启侧栏窗口才生效;「收拉快捷键」「窗口置顶」「跟随对话」保存后立即生效。',
|
|
1315
|
+
),
|
|
1316
|
+
h(
|
|
1317
|
+
'div',
|
|
1318
|
+
{ style: S.card },
|
|
1319
|
+
h(Toggle, {
|
|
1320
|
+
label: '启动弹出',
|
|
1321
|
+
hint: '在启动时,自动弹出侧边框。',
|
|
1322
|
+
checked: config.autoShow === true,
|
|
1323
|
+
disabled,
|
|
1324
|
+
onChange: (next) => patch({ autoShow: next }),
|
|
1325
|
+
}),
|
|
1326
|
+
h('div', { style: { borderTop: '0.5px solid ' + C.border } }),
|
|
1327
|
+
h(HotkeyField, {
|
|
1328
|
+
value: hotkeyText,
|
|
1329
|
+
state: hotkeyState,
|
|
1330
|
+
disabled,
|
|
1331
|
+
onChange: (next) => patch({ hotkey: next }),
|
|
1332
|
+
}),
|
|
1333
|
+
h('div', { style: { borderTop: '0.5px solid ' + C.border } }),
|
|
1334
|
+
h(Slider, {
|
|
1335
|
+
label: '窗口宽度',
|
|
1336
|
+
display: width + ' px',
|
|
1337
|
+
min: 320,
|
|
1338
|
+
max: 900,
|
|
1339
|
+
step: 10,
|
|
1340
|
+
value: width,
|
|
1341
|
+
disabled,
|
|
1342
|
+
hint: '保存并重启后生效。',
|
|
1343
|
+
onChange: (next) => patch({ width: next }),
|
|
1344
|
+
}),
|
|
1345
|
+
h('div', { style: { borderTop: '0.5px solid ' + C.border } }),
|
|
1346
|
+
h(Slider, {
|
|
1347
|
+
label: '不透明度',
|
|
1348
|
+
display: bgPercent + ' %',
|
|
1349
|
+
min: 40,
|
|
1350
|
+
max: 100,
|
|
1351
|
+
step: 5,
|
|
1352
|
+
value: bgPercent,
|
|
1353
|
+
disabled,
|
|
1354
|
+
hint: '影响侧边窗口的底色。拖动1s后自动切换',
|
|
1355
|
+
onChange: (next) => {
|
|
1356
|
+
const value = next / 100
|
|
1357
|
+
patch({ bgAlpha: value })
|
|
1358
|
+
saveBgAlpha(value)
|
|
1359
|
+
},
|
|
1360
|
+
}),
|
|
1361
|
+
h('div', { style: { borderTop: '0.5px solid ' + C.border } }),
|
|
1362
|
+
h(Toggle, {
|
|
1363
|
+
label: '强制透明',
|
|
1364
|
+
hint: '强制进行底色透明,可以解决其他插件导致的透明无效化,但效果不稳定,保存后1s左右生效。',
|
|
1365
|
+
checked: config.forceTransparentLayers === true,
|
|
1366
|
+
disabled,
|
|
1367
|
+
onChange: (next) => patch({ forceTransparentLayers: next }),
|
|
1368
|
+
}),
|
|
1369
|
+
h('div', { style: { borderTop: '0.5px solid ' + C.border } }),
|
|
1370
|
+
h(Toggle, {
|
|
1371
|
+
label: '自动收起',
|
|
1372
|
+
hint: '侧边窗口在失去焦点时自动收起。保存并重启后生效',
|
|
1373
|
+
checked: config.autoHideOnBlur !== false,
|
|
1374
|
+
disabled,
|
|
1375
|
+
onChange: (next) => patch({ autoHideOnBlur: next }),
|
|
1376
|
+
}),
|
|
1377
|
+
h('div', { style: { borderTop: '0.5px solid ' + C.border } }),
|
|
1378
|
+
h(Toggle, {
|
|
1379
|
+
label: '窗口置顶',
|
|
1380
|
+
hint: '置顶侧栏窗口,保存后生效。',
|
|
1381
|
+
checked: config.alwaysOnTop === true,
|
|
1382
|
+
disabled,
|
|
1383
|
+
onChange: (next) => patch({ alwaysOnTop: next }),
|
|
1384
|
+
}),
|
|
1385
|
+
h('div', { style: { borderTop: '0.5px solid ' + C.border } }),
|
|
1386
|
+
h(Toggle, {
|
|
1387
|
+
label: '跟随对话',
|
|
1388
|
+
hint: '侧栏窗口跟随主dsh切换对话,保存后生效。',
|
|
1389
|
+
checked: config.followMainSession !== false,
|
|
1390
|
+
disabled,
|
|
1391
|
+
onChange: (next) => patch({ followMainSession: next }),
|
|
1392
|
+
}),
|
|
1393
|
+
),
|
|
1394
|
+
h(
|
|
1395
|
+
'div',
|
|
1396
|
+
{ style: S.card },
|
|
1397
|
+
h(
|
|
1398
|
+
'div',
|
|
1399
|
+
{ style: S.row },
|
|
1400
|
+
h(
|
|
1401
|
+
'div',
|
|
1402
|
+
{ style: S.rowText },
|
|
1403
|
+
h('span', null, '窗口运行时'),
|
|
1404
|
+
h('span', { style: S.hint }, runtimeText(runtime)),
|
|
1405
|
+
),
|
|
1406
|
+
runtime !== null && runtime.ready === false && runtime.state !== 'downloading'
|
|
1407
|
+
? h(
|
|
1408
|
+
'button',
|
|
1409
|
+
{ type: 'button', onClick: installRuntime, style: S.button },
|
|
1410
|
+
runtime.state === 'error' ? '重试' : '下载并安装',
|
|
1411
|
+
)
|
|
1412
|
+
: null,
|
|
1413
|
+
),
|
|
1414
|
+
),
|
|
1415
|
+
h(
|
|
1416
|
+
'div',
|
|
1417
|
+
{ style: S.footer },
|
|
1418
|
+
h(
|
|
1419
|
+
'button',
|
|
1420
|
+
{
|
|
1421
|
+
type: 'button',
|
|
1422
|
+
onClick: save,
|
|
1423
|
+
disabled,
|
|
1424
|
+
style: { ...S.buttonPrimary, ...(disabled ? S.buttonDisabled : {}) },
|
|
1425
|
+
},
|
|
1426
|
+
busy ? '处理中…' : '保存',
|
|
1427
|
+
),
|
|
1428
|
+
h(
|
|
1429
|
+
'button',
|
|
1430
|
+
{
|
|
1431
|
+
type: 'button',
|
|
1432
|
+
onClick: saveAndRestart,
|
|
1433
|
+
disabled,
|
|
1434
|
+
style: { ...S.button, ...(disabled ? S.buttonDisabled : {}) },
|
|
1435
|
+
},
|
|
1436
|
+
'保存并重启',
|
|
1437
|
+
),
|
|
1438
|
+
h(
|
|
1439
|
+
'span',
|
|
1440
|
+
{
|
|
1441
|
+
style: {
|
|
1442
|
+
...S.status,
|
|
1443
|
+
color: status === null ? C.tertiary : status.kind === 'ok' ? C.success : C.danger,
|
|
1444
|
+
},
|
|
1445
|
+
},
|
|
1446
|
+
status === null ? (dirty ? '有未保存的修改' : '呼出快捷键:' + hotkeyText) : status.text,
|
|
1447
|
+
),
|
|
1448
|
+
),
|
|
1449
|
+
)
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1452
|
+
return {
|
|
1453
|
+
inject: ['slots'],
|
|
1454
|
+
apply(ctx) {
|
|
1455
|
+
ctx.effect(() => startSessionPublisher(), 'dsh-side-window: publish active session')
|
|
1456
|
+
ctx.effect(() => startSessionFollower(ctx), 'dsh-side-window: follow main session')
|
|
1457
|
+
ctx.effect(() => registerOpenMenus(ctx), 'dsh-side-window: open in side window menu')
|
|
1458
|
+
ctx.effect(() => startSidebarOpener(ctx), 'dsh-side-window: open in side window handler')
|
|
1459
|
+
ctx.effect(() => startCanvasEffects(), 'dsh-side-window: canvas effects')
|
|
1460
|
+
ctx.slots.inject('settings.section', () =>
|
|
1461
|
+
ctx.slots.register(
|
|
1462
|
+
{ name: 'settings.section', id: 'dsh-side-window', order: 34, label: () => '侧栏窗口' },
|
|
1463
|
+
Section,
|
|
1464
|
+
),
|
|
1465
|
+
)
|
|
1466
|
+
// 顶部把手 + 固定/退出:只有侧栏那一侧渲染,主窗口注册了也是 null
|
|
1467
|
+
ctx.slots.inject('shell.overlay', () =>
|
|
1468
|
+
ctx.slots.register(
|
|
1469
|
+
{ name: 'shell.overlay', id: 'dsh-side-window.toolbar', order: 100 },
|
|
1470
|
+
() => (isSidebarWindow() ? h(WindowToolbar) : null),
|
|
1471
|
+
),
|
|
1472
|
+
)
|
|
1473
|
+
},
|
|
1474
|
+
}
|
|
1475
|
+
},
|
|
1476
|
+
})
|