dsh-folder-path 0.2.1 → 0.2.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/README.md +32 -58
- package/README_EN.md +9 -7
- package/cordis.patch.yml +3 -2
- package/docs/DETAIL.md +7 -4
- package/docs/DETAIL.zh.md +9 -4
- package/lib/client.js +21 -1
- package/lib/index.js +20 -7
- package/lib/types/index.d.ts +17 -0
- package/package.json +7 -4
- package/client/globals.d.ts +0 -30
- package/client/index.ts +0 -323
- package/scripts/build-client.mts +0 -46
package/README.md
CHANGED
|
@@ -1,26 +1,20 @@
|
|
|
1
1
|
# dsh-folder-path
|
|
2
2
|
|
|
3
|
-
DSH(DeepSeek Harness)Web
|
|
4
|
-
不再尝试把它当成附件上传(原版会显示「上传失败,点击重试」),而是把它的**完整路径**作为文本插进输入框。
|
|
3
|
+
DSH(DeepSeek Harness)Web 插件:在输入框里粘贴刚从资源管理器或 Finder 复制的文件夹时,不再尝试把它当成附件上传(原版会显示「上传失败,点击重试」),而是把完整路径作为文本插入输入框。
|
|
5
4
|
|
|
6
|
-
-
|
|
7
|
-
-
|
|
8
|
-
-
|
|
5
|
+
- 复制一个文件夹,按 `Ctrl+V`(macOS `Cmd+V`),输入框出现 `D:\项目\数据\2026Q1` 或 `/Users/me/项目/数据`
|
|
6
|
+
- 复制多个文件夹,每行一条
|
|
7
|
+
- 普通文本、图片、真实文件的粘贴完全不受影响(插件只在粘贴载荷里出现目录时才接管)
|
|
9
8
|
|
|
10
|
-
##
|
|
9
|
+
## 它解决什么问题
|
|
11
10
|
|
|
12
|
-
浏览器是沙盒:网页 JavaScript
|
|
13
|
-
浏览器交出的是一个 `size: 0`、`type: ''` 的 `File`——字节不可读,因此 DSH 原版的上传链路必然失败;
|
|
14
|
-
而路径也根本不在粘贴载荷里(`File.name` 只有文件夹名)。
|
|
11
|
+
浏览器是沙盒:网页 JavaScript 拿不到本地文件的磁盘路径。把文件夹粘进网页时,浏览器交出的是一个 `size: 0`、`type: ''` 的 `File`,字节不可读,因此 DSH 原版的上传链路必然失败;路径也不在粘贴载荷里(`File.name` 只有文件夹名)。
|
|
15
12
|
|
|
16
|
-
|
|
17
|
-
所以本插件由**宿主半边**去读系统剪贴板的文件列表——Windows 走 CF_HDROP,macOS 走 NSPasteboard 的文件 URL——
|
|
18
|
-
再把真实全路径交回浏览器半边。
|
|
13
|
+
剪贴板是进程级的:运行宿主的那台机器上,剪贴板里此刻装的正是刚复制的那份文件列表。本插件由宿主半边去读系统剪贴板的文件列表(Windows 走 CF_HDROP,macOS 走 NSPasteboard 的文件 URL),再把真实全路径交回浏览器半边。
|
|
19
14
|
|
|
20
15
|
## 平台要求
|
|
21
16
|
|
|
22
|
-
Windows(剪贴板经 PowerShell 5.1 的 `System.Windows.Forms` 读取)或 macOS(系统自带 `osascript
|
|
23
|
-
其他平台宿主返回 `unsupported-platform`,粘贴行为退回原版。
|
|
17
|
+
Windows(剪贴板经 PowerShell 5.1 的 `System.Windows.Forms` 读取)或 macOS(系统自带 `osascript`)。其他平台宿主返回 `unsupported-platform`,客户端提示,路径不插入。
|
|
24
18
|
|
|
25
19
|
## 安装
|
|
26
20
|
|
|
@@ -37,8 +31,7 @@ git clone https://github.com/CooperZhuang/dsh-plugins-cooper-custom.git
|
|
|
37
31
|
dsh plugin --profile <profile> add ./dsh-plugins-cooper-custom/packages/dsh-folder-path
|
|
38
32
|
```
|
|
39
33
|
|
|
40
|
-
安装后刷新页面即可。Desktop Beta 2.0.18-beta.1(内核 0.2.1-rc.2)起新增的 bundle
|
|
41
|
-
宿主不必重启;**更早的版本必须重启 `dsh web`**,不确定时重启一次总是安全的。
|
|
34
|
+
安装后刷新页面即可。Desktop Beta 2.0.18-beta.1(内核 0.2.1-rc.2)起新增的 bundle 行通常能热挂载,宿主不必重启;更早的版本必须重启 `dsh web`,不确定时重启一次总是安全的。
|
|
42
35
|
|
|
43
36
|
验证是否挂上:`dsh --profile <profile> --dump-config` 里应出现 `# == dsh-folder-path` 层。
|
|
44
37
|
|
|
@@ -52,10 +45,7 @@ dsh plugin --profile <profile> remove dsh-folder-path
|
|
|
52
45
|
|
|
53
46
|
## 兼容性
|
|
54
47
|
|
|
55
|
-
本包声明的兼容区间是 `engines.dsh: ^0.2.1-rc.2 || ^0.2.2-alpha.2`(Node.js `>=24
|
|
56
|
-
已在 **DSH 0.2.1-rc.2**(npm `@deepseek-ai/dsh` 0.2.1-rc.2)上逐项核对过安装、组合、宿主路由与浏览器半边;
|
|
57
|
-
内核接口一旦变化,最坏结果是插件不再接管粘贴、行为退回原版,而不会破坏原有的粘贴/上传链路。
|
|
58
|
-
本插件只用到内核的这几处:
|
|
48
|
+
本包声明的兼容区间是 `engines.dsh: ^0.2.1-rc.2 || ^0.2.2-alpha.2`(Node.js `>=24`)。已在 DSH 0.2.1-rc.2(npm `@deepseek-ai/dsh` 0.2.1-rc.2)上逐项核对过安装、组合、宿主路由与浏览器半边;内核接口一旦变化,最坏结果是插件不再接管粘贴、行为退回原版,而不会破坏原有的粘贴与上传链路。本插件只用到内核的这几处:
|
|
59
49
|
|
|
60
50
|
| 内核接口 | 用途 |
|
|
61
51
|
|---|---|
|
|
@@ -65,42 +55,35 @@ dsh plugin --profile <profile> remove dsh-folder-path
|
|
|
65
55
|
| `package.json` 的 `dsh.bundle.patch` 与 `dsh.client.platform` | bundle 补丁层与浏览器半的模块登记 |
|
|
66
56
|
| `exports['./client']` + `window.__ModuleLoader__.load({ id, factory })` | 浏览器半的加载方式 |
|
|
67
57
|
|
|
68
|
-
|
|
69
|
-
不改变它的任何行为。`test/manifest.test.js` 与 `test/client.test.js` 把上面这份清单钉在测试里。
|
|
58
|
+
插件不涉及子代理,所以内核 0.2.1-rc.2 的子代理约定(`startActivation({provider, request})`、Agent Preset)不改变它的任何行为。`test/manifest.test.ts` 与 `test/client.test.ts` 把上面这份清单钉在测试里。
|
|
70
59
|
|
|
71
60
|
## 使用
|
|
72
61
|
|
|
73
|
-
1. 在资源管理器(Windows)或 Finder(macOS
|
|
74
|
-
2. 回到 DSH Web
|
|
75
|
-
3.
|
|
62
|
+
1. 在资源管理器(Windows)或 Finder(macOS)里选中一个或多个文件夹,按 `Ctrl+C`(macOS `Cmd+C`)
|
|
63
|
+
2. 回到 DSH Web 输入框,按 `Ctrl+V`(macOS `Cmd+V`)
|
|
64
|
+
3. 路径作为文本出现在草稿里;解析失败时输入框上方出现一条提示,草稿不动
|
|
76
65
|
|
|
77
66
|
## 工作原理
|
|
78
67
|
|
|
79
68
|
| 半边 | 位置 | 职责 |
|
|
80
69
|
|---|---|---|
|
|
81
70
|
| Host 半 `lib/index.js` | 宿主进程(`webServer` 服务) | 精确路由 `POST /dsh-folder-path/api/v1/clipboard-paths`:读系统剪贴板的文件列表(Windows CF_HDROP / macOS NSPasteboard)、按浏览器报来的文件夹名匹配、返回真实全路径 |
|
|
82
|
-
| Client 半 `lib/client.js` | 浏览器(`__ModuleLoader__` bundle) | document 捕获阶段拦截 `paste
|
|
71
|
+
| Client 半 `lib/client.js` | 浏览器(`__ModuleLoader__` bundle) | document 捕获阶段拦截 `paste`,只有载荷里含目录时才接管;向宿主取路径,再把文本按 DSH 自己的粘贴通路插进 composer |
|
|
83
72
|
|
|
84
|
-
插入不是直接改 DOM:客户端补发一个只带 `text/plain` 的合成 `paste`
|
|
85
|
-
走的是 DSH 编辑器**原生的文本粘贴命令**(草稿状态、命令菜单、撤销历史、已有 chip 都不受影响);
|
|
86
|
-
引擎不允许构造带数据的 `ClipboardEvent` 时,退化为 `document.execCommand('insertText')`。
|
|
73
|
+
插入不是直接改 DOM:客户端补发一个只带 `text/plain` 的合成 `paste` 事件,走的是 DSH 编辑器原生的文本粘贴命令(草稿状态、命令菜单、撤销历史、已有 chip 都不受影响);引擎不允许构造带数据的 `ClipboardEvent` 时,退化为 `document.execCommand('insertText')`。
|
|
87
74
|
|
|
88
|
-
宿主按平台读剪贴板:Windows 用 PowerShell(`System.Windows.Forms.Clipboard`,输出
|
|
89
|
-
中文/空格路径不会被重定向进程的代码页破坏);macOS 用 `osascript -l JavaScript` 读 NSPasteboard 的
|
|
90
|
-
`public.file-url` 条目,并合并 Finder 的旧格式 `NSFilenamesPboardType` 列表,避免多选文件夹被折叠成一条。
|
|
91
|
-
目录还是文件由宿主进程自己 `stat` 判断。同一时刻的多次粘贴会合并成一次探测。
|
|
75
|
+
宿主按平台读剪贴板:Windows 用 PowerShell(`System.Windows.Forms.Clipboard`,输出 base64(UTF-8 JSON),中文与空格路径不会被重定向进程的代码页破坏);macOS 用 `osascript -l JavaScript` 读 NSPasteboard 的 `public.file-url` 条目,并合并 Finder 的旧格式 `NSFilenamesPboardType` 列表,避免多选文件夹被折叠成一条。目录还是文件由宿主侧判断(macOS 用 `stat`,Windows 由 PowerShell 脚本判断)。同一时刻的多次粘贴会合并成一次探测。
|
|
92
76
|
|
|
93
77
|
## 配置
|
|
94
78
|
|
|
95
|
-
插件的配置写在 profile 的补丁层里(`~/.dsh/profiles/web/cordis.patch.yml` 或本包自带的
|
|
96
|
-
`cordis.patch.yml` 会被 `dsh plugin add` 接入),全部可选:
|
|
79
|
+
插件的配置写在 profile 的补丁层里(`~/.dsh/profiles/web/cordis.patch.yml` 或本包自带的 `cordis.patch.yml` 会被 `dsh plugin add` 接入),全部可选:
|
|
97
80
|
|
|
98
81
|
```yaml
|
|
99
82
|
- insert:
|
|
100
83
|
- id: folder-path
|
|
101
84
|
name: dsh-folder-path
|
|
102
85
|
config:
|
|
103
|
-
enabled: true # false =
|
|
86
|
+
enabled: true # false = 宿主半边空转:路由不注册,粘贴文件夹时客户端只提示、不插入
|
|
104
87
|
timeoutMs: 5000 # 单次剪贴板探测预算
|
|
105
88
|
powershell: powershell.exe # 也可用环境变量 DSH_FOLDER_PATH_POWERSHELL 覆盖
|
|
106
89
|
osascript: /usr/bin/osascript # 也可用环境变量 DSH_FOLDER_PATH_OSASCRIPT 覆盖
|
|
@@ -109,21 +92,19 @@ dsh plugin --profile <profile> remove dsh-folder-path
|
|
|
109
92
|
|
|
110
93
|
## 安全边界
|
|
111
94
|
|
|
112
|
-
-
|
|
113
|
-
-
|
|
114
|
-
-
|
|
115
|
-
-
|
|
116
|
-
-
|
|
95
|
+
- 只读:路由只读剪贴板,从不打开、复制或上传文件
|
|
96
|
+
- 只接受环回:默认拒绝非环回对端(`remote-not-supported`),否则远程浏览器会被喂宿主机的路径
|
|
97
|
+
- 同源:带 `Origin` 的请求必须与 `Host` 一致,否则 403
|
|
98
|
+
- 不猜路径:宿主报回的文件夹名必须与剪贴板条目对上;对不上就返回 `no-match`,绝不返回一个「看起来像」的路径
|
|
99
|
+
- 不缓存:响应 `cache-control: no-store`
|
|
117
100
|
|
|
118
101
|
## 已知限制
|
|
119
102
|
|
|
120
|
-
-
|
|
121
|
-
-
|
|
122
|
-
-
|
|
123
|
-
默认直接拒绝;`allowNonLoopback: true` 会读宿主机的剪贴板(只有"浏览器和宿主在同一台机器、但用局域网 IP 打开"时才该打开它)。
|
|
103
|
+
- 只支持 Windows 与 macOS。其它平台宿主返回 `unsupported-platform`,客户端给出提示,路径不插入。
|
|
104
|
+
- 只处理粘贴,不处理拖放。浏览器拖放载荷里同样没有磁盘路径,本插件不猜;把文件夹拖进窗口仍是原版行为。
|
|
105
|
+
- 远程访问时读不到你的剪贴板。通过局域网 IP、端口转发或跳板机访问 `dsh web` 时,剪贴板属于运行宿主的那台机器。默认直接拒绝;`allowNonLoopback: true` 会读宿主机的剪贴板(只有「浏览器和宿主在同一台机器、但用局域网 IP 打开」时才该打开它)。
|
|
124
106
|
- Windows 依赖 PowerShell 5.1 的 `System.Windows.Forms`;被安全策略挡住时返回 `clipboard-error` 并提示。
|
|
125
|
-
- macOS 依赖系统自带的 `osascript`(读粘贴板不需要 TCC
|
|
126
|
-
只认标准文件复制格式(`public.file-url` / `NSFilenamesPboardType`),自造私有剪贴板格式的 App 读不到。
|
|
107
|
+
- macOS 依赖系统自带的 `osascript`(读粘贴板不需要 TCC 授权,也不会弹「允许访问剪贴板」);只认标准文件复制格式(`public.file-url` / `NSFilenamesPboardType`),自造私有剪贴板格式的 App 读不到。
|
|
127
108
|
|
|
128
109
|
## 开发
|
|
129
110
|
|
|
@@ -132,8 +113,7 @@ npm run build # tsc: src/index.ts -> lib/index.js,client/index.ts -> .client
|
|
|
132
113
|
npm test # 先 build,再跑宿主路由 / 浏览器半契约 / 清单契约测试(node --test,无需 DSH 服务器)
|
|
133
114
|
```
|
|
134
115
|
|
|
135
|
-
宿主半边源码在 `src/index.ts`(TypeScript,`lib/index.js` 与 `lib/types/` 是 tsc
|
|
136
|
-
浏览器半边是 `client/index.ts`(`.client-build/index.js` 是 tsc 产物),由构建脚本包成 `lib/client.js`。
|
|
116
|
+
宿主半边源码在 `src/index.ts`(TypeScript,`lib/index.js` 与 `lib/types/` 是 tsc 产物,随仓库提交);浏览器半边是 `client/index.ts`(`.client-build/index.js` 是 tsc 产物),由构建脚本包成 `lib/client.js`。
|
|
137
117
|
|
|
138
118
|
浏览器侧调试钩子(devtools 控制台):
|
|
139
119
|
|
|
@@ -142,19 +122,13 @@ await __dshFolderPath.probe(['文件夹名']) // 直接问宿主要路径
|
|
|
142
122
|
__dshFolderPath.insert(document.querySelector('[contenteditable]'), '文本') // 直接试插入
|
|
143
123
|
```
|
|
144
124
|
|
|
145
|
-
|
|
146
|
-
`dsh-client-hmr` 每 500 ms 轮询各客户端产物,重建后重算 revision 并推给页面。
|
|
147
|
-
只有**新增**客户端模块才需要重启宿主(启动时的扫描决定「有哪些客户端模块」)。
|
|
125
|
+
改浏览器半边后:`npm run build`,然后刷新页面即可,不必重启宿主——`dsh-client-hmr` 每 500 ms 轮询各客户端产物,重建后重算 revision 并推给页面。只有新增客户端模块才需要重启宿主(启动时的扫描决定「有哪些客户端模块」)。
|
|
148
126
|
|
|
149
|
-
|
|
150
|
-
Desktop 版要把应用**完全退出**再打开——关窗口不算退出。
|
|
127
|
+
改宿主半边后:必须重启宿主进程(宿主走 Node 的 ESM 缓存,运行中的那份代码不会跟着变)。Desktop 版要把应用完全退出再打开,关窗口不算退出。
|
|
151
128
|
|
|
152
129
|
## 致谢
|
|
153
130
|
|
|
154
|
-
架构参考了同类的 macOS 插件 [jhuanxx44/dsh-paste-path](https://github.com/jhuanxx44/dsh-paste-path)
|
|
155
|
-
(Finder 复制 → 宿主读粘贴板 → Ctrl+V 插路径):环回/同源校验、宿主侧读取系统剪贴板、
|
|
156
|
-
"宿主属于运行 dsh 的那台机器"这条边界都沿用它的判断;本插件把同一思路落到 Windows 剪贴板,
|
|
157
|
-
并把触发方式改为**粘贴事件驱动的目录识别**(Windows 上无粘贴板变更计数可供廉价轮询)。
|
|
131
|
+
架构参考了同类的 macOS 插件 [jhuanxx44/dsh-paste-path](https://github.com/jhuanxx44/dsh-paste-path)(Finder 复制 → 宿主读粘贴板 → Ctrl+V 插路径):环回与同源校验、宿主侧读取系统剪贴板、「宿主属于运行 dsh 的那台机器」这条边界都沿用它的判断;本插件把同一思路落到 Windows 剪贴板,并把触发方式改为粘贴事件驱动的目录识别(Windows 上无粘贴板变更计数可供廉价轮询)。
|
|
158
132
|
|
|
159
133
|
## License
|
|
160
134
|
|
package/README_EN.md
CHANGED
|
@@ -22,7 +22,8 @@ Windows, NSPasteboard file URLs on macOS — and hands the real absolute paths b
|
|
|
22
22
|
## Requirements
|
|
23
23
|
|
|
24
24
|
Windows (the clipboard is read through PowerShell 5.1 `System.Windows.Forms`) or macOS (system
|
|
25
|
-
`osascript`). Other platforms answer `unsupported-platform
|
|
25
|
+
`osascript`). Other platforms answer `unsupported-platform`; the client shows a notice and inserts no
|
|
26
|
+
path.
|
|
26
27
|
|
|
27
28
|
## Install
|
|
28
29
|
|
|
@@ -70,8 +71,8 @@ paste/upload path is never broken. The plugin touches exactly this much of the k
|
|
|
70
71
|
| `exports['./client']` + `window.__ModuleLoader__.load({ id, factory })` | how the browser half is loaded |
|
|
71
72
|
|
|
72
73
|
The plugin involves **no subagents**, so the kernel 0.2.1-rc.2 subagent convention
|
|
73
|
-
(`startActivation({provider, request})`, Agent Presets) changes nothing for it. `test/manifest.test.
|
|
74
|
-
and `test/client.test.
|
|
74
|
+
(`startActivation({provider, request})`, Agent Presets) changes nothing for it. `test/manifest.test.ts`
|
|
75
|
+
and `test/client.test.ts` pin the list above in the test suite.
|
|
75
76
|
|
|
76
77
|
## Use
|
|
77
78
|
|
|
@@ -96,8 +97,8 @@ The host reads the clipboard per platform. Windows uses PowerShell
|
|
|
96
97
|
(`System.Windows.Forms.Clipboard`) and emits **base64(UTF-8 JSON)**, so paths with Chinese characters
|
|
97
98
|
or spaces survive the redirected process's code page. macOS uses `osascript -l JavaScript` to read the
|
|
98
99
|
NSPasteboard's `public.file-url` entries and merges Finder's legacy `NSFilenamesPboardType` list, so a
|
|
99
|
-
multi-selection cannot collapse into one item; directory versus file is decided
|
|
100
|
-
`stat
|
|
100
|
+
multi-selection cannot collapse into one item; directory versus file is decided on the host side
|
|
101
|
+
(`stat` on macOS, `Directory::Exists` in the PowerShell probe). Concurrent pastes share one probe.
|
|
101
102
|
|
|
102
103
|
## Configuration
|
|
103
104
|
|
|
@@ -109,7 +110,7 @@ or this package's own `cordis.patch.yml`, which `dsh plugin add` wires in):
|
|
|
109
110
|
- id: folder-path
|
|
110
111
|
name: dsh-folder-path
|
|
111
112
|
config:
|
|
112
|
-
enabled: true # false mounts the host half inert
|
|
113
|
+
enabled: true # false mounts the host half inert: no route is registered, so a folder paste only shows a notice
|
|
113
114
|
timeoutMs: 5000 # per-probe budget
|
|
114
115
|
powershell: powershell.exe # also settable via DSH_FOLDER_PATH_POWERSHELL
|
|
115
116
|
osascript: /usr/bin/osascript # also settable via DSH_FOLDER_PATH_OSASCRIPT
|
|
@@ -145,7 +146,8 @@ or this package's own `cordis.patch.yml`, which `dsh plugin add` wires in):
|
|
|
145
146
|
## Development
|
|
146
147
|
|
|
147
148
|
```sh
|
|
148
|
-
npm run build #
|
|
149
|
+
npm run build # host tsc (src/index.ts -> lib/index.js, declarations in lib/types) and client tsc
|
|
150
|
+
# (client/index.ts -> .client-build/index.js); then scripts/build-client.mts writes lib/client.js
|
|
149
151
|
npm test # builds both halves, then host route / browser-half contract / manifest contract tests
|
|
150
152
|
# (node --test, no DSH server needed)
|
|
151
153
|
```
|
package/cordis.patch.yml
CHANGED
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
# the browser half (`dsh.client.platform: web` in package.json) is served as a
|
|
7
7
|
# client bundle the module system loads at boot.
|
|
8
8
|
#
|
|
9
|
-
# Every knob is optional. `enabled: false` mounts the host half inert
|
|
10
|
-
#
|
|
9
|
+
# Every knob is optional. `enabled: false` mounts the host half inert: the
|
|
10
|
+
# clipboard route is not registered, so a folder paste only shows a notice and
|
|
11
|
+
# inserts no path (the browser half still intercepts the paste).
|
|
11
12
|
- insert:
|
|
12
13
|
- id: folder-path
|
|
13
14
|
name: dsh-folder-path
|
package/docs/DETAIL.md
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
# Paste folder path
|
|
2
2
|
|
|
3
|
-
Pasting a folder into the composer normally tries to upload it and fails. This plugin reads the folder path from the system clipboard and inserts it instead.
|
|
3
|
+
Pasting a folder into the composer normally tries to upload it and fails. This plugin reads the folder path from the system clipboard and inserts it as text instead.
|
|
4
4
|
|
|
5
5
|
## Behavior
|
|
6
|
-
- Windows and macOS only; other platforms
|
|
7
|
-
- The client half intercepts paste
|
|
6
|
+
- Windows and macOS only; other platforms answer `unsupported-platform`.
|
|
7
|
+
- The client half intercepts paste in the capture phase and only takes over when the payload contains a directory.
|
|
8
|
+
- The host half reads the system clipboard (CF_HDROP on Windows, NSPasteboard on macOS) and matches the folder names the browser reported before answering with the paths.
|
|
8
9
|
|
|
9
10
|
## Notes
|
|
10
|
-
-
|
|
11
|
+
- Loopback only by default; a non-loopback peer gets `remote-not-supported`. A request carrying `Origin` must match `Host`, else 403.
|
|
12
|
+
- Other platforms answer `unsupported-platform`, a failed clipboard read answers `clipboard-error`, and unmatched names answer `no-match`. In each case the client shows a notice and inserts no path.
|
|
13
|
+
- Responses carry `cache-control: no-store`; the route only reads the clipboard and never opens, copies or uploads files.
|
package/docs/DETAIL.zh.md
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
# 粘贴文件夹路径
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
浏览器里往输入框粘贴文件夹时,默认行为是尝试上传;文件夹无法上传,只会失败。本插件改为读取系统剪贴板里的文件夹路径,把路径作为文本插入输入框。
|
|
4
4
|
|
|
5
5
|
## 行为
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
|
|
7
|
+
- 仅 Windows 与 macOS 生效;其它平台返回 `unsupported-platform`。
|
|
8
|
+
- 浏览器半边在捕获阶段拦截粘贴事件,只在载荷含目录时接管。
|
|
9
|
+
- 宿主半边读系统剪贴板拿路径(Windows 走 CF_HDROP,macOS 走 NSPasteboard),按浏览器报来的文件夹名匹配后返回。
|
|
8
10
|
|
|
9
11
|
## 注意
|
|
10
|
-
|
|
12
|
+
|
|
13
|
+
- 默认只接受环回请求;非环回对端返回 `remote-not-supported`。带 `Origin` 的请求必须与 `Host` 一致,否则 403。
|
|
14
|
+
- 其它平台返回 `unsupported-platform`,读剪贴板失败返回 `clipboard-error`,文件夹名对不上返回 `no-match`;这几种情况下客户端提示,路径不插入。
|
|
15
|
+
- 响应 `cache-control: no-store`;路由只读剪贴板,不打开、复制或上传文件。
|
package/lib/client.js
CHANGED
|
@@ -250,6 +250,8 @@ window.__ModuleLoader__.load({
|
|
|
250
250
|
* @param files - the non-directory files from the same paste.
|
|
251
251
|
*/
|
|
252
252
|
async function resolveAndInsert(root, directories, files) {
|
|
253
|
+
var hrefBefore = typeof location === 'object' && location !== null ? location.href : '';
|
|
254
|
+
var activeBefore = typeof document.activeElement !== 'undefined' ? document.activeElement : null;
|
|
253
255
|
var result;
|
|
254
256
|
try {
|
|
255
257
|
result = await requestPaths(directories);
|
|
@@ -258,14 +260,25 @@ window.__ModuleLoader__.load({
|
|
|
258
260
|
result = { paths: [], reason: 'host-unreachable' };
|
|
259
261
|
}
|
|
260
262
|
if (result.paths.length === 0) {
|
|
261
|
-
|
|
263
|
+
if (result.reason !== 'http-404')
|
|
264
|
+
toast(failureText(result.reason));
|
|
262
265
|
forwardFiles(root, files);
|
|
263
266
|
return;
|
|
264
267
|
}
|
|
268
|
+
var hrefNow = typeof location === 'object' && location !== null ? location.href : '';
|
|
269
|
+
if (hrefNow !== hrefBefore) {
|
|
270
|
+
toast('页面已切换,路径没有插入:' + result.paths.join('、'));
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
265
273
|
if (!root.isConnected) {
|
|
266
274
|
toast('输入框已经关闭,路径没有插入:' + result.paths.join('、'));
|
|
267
275
|
return;
|
|
268
276
|
}
|
|
277
|
+
var activeNow = typeof document.activeElement !== 'undefined' ? document.activeElement : null;
|
|
278
|
+
if (activeBefore !== null && activeNow !== null && activeNow !== activeBefore && !root.contains(activeNow)) {
|
|
279
|
+
toast('焦点已离开输入框,路径没有插入:' + result.paths.join('、'));
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
269
282
|
insertText(root, result.paths.join('\n'));
|
|
270
283
|
forwardFiles(root, files);
|
|
271
284
|
}
|
|
@@ -307,6 +320,13 @@ window.__ModuleLoader__.load({
|
|
|
307
320
|
insert: insertText,
|
|
308
321
|
classify: classifyPayload,
|
|
309
322
|
};
|
|
323
|
+
ctx.effect(function () {
|
|
324
|
+
return function () {
|
|
325
|
+
if (typeof window !== 'undefined' && '__dshFolderPath' in window) {
|
|
326
|
+
delete window.__dshFolderPath;
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
}, 'dsh-folder-path: debug hook');
|
|
310
330
|
}
|
|
311
331
|
return { apply: apply };
|
|
312
332
|
}
|
package/lib/index.js
CHANGED
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
import { execFile } from 'node:child_process';
|
|
32
32
|
import { stat } from 'node:fs/promises';
|
|
33
33
|
import { basename } from 'node:path';
|
|
34
|
+
import z from '@deepseek-ai/schemastery';
|
|
34
35
|
/** Cordis plugin name; the profile row's `id` is independent of it. */
|
|
35
36
|
export const name = 'folder-path';
|
|
36
37
|
/**
|
|
@@ -45,6 +46,14 @@ const DEFAULT_TIMEOUT_MS = 5000;
|
|
|
45
46
|
const MAX_REQUEST_BYTES = 64 * 1024;
|
|
46
47
|
/** Hosts whose clipboard can answer this route. */
|
|
47
48
|
const DESKTOP_PLATFORMS = new Set(['win32', 'darwin']);
|
|
49
|
+
/** The host's config schema, exposed so Settings -> Plugins can inspect defaults. */
|
|
50
|
+
export const Config = z.object({
|
|
51
|
+
enabled: z.boolean().default(true),
|
|
52
|
+
timeoutMs: z.number().default(DEFAULT_TIMEOUT_MS),
|
|
53
|
+
powershell: z.string(),
|
|
54
|
+
osascript: z.string(),
|
|
55
|
+
allowNonLoopback: z.boolean().default(false),
|
|
56
|
+
});
|
|
48
57
|
/**
|
|
49
58
|
* Windows PowerShell probe. Emits base64(UTF-8 JSON) on stdout so a Chinese path
|
|
50
59
|
* can never be mangled by the console code page of a redirected Windows
|
|
@@ -61,7 +70,8 @@ const WINDOWS_PROBE_SCRIPT = [
|
|
|
61
70
|
' $full = [string]$raw',
|
|
62
71
|
' if ([string]::IsNullOrWhiteSpace($full)) { continue }',
|
|
63
72
|
' $trimmed = $full.TrimEnd([char]92, [char]47)',
|
|
64
|
-
|
|
73
|
+
" $candidate = if ($trimmed.Length -gt 0) { [System.IO.Path]::GetFileName($trimmed) } else { '' }",
|
|
74
|
+
' $name = if ([string]::IsNullOrEmpty($candidate)) { $full } else { $candidate }',
|
|
65
75
|
' $items += [pscustomobject]@{',
|
|
66
76
|
' path = $full',
|
|
67
77
|
' name = $name',
|
|
@@ -124,11 +134,16 @@ const DARWIN_PROBE_SCRIPT = [
|
|
|
124
134
|
* @param config - the profile row's `config` block.
|
|
125
135
|
* @returns nothing; the route is registered once `webServer` appears.
|
|
126
136
|
*/
|
|
137
|
+
/** Normalize the clipboard probe timeout, rejecting non-finite or non-positive values. */
|
|
138
|
+
export function resolveTimeoutMs(value) {
|
|
139
|
+
const numeric = Number(value);
|
|
140
|
+
return Number.isFinite(numeric) && numeric > 0 ? numeric : DEFAULT_TIMEOUT_MS;
|
|
141
|
+
}
|
|
127
142
|
export function apply(ctx, config = {}) {
|
|
128
143
|
if (config?.enabled === false)
|
|
129
144
|
return;
|
|
130
145
|
const probe = {
|
|
131
|
-
timeoutMs:
|
|
146
|
+
timeoutMs: resolveTimeoutMs(config?.timeoutMs),
|
|
132
147
|
powershell: firstString(config?.powershell) || firstString(process.env.DSH_FOLDER_PATH_POWERSHELL) || 'powershell.exe',
|
|
133
148
|
osascript: firstString(config?.osascript) || firstString(process.env.DSH_FOLDER_PATH_OSASCRIPT) || '/usr/bin/osascript',
|
|
134
149
|
};
|
|
@@ -319,8 +334,7 @@ async function probeWindowsClipboard(probe) {
|
|
|
319
334
|
async function probeMacClipboard(probe) {
|
|
320
335
|
const stdout = await runProbe(probe.osascript, ['-l', 'JavaScript', '-e', DARWIN_PROBE_SCRIPT], probe.timeoutMs);
|
|
321
336
|
const paths = parseProbePaths(stdout);
|
|
322
|
-
|
|
323
|
-
for (const path of paths) {
|
|
337
|
+
return Promise.all(paths.map(async (path) => {
|
|
324
338
|
let isDirectory = false;
|
|
325
339
|
try {
|
|
326
340
|
isDirectory = (await stat(path)).isDirectory();
|
|
@@ -328,9 +342,8 @@ async function probeMacClipboard(probe) {
|
|
|
328
342
|
catch {
|
|
329
343
|
isDirectory = false;
|
|
330
344
|
}
|
|
331
|
-
|
|
332
|
-
}
|
|
333
|
-
return entries;
|
|
345
|
+
return { path, name: basename(path), isDirectory };
|
|
346
|
+
}));
|
|
334
347
|
}
|
|
335
348
|
/**
|
|
336
349
|
* Read a JSON request body.
|
package/lib/types/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import z from '@deepseek-ai/schemastery';
|
|
1
2
|
/** Cordis plugin name; the profile row's `id` is independent of it. */
|
|
2
3
|
export declare const name = "folder-path";
|
|
3
4
|
/**
|
|
@@ -17,6 +18,20 @@ export interface ClipboardEntry {
|
|
|
17
18
|
name: string;
|
|
18
19
|
isDirectory: boolean;
|
|
19
20
|
}
|
|
21
|
+
/** The host's config schema, exposed so Settings -> Plugins can inspect defaults. */
|
|
22
|
+
export declare const Config: z<Schemastery.ObjectS<NoInfer<{
|
|
23
|
+
enabled: z<boolean, boolean, "defined">;
|
|
24
|
+
timeoutMs: z<number, number, "defined">;
|
|
25
|
+
powershell: z<string, string, "plain">;
|
|
26
|
+
osascript: z<string, string, "plain">;
|
|
27
|
+
allowNonLoopback: z<boolean, boolean, "defined">;
|
|
28
|
+
}>>, Schemastery.ObjectT<NoInfer<{
|
|
29
|
+
enabled: z<boolean, boolean, "defined">;
|
|
30
|
+
timeoutMs: z<number, number, "defined">;
|
|
31
|
+
powershell: z<string, string, "plain">;
|
|
32
|
+
osascript: z<string, string, "plain">;
|
|
33
|
+
allowNonLoopback: z<boolean, boolean, "defined">;
|
|
34
|
+
}>>, "plain">;
|
|
20
35
|
/**
|
|
21
36
|
* The part of the profile row's `config` block this plugin reads.
|
|
22
37
|
*/
|
|
@@ -84,6 +99,8 @@ export interface HostFiber {
|
|
|
84
99
|
* @param config - the profile row's `config` block.
|
|
85
100
|
* @returns nothing; the route is registered once `webServer` appears.
|
|
86
101
|
*/
|
|
102
|
+
/** Normalize the clipboard probe timeout, rejecting non-finite or non-positive values. */
|
|
103
|
+
export declare function resolveTimeoutMs(value: unknown): number;
|
|
87
104
|
export declare function apply(ctx: HostContext, config?: FolderPathConfig): void;
|
|
88
105
|
/**
|
|
89
106
|
* Filter clipboard directories down to the ones the browser is pasting.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-folder-path",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.2",
|
|
4
4
|
"description": "DSH 插件:粘贴(复制来的)文件夹时不再尝试上传,而是把它的完整路径作为文本插入输入框(Windows / macOS)。Paste a copied folder and get its full path as text instead of a failed upload (Windows and macOS).",
|
|
5
5
|
"private": false,
|
|
6
6
|
"publishConfig": {
|
|
@@ -16,14 +16,14 @@
|
|
|
16
16
|
"./client": {
|
|
17
17
|
"default": "./lib/client.js"
|
|
18
18
|
},
|
|
19
|
+
"./locale/*.json": "./locale/*.json",
|
|
20
|
+
"./icon": "./icon.svg",
|
|
19
21
|
"./package.json": "./package.json"
|
|
20
22
|
},
|
|
21
23
|
"files": [
|
|
22
24
|
"lib/index.js",
|
|
23
25
|
"lib/types",
|
|
24
26
|
"lib/client.js",
|
|
25
|
-
"client",
|
|
26
|
-
"scripts",
|
|
27
27
|
"cordis.patch.yml",
|
|
28
28
|
"README.md",
|
|
29
29
|
"README_EN.md",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
},
|
|
41
41
|
"engines": {
|
|
42
42
|
"node": ">=24",
|
|
43
|
-
"dsh": "^0.2.1-rc.2 || ^0.2.2-alpha.2"
|
|
43
|
+
"dsh": "^0.2.1-rc.2 || ^0.2.2-alpha.2 || ^0.2.3-alpha.1"
|
|
44
44
|
},
|
|
45
45
|
"keywords": [
|
|
46
46
|
"dsh",
|
|
@@ -75,6 +75,9 @@
|
|
|
75
75
|
"platform": "web"
|
|
76
76
|
}
|
|
77
77
|
},
|
|
78
|
+
"peerDependencies": {
|
|
79
|
+
"@deepseek-ai/schemastery": "^3.18.5 || ^3.18.6-alpha.1"
|
|
80
|
+
},
|
|
78
81
|
"devDependencies": {
|
|
79
82
|
"@types/node": "^26.6.5",
|
|
80
83
|
"typescript": "^7.0.2"
|
package/client/globals.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Ambient declarations for the browser half.
|
|
3
|
-
*
|
|
4
|
-
* `client/index.ts` is a classic script (it has no imports or exports), so the
|
|
5
|
-
* bundle wrapper in `scripts/build-client.mts` can embed it inside a
|
|
6
|
-
* `window.__ModuleLoader__.load({ id, factory })` factory unchanged. The DOM
|
|
7
|
-
* types come from the `DOM` lib in `tsconfig.client.json`; only the two
|
|
8
|
-
* page globals this plugin defines or consumes are declared here.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
/** The troubleshooting hook the browser half installs on `window`. */
|
|
12
|
-
interface FolderPathDebugHook {
|
|
13
|
-
/** The host route the plugin calls. */
|
|
14
|
-
route: string
|
|
15
|
-
/** Ask the host for the full paths behind these folder names. */
|
|
16
|
-
probe(names: string[]): Promise<{ paths: string[]; reason: string | null }>
|
|
17
|
-
/** Insert text through DSH's own paste path. */
|
|
18
|
-
insert(root: HTMLElement, text: string): void
|
|
19
|
-
/** Split a paste payload into directories and files. */
|
|
20
|
-
classify(dataTransfer: DataTransfer): { directories: string[]; files: File[] }
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
interface Window {
|
|
24
|
-
/** The DSH client module system; the bundle wrapper registers through it. */
|
|
25
|
-
__ModuleLoader__: {
|
|
26
|
-
load(registration: { id: string; factory: (require: (id: string) => unknown) => unknown }): void
|
|
27
|
-
}
|
|
28
|
-
/** Installed by `apply` so the devtools console can drive the plugin. */
|
|
29
|
-
__dshFolderPath?: FolderPathDebugHook
|
|
30
|
-
}
|
package/client/index.ts
DELETED
|
@@ -1,323 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `dsh-folder-path` browser half.
|
|
3
|
-
*
|
|
4
|
-
* Shipped as the package's `./client` bundle: the DSH client module system
|
|
5
|
-
* loads it as a `window.__ModuleLoader__.load({ id, factory })` script, calls
|
|
6
|
-
* `factory(require)`, and mounts the exported plugin (`apply`) into the browser
|
|
7
|
-
* Cordis tree.
|
|
8
|
-
*
|
|
9
|
-
* What it does — one thing, in the capture phase, before the conversation
|
|
10
|
-
* plugin's Lexical paste command can turn the folder into a doomed upload:
|
|
11
|
-
*
|
|
12
|
-
* * a paste is inspected for entries that are *directories*
|
|
13
|
-
* (`webkitGetAsEntry().isDirectory`; the paste payload is a `File` of size 0
|
|
14
|
-
* with no bytes to upload, which is why 上传失败 appears today);
|
|
15
|
-
* * the pasted folder names go to the host half, which reads the host
|
|
16
|
-
* machine's clipboard (Windows file-drop list, macOS NSPasteboard file
|
|
17
|
-
* URLs) and answers with the real full paths;
|
|
18
|
-
* * those paths are inserted into the composer as plain text by re-dispatching
|
|
19
|
-
* a synthetic text/plain paste, so the insertion goes through DSH's own
|
|
20
|
-
* composer path (command menu, draft state, undo history) instead of
|
|
21
|
-
* poking at the DOM.
|
|
22
|
-
*
|
|
23
|
-
* Ordinary pastes — text, images, real files — are never touched: the handler
|
|
24
|
-
* returns before `preventDefault()` unless a directory is present.
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
/** The host route; hand-mirrored from `src/index.ts` (ROUTE_PATH). */
|
|
28
|
-
var ROUTE_PATH = '/dsh-folder-path/api/v1/clipboard-paths'
|
|
29
|
-
|
|
30
|
-
/** Marks the synthetic events this plugin dispatches, so they are not re-read. */
|
|
31
|
-
var SYNTHETIC = '__dshFolderPathSynthetic'
|
|
32
|
-
|
|
33
|
-
/** The composer's editable root, as Lexical renders it. */
|
|
34
|
-
var EDITABLE_SELECTOR = '[contenteditable=""],[contenteditable="true"]'
|
|
35
|
-
|
|
36
|
-
/** How long a failure notice stays on screen. */
|
|
37
|
-
var TOAST_MS = 5000
|
|
38
|
-
|
|
39
|
-
/** Directories and files a paste carried, split apart by `classifyPayload`. */
|
|
40
|
-
interface ClassifiedPayload {
|
|
41
|
-
directories: string[]
|
|
42
|
-
files: File[]
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** The host half's answer to a clipboard probe. */
|
|
46
|
-
interface HostAnswer {
|
|
47
|
-
paths: string[]
|
|
48
|
-
reason: string | null
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/** The browser Cordis context surface this plugin uses. */
|
|
52
|
-
interface BrowserContext {
|
|
53
|
-
effect(factory: () => () => void, label: string): void
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Split a paste payload into directories and files.
|
|
58
|
-
*
|
|
59
|
-
* `webkitGetAsEntry()` is the only reliable directory signal. The
|
|
60
|
-
* `size === 0 && type === ''` arm is a fallback for engines without the entry
|
|
61
|
-
* API; it is deliberately not applied when an entry exists, so a genuinely
|
|
62
|
-
* empty file is never mistaken for a folder.
|
|
63
|
-
*
|
|
64
|
-
* @param dataTransfer - the event's clipboard or drag data.
|
|
65
|
-
* @returns directory basenames and the non-directory files.
|
|
66
|
-
*/
|
|
67
|
-
function classifyPayload(dataTransfer: DataTransfer): ClassifiedPayload {
|
|
68
|
-
var directories: string[] = []
|
|
69
|
-
var files: File[] = []
|
|
70
|
-
var items: DataTransferItem[] = dataTransfer.items ? Array.prototype.slice.call(dataTransfer.items) : []
|
|
71
|
-
|
|
72
|
-
for (var index = 0; index < items.length; index += 1) {
|
|
73
|
-
var item = items[index]
|
|
74
|
-
if (item.kind !== 'file') continue
|
|
75
|
-
|
|
76
|
-
var entry: FileSystemEntry | null = null
|
|
77
|
-
try {
|
|
78
|
-
entry = typeof item.webkitGetAsEntry === 'function' ? item.webkitGetAsEntry() : null
|
|
79
|
-
} catch (error) {
|
|
80
|
-
entry = null
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (entry !== null) {
|
|
84
|
-
// The entry API answers both questions. Reading the file of a directory is
|
|
85
|
-
// deliberately skipped: the browser hands over a zero-byte `File` whose
|
|
86
|
-
// bytes cannot be read, which is the upload failure this plugin exists to
|
|
87
|
-
// avoid, so there is nothing to forward for it.
|
|
88
|
-
if (entry.isDirectory === true) {
|
|
89
|
-
if (entry.name) directories.push(entry.name)
|
|
90
|
-
continue
|
|
91
|
-
}
|
|
92
|
-
var entryFile: File | null = null
|
|
93
|
-
try {
|
|
94
|
-
entryFile = item.getAsFile()
|
|
95
|
-
} catch (error) {
|
|
96
|
-
entryFile = null
|
|
97
|
-
}
|
|
98
|
-
if (entryFile !== null) files.push(entryFile)
|
|
99
|
-
continue
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
var file: File | null = null
|
|
103
|
-
try {
|
|
104
|
-
file = item.getAsFile()
|
|
105
|
-
} catch (error) {
|
|
106
|
-
file = null
|
|
107
|
-
}
|
|
108
|
-
if (file === null) continue
|
|
109
|
-
if (file.size === 0 && file.type === '') {
|
|
110
|
-
// No entry API (a non-Chromium engine): a zero-byte, type-less item is
|
|
111
|
-
// the only directory-shaped signal left to go on.
|
|
112
|
-
if (file.name) directories.push(file.name)
|
|
113
|
-
continue
|
|
114
|
-
}
|
|
115
|
-
files.push(file)
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return { directories: directories, files: files }
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Find the composer root a paste belongs to.
|
|
123
|
-
*
|
|
124
|
-
* A paste into anything that is not an editable surface is left alone: the
|
|
125
|
-
* plugin swallows events it handles, and swallowing a paste it cannot insert
|
|
126
|
-
* into would silently eat user data.
|
|
127
|
-
*
|
|
128
|
-
* @param target - the event target.
|
|
129
|
-
* @returns the editable root element, or null.
|
|
130
|
-
*/
|
|
131
|
-
function editableRootOf(target: EventTarget | null): HTMLElement | null {
|
|
132
|
-
var candidates: Array<Element | null> = [target as Element | null, document.activeElement]
|
|
133
|
-
for (var index = 0; index < candidates.length; index += 1) {
|
|
134
|
-
var candidate = candidates[index]
|
|
135
|
-
if (candidate && typeof candidate.closest === 'function') {
|
|
136
|
-
var root = candidate.closest(EDITABLE_SELECTOR)
|
|
137
|
-
if (root !== null) return root as HTMLElement
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
return null
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Ask the host half for the full paths behind the pasted folder names.
|
|
145
|
-
* @param names - directory basenames the browser saw.
|
|
146
|
-
* @returns the resolved paths (empty when the host could not answer).
|
|
147
|
-
*/
|
|
148
|
-
async function requestPaths(names: string[]): Promise<HostAnswer> {
|
|
149
|
-
var response = await fetch(ROUTE_PATH, {
|
|
150
|
-
method: 'POST',
|
|
151
|
-
headers: { 'content-type': 'application/json' },
|
|
152
|
-
body: JSON.stringify({ names: names }),
|
|
153
|
-
})
|
|
154
|
-
if (!response.ok) return { paths: [], reason: 'http-' + String(response.status) }
|
|
155
|
-
var payload = await response.json()
|
|
156
|
-
if (payload && payload.ok === true && Array.isArray(payload.paths)) {
|
|
157
|
-
return { paths: payload.paths, reason: null }
|
|
158
|
-
}
|
|
159
|
-
return { paths: [], reason: (payload && payload.reason) || 'unknown' }
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Insert plain text through DSH's own paste path.
|
|
164
|
-
*
|
|
165
|
-
* A synthetic `paste` carrying only `text/plain` reaches the same Lexical
|
|
166
|
-
* command a real text paste does, so the draft, command menu and undo history
|
|
167
|
-
* stay consistent. `execCommand` is the fallback for engines that refuse to
|
|
168
|
-
* construct a `ClipboardEvent` with clipboard data.
|
|
169
|
-
*
|
|
170
|
-
* @param root - the composer's editable root.
|
|
171
|
-
* @param text - the text to insert.
|
|
172
|
-
*/
|
|
173
|
-
function insertText(root: HTMLElement, text: string): void {
|
|
174
|
-
root.focus({ preventScroll: true })
|
|
175
|
-
try {
|
|
176
|
-
var dataTransfer = new DataTransfer()
|
|
177
|
-
dataTransfer.setData('text/plain', text)
|
|
178
|
-
var event = new ClipboardEvent('paste', { clipboardData: dataTransfer, bubbles: true, cancelable: true })
|
|
179
|
-
;(event as ClipboardEvent & Record<string, unknown>)[SYNTHETIC] = true
|
|
180
|
-
root.dispatchEvent(event)
|
|
181
|
-
if (event.defaultPrevented) return
|
|
182
|
-
} catch (error) {
|
|
183
|
-
/* fall through to the DOM insertion below */
|
|
184
|
-
}
|
|
185
|
-
document.execCommand('insertText', false, text)
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Hand non-directory files back to the composer.
|
|
190
|
-
*
|
|
191
|
-
* Copying a folder and files together is legal; the files still belong to the
|
|
192
|
-
* attachment flow, so they are forwarded as one synthetic file paste after the
|
|
193
|
-
* paths are in.
|
|
194
|
-
*
|
|
195
|
-
* @param root - the composer's editable root.
|
|
196
|
-
* @param files - the files taken out of the original paste.
|
|
197
|
-
*/
|
|
198
|
-
function forwardFiles(root: HTMLElement, files: File[]): void {
|
|
199
|
-
if (files.length === 0) return
|
|
200
|
-
try {
|
|
201
|
-
var dataTransfer = new DataTransfer()
|
|
202
|
-
for (var index = 0; index < files.length; index += 1) dataTransfer.items.add(files[index])
|
|
203
|
-
var event = new ClipboardEvent('paste', { clipboardData: dataTransfer, bubbles: true, cancelable: true })
|
|
204
|
-
;(event as ClipboardEvent & Record<string, unknown>)[SYNTHETIC] = true
|
|
205
|
-
root.dispatchEvent(event)
|
|
206
|
-
} catch (error) {
|
|
207
|
-
/* the paths are the point; a failed forward must not undo them */
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Failure copy for a host answer.
|
|
213
|
-
* @param reason - the host's reason code.
|
|
214
|
-
* @returns the message to show.
|
|
215
|
-
*/
|
|
216
|
-
function failureText(reason: string | null): string {
|
|
217
|
-
if (reason === 'unsupported-platform') return '当前宿主系统不支持读取剪贴板里的文件夹路径(仅支持 Windows / macOS)'
|
|
218
|
-
if (reason === 'remote-not-supported') return '当前页面不在运行 dsh web 的这台机器上,读不到这台机器的剪贴板'
|
|
219
|
-
if (reason === 'clipboard-error') return '读取剪贴板失败,没能解析文件夹的完整路径'
|
|
220
|
-
if (reason === 'no-match') return '剪贴板里的内容与粘贴的文件夹不一致,没能解析完整路径'
|
|
221
|
-
return '没能解析文件夹的完整路径(宿主未响应)'
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Show a short-lived notice above the composer.
|
|
226
|
-
* @param message - the text to show.
|
|
227
|
-
*/
|
|
228
|
-
function toast(message: string): void {
|
|
229
|
-
try {
|
|
230
|
-
var element = document.createElement('div')
|
|
231
|
-
element.textContent = message
|
|
232
|
-
element.setAttribute('data-dsh-folder-path-toast', '')
|
|
233
|
-
element.style.cssText =
|
|
234
|
-
'position:fixed;left:50%;bottom:96px;transform:translateX(-50%);z-index:2147483000;' +
|
|
235
|
-
'max-width:70vw;padding:8px 14px;border-radius:10px;background:rgba(28,28,30,0.92);color:#fff;' +
|
|
236
|
-
'font-size:13px;line-height:1.45;box-shadow:0 6px 24px rgba(0,0,0,0.28);pointer-events:none;'
|
|
237
|
-
document.body.appendChild(element)
|
|
238
|
-
setTimeout(function () {
|
|
239
|
-
element.remove()
|
|
240
|
-
}, TOAST_MS)
|
|
241
|
-
} catch (error) {
|
|
242
|
-
/* a missing notice must never break the paste */
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* Build the browser-half plugin from the bundle's `require`.
|
|
248
|
-
* @param require - the module system's synchronous require.
|
|
249
|
-
* @returns the plugin's `apply`.
|
|
250
|
-
*/
|
|
251
|
-
function createPlugin(require: (id: string) => unknown): { apply: (ctx: BrowserContext) => void } {
|
|
252
|
-
/**
|
|
253
|
-
* Resolve the pasted folders and put their paths in the composer.
|
|
254
|
-
* @param root - the composer's editable root.
|
|
255
|
-
* @param directories - the pasted directory basenames.
|
|
256
|
-
* @param files - the non-directory files from the same paste.
|
|
257
|
-
*/
|
|
258
|
-
async function resolveAndInsert(root: HTMLElement, directories: string[], files: File[]): Promise<void> {
|
|
259
|
-
var result: HostAnswer
|
|
260
|
-
try {
|
|
261
|
-
result = await requestPaths(directories)
|
|
262
|
-
} catch (error) {
|
|
263
|
-
result = { paths: [], reason: 'host-unreachable' }
|
|
264
|
-
}
|
|
265
|
-
if (result.paths.length === 0) {
|
|
266
|
-
toast(failureText(result.reason))
|
|
267
|
-
forwardFiles(root, files)
|
|
268
|
-
return
|
|
269
|
-
}
|
|
270
|
-
if (!root.isConnected) {
|
|
271
|
-
toast('输入框已经关闭,路径没有插入:' + result.paths.join('、'))
|
|
272
|
-
return
|
|
273
|
-
}
|
|
274
|
-
insertText(root, result.paths.join('\n'))
|
|
275
|
-
forwardFiles(root, files)
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
/**
|
|
279
|
-
* Capture-phase paste handler: intercepts directory pastes only.
|
|
280
|
-
* @param event - the paste event.
|
|
281
|
-
*/
|
|
282
|
-
function onPaste(event: ClipboardEvent): void {
|
|
283
|
-
if ((event as ClipboardEvent & Record<string, unknown>)[SYNTHETIC] === true) return
|
|
284
|
-
if (!event.clipboardData) return
|
|
285
|
-
|
|
286
|
-
var payload = classifyPayload(event.clipboardData)
|
|
287
|
-
if (payload.directories.length === 0) return
|
|
288
|
-
|
|
289
|
-
var root = editableRootOf(event.target)
|
|
290
|
-
if (root === null) return
|
|
291
|
-
|
|
292
|
-
event.preventDefault()
|
|
293
|
-
event.stopPropagation()
|
|
294
|
-
void resolveAndInsert(root, payload.directories, payload.files)
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
* Mount the listener; the returned plugin is applied by the client runner.
|
|
299
|
-
* @param ctx - browser Cordis context.
|
|
300
|
-
*/
|
|
301
|
-
function apply(ctx: BrowserContext): void {
|
|
302
|
-
document.addEventListener('paste', onPaste, true)
|
|
303
|
-
ctx.effect(
|
|
304
|
-
function () {
|
|
305
|
-
return function () {
|
|
306
|
-
document.removeEventListener('paste', onPaste, true)
|
|
307
|
-
}
|
|
308
|
-
},
|
|
309
|
-
'dsh-folder-path: paste listener',
|
|
310
|
-
)
|
|
311
|
-
|
|
312
|
-
// Troubleshooting hook: the same resolution the paste path uses, callable
|
|
313
|
-
// from the devtools console (`await __dshFolderPath.probe(['name'])`).
|
|
314
|
-
window.__dshFolderPath = {
|
|
315
|
-
route: ROUTE_PATH,
|
|
316
|
-
probe: requestPaths,
|
|
317
|
-
insert: insertText,
|
|
318
|
-
classify: classifyPayload,
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
return { apply: apply }
|
|
323
|
-
}
|
package/scripts/build-client.mts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Wrap the compiled browser half into the DSH client bundle contract.
|
|
3
|
-
*
|
|
4
|
-
* The client module system executes a plugin bundle as a plain classic script
|
|
5
|
-
* that REGISTERS a lazy CJS factory:
|
|
6
|
-
*
|
|
7
|
-
* window.__ModuleLoader__.load({ id, factory: (require) => exports })
|
|
8
|
-
*
|
|
9
|
-
* so the bundle needs no bundler. `client/index.ts` defines `createPlugin`;
|
|
10
|
-
* `tsc -p tsconfig.client.json` compiles it to `.client-build/index.js`, and
|
|
11
|
-
* this script wraps that compiled output into `lib/client.js`, which is what
|
|
12
|
-
* `exports['./client']` points at. Run by `npm run build` (and by `prepare`, so
|
|
13
|
-
* a `dsh plugin add` always ships a bundle).
|
|
14
|
-
*/
|
|
15
|
-
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
16
|
-
import { dirname, join, resolve } from 'node:path'
|
|
17
|
-
import { fileURLToPath } from 'node:url'
|
|
18
|
-
|
|
19
|
-
const root = resolve(dirname(fileURLToPath(import.meta.url)), '..')
|
|
20
|
-
const source = readFileSync(join(root, '.client-build', 'index.js'), 'utf8')
|
|
21
|
-
const target = join(root, 'lib', 'client.js')
|
|
22
|
-
|
|
23
|
-
const indent = (text: string): string =>
|
|
24
|
-
text
|
|
25
|
-
.split('\n')
|
|
26
|
-
.map((line) => (line.length > 0 ? ` ${line}` : line))
|
|
27
|
-
.join('\n')
|
|
28
|
-
|
|
29
|
-
const bundle = `window.__ModuleLoader__.load({
|
|
30
|
-
id: "dsh-folder-path",
|
|
31
|
-
factory: (require) => {
|
|
32
|
-
var module = { exports: {} };
|
|
33
|
-
var exports = module.exports;
|
|
34
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
35
|
-
// ---- .client-build/index.js ----
|
|
36
|
-
${indent(source)}
|
|
37
|
-
const plugin = createPlugin(require);
|
|
38
|
-
exports.apply = plugin.apply;
|
|
39
|
-
return module.exports;
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
`
|
|
43
|
-
|
|
44
|
-
mkdirSync(dirname(target), { recursive: true })
|
|
45
|
-
writeFileSync(target, bundle)
|
|
46
|
-
console.log(`client bundle -> ${target} (${bundle.length} bytes)`)
|