dsh-ide-window 0.3.1 → 0.3.3
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/AGENTS.md +40 -2
- package/CHANGELOG.md +28 -0
- package/README.md +22 -0
- package/client.js +1052 -0
- package/index.js +215 -5
- package/locale/en.json +22 -1
- package/locale/zh.json +22 -1
- package/package.json +2 -2
- package/platform/contract.js +10 -0
- package/platform/macos.js +19 -0
- package/platform/windows.js +67 -0
package/client.js
CHANGED
|
@@ -235,6 +235,27 @@ window.__ModuleLoader__.load({
|
|
|
235
235
|
"theme_atom_one_light": "Atom One Light",
|
|
236
236
|
"theme_solarized_light": "Solarized Light",
|
|
237
237
|
"theme_intellij_light": "IntelliJ Light",
|
|
238
|
+
"shortcutScreenshot": "Screenshot (can jump to chat)",
|
|
239
|
+
"shotBusy": "Capturing…",
|
|
240
|
+
"shotRetry": "Retry",
|
|
241
|
+
"shotFailed": "Screenshot failed",
|
|
242
|
+
"shotNoPermissionTitle": "Cannot capture: Screen Recording permission is missing",
|
|
243
|
+
"shotNoPermissionMac": "Enable DeepSeek Harness under System Settings → Privacy & Security → Screen Recording, then quit and reopen the app (the grant only applies after a relaunch).",
|
|
244
|
+
"shotNoPermissionWindows": "Windows needs no permission for this. Check that the screen is unlocked and no remote-desktop session or policy blocks capture, then retry.",
|
|
245
|
+
"shotOpenSettings": "Open System Settings",
|
|
246
|
+
"shotAttached": "Screenshot inserted into the conversation",
|
|
247
|
+
"shotCopied": "Screenshot copied to the clipboard",
|
|
248
|
+
"shotNoEditor": "Composer not found: the screenshot is on the clipboard, press ⌘V",
|
|
249
|
+
"shotOpenFailed": "Could not open System Settings. Open it manually: Privacy & Security → Screen Recording",
|
|
250
|
+
"paletteActionScreenshot": "Take a screenshot",
|
|
251
|
+
"shotSelectHint": "Press the left button and drag to frame the area; release to finish, Esc cancels.",
|
|
252
|
+
"shotMarkHint": "Drag inside the frame to draw a box, or pick Text and click to type. ⌘Z undoes, Esc closes.",
|
|
253
|
+
"shotTool_rect": "Box",
|
|
254
|
+
"shotTool_text": "Text",
|
|
255
|
+
"shotUndo": "Undo",
|
|
256
|
+
"shotTextPlaceholder": "Type, then press Enter",
|
|
257
|
+
"shotCopy": "Capture",
|
|
258
|
+
"shotJump": "Capture and open chat",
|
|
238
259
|
}
|
|
239
260
|
const IDE_TEXT_ZH = {
|
|
240
261
|
"title": "代码工作区",
|
|
@@ -463,6 +484,27 @@ window.__ModuleLoader__.load({
|
|
|
463
484
|
"theme_atom_one_light": "Atom One 浅色",
|
|
464
485
|
"theme_solarized_light": "Solarized 浅色",
|
|
465
486
|
"theme_intellij_light": "IntelliJ 浅色",
|
|
487
|
+
"shortcutScreenshot": "截图(可跳转会话)",
|
|
488
|
+
"shotBusy": "正在截图…",
|
|
489
|
+
"shotRetry": "重试",
|
|
490
|
+
"shotFailed": "截图失败",
|
|
491
|
+
"shotNoPermissionTitle": "无法截图:缺少屏幕录制权限",
|
|
492
|
+
"shotNoPermissionMac": "请在「系统设置 → 隐私与安全性 → 屏幕录制」里勾选 DeepSeek Harness,然后完全退出并重新打开应用(授权后必须重启才生效)。",
|
|
493
|
+
"shotNoPermissionWindows": "Windows 截图不需要授权。请确认没有锁屏、远程桌面占用或企业策略拦截,然后重试。",
|
|
494
|
+
"shotOpenSettings": "打开系统设置",
|
|
495
|
+
"shotAttached": "已把截图插入对话",
|
|
496
|
+
"shotCopied": "截图已复制到剪贴板",
|
|
497
|
+
"shotNoEditor": "没找到对话框:截图已复制到剪贴板,按 ⌘V 粘贴",
|
|
498
|
+
"shotOpenFailed": "打不开系统设置,请手动打开:系统设置 → 隐私与安全性 → 屏幕录制",
|
|
499
|
+
"paletteActionScreenshot": "截图",
|
|
500
|
+
"shotSelectHint": "按住鼠标左键拖动,框出要截的范围;松开后完成选框,Esc 取消",
|
|
501
|
+
"shotMarkHint": "在选框内拖动可画矩形,选「文字」后点一下输入;⌘Z 撤销,Esc 关闭",
|
|
502
|
+
"shotTool_rect": "矩形",
|
|
503
|
+
"shotTool_text": "文字",
|
|
504
|
+
"shotUndo": "撤销",
|
|
505
|
+
"shotTextPlaceholder": "输入文字后回车",
|
|
506
|
+
"shotCopy": "截图",
|
|
507
|
+
"shotJump": "截图并跳转会话",
|
|
466
508
|
}
|
|
467
509
|
|
|
468
510
|
// ---- 00-css.js -----------------------------------------------------------
|
|
@@ -1948,6 +1990,151 @@ html.iw-select-code .iw-code .iw-vision {
|
|
|
1948
1990
|
.iw-theme-dot--solarized-light { background: #fdf6e3; }
|
|
1949
1991
|
.iw-theme-dot--intellij-light { background: #f7f8fa; }
|
|
1950
1992
|
|
|
1993
|
+
/* ---- screenshot: veil, permission card, editor ---- */
|
|
1994
|
+
.iw-shot-veil {
|
|
1995
|
+
position: fixed;
|
|
1996
|
+
inset: 0;
|
|
1997
|
+
display: flex;
|
|
1998
|
+
align-items: center;
|
|
1999
|
+
justify-content: center;
|
|
2000
|
+
background: rgba(0, 0, 0, 0.45);
|
|
2001
|
+
z-index: 60;
|
|
2002
|
+
}
|
|
2003
|
+
.iw-shot-veil--editor {
|
|
2004
|
+
flex-direction: column;
|
|
2005
|
+
gap: 10px;
|
|
2006
|
+
padding: 16px;
|
|
2007
|
+
box-sizing: border-box;
|
|
2008
|
+
background: rgba(8, 10, 14, 0.92);
|
|
2009
|
+
outline: none;
|
|
2010
|
+
}
|
|
2011
|
+
.iw-shot {
|
|
2012
|
+
width: 420px;
|
|
2013
|
+
max-width: calc(100% - 32px);
|
|
2014
|
+
padding: 14px;
|
|
2015
|
+
box-sizing: border-box;
|
|
2016
|
+
border: 1px solid var(--dsw-alias-border-l1, #2c3038);
|
|
2017
|
+
border-radius: var(--dsw-radius-lg, 10px);
|
|
2018
|
+
background: var(--dsw-alias-bg-layer-2, #23262c);
|
|
2019
|
+
box-shadow: var(--dsw-elevation-prominent, 0 12px 32px rgba(0, 0, 0, 0.4));
|
|
2020
|
+
}
|
|
2021
|
+
.iw-shot-title {
|
|
2022
|
+
font-size: 13px;
|
|
2023
|
+
font-weight: 500;
|
|
2024
|
+
color: var(--dsw-alias-label-primary, #e6e8ec);
|
|
2025
|
+
}
|
|
2026
|
+
.iw-shot-guide {
|
|
2027
|
+
margin-top: 8px;
|
|
2028
|
+
color: var(--dsw-alias-label-secondary, #98a0ad);
|
|
2029
|
+
font-size: 12px;
|
|
2030
|
+
line-height: 1.6;
|
|
2031
|
+
}
|
|
2032
|
+
.iw-shot-actions {
|
|
2033
|
+
display: flex;
|
|
2034
|
+
align-items: center;
|
|
2035
|
+
justify-content: flex-end;
|
|
2036
|
+
gap: 8px;
|
|
2037
|
+
margin-top: 12px;
|
|
2038
|
+
}
|
|
2039
|
+
.iw-shot-actions--bar {
|
|
2040
|
+
margin-top: 0;
|
|
2041
|
+
}
|
|
2042
|
+
.iw-shot-button,
|
|
2043
|
+
.iw-shot-primary {
|
|
2044
|
+
height: 28px;
|
|
2045
|
+
padding: 0 12px;
|
|
2046
|
+
border-radius: var(--dsw-radius-sm, 4px);
|
|
2047
|
+
border: 1px solid var(--dsw-alias-border-l1, #2c3038);
|
|
2048
|
+
background: transparent;
|
|
2049
|
+
color: var(--dsw-alias-label-primary, #e6e8ec);
|
|
2050
|
+
font: inherit;
|
|
2051
|
+
font-size: 12px;
|
|
2052
|
+
cursor: pointer;
|
|
2053
|
+
}
|
|
2054
|
+
.iw-shot-button:disabled {
|
|
2055
|
+
opacity: 0.45;
|
|
2056
|
+
cursor: default;
|
|
2057
|
+
}
|
|
2058
|
+
.iw-shot-primary {
|
|
2059
|
+
border-color: transparent;
|
|
2060
|
+
background: var(--dsw-alias-brand-primary, #4f8cff);
|
|
2061
|
+
color: #fff;
|
|
2062
|
+
}
|
|
2063
|
+
.iw-shot-button:hover:not(:disabled) {
|
|
2064
|
+
background: var(--dsw-alias-interactive-bg-hover, rgba(127, 127, 127, 0.16));
|
|
2065
|
+
}
|
|
2066
|
+
.iw-shot-card {
|
|
2067
|
+
position: fixed;
|
|
2068
|
+
display: flex;
|
|
2069
|
+
align-items: center;
|
|
2070
|
+
flex-wrap: nowrap;
|
|
2071
|
+
gap: 4px;
|
|
2072
|
+
padding: 5px 6px;
|
|
2073
|
+
box-sizing: border-box;
|
|
2074
|
+
border-radius: var(--dsw-radius-md, 8px);
|
|
2075
|
+
background: var(--dsw-alias-bg-layer-2, #23262c);
|
|
2076
|
+
border: 1px solid var(--dsw-alias-border-l1, #2c3038);
|
|
2077
|
+
box-shadow: var(--dsw-elevation-prominent, 0 10px 28px rgba(0, 0, 0, 0.45));
|
|
2078
|
+
color: var(--dsw-alias-label-primary, #e6e8ec);
|
|
2079
|
+
z-index: 62;
|
|
2080
|
+
}
|
|
2081
|
+
.iw-shot-tool,
|
|
2082
|
+
.iw-shot-card .iw-shot-button,
|
|
2083
|
+
.iw-shot-card .iw-shot-primary {
|
|
2084
|
+
flex: 0 0 auto;
|
|
2085
|
+
white-space: nowrap;
|
|
2086
|
+
}
|
|
2087
|
+
.iw-shot-card .iw-btn {
|
|
2088
|
+
flex: 0 0 auto;
|
|
2089
|
+
white-space: nowrap;
|
|
2090
|
+
font-size: inherit;
|
|
2091
|
+
}
|
|
2092
|
+
.iw-shot-card .iw-sep {
|
|
2093
|
+
width: 1px;
|
|
2094
|
+
height: 16px;
|
|
2095
|
+
margin: 0 2px;
|
|
2096
|
+
flex: 0 0 auto;
|
|
2097
|
+
background: var(--dsw-alias-border-l1, #2c3038);
|
|
2098
|
+
}
|
|
2099
|
+
.iw-shot-grow {
|
|
2100
|
+
flex: 1;
|
|
2101
|
+
}
|
|
2102
|
+
.iw-shot-hint {
|
|
2103
|
+
color: var(--dsw-alias-label-secondary, #98a0ad);
|
|
2104
|
+
}
|
|
2105
|
+
.iw-shot-hint--top {
|
|
2106
|
+
padding: 0 4px 2px;
|
|
2107
|
+
text-align: center;
|
|
2108
|
+
}
|
|
2109
|
+
.iw-shot-stage {
|
|
2110
|
+
position: relative;
|
|
2111
|
+
display: flex;
|
|
2112
|
+
align-items: center;
|
|
2113
|
+
justify-content: center;
|
|
2114
|
+
flex: 1;
|
|
2115
|
+
min-height: 0;
|
|
2116
|
+
width: 100%;
|
|
2117
|
+
touch-action: none;
|
|
2118
|
+
cursor: crosshair;
|
|
2119
|
+
}
|
|
2120
|
+
.iw-shot-canvas {
|
|
2121
|
+
display: block;
|
|
2122
|
+
border-radius: 4px;
|
|
2123
|
+
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
|
|
2124
|
+
}
|
|
2125
|
+
.iw-shot-text {
|
|
2126
|
+
position: absolute;
|
|
2127
|
+
min-width: 160px;
|
|
2128
|
+
padding: 4px 6px;
|
|
2129
|
+
border: 1px solid var(--dsw-alias-brand-primary, #4f8cff);
|
|
2130
|
+
border-radius: 4px;
|
|
2131
|
+
background: var(--dsw-alias-bg-layer-2, #23262c);
|
|
2132
|
+
color: var(--dsw-alias-label-primary, #e6e8ec);
|
|
2133
|
+
font: inherit;
|
|
2134
|
+
font-size: 13px;
|
|
2135
|
+
outline: none;
|
|
2136
|
+
}
|
|
2137
|
+
|
|
1951
2138
|
`
|
|
1952
2139
|
|
|
1953
2140
|
/** Render the stylesheet as a React element so unmounting removes it. */
|
|
@@ -2425,6 +2612,18 @@ const IDE_SHORTCUTS = [
|
|
|
2425
2612
|
},
|
|
2426
2613
|
action: 'focus-filter',
|
|
2427
2614
|
},
|
|
2615
|
+
{
|
|
2616
|
+
id: 'code-workspace.screenshot',
|
|
2617
|
+
kind: 'key',
|
|
2618
|
+
labelKey: 'shortcutScreenshot',
|
|
2619
|
+
aliases: ['screenshot', 'capture', 'shot', '截图', '截屏'],
|
|
2620
|
+
// ⌘⇧4 belongs to macOS itself, so the workspace uses its own combination.
|
|
2621
|
+
defaults: {
|
|
2622
|
+
desktop: { code: 'KeyS', modifiers: ['primary', 'shift'] },
|
|
2623
|
+
web: { code: 'KeyS', modifiers: ['primary', 'shift'] },
|
|
2624
|
+
},
|
|
2625
|
+
action: 'screenshot',
|
|
2626
|
+
},
|
|
2428
2627
|
{
|
|
2429
2628
|
id: 'code-workspace.gesture-definition',
|
|
2430
2629
|
kind: 'gesture',
|
|
@@ -3122,6 +3321,11 @@ function ideCreateStore(refs) {
|
|
|
3122
3321
|
theme: typeof prefs.theme === 'string' ? prefs.theme : '',
|
|
3123
3322
|
/** Whether the palette popup is open (rendered at panel level, not in the bar). */
|
|
3124
3323
|
themeMenu: false,
|
|
3324
|
+
/**
|
|
3325
|
+
* Screenshot dialog: `null` when closed, otherwise
|
|
3326
|
+
* `{ phase: 'editing' | 'error', dataUrl?, path?, code?, message? }`.
|
|
3327
|
+
*/
|
|
3328
|
+
screenshot: null,
|
|
3125
3329
|
/** Editor and tree font sizes, in px, shared by the whole workspace. */
|
|
3126
3330
|
codeFontSize: Number.isFinite(prefs.codeFontSize) ? prefs.codeFontSize : 12.5,
|
|
3127
3331
|
treeFontSize: Number.isFinite(prefs.treeFontSize) ? prefs.treeFontSize : 14.5,
|
|
@@ -4174,6 +4378,8 @@ function ideCreateStore(refs) {
|
|
|
4174
4378
|
/** Turn reference interception on or off (persisted). */
|
|
4175
4379
|
/** Open or close the palette popup. */
|
|
4176
4380
|
setThemeMenu: (themeMenu) => patch({ themeMenu }),
|
|
4381
|
+
/** Show, replace or clear the screenshot dialog. */
|
|
4382
|
+
setScreenshot: (screenshot) => patch({ screenshot }),
|
|
4177
4383
|
/** Switch the panel palette; an empty id goes back to the DSH colours. */
|
|
4178
4384
|
setTheme: (theme) => {
|
|
4179
4385
|
ideWritePrefs({ theme })
|
|
@@ -7122,6 +7328,7 @@ function IdePanel({ store, translate, onBackToChat, onToggleSidebar, onEnter, us
|
|
|
7122
7328
|
state.themeMenu === true
|
|
7123
7329
|
? h(IdeThemePopup, { key: 'themepopup', store, themeId: ideIsThemeId(state.theme) ? state.theme : '', tr })
|
|
7124
7330
|
: null,
|
|
7331
|
+
|
|
7125
7332
|
h(IdeTreeMenu, {
|
|
7126
7333
|
key: 'treemenu',
|
|
7127
7334
|
store,
|
|
@@ -10221,6 +10428,771 @@ function IdePalette({ store, tr, actions, onClose }) {
|
|
|
10221
10428
|
])
|
|
10222
10429
|
}
|
|
10223
10430
|
|
|
10431
|
+
// ---- 72-screenshot.js ----------------------------------------------------
|
|
10432
|
+
/**
|
|
10433
|
+
* Screenshot editor: the Host captures, the panel decides and annotates.
|
|
10434
|
+
*
|
|
10435
|
+
* The Web page cannot read the screen (the shell answers `getDisplayMedia` with
|
|
10436
|
+
* a refusal), so the Host captures a full screen with an OS built-in tool and
|
|
10437
|
+
* hands it back as a data URL. Everything after that is ordinary Canvas work,
|
|
10438
|
+
* which is why macOS and Windows share this whole file:
|
|
10439
|
+
*
|
|
10440
|
+
* - **range**: the user drags a rectangle on the capture (move it, pull its
|
|
10441
|
+
* handles), and only that rectangle is exported;
|
|
10442
|
+
* - **marks**: rectangle, ellipse, arrow, pen, text and mosaic, each stored as
|
|
10443
|
+
* an operation instead of pixels, so undo is a `pop()` and the export redraws
|
|
10444
|
+
* from the untouched base image;
|
|
10445
|
+
* - **budget**: the export is cropped, scaled to a 1920 long edge and encoded as
|
|
10446
|
+
* JPEG with a quality ladder, so a screenshot stays well under 1 MB.
|
|
10447
|
+
*
|
|
10448
|
+
* Two exits: paste it into the conversation (only an image is added — the draft's
|
|
10449
|
+
* text and attachments are untouched) or copy it to the system clipboard.
|
|
10450
|
+
*/
|
|
10451
|
+
|
|
10452
|
+
const IDE_SHOT_ROUTE = '/dsh-ide/screenshot'
|
|
10453
|
+
const IDE_SHOT_PERMISSION_ROUTE = '/dsh-ide/screen-permission'
|
|
10454
|
+
|
|
10455
|
+
/** Long edge of the export; 1920 keeps a 16:9 crop at 1080p. */
|
|
10456
|
+
const IDE_SHOT_EXPORT_EDGE = 1920
|
|
10457
|
+
/** The export must not exceed this when it becomes a chat attachment. */
|
|
10458
|
+
const IDE_SHOT_BUDGET = 1024 * 1024
|
|
10459
|
+
/** JPEG qualities tried in order until the budget is met. */
|
|
10460
|
+
const IDE_SHOT_QUALITIES = [0.8, 0.7, 0.6, 0.5, 0.4]
|
|
10461
|
+
|
|
10462
|
+
/**
|
|
10463
|
+
* Tools the popup card offers.
|
|
10464
|
+
*
|
|
10465
|
+
* `select` is the range tool, not a mark. Ellipse, arrow, pen and mosaic still
|
|
10466
|
+
* have their drawing code (`ideShotDrawOp`) but are not surfaced: the card stays
|
|
10467
|
+
* small, and the two that cover callouts are enough.
|
|
10468
|
+
*/
|
|
10469
|
+
const IDE_SHOT_TOOLS = ['select', 'rect', 'text']
|
|
10470
|
+
/** Stroke colours offered; red first because a screenshot mark is usually red. */
|
|
10471
|
+
const IDE_SHOT_COLORS = ['#ff4d4f', '#ffd666', '#52c41a', '#4f8cff', '#ffffff']
|
|
10472
|
+
/** Stroke widths offered, in image pixels. */
|
|
10473
|
+
const IDE_SHOT_WIDTHS = [2, 4, 8]
|
|
10474
|
+
/** Smallest selection a drag may produce, in image pixels. */
|
|
10475
|
+
const IDE_SHOT_MIN_SELECTION = 24
|
|
10476
|
+
|
|
10477
|
+
/** POST one screenshot request; never throws, returns `{ ok }` shapes. */
|
|
10478
|
+
async function ideShotRequest(body) {
|
|
10479
|
+
let response
|
|
10480
|
+
try {
|
|
10481
|
+
response = await fetch(IDE_SHOT_ROUTE, {
|
|
10482
|
+
method: 'POST',
|
|
10483
|
+
headers: { 'content-type': 'application/json' },
|
|
10484
|
+
body: JSON.stringify(body ?? {}),
|
|
10485
|
+
})
|
|
10486
|
+
} catch (error) {
|
|
10487
|
+
return { ok: false, code: 'network', message: String(error?.message ?? error) }
|
|
10488
|
+
}
|
|
10489
|
+
const parsed = await response.json().catch(() => null)
|
|
10490
|
+
if (parsed === null) return { ok: false, code: `http-${response.status}`, message: '' }
|
|
10491
|
+
if (parsed.ok !== true) {
|
|
10492
|
+
return {
|
|
10493
|
+
ok: false,
|
|
10494
|
+
code: parsed.error?.code ?? 'capture-failed',
|
|
10495
|
+
message: parsed.error?.message ?? '',
|
|
10496
|
+
}
|
|
10497
|
+
}
|
|
10498
|
+
return { ok: true, value: parsed.value }
|
|
10499
|
+
}
|
|
10500
|
+
|
|
10501
|
+
/** Ask the OS to open the place where screen recording is granted. */
|
|
10502
|
+
async function ideOpenScreenPermission() {
|
|
10503
|
+
let response
|
|
10504
|
+
try {
|
|
10505
|
+
response = await fetch(IDE_SHOT_PERMISSION_ROUTE, {
|
|
10506
|
+
method: 'POST',
|
|
10507
|
+
headers: { 'content-type': 'application/json' },
|
|
10508
|
+
body: '{}',
|
|
10509
|
+
})
|
|
10510
|
+
} catch {
|
|
10511
|
+
return { opened: false }
|
|
10512
|
+
}
|
|
10513
|
+
const parsed = await response.json().catch(() => null)
|
|
10514
|
+
return parsed?.ok === true ? parsed.value : { opened: false }
|
|
10515
|
+
}
|
|
10516
|
+
|
|
10517
|
+
/** The image inside a `data:image/…;base64,…` URL as a `File`. */
|
|
10518
|
+
function ideShotFile(dataUrl, name = 'screenshot.jpg') {
|
|
10519
|
+
const comma = dataUrl.indexOf(',')
|
|
10520
|
+
const head = dataUrl.slice(0, comma)
|
|
10521
|
+
const type = /^data:(image\/[a-z+]+)/.exec(head)?.[1] ?? 'image/jpeg'
|
|
10522
|
+
const binary = atob(dataUrl.slice(comma + 1))
|
|
10523
|
+
const bytes = new Uint8Array(binary.length)
|
|
10524
|
+
for (let index = 0; index < binary.length; index += 1) bytes[index] = binary.charCodeAt(index)
|
|
10525
|
+
return new File([bytes], name, { type })
|
|
10526
|
+
}
|
|
10527
|
+
|
|
10528
|
+
/**
|
|
10529
|
+
* Paste the image into the conversation composer.
|
|
10530
|
+
*
|
|
10531
|
+
* Only a paste is dispatched — no editor mutation, no `@` mention flow — so the
|
|
10532
|
+
* draft keeps whatever the user already typed or attached. Returns `false` when
|
|
10533
|
+
* no composer is on screen, which the caller degrades to "copy it and paste".
|
|
10534
|
+
*/
|
|
10535
|
+
async function idePasteScreenshot(dataUrl) {
|
|
10536
|
+
const findEditor = () => (typeof document === 'undefined' ? null : document.querySelector('[data-lexical-editor="true"]'))
|
|
10537
|
+
const deadline = Date.now() + 2000
|
|
10538
|
+
let editor = findEditor()
|
|
10539
|
+
while (editor === null && Date.now() < deadline) {
|
|
10540
|
+
await new Promise((resolve) => setTimeout(resolve, 40))
|
|
10541
|
+
editor = findEditor()
|
|
10542
|
+
}
|
|
10543
|
+
if (editor === null) return false
|
|
10544
|
+
const file = ideShotFile(dataUrl)
|
|
10545
|
+
const transfer = new DataTransfer()
|
|
10546
|
+
transfer.items.add(file)
|
|
10547
|
+
editor.focus()
|
|
10548
|
+
editor.dispatchEvent(new ClipboardEvent('paste', { clipboardData: transfer, bubbles: true, cancelable: true }))
|
|
10549
|
+
return true
|
|
10550
|
+
}
|
|
10551
|
+
|
|
10552
|
+
// ------------------------------------------------------------------ geometry
|
|
10553
|
+
// Pure helpers: the editor converts pointer positions into image pixels, keeps
|
|
10554
|
+
// a selection inside the capture and resizes it from one of eight handles.
|
|
10555
|
+
|
|
10556
|
+
/** Scale that fits `width × height` into the given box, never upscaling. */
|
|
10557
|
+
function ideShotFitScale(width, height, boxWidth, boxHeight) {
|
|
10558
|
+
if (!(width > 0) || !(height > 0) || !(boxWidth > 0) || !(boxHeight > 0)) return 1
|
|
10559
|
+
return Math.min(boxWidth / width, boxHeight / height, 1)
|
|
10560
|
+
}
|
|
10561
|
+
|
|
10562
|
+
/** Keep a rectangle inside `bounds` without changing its size (unless it must shrink). */
|
|
10563
|
+
function ideShotClampSelection(rect, bounds) {
|
|
10564
|
+
const width = Math.min(rect.width, bounds.width)
|
|
10565
|
+
const height = Math.min(rect.height, bounds.height)
|
|
10566
|
+
const x = Math.min(Math.max(rect.x, 0), Math.max(bounds.width - width, 0))
|
|
10567
|
+
const y = Math.min(Math.max(rect.y, 0), Math.max(bounds.height - height, 0))
|
|
10568
|
+
return { x, y, width, height }
|
|
10569
|
+
}
|
|
10570
|
+
|
|
10571
|
+
/** A rectangle from two dragged corners, normalized and clamped to the image. */
|
|
10572
|
+
function ideShotRectFromPoints(start, end, bounds) {
|
|
10573
|
+
const x = Math.min(start.x, end.x)
|
|
10574
|
+
const y = Math.min(start.y, end.y)
|
|
10575
|
+
const width = Math.abs(end.x - start.x)
|
|
10576
|
+
const height = Math.abs(end.y - start.y)
|
|
10577
|
+
return ideShotClampSelection({ x, y, width, height }, bounds)
|
|
10578
|
+
}
|
|
10579
|
+
|
|
10580
|
+
/** The eight resize handles of a rectangle, as `{ id, x, y }` in image pixels. */
|
|
10581
|
+
function ideShotHandles(rect) {
|
|
10582
|
+
const midX = rect.x + rect.width / 2
|
|
10583
|
+
const midY = rect.y + rect.height / 2
|
|
10584
|
+
const right = rect.x + rect.width
|
|
10585
|
+
const bottom = rect.y + rect.height
|
|
10586
|
+
return [
|
|
10587
|
+
{ id: 'nw', x: rect.x, y: rect.y },
|
|
10588
|
+
{ id: 'n', x: midX, y: rect.y },
|
|
10589
|
+
{ id: 'ne', x: right, y: rect.y },
|
|
10590
|
+
{ id: 'e', x: right, y: midY },
|
|
10591
|
+
{ id: 'se', x: right, y: bottom },
|
|
10592
|
+
{ id: 's', x: midX, y: bottom },
|
|
10593
|
+
{ id: 'sw', x: rect.x, y: bottom },
|
|
10594
|
+
{ id: 'w', x: rect.x, y: midY },
|
|
10595
|
+
]
|
|
10596
|
+
}
|
|
10597
|
+
|
|
10598
|
+
/** Whether a point is inside `rect`, expanded by `slack` on every side. */
|
|
10599
|
+
function ideShotHitsRect(point, rect, slack) {
|
|
10600
|
+
const pad = Number.isFinite(slack) ? slack : 0
|
|
10601
|
+
return point.x >= rect.x - pad && point.x <= rect.x + rect.width + pad
|
|
10602
|
+
&& point.y >= rect.y - pad && point.y <= rect.y + rect.height + pad
|
|
10603
|
+
}
|
|
10604
|
+
|
|
10605
|
+
/** Resize `rect` by dragging one handle to `point`, keeping it valid and in bounds. */
|
|
10606
|
+
function ideShotResize(rect, handle, point, bounds) {
|
|
10607
|
+
let left = rect.x
|
|
10608
|
+
let top = rect.y
|
|
10609
|
+
let right = rect.x + rect.width
|
|
10610
|
+
let bottom = rect.y + rect.height
|
|
10611
|
+
if (handle.includes('w')) left = point.x
|
|
10612
|
+
if (handle.includes('e')) right = point.x
|
|
10613
|
+
if (handle.includes('n')) top = point.y
|
|
10614
|
+
if (handle.includes('s')) bottom = point.y
|
|
10615
|
+
const x = Math.min(left, right)
|
|
10616
|
+
const y = Math.min(top, bottom)
|
|
10617
|
+
const width = Math.max(Math.abs(right - left), IDE_SHOT_MIN_SELECTION / 4)
|
|
10618
|
+
const height = Math.max(Math.abs(bottom - top), IDE_SHOT_MIN_SELECTION / 4)
|
|
10619
|
+
return ideShotClampSelection({ x, y, width, height }, bounds)
|
|
10620
|
+
}
|
|
10621
|
+
|
|
10622
|
+
/**
|
|
10623
|
+
* Pointer position in image pixels.
|
|
10624
|
+
*
|
|
10625
|
+
* The capture is centred inside the stage, so the offset of the drawn canvas
|
|
10626
|
+
* within the stage is half of the leftover space; `bounds` is the image size
|
|
10627
|
+
* (the stage element itself has no width/height of its own).
|
|
10628
|
+
*/
|
|
10629
|
+
function ideShotPoint(event, stage, scale, bounds) {
|
|
10630
|
+
const box = stage.getBoundingClientRect()
|
|
10631
|
+
const drawnWidth = bounds.width * scale
|
|
10632
|
+
const drawnHeight = bounds.height * scale
|
|
10633
|
+
const x = (event.clientX - box.left - (box.width - drawnWidth) / 2) / scale
|
|
10634
|
+
const y = (event.clientY - box.top - (box.height - drawnHeight) / 2) / scale
|
|
10635
|
+
return {
|
|
10636
|
+
x: Math.max(0, Math.min(Number.isFinite(x) ? x : 0, bounds.width)),
|
|
10637
|
+
y: Math.max(0, Math.min(Number.isFinite(y) ? y : 0, bounds.height)),
|
|
10638
|
+
}
|
|
10639
|
+
}
|
|
10640
|
+
|
|
10641
|
+
// -------------------------------------------------------------------- drawing
|
|
10642
|
+
|
|
10643
|
+
/** Draw one stored operation onto a context that already shows the capture. */
|
|
10644
|
+
function ideShotDrawOp(ctx, op, image) {
|
|
10645
|
+
const color = op.color ?? '#ff4d4f'
|
|
10646
|
+
// `stroke` is the pen width; `width`/`height` are the rectangle being drawn.
|
|
10647
|
+
const stroke = op.stroke ?? 4
|
|
10648
|
+
ctx.save()
|
|
10649
|
+
ctx.strokeStyle = color
|
|
10650
|
+
ctx.fillStyle = color
|
|
10651
|
+
ctx.lineWidth = stroke
|
|
10652
|
+
ctx.lineCap = 'round'
|
|
10653
|
+
ctx.lineJoin = 'round'
|
|
10654
|
+
if (op.tool === 'rect') {
|
|
10655
|
+
ctx.strokeRect(op.x, op.y, op.width, op.height)
|
|
10656
|
+
} else if (op.tool === 'ellipse') {
|
|
10657
|
+
ctx.beginPath()
|
|
10658
|
+
ctx.ellipse(op.x + op.width / 2, op.y + op.height / 2, Math.abs(op.width / 2), Math.abs(op.height / 2), 0, 0, Math.PI * 2)
|
|
10659
|
+
ctx.stroke()
|
|
10660
|
+
} else if (op.tool === 'arrow') {
|
|
10661
|
+
const x1 = op.x
|
|
10662
|
+
const y1 = op.y
|
|
10663
|
+
const x2 = op.x + op.width
|
|
10664
|
+
const y2 = op.y + op.height
|
|
10665
|
+
const angle = Math.atan2(y2 - y1, x2 - x1)
|
|
10666
|
+
const head = Math.max(10, stroke * 4)
|
|
10667
|
+
ctx.beginPath()
|
|
10668
|
+
ctx.moveTo(x1, y1)
|
|
10669
|
+
ctx.lineTo(x2, y2)
|
|
10670
|
+
ctx.stroke()
|
|
10671
|
+
ctx.beginPath()
|
|
10672
|
+
ctx.moveTo(x2, y2)
|
|
10673
|
+
ctx.lineTo(x2 - head * Math.cos(angle - Math.PI / 7), y2 - head * Math.sin(angle - Math.PI / 7))
|
|
10674
|
+
ctx.lineTo(x2 - head * Math.cos(angle + Math.PI / 7), y2 - head * Math.sin(angle + Math.PI / 7))
|
|
10675
|
+
ctx.closePath()
|
|
10676
|
+
ctx.fill()
|
|
10677
|
+
} else if (op.tool === 'pen') {
|
|
10678
|
+
const points = op.points ?? []
|
|
10679
|
+
if (points.length > 1) {
|
|
10680
|
+
ctx.beginPath()
|
|
10681
|
+
ctx.moveTo(points[0].x, points[0].y)
|
|
10682
|
+
for (const point of points.slice(1)) ctx.lineTo(point.x, point.y)
|
|
10683
|
+
ctx.stroke()
|
|
10684
|
+
}
|
|
10685
|
+
} else if (op.tool === 'text') {
|
|
10686
|
+
ctx.font = `${Math.max(12, stroke * 5)}px -apple-system, "Segoe UI", "PingFang SC", sans-serif`
|
|
10687
|
+
ctx.textBaseline = 'top'
|
|
10688
|
+
// A dark halo keeps the label readable on light and dark captures alike.
|
|
10689
|
+
ctx.lineWidth = Math.max(2, stroke / 2)
|
|
10690
|
+
ctx.strokeStyle = 'rgba(0, 0, 0, 0.55)'
|
|
10691
|
+
ctx.strokeText(op.text ?? '', op.x, op.y)
|
|
10692
|
+
ctx.fillStyle = color
|
|
10693
|
+
ctx.fillText(op.text ?? '', op.x, op.y)
|
|
10694
|
+
} else if (op.tool === 'mosaic') {
|
|
10695
|
+
const x = Math.min(op.x, op.x + op.width)
|
|
10696
|
+
const y = Math.min(op.y, op.y + op.height)
|
|
10697
|
+
const w = Math.abs(op.width)
|
|
10698
|
+
const h = Math.abs(op.height)
|
|
10699
|
+
if (w > 2 && h > 2 && image !== undefined) {
|
|
10700
|
+
const block = Math.max(6, Math.round(Math.min(w, h) / 12))
|
|
10701
|
+
const small = document.createElement('canvas')
|
|
10702
|
+
small.width = Math.max(1, Math.round(w / block))
|
|
10703
|
+
small.height = Math.max(1, Math.round(h / block))
|
|
10704
|
+
const smallCtx = small.getContext('2d')
|
|
10705
|
+
smallCtx.imageSmoothingEnabled = true
|
|
10706
|
+
smallCtx.drawImage(image, x, y, w, h, 0, 0, small.width, small.height)
|
|
10707
|
+
ctx.imageSmoothingEnabled = false
|
|
10708
|
+
ctx.drawImage(small, 0, 0, small.width, small.height, x, y, w, h)
|
|
10709
|
+
}
|
|
10710
|
+
}
|
|
10711
|
+
ctx.restore()
|
|
10712
|
+
}
|
|
10713
|
+
|
|
10714
|
+
/** Redraw the whole editor frame: capture, dimmed outside, marks, selection. */
|
|
10715
|
+
function ideShotPaint(canvas, image, ops, draft, selection, showSelection) {
|
|
10716
|
+
const ctx = canvas.getContext('2d')
|
|
10717
|
+
if (ctx === null || image === null) return
|
|
10718
|
+
ctx.setTransform(1, 0, 0, 1, 0, 0)
|
|
10719
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height)
|
|
10720
|
+
ctx.drawImage(image, 0, 0, canvas.width, canvas.height)
|
|
10721
|
+
for (const op of ops) ideShotDrawOp(ctx, op, image)
|
|
10722
|
+
if (draft !== null) ideShotDrawOp(ctx, draft, image)
|
|
10723
|
+
if (selection === null) {
|
|
10724
|
+
// Nothing framed yet: dim the whole capture so the hint reads as a mode.
|
|
10725
|
+
ctx.save()
|
|
10726
|
+
ctx.fillStyle = 'rgba(0, 0, 0, 0.35)'
|
|
10727
|
+
ctx.fillRect(0, 0, canvas.width, canvas.height)
|
|
10728
|
+
ctx.restore()
|
|
10729
|
+
} else if (showSelection) {
|
|
10730
|
+
const { x, y, width, height } = selection
|
|
10731
|
+
ctx.save()
|
|
10732
|
+
ctx.fillStyle = 'rgba(0, 0, 0, 0.45)'
|
|
10733
|
+
ctx.beginPath()
|
|
10734
|
+
ctx.rect(0, 0, canvas.width, canvas.height)
|
|
10735
|
+
ctx.rect(x, y, width, height)
|
|
10736
|
+
ctx.fill('evenodd')
|
|
10737
|
+
ctx.strokeStyle = '#4f8cff'
|
|
10738
|
+
ctx.lineWidth = Math.max(1, canvas.width / 900)
|
|
10739
|
+
ctx.strokeRect(x, y, width, height)
|
|
10740
|
+
for (const handle of ideShotHandles(selection)) {
|
|
10741
|
+
const size = Math.max(6, canvas.width / 160)
|
|
10742
|
+
ctx.fillStyle = '#ffffff'
|
|
10743
|
+
ctx.strokeStyle = '#4f8cff'
|
|
10744
|
+
ctx.lineWidth = Math.max(1, canvas.width / 900)
|
|
10745
|
+
ctx.beginPath()
|
|
10746
|
+
ctx.rect(handle.x - size / 2, handle.y - size / 2, size, size)
|
|
10747
|
+
ctx.fill()
|
|
10748
|
+
ctx.stroke()
|
|
10749
|
+
}
|
|
10750
|
+
ctx.restore()
|
|
10751
|
+
}
|
|
10752
|
+
}
|
|
10753
|
+
|
|
10754
|
+
/**
|
|
10755
|
+
* Crop the selection, redraw the marks and encode within the budget.
|
|
10756
|
+
*
|
|
10757
|
+
* The ladder stops at the first size that fits, so a flat capture keeps its
|
|
10758
|
+
* quality while a text-heavy one steps down instead of blowing the budget.
|
|
10759
|
+
*/
|
|
10760
|
+
function ideShotExport(image, ops, selection) {
|
|
10761
|
+
const sourceX = Math.max(0, Math.round(selection.x))
|
|
10762
|
+
const sourceY = Math.max(0, Math.round(selection.y))
|
|
10763
|
+
const sourceWidth = Math.max(1, Math.round(Math.min(selection.width, image.width - sourceX)))
|
|
10764
|
+
const sourceHeight = Math.max(1, Math.round(Math.min(selection.height, image.height - sourceY)))
|
|
10765
|
+
const scale = Math.min(1, IDE_SHOT_EXPORT_EDGE / Math.max(sourceWidth, sourceHeight))
|
|
10766
|
+
const canvas = document.createElement('canvas')
|
|
10767
|
+
canvas.width = Math.max(1, Math.round(sourceWidth * scale))
|
|
10768
|
+
canvas.height = Math.max(1, Math.round(sourceHeight * scale))
|
|
10769
|
+
const ctx = canvas.getContext('2d')
|
|
10770
|
+
if (ctx === null) return ''
|
|
10771
|
+
ctx.drawImage(image, sourceX, sourceY, sourceWidth, sourceHeight, 0, 0, canvas.width, canvas.height)
|
|
10772
|
+
ctx.save()
|
|
10773
|
+
ctx.scale(scale, scale)
|
|
10774
|
+
ctx.translate(-sourceX, -sourceY)
|
|
10775
|
+
for (const op of ops) ideShotDrawOp(ctx, op, image)
|
|
10776
|
+
ctx.restore()
|
|
10777
|
+
let smallest = canvas.toDataURL('image/jpeg', IDE_SHOT_QUALITIES[IDE_SHOT_QUALITIES.length - 1])
|
|
10778
|
+
for (const quality of IDE_SHOT_QUALITIES) {
|
|
10779
|
+
const url = canvas.toDataURL('image/jpeg', quality)
|
|
10780
|
+
if (url.length * 0.75 <= IDE_SHOT_BUDGET) return url
|
|
10781
|
+
if (url.length < smallest.length) smallest = url
|
|
10782
|
+
}
|
|
10783
|
+
return smallest
|
|
10784
|
+
}
|
|
10785
|
+
|
|
10786
|
+
// ------------------------------------------------------------------ component
|
|
10787
|
+
|
|
10788
|
+
/** One toolbar button. */
|
|
10789
|
+
function IdeShotToolButton({ active, label, title, onClick }) {
|
|
10790
|
+
return h('button', {
|
|
10791
|
+
type: 'button',
|
|
10792
|
+
className: `iw-shot-tool${active ? ' on' : ''}`,
|
|
10793
|
+
title,
|
|
10794
|
+
onClick,
|
|
10795
|
+
}, label)
|
|
10796
|
+
}
|
|
10797
|
+
|
|
10798
|
+
/**
|
|
10799
|
+
* The editor overlay.
|
|
10800
|
+
*
|
|
10801
|
+
* Rendered through `shell.overlay`, so it covers the window regardless of which
|
|
10802
|
+
* column is showing and is never clipped by the panel's overflow.
|
|
10803
|
+
*/
|
|
10804
|
+
function IdeScreenshotEditor({ store, tr, onInsert, onCopy, onOpenSettings, onClose }) {
|
|
10805
|
+
const state = useSyncExternalStore(store.subscribe, store.getState, store.getState)
|
|
10806
|
+
const shot = state.screenshot ?? null
|
|
10807
|
+
const editing = shot?.phase === 'editing'
|
|
10808
|
+
const [image, setImage] = useState(null)
|
|
10809
|
+
const [tool, setTool] = useState('select')
|
|
10810
|
+
const [color, setColor] = useState(IDE_SHOT_COLORS[0])
|
|
10811
|
+
const [width, setWidth] = useState(IDE_SHOT_WIDTHS[1])
|
|
10812
|
+
const [ops, setOps] = useState([])
|
|
10813
|
+
const [selection, setSelection] = useState(null)
|
|
10814
|
+
/** True while the frame is being dragged: the card only shows once released. */
|
|
10815
|
+
const [dragging, setDragging] = useState(false)
|
|
10816
|
+
const [draft, setDraft] = useState(null)
|
|
10817
|
+
const [textAt, setTextAt] = useState(null)
|
|
10818
|
+
const [textValue, setTextValue] = useState('')
|
|
10819
|
+
const [scale, setScale] = useState(1)
|
|
10820
|
+
const [stageRect, setStageRect] = useState(null)
|
|
10821
|
+
const canvasRef = useRef(null)
|
|
10822
|
+
const frameRef = useRef(null)
|
|
10823
|
+
const dragRef = useRef(null)
|
|
10824
|
+
|
|
10825
|
+
// Load the capture into an <img>: the canvas needs natural pixels, and the
|
|
10826
|
+
// selection is expressed in those pixels.
|
|
10827
|
+
useEffect(() => {
|
|
10828
|
+
if (!editing) {
|
|
10829
|
+
setImage(null)
|
|
10830
|
+
return
|
|
10831
|
+
}
|
|
10832
|
+
setOps([])
|
|
10833
|
+
setDraft(null)
|
|
10834
|
+
setTool('select')
|
|
10835
|
+
setTextAt(null)
|
|
10836
|
+
setSelection(null)
|
|
10837
|
+
const element = new Image()
|
|
10838
|
+
element.onload = () => setImage(element)
|
|
10839
|
+
element.src = shot.dataUrl
|
|
10840
|
+
}, [editing, shot?.dataUrl])
|
|
10841
|
+
|
|
10842
|
+
// Fit the capture into the window and keep it fitted across resizes.
|
|
10843
|
+
useEffect(() => {
|
|
10844
|
+
if (image === null) return
|
|
10845
|
+
const fit = () => {
|
|
10846
|
+
const box = { width: Math.max(320, globalThis.innerWidth - 96), height: Math.max(240, globalThis.innerHeight - 180) }
|
|
10847
|
+
setScale(ideShotFitScale(image.naturalWidth, image.naturalHeight, box.width, box.height))
|
|
10848
|
+
}
|
|
10849
|
+
fit()
|
|
10850
|
+
globalThis.addEventListener?.('resize', fit)
|
|
10851
|
+
return () => globalThis.removeEventListener?.('resize', fit)
|
|
10852
|
+
}, [image])
|
|
10853
|
+
|
|
10854
|
+
// Repaint whenever anything visible changed.
|
|
10855
|
+
useEffect(() => {
|
|
10856
|
+
const canvas = canvasRef.current
|
|
10857
|
+
if (canvas === null || image === null) return
|
|
10858
|
+
canvas.width = image.naturalWidth
|
|
10859
|
+
canvas.height = image.naturalHeight
|
|
10860
|
+
ideShotPaint(canvas, image, ops, draft, selection, selection !== null)
|
|
10861
|
+
}, [image, ops, draft, selection, tool])
|
|
10862
|
+
|
|
10863
|
+
// Undo from anywhere: the overlay never holds focus, so the key is read at
|
|
10864
|
+
// the window instead of relying on a focused element.
|
|
10865
|
+
useEffect(() => {
|
|
10866
|
+
if (!editing) return undefined
|
|
10867
|
+
const onKey = (event) => {
|
|
10868
|
+
if (!(event.metaKey || event.ctrlKey) || event.key.toLowerCase() !== 'z') return
|
|
10869
|
+
event.preventDefault()
|
|
10870
|
+
setOps((list) => list.slice(0, -1))
|
|
10871
|
+
}
|
|
10872
|
+
globalThis.addEventListener?.('keydown', onKey, true)
|
|
10873
|
+
return () => globalThis.removeEventListener?.('keydown', onKey, true)
|
|
10874
|
+
}, [editing])
|
|
10875
|
+
|
|
10876
|
+
// Where the canvas sits on screen: the popup card is anchored to the frame.
|
|
10877
|
+
const layoutKey = selection === null ? '' : `${selection.x},${selection.y},${selection.width},${selection.height}`
|
|
10878
|
+
useEffect(() => {
|
|
10879
|
+
const canvas = canvasRef.current
|
|
10880
|
+
if (canvas === null || image === null) {
|
|
10881
|
+
setStageRect(null)
|
|
10882
|
+
return
|
|
10883
|
+
}
|
|
10884
|
+
const rect = canvas.getBoundingClientRect()
|
|
10885
|
+
setStageRect({ left: rect.left, top: rect.top, width: rect.width, height: rect.height })
|
|
10886
|
+
}, [image, scale, layoutKey])
|
|
10887
|
+
|
|
10888
|
+
const bounds = image === null
|
|
10889
|
+
? { width: 1, height: 1 }
|
|
10890
|
+
: { width: image.naturalWidth, height: image.naturalHeight }
|
|
10891
|
+
|
|
10892
|
+
/** Whether a point may carry a mark (a mark needs a framed area). */
|
|
10893
|
+
const canMark = selection !== null && ideShotHitsRect
|
|
10894
|
+
|
|
10895
|
+
const beginSelection = (point) => {
|
|
10896
|
+
dragRef.current = { mode: 'create', origin: point }
|
|
10897
|
+
setDragging(true)
|
|
10898
|
+
setSelection({ x: point.x, y: point.y, width: 0, height: 0 })
|
|
10899
|
+
}
|
|
10900
|
+
|
|
10901
|
+
/**
|
|
10902
|
+
* Pointer down: dragging on empty space always draws a new frame — that is
|
|
10903
|
+
* the first thing the user does after the shortcut. Inside an existing frame
|
|
10904
|
+
* the active tool decides (range adjust, rectangle, or text).
|
|
10905
|
+
*/
|
|
10906
|
+
const onPointerDown = (event) => {
|
|
10907
|
+
if (image === null) return
|
|
10908
|
+
const frame = frameRef.current
|
|
10909
|
+
if (frame === null) return
|
|
10910
|
+
const point = ideShotPoint(event, frame, scale, bounds)
|
|
10911
|
+
try {
|
|
10912
|
+
frame.setPointerCapture?.(event.pointerId)
|
|
10913
|
+
} catch {
|
|
10914
|
+
/* a synthetic or already-captured pointer: dragging still works */
|
|
10915
|
+
}
|
|
10916
|
+
const inside = selection !== null && selection.width > 0 && ideShotHitsRect(point, selection, 0)
|
|
10917
|
+
if (!inside) {
|
|
10918
|
+
beginSelection(point)
|
|
10919
|
+
return
|
|
10920
|
+
}
|
|
10921
|
+
if (tool === 'text') {
|
|
10922
|
+
setTextAt(point)
|
|
10923
|
+
setTextValue('')
|
|
10924
|
+
return
|
|
10925
|
+
}
|
|
10926
|
+
if (tool === 'rect') {
|
|
10927
|
+
setDraft({ tool, color, stroke: width, x: point.x, y: point.y, width: 0, height: 0, points: [point] })
|
|
10928
|
+
return
|
|
10929
|
+
}
|
|
10930
|
+
// Range tool: handles first, then the body.
|
|
10931
|
+
const handle = ideShotHandles(selection).find((entry) => (
|
|
10932
|
+
Math.abs(entry.x - point.x) <= 12 / scale && Math.abs(entry.y - point.y) <= 12 / scale
|
|
10933
|
+
))
|
|
10934
|
+
if (handle !== undefined) {
|
|
10935
|
+
dragRef.current = { mode: 'resize', handle: handle.id }
|
|
10936
|
+
return
|
|
10937
|
+
}
|
|
10938
|
+
dragRef.current = { mode: 'move', origin: point, start: selection }
|
|
10939
|
+
}
|
|
10940
|
+
|
|
10941
|
+
const onPointerMove = (event) => {
|
|
10942
|
+
const drag = dragRef.current
|
|
10943
|
+
const frame = frameRef.current
|
|
10944
|
+
if (frame === null) return
|
|
10945
|
+
const point = ideShotPoint(event, frame, scale, bounds)
|
|
10946
|
+
if (drag !== null) {
|
|
10947
|
+
if (drag.mode === 'move') {
|
|
10948
|
+
const dx = point.x - drag.origin.x
|
|
10949
|
+
const dy = point.y - drag.origin.y
|
|
10950
|
+
setSelection(ideShotClampSelection({
|
|
10951
|
+
x: drag.start.x + dx,
|
|
10952
|
+
y: drag.start.y + dy,
|
|
10953
|
+
width: drag.start.width,
|
|
10954
|
+
height: drag.start.height,
|
|
10955
|
+
}, bounds))
|
|
10956
|
+
return
|
|
10957
|
+
}
|
|
10958
|
+
if (drag.mode === 'resize') {
|
|
10959
|
+
setSelection(ideShotResize(selection, drag.handle, point, bounds))
|
|
10960
|
+
return
|
|
10961
|
+
}
|
|
10962
|
+
setSelection(ideShotRectFromPoints(drag.origin, point, bounds))
|
|
10963
|
+
return
|
|
10964
|
+
}
|
|
10965
|
+
if (draft !== null) {
|
|
10966
|
+
const next = { ...draft }
|
|
10967
|
+
if (draft.tool === 'pen') next.points = [...draft.points, point]
|
|
10968
|
+
else {
|
|
10969
|
+
next.width = point.x - draft.x
|
|
10970
|
+
next.height = point.y - draft.y
|
|
10971
|
+
}
|
|
10972
|
+
setDraft(next)
|
|
10973
|
+
}
|
|
10974
|
+
}
|
|
10975
|
+
|
|
10976
|
+
const onPointerUp = () => {
|
|
10977
|
+
const drag = dragRef.current
|
|
10978
|
+
dragRef.current = null
|
|
10979
|
+
setDragging(false)
|
|
10980
|
+
if (draft !== null) {
|
|
10981
|
+
const op = draft.tool === 'pen'
|
|
10982
|
+
? { ...draft }
|
|
10983
|
+
: {
|
|
10984
|
+
...draft,
|
|
10985
|
+
x: Math.min(draft.x, draft.x + draft.width),
|
|
10986
|
+
y: Math.min(draft.y, draft.y + draft.height),
|
|
10987
|
+
width: Math.abs(draft.width),
|
|
10988
|
+
height: Math.abs(draft.height),
|
|
10989
|
+
}
|
|
10990
|
+
setDraft(null)
|
|
10991
|
+
if (op.tool === 'pen' ? (op.points?.length ?? 0) > 1 : (op.width > 2 || op.height > 2)) {
|
|
10992
|
+
setOps((list) => [...list, op])
|
|
10993
|
+
}
|
|
10994
|
+
return
|
|
10995
|
+
}
|
|
10996
|
+
if (drag?.mode === 'create' && selection !== null) {
|
|
10997
|
+
// A click (or a stray twitch) is not a frame: drop it and stay unframed.
|
|
10998
|
+
if (selection.width < IDE_SHOT_MIN_SELECTION || selection.height < IDE_SHOT_MIN_SELECTION) {
|
|
10999
|
+
setSelection(null)
|
|
11000
|
+
}
|
|
11001
|
+
}
|
|
11002
|
+
}
|
|
11003
|
+
|
|
11004
|
+
const commitText = () => {
|
|
11005
|
+
if (textAt === null || textValue.trim() === '') {
|
|
11006
|
+
setTextAt(null)
|
|
11007
|
+
return
|
|
11008
|
+
}
|
|
11009
|
+
setOps((list) => [...list, { tool: 'text', x: textAt.x, y: textAt.y, text: textValue.trim(), color, stroke: width }])
|
|
11010
|
+
setTextAt(null)
|
|
11011
|
+
setTextValue('')
|
|
11012
|
+
}
|
|
11013
|
+
|
|
11014
|
+
const undo = () => setOps((list) => list.slice(0, -1))
|
|
11015
|
+
const exportImage = () => (selection === null || image === null ? '' : ideShotExport(image, ops, selection))
|
|
11016
|
+
|
|
11017
|
+
/**
|
|
11018
|
+
* Esc leaves the whole operation — before the first press, and also in the
|
|
11019
|
+
* middle of dragging a frame (the pointer capture is dropped so the "up"
|
|
11020
|
+
* that follows cannot revive it). A text box open eats the first Esc.
|
|
11021
|
+
*
|
|
11022
|
+
* The listener is on `window` in the capture phase: the overlay never takes
|
|
11023
|
+
* focus, and the shell handles Esc for its own panels.
|
|
11024
|
+
*/
|
|
11025
|
+
useEffect(() => {
|
|
11026
|
+
if (!editing) return undefined
|
|
11027
|
+
const onKey = (event) => {
|
|
11028
|
+
if (event.key !== 'Escape') return
|
|
11029
|
+
if (textAt !== null) return
|
|
11030
|
+
event.preventDefault()
|
|
11031
|
+
event.stopPropagation()
|
|
11032
|
+
dragRef.current = null
|
|
11033
|
+
setDragging(false)
|
|
11034
|
+
setDraft(null)
|
|
11035
|
+
onClose()
|
|
11036
|
+
}
|
|
11037
|
+
globalThis.addEventListener?.('keydown', onKey, true)
|
|
11038
|
+
return () => globalThis.removeEventListener?.('keydown', onKey, true)
|
|
11039
|
+
}, [editing, textAt, onClose])
|
|
11040
|
+
|
|
11041
|
+
if (shot === null) return null
|
|
11042
|
+
|
|
11043
|
+
// The editor renders through `shell.overlay`, i.e. outside the panel: the
|
|
11044
|
+
// workspace stylesheet is only mounted by the panel components, so without
|
|
11045
|
+
// this the card falls back to browser-default buttons (white) and fonts.
|
|
11046
|
+
const styled = (node) => h(React.Fragment, null, [h(IdeStyles, { key: 'style' }), node])
|
|
11047
|
+
const palette = ideIsThemeId(state.theme) ? { 'data-dsh-theme': state.theme } : {}
|
|
11048
|
+
|
|
11049
|
+
// Typography follows the user's own settings; the variables are set here
|
|
11050
|
+
// because `.iw-root` (which normally sets them) is not an ancestor.
|
|
11051
|
+
const skin = {
|
|
11052
|
+
fontFamily: 'ui-sans-serif, system-ui, -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif',
|
|
11053
|
+
fontSize: `${Number.isFinite(state.treeFontSize) ? state.treeFontSize : 14.5}px`,
|
|
11054
|
+
'--iw-code-font-size': `${Number.isFinite(state.codeFontSize) ? state.codeFontSize : 12.5}px`,
|
|
11055
|
+
'--iw-tree-font-size': `${Number.isFinite(state.treeFontSize) ? state.treeFontSize : 14.5}px`,
|
|
11056
|
+
}
|
|
11057
|
+
|
|
11058
|
+
if (shot.phase === 'error') {
|
|
11059
|
+
const needsPermission = shot.code === 'no-screen-permission'
|
|
11060
|
+
const isMac = ideHostIsMacos()
|
|
11061
|
+
const actions = []
|
|
11062
|
+
if (needsPermission && isMac) {
|
|
11063
|
+
actions.push(h('button', { key: 'settings', type: 'button', className: 'iw-shot-primary', onClick: onOpenSettings }, tr('shotOpenSettings')))
|
|
11064
|
+
}
|
|
11065
|
+
actions.push(h('button', { key: 'close', type: 'button', className: 'iw-shot-button', onClick: onClose }, tr('setupClose')))
|
|
11066
|
+
return styled(h('div', { className: 'iw-shot-veil', style: skin, ...palette }, [
|
|
11067
|
+
h('div', { key: 'card', className: 'iw-shot' }, [
|
|
11068
|
+
h('div', { key: 'title', className: 'iw-shot-title' }, needsPermission ? tr('shotNoPermissionTitle') : tr('shotFailed')),
|
|
11069
|
+
h('div', { key: 'guide', className: 'iw-shot-guide' }, needsPermission
|
|
11070
|
+
? (isMac ? tr('shotNoPermissionMac') : tr('shotNoPermissionWindows'))
|
|
11071
|
+
: (shot.message === '' ? tr('shotFailed') : `${tr('shotFailed')}:${shot.message}`)),
|
|
11072
|
+
h('div', { key: 'actions', className: 'iw-shot-actions' }, actions),
|
|
11073
|
+
]),
|
|
11074
|
+
]))
|
|
11075
|
+
}
|
|
11076
|
+
|
|
11077
|
+
if (image === null) {
|
|
11078
|
+
return styled(h('div', { className: 'iw-shot-veil', style: skin, ...palette }, [
|
|
11079
|
+
h('div', { key: 'card', className: 'iw-shot' }, [h('div', { key: 'title', className: 'iw-shot-title' }, tr('shotBusy'))]),
|
|
11080
|
+
]))
|
|
11081
|
+
}
|
|
11082
|
+
|
|
11083
|
+
// The popup card: below the frame, or above it when the bottom is crowded.
|
|
11084
|
+
const card = (() => {
|
|
11085
|
+
if (selection === null || dragging || stageRect === null) return null
|
|
11086
|
+
const cardWidth = 392
|
|
11087
|
+
const frameLeft = stageRect.left + selection.x * scale
|
|
11088
|
+
const frameTop = stageRect.top + selection.y * scale
|
|
11089
|
+
const frameWidth = selection.width * scale
|
|
11090
|
+
const frameHeight = selection.height * scale
|
|
11091
|
+
const left = Math.max(8, Math.min(globalThis.innerWidth - cardWidth - 8, frameLeft + frameWidth / 2 - cardWidth / 2))
|
|
11092
|
+
const below = frameTop + frameHeight + 10
|
|
11093
|
+
const top = below + 44 <= globalThis.innerHeight ? below : Math.max(8, frameTop - 44)
|
|
11094
|
+
const pick = (id) => {
|
|
11095
|
+
setTool(tool === id ? 'select' : id)
|
|
11096
|
+
setTextAt(null)
|
|
11097
|
+
}
|
|
11098
|
+
return h('div', {
|
|
11099
|
+
className: 'iw-shot-card',
|
|
11100
|
+
style: { left: `${left}px`, top: `${top}px`, width: `${cardWidth}px` },
|
|
11101
|
+
onPointerDown: (event) => event.stopPropagation(),
|
|
11102
|
+
}, [
|
|
11103
|
+
h('button', {
|
|
11104
|
+
key: 'rect',
|
|
11105
|
+
type: 'button',
|
|
11106
|
+
className: `iw-btn${tool === 'rect' ? ' on' : ''}`,
|
|
11107
|
+
title: tr('shotTool_rect'),
|
|
11108
|
+
onClick: () => pick('rect'),
|
|
11109
|
+
}, tr('shotTool_rect')),
|
|
11110
|
+
h('button', {
|
|
11111
|
+
key: 'text',
|
|
11112
|
+
type: 'button',
|
|
11113
|
+
className: `iw-btn${tool === 'text' ? ' on' : ''}`,
|
|
11114
|
+
title: tr('shotTool_text'),
|
|
11115
|
+
onClick: () => pick('text'),
|
|
11116
|
+
}, tr('shotTool_text')),
|
|
11117
|
+
h('button', {
|
|
11118
|
+
key: 'undo',
|
|
11119
|
+
type: 'button',
|
|
11120
|
+
className: 'iw-btn',
|
|
11121
|
+
disabled: ops.length === 0,
|
|
11122
|
+
onClick: undo,
|
|
11123
|
+
}, tr('shotUndo')),
|
|
11124
|
+
h('span', { key: 'grow', className: 'iw-shot-grow' }),
|
|
11125
|
+
h('button', {
|
|
11126
|
+
key: 'copy',
|
|
11127
|
+
type: 'button',
|
|
11128
|
+
className: 'iw-btn',
|
|
11129
|
+
onClick: () => { const url = exportImage(); if (url !== '') onCopy(url) },
|
|
11130
|
+
}, tr('shotCopy')),
|
|
11131
|
+
h('button', {
|
|
11132
|
+
key: 'insert',
|
|
11133
|
+
type: 'button',
|
|
11134
|
+
className: 'iw-btn primary',
|
|
11135
|
+
onClick: () => { const url = exportImage(); if (url !== '') onInsert(url) },
|
|
11136
|
+
}, tr('shotJump')),
|
|
11137
|
+
])
|
|
11138
|
+
})()
|
|
11139
|
+
|
|
11140
|
+
return styled(h('div', {
|
|
11141
|
+
className: 'iw-shot-veil iw-shot-veil--editor',
|
|
11142
|
+
style: skin,
|
|
11143
|
+
...palette,
|
|
11144
|
+
tabIndex: -1,
|
|
11145
|
+
onKeyDown: (event) => {
|
|
11146
|
+
if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === 'z') {
|
|
11147
|
+
event.preventDefault()
|
|
11148
|
+
undo()
|
|
11149
|
+
}
|
|
11150
|
+
},
|
|
11151
|
+
}, [
|
|
11152
|
+
h('div', { key: 'hint', className: 'iw-shot-hint iw-shot-hint--top' }, selection === null || dragging ? tr('shotSelectHint') : tr('shotMarkHint')),
|
|
11153
|
+
h('div', {
|
|
11154
|
+
key: 'stage',
|
|
11155
|
+
ref: frameRef,
|
|
11156
|
+
className: 'iw-shot-stage',
|
|
11157
|
+
onPointerDown: onPointerDown,
|
|
11158
|
+
onPointerMove: onPointerMove,
|
|
11159
|
+
onPointerUp: onPointerUp,
|
|
11160
|
+
onPointerCancel: onPointerUp,
|
|
11161
|
+
}, [
|
|
11162
|
+
h('canvas', {
|
|
11163
|
+
key: 'canvas',
|
|
11164
|
+
ref: canvasRef,
|
|
11165
|
+
className: 'iw-shot-canvas',
|
|
11166
|
+
style: {
|
|
11167
|
+
width: `${Math.round(image.naturalWidth * scale)}px`,
|
|
11168
|
+
height: `${Math.round(image.naturalHeight * scale)}px`,
|
|
11169
|
+
},
|
|
11170
|
+
}),
|
|
11171
|
+
textAt === null ? null : h('input', {
|
|
11172
|
+
key: 'text',
|
|
11173
|
+
className: 'iw-shot-text',
|
|
11174
|
+
autoFocus: true,
|
|
11175
|
+
value: textValue,
|
|
11176
|
+
placeholder: tr('shotTextPlaceholder'),
|
|
11177
|
+
style: {
|
|
11178
|
+
left: `${textAt.x * scale}px`,
|
|
11179
|
+
top: `${textAt.y * scale}px`,
|
|
11180
|
+
},
|
|
11181
|
+
onChange: (event) => setTextValue(event.target.value),
|
|
11182
|
+
onKeyDown: (event) => {
|
|
11183
|
+
event.stopPropagation()
|
|
11184
|
+
if (event.key === 'Enter') commitText()
|
|
11185
|
+
if (event.key === 'Escape') {
|
|
11186
|
+
setTextAt(null)
|
|
11187
|
+
setTextValue('')
|
|
11188
|
+
}
|
|
11189
|
+
},
|
|
11190
|
+
}),
|
|
11191
|
+
]),
|
|
11192
|
+
card,
|
|
11193
|
+
]))
|
|
11194
|
+
}
|
|
11195
|
+
|
|
10224
11196
|
// ---- 90-apply.js ---------------------------------------------------------
|
|
10225
11197
|
/**
|
|
10226
11198
|
* Client activation.
|
|
@@ -10518,6 +11490,66 @@ function apply(ctx) {
|
|
|
10518
11490
|
return chipped === true ? 'chip' : 'plain'
|
|
10519
11491
|
}
|
|
10520
11492
|
|
|
11493
|
+
/**
|
|
11494
|
+
* Screenshot flow.
|
|
11495
|
+
*
|
|
11496
|
+
* 1. capture in the Host (OS built-in tool, no permission on Windows, Screen
|
|
11497
|
+
* Recording on macOS);
|
|
11498
|
+
* 2. show the preview with the two choices;
|
|
11499
|
+
* 3. "截图并跳转会话" returns to the conversation and *pastes* the image, so the
|
|
11500
|
+
* draft's text and attachments are untouched; "只截图" puts it on the system
|
|
11501
|
+
* clipboard and keeps the file.
|
|
11502
|
+
*/
|
|
11503
|
+
/**
|
|
11504
|
+
* Capture first, render after.
|
|
11505
|
+
*
|
|
11506
|
+
* Anything drawn before the Host grabs the screen ends up *inside* the
|
|
11507
|
+
* screenshot — an overlay that says "capturing…" would be printed onto the
|
|
11508
|
+
* picture. So the panel stays untouched until the image arrives, and a short
|
|
11509
|
+
* guard keeps a double press from firing two captures.
|
|
11510
|
+
*/
|
|
11511
|
+
let screenshotBusy = false
|
|
11512
|
+
async function startScreenshot() {
|
|
11513
|
+
if (screenshotBusy) return
|
|
11514
|
+
if (store.getState().screenshot !== null) return
|
|
11515
|
+
screenshotBusy = true
|
|
11516
|
+
try {
|
|
11517
|
+
const result = await ideShotRequest({})
|
|
11518
|
+
if (!result.ok) {
|
|
11519
|
+
store.setScreenshot({ phase: 'error', code: result.code, message: result.message })
|
|
11520
|
+
return
|
|
11521
|
+
}
|
|
11522
|
+
// The editor owns the capture from here: range, marks, export.
|
|
11523
|
+
store.setScreenshot({ phase: 'editing', ...result.value })
|
|
11524
|
+
} finally {
|
|
11525
|
+
screenshotBusy = false
|
|
11526
|
+
}
|
|
11527
|
+
}
|
|
11528
|
+
|
|
11529
|
+
/** The editor's "insert" exit: back to the chat, paste only that image. */
|
|
11530
|
+
async function screenshotInsert(dataUrl) {
|
|
11531
|
+
store.setScreenshot(null)
|
|
11532
|
+
backToChat()
|
|
11533
|
+
if (typeof dataUrl !== 'string' || dataUrl === '') return
|
|
11534
|
+
const pasted = await idePasteScreenshot(dataUrl)
|
|
11535
|
+
store.setNotice(pasted ? translate('shotAttached') : translate('shotNoEditor'))
|
|
11536
|
+
}
|
|
11537
|
+
|
|
11538
|
+
/** The editor's "copy" exit: the annotated result goes to the clipboard. */
|
|
11539
|
+
async function screenshotCopy(dataUrl) {
|
|
11540
|
+
if (typeof dataUrl !== 'string' || dataUrl === '') return
|
|
11541
|
+
const copied = await ideShotRequest({ copyDataUrl: dataUrl })
|
|
11542
|
+
store.setScreenshot(null)
|
|
11543
|
+
store.setNotice(copied.ok ? translate('shotCopied') : translate('shotFailed'))
|
|
11544
|
+
}
|
|
11545
|
+
|
|
11546
|
+
async function screenshotOpenSettings() {
|
|
11547
|
+
const result = await ideOpenScreenPermission()
|
|
11548
|
+
if (result.opened !== true) store.setNotice(translate('shotOpenFailed'))
|
|
11549
|
+
}
|
|
11550
|
+
|
|
11551
|
+
const screenshotClose = () => store.setScreenshot(null)
|
|
11552
|
+
|
|
10521
11553
|
/**
|
|
10522
11554
|
* Back to the conversation with the reference appended to the draft.
|
|
10523
11555
|
*
|
|
@@ -10590,6 +11622,7 @@ function apply(ctx) {
|
|
|
10590
11622
|
{ id: 'jump', labelKey: 'paletteActionJump', run: () => store.setJumpEnabled(!store.getState().jumpEnabled) },
|
|
10591
11623
|
{ id: 'closeTabs', labelKey: 'paletteActionCloseTabs', run: () => store.closeAllTabs() },
|
|
10592
11624
|
{ id: 'back', labelKey: 'paletteActionBack', run: () => backToChat() },
|
|
11625
|
+
{ id: 'screenshot', labelKey: 'paletteActionScreenshot', run: () => { void startScreenshot() } },
|
|
10593
11626
|
{ id: 'licenseRelogin', labelKey: 'paletteActionLicenseRelogin', run: () => { void reloginLicense() } },
|
|
10594
11627
|
{ id: 'diagnostics', labelKey: 'paletteActionDiagnostics', run: () => { void exportDiagnostics() } },
|
|
10595
11628
|
]
|
|
@@ -11097,6 +12130,7 @@ function apply(ctx) {
|
|
|
11097
12130
|
event.preventDefault()
|
|
11098
12131
|
if (command.action === 'toggle') openPanel()
|
|
11099
12132
|
else if (command.action === 'back') selectPanel(null)
|
|
12133
|
+
else if (command.action === 'screenshot') void startScreenshot()
|
|
11100
12134
|
return
|
|
11101
12135
|
}
|
|
11102
12136
|
}, true)
|
|
@@ -11150,6 +12184,24 @@ function apply(ctx) {
|
|
|
11150
12184
|
inject: () => ({ store }),
|
|
11151
12185
|
}, IdeSessionTracker)), 'dsh-ide-window: session tracker')
|
|
11152
12186
|
|
|
12187
|
+
// The screenshot editor lives at shell level: it must cover the window (not
|
|
12188
|
+
// the centre panel) and must not be clipped by the panel's overflow.
|
|
12189
|
+
ctx.effect(() => ctx.slots.inject('shell.overlay', () => ctx.slots.register({
|
|
12190
|
+
name: 'shell.overlay',
|
|
12191
|
+
id: `${IDE_NS}:screenshot`,
|
|
12192
|
+
order: 41,
|
|
12193
|
+
registrant: IDE_NS,
|
|
12194
|
+
locale: IDE_NS,
|
|
12195
|
+
inject: () => ({
|
|
12196
|
+
store,
|
|
12197
|
+
tr: translate,
|
|
12198
|
+
onInsert: screenshotInsert,
|
|
12199
|
+
onCopy: screenshotCopy,
|
|
12200
|
+
onOpenSettings: screenshotOpenSettings,
|
|
12201
|
+
onClose: screenshotClose,
|
|
12202
|
+
}),
|
|
12203
|
+
}, IdeScreenshotEditor)), 'dsh-ide-window: screenshot editor')
|
|
12204
|
+
|
|
11153
12205
|
|
|
11154
12206
|
/**
|
|
11155
12207
|
* The right-Sidebar page type and its body.
|