dsh-workspace-explorer 1.0.1 → 1.1.1

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.
@@ -1,82 +1,82 @@
1
- import { execFile } from 'node:child_process';
2
- import { win32 as path } from 'node:path';
3
- import { fileURLToPath } from 'node:url';
4
- import { promisify } from 'node:util';
5
-
6
- const execute = promisify(execFile);
7
- const helper = fileURLToPath(new URL('./ExplorerShell.cs', import.meta.url));
8
- const script = String.raw`
9
- $ErrorActionPreference = 'Stop'
10
- [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding($false)
11
- [Console]::InputEncoding = [System.Text.Encoding]::UTF8
12
- Add-Type -Path $env:DSH_EXPLORER_SHELL
13
- $request = ConvertFrom-Json ([Console]::In.ReadToEnd())
14
- if ($request.action -in 'stage', 'clipboard') {
15
- Add-Type -AssemblyName System.Windows.Forms
16
- }
17
- switch ($request.action) {
18
- 'stage' {
19
- $paths = New-Object System.Collections.Specialized.StringCollection
20
- foreach ($source in $request.sources) { [void]$paths.Add($source) }
21
- $data = New-Object System.Windows.Forms.DataObject
22
- $data.SetFileDropList($paths)
23
- $effect = if ($request.cut) { 2 } else { 1 }
24
- $stream = New-Object System.IO.MemoryStream -ArgumentList (,[BitConverter]::GetBytes([int]$effect))
25
- $data.SetData('Preferred DropEffect', $false, $stream)
26
- [System.Windows.Forms.Clipboard]::SetDataObject($data, $true, 8, 30)
27
- @{ ok = $true } | ConvertTo-Json -Compress
28
- }
29
- 'clipboard' {
30
- for ($attempt = 0; $attempt -lt 3; $attempt++) {
31
- try {
32
- $sequence = [ExplorerShell]::GetClipboardSequenceNumber()
33
- $paths = @([System.Windows.Forms.Clipboard]::GetFileDropList())
34
- $cut = if ($paths.Count -gt 0) { [ExplorerShell]::ClipboardIsCut() } else { $false }
35
- if ($sequence -eq [ExplorerShell]::GetClipboardSequenceNumber()) {
36
- @{ paths = $paths; cut = $cut; sequence = $sequence } | ConvertTo-Json -Compress
37
- return
38
- }
39
- } catch {
40
- if ($attempt -eq 2) { throw }
41
- }
42
- Start-Sleep -Milliseconds 40
43
- }
44
- throw 'Clipboard changed'
45
- }
46
- 'clearCut' {
47
- [ExplorerShell]::ClearClipboardIfUnchanged($request.sequence)
48
- @{ ok = $true } | ConvertTo-Json -Compress
49
- }
50
- 'transfer' {
51
- $results = @(foreach ($source in $request.sources) {
52
- try { [ExplorerShell]::Transfer($source, $request.destination, $request.cut) }
53
- catch { $false }
54
- })
55
- @{ results = $results } | ConvertTo-Json -Compress
56
- }
57
- 'rename' {
58
- @{ code = [ExplorerShell]::Rename($request.source, $request.destination) } | ConvertTo-Json -Compress
59
- }
60
- 'recycle' {
61
- $results = @(foreach ($source in $request.sources) {
62
- try { [ExplorerShell]::Recycle($source) }
63
- catch { $false }
64
- })
65
- @{ results = $results } | ConvertTo-Json -Compress
66
- }
67
- }
68
- `;
69
- const encoded = Buffer.from(script, 'utf16le').toString('base64');
70
-
71
- export async function windowsFileOperation(request) {
72
- const powershell = path.join(process.env.SystemRoot || 'C:\Windows', 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe');
73
- const result = execute(powershell, ['-NoProfile', '-NonInteractive', '-STA', '-EncodedCommand', encoded], {
74
- windowsHide: true, maxBuffer: 256 * 1024,
75
- ...(['transfer', 'recycle'].includes(request.action) ? {} : { timeout: 15000 }),
76
- env: { ...process.env, DSH_EXPLORER_SHELL: helper },
77
- });
78
- result.child.stdin.on('error', () => {}); // The process result reports early exit or spawn failure.
79
- result.child.stdin.end(JSON.stringify(request), 'utf8');
80
- const { stdout } = await result;
81
- return JSON.parse(stdout.trim());
1
+ import { execFile } from 'node:child_process';
2
+ import { win32 as path } from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ import { promisify } from 'node:util';
5
+
6
+ const execute = promisify(execFile);
7
+ const helper = fileURLToPath(new URL('./ExplorerShell.cs', import.meta.url));
8
+ const script = String.raw`
9
+ $ErrorActionPreference = 'Stop'
10
+ [Console]::OutputEncoding = New-Object System.Text.UTF8Encoding($false)
11
+ [Console]::InputEncoding = [System.Text.Encoding]::UTF8
12
+ Add-Type -Path $env:DSH_EXPLORER_SHELL
13
+ $request = ConvertFrom-Json ([Console]::In.ReadToEnd())
14
+ if ($request.action -in 'stage', 'clipboard') {
15
+ Add-Type -AssemblyName System.Windows.Forms
16
+ }
17
+ switch ($request.action) {
18
+ 'stage' {
19
+ $paths = New-Object System.Collections.Specialized.StringCollection
20
+ foreach ($source in $request.sources) { [void]$paths.Add($source) }
21
+ $data = New-Object System.Windows.Forms.DataObject
22
+ $data.SetFileDropList($paths)
23
+ $effect = if ($request.cut) { 2 } else { 1 }
24
+ $stream = New-Object System.IO.MemoryStream -ArgumentList (,[BitConverter]::GetBytes([int]$effect))
25
+ $data.SetData('Preferred DropEffect', $false, $stream)
26
+ [System.Windows.Forms.Clipboard]::SetDataObject($data, $true, 8, 30)
27
+ @{ ok = $true } | ConvertTo-Json -Compress
28
+ }
29
+ 'clipboard' {
30
+ for ($attempt = 0; $attempt -lt 3; $attempt++) {
31
+ try {
32
+ $sequence = [ExplorerShell]::GetClipboardSequenceNumber()
33
+ $paths = @([System.Windows.Forms.Clipboard]::GetFileDropList())
34
+ $cut = if ($paths.Count -gt 0) { [ExplorerShell]::ClipboardIsCut() } else { $false }
35
+ if ($sequence -eq [ExplorerShell]::GetClipboardSequenceNumber()) {
36
+ @{ paths = $paths; cut = $cut; sequence = $sequence } | ConvertTo-Json -Compress
37
+ return
38
+ }
39
+ } catch {
40
+ if ($attempt -eq 2) { throw }
41
+ }
42
+ Start-Sleep -Milliseconds 40
43
+ }
44
+ throw 'Clipboard changed'
45
+ }
46
+ 'clearCut' {
47
+ [ExplorerShell]::ClearClipboardIfUnchanged($request.sequence)
48
+ @{ ok = $true } | ConvertTo-Json -Compress
49
+ }
50
+ 'transfer' {
51
+ $results = @(foreach ($source in $request.sources) {
52
+ try { [ExplorerShell]::Transfer($source, $request.destination, $request.cut) }
53
+ catch { $false }
54
+ })
55
+ @{ results = $results } | ConvertTo-Json -Compress
56
+ }
57
+ 'rename' {
58
+ @{ code = [ExplorerShell]::Rename($request.source, $request.destination) } | ConvertTo-Json -Compress
59
+ }
60
+ 'recycle' {
61
+ $results = @(foreach ($source in $request.sources) {
62
+ try { [ExplorerShell]::Recycle($source) }
63
+ catch { $false }
64
+ })
65
+ @{ results = $results } | ConvertTo-Json -Compress
66
+ }
67
+ }
68
+ `;
69
+ const encoded = Buffer.from(script, 'utf16le').toString('base64');
70
+
71
+ export async function windowsFileOperation(request) {
72
+ const powershell = path.join(process.env.SystemRoot || 'C:\Windows', 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe');
73
+ const result = execute(powershell, ['-NoProfile', '-NonInteractive', '-STA', '-EncodedCommand', encoded], {
74
+ windowsHide: true, maxBuffer: 256 * 1024,
75
+ ...(['transfer', 'recycle'].includes(request.action) ? {} : { timeout: 15000 }),
76
+ env: { ...process.env, DSH_EXPLORER_SHELL: helper },
77
+ });
78
+ result.child.stdin.on('error', () => {}); // The process result reports early exit or spawn failure.
79
+ result.child.stdin.end(JSON.stringify(request), 'utf8');
80
+ const { stdout } = await result;
81
+ return JSON.parse(stdout.trim());
82
82
  }
@@ -1,19 +1,19 @@
1
- const imageExtensions = new Map([
2
- ['png', 'image/png'], ['jpg', 'image/jpeg'], ['jpeg', 'image/jpeg'],
3
- ['webp', 'image/webp'], ['gif', 'image/gif'], ['bmp', 'image/bmp'],
4
- ['ico', 'image/x-icon'], ['avif', 'image/avif'], ['apng', 'image/apng'],
5
- ['jfif', 'image/jpeg'], ['heic', 'image/heic'], ['heif', 'image/heif'],
6
- ['tif', 'image/tiff'], ['tiff', 'image/tiff'],
7
- ]);
8
-
9
- const videoExtensions = new Map([
10
- ['mp4', 'video/mp4'], ['m4v', 'video/mp4'], ['webm', 'video/webm'],
11
- ['mov', 'video/quicktime'], ['mkv', 'video/x-matroska'], ['ogv', 'video/ogg'],
12
- ['avi', 'video/x-msvideo'], ['wmv', 'video/x-ms-wmv'], ['mpeg', 'video/mpeg'],
13
- ['mpg', 'video/mpeg'], ['3gp', 'video/3gpp'],
14
- ]);
15
-
16
- export function thumbnailType(path) {
17
- const extension = /\.([a-z0-9]+)$/i.exec(path)?.[1].toLowerCase();
18
- return imageExtensions.get(extension) ?? videoExtensions.get(extension);
1
+ const imageExtensions = new Map([
2
+ ['png', 'image/png'], ['jpg', 'image/jpeg'], ['jpeg', 'image/jpeg'],
3
+ ['webp', 'image/webp'], ['gif', 'image/gif'], ['bmp', 'image/bmp'],
4
+ ['ico', 'image/x-icon'], ['avif', 'image/avif'], ['apng', 'image/apng'],
5
+ ['jfif', 'image/jpeg'], ['heic', 'image/heic'], ['heif', 'image/heif'],
6
+ ['tif', 'image/tiff'], ['tiff', 'image/tiff'],
7
+ ]);
8
+
9
+ const videoExtensions = new Map([
10
+ ['mp4', 'video/mp4'], ['m4v', 'video/mp4'], ['webm', 'video/webm'],
11
+ ['mov', 'video/quicktime'], ['mkv', 'video/x-matroska'], ['ogv', 'video/ogg'],
12
+ ['avi', 'video/x-msvideo'], ['wmv', 'video/x-ms-wmv'], ['mpeg', 'video/mpeg'],
13
+ ['mpg', 'video/mpeg'], ['3gp', 'video/3gpp'],
14
+ ]);
15
+
16
+ export function thumbnailType(path) {
17
+ const extension = /\.([a-z0-9]+)$/i.exec(path)?.[1].toLowerCase();
18
+ return imageExtensions.get(extension) ?? videoExtensions.get(extension);
19
19
  }
@@ -1,119 +1,119 @@
1
- import { disks } from './workspace.js';
2
-
3
- const { useEffect, useRef } = react;
4
- const dragType = 'application/x-dsh-workspace-explorer-file';
5
- const imageTypes = new Map([
6
- ['png', 'image/png'], ['jpg', 'image/jpeg'], ['jpeg', 'image/jpeg'],
7
- ['gif', 'image/gif'], ['webp', 'image/webp'],
8
- ]);
9
-
10
- export function useComposerDrop(sessionId, openFolderTab, reportError, root) {
11
- const active = useRef(null);
12
-
13
- useEffect(() => {
14
- if (!sessionId) return;
15
- const lifetime = new AbortController();
16
- const signal = lifetime.signal;
17
- let highlighted;
18
- let highlightClass;
19
-
20
- function highlight(element, className) {
21
- if (element === highlighted && className === highlightClass) return;
22
- if (highlighted) highlighted.classList.remove(highlightClass);
23
- highlighted = element;
24
- highlightClass = className;
25
- if (highlighted) highlighted.classList.add(highlightClass);
26
- }
27
-
28
- function targetCard(target) {
29
- if (!(target instanceof Element)) return null;
30
- const conversation = target.closest('[data-conversation-content]');
31
- if (conversation?.dataset.conversationSession !== sessionId) return null;
32
- const card = conversation.querySelector('[data-composer-card]');
33
- return card?.querySelector('[data-composer-input][contenteditable="true"]') ? card : null;
34
- }
35
-
36
- function over(event) {
37
- const drag = active.current;
38
- if (!drag || !event.dataTransfer?.types.includes(dragType)) return;
39
- const strip = drag.folder && tabStrip(event.target);
40
- if (strip) {
41
- highlight(strip, 'dwe-tab-drop-target');
42
- event.preventDefault();
43
- event.dataTransfer.dropEffect = 'copy';
44
- return;
45
- }
46
- const card = targetCard(event.target);
47
- highlight(card, 'dwe-drop-target');
48
- if (!card) return;
49
- event.preventDefault();
50
- event.dataTransfer.dropEffect = 'copy';
51
- }
52
-
53
- function reset() {
54
- active.current = null;
55
- highlight(null);
56
- }
57
-
58
- async function drop(event) {
59
- const file = active.current;
60
- if (!file || event.dataTransfer?.getData(dragType) !== file.token) return;
61
- const strip = file.folder && tabStrip(event.target);
62
- const card = targetCard(event.target);
63
- reset();
64
- if (!strip && !card) return;
65
- event.preventDefault();
66
- event.stopPropagation();
67
- reportError('');
68
- try {
69
- if (strip) {
70
- openFolderTab(file.path, strip.dataset.dockkitStrip);
71
- return;
72
- }
73
- if (file.folder) {
74
- const editor = card.querySelector('[data-composer-input][contenteditable="true"]');
75
- const data = new DataTransfer();
76
- data.setData('text/plain', file.path);
77
- editor.focus({ preventScroll: true });
78
- editor.dispatchEvent(new ClipboardEvent('paste', { bubbles: true, cancelable: true, clipboardData: data }));
79
- return;
80
- }
81
- const bytes = await disks.readBytes(file.path, signal, root);
82
- // The composer may have switched sessions or become locked during the read.
83
- if (signal.aborted || !card.isConnected || targetCard(card) !== card) return;
84
- const extension = file.name.split('.').at(-1)?.toLowerCase();
85
- const dataTransfer = new DataTransfer();
86
- dataTransfer.items.add(new File([bytes], file.name, {
87
- type: imageTypes.get(extension) ?? 'application/octet-stream',
88
- }));
89
- // Use the composer's existing attachment intake, including its file checks.
90
- card.dispatchEvent(new DragEvent('drop', { bubbles: true, cancelable: true, dataTransfer }));
91
- } catch (error) {
92
- if (!signal.aborted) reportError('无法拖入会话 · ' + (error?.message || String(error)));
93
- }
94
- }
95
-
96
- document.addEventListener('dragover', over, { signal, capture: true });
97
- document.addEventListener('drop', drop, { signal, capture: true });
98
- window.addEventListener('dragend', reset, { signal });
99
- return () => {
100
- lifetime.abort();
101
- reset();
102
- };
103
- }, [sessionId, openFolderTab, reportError, root]);
104
-
105
- return (event, path, name, folder = false) => {
106
- if (!sessionId) {
107
- event.preventDefault();
108
- return;
109
- }
110
- const token = crypto.randomUUID();
111
- active.current = { token, path, name, folder };
112
- event.dataTransfer.effectAllowed = 'copy';
113
- event.dataTransfer.setData(dragType, token);
114
- };
115
- }
116
-
117
- function tabStrip(target) {
118
- return target instanceof Element ? target.closest('[data-dockkit-strip]') : null;
119
- }
1
+ import { disks } from './workspace.js';
2
+
3
+ const { useEffect, useRef } = react;
4
+ const dragType = 'application/x-dsh-workspace-explorer-file';
5
+ const imageTypes = new Map([
6
+ ['png', 'image/png'], ['jpg', 'image/jpeg'], ['jpeg', 'image/jpeg'],
7
+ ['gif', 'image/gif'], ['webp', 'image/webp'],
8
+ ]);
9
+
10
+ export function useComposerDrop(sessionId, openFolderTab, reportError, root) {
11
+ const active = useRef(null);
12
+
13
+ useEffect(() => {
14
+ if (!sessionId) return;
15
+ const lifetime = new AbortController();
16
+ const signal = lifetime.signal;
17
+ let highlighted;
18
+ let highlightClass;
19
+
20
+ function highlight(element, className) {
21
+ if (element === highlighted && className === highlightClass) return;
22
+ if (highlighted) highlighted.classList.remove(highlightClass);
23
+ highlighted = element;
24
+ highlightClass = className;
25
+ if (highlighted) highlighted.classList.add(highlightClass);
26
+ }
27
+
28
+ function targetCard(target) {
29
+ if (!(target instanceof Element)) return null;
30
+ const conversation = target.closest('[data-conversation-content]');
31
+ if (conversation?.dataset.conversationSession !== sessionId) return null;
32
+ const card = conversation.querySelector('[data-composer-card]');
33
+ return card?.querySelector('[data-composer-input][contenteditable="true"]') ? card : null;
34
+ }
35
+
36
+ function over(event) {
37
+ const drag = active.current;
38
+ if (!drag || !event.dataTransfer?.types.includes(dragType)) return;
39
+ const strip = drag.folder && tabStrip(event.target);
40
+ if (strip) {
41
+ highlight(strip, 'dwe-tab-drop-target');
42
+ event.preventDefault();
43
+ event.dataTransfer.dropEffect = 'copy';
44
+ return;
45
+ }
46
+ const card = targetCard(event.target);
47
+ highlight(card, 'dwe-drop-target');
48
+ if (!card) return;
49
+ event.preventDefault();
50
+ event.dataTransfer.dropEffect = 'copy';
51
+ }
52
+
53
+ function reset() {
54
+ active.current = null;
55
+ highlight(null);
56
+ }
57
+
58
+ async function drop(event) {
59
+ const file = active.current;
60
+ if (!file || event.dataTransfer?.getData(dragType) !== file.token) return;
61
+ const strip = file.folder && tabStrip(event.target);
62
+ const card = targetCard(event.target);
63
+ reset();
64
+ if (!strip && !card) return;
65
+ event.preventDefault();
66
+ event.stopPropagation();
67
+ reportError('');
68
+ try {
69
+ if (strip) {
70
+ openFolderTab(file.path, strip.dataset.dockkitStrip);
71
+ return;
72
+ }
73
+ if (file.folder) {
74
+ const editor = card.querySelector('[data-composer-input][contenteditable="true"]');
75
+ const data = new DataTransfer();
76
+ data.setData('text/plain', file.path);
77
+ editor.focus({ preventScroll: true });
78
+ editor.dispatchEvent(new ClipboardEvent('paste', { bubbles: true, cancelable: true, clipboardData: data }));
79
+ return;
80
+ }
81
+ const bytes = await disks.readBytes(file.path, signal, root);
82
+ // The composer may have switched sessions or become locked during the read.
83
+ if (signal.aborted || !card.isConnected || targetCard(card) !== card) return;
84
+ const extension = file.name.split('.').at(-1)?.toLowerCase();
85
+ const dataTransfer = new DataTransfer();
86
+ dataTransfer.items.add(new File([bytes], file.name, {
87
+ type: imageTypes.get(extension) ?? 'application/octet-stream',
88
+ }));
89
+ // Use the composer's existing attachment intake, including its file checks.
90
+ card.dispatchEvent(new DragEvent('drop', { bubbles: true, cancelable: true, dataTransfer }));
91
+ } catch (error) {
92
+ if (!signal.aborted) reportError('无法拖入会话 · ' + (error?.message || String(error)));
93
+ }
94
+ }
95
+
96
+ document.addEventListener('dragover', over, { signal, capture: true });
97
+ document.addEventListener('drop', drop, { signal, capture: true });
98
+ window.addEventListener('dragend', reset, { signal });
99
+ return () => {
100
+ lifetime.abort();
101
+ reset();
102
+ };
103
+ }, [sessionId, openFolderTab, reportError, root]);
104
+
105
+ return (event, path, name, folder = false) => {
106
+ if (!sessionId) {
107
+ event.preventDefault();
108
+ return;
109
+ }
110
+ const token = crypto.randomUUID();
111
+ active.current = { token, path, name, folder };
112
+ event.dataTransfer.effectAllowed = 'copy';
113
+ event.dataTransfer.setData(dragType, token);
114
+ };
115
+ }
116
+
117
+ function tabStrip(target) {
118
+ return target instanceof Element ? target.closest('[data-dockkit-strip]') : null;
119
+ }