@andy.ian/dsh-homing 2.3.4 → 2.3.7

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/index.js CHANGED
@@ -3060,7 +3060,7 @@ a{color:#8ab4f8}
3060
3060
  return;
3061
3061
  }
3062
3062
 
3063
- if (url.pathname === '/files' || url.pathname === '/files/view' || url.pathname === '/files/wall' || url.pathname === '/files/pdf' || url.pathname === '/files/download' || url.pathname === '/files/raw' || url.pathname === '/files/compress' || url.pathname === '/files/run'
3063
+ if (url.pathname === '/files' || url.pathname === '/files/view' || url.pathname === '/files/wall' || url.pathname === '/files/open' || url.pathname === '/files/pdf' || url.pathname === '/files/download' || url.pathname === '/files/raw' || url.pathname === '/files/compress' || url.pathname === '/files/run'
3064
3064
  || url.pathname === '/files/rename'
3065
3065
  || url.pathname === '/api/file'
3066
3066
  || url.pathname === '/files/delete' || url.pathname === '/files/paste'
@@ -3326,6 +3326,32 @@ a{color:#8ab4f8}
3326
3326
  return;
3327
3327
  }
3328
3328
 
3329
+ // 手机端补丁点聊天里的图片卡片会来这里:按文件名/相对路径在工作区里找到文件,
3330
+ // 找到就跳预览页,找不到退回图片墙(比显示「文件资源服务不可用」有用)。
3331
+ if (url.pathname === '/files/open' && req.method === 'GET') {
3332
+ const wantedName = url.searchParams.get('name') ?? '';
3333
+ const sessionHint = url.searchParams.get('session') ?? '';
3334
+ if (wantedName === '') {
3335
+ res.writeHead(302, { location: '/files', 'cache-control': 'no-store' });
3336
+ res.end();
3337
+ return;
3338
+ }
3339
+ const dshHome = path.resolve(dataDir(), '..', '..');
3340
+ const found = filebrowse.findWorkspaceFile(dshHome, wantedName);
3341
+ appendAudit(dir, 'file-open-card', {
3342
+ name: wantedName.slice(0, 200),
3343
+ session: sessionHint === '' ? null : sessionHint.slice(0, 80),
3344
+ ok: found !== null,
3345
+ path: found === null ? null : found.path,
3346
+ });
3347
+ res.writeHead(302, {
3348
+ location: found === null ? '/files/wall' : '/files/view?path=' + encodeURIComponent(found.path),
3349
+ 'cache-control': 'no-store',
3350
+ });
3351
+ res.end();
3352
+ return;
3353
+ }
3354
+
3329
3355
  if (url.pathname === '/files/view' && req.method === 'GET') {
3330
3356
  const asked = url.searchParams.get('path') ?? '';
3331
3357
  if (office.isOfficeFile(asked) || office.isPdfFile(asked)) {
@@ -17,6 +17,15 @@ export const MOBILE_CSS = `html { -webkit-text-size-adjust: 100%; }
17
17
  @media (max-width: 760px) {
18
18
  [class*="_frame"] { min-width: 0; }
19
19
  }
20
+ /* 浮层(额度弹窗、菜单、对话框)先按视口限宽/限高:
21
+ 宽度超过屏幕的弹窗即使能平移也一定有内容看不见,限宽后第一帧就在屏幕内,
22
+ 再配合 JS 的位置修正,避免「先跑到屏幕外再跳回来」。 */
23
+ [role="dialog"], [role="menu"], [role="listbox"], [data-radix-popper-content-wrapper] {
24
+ max-width: calc(100vw - 16px) !important;
25
+ }
26
+ [role="dialog"] {
27
+ max-height: calc(100vh - 32px) !important;
28
+ }
20
29
  `;
21
30
 
22
31
  /**
@@ -118,6 +127,78 @@ export const MOBILE_JS = String.raw`(() => {
118
127
  window.location.href = '/files' + (hint === '' ? '' : '?hint=' + encodeURIComponent(hint))
119
128
  }
120
129
 
130
+ /* ---------- 聊天里的图片卡片:点一下直接看大图 ----------
131
+ * DSH 网页端点图片卡片会打开应用内标签页,预览组件依赖桌面端专属的
132
+ * 「workspace-files 服务」;网页版没有这个服务,只显示「文件资源服务不可用」。
133
+ * 这里在点击阶段拦截含图片文件名的卡片,跳到插件的预览页;找不到就退回图片墙。
134
+ * 原则:识别不明确时绝不 preventDefault,保证 DSH 原行为不受影响。
135
+ */
136
+ const IMAGE_FILE_NAME_RE = /[^\s"'<>|\\/:]{1,200}\.(?:png|jpe?g|gif|webp|bmp)\b/giu
137
+ const CLICK_IGNORE_SELECTORS = ['[role="menu"]', '[role="navigation"]', 'header', 'nav', 'aside', '[aria-haspopup]', '[aria-expanded]']
138
+
139
+ /** 从一段文本里取最后一个图片文件名(纯函数,方便单测)。 */
140
+ const pickImageFileName = (text) => {
141
+ const value = String(text ?? '')
142
+ if (value.length > 400) return ''
143
+ const matches = value.match(IMAGE_FILE_NAME_RE)
144
+ if (matches === null) return ''
145
+ return matches[matches.length - 1]
146
+ }
147
+
148
+ /** 从点击目标向上找图片文件名;菜单/侧栏里的点击不处理。 */
149
+ const findClickImageName = (target) => {
150
+ let node = target
151
+ for (let depth = 0; depth < 10 && node !== null && node !== undefined; depth += 1) {
152
+ if (typeof node.closest === 'function') {
153
+ for (const selector of CLICK_IGNORE_SELECTORS) {
154
+ if (node.closest(selector) !== null) return ''
155
+ }
156
+ }
157
+ const name = pickImageFileName(node.textContent || '')
158
+ if (name !== '') return name
159
+ node = node.parentElement
160
+ }
161
+ return ''
162
+ }
163
+
164
+ /** 打开某文件的预览页:先让服务端按文件名/相对路径在工作区里解析。 */
165
+ const openFileByName = (rawName) => {
166
+ const name = String(rawName ?? '').trim()
167
+ if (name === '') return false
168
+ const session = currentSessionId()
169
+ window.location.href = '/files/open?name=' + encodeURIComponent(name) + (session === '' ? '' : '&session=' + encodeURIComponent(session))
170
+ return true
171
+ }
172
+ if (window.__dshHomingOpenFile === undefined) window.__dshHomingOpenFile = openFileByName
173
+
174
+ document.addEventListener('click', (event) => {
175
+ try {
176
+ const target = event.target
177
+ if (target === null || target === undefined) return
178
+ const name = findClickImageName(target)
179
+ if (name === '') return
180
+ // 已经是我们自己的文件链接(比如文件页里的图片名),不要二次拦截
181
+ const anchor = typeof target.closest === 'function' ? target.closest('a[href]') : null
182
+ if (anchor !== null && /^\/files\//u.test(String(anchor.getAttribute('href') ?? ''))) return
183
+ event.preventDefault()
184
+ event.stopPropagation()
185
+ openFileByName(name)
186
+ } catch { /* 补丁绝不能影响 DSH 本身 */ }
187
+ }, true)
188
+
189
+ /** 图片墙快捷入口:当前会话有会话 id 时带上,服务端会映射到工作区。 */
190
+ const goWall = () => {
191
+ const session = currentSessionId()
192
+ window.location.href = '/files/wall' + (session === '' ? '' : '?session=' + encodeURIComponent(session))
193
+ }
194
+ // 调试/测试用入口:立即重跑菜单增强(生产上 MutationObserver 会自动触发)
195
+ if (window.__dshHomingEnhanceMenus === undefined) {
196
+ window.__dshHomingEnhanceMenus = () => {
197
+ enhanceMenus()
198
+ pruneMenuItems()
199
+ }
200
+ }
201
+
121
202
  /* ---------- 页头「文件夹 + 小箭头」分体按钮 ----------
122
203
  * 它做的事是在 DSH 所在的电脑上启动程序(资源管理器 / VS Code / Git Bash)。
123
204
  * 手机上没有那台电脑的屏幕,点了只会收到「已在其它设备打开」——纯噪音。
@@ -253,6 +334,7 @@ export const MOBILE_JS = String.raw`(() => {
253
334
  const MENU_HINT = /下载 Session 日志|复制会话为 Markdown|反馈/u
254
335
  const ICON_RESTART = '<path d="M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"/><path d="M3 3v5h5"/>'
255
336
  const ICON_DOWNLOAD = '<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><path d="M7 10l5 5 5-5"/><path d="M12 15V3"/>'
337
+ const ICON_IMAGE = '<rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="9" cy="9" r="2"/><path d="m21 15-3.1-3.1a2 2 0 0 0-2.8 0L6 21"/>'
256
338
  let menuTimer = null
257
339
 
258
340
  const textNodeMatching = (node, pattern) => {
@@ -289,6 +371,17 @@ export const MOBILE_JS = String.raw`(() => {
289
371
  return link
290
372
  }
291
373
 
374
+ // 我们是在 MutationObserver 回调里改 DOM 的:如果每次都「删掉再加一遍」,
375
+ // 自己的改动会再次触发观察器 → 80ms 一次无限循环,菜单项肉眼可见地一直闪
376
+ //(2026-11-22 手机录屏实测)。
377
+ // 用标记 + 数量校验做幂等:已增强过就原地跳过,保证不再产生新 mutation。
378
+ const MENU_MARKER = 'data-dsh-homing-menu'
379
+ const menuIsEnhanced = (menu) =>
380
+ menu.getAttribute(MENU_MARKER) === '1'
381
+ && menu.querySelectorAll('[data-dsh-homing-download]').length === 1
382
+ && menu.querySelectorAll('[data-dsh-homing-wall]').length === 1
383
+ && menu.querySelectorAll('[data-dsh-homing-restart]').length === 1
384
+
292
385
  const enhanceMenus = () => {
293
386
  const done = new Set()
294
387
  for (const item of document.querySelectorAll('[role="menuitem"]')) {
@@ -296,8 +389,9 @@ export const MOBILE_JS = String.raw`(() => {
296
389
  if (menu === null || done.has(menu)) continue
297
390
  if (!MENU_HINT.test(menu.textContent || '')) continue
298
391
  done.add(menu)
392
+ if (menuIsEnhanced(menu)) continue
299
393
 
300
- for (const old of menu.querySelectorAll('[data-dsh-homing-download],[data-dsh-homing-restart]')) old.remove()
394
+ for (const old of menu.querySelectorAll('[data-dsh-homing-download],[data-dsh-homing-restart],[data-dsh-homing-wall]')) old.remove()
301
395
 
302
396
  const items = [...menu.querySelectorAll('[role="menuitem"]')]
303
397
  const template = items.find((node) => node.querySelector('svg')) ?? items[0]
@@ -305,27 +399,29 @@ export const MOBILE_JS = String.raw`(() => {
305
399
 
306
400
  const download = buildMenuItem(template, '下载工作区文件', ICON_DOWNLOAD, () => goFiles())
307
401
  download.setAttribute('data-dsh-homing-download', '1')
402
+ const wall = buildMenuItem(template, '看图(图片墙)', ICON_IMAGE, () => goWall())
403
+ wall.setAttribute('data-dsh-homing-wall', '1')
308
404
  const restart = buildMenuItem(template, '重启 DSH / 守护进程', ICON_RESTART, () => window.location.assign('/dsh_homing/restart'))
309
405
  restart.setAttribute('data-dsh-homing-restart', '1')
310
406
 
311
407
  menu.appendChild(download)
408
+ menu.appendChild(wall)
312
409
  menu.appendChild(restart)
410
+ // 标记必须在本轮 mutation 被观察器处理前写好(同一个任务内完成),下一轮直接跳过
411
+ menu.setAttribute(MENU_MARKER, '1')
313
412
  }
314
413
  }
315
414
 
316
415
  // 兜底:无论菜单容器怎么嵌套,每类最多保留一个
317
416
  const pruneMenuItems = () => {
318
- for (const selector of ['[data-dsh-homing-download]', '[data-dsh-homing-restart]']) {
417
+ for (const selector of ['[data-dsh-homing-download]', '[data-dsh-homing-wall]', '[data-dsh-homing-restart]']) {
319
418
  const all = [...document.querySelectorAll(selector)]
320
419
  for (let i = 0; i < all.length - 1; i += 1) all[i].remove()
321
420
  }
322
421
  }
323
422
 
324
- const menuObserver = new MutationObserver(() => {
325
- if (menuTimer) return
326
- menuTimer = setTimeout(() => { menuTimer = null; enhanceMenus(); pruneMenuItems(); scheduleClamp() }, 80)
327
- })
328
- menuObserver.observe(document.body, { childList: true, subtree: true })
423
+ // 菜单/浮层的 MutationObserver 在「浮层不越界」定义之后统一注册(见下方),
424
+ // 因为新的浮层出现时要在同一个微任务里立刻修正位置。
329
425
 
330
426
  /* ---------- 浮层不越出屏幕 ----------
331
427
  * 额度面板、菜单、对话框大多按「相对触发按钮」定位,窄屏上会有一部分落到
@@ -396,6 +492,40 @@ export const MOBILE_JS = String.raw`(() => {
396
492
  // 调试/测试入口:也可手动触发一次
397
493
  if (window.__dshHomingClampPopovers === undefined) window.__dshHomingClampPopovers = clampPopovers
398
494
 
495
+ /* ---------- DOM 观察器 ----------
496
+ * 两件事:
497
+ * 1) 新浮层出现时,在 MutationObserver 微任务里「同步」修正位置。
498
+ * MutationObserver 回调发生在浏览器绘制之前,所以用户看到的第一帧就是
499
+ * 修正后的位置,不会出现「先在屏幕外冒出来、120ms 后再跳回来」
500
+ * (2026-11-22 录屏里的 OpenCode 额度弹窗就是这个现象)。
501
+ * 2) 会话菜单增强 + prunes(80ms 防抖,避免 DSH 流式输出时频繁扫描)。
502
+ */
503
+ const OBSERVED_POPOVER_SELECTOR = 'body > div, [role="dialog"], [role="menu"], [role="listbox"], [data-radix-popper-content-wrapper]'
504
+ const isPopoverNode = (node) => {
505
+ if (node === null || node === undefined || typeof node.matches !== 'function') return false
506
+ try {
507
+ if (node.matches(OBSERVED_POPOVER_SELECTOR)) return true
508
+ return typeof node.querySelector === 'function' && node.querySelector(OBSERVED_POPOVER_SELECTOR) !== null
509
+ } catch (error) {
510
+ return false
511
+ }
512
+ }
513
+
514
+ const menuObserver = new MutationObserver((records) => {
515
+ let popoverAdded = false
516
+ for (const record of records) {
517
+ const added = record.addedNodes || []
518
+ for (const node of added) {
519
+ if (isPopoverNode(node)) { popoverAdded = true; break }
520
+ }
521
+ if (popoverAdded) break
522
+ }
523
+ if (popoverAdded) clampPopovers()
524
+ if (menuTimer) return
525
+ menuTimer = setTimeout(() => { menuTimer = null; enhanceMenus(); pruneMenuItems(); scheduleClamp() }, 80)
526
+ })
527
+ menuObserver.observe(document.body, { childList: true, subtree: true })
528
+
399
529
  /* ---------- ALIVE_BEACON ----------
400
530
  * 告诉插件"用户正在操作":重启脚本据此判断能否安全重启。
401
531
  * 手机上打字/滑动手势不会修改会话文件,只有这个信标能反映"人在用"。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@andy.ian/dsh-homing",
3
- "version": "2.3.4",
3
+ "version": "2.3.7",
4
4
  "type": "module",
5
5
  "description": "把本机 DSH 通过 SSH 反向隧道安全开放到手机浏览器:TLS 端到端(服务器只透传)、账号 + TOTP、局域网与公网双入口,随 DSH 启动。",
6
6
  "keywords": [