@andy.ian/dsh-homing 2.5.0 → 2.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +25 -0
- package/lib/mobile-patch.mjs +118 -8
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -3084,6 +3084,31 @@ a{color:#8ab4f8}
|
|
|
3084
3084
|
return;
|
|
3085
3085
|
}
|
|
3086
3086
|
|
|
3087
|
+
// 手机端目录选择器的 JSON API(配合 mobile-patch 注入的 __DSH_DIRECTORY_PICKER__):
|
|
3088
|
+
// DSH 网页版点「添加工作区」会调用桌面端原生目录选择器(loopback RPC),手机必然失败。
|
|
3089
|
+
// 插件用这个接口给手机端提供自己的目录浏览能力。
|
|
3090
|
+
if (url.pathname === '/dsh_homing/fs-dirs' && req.method === 'GET') {
|
|
3091
|
+
if (!sessionValid(req)) {
|
|
3092
|
+
res.writeHead(401, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
3093
|
+
res.end(JSON.stringify({ ok: false, error: '请先登录' }));
|
|
3094
|
+
return;
|
|
3095
|
+
}
|
|
3096
|
+
const wanted = url.searchParams.get('path') ?? '';
|
|
3097
|
+
const info = filebrowse.listDirectory(wanted);
|
|
3098
|
+
const dirs = info.entries
|
|
3099
|
+
.filter((entry) => entry.dir)
|
|
3100
|
+
.map((entry) => ({ name: entry.name, path: entry.path }));
|
|
3101
|
+
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' });
|
|
3102
|
+
res.end(JSON.stringify({
|
|
3103
|
+
ok: info.error === undefined,
|
|
3104
|
+
path: info.path,
|
|
3105
|
+
parent: info.parent ?? null,
|
|
3106
|
+
dirs,
|
|
3107
|
+
error: info.error ?? null,
|
|
3108
|
+
}));
|
|
3109
|
+
return;
|
|
3110
|
+
}
|
|
3111
|
+
|
|
3087
3112
|
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'
|
|
3088
3113
|
|| url.pathname === '/files/rename'
|
|
3089
3114
|
|| url.pathname === '/api/file'
|
package/lib/mobile-patch.mjs
CHANGED
|
@@ -69,6 +69,111 @@ export function clampPopoverOffset(rect, viewport, margin = 8) {
|
|
|
69
69
|
export const MOBILE_JS = String.raw`(() => {
|
|
70
70
|
const clampPopoverOffset = ${clampPopoverOffset.toString()}
|
|
71
71
|
const narrow = () => window.innerWidth <= 760
|
|
72
|
+
|
|
73
|
+
/* ---------- 手机端目录选择器(让「添加工作区」在手机上能用) ----------
|
|
74
|
+
* DSH 网页版点「添加工作区」调的是桌面端原生目录选择器(loopback RPC),
|
|
75
|
+
* 手机经公网访问时这个 RPC 不可用(2026-11-23 实测:请求挂起)。
|
|
76
|
+
* DSH 前端留了官方钩子 globalThis.__DSH_DIRECTORY_PICKER__,有它就用自己的
|
|
77
|
+
* pick()。这里提供一个手机可操作的选择器:浏览插件文件服务的目录树。
|
|
78
|
+
* 必须在 DSH 前端加载前定义 —— mobile.js 在 <head> 里同步执行,满足条件。
|
|
79
|
+
*/
|
|
80
|
+
const DIRS_API = '/dsh_homing/fs-dirs'
|
|
81
|
+
const fetchDirs = async (path) => {
|
|
82
|
+
const query = path === '' ? '' : '?path=' + encodeURIComponent(path)
|
|
83
|
+
const response = await fetch(DIRS_API + query, { headers: { accept: 'application/json' }, credentials: 'same-origin' })
|
|
84
|
+
if (!response.ok) throw new Error('目录读取失败:HTTP ' + response.status)
|
|
85
|
+
return await response.json()
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** 打开目录选择浮层,返回 Promise<string|null>(选择目录 / 取消)。 */
|
|
89
|
+
const openDirectoryPicker = () => new Promise((resolve) => {
|
|
90
|
+
const overlay = document.createElement('div')
|
|
91
|
+
overlay.setAttribute('data-dsh-homing-dirpicker', '1')
|
|
92
|
+
overlay.style.cssText = 'position:fixed;inset:0;z-index:2147483647;background:rgba(0,0,0,.55);'
|
|
93
|
+
+ 'display:flex;align-items:flex-end;justify-content:center;'
|
|
94
|
+
const sheet = document.createElement('div')
|
|
95
|
+
sheet.style.cssText = 'background:#171a21;color:#e8eaed;width:100%;max-width:560px;max-height:82vh;'
|
|
96
|
+
+ 'border-radius:16px 16px 0 0;padding:14px 14px 18px;display:flex;flex-direction:column;'
|
|
97
|
+
+ 'font:14px/1.5 system-ui,-apple-system,"Noto Sans SC",sans-serif;box-shadow:0 -8px 30px rgba(0,0,0,.5)'
|
|
98
|
+
const title = document.createElement('div')
|
|
99
|
+
title.textContent = '选择工作区目录'
|
|
100
|
+
title.style.cssText = 'font-size:16px;font-weight:700;margin-bottom:8px'
|
|
101
|
+
const pathLine = document.createElement('div')
|
|
102
|
+
pathLine.style.cssText = 'color:#9aa3b2;font-size:12px;word-break:break-all;margin-bottom:10px;min-height:18px'
|
|
103
|
+
const list = document.createElement('div')
|
|
104
|
+
list.style.cssText = 'overflow:auto;flex:1 1 auto;border:1px solid #262b36;border-radius:10px;background:#0f1319'
|
|
105
|
+
const row = document.createElement('div')
|
|
106
|
+
row.style.cssText = 'display:flex;gap:8px;margin-top:10px'
|
|
107
|
+
const up = document.createElement('button')
|
|
108
|
+
up.textContent = '上一级'
|
|
109
|
+
up.style.cssText = 'flex:0 0 auto;padding:10px 12px;border:1px solid #3a4658;border-radius:9px;background:#232a34;color:#dce9f7'
|
|
110
|
+
const pick = document.createElement('button')
|
|
111
|
+
pick.textContent = '选择当前目录'
|
|
112
|
+
pick.style.cssText = 'flex:1 1 auto;padding:10px;border:0;border-radius:9px;background:#4f7cff;color:#fff;font-weight:600'
|
|
113
|
+
const cancel = document.createElement('button')
|
|
114
|
+
cancel.textContent = '取消'
|
|
115
|
+
cancel.style.cssText = 'flex:0 0 auto;padding:10px 12px;border:1px solid #3a4658;border-radius:9px;background:#232a31;color:#c7cdd8'
|
|
116
|
+
row.append(up, pick, cancel)
|
|
117
|
+
sheet.append(title, pathLine, list, row)
|
|
118
|
+
overlay.appendChild(sheet)
|
|
119
|
+
document.body.appendChild(overlay)
|
|
120
|
+
|
|
121
|
+
let current = ''
|
|
122
|
+
let parent = null
|
|
123
|
+
const close = (value) => {
|
|
124
|
+
try { overlay.remove() } catch (error) { /* 忽略 */ }
|
|
125
|
+
resolve(value)
|
|
126
|
+
}
|
|
127
|
+
const render = (state) => {
|
|
128
|
+
current = state.path
|
|
129
|
+
parent = state.parent
|
|
130
|
+
pathLine.textContent = state.path
|
|
131
|
+
list.textContent = ''
|
|
132
|
+
for (const dir of state.dirs) {
|
|
133
|
+
const item = document.createElement('button')
|
|
134
|
+
item.type = 'button'
|
|
135
|
+
item.textContent = '📁 ' + dir.name
|
|
136
|
+
item.style.cssText = 'display:block;width:100%;text-align:left;padding:11px 12px;background:transparent;'
|
|
137
|
+
+ 'border:0;border-bottom:1px solid #1d2229;color:#e8eaed;font:inherit;cursor:pointer'
|
|
138
|
+
item.addEventListener('click', () => {
|
|
139
|
+
load(dir.path).catch((error) => { pathLine.textContent = '读取失败:' + String(error && error.message ? error.message : error) })
|
|
140
|
+
})
|
|
141
|
+
list.appendChild(item)
|
|
142
|
+
}
|
|
143
|
+
if (state.dirs.length === 0) {
|
|
144
|
+
const empty = document.createElement('div')
|
|
145
|
+
empty.textContent = '(没有子目录)'
|
|
146
|
+
empty.style.cssText = 'padding:14px;color:#8b93a1'
|
|
147
|
+
list.appendChild(empty)
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const load = async (path) => {
|
|
151
|
+
pathLine.textContent = '加载中…'
|
|
152
|
+
try {
|
|
153
|
+
const state = await fetchDirs(path)
|
|
154
|
+
if (state.ok === false) {
|
|
155
|
+
pathLine.textContent = state.error === null ? '读取失败' : String(state.error)
|
|
156
|
+
return
|
|
157
|
+
}
|
|
158
|
+
render(state)
|
|
159
|
+
} catch (error) {
|
|
160
|
+
pathLine.textContent = '读取失败:' + String(error && error.message ? error.message : error)
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
up.addEventListener('click', () => {
|
|
164
|
+
if (parent === null) return
|
|
165
|
+
load(parent).catch(() => {})
|
|
166
|
+
})
|
|
167
|
+
pick.addEventListener('click', () => { if (current !== '') close(current) })
|
|
168
|
+
cancel.addEventListener('click', () => close(null))
|
|
169
|
+
load('').catch(() => {})
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
// 只在手机宽度下接管;桌面端保持 DSH 原生选择器
|
|
173
|
+
if (narrow() && globalThis.__DSH_DIRECTORY_PICKER__ === undefined) {
|
|
174
|
+
globalThis.__DSH_DIRECTORY_PICKER__ = { pick: openDirectoryPicker }
|
|
175
|
+
}
|
|
176
|
+
|
|
72
177
|
// 记住最后点过的工作区:切换工作区必然有一次点击,这是最可靠的信号。
|
|
73
178
|
// 存的只是名字,由服务端映射成路径;对不上就退回默认工作区。
|
|
74
179
|
const LAST_WS_KEY = 'dsh-homing:last-workspace'
|
|
@@ -220,13 +325,15 @@ export const MOBILE_JS = String.raw`(() => {
|
|
|
220
325
|
* 手机上没有那台电脑的屏幕,点了只会收到「已在其它设备打开」——纯噪音。
|
|
221
326
|
* 这里把它(主按钮和小箭头都算)接到文件页 /files。
|
|
222
327
|
*
|
|
223
|
-
*
|
|
224
|
-
* 它们排在分体按钮前面。「找第一个 aria-haspopup 的父容器」会认成子智能体
|
|
225
|
-
* 那个容器,于是点分体按钮时判定不命中、补丁失效(手机端历史 bug)。
|
|
226
|
-
* 所以:
|
|
328
|
+
* 定位:
|
|
227
329
|
* 1. 优先用 DSH 的稳定标记 data-open-target="directory"(分体按钮容器);
|
|
228
|
-
* 2.
|
|
229
|
-
*
|
|
330
|
+
* 2. 老版本没有标记时退回结构判断,但条件必须够严:
|
|
331
|
+
* 同容器恰好两个按钮,一个是纯图标主按钮且**里面是 <img> 应用图标**
|
|
332
|
+
* (分体按钮的真实特征),另一个是 aria-haspopup="menu" 的小箭头,
|
|
333
|
+
* 且整个容器在页头左上角。
|
|
334
|
+
* 2026-11-23 线上回归:新版 DSH 页头把「git 图标 + ⋯ 菜单」放在同一个
|
|
335
|
+
* 容器里(恰好两个按钮、后者 aria-haspopup=menu、前者纯 svg 图标),
|
|
336
|
+
* 只判断 svg 会把「⋯」劫持到文件页 —— 所以主按钮必须真是 <img> 图标。
|
|
230
337
|
*/
|
|
231
338
|
const splitHost = (node) => {
|
|
232
339
|
if (node === null || node === undefined || typeof node.closest !== 'function') return null
|
|
@@ -247,10 +354,13 @@ export const MOBILE_JS = String.raw`(() => {
|
|
|
247
354
|
if (main === null || chevron === null) return null
|
|
248
355
|
if (chevron.getAttribute('aria-haspopup') !== 'menu') return null
|
|
249
356
|
if ((main.textContent || '').trim() !== '') return null
|
|
250
|
-
|
|
357
|
+
// 分体按钮的主按钮是应用图标(<img>);git / ⋯ 这类页头按钮是 <svg>,不能混为一谈
|
|
358
|
+
if (main.querySelector('img') === null) return null
|
|
251
359
|
const rect = chevron.getBoundingClientRect()
|
|
252
360
|
if (rect.width === 0 || rect.height === 0) return null
|
|
253
|
-
if (rect.top >
|
|
361
|
+
if (rect.top > 120) return null
|
|
362
|
+
const hostRect = host.getBoundingClientRect()
|
|
363
|
+
if (hostRect.left > 160) return null
|
|
254
364
|
return host
|
|
255
365
|
}
|
|
256
366
|
|