dsh-gallery 0.14.4 → 0.16.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/CHANGELOG.md +95 -1
- package/README.md +88 -12
- package/client.js +2426 -37
- package/cordis.patch.yml +1 -1
- package/docs/screenshots/01-current-chat.png +0 -0
- package/docs/screenshots/02-global-assets.png +0 -0
- package/docs/screenshots/03-frame-feedback.png +0 -0
- package/docs/screenshots/04-annot-follow.png +0 -0
- package/docs/screenshots/05-mask-principle.png +0 -0
- package/host/clipboard.js +211 -0
- package/host/derive.js +62 -0
- package/host/feedback-text.js +44 -1
- package/host/frames.js +59 -0
- package/host/http.js +615 -2
- package/host/scan.js +138 -10
- package/host/session-index.js +34 -1
- package/host/trim.js +134 -0
- package/icon.svg +4 -6
- package/index.js +1 -1
- package/locale/zh.json +1 -1
- package/package.json +98 -84
package/client.js
CHANGED
|
@@ -68,6 +68,37 @@ function clamp01(v) {
|
|
|
68
68
|
return n
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
/** 把数夹到 [min, max] 闭区间内。 */
|
|
72
|
+
function clampNum(v, min, max) {
|
|
73
|
+
const n = Number(v)
|
|
74
|
+
if (!Number.isFinite(n)) return min
|
|
75
|
+
if (n < min) return min
|
|
76
|
+
if (n > max) return max
|
|
77
|
+
return n
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* v0.16.0 选段播放决策(纯函数,便于单测)。
|
|
82
|
+
*
|
|
83
|
+
* 老板 2026-11-13 11:56 反馈:播放必须跟选段走 —— **从入点起播、到出点结束**,
|
|
84
|
+
* 而不是播放器自己按整段播(截图里选段是 2:16–3:21,播放头却停在 0:29)。
|
|
85
|
+
* 拖动端点 / 点轨道时(scrubbing)不做钳制,否则播放头会被反复拽回,没法目视选帧。
|
|
86
|
+
*
|
|
87
|
+
* @returns {{ action: 'none' | 'stop' | 'seekIn', seekTo?: number }}
|
|
88
|
+
*/
|
|
89
|
+
function trimPlaybackDecision(now, inPoint, outPoint, opts) {
|
|
90
|
+
const o = opts || {}
|
|
91
|
+
if (!Number.isFinite(now)) return { action: 'none' }
|
|
92
|
+
// 区间还没初始化(或过短)时不要干预原生播放
|
|
93
|
+
if (!(outPoint > inPoint + 0.05)) return { action: 'none' }
|
|
94
|
+
if (o.scrubbing === true) return { action: 'none' }
|
|
95
|
+
// 到出点:停住,不循环 —— 老板要的是「到出点结束」
|
|
96
|
+
if (now >= outPoint - 0.02) return { action: 'stop', seekTo: Math.min(now, outPoint) }
|
|
97
|
+
// 播放跑到入点之前(例如用原生进度条拖到了区间外)→ 拉回入点
|
|
98
|
+
if (now < inPoint - 0.25 && o.paused !== true) return { action: 'seekIn', seekTo: inPoint }
|
|
99
|
+
return { action: 'none' }
|
|
100
|
+
}
|
|
101
|
+
|
|
71
102
|
/** 校验并规整一条批注;不合法返回 null。 */
|
|
72
103
|
function normalizeAnnotation(a) {
|
|
73
104
|
if (a === null || typeof a !== "object") return null
|
|
@@ -126,6 +157,335 @@ function annotationSvgCursor(active, dragging, zoom) {
|
|
|
126
157
|
return 'crosshair'
|
|
127
158
|
}
|
|
128
159
|
|
|
160
|
+
/**
|
|
161
|
+
* 遮罩编码契约(v0.14.5 本机 MVP)—— 本机实证,不是推测:
|
|
162
|
+
* ComfyUI 官方 LoadImage 与本机孤海 LoadImageGoohai 的 MASK 输出都是 `1 - alpha/255`。
|
|
163
|
+
* 所以「框内 alpha=0、框外 alpha=255」的普通 RGBA PNG 直接就是可用遮罩,
|
|
164
|
+
* RGB 必须保留原图像素(LoadImage 的 IMAGE 输出仍拿它做局部重绘)。
|
|
165
|
+
* 这与 clipspace-painted-* 涂鸦遮罩等价,但不必复刻 ComfyUI 画布交互。
|
|
166
|
+
*
|
|
167
|
+
* 注意:遮罩是**新资产**(同视频截帧,落盘并登记到当前对话),不是批注;
|
|
168
|
+
* 批注依旧只存内存。导出的路径只进反馈草稿,零自动发送、插件零外部请求。
|
|
169
|
+
*/
|
|
170
|
+
|
|
171
|
+
/** 原图像素上限:超过拒绝生成,避免超大图卡死前端(host 另有 24 MB 兜底)。 */
|
|
172
|
+
const MASK_MAX_PIXELS = 24000000
|
|
173
|
+
|
|
174
|
+
/** 归一化框选批注 → 原图像素矩形(clamp + 至少 1px,不越界)。 */
|
|
175
|
+
function maskPixelRect(annotation, width, height) {
|
|
176
|
+
const w = Math.max(1, Math.round(Number(width) || 0))
|
|
177
|
+
const h = Math.max(1, Math.round(Number(height) || 0))
|
|
178
|
+
const x = clamp01(annotation?.x)
|
|
179
|
+
const y = clamp01(annotation?.y)
|
|
180
|
+
let nw = clamp01(annotation?.w)
|
|
181
|
+
let nh = clamp01(annotation?.h)
|
|
182
|
+
if (nw > 0 && x + nw > 1) nw = 1 - x
|
|
183
|
+
if (nh > 0 && y + nh > 1) nh = 1 - y
|
|
184
|
+
const x0 = Math.min(w - 1, Math.max(0, Math.round(x * w)))
|
|
185
|
+
const y0 = Math.min(h - 1, Math.max(0, Math.round(y * h)))
|
|
186
|
+
const x1 = Math.min(w, Math.max(x0 + 1, Math.round((x + nw) * w)))
|
|
187
|
+
const y1 = Math.min(h, Math.max(y0 + 1, Math.round((y + nh) * h)))
|
|
188
|
+
return { x: x0, y: y0, w: x1 - x0, h: y1 - y0, width: w, height: h }
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** 单像素 alpha:框内 0(mask=1 待重绘),框外 255(mask=0 保留)。 */
|
|
192
|
+
function maskAlphaAt(px, py, rect) {
|
|
193
|
+
const inside = px >= rect.x && px < rect.x + rect.w && py >= rect.y && py < rect.y + rect.h
|
|
194
|
+
return inside ? 0 : 255
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/* ------------------------------------------------------------------ *
|
|
198
|
+
* v0.14.6 右键多点选区(老板 2026-11-12 拍板):
|
|
199
|
+
* 标记态下左键仍是矩形批注,右键逐点点击 4–20 个顶点,闭合成实心多边形。
|
|
200
|
+
* 实测口径(见验证报告第九节):必须是「实心、贴合目标外接框」的块,
|
|
201
|
+
* 描笔画的镂空线框 / 椭圆 / 大矩形都会出白底;多边形天然实心且面积可量。
|
|
202
|
+
* ------------------------------------------------------------------ */
|
|
203
|
+
const POLYGON_LIMITS = { min: 4, max: 20, closeRadius: 0.02, minArea: 0.0001 }
|
|
204
|
+
|
|
205
|
+
/* ------------------------------------------------------------------ *
|
|
206
|
+
* 选区纯色半透明填充(v0.14.7 引入;老板 2026-11-12 20:22 再定版):
|
|
207
|
+
* 填充 0.4 / 轮廓 0.7,界面选区与「填充预览图」共用红 / 绿 / 蓝三色;
|
|
208
|
+
* 导出的主 alpha 遮罩仍是「RGB 原图 + alpha 二值」,ComfyUI 链路一点不动。
|
|
209
|
+
* 轮廓视觉宽度统一 1.5px:界面 SVG 用 non-scaling-stroke 直接画 1.5px;
|
|
210
|
+
* 导出预览图按「DSH 缩到 200px 长边后仍留 1.5px」反推线宽 —— 固定 2px
|
|
211
|
+
* 描边缩完会消失,过宽的边线又会糊住选区,两端共用同一个基准常量。
|
|
212
|
+
* ------------------------------------------------------------------ */
|
|
213
|
+
const MASK_FILL_COLORS = Object.freeze({
|
|
214
|
+
green: { rgb: [94, 214, 150], label: '绿' },
|
|
215
|
+
red: { rgb: [239, 68, 68], label: '红' },
|
|
216
|
+
blue: { rgb: [59, 130, 246], label: '蓝' },
|
|
217
|
+
})
|
|
218
|
+
const MASK_FILL_KEYS = Object.keys(MASK_FILL_COLORS)
|
|
219
|
+
const MASK_FILL_ALPHA = 0.4
|
|
220
|
+
const MASK_STROKE_ALPHA = 0.7
|
|
221
|
+
/** 轮廓视觉宽度基准(px):界面直接画,导出预览图按缩略图比例反推。 */
|
|
222
|
+
const MASK_STROKE_PX = 1.5
|
|
223
|
+
|
|
224
|
+
/** 未知颜色一律回落绿色,避免脏值写进文件名 / 元数据。 */
|
|
225
|
+
function maskFillColor(key) {
|
|
226
|
+
return Object.prototype.hasOwnProperty.call(MASK_FILL_COLORS, key)
|
|
227
|
+
? MASK_FILL_COLORS[key] : MASK_FILL_COLORS.green
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function maskFillRgba(key, alpha = MASK_FILL_ALPHA) {
|
|
231
|
+
const [r, g, b] = maskFillColor(key).rgb
|
|
232
|
+
return `rgba(${r}, ${g}, ${b}, ${alpha})`
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* 预览图轮廓线宽:以「DSH 缩略图长边 200px 时描边仍约 1.5px」反推,
|
|
237
|
+
* 夹在 6–40px,避免小图线条消失、超大图线条糊成一片。纯函数,测试跑真身。
|
|
238
|
+
*/
|
|
239
|
+
function maskStrokeWidth(width, height) {
|
|
240
|
+
const w = Math.max(1, Number(width) || 0)
|
|
241
|
+
const h = Math.max(1, Number(height) || 0)
|
|
242
|
+
const scale = Math.max(w, h) / 200
|
|
243
|
+
return Math.max(6, Math.min(40, Math.round(MASK_STROKE_PX * scale)))
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** 预览层的填充 / 轮廓描述(纯函数,测试直接跑真身):多边形优先,矩形回落。 */
|
|
247
|
+
function maskOverlayPath(rect, polygonPx, colorKey) {
|
|
248
|
+
const hasPolygon = Array.isArray(polygonPx) && polygonPx.length >= 3
|
|
249
|
+
return {
|
|
250
|
+
key: Object.prototype.hasOwnProperty.call(MASK_FILL_COLORS, colorKey) ? colorKey : 'green',
|
|
251
|
+
label: maskFillColor(colorKey).label,
|
|
252
|
+
rgba: maskFillRgba(colorKey, MASK_FILL_ALPHA),
|
|
253
|
+
strokeRgba: maskFillRgba(colorKey, MASK_STROKE_ALPHA),
|
|
254
|
+
strokeWidth: maskStrokeWidth(rect?.width, rect?.height),
|
|
255
|
+
points: hasPolygon ? polygonPx : null,
|
|
256
|
+
rect: hasPolygon ? null : { x: rect.x, y: rect.y, w: rect.w, h: rect.h },
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** 顶点数组的归一外接框;points 既接受 [{x,y}] 也接受 [[x,y]]。 */
|
|
261
|
+
function polygonBBox(points) {
|
|
262
|
+
const xs = points.map((p) => clamp01(Array.isArray(p) ? p[0] : p?.x))
|
|
263
|
+
const ys = points.map((p) => clamp01(Array.isArray(p) ? p[1] : p?.y))
|
|
264
|
+
const x0 = Math.min(...xs)
|
|
265
|
+
const y0 = Math.min(...ys)
|
|
266
|
+
const x1 = Math.max(...xs)
|
|
267
|
+
const y1 = Math.max(...ys)
|
|
268
|
+
return { x: x0, y: y0, w: clamp01(x1 - x0), h: clamp01(y1 - y0) }
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** 鞋带公式:归一化面积,用来挡掉「点成一条线」的退化选区。 */
|
|
272
|
+
function polygonArea(points) {
|
|
273
|
+
let sum = 0
|
|
274
|
+
for (let i = 0, j = points.length - 1; i < points.length; j = i, i += 1) {
|
|
275
|
+
const xi = Array.isArray(points[i]) ? points[i][0] : points[i].x
|
|
276
|
+
const yi = Array.isArray(points[i]) ? points[i][1] : points[i].y
|
|
277
|
+
const xj = Array.isArray(points[j]) ? points[j][0] : points[j].x
|
|
278
|
+
const yj = Array.isArray(points[j]) ? points[j][1] : points[j].y
|
|
279
|
+
sum += (xj * yi) - (xi * yj)
|
|
280
|
+
}
|
|
281
|
+
return Math.abs(sum) / 2
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** 射线法(无 canvas 环境兜底,也用于离线断言钉死「多边形内 alpha=0」)。 */
|
|
285
|
+
function pointInPolygon(px, py, points) {
|
|
286
|
+
let inside = false
|
|
287
|
+
for (let i = 0, j = points.length - 1; i < points.length; j = i, i += 1) {
|
|
288
|
+
const xi = points[i][0]
|
|
289
|
+
const yi = points[i][1]
|
|
290
|
+
const xj = points[j][0]
|
|
291
|
+
const yj = points[j][1]
|
|
292
|
+
const hit = ((yi > py) !== (yj > py))
|
|
293
|
+
&& (px < (((xj - xi) * (py - yi)) / (yj - yi)) + xi)
|
|
294
|
+
if (hit) inside = !inside
|
|
295
|
+
}
|
|
296
|
+
return inside
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** 归一化顶点 → 原图像素顶点;点不合法或数量越界一律返回 null。 */
|
|
300
|
+
function normalizePolygon(value, width, height) {
|
|
301
|
+
if (!Array.isArray(value) || value.length < POLYGON_LIMITS.min || value.length > POLYGON_LIMITS.max) return null
|
|
302
|
+
const w = Math.max(1, Math.round(Number(width) || 0))
|
|
303
|
+
const h = Math.max(1, Math.round(Number(height) || 0))
|
|
304
|
+
const pts = []
|
|
305
|
+
for (const p of value) {
|
|
306
|
+
const nx = Number(Array.isArray(p) ? p[0] : p?.x)
|
|
307
|
+
const ny = Number(Array.isArray(p) ? p[1] : p?.y)
|
|
308
|
+
if (!Number.isFinite(nx) || !Number.isFinite(ny)) return null
|
|
309
|
+
pts.push([clamp01(nx) * w, clamp01(ny) * h])
|
|
310
|
+
}
|
|
311
|
+
return pts
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* 多边形 → 全尺寸二值 alpha 层(0 = 待重绘,255 = 保留)。
|
|
316
|
+
* 优先用离屏 canvas 填充(浏览器自动处理自交、抗锯齿与非零环绕),
|
|
317
|
+
* 只在 bbox 子画布上读一次像素,避免全图再开一份 ImageData;
|
|
318
|
+
* 读回后按 >127 强制二值,保持与 ComfyUI `MASK = 1 - alpha/255` 的实证契约一致。
|
|
319
|
+
* Node / 无 canvas 环境退化为射线法,测试可直接跑真身。
|
|
320
|
+
*/
|
|
321
|
+
function polygonAlphaLayer(points, width, height) {
|
|
322
|
+
const w = Math.max(1, Math.round(Number(width) || 0))
|
|
323
|
+
const h = Math.max(1, Math.round(Number(height) || 0))
|
|
324
|
+
const out = new Uint8Array(w * h).fill(255)
|
|
325
|
+
const xs = points.map((p) => p[0])
|
|
326
|
+
const ys = points.map((p) => p[1])
|
|
327
|
+
const x0 = Math.max(0, Math.min(w - 1, Math.floor(Math.min(...xs))))
|
|
328
|
+
const y0 = Math.max(0, Math.min(h - 1, Math.floor(Math.min(...ys))))
|
|
329
|
+
const x1 = Math.min(w, Math.max(x0 + 1, Math.ceil(Math.max(...xs))))
|
|
330
|
+
const y1 = Math.min(h, Math.max(y0 + 1, Math.ceil(Math.max(...ys))))
|
|
331
|
+
const bw = x1 - x0
|
|
332
|
+
const bh = y1 - y0
|
|
333
|
+
let ctx = null
|
|
334
|
+
try {
|
|
335
|
+
if (typeof OffscreenCanvas === 'function') {
|
|
336
|
+
const canvas = new OffscreenCanvas(bw, bh)
|
|
337
|
+
ctx = canvas.getContext('2d', { willReadFrequently: true })
|
|
338
|
+
} else if (typeof document !== 'undefined') {
|
|
339
|
+
const canvas = document.createElement('canvas')
|
|
340
|
+
canvas.width = bw
|
|
341
|
+
canvas.height = bh
|
|
342
|
+
ctx = canvas.getContext('2d', { willReadFrequently: true })
|
|
343
|
+
}
|
|
344
|
+
} catch { ctx = null }
|
|
345
|
+
if (ctx !== null) {
|
|
346
|
+
ctx.fillStyle = '#000'
|
|
347
|
+
ctx.fillRect(0, 0, bw, bh)
|
|
348
|
+
ctx.fillStyle = '#fff'
|
|
349
|
+
ctx.beginPath()
|
|
350
|
+
ctx.moveTo(points[0][0] - x0, points[0][1] - y0)
|
|
351
|
+
for (let i = 1; i < points.length; i += 1) ctx.lineTo(points[i][0] - x0, points[i][1] - y0)
|
|
352
|
+
ctx.closePath()
|
|
353
|
+
ctx.fill()
|
|
354
|
+
const sub = ctx.getImageData(0, 0, bw, bh).data
|
|
355
|
+
for (let py = 0; py < bh; py += 1) {
|
|
356
|
+
for (let px = 0; px < bw; px += 1) {
|
|
357
|
+
if (sub[((py * bw) + px) * 4] > 127) out[((py + y0) * w) + (px + x0)] = 0
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
} else {
|
|
361
|
+
for (let py = y0; py < y1; py += 1) {
|
|
362
|
+
for (let px = x0; px < x1; px += 1) {
|
|
363
|
+
if (pointInPolygon(px + 0.5, py + 0.5, points)) out[(py * w) + px] = 0
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
return out
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/* ------------------------------------------------------------------ *
|
|
371
|
+
* RGBA 主遮罩 PNG 编码器(v0.14.9 修复 canvas 预乘 alpha 丢 RGB)
|
|
372
|
+
*
|
|
373
|
+
* 本机实证:canvas 内部是预乘 alpha 存储,putImageData → convertToBlob 往返后,
|
|
374
|
+
* alpha=0 的选区内 RGB 会被清零(区内均值差 147.39),而 ComfyUI
|
|
375
|
+
* #369 LoadImageGoohai 的 IMAGE 输出需要原图 RGB。主遮罩必须直接从
|
|
376
|
+
* 「RGB 原图 + 只改过 alpha」的 ImageData 编 PNG,一次也不回写 canvas。
|
|
377
|
+
* ------------------------------------------------------------------ */
|
|
378
|
+
|
|
379
|
+
/** CRC-32(IEEE 802.3),PNG chunk 校验用。 */
|
|
380
|
+
function pngCrc32(bytes) {
|
|
381
|
+
let c = 0xffffffff
|
|
382
|
+
for (let i = 0; i < bytes.length; i += 1) {
|
|
383
|
+
c ^= bytes[i]
|
|
384
|
+
for (let k = 0; k < 8; k += 1) c = (c >>> 1) ^ (0xedb88320 & -(c & 1))
|
|
385
|
+
}
|
|
386
|
+
return (c ^ 0xffffffff) >>> 0
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** 组装一个 PNG chunk:长度 + 类型 + 数据 + CRC。 */
|
|
390
|
+
function pngChunk(type, data) {
|
|
391
|
+
const out = new Uint8Array(12 + data.length)
|
|
392
|
+
const view = new DataView(out.buffer)
|
|
393
|
+
view.setUint32(0, data.length)
|
|
394
|
+
for (let i = 0; i < 4; i += 1) out[4 + i] = type.charCodeAt(i)
|
|
395
|
+
out.set(data, 8)
|
|
396
|
+
view.setUint32(8 + data.length, pngCrc32(out.subarray(4, 8 + data.length)))
|
|
397
|
+
return out
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** ImageData → PNG 扫描线:每行前置 filter=0,RGBA 四通道原样搬运。 */
|
|
401
|
+
function pngScanlines(imageData, width, height) {
|
|
402
|
+
const src = imageData.data
|
|
403
|
+
const stride = width * 4
|
|
404
|
+
const out = new Uint8Array((stride + 1) * height)
|
|
405
|
+
let p = 0
|
|
406
|
+
for (let y = 0; y < height; y += 1) {
|
|
407
|
+
out[p] = 0
|
|
408
|
+
p += 1
|
|
409
|
+
out.set(src.subarray(y * stride, (y + 1) * stride), p)
|
|
410
|
+
p += stride
|
|
411
|
+
}
|
|
412
|
+
return out
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/** Adler-32,zlib 尾部校验。 */
|
|
416
|
+
function pngAdler32(bytes) {
|
|
417
|
+
let a = 1
|
|
418
|
+
let b = 0
|
|
419
|
+
for (let i = 0; i < bytes.length; i += 1) {
|
|
420
|
+
a = (a + bytes[i]) % 65521
|
|
421
|
+
b = (b + a) % 65521
|
|
422
|
+
}
|
|
423
|
+
return ((b << 16) | a) >>> 0
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/** 拼接若干 Uint8Array。 */
|
|
427
|
+
function pngConcat(parts) {
|
|
428
|
+
const out = new Uint8Array(parts.reduce((s, p) => s + p.length, 0))
|
|
429
|
+
let p = 0
|
|
430
|
+
for (const part of parts) { out.set(part, p); p += part.length }
|
|
431
|
+
return out
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/** zlib「stored」无压缩流,同步兜底(CompressionStream 不可用时)。 */
|
|
435
|
+
function pngZlibStored(bytes) {
|
|
436
|
+
const parts = [new Uint8Array([0x78, 0x01])]
|
|
437
|
+
let off = 0
|
|
438
|
+
while (off < bytes.length) {
|
|
439
|
+
const len = Math.min(65535, bytes.length - off)
|
|
440
|
+
const last = off + len >= bytes.length ? 1 : 0
|
|
441
|
+
const blk = new Uint8Array(5 + len)
|
|
442
|
+
blk[0] = last
|
|
443
|
+
blk[1] = len & 0xff
|
|
444
|
+
blk[2] = (len >> 8) & 0xff
|
|
445
|
+
blk[3] = (~len) & 0xff
|
|
446
|
+
blk[4] = ((~len) >> 8) & 0xff
|
|
447
|
+
blk.set(bytes.subarray(off, off + len), 5)
|
|
448
|
+
parts.push(blk)
|
|
449
|
+
off += len
|
|
450
|
+
}
|
|
451
|
+
const tail = new Uint8Array(4)
|
|
452
|
+
new DataView(tail.buffer).setUint32(0, pngAdler32(bytes))
|
|
453
|
+
parts.push(tail)
|
|
454
|
+
return pngConcat(parts)
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* ImageData → RGBA PNG 字节。优先 CompressionStream('deflate') 出标准 zlib 流,
|
|
459
|
+
* 不可用则退回同步 stored 流;两条路都不经过 canvas,RGB 一像素不丢。
|
|
460
|
+
*/
|
|
461
|
+
async function encodeMaskPngBytes(imageData, width, height) {
|
|
462
|
+
const w = Math.max(1, Math.round(Number(width) || 0))
|
|
463
|
+
const h = Math.max(1, Math.round(Number(height) || 0))
|
|
464
|
+
const ihdr = new Uint8Array(13)
|
|
465
|
+
const iv = new DataView(ihdr.buffer)
|
|
466
|
+
iv.setUint32(0, w)
|
|
467
|
+
iv.setUint32(4, h)
|
|
468
|
+
ihdr[8] = 8
|
|
469
|
+
ihdr[9] = 6
|
|
470
|
+
const raw = pngScanlines(imageData, w, h)
|
|
471
|
+
let idat = null
|
|
472
|
+
if (typeof CompressionStream === 'function' && typeof Blob === 'function' && typeof Response === 'function') {
|
|
473
|
+
try {
|
|
474
|
+
const stream = new Blob([raw]).stream().pipeThrough(new CompressionStream('deflate'))
|
|
475
|
+
idat = new Uint8Array(await new Response(stream).arrayBuffer())
|
|
476
|
+
} catch {
|
|
477
|
+
idat = null
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
if (idat === null) idat = pngZlibStored(raw)
|
|
481
|
+
return pngConcat([
|
|
482
|
+
new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]),
|
|
483
|
+
pngChunk('IHDR', ihdr),
|
|
484
|
+
pngChunk('IDAT', idat),
|
|
485
|
+
pngChunk('IEND', new Uint8Array(0)),
|
|
486
|
+
])
|
|
487
|
+
}
|
|
488
|
+
|
|
129
489
|
|
|
130
490
|
/** tab 类型的身份;也是正文与标题在 keyed slot 里的 key。 */
|
|
131
491
|
const KIND = 'gallery'
|
|
@@ -176,8 +536,8 @@ function annotationSvgCursor(active, dragging, zoom) {
|
|
|
176
536
|
/** 中英字典(拆分自 client.js L54-L379)。 */
|
|
177
537
|
|
|
178
538
|
const zh = {
|
|
179
|
-
'type.label': '
|
|
180
|
-
'guide.title': '
|
|
539
|
+
'type.label': '产出台',
|
|
540
|
+
'guide.title': '产出台',
|
|
181
541
|
'guide.description': '最近 24 小时的产出图片',
|
|
182
542
|
loading: '正在扫描…',
|
|
183
543
|
refreshing: '刷新中…',
|
|
@@ -237,7 +597,7 @@ function annotationSvgCursor(active, dragging, zoom) {
|
|
|
237
597
|
annotNotePrompt: '这块要改什么?(可留空)',
|
|
238
598
|
annotNotePlaceholder: '例如:沙发位置偏了 / 色调太暖',
|
|
239
599
|
annotNoNote: '(无说明)',
|
|
240
|
-
annotHint: '
|
|
600
|
+
annotHint: '左键拖拽或点两下画矩形批注;右键逐点圈出 4–20 点的多边形,闭合后直接生成局部重绘遮罩',
|
|
241
601
|
annotCount: '批注 {n} 处',
|
|
242
602
|
annotPendingTag: '待保存',
|
|
243
603
|
annotPendingHint: '黄色亮框就是刚圈的位置;写完点「保存并生效」,也可以直接在图上继续圈下一处(这处会先自动存档)',
|
|
@@ -381,6 +741,8 @@ function annotationSvgCursor(active, dragging, zoom) {
|
|
|
381
741
|
ctxDeleteConfirm: '确认删除?此操作不可恢复',
|
|
382
742
|
ctxDeleteYes: '确认删除',
|
|
383
743
|
ctxDeleteNo: '取消',
|
|
744
|
+
// 批注删除按钮的 title 与卡片删除共用 t('delete')
|
|
745
|
+
delete: '删除',
|
|
384
746
|
ctxHiddenDone: '已隐藏,可在设置里恢复',
|
|
385
747
|
ctxDeleting: '删除中…',
|
|
386
748
|
ctxDeleteFailed: '删除失败',
|
|
@@ -418,6 +780,23 @@ function annotationSvgCursor(active, dragging, zoom) {
|
|
|
418
780
|
feedbackAnnotRegion: '区域 左{x}% 上{y}% · 宽{w}% 高{h}%',
|
|
419
781
|
feedbackAnnotNoNote: '(未填说明,按框选区域改)',
|
|
420
782
|
feedbackFrameFrom: '取自《{name}》{timecode} 帧',
|
|
783
|
+
// 遮罩导出(v0.14.5 本机 MVP):矩形框选批注 → ComfyUI 可用的 RGBA 遮罩
|
|
784
|
+
maskExport: '导出遮罩',
|
|
785
|
+
maskExportHint: '按这个框生成 ComfyUI 可用的 RGBA 遮罩(框内为待重绘区域)',
|
|
786
|
+
maskExporting: '正在生成遮罩…',
|
|
787
|
+
maskSaved: '已生成遮罩:{name}(已入当前对话资产)',
|
|
788
|
+
maskFailed: '遮罩生成失败:{message}',
|
|
789
|
+
// v0.14.6 右键多点选区(4–20 点)→ 实心多边形遮罩
|
|
790
|
+
polyTitle: '多边形遮罩选区',
|
|
791
|
+
polyInfo: '{n}/{max} 点 · 外接框 左{x}% 上{y}% · {w}%×{h}% · 选区约占全图 {area}%',
|
|
792
|
+
polyHint: '右键逐点圈选;点回起点、按 Enter 或点「闭合并生成遮罩」即导出。顶点可左键拖动微调,Backspace 撤销一点,Escape 清空。',
|
|
793
|
+
polyFillColor: '填充色:',
|
|
794
|
+
polyClose: '闭合并生成遮罩',
|
|
795
|
+
polyUndo: '撤销一点',
|
|
796
|
+
polyClear: '清空',
|
|
797
|
+
polyTooFew: '至少 4 个点才能闭合',
|
|
798
|
+
polyDegenerate: '选区面积太小(点几乎连成一条线),请重新围一圈',
|
|
799
|
+
feedbackMaskFrom: '已有遮罩:{name}',
|
|
421
800
|
feedbackAnnotHint: '{n} 条框选批注会随反馈一起发出,序号与草稿里的 Annotation N 一一对应',
|
|
422
801
|
feedbackEditTitle: '编辑:{name}',
|
|
423
802
|
feedbackReasonLabel: '理由(可选)',
|
|
@@ -491,6 +870,49 @@ function annotationSvgCursor(active, dragging, zoom) {
|
|
|
491
870
|
meta: '{size} · {time}',
|
|
492
871
|
scanned: '已扫描 {dirs} 个目录',
|
|
493
872
|
capped: '已达扫描上限,结果可能不完整',
|
|
873
|
+
// v0.16.0 粘贴 / 裁剪 / 音视频选段
|
|
874
|
+
ctxPaste: '粘贴',
|
|
875
|
+
pasteToCurrent: '粘贴到当前对话素材',
|
|
876
|
+
pasteHint: '作为「素材」放进鼠标所在的对话;不在任何分组内则放进当前对话',
|
|
877
|
+
pasteImageOk: '已粘贴图片:{name}(到「{target}」)',
|
|
878
|
+
pasteFileOk: '已粘贴文件:{name}(到「{target}」)',
|
|
879
|
+
pasteEmpty: '剪贴板里没有可粘贴的图片或文件',
|
|
880
|
+
pasteFailed: '粘贴失败:{message}',
|
|
881
|
+
pasteFileByKey: '剪贴板里没有可粘贴的图片或文件;请在资源管理器里复制文件后重试',
|
|
882
|
+
pasteUnsupported: '这类文件拿不到磁盘路径,请在资源管理器里复制后重试',
|
|
883
|
+
// v0.16.0 单文件 / 批量复制 + 任意文件粘贴
|
|
884
|
+
ctxCopyFile: '复制文件',
|
|
885
|
+
ctxCopyFileHint: '把该文件写入系统剪贴板,可在别的对话或资源管理器里粘贴',
|
|
886
|
+
batchCopyFiles: '复制所选文件',
|
|
887
|
+
batchCopyHint: '把选中的文件写入系统剪贴板,可在别的对话或资源管理器里粘贴',
|
|
888
|
+
copyOk: '已复制 {n} 个文件到剪贴板;切到目标对话后右键「粘贴」或按 Ctrl+V',
|
|
889
|
+
copyOkInternal: '已复制 {n} 个文件(系统剪贴板不可用,仅插件内粘贴有效)',
|
|
890
|
+
copyFailed: '复制失败:{message}',
|
|
891
|
+
pasteFilesOk: '已粘贴 {n} 个文件到「{target}」:{name}',
|
|
892
|
+
// v0.16.2 粘贴落点:全局视图下按「鼠标所在的对话组」决定,不是永远当前对话
|
|
893
|
+
pasteTargetCurrent: '当前对话',
|
|
894
|
+
pasteTo: '粘贴到「{target}」',
|
|
895
|
+
cropEnter: '裁剪',
|
|
896
|
+
cropExit: '退出裁剪',
|
|
897
|
+
cropConfirm: '确认裁剪',
|
|
898
|
+
cropCancel: '取消',
|
|
899
|
+
cropHint: '拖动框选要保留的区域;放大镜显示光标处细节,可拖角调整、拖动框移动',
|
|
900
|
+
cropSaving: '正在裁剪…',
|
|
901
|
+
cropSaved: '已裁剪:{name}(已加入「{target}」素材)',
|
|
902
|
+
cropFailed: '裁剪失败:{message}',
|
|
903
|
+
cropMinSize: '选区太小',
|
|
904
|
+
trimEnter: '裁剪片段',
|
|
905
|
+
trimExit: '退出裁剪',
|
|
906
|
+
trimPreview: '从入点播放',
|
|
907
|
+
trimPause: '暂停',
|
|
908
|
+
trimSave: '保存片段',
|
|
909
|
+
trimSaving: '正在裁剪音视频…',
|
|
910
|
+
trimSaved: '已保存片段:{name}(已加入当前对话素材)',
|
|
911
|
+
trimFailed: '裁剪失败:{message}',
|
|
912
|
+
trimIn: '入点',
|
|
913
|
+
trimOut: '出点',
|
|
914
|
+
trimDragHint: '拖蓝色入点 / 橙色出点选段,也可点轨道设定就近端点;播放自动从入点开始、到出点暂停,拖端点时画面跟着走',
|
|
915
|
+
trimNoDuration: '媒体尚未就绪,无法裁剪',
|
|
494
916
|
}
|
|
495
917
|
|
|
496
918
|
const en = {
|
|
@@ -555,7 +977,7 @@ function annotationSvgCursor(active, dragging, zoom) {
|
|
|
555
977
|
annotNotePrompt: 'What should change here? (optional)',
|
|
556
978
|
annotNotePlaceholder: 'e.g. sofa position is off / color is too warm',
|
|
557
979
|
annotNoNote: '(no note)',
|
|
558
|
-
annotHint: '
|
|
980
|
+
annotHint: 'Left-drag or click twice for a boxed note; right-click 4–20 vertices for a polygon selection that becomes an inpaint mask',
|
|
559
981
|
annotCount: '{n} annotation(s)',
|
|
560
982
|
annotPendingTag: 'Pending',
|
|
561
983
|
annotPendingHint: 'The yellow box is the area you just marked. Add a note and click Save, or keep boxing more areas (this one is stored first).',
|
|
@@ -698,6 +1120,7 @@ function annotationSvgCursor(active, dragging, zoom) {
|
|
|
698
1120
|
ctxDeleteConfirm: 'Delete permanently? This cannot be undone',
|
|
699
1121
|
ctxDeleteYes: 'Delete',
|
|
700
1122
|
ctxDeleteNo: 'Cancel',
|
|
1123
|
+
delete: 'Delete',
|
|
701
1124
|
ctxHiddenDone: 'Hidden. Restore it in settings later.',
|
|
702
1125
|
ctxDeleting: 'Deleting…',
|
|
703
1126
|
ctxDeleteFailed: 'Delete failed',
|
|
@@ -734,6 +1157,23 @@ function annotationSvgCursor(active, dragging, zoom) {
|
|
|
734
1157
|
feedbackAnnotRegion: 'region left {x}% top {y}% · {w}%×{h}%',
|
|
735
1158
|
feedbackAnnotNoNote: '(no note — change the boxed area)',
|
|
736
1159
|
feedbackFrameFrom: 'From "{name}" at frame {timecode}',
|
|
1160
|
+
// Mask export (v0.14.5 local MVP): rectangular box annotation -> ComfyUI-ready RGBA mask
|
|
1161
|
+
maskExport: 'Export mask',
|
|
1162
|
+
maskExportHint: 'Generate a ComfyUI-ready RGBA mask from this box (box = area to inpaint)',
|
|
1163
|
+
maskExporting: 'Generating mask…',
|
|
1164
|
+
maskSaved: 'Mask saved: {name} (added to current chat assets)',
|
|
1165
|
+
maskFailed: 'Mask export failed: {message}',
|
|
1166
|
+
// v0.14.6 right-click polygon selection (4-20 points) -> solid polygon mask
|
|
1167
|
+
polyTitle: 'Polygon mask selection',
|
|
1168
|
+
polyInfo: '{n}/{max} points · bbox left {x}% top {y}% · {w}%×{h}% · ~{area}% of image',
|
|
1169
|
+
polyHint: 'Right-click to add vertices; click the first point, press Enter, or use Close & make mask. Drag vertices with the left button; Backspace removes the last point.',
|
|
1170
|
+
polyFillColor: 'Fill:',
|
|
1171
|
+
polyClose: 'Close & make mask',
|
|
1172
|
+
polyUndo: 'Undo point',
|
|
1173
|
+
polyClear: 'Clear',
|
|
1174
|
+
polyTooFew: 'At least 4 points are needed',
|
|
1175
|
+
polyDegenerate: 'Selection area is too small — the points are nearly a line',
|
|
1176
|
+
feedbackMaskFrom: 'Mask ready: {name}',
|
|
737
1177
|
feedbackAnnotHint: '{n} boxed note(s) will be sent with this feedback, numbered to match `Annotation N` in the draft',
|
|
738
1178
|
feedbackEditTitle: 'Edit: {name}',
|
|
739
1179
|
feedbackReasonLabel: 'Reason (optional)',
|
|
@@ -805,6 +1245,49 @@ function annotationSvgCursor(active, dragging, zoom) {
|
|
|
805
1245
|
meta: '{size} · {time}',
|
|
806
1246
|
scanned: 'Scanned {dirs} folders',
|
|
807
1247
|
capped: 'Scan limit reached; results may be incomplete',
|
|
1248
|
+
// v0.16.0 paste / image crop / media trim
|
|
1249
|
+
ctxPaste: 'Paste',
|
|
1250
|
+
pasteToCurrent: 'Paste into current chat assets',
|
|
1251
|
+
pasteHint: 'Add as "source" assets to the chat group under the cursor; falls back to this chat',
|
|
1252
|
+
pasteImageOk: 'Pasted image: {name} (into "{target}")',
|
|
1253
|
+
pasteFileOk: 'Pasted file: {name} (into "{target}")',
|
|
1254
|
+
pasteEmpty: 'No image or file to paste in the clipboard',
|
|
1255
|
+
pasteFailed: 'Paste failed: {message}',
|
|
1256
|
+
pasteFileByKey: 'No image or file to paste; copy files in Explorer and try again',
|
|
1257
|
+
pasteUnsupported: 'No disk path for this file; copy it in Explorer and try again',
|
|
1258
|
+
// v0.16.0 single / batch copy + any-file paste
|
|
1259
|
+
ctxCopyFile: 'Copy file',
|
|
1260
|
+
ctxCopyFileHint: 'Write this file to the system clipboard; paste it in another chat or Explorer',
|
|
1261
|
+
batchCopyFiles: 'Copy selected files',
|
|
1262
|
+
batchCopyHint: 'Write the selected files to the system clipboard; paste them in another chat or Explorer',
|
|
1263
|
+
copyOk: 'Copied {n} file(s) to the clipboard; right-click "Paste" or press Ctrl+V in the target chat',
|
|
1264
|
+
copyOkInternal: 'Copied {n} file(s) (system clipboard unavailable; in-plugin paste only)',
|
|
1265
|
+
copyFailed: 'Copy failed: {message}',
|
|
1266
|
+
pasteFilesOk: 'Pasted {n} file(s) into "{target}": {name}',
|
|
1267
|
+
// v0.16.2 paste destination follows the hovered chat group in the global view
|
|
1268
|
+
pasteTargetCurrent: 'this chat',
|
|
1269
|
+
pasteTo: 'Paste into "{target}"',
|
|
1270
|
+
cropEnter: 'Crop',
|
|
1271
|
+
cropExit: 'Exit crop',
|
|
1272
|
+
cropConfirm: 'Apply crop',
|
|
1273
|
+
cropCancel: 'Cancel',
|
|
1274
|
+
cropHint: 'Drag to select the area to keep; the magnifier shows cursor detail; drag corners to resize, drag the box to move',
|
|
1275
|
+
cropSaving: 'Cropping…',
|
|
1276
|
+
cropSaved: 'Cropped: {name} (added to "{target}" assets)',
|
|
1277
|
+
cropFailed: 'Crop failed: {message}',
|
|
1278
|
+
cropMinSize: 'Selection too small',
|
|
1279
|
+
trimEnter: 'Trim clip',
|
|
1280
|
+
trimExit: 'Exit trim',
|
|
1281
|
+
trimPreview: 'Play from in',
|
|
1282
|
+
trimPause: 'Pause',
|
|
1283
|
+
trimSave: 'Save clip',
|
|
1284
|
+
trimSaving: 'Trimming media…',
|
|
1285
|
+
trimSaved: 'Saved clip: {name} (added to current chat assets)',
|
|
1286
|
+
trimFailed: 'Trim failed: {message}',
|
|
1287
|
+
trimIn: 'In',
|
|
1288
|
+
trimOut: 'Out',
|
|
1289
|
+
trimDragHint: 'Drag the blue In / orange Out handle, or click the track to set the nearest endpoint; playback starts at the in-point and stops at the out-point',
|
|
1290
|
+
trimNoDuration: 'Media not ready yet — cannot trim',
|
|
808
1291
|
}
|
|
809
1292
|
|
|
810
1293
|
/** 格式化 / 图片压缩 / 并发限流工具。 */
|
|
@@ -871,7 +1354,24 @@ function annotationSvgCursor(active, dragging, zoom) {
|
|
|
871
1354
|
body: JSON.stringify({ op, ...(payload ?? {}) }),
|
|
872
1355
|
signal: merged,
|
|
873
1356
|
})
|
|
874
|
-
if (!response.ok)
|
|
1357
|
+
if (!response.ok) {
|
|
1358
|
+
// Host 的 op 路由会把具体原因放在 { error: { message } } 里。
|
|
1359
|
+
// 旧实现只抛 `HTTP 500`,老板截图只剩状态码、没法排障 —— 这里把原因带出来。
|
|
1360
|
+
let detail = ''
|
|
1361
|
+
try {
|
|
1362
|
+
const text = await response.text()
|
|
1363
|
+
try {
|
|
1364
|
+
const parsed = JSON.parse(text)
|
|
1365
|
+
const msg = parsed?.error?.message ?? parsed?.message
|
|
1366
|
+
if (typeof msg === 'string') detail = msg
|
|
1367
|
+
} catch {
|
|
1368
|
+
// 网关可能返回 HTML / 纯文本,取一小段兜底(别把整页 HTML 塞进提示)
|
|
1369
|
+
if (text.length < 300 && !text.includes('<')) detail = text.trim()
|
|
1370
|
+
}
|
|
1371
|
+
} catch { /* body 读不动就只报状态码 */ }
|
|
1372
|
+
if (detail.length > 240) detail = `${detail.slice(0, 240)}…`
|
|
1373
|
+
throw new Error(detail === '' ? `HTTP ${response.status}` : `HTTP ${response.status}: ${detail}`)
|
|
1374
|
+
}
|
|
875
1375
|
return await response.json()
|
|
876
1376
|
} catch (error) {
|
|
877
1377
|
// 组件卸载造成的取消仍按 AbortError 交给上层忽略;只有自己的超时转成可读错误
|
|
@@ -988,6 +1488,106 @@ function annotationSvgCursor(active, dragging, zoom) {
|
|
|
988
1488
|
})
|
|
989
1489
|
}
|
|
990
1490
|
|
|
1491
|
+
/**
|
|
1492
|
+
* 由「原图 + 一条矩形框选批注」生成 ComfyUI 可用的 RGBA 遮罩 PNG。
|
|
1493
|
+
* 只走插件自己的 Host 路由取原图(同源,canvas 不会被 taint),不发任何外部请求。
|
|
1494
|
+
*/
|
|
1495
|
+
async function buildMaskPng(relPath, annotation) {
|
|
1496
|
+
const blob = await fetchRaw(relPath, new AbortController().signal)
|
|
1497
|
+
const bitmap = await createImageBitmap(blob)
|
|
1498
|
+
try {
|
|
1499
|
+
const rect = maskPixelRect(annotation, bitmap.width, bitmap.height)
|
|
1500
|
+
if (rect.width * rect.height > MASK_MAX_PIXELS) throw new Error('mask-image-too-large')
|
|
1501
|
+
// 右键多边形选区:归一化顶点换算成原图像素坐标;矩形批注为 null,走原来的逐像素框判定。
|
|
1502
|
+
const polygonPx = normalizePolygon(annotation?.polygon, rect.width, rect.height)
|
|
1503
|
+
let canvas
|
|
1504
|
+
if (typeof OffscreenCanvas === 'function') {
|
|
1505
|
+
canvas = new OffscreenCanvas(rect.width, rect.height)
|
|
1506
|
+
} else {
|
|
1507
|
+
canvas = document.createElement('canvas')
|
|
1508
|
+
canvas.width = rect.width
|
|
1509
|
+
canvas.height = rect.height
|
|
1510
|
+
}
|
|
1511
|
+
const context = canvas.getContext('2d', { willReadFrequently: true })
|
|
1512
|
+
if (context === null) throw new Error('mask-canvas-unavailable')
|
|
1513
|
+
// 先画原图:RGB 完整保留;框外 alpha 一律强制 255,源 PNG 自带透明也不会被误判成待重绘。
|
|
1514
|
+
context.drawImage(bitmap, 0, 0)
|
|
1515
|
+
const image = context.getImageData(0, 0, rect.width, rect.height)
|
|
1516
|
+
const data = image.data
|
|
1517
|
+
if (polygonPx !== null) {
|
|
1518
|
+
// 多边形:只在选区内 alpha=0,仍是严格二值,RGB 一像素不动。
|
|
1519
|
+
const alpha = polygonAlphaLayer(polygonPx, rect.width, rect.height)
|
|
1520
|
+
for (let i = 0; i < alpha.length; i += 1) data[(i * 4) + 3] = alpha[i]
|
|
1521
|
+
} else {
|
|
1522
|
+
for (let py = 0; py < rect.height; py += 1) {
|
|
1523
|
+
for (let px = 0; px < rect.width; px += 1) {
|
|
1524
|
+
data[((py * rect.width) + px) * 4 + 3] = maskAlphaAt(px, py, rect)
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
// 🔴 不再 putImageData → convertToBlob:canvas 是预乘 alpha 存储,
|
|
1529
|
+
// alpha=0 的选区内 RGB 会被清零,ComfyUI #369 的 IMAGE 输出必须是原图 RGB。
|
|
1530
|
+
// 直接从上面这份「RGB 原图 + 只改过 alpha」的 ImageData 编 PNG。
|
|
1531
|
+
const pngBytes = await encodeMaskPngBytes(image, rect.width, rect.height)
|
|
1532
|
+
const url = await toDataUrl(new Blob([pngBytes], { type: 'image/png' }))
|
|
1533
|
+
|
|
1534
|
+
// 彩色填充预览:另起一张干净画布,绝不能复用已改 alpha 的遮罩画布。
|
|
1535
|
+
// 选区内叠 0.4 半透明纯色 + 沿同一路径叠 0.7 半透明轮廓;
|
|
1536
|
+
// 轮廓居中描边会让边界外一圈也带色,这是「视觉钥匙」的预期行为,
|
|
1537
|
+
// 精确几何仍只认主 alpha 遮罩,绝不拿这张图去反解或回填工作流。
|
|
1538
|
+
const fill = maskOverlayPath(rect, polygonPx, annotation?.fillColor)
|
|
1539
|
+
let overlayData = null
|
|
1540
|
+
let previewCanvas
|
|
1541
|
+
if (typeof OffscreenCanvas === 'function') {
|
|
1542
|
+
previewCanvas = new OffscreenCanvas(rect.width, rect.height)
|
|
1543
|
+
} else {
|
|
1544
|
+
previewCanvas = document.createElement('canvas')
|
|
1545
|
+
previewCanvas.width = rect.width
|
|
1546
|
+
previewCanvas.height = rect.height
|
|
1547
|
+
}
|
|
1548
|
+
const previewCtx = previewCanvas.getContext('2d')
|
|
1549
|
+
if (previewCtx !== null) {
|
|
1550
|
+
previewCtx.drawImage(bitmap, 0, 0)
|
|
1551
|
+
previewCtx.fillStyle = fill.rgba
|
|
1552
|
+
previewCtx.beginPath()
|
|
1553
|
+
if (fill.points !== null) {
|
|
1554
|
+
previewCtx.moveTo(fill.points[0][0], fill.points[0][1])
|
|
1555
|
+
for (let i = 1; i < fill.points.length; i += 1) {
|
|
1556
|
+
previewCtx.lineTo(fill.points[i][0], fill.points[i][1])
|
|
1557
|
+
}
|
|
1558
|
+
previewCtx.closePath()
|
|
1559
|
+
} else {
|
|
1560
|
+
previewCtx.rect(fill.rect.x, fill.rect.y, fill.rect.w, fill.rect.h)
|
|
1561
|
+
}
|
|
1562
|
+
previewCtx.fill()
|
|
1563
|
+
// 0.7 半透明轮廓:与填充共用同一条路径,线宽按缩略图可见性自适应。
|
|
1564
|
+
// 只画在预览画布上,主遮罩画布一像素不动。
|
|
1565
|
+
previewCtx.strokeStyle = fill.strokeRgba
|
|
1566
|
+
previewCtx.lineWidth = fill.strokeWidth
|
|
1567
|
+
previewCtx.lineJoin = 'round'
|
|
1568
|
+
previewCtx.lineCap = 'round'
|
|
1569
|
+
previewCtx.stroke()
|
|
1570
|
+
const previewBlob = typeof previewCanvas.convertToBlob === 'function'
|
|
1571
|
+
? await previewCanvas.convertToBlob({ type: 'image/png' })
|
|
1572
|
+
: await new Promise((resolve, reject) => {
|
|
1573
|
+
previewCanvas.toBlob((b) => (b === null
|
|
1574
|
+
? reject(new Error('preview-toBlob-null')) : resolve(b)), 'image/png')
|
|
1575
|
+
})
|
|
1576
|
+
const previewUrl = await toDataUrl(previewBlob)
|
|
1577
|
+
overlayData = previewUrl.slice(previewUrl.indexOf(',') + 1)
|
|
1578
|
+
}
|
|
1579
|
+
return {
|
|
1580
|
+
data: url.slice(url.indexOf(',') + 1),
|
|
1581
|
+
overlay: overlayData,
|
|
1582
|
+
fillColor: fill.key,
|
|
1583
|
+
fillLabel: fill.label,
|
|
1584
|
+
rect, width: rect.width, height: rect.height, polygonPx,
|
|
1585
|
+
}
|
|
1586
|
+
} finally {
|
|
1587
|
+
if (typeof bitmap.close === 'function') bitmap.close()
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
|
|
991
1591
|
/**
|
|
992
1592
|
* 读图片真实像素尺寸。优先用 host 已解析好的 item.width/height(零成本);
|
|
993
1593
|
* 缺失时才解码一次图像测 naturalWidth/Height(大图解码有开销,能免则免)。
|
|
@@ -1090,7 +1690,11 @@ function annotationSvgCursor(active, dragging, zoom) {
|
|
|
1090
1690
|
background: color-mix(in srgb, var(--dsw-alias-brand-primary) 24%, transparent);
|
|
1091
1691
|
}
|
|
1092
1692
|
.dg-scopeBtn:focus-visible { outline: 2px solid var(--dsw-alias-brand-primary); outline-offset: 1px; }
|
|
1093
|
-
.dg-title { font-weight: 600; white-space: nowrap; overflow:
|
|
1693
|
+
.dg-title { font-weight: 600; white-space: nowrap; overflow: visible; text-overflow: clip; flex: 0 0 auto; }
|
|
1694
|
+
/* tab 标题:包成单个 inline-flex 容器,宿主 chip 的 gap 插不进图标与文字之间;名称不再被裁切。 */
|
|
1695
|
+
.dg-titleWrap { display: inline-flex; align-items: center; gap: 4px; flex: 0 0 auto; white-space: nowrap; }
|
|
1696
|
+
/* tab 标题图标:与宿主内置 tab 的 titleIcon 同规格(16px / currentColor);间距统一由容器 gap 控制 */
|
|
1697
|
+
.dg-tabTitleIcon { width: 16px; height: 16px; flex: 0 0 auto; margin-right: 0; color: var(--dsw-alias-brand-primary); }
|
|
1094
1698
|
.dg-sub { color: var(--dsw-alias-label-secondary); font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
|
1095
1699
|
/* ⚠️ 硬编码:宿主 --dsw-alias-* 变量为空字符串时不会启用 var() 的 fallback */
|
|
1096
1700
|
.dg-btn {
|
|
@@ -2077,8 +2681,11 @@ function annotationSvgCursor(active, dragging, zoom) {
|
|
|
2077
2681
|
.dg-mediaStage {
|
|
2078
2682
|
display: flex; align-items: center; justify-content: center;
|
|
2079
2683
|
background: #000; min-height: 120px;
|
|
2080
|
-
padding: 0;
|
|
2684
|
+
padding: 0; overflow: hidden; flex: 1 1 auto;
|
|
2081
2685
|
}
|
|
2686
|
+
/* 选段条展开时把舞台的最小高度让出来,否则小窗口下轨道会被面板裁掉 */
|
|
2687
|
+
.dg-mediaPanel.is-trim .dg-mediaStage { min-height: 0; }
|
|
2688
|
+
.dg-mediaPanel.is-trim .dg-mediaStage video { max-height: 50vh; }
|
|
2082
2689
|
.dg-mediaPanelVideo .dg-mediaStage video { width: 100%; max-height: 60vh; display: block; }
|
|
2083
2690
|
.dg-mediaPanelAudio .dg-mediaStage { padding: 24px 16px; background: transparent; }
|
|
2084
2691
|
.dg-mediaPanelAudio .dg-mediaStage audio { width: 100%; }
|
|
@@ -2141,7 +2748,13 @@ function annotationSvgCursor(active, dragging, zoom) {
|
|
|
2141
2748
|
.dg-mediaNoticeFail { color: #ff8a8a; background: rgba(220, 60, 60, 0.12); }
|
|
2142
2749
|
|
|
2143
2750
|
.dg-open { white-space: nowrap; }
|
|
2144
|
-
|
|
2751
|
+
/* 侧栏脚部入口:展开态「图标 + 名称」并排,收窄态仅图标;图标跟随 currentColor */
|
|
2752
|
+
.dg-openWide, .dg-openNarrow { display: inline-flex; align-items: center; gap: 7px; }
|
|
2753
|
+
.dg-openNarrow { justify-content: center; padding: 5px 8px; }
|
|
2754
|
+
.dg-openLabel { line-height: 18px; }
|
|
2755
|
+
.dg-sideIcon { width: 16px; height: 16px; flex: 0 0 auto; color: currentColor; }
|
|
2756
|
+
.dg-openCompact { padding: 2px 6px; line-height: 16px; font-size: 12px; display: inline-flex; align-items: center; }
|
|
2757
|
+
.dg-openCompact .dg-sideIcon { width: 15px; height: 15px; }
|
|
2145
2758
|
|
|
2146
2759
|
/* ---------- 反馈篮(S3-a)---------- */
|
|
2147
2760
|
/* ⚠️ 配色全部硬编码:宿主 --dsw-alias-* 变量「已定义但值为空」,
|
|
@@ -2407,6 +3020,17 @@ function annotationSvgCursor(active, dragging, zoom) {
|
|
|
2407
3020
|
.dg-annotItem { display: flex; align-items: center; gap: 6px; font-size: 11px; color: #e8eaed; }
|
|
2408
3021
|
.dg-annotIndex { color: #8fb6ff; font-weight: 600; }
|
|
2409
3022
|
.dg-annotText { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
3023
|
+
.dg-annotItem .dg-btn { flex: 0 0 auto; }
|
|
3024
|
+
/* 遮罩导出结果提示:避开左上批注列表与底部批注输入框,不受 svg 缩放平移影响 */
|
|
3025
|
+
.dg-maskNotice {
|
|
3026
|
+
position: absolute; right: 8px; top: 8px; z-index: 7;
|
|
3027
|
+
max-width: min(360px, 70%); padding: 6px 10px; border-radius: 6px;
|
|
3028
|
+
font-size: 11px; line-height: 1.5; color: #e8eaed;
|
|
3029
|
+
background: rgba(27, 30, 36, 0.96); border: 1px solid rgba(255, 255, 255, 0.18);
|
|
3030
|
+
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
|
|
3031
|
+
}
|
|
3032
|
+
.dg-maskNotice-ok { border-color: rgba(94, 214, 150, 0.65); }
|
|
3033
|
+
.dg-maskNotice-failed { border-color: rgba(255, 107, 107, 0.85); color: #ffd7d7; }
|
|
2410
3034
|
|
|
2411
3035
|
/* 批注输入框:贴底居中,别挡住刚圈出来的框 */
|
|
2412
3036
|
.dg-annotEditor {
|
|
@@ -2443,6 +3067,106 @@ function annotationSvgCursor(active, dragging, zoom) {
|
|
|
2443
3067
|
}
|
|
2444
3068
|
.dg-annotEditorOps .dg-annotSave:hover:not(:disabled) { background: #6a92ff !important; }
|
|
2445
3069
|
.dg-annotPendingHint { font-size: 11px; color: #ffd166; margin: -2px 0 8px; }
|
|
3070
|
+
|
|
3071
|
+
/* v0.14.6 右键多边形选区操作条:贴右下,避开左上批注列表与底部矩形输入框 */
|
|
3072
|
+
.dg-polyPanel {
|
|
3073
|
+
position: absolute; right: 8px; bottom: 14px; z-index: 6;
|
|
3074
|
+
width: min(320px, 76%); box-sizing: border-box;
|
|
3075
|
+
background: #1b1e24; color: #e8eaed;
|
|
3076
|
+
border: 1px solid rgba(94, 214, 150, 0.55); border-radius: 8px; padding: 10px;
|
|
3077
|
+
box-shadow: 0 8px 28px rgba(0, 0, 0, 0.6);
|
|
3078
|
+
}
|
|
3079
|
+
.dg-polyPanelHead { font-size: 12px; font-weight: 600; color: #9fe1cb; margin-bottom: 6px; }
|
|
3080
|
+
.dg-polyPanelInfo { font-size: 11px; line-height: 1.5; word-break: break-all; }
|
|
3081
|
+
.dg-polyPanelHint { font-size: 11px; color: #b9c2cf; line-height: 1.5; margin-top: 6px; }
|
|
3082
|
+
.dg-polyColors { display: inline-flex; align-items: center; gap: 5px; margin-top: 8px; }
|
|
3083
|
+
.dg-polyColorsLabel { font-size: 11px; color: #b9c2cf; margin-right: 2px; }
|
|
3084
|
+
.dg-polyColorBtn { min-width: 26px; height: 20px; padding: 0 5px; border-radius: 4px; border: 1px solid rgba(255,255,255,0.22); color: #10161f; font-size: 11px; font-weight: 600; cursor: pointer; line-height: 18px; }
|
|
3085
|
+
.dg-polyColorBtn.is-active { border-color: #fff; box-shadow: 0 0 0 2px rgba(255,255,255,0.28); }
|
|
3086
|
+
.dg-polyPanelOps { display: flex; justify-content: flex-end; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
|
|
3087
|
+
.dg-polyPanelOps .dg-polyClose {
|
|
3088
|
+
background: #0f6e56 !important; color: #fff !important;
|
|
3089
|
+
border-color: #1d9e75 !important; font-weight: 600;
|
|
3090
|
+
}
|
|
3091
|
+
.dg-polyPanelOps .dg-polyClose:disabled { opacity: 0.45; }
|
|
3092
|
+
|
|
3093
|
+
/* v0.16.0 粘贴结果提示 */
|
|
3094
|
+
.dg-pasteNotice {
|
|
3095
|
+
position: absolute; left: 50%; bottom: 14px; transform: translateX(-50%);
|
|
3096
|
+
z-index: 50; max-width: 84%; box-sizing: border-box;
|
|
3097
|
+
padding: 8px 14px; border-radius: 8px; font-size: 12px; line-height: 1.4;
|
|
3098
|
+
background: #1b1e24; color: #e8eaed; box-shadow: 0 8px 28px rgba(0,0,0,0.6);
|
|
3099
|
+
border: 1px solid rgba(255,255,255,0.18); pointer-events: none; word-break: break-all;
|
|
3100
|
+
}
|
|
3101
|
+
.dg-pasteOk { border-color: rgba(94,214,150,0.6); }
|
|
3102
|
+
.dg-pasteFail { border-color: rgba(255,107,107,0.6); color: #ffd2d2; }
|
|
3103
|
+
|
|
3104
|
+
/* v0.16.0 图片裁剪层 + 微信式圆形放大镜
|
|
3105
|
+
坐标系说明:裁剪框/放大镜都用 getBoundingClientRect 的**视口坐标**定位,
|
|
3106
|
+
所以这一层必须 fixed 铺满视口,absolute 会相对 stage 偏移导致框错位。 */
|
|
3107
|
+
.dg-cropLayer { position: fixed; inset: 0; z-index: 60; cursor: crosshair; }
|
|
3108
|
+
.dg-cropRect {
|
|
3109
|
+
position: absolute; border: 1px solid #6a92ff;
|
|
3110
|
+
box-shadow: 0 0 0 9999px rgba(0,0,0,0.5); box-sizing: border-box;
|
|
3111
|
+
cursor: move; background: rgba(106,146,255,0.06);
|
|
3112
|
+
}
|
|
3113
|
+
.dg-cropHandle {
|
|
3114
|
+
position: absolute; width: 13px; height: 13px; background: #6a92ff;
|
|
3115
|
+
border: 2px solid #fff; border-radius: 2px; box-sizing: border-box;
|
|
3116
|
+
}
|
|
3117
|
+
.dg-cropHandleNW { left: -7px; top: -7px; cursor: nwse-resize; }
|
|
3118
|
+
.dg-cropHandleNE { right: -7px; top: -7px; cursor: nesw-resize; }
|
|
3119
|
+
.dg-cropHandleSW { left: -7px; bottom: -7px; cursor: nesw-resize; }
|
|
3120
|
+
.dg-cropHandleSE { right: -7px; bottom: -7px; cursor: nwse-resize; }
|
|
3121
|
+
.dg-cropMagnifier {
|
|
3122
|
+
position: absolute; width: 130px; height: 130px; border-radius: 50%;
|
|
3123
|
+
border: 2px solid #fff; box-shadow: 0 4px 16px rgba(0,0,0,0.6);
|
|
3124
|
+
pointer-events: none; overflow: hidden; background: #000; transform: translate(-50%, -50%);
|
|
3125
|
+
}
|
|
3126
|
+
.dg-cropMagnifier canvas { display: block; width: 100%; height: 100%; }
|
|
3127
|
+
.dg-cropPanel {
|
|
3128
|
+
/* 位置由组件按裁剪框实时算好、以内联 left/top 注入:贴在选区下方,
|
|
3129
|
+
靠窗口下沿时自动翻到选区上方。千万别写死 left/bottom ——
|
|
3130
|
+
cropLayer 是 fixed inset:0,写死就会跑到视口左下角,离选区十万八千里。 */
|
|
3131
|
+
position: absolute; z-index: 6; width: 300px;
|
|
3132
|
+
box-sizing: border-box; background: #1b1e24; color: #e8eaed;
|
|
3133
|
+
border: 1px solid rgba(106,146,255,0.55); border-radius: 8px; padding: 10px;
|
|
3134
|
+
box-shadow: 0 8px 28px rgba(0,0,0,0.6);
|
|
3135
|
+
}
|
|
3136
|
+
.dg-cropPanelHead { font-size: 12px; font-weight: 600; color: #a9c4ff; margin-bottom: 6px; }
|
|
3137
|
+
.dg-cropPanelHint { font-size: 11px; color: #b9c2cf; line-height: 1.5; margin-top: 6px; }
|
|
3138
|
+
.dg-cropPanelOps { display: flex; justify-content: flex-end; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
|
|
3139
|
+
|
|
3140
|
+
/* v0.16.0 音视频选段轨道 */
|
|
3141
|
+
.dg-trimWrap { padding: 6px 10px 2px; box-sizing: border-box; }
|
|
3142
|
+
.dg-trimHint { font-size: 11px; color: #b9c2cf; margin-bottom: 6px; }
|
|
3143
|
+
.dg-trimTrack {
|
|
3144
|
+
position: relative; height: 30px; border-radius: 6px;
|
|
3145
|
+
background: rgba(255,255,255,0.08); overflow: hidden; user-select: none;
|
|
3146
|
+
}
|
|
3147
|
+
.dg-trimTrackBg { position: absolute; inset: 0; }
|
|
3148
|
+
.dg-trimRegion { position: absolute; top: 0; bottom: 0; background: rgba(106,146,255,0.35); }
|
|
3149
|
+
.dg-trimHandle {
|
|
3150
|
+
position: absolute; top: 0; bottom: 0; width: 12px; background: #6a92ff;
|
|
3151
|
+
border-radius: 2px; cursor: ew-resize; box-shadow: 0 0 0 1px rgba(0,0,0,0.4);
|
|
3152
|
+
/* 以时间点为**中心**;left 由内联样式给出并钳到 1%–99%。
|
|
3153
|
+
否则 outPoint=duration 时 left:100% 会让整条手柄溢出轨道被裁掉(只剩入点可见)。 */
|
|
3154
|
+
transform: translateX(-50%); z-index: 2;
|
|
3155
|
+
}
|
|
3156
|
+
.dg-trimHandleOut { background: #ff9e64; } /* 与入点区分,一眼看出有两个端点 */
|
|
3157
|
+
/* 播放头:让「播放跟随选定点」在轨道上可见;pointer-events:none 才不挡拖手柄 */
|
|
3158
|
+
.dg-trimPlayhead {
|
|
3159
|
+
position: absolute; top: 0; bottom: 0; width: 2px; background: #fff;
|
|
3160
|
+
box-shadow: 0 0 0 1px rgba(0,0,0,0.55); pointer-events: none; z-index: 4;
|
|
3161
|
+
}
|
|
3162
|
+
.dg-trimPlayhead::after {
|
|
3163
|
+
content: ''; position: absolute; top: 0; left: -3px;
|
|
3164
|
+
border-left: 4px solid transparent; border-right: 4px solid transparent;
|
|
3165
|
+
border-top: 6px solid #fff;
|
|
3166
|
+
}
|
|
3167
|
+
.dg-trimTicks { position: absolute; inset: 0; pointer-events: none; }
|
|
3168
|
+
.dg-trimLabel { position: absolute; top: 50%; transform: translateY(-50%); font-size: 10px; color: #e8eaed; pointer-events: none; }
|
|
3169
|
+
.dg-trimOps { display: flex; justify-content: center; gap: 8px; margin: 8px 0 4px; }
|
|
2446
3170
|
`
|
|
2447
3171
|
|
|
2448
3172
|
|
|
@@ -2504,6 +3228,125 @@ function annotationSvgCursor(active, dragging, zoom) {
|
|
|
2504
3228
|
}
|
|
2505
3229
|
}
|
|
2506
3230
|
|
|
3231
|
+
/**
|
|
3232
|
+
* 取 File 的真实磁盘路径(Electron)。
|
|
3233
|
+
*
|
|
3234
|
+
* 为什么不能只看 `file.path`:Electron 32 起已移除 `File.prototype.path`,
|
|
3235
|
+
* 而 DSH 宿主是 Electron 44 —— 只看 file.path 会让「复制的文件」全部落到
|
|
3236
|
+
* 「当图片读」的分支,非图片文件直接失败。必须补 `webUtils.getPathForFile(file)`。
|
|
3237
|
+
*
|
|
3238
|
+
* @returns {string} 绝对路径;拿不到返回空串
|
|
3239
|
+
*/
|
|
3240
|
+
function filePathOf(file) {
|
|
3241
|
+
if (file === null || file === undefined) return ''
|
|
3242
|
+
// ① 旧 Electron / 部分定制运行环境仍可能带 path
|
|
3243
|
+
if (typeof file.path === 'string' && file.path !== '') return file.path
|
|
3244
|
+
// ② 新 Electron 的正规入口:webUtils.getPathForFile
|
|
3245
|
+
try {
|
|
3246
|
+
const g = globalThis
|
|
3247
|
+
const req = typeof g.require === 'function' ? g.require : null
|
|
3248
|
+
if (req !== null) {
|
|
3249
|
+
const electron = req('electron')
|
|
3250
|
+
const webUtils = electron?.webUtils
|
|
3251
|
+
if (webUtils !== null && webUtils !== undefined && typeof webUtils.getPathForFile === 'function') {
|
|
3252
|
+
const p = webUtils.getPathForFile(file)
|
|
3253
|
+
if (typeof p === 'string' && p !== '') return p
|
|
3254
|
+
}
|
|
3255
|
+
}
|
|
3256
|
+
} catch { /* 渲染进程未开 node 集成时正常失败,走下一档 */ }
|
|
3257
|
+
return ''
|
|
3258
|
+
}
|
|
3259
|
+
|
|
3260
|
+
/**
|
|
3261
|
+
* v0.16.0 粘贴入口的剪贴板解析(纯逻辑,可单测)。
|
|
3262
|
+
*
|
|
3263
|
+
* Electron 渲染进程里:截图/复制的图片与复制的文件都走 `paste` 事件的
|
|
3264
|
+
* `clipboardData.items`(`kind === 'file'`)。区别在能否拿到磁盘路径:
|
|
3265
|
+
* ① 复制的文件(资源管理器里 Ctrl+C):webUtils 给到真实绝对路径;
|
|
3266
|
+
* ② 截图/复制的图片:没有路径,但 `getAsFile()` 给到 Blob,可转 base64。
|
|
3267
|
+
* 所以:有路径 → 走「粘贴文件」(Host 直接 copyFile);无路径 → 按图片转 base64。
|
|
3268
|
+
*/
|
|
3269
|
+
function extractFromPasteEvent(clipboardData) {
|
|
3270
|
+
/** @type {File[]} */
|
|
3271
|
+
const images = []
|
|
3272
|
+
/** @type {{ path: string, name: string }[]} */
|
|
3273
|
+
const files = []
|
|
3274
|
+
if (clipboardData === null || clipboardData === undefined) return { images, files }
|
|
3275
|
+
const items = clipboardData.items
|
|
3276
|
+
if (items === null || items === undefined) return { images, files }
|
|
3277
|
+
for (let i = 0; i < items.length; i += 1) {
|
|
3278
|
+
const it = items[i]
|
|
3279
|
+
if (it === null || it === undefined || it.kind !== 'file') continue
|
|
3280
|
+
let file = null
|
|
3281
|
+
try { file = it.getAsFile() } catch { continue }
|
|
3282
|
+
if (file === null) continue
|
|
3283
|
+
const p = filePathOf(file)
|
|
3284
|
+
if (p !== '') {
|
|
3285
|
+
const name = (typeof file.name === 'string' && file.name !== '') ? file.name : (p.split(/[\\/]/).pop() || 'file')
|
|
3286
|
+
files.push({ path: p, name })
|
|
3287
|
+
} else {
|
|
3288
|
+
// 无磁盘路径的 Blob 只有图片能直接转 base64 落盘。音视频 / PDF / ZIP
|
|
3289
|
+
// 在 Chromium 里拿不到路径,若塞进 images 会让 ingestPasted 判「不支持」
|
|
3290
|
+
// 而短路,Host 的系统剪贴板兜底就没机会跑 —— 这里明确跳过。
|
|
3291
|
+
const mime = typeof file.type === 'string' ? file.type : ''
|
|
3292
|
+
if (mime === '' || mime.startsWith('image/')) images.push(file)
|
|
3293
|
+
}
|
|
3294
|
+
}
|
|
3295
|
+
return { images, files }
|
|
3296
|
+
}
|
|
3297
|
+
|
|
3298
|
+
/**
|
|
3299
|
+
* v0.16.0:判断是不是「纯文本粘贴」—— 这类粘贴必须放行,不能抢。
|
|
3300
|
+
* 从资源管理器复制的文件在 Chromium 里可能只挂一个 text/plain 条目,
|
|
3301
|
+
* 但它同时带 Files / 非纯文本类型;只有**真的只有文本**才放行。
|
|
3302
|
+
*/
|
|
3303
|
+
function isPlainTextPaste(clipboardData) {
|
|
3304
|
+
if (clipboardData === null || clipboardData === undefined) return false
|
|
3305
|
+
const types = Array.from(clipboardData.types ?? [])
|
|
3306
|
+
if (types.length === 0) return false
|
|
3307
|
+
if (!types.every((x) => x === 'text/plain')) return false
|
|
3308
|
+
let text = ''
|
|
3309
|
+
try { text = String(clipboardData.getData('text/plain') ?? '') } catch { return false }
|
|
3310
|
+
return text !== ''
|
|
3311
|
+
}
|
|
3312
|
+
|
|
3313
|
+
/** 把 File/Blob 读成 dataURL(含 `data:…;base64,` 前缀)。 */
|
|
3314
|
+
function fileToDataUrl(file) {
|
|
3315
|
+
return new Promise((resolve, reject) => {
|
|
3316
|
+
const reader = new FileReader()
|
|
3317
|
+
reader.onload = () => resolve(String(reader.result))
|
|
3318
|
+
reader.onerror = () => reject(reader.error ?? new Error('FileReader failed'))
|
|
3319
|
+
reader.readAsDataURL(file)
|
|
3320
|
+
})
|
|
3321
|
+
}
|
|
3322
|
+
|
|
3323
|
+
/**
|
|
3324
|
+
* 程序化读剪贴板(右键菜单「粘贴」用,无 paste 事件)。
|
|
3325
|
+
* `navigator.clipboard.read()` 能拿到图片(截图/复制的图),但拿不到任意文件,
|
|
3326
|
+
* 所以这里只返回图片;文件类粘贴统一走 Ctrl+V(见 extractFromPasteEvent)。
|
|
3327
|
+
* @returns {Promise<File[]>} 图片 Blob 列表
|
|
3328
|
+
*/
|
|
3329
|
+
async function readClipboardImages() {
|
|
3330
|
+
const nav = navigator
|
|
3331
|
+
if (nav === null || nav === undefined || nav.clipboard === null || nav.clipboard === undefined) return []
|
|
3332
|
+
if (typeof nav.clipboard.read !== 'function') return []
|
|
3333
|
+
let items = []
|
|
3334
|
+
try { items = await nav.clipboard.read() } catch { return [] }
|
|
3335
|
+
/** @type {File[]} */
|
|
3336
|
+
const out = []
|
|
3337
|
+
for (const ci of items) {
|
|
3338
|
+
if (ci === null || ci === undefined || typeof ci.types === 'undefined') continue
|
|
3339
|
+
for (const type of ci.types) {
|
|
3340
|
+
if (typeof type !== 'string' || !type.startsWith('image/')) continue
|
|
3341
|
+
try {
|
|
3342
|
+
const blob = await ci.getType(type)
|
|
3343
|
+
if (blob !== null) out.push(blob)
|
|
3344
|
+
} catch { /* 忽略单个类型失败 */ }
|
|
3345
|
+
}
|
|
3346
|
+
}
|
|
3347
|
+
return out
|
|
3348
|
+
}
|
|
3349
|
+
|
|
2507
3350
|
/**
|
|
2508
3351
|
* 文件卡片:三段式布局。
|
|
2509
3352
|
* ① 头部:类型图标 + 文件名 + 简介(悬停展开,此处用大小/时间)
|
|
@@ -2619,6 +3462,24 @@ function makeItem(item) {
|
|
|
2619
3462
|
frameMs: Number(item.frameOf.frameMs) || 0,
|
|
2620
3463
|
}
|
|
2621
3464
|
}
|
|
3465
|
+
// v0.14.5 遮罩来源:由本条框选区域生成的 RGBA 遮罩资产,路径只随草稿带出
|
|
3466
|
+
// v0.14.6 右键多边形:把外接框 / 顶点像素坐标 / 面积一并带出,供 Agent 精确回填。
|
|
3467
|
+
if (item?.maskOf !== undefined && item?.maskOf !== null && typeof item.maskOf === 'object') {
|
|
3468
|
+
out.maskOf = {
|
|
3469
|
+
relPath: String(item.maskOf.relPath ?? ''),
|
|
3470
|
+
sourceName: String(item.maskOf.sourceName ?? ''),
|
|
3471
|
+
region: item.maskOf.region ?? null,
|
|
3472
|
+
polygon: Array.isArray(item.maskOf.polygon) ? item.maskOf.polygon : null,
|
|
3473
|
+
polygonPx: Array.isArray(item.maskOf.polygonPx) ? item.maskOf.polygonPx : null,
|
|
3474
|
+
bboxPx: Array.isArray(item.maskOf.bboxPx) ? item.maskOf.bboxPx : null,
|
|
3475
|
+
areaPx: Number(item.maskOf.areaPx) || 0,
|
|
3476
|
+
imageWidth: Number(item.maskOf.imageWidth) || 0,
|
|
3477
|
+
imageHeight: Number(item.maskOf.imageHeight) || 0,
|
|
3478
|
+
// v0.14.7 彩色填充预览图:只用于目视核对,工作流仍用主 alpha 遮罩
|
|
3479
|
+
overlayRelPath: String(item.maskOf.overlayRelPath ?? ''),
|
|
3480
|
+
fillColor: String(item.maskOf.fillColor ?? ''),
|
|
3481
|
+
}
|
|
3482
|
+
}
|
|
2622
3483
|
return out
|
|
2623
3484
|
}
|
|
2624
3485
|
|
|
@@ -2644,6 +3505,86 @@ function baseName(relPath) {
|
|
|
2644
3505
|
return i >= 0 ? s.slice(i + 1) : s
|
|
2645
3506
|
}
|
|
2646
3507
|
|
|
3508
|
+
/* ------------------------------------------------------------------ *
|
|
3509
|
+
* 拖图带位置信息(2026-11-12 实证后落地)
|
|
3510
|
+
*
|
|
3511
|
+
* DSH 宿主源码实证(`dsh-client-ui-attachment` / `dsh-client-ui-conversation`):
|
|
3512
|
+
* · document 级 drop 只认 `dataTransfer.files`(或内部 application/x-dsh-file
|
|
3513
|
+
* 令牌),**完全不读 text/plain / text/uri-list** —— 所以塞文本无效;
|
|
3514
|
+
* · `browserDraftAttachment(file)` 保留原 File,
|
|
3515
|
+
* `encodeImage(file)` 返回 `{ mediaType, data, name: file.name }`,
|
|
3516
|
+
* 即 **file.name 会原样进发给模型的消息**。
|
|
3517
|
+
* 因此正解是:拖出时把「文件名 + 绝对路径 + 主遮罩」写进 File.name,
|
|
3518
|
+
* DSH 的 Agent 拿到附件就能直接定位,不必整目录扫描比对。
|
|
3519
|
+
*
|
|
3520
|
+
* 红线:只写草稿附件,绝不调用 submit();主 alpha 遮罩契约不动。
|
|
3521
|
+
* ------------------------------------------------------------------ */
|
|
3522
|
+
|
|
3523
|
+
/** relPath → { item, absPath },由 GalleryBody 在列表刷新时填,供 dragstart 同步查。 */
|
|
3524
|
+
const dragAssetIndex = new Map()
|
|
3525
|
+
/**
|
|
3526
|
+
* 彩色填充预览图 / 原图 → 主遮罩元数据。
|
|
3527
|
+
* frames.json 只登记主遮罩帧,预览图靠「同目录素材」规则出现、本身没有 maskOf,
|
|
3528
|
+
* 所以拖预览图时必须按 overlayRelPath(或原图 sourceRelPath)反查主遮罩。
|
|
3529
|
+
*/
|
|
3530
|
+
const dragMaskByOverlay = new Map()
|
|
3531
|
+
const dragMaskBySource = new Map()
|
|
3532
|
+
|
|
3533
|
+
/**
|
|
3534
|
+
* 生成拖出 File 的名字(纯函数,测试跑真身)。
|
|
3535
|
+
* 全角「|」分隔,控制字符清掉,长度夹在 240 以内,避免 DSH 卡片显示被截到看不懂。
|
|
3536
|
+
*/
|
|
3537
|
+
function dragNameOf(item, absPath) {
|
|
3538
|
+
const name = String(item?.name ?? baseName(item?.relPath ?? ''))
|
|
3539
|
+
const parts = [`产出台·${name}`]
|
|
3540
|
+
const path = String(absPath ?? '')
|
|
3541
|
+
if (path !== '') parts.push(`路径=${path}`)
|
|
3542
|
+
const m = item?.maskOf ?? null
|
|
3543
|
+
if (m !== null && typeof m === 'object') {
|
|
3544
|
+
const maskRel = String(m.relPath ?? '')
|
|
3545
|
+
if (maskRel !== '') parts.push(`主遮罩=${baseName(maskRel)}`)
|
|
3546
|
+
const overlayRel = String(m.overlayRelPath ?? '')
|
|
3547
|
+
if (m.linkedMask === true) {
|
|
3548
|
+
// 反查命中的预览图 / 原图本身不是遮罩,必须把用法讲死,
|
|
3549
|
+
// 免得 Agent 把这张 RGB 合成图直接回填 #369.image。
|
|
3550
|
+
parts.push('工作流请用主遮罩,预览仅供目视')
|
|
3551
|
+
} else if (overlayRel !== '') {
|
|
3552
|
+
parts.push(`填充预览=${baseName(overlayRel)}`)
|
|
3553
|
+
}
|
|
3554
|
+
}
|
|
3555
|
+
const rel = String(item?.relPath ?? '')
|
|
3556
|
+
if (rel !== '' && path === '') parts.push(`相对路径=${rel}`)
|
|
3557
|
+
return parts.join(' | ').replace(/[\u0000-\u001f]/g, ' ').slice(0, 240)
|
|
3558
|
+
}
|
|
3559
|
+
|
|
3560
|
+
/**
|
|
3561
|
+
* dragstart 是同步事件,不能 await 取原图字节;这里从已渲染的 <img> 同步重编码 PNG。
|
|
3562
|
+
* 卡片缩略图是 data/blob URL,同源不污染 canvas;失败返回 null 走剪贴板兜底。
|
|
3563
|
+
*/
|
|
3564
|
+
function syncImagePngBytes(img) {
|
|
3565
|
+
if (img === null || img === undefined || String(img.tagName ?? '').toLowerCase() !== 'img') return null
|
|
3566
|
+
const w = Number(img.naturalWidth || img.width || 0)
|
|
3567
|
+
const h = Number(img.naturalHeight || img.height || 0)
|
|
3568
|
+
if (w < 1 || h < 1) return null
|
|
3569
|
+
try {
|
|
3570
|
+
const canvas = document.createElement('canvas')
|
|
3571
|
+
canvas.width = w
|
|
3572
|
+
canvas.height = h
|
|
3573
|
+
const cx = canvas.getContext('2d')
|
|
3574
|
+
if (cx === null) return null
|
|
3575
|
+
cx.drawImage(img, 0, 0, w, h)
|
|
3576
|
+
const url = canvas.toDataURL('image/png')
|
|
3577
|
+
const comma = url.indexOf(',')
|
|
3578
|
+
if (comma < 0) return null
|
|
3579
|
+
const bin = atob(url.slice(comma + 1))
|
|
3580
|
+
const bytes = new Uint8Array(bin.length)
|
|
3581
|
+
for (let i = 0; i < bin.length; i += 1) bytes[i] = bin.charCodeAt(i)
|
|
3582
|
+
return bytes
|
|
3583
|
+
} catch {
|
|
3584
|
+
return null
|
|
3585
|
+
}
|
|
3586
|
+
}
|
|
3587
|
+
|
|
2647
3588
|
/**
|
|
2648
3589
|
* 把契约渲染成**人类与模型都能读的草稿正文**。
|
|
2649
3590
|
*
|
|
@@ -2672,7 +3613,7 @@ function renderFeedbackText(feedback, options) {
|
|
|
2672
3613
|
lines.push('Reply item by item using the `Annotation N:` prefix, so each decision maps to one annotation.')
|
|
2673
3614
|
lines.push('')
|
|
2674
3615
|
} else {
|
|
2675
|
-
lines.push(
|
|
3616
|
+
lines.push(`【产出台反馈 · 共 ${items.length} 条】`)
|
|
2676
3617
|
lines.push('')
|
|
2677
3618
|
lines.push('请按 `Annotation 1:`、`Annotation 2:` … 逐条回应,让每条决定都对应到一个批注。')
|
|
2678
3619
|
lines.push('')
|
|
@@ -2695,6 +3636,31 @@ function renderFeedbackText(feedback, options) {
|
|
|
2695
3636
|
? `Frame source: taken from "${f.videoName}" at ${f.timecode} (${f.frameMs} ms)`
|
|
2696
3637
|
: `帧来源:取自视频《${f.videoName}》${f.timecode}(第 ${f.frameMs} 毫秒)`)
|
|
2697
3638
|
}
|
|
3639
|
+
// 遮罩来源是「资产路径 + 编码说明」,不是自动指令:模型拿到后自行上传 / 回填 image 输入
|
|
3640
|
+
if (item.maskOf !== undefined && item.maskOf !== null && String(item.maskOf.relPath ?? '') !== '') {
|
|
3641
|
+
lines.push(locale === 'en'
|
|
3642
|
+
? `Mask source: ${item.maskOf.relPath} — RGBA mask from this box; alpha=0 inside the box, so ComfyUI LoadImage MASK (1 - alpha/255) marks only the area to inpaint`
|
|
3643
|
+
: `遮罩来源:${item.maskOf.relPath} —— 由本条框选区域生成的 RGBA 遮罩,框内 alpha=0,ComfyUI LoadImage 的 MASK 输出(1 - alpha/255)即待重绘区域;可用于局部重绘 / 移除流回填 image 输入`)
|
|
3644
|
+
const mp = item.maskOf
|
|
3645
|
+
// v0.14.7 彩色填充预览图:只供人核对选区位置,别拿去回填工作流
|
|
3646
|
+
if (String(mp.overlayRelPath ?? '') !== '') {
|
|
3647
|
+
const fillName = { green: '绿', red: '红', blue: '蓝' }[String(mp.fillColor ?? '')] ?? '绿'
|
|
3648
|
+
lines.push(locale === 'en'
|
|
3649
|
+
? `Mask preview: ${mp.overlayRelPath} — original image with a solid semi-transparent fill over the selection; visual check only, hand the RGBA mask above to the workflow`
|
|
3650
|
+
: `遮罩预览:${mp.overlayRelPath} —— 原图上叠加${fillName}色半透明填充,仅用于目视核对选区位置;工作流请用上面的 RGBA 遮罩`)
|
|
3651
|
+
}
|
|
3652
|
+
if (Array.isArray(mp.polygonPx) && mp.polygonPx.length > 0) {
|
|
3653
|
+
const b = Array.isArray(mp.bboxPx) ? mp.bboxPx : [0, 0, 0, 0]
|
|
3654
|
+
const size = `${Number(mp.imageWidth) || 0}×${Number(mp.imageHeight) || 0}`
|
|
3655
|
+
const verts = JSON.stringify(mp.polygonPx)
|
|
3656
|
+
lines.push(locale === 'en'
|
|
3657
|
+
? `Mask region: ${size}px source · ${mp.polygonPx.length} vertices · bbox (${b[0]},${b[1]})-(${b[2]},${b[3]}) · ~${Number(mp.areaPx) || 0}px · vertex pixels ${verts}`
|
|
3658
|
+
: `遮罩区域:原图 ${size}px · ${mp.polygonPx.length} 个顶点 · 外接框 (${b[0]},${b[1]})-(${b[2]},${b[3]}) · 面积约 ${Number(mp.areaPx) || 0}px · 顶点像素坐标 ${verts}`)
|
|
3659
|
+
lines.push(locale === 'en'
|
|
3660
|
+
? 'Mask handoff: upload it, then replace the target workflow image input; use ONE short target phrase (e.g. change "X" to "Y", keep the original lettering), do not expand into a whole-word rewrite with extra constraints'
|
|
3661
|
+
: '遮罩回填:上传后替换目标工作流的 image 入参;提示词用单目标短句(例如:将图中的「X」,改为「Y」,保持原有字体色调),不要扩写成整词替换加约束')
|
|
3662
|
+
}
|
|
3663
|
+
}
|
|
2698
3664
|
if (item.reason !== undefined) {
|
|
2699
3665
|
lines.push(`${locale === 'en' ? 'Reason' : '理由'}:${item.reason}`)
|
|
2700
3666
|
}
|
|
@@ -3031,6 +3997,8 @@ function serializeFeedback(feedback) {
|
|
|
3031
3997
|
preserve: mark.preserve,
|
|
3032
3998
|
// 截帧来源随草稿带出(v0.14.3)
|
|
3033
3999
|
frameOf: it.frameOf,
|
|
4000
|
+
// 遮罩来源随草稿带出(v0.14.5):由本图框选区域生成的 RGBA 遮罩资产
|
|
4001
|
+
maskOf: it.maskOf,
|
|
3034
4002
|
})
|
|
3035
4003
|
}
|
|
3036
4004
|
const annots = Array.isArray(it.annotations) ? it.annotations : []
|
|
@@ -3044,6 +4012,8 @@ function serializeFeedback(feedback) {
|
|
|
3044
4012
|
reason: note === '' ? undefined : note,
|
|
3045
4013
|
region: { x: a.x, y: a.y, w: a.w, h: a.h },
|
|
3046
4014
|
frameOf: it.frameOf,
|
|
4015
|
+
// 遮罩来源随草稿带出(v0.14.5):由本图框选区域生成的 RGBA 遮罩资产
|
|
4016
|
+
maskOf: it.maskOf,
|
|
3047
4017
|
})
|
|
3048
4018
|
}
|
|
3049
4019
|
}
|
|
@@ -3226,6 +4196,11 @@ function serializeFeedback(feedback) {
|
|
|
3226
4196
|
timecode: it.frameOf.timecode ?? '',
|
|
3227
4197
|
}))
|
|
3228
4198
|
: null,
|
|
4199
|
+
// 遮罩资产已生成:在篮子里就能看到路径,不必等草稿
|
|
4200
|
+
it.maskOf !== undefined && it.maskOf !== null && String(it.maskOf.relPath ?? '') !== ''
|
|
4201
|
+
? h('span', { className: 'dg-feedbackDim', key: 'mask', 'data-gallery-feedback-mask': it.maskOf.relPath },
|
|
4202
|
+
format(t('feedbackMaskFrom'), { name: baseName(it.maskOf.relPath) }))
|
|
4203
|
+
: null,
|
|
3229
4204
|
h('span', { className: 'dg-feedbackDim dg-feedbackRegion', key: 'r' },
|
|
3230
4205
|
format(t('feedbackAnnotRegion'), { x: pct(a.x), y: pct(a.y), w: pct(a.w), h: pct(a.h) })),
|
|
3231
4206
|
h('span', { className: 'dg-feedbackNote', key: 'note' },
|
|
@@ -3264,6 +4239,11 @@ function serializeFeedback(feedback) {
|
|
|
3264
4239
|
timecode: it.frameOf.timecode ?? '',
|
|
3265
4240
|
}))
|
|
3266
4241
|
: null,
|
|
4242
|
+
// 遮罩资产已生成:在篮子里就能看到路径,不必等草稿
|
|
4243
|
+
it.maskOf !== undefined && it.maskOf !== null && String(it.maskOf.relPath ?? '') !== ''
|
|
4244
|
+
? h('span', { className: 'dg-feedbackDim', key: 'mask', 'data-gallery-feedback-mask': it.maskOf.relPath },
|
|
4245
|
+
format(t('feedbackMaskFrom'), { name: baseName(it.maskOf.relPath) }))
|
|
4246
|
+
: null,
|
|
3267
4247
|
(it.mark?.changeOnly !== undefined && it.mark.changeOnly.length > 0)
|
|
3268
4248
|
? h('span', { className: 'dg-feedbackDim', key: 'co' },
|
|
3269
4249
|
`${t('feedbackChangeOnly')}:${it.mark.changeOnly.join('/')}`)
|
|
@@ -3439,14 +4419,30 @@ function serializeFeedback(feedback) {
|
|
|
3439
4419
|
style: { left: `${pos.left}px`, top: `${pos.top}px` },
|
|
3440
4420
|
}, [
|
|
3441
4421
|
h('div', { className: 'dg-ctxHead', key: 'head' }, item.name),
|
|
4422
|
+
// v0.16.0:列表级「粘贴」(右键任意处都可出现);无 item 的空白区菜单只给这一项
|
|
4423
|
+
actions.onPaste !== undefined
|
|
4424
|
+
? entry('paste', t('ctxPaste'), '📥', () => actions.onPaste())
|
|
4425
|
+
: null,
|
|
3442
4426
|
// 「查看原图」只对图片出现:文件卡片没有原图概念,避免无效按钮
|
|
3443
4427
|
actions.onOpenOriginal !== undefined
|
|
3444
4428
|
? entry('original', t('ctxOpenOriginal'), '🔍', () => actions.onOpenOriginal())
|
|
3445
4429
|
: null,
|
|
3446
|
-
|
|
3447
|
-
|
|
3448
|
-
|
|
3449
|
-
|
|
4430
|
+
actions.onHide !== undefined
|
|
4431
|
+
? entry('hide', t('ctxHide'), '🚫', () => actions.onHide())
|
|
4432
|
+
: null,
|
|
4433
|
+
actions.onReveal !== undefined
|
|
4434
|
+
? entry('reveal', t('ctxReveal'), '📂', () => actions.onReveal())
|
|
4435
|
+
: null,
|
|
4436
|
+
actions.onCopy !== undefined
|
|
4437
|
+
? entry('copy', t('ctxCopy'), '⧉', () => actions.onCopy())
|
|
4438
|
+
: null,
|
|
4439
|
+
// v0.16.0:复制**文件本体**到系统剪贴板(与上面的「复制路径」文本区分)
|
|
4440
|
+
actions.onCopyFile !== undefined
|
|
4441
|
+
? entry('copyFile', t('ctxCopyFile'), '📄', () => actions.onCopyFile())
|
|
4442
|
+
: null,
|
|
4443
|
+
actions.onDelete !== undefined
|
|
4444
|
+
? entry('delete', t('ctxDelete'), '🗑', () => actions.onDelete(), true)
|
|
4445
|
+
: null,
|
|
3450
4446
|
// ---- v0.14.2 手动标签:表态层已下线,只留「改标签 / 恢复自动」两个入口 ----
|
|
3451
4447
|
actions.onEditLabel !== undefined
|
|
3452
4448
|
? h('div', { className: 'dg-ctxDivider', key: 'labelDivider' }, t('labelEdit'))
|
|
@@ -3789,6 +4785,18 @@ function serializeFeedback(feedback) {
|
|
|
3789
4785
|
const [dims, setDims] = React.useState(null)
|
|
3790
4786
|
// 截帧结果提示(成功/失败),2.5 秒自动消失
|
|
3791
4787
|
const [grabNotice, setGrabNotice] = React.useState(null)
|
|
4788
|
+
// v0.16.0 音视频选段(入点/出点裁剪)
|
|
4789
|
+
const [duration, setDuration] = React.useState(0)
|
|
4790
|
+
const [trimActive, setTrimActive] = React.useState(false)
|
|
4791
|
+
const [inPoint, setInPoint] = React.useState(0)
|
|
4792
|
+
const [outPoint, setOutPoint] = React.useState(0)
|
|
4793
|
+
const [trimBusy, setTrimBusy] = React.useState(false)
|
|
4794
|
+
// previewing = 正在按选区播放(到出点会自动置 false);不再是「循环预览」开关
|
|
4795
|
+
const [previewing, setPreviewing] = React.useState(false)
|
|
4796
|
+
const [playhead, setPlayhead] = React.useState(0)
|
|
4797
|
+
const trimTrackRef = React.useRef(null)
|
|
4798
|
+
// 拖动端点期间不做区间钳制,否则播放头会被反复拽回、没法目视选帧
|
|
4799
|
+
const scrubbingRef = React.useRef(false)
|
|
3792
4800
|
|
|
3793
4801
|
// 取回完整文件为 blob 并建 URL(支持 seek 的唯一可靠路径)
|
|
3794
4802
|
React.useEffect(() => {
|
|
@@ -3946,13 +4954,233 @@ function serializeFeedback(feedback) {
|
|
|
3946
4954
|
return () => clearTimeout(timer)
|
|
3947
4955
|
}, [grabNotice])
|
|
3948
4956
|
|
|
4957
|
+
/**
|
|
4958
|
+
* v0.16.0:把播放头移到指定秒(拖端点 / 点轨道 / 从入点重播共用)。
|
|
4959
|
+
* 只改 currentTime 不换 src —— React 复用同一个 <video> DOM,播放不会被重建。
|
|
4960
|
+
*/
|
|
4961
|
+
const seekMedia = (sec) => {
|
|
4962
|
+
const v = videoRef.current
|
|
4963
|
+
if (v === null || duration <= 0) return
|
|
4964
|
+
try { v.currentTime = clampNum(sec, 0, duration) } catch { /* 元数据未就绪 */ }
|
|
4965
|
+
}
|
|
4966
|
+
/** 播放头跟随选段:更新轨道指示线 + 到出点暂停 + 区间外起播拉回入点。 */
|
|
4967
|
+
const onMediaTimeUpdate = (event) => {
|
|
4968
|
+
const v = event.currentTarget
|
|
4969
|
+
const now = v.currentTime
|
|
4970
|
+
// 只在选段态更新播放头:没开选段时不必每 250ms 重渲染整个播放器
|
|
4971
|
+
if (trimActive && Number.isFinite(now)) setPlayhead(now)
|
|
4972
|
+
if (!trimActive) return
|
|
4973
|
+
const d = trimPlaybackDecision(now, inPoint, outPoint, {
|
|
4974
|
+
paused: v.paused, scrubbing: scrubbingRef.current,
|
|
4975
|
+
})
|
|
4976
|
+
if (d.action === 'stop') {
|
|
4977
|
+
try {
|
|
4978
|
+
if (typeof d.seekTo === 'number' && Math.abs(v.currentTime - d.seekTo) > 0.001) v.currentTime = d.seekTo
|
|
4979
|
+
v.pause()
|
|
4980
|
+
} catch { /* 忽略 */ }
|
|
4981
|
+
setPreviewing(false)
|
|
4982
|
+
} else if (d.action === 'seekIn') {
|
|
4983
|
+
try { v.currentTime = d.seekTo } catch { /* 忽略 */ }
|
|
4984
|
+
}
|
|
4985
|
+
}
|
|
4986
|
+
/** 用户按原生播放键时,播放头若不在选区内,直接跳到入点起播。 */
|
|
4987
|
+
const onMediaPlay = (event) => {
|
|
4988
|
+
if (!trimActive) return
|
|
4989
|
+
const v = event.currentTarget
|
|
4990
|
+
const now = v.currentTime
|
|
4991
|
+
if (!(outPoint > inPoint + 0.05)) return
|
|
4992
|
+
if (now < inPoint - 0.25 || now >= outPoint - 0.05) {
|
|
4993
|
+
try { v.currentTime = inPoint } catch { /* 忽略 */ }
|
|
4994
|
+
}
|
|
4995
|
+
}
|
|
4996
|
+
// v0.16.0 音视频选段:秒数 → m:ss
|
|
4997
|
+
const fmtClock = (sec) => {
|
|
4998
|
+
const s = Math.max(0, Math.floor(Number(sec) || 0))
|
|
4999
|
+
const m = Math.floor(s / 60)
|
|
5000
|
+
const r = s % 60
|
|
5001
|
+
return `${m}:${String(r).padStart(2, '0')}`
|
|
5002
|
+
}
|
|
5003
|
+
// 轨道坐标 → 时间
|
|
5004
|
+
const timeFromClientX = (clientX) => {
|
|
5005
|
+
const track = trimTrackRef.current
|
|
5006
|
+
if (track === null || duration <= 0) return 0
|
|
5007
|
+
const r = track.getBoundingClientRect()
|
|
5008
|
+
if (r.width <= 0) return 0
|
|
5009
|
+
return clampNum((clientX - r.left) / r.width, 0, 1) * duration
|
|
5010
|
+
}
|
|
5011
|
+
// 拖动入点/出点滑块(用 window 监听保证拖出元素也不丢)
|
|
5012
|
+
const startTrimDrag = (e, which) => {
|
|
5013
|
+
e.preventDefault()
|
|
5014
|
+
e.stopPropagation()
|
|
5015
|
+
try { e.currentTarget.setPointerCapture(e.pointerId) } catch {}
|
|
5016
|
+
// 拖动期间暂停区间钳制,让画面**跟着端点走**(老板要的「播放跟随选定点」)
|
|
5017
|
+
scrubbingRef.current = true
|
|
5018
|
+
const move = (ev) => {
|
|
5019
|
+
const t = timeFromClientX(ev.clientX)
|
|
5020
|
+
if (which === 'in') {
|
|
5021
|
+
const n = clampNum(t, 0, Math.max(0, outPoint - 0.1))
|
|
5022
|
+
setInPoint(n); seekMedia(n); setPlayhead(n)
|
|
5023
|
+
} else if (which === 'out') {
|
|
5024
|
+
const n = clampNum(t, Math.min(duration, inPoint + 0.1), duration)
|
|
5025
|
+
setOutPoint(n); seekMedia(n); setPlayhead(n)
|
|
5026
|
+
}
|
|
5027
|
+
}
|
|
5028
|
+
const up = () => {
|
|
5029
|
+
scrubbingRef.current = false
|
|
5030
|
+
window.removeEventListener('pointermove', move)
|
|
5031
|
+
window.removeEventListener('pointerup', up)
|
|
5032
|
+
}
|
|
5033
|
+
window.addEventListener('pointermove', move)
|
|
5034
|
+
window.addEventListener('pointerup', up)
|
|
5035
|
+
}
|
|
5036
|
+
/**
|
|
5037
|
+
* 点轨道空白处也能设点:把离点击位置**最近**的那个端点移过来。
|
|
5038
|
+
* 手柄自身的 onPointerDown 已 stopPropagation,不会误触发这里。
|
|
5039
|
+
* 有了这条,即使出点贴到最右端,老板也能直接点轨道右侧把它拉回来。
|
|
5040
|
+
*/
|
|
5041
|
+
const onTrackPointerDown = (e) => {
|
|
5042
|
+
if (duration <= 0) return
|
|
5043
|
+
e.preventDefault()
|
|
5044
|
+
const t = timeFromClientX(e.clientX)
|
|
5045
|
+
let n
|
|
5046
|
+
if (Math.abs(t - inPoint) <= Math.abs(t - outPoint)) {
|
|
5047
|
+
n = clampNum(t, 0, Math.max(0, outPoint - 0.1))
|
|
5048
|
+
setInPoint(n)
|
|
5049
|
+
} else {
|
|
5050
|
+
n = clampNum(t, Math.min(duration, inPoint + 0.1), duration)
|
|
5051
|
+
setOutPoint(n)
|
|
5052
|
+
}
|
|
5053
|
+
// 点轨道设点后,播放头也跳到该点,方便目视核对这一帧
|
|
5054
|
+
seekMedia(n); setPlayhead(n)
|
|
5055
|
+
}
|
|
5056
|
+
/**
|
|
5057
|
+
* 进入选段 / 拿到时长后:立刻把播放头移到入点并起播。
|
|
5058
|
+
*
|
|
5059
|
+
* 老板 11:56 反馈的根因就在这里 —— 原来只有点「预览选区」才 seek,
|
|
5060
|
+
* 不点的话播放器一直按整段自动播(选段 2:16–3:21,播放头却停在 0:29)。
|
|
5061
|
+
* 依赖数组刻意不含 inPoint/outPoint:拖动端点不该重复触发这个 effect。
|
|
5062
|
+
*/
|
|
5063
|
+
React.useEffect(() => {
|
|
5064
|
+
if (!trimActive || duration <= 0) return
|
|
5065
|
+
const initIn = outPoint > 0 ? inPoint : duration * 0.2
|
|
5066
|
+
if (outPoint <= 0) {
|
|
5067
|
+
setInPoint(duration * 0.2); setOutPoint(duration * 0.8)
|
|
5068
|
+
}
|
|
5069
|
+
seekMedia(initIn)
|
|
5070
|
+
setPlayhead(initIn)
|
|
5071
|
+
const v = videoRef.current
|
|
5072
|
+
if (v === null) return
|
|
5073
|
+
try { v.play().catch(() => {}) } catch { /* 自动播放被拦就等用户点按钮 */ }
|
|
5074
|
+
setPreviewing(true)
|
|
5075
|
+
}, [trimActive, duration])
|
|
5076
|
+
// 退出选段时清掉「按选区播放」标记,恢复原生自由播放
|
|
5077
|
+
React.useEffect(() => {
|
|
5078
|
+
if (!trimActive) setPreviewing(false)
|
|
5079
|
+
}, [trimActive])
|
|
5080
|
+
const saveTrim = React.useCallback(async () => {
|
|
5081
|
+
if (duration <= 0 || outPoint <= inPoint + 0.05) {
|
|
5082
|
+
setGrabNotice({ kind: 'fail', text: t('cropMinSize') })
|
|
5083
|
+
return
|
|
5084
|
+
}
|
|
5085
|
+
setTrimBusy(true)
|
|
5086
|
+
try {
|
|
5087
|
+
const payload = await callHost('cropMedia', {
|
|
5088
|
+
path: item.relPath,
|
|
5089
|
+
start: Math.round(inPoint * 1000) / 1000,
|
|
5090
|
+
end: Math.round(outPoint * 1000) / 1000,
|
|
5091
|
+
kind: isVideo ? 'video' : 'audio',
|
|
5092
|
+
// v0.16.1:带上对话身份,Host 才能把片段登记回当前对话组;
|
|
5093
|
+
// 不传的话全局资产会把它扔进「未归类」,界面上等于「切了但没生成」。
|
|
5094
|
+
sessionId: sessionId ?? '',
|
|
5095
|
+
sessionTitle: sessionTitle ?? '',
|
|
5096
|
+
sessionCreatedAt: sessionCreatedAt ?? 0,
|
|
5097
|
+
})
|
|
5098
|
+
if (payload?.ok === false) throw new Error(payload?.error?.message ?? 'cropMedia failed')
|
|
5099
|
+
if (typeof onSaved === 'function') onSaved(payload)
|
|
5100
|
+
setGrabNotice({ kind: 'ok', text: format(t('trimSaved'), { name: payload?.name ?? '' }) })
|
|
5101
|
+
} catch (error) {
|
|
5102
|
+
setGrabNotice({ kind: 'fail', text: format(t('trimFailed'), { message: String(error?.message ?? error) }) })
|
|
5103
|
+
} finally {
|
|
5104
|
+
setTrimBusy(false)
|
|
5105
|
+
}
|
|
5106
|
+
}, [duration, inPoint, outPoint, isVideo, item.relPath, onSaved, t, sessionId, sessionTitle, sessionCreatedAt])
|
|
5107
|
+
const trimUI = (trimActive && duration > 0)
|
|
5108
|
+
? h('div', {
|
|
5109
|
+
className: 'dg-trimWrap', key: 'trim', 'data-gallery-trim': item.relPath,
|
|
5110
|
+
onPointerDown: (e) => e.stopPropagation(),
|
|
5111
|
+
}, [
|
|
5112
|
+
h('div', { className: 'dg-trimHint', key: 'hint' }, t('trimDragHint')),
|
|
5113
|
+
h('div', {
|
|
5114
|
+
className: 'dg-trimTrack', key: 'track', ref: trimTrackRef,
|
|
5115
|
+
'data-gallery-trim-track': true,
|
|
5116
|
+
onPointerDown: onTrackPointerDown,
|
|
5117
|
+
}, [
|
|
5118
|
+
h('div', {
|
|
5119
|
+
className: 'dg-trimRegion', key: 'region',
|
|
5120
|
+
style: {
|
|
5121
|
+
left: `${(inPoint / duration) * 100}%`,
|
|
5122
|
+
width: `${((outPoint - inPoint) / duration) * 100}%`,
|
|
5123
|
+
},
|
|
5124
|
+
}),
|
|
5125
|
+
h('div', {
|
|
5126
|
+
className: 'dg-trimHandle dg-trimHandleIn', key: 'in',
|
|
5127
|
+
'data-gallery-trim-in': true,
|
|
5128
|
+
// 钳到 1%–99%:配合 CSS 的 translateX(-50%),贴边时仍露出半个手柄可拖
|
|
5129
|
+
style: { left: `${clampNum((inPoint / duration) * 100, 1, 99)}%` },
|
|
5130
|
+
onPointerDown: (e) => startTrimDrag(e, 'in'),
|
|
5131
|
+
}),
|
|
5132
|
+
h('div', {
|
|
5133
|
+
className: 'dg-trimHandle dg-trimHandleOut', key: 'out',
|
|
5134
|
+
'data-gallery-trim-out': true,
|
|
5135
|
+
style: { left: `${clampNum((outPoint / duration) * 100, 1, 99)}%` },
|
|
5136
|
+
onPointerDown: (e) => startTrimDrag(e, 'out'),
|
|
5137
|
+
}),
|
|
5138
|
+
// 白色播放头:让「播放跟随选定点」在轨道上可见
|
|
5139
|
+
h('div', {
|
|
5140
|
+
className: 'dg-trimPlayhead', key: 'playhead',
|
|
5141
|
+
'data-gallery-trim-playhead': true,
|
|
5142
|
+
style: { left: `${clampNum((playhead / duration) * 100, 0, 100)}%` },
|
|
5143
|
+
}),
|
|
5144
|
+
]),
|
|
5145
|
+
h('div', {
|
|
5146
|
+
className: 'dg-trimLabels', key: 'labels',
|
|
5147
|
+
style: { display: 'flex', justifyContent: 'space-between', fontSize: '11px', color: '#b9c2cf', marginTop: '4px' },
|
|
5148
|
+
}, [
|
|
5149
|
+
h('span', { key: 'li' }, `${t('trimIn')} ${fmtClock(inPoint)}`),
|
|
5150
|
+
h('span', { key: 'lo' }, `${t('trimOut')} ${fmtClock(outPoint)}`),
|
|
5151
|
+
]),
|
|
5152
|
+
h('div', { className: 'dg-trimOps', key: 'ops' }, [
|
|
5153
|
+
h('button', {
|
|
5154
|
+
type: 'button', className: 'dg-btn', key: 'preview',
|
|
5155
|
+
onClick: (e) => {
|
|
5156
|
+
e.stopPropagation()
|
|
5157
|
+
const v = videoRef.current
|
|
5158
|
+
if (previewing) {
|
|
5159
|
+
try { v?.pause() } catch { /* 忽略 */ }
|
|
5160
|
+
setPreviewing(false)
|
|
5161
|
+
} else {
|
|
5162
|
+
// 从入点重播(播放到出点由 onMediaTimeUpdate 自动停住)
|
|
5163
|
+
seekMedia(inPoint); setPlayhead(inPoint)
|
|
5164
|
+
try { v?.play().catch(() => {}) } catch { /* 忽略 */ }
|
|
5165
|
+
setPreviewing(true)
|
|
5166
|
+
}
|
|
5167
|
+
},
|
|
5168
|
+
}, previewing ? `⏸ ${t('trimPause')}` : `▶ ${t('trimPreview')}`),
|
|
5169
|
+
h('button', {
|
|
5170
|
+
type: 'button', className: 'dg-btn dg-btnPrimary', key: 'save', disabled: trimBusy,
|
|
5171
|
+
onClick: (e) => { e.stopPropagation(); void saveTrim() },
|
|
5172
|
+
}, trimBusy ? t('trimSaving') : t('trimSave')),
|
|
5173
|
+
]),
|
|
5174
|
+
])
|
|
5175
|
+
: null
|
|
5176
|
+
|
|
3949
5177
|
return h('div', {
|
|
3950
5178
|
className: 'dg-mediaLayer',
|
|
3951
5179
|
key: 'mediaLayer',
|
|
3952
5180
|
'data-gallery-player': item.relPath,
|
|
3953
5181
|
onClick: onClose,
|
|
3954
5182
|
}, h('div', {
|
|
3955
|
-
className: `dg-mediaPanel${isVideo ? ' dg-mediaPanelVideo' : ' dg-mediaPanelAudio'}`,
|
|
5183
|
+
className: `dg-mediaPanel${isVideo ? ' dg-mediaPanelVideo' : ' dg-mediaPanelAudio'}${trimActive ? ' is-trim' : ''}`,
|
|
3956
5184
|
key: 'panel',
|
|
3957
5185
|
onClick: (event) => event.stopPropagation(),
|
|
3958
5186
|
}, [
|
|
@@ -3969,6 +5197,23 @@ function serializeFeedback(feedback) {
|
|
|
3969
5197
|
onClick: (event) => { event.stopPropagation(); grabFrame() },
|
|
3970
5198
|
}, ['📷 ', t('mediaGrabFrame')])
|
|
3971
5199
|
: null,
|
|
5200
|
+
// v0.16.0 选段裁剪:开关入点/出点选段 UI
|
|
5201
|
+
source.status === 'ready' && !failed
|
|
5202
|
+
? h('button', {
|
|
5203
|
+
type: 'button',
|
|
5204
|
+
className: `dg-btn${trimActive ? ' dg-btnActive' : ''}`,
|
|
5205
|
+
key: 'trim',
|
|
5206
|
+
'data-gallery-trim-toggle': item.relPath,
|
|
5207
|
+
title: trimActive ? t('trimExit') : t('trimEnter'),
|
|
5208
|
+
'aria-label': trimActive ? t('trimExit') : t('trimEnter'),
|
|
5209
|
+
onClick: (event) => {
|
|
5210
|
+
event.stopPropagation()
|
|
5211
|
+
// 默认选区与「跳到入点起播」都交给下面的进入 effect,
|
|
5212
|
+
// 这里只做状态切换(updater 里不要塞副作用)。
|
|
5213
|
+
setTrimActive((v) => !v)
|
|
5214
|
+
},
|
|
5215
|
+
}, ['✂ ', trimActive ? t('trimExit') : t('trimEnter')])
|
|
5216
|
+
: null,
|
|
3972
5217
|
h('button', {
|
|
3973
5218
|
type: 'button', className: 'dg-btn', key: 'close',
|
|
3974
5219
|
title: t('mediaCloseHint'), 'aria-label': t('mediaCloseHint'),
|
|
@@ -4033,10 +5278,13 @@ function serializeFeedback(feedback) {
|
|
|
4033
5278
|
preload: 'metadata',
|
|
4034
5279
|
key: 'v',
|
|
4035
5280
|
'data-gallery-video': item.relPath,
|
|
5281
|
+
onTimeUpdate: onMediaTimeUpdate,
|
|
5282
|
+
onPlay: onMediaPlay,
|
|
4036
5283
|
onError: () => setFailed(true),
|
|
4037
5284
|
onLoadedMetadata: (event) => {
|
|
4038
5285
|
const v = event.currentTarget
|
|
4039
5286
|
if (v.videoWidth > 0) setDims({ w: v.videoWidth, h: v.videoHeight })
|
|
5287
|
+
if (Number.isFinite(v.duration) && v.duration > 0) setDuration(v.duration)
|
|
4040
5288
|
},
|
|
4041
5289
|
})
|
|
4042
5290
|
: h('audio', {
|
|
@@ -4046,9 +5294,17 @@ function serializeFeedback(feedback) {
|
|
|
4046
5294
|
preload: 'metadata',
|
|
4047
5295
|
key: 'a',
|
|
4048
5296
|
'data-gallery-audio': item.relPath,
|
|
5297
|
+
ref: videoRef,
|
|
5298
|
+
onTimeUpdate: onMediaTimeUpdate,
|
|
5299
|
+
onPlay: onMediaPlay,
|
|
4049
5300
|
onError: () => setFailed(true),
|
|
5301
|
+
onLoadedMetadata: (event) => {
|
|
5302
|
+
const a = event.currentTarget
|
|
5303
|
+
if (Number.isFinite(a.duration) && a.duration > 0) setDuration(a.duration)
|
|
5304
|
+
},
|
|
4050
5305
|
})
|
|
4051
5306
|
)),
|
|
5307
|
+
trimUI,
|
|
4052
5308
|
h('div', { className: 'dg-mediaFoot', key: 'foot' }, [
|
|
4053
5309
|
h('span', { className: 'dg-mediaFootName', key: 'name', title: item.relPath }, item.name),
|
|
4054
5310
|
h('span', { className: 'dg-mediaFootMeta', key: 'meta' },
|
|
@@ -4295,7 +5551,279 @@ ${absPath}
|
|
|
4295
5551
|
* - 右键预览区、按 O、点底部按钮 三种方式切换
|
|
4296
5552
|
* - 切换时**保留当前缩放与位置**,否则切过去跳回 1x 就没法对比
|
|
4297
5553
|
*/
|
|
4298
|
-
|
|
5554
|
+
|
|
5555
|
+
/**
|
|
5556
|
+
* v0.16.0 图片裁剪 + 微信式圆形放大镜。
|
|
5557
|
+
*
|
|
5558
|
+
* 设计要点:
|
|
5559
|
+
* · 固定覆盖层(viewport 坐标),与查看器的 pan/zoom 解耦 —— 归一化坐标由图片在屏幕上的
|
|
5560
|
+
* 实际矩形算出,无论放大/平移都准确。
|
|
5561
|
+
* · 放大镜与最终裁图都从「全分辨率源图」取像素(fetchRaw 现拉),保证细节真实、不被预览缩放糊掉。
|
|
5562
|
+
* · 裁剪结果(归一化矩形)交给上层 callHost('cropImage'),落盘到源图同目录并标素材。
|
|
5563
|
+
* · 零自动发送红线不变:本组件只产出 PNG,落盘与登记由 Host 完成。
|
|
5564
|
+
*/
|
|
5565
|
+
function ImageCropper({ t, item, imgRef, onCrop, onCancel }) {
|
|
5566
|
+
const [rect, setRect] = React.useState(null) // {x,y,w,h} 归一化 0–1(相对图片宽高)
|
|
5567
|
+
const [drag, setDrag] = React.useState(null) // { mode:'new'|'move'|'nw'|'ne'|'sw'|'se', sx, sy, startRect }
|
|
5568
|
+
const [mag, setMag] = React.useState(null) // { cx, cy(视口), nx, ny(归一化) }
|
|
5569
|
+
const [full, setFull] = React.useState(null) // 全分辨率 HTMLImageElement
|
|
5570
|
+
const [busy, setBusy] = React.useState(false)
|
|
5571
|
+
const magCanvasRef = React.useRef(null)
|
|
5572
|
+
|
|
5573
|
+
// 载入全分辨率源图(放大镜取真实像素 + 最终裁图)
|
|
5574
|
+
React.useEffect(() => {
|
|
5575
|
+
let alive = true
|
|
5576
|
+
const controller = new AbortController()
|
|
5577
|
+
fetchRaw(item.relPath, controller.signal)
|
|
5578
|
+
.then((blob) => {
|
|
5579
|
+
if (!alive) return
|
|
5580
|
+
const url = URL.createObjectURL(blob)
|
|
5581
|
+
const im = new Image()
|
|
5582
|
+
im.onload = () => { if (alive) setFull(im) }
|
|
5583
|
+
im.onerror = () => {}
|
|
5584
|
+
im.src = url
|
|
5585
|
+
})
|
|
5586
|
+
.catch(() => {})
|
|
5587
|
+
return () => { alive = false; controller.abort() }
|
|
5588
|
+
}, [item.relPath])
|
|
5589
|
+
|
|
5590
|
+
// 图片在屏幕上的实际矩形(getBoundingClientRect 含 zoom/pan 变换后的真实位置)
|
|
5591
|
+
const imgRect = () => {
|
|
5592
|
+
const el = imgRef.current
|
|
5593
|
+
if (el === null) return null
|
|
5594
|
+
const r = el.getBoundingClientRect()
|
|
5595
|
+
if (r.width <= 0 || r.height <= 0) return null
|
|
5596
|
+
return r
|
|
5597
|
+
}
|
|
5598
|
+
const toNorm = (clientX, clientY) => {
|
|
5599
|
+
const r = imgRect()
|
|
5600
|
+
if (r === null) return null
|
|
5601
|
+
return {
|
|
5602
|
+
x: clamp01((clientX - r.left) / r.width),
|
|
5603
|
+
y: clamp01((clientY - r.top) / r.height),
|
|
5604
|
+
}
|
|
5605
|
+
}
|
|
5606
|
+
// 由拖拽状态与新归一化点计算矩形;负宽高先规范化再夹到图像内。
|
|
5607
|
+
const computeRect = (d, n) => {
|
|
5608
|
+
if (d.mode === 'new') {
|
|
5609
|
+
return {
|
|
5610
|
+
x: Math.min(d.sx, n.x), y: Math.min(d.sy, n.y),
|
|
5611
|
+
w: Math.abs(n.x - d.sx), h: Math.abs(n.y - d.sy),
|
|
5612
|
+
}
|
|
5613
|
+
}
|
|
5614
|
+
const s = d.startRect
|
|
5615
|
+
if (s === null) return null
|
|
5616
|
+
let x = s.x
|
|
5617
|
+
let y = s.y
|
|
5618
|
+
let w = s.w
|
|
5619
|
+
let h = s.h
|
|
5620
|
+
if (d.mode === 'move') {
|
|
5621
|
+
x = clamp01(s.x + (n.x - d.sx))
|
|
5622
|
+
y = clamp01(s.y + (n.y - d.sy))
|
|
5623
|
+
x = Math.min(x, 1 - s.w)
|
|
5624
|
+
y = Math.min(y, 1 - s.h)
|
|
5625
|
+
return { x: Math.max(0, x), y: Math.max(0, y), w: s.w, h: s.h }
|
|
5626
|
+
}
|
|
5627
|
+
if (d.mode === 'se') { w = n.x - s.x; h = n.y - s.y }
|
|
5628
|
+
else if (d.mode === 'ne') { y = n.y; w = n.x - s.x; h = (s.y + s.h) - n.y }
|
|
5629
|
+
else if (d.mode === 'sw') { x = n.x; w = (s.x + s.w) - n.x; h = n.y - s.y }
|
|
5630
|
+
else if (d.mode === 'nw') { x = n.x; y = n.y; w = (s.x + s.w) - n.x; h = (s.y + s.h) - n.y }
|
|
5631
|
+
if (w < 0) { x += w; w = -w }
|
|
5632
|
+
if (h < 0) { y += h; h = -h }
|
|
5633
|
+
x = clamp01(x); y = clamp01(y); w = clamp01(w); h = clamp01(h)
|
|
5634
|
+
if (x + w > 1) w = 1 - x
|
|
5635
|
+
if (y + h > 1) h = 1 - y
|
|
5636
|
+
return { x, y, w, h }
|
|
5637
|
+
}
|
|
5638
|
+
const startDrag = (e, mode) => {
|
|
5639
|
+
e.preventDefault()
|
|
5640
|
+
e.stopPropagation()
|
|
5641
|
+
const n = toNorm(e.clientX, e.clientY)
|
|
5642
|
+
if (n === null) return
|
|
5643
|
+
try { e.currentTarget.setPointerCapture(e.pointerId) } catch {}
|
|
5644
|
+
setDrag({ mode, sx: n.x, sy: n.y, startRect: rect })
|
|
5645
|
+
}
|
|
5646
|
+
const onLayerDown = (e) => {
|
|
5647
|
+
const n = toNorm(e.clientX, e.clientY)
|
|
5648
|
+
if (n === null) return
|
|
5649
|
+
try { e.currentTarget.setPointerCapture(e.pointerId) } catch {}
|
|
5650
|
+
setDrag({ mode: 'new', sx: n.x, sy: n.y, startRect: null })
|
|
5651
|
+
setRect({ x: n.x, y: n.y, w: 0, h: 0 })
|
|
5652
|
+
}
|
|
5653
|
+
const onLayerMove = (e) => {
|
|
5654
|
+
const n = toNorm(e.clientX, e.clientY)
|
|
5655
|
+
if (n === null) return
|
|
5656
|
+
setMag({ cx: e.clientX, cy: e.clientY, nx: n.x, ny: n.y })
|
|
5657
|
+
if (drag === null) return
|
|
5658
|
+
setRect((cur) => computeRect(drag, n, cur))
|
|
5659
|
+
}
|
|
5660
|
+
const onLayerUp = () => setDrag(null)
|
|
5661
|
+
|
|
5662
|
+
// 放大镜绘制:从全分辨率源图取光标处的 3× 局部放大;越界区域填黑(与相机放大镜一致)。
|
|
5663
|
+
React.useEffect(() => {
|
|
5664
|
+
const canvas = magCanvasRef.current
|
|
5665
|
+
if (canvas === null || full === null || mag === null) return
|
|
5666
|
+
const ctx = canvas.getContext('2d')
|
|
5667
|
+
if (ctx === null) return
|
|
5668
|
+
const SIZE = canvas.width
|
|
5669
|
+
const FACTOR = 3
|
|
5670
|
+
const FW = full.naturalWidth
|
|
5671
|
+
const FH = full.naturalHeight
|
|
5672
|
+
const halfSrc = (SIZE / FACTOR) / 2
|
|
5673
|
+
const sx = clampNum(mag.nx * FW - halfSrc, 0, Math.max(0, FW - SIZE / FACTOR))
|
|
5674
|
+
const sy = clampNum(mag.ny * FH - halfSrc, 0, Math.max(0, FH - SIZE / FACTOR))
|
|
5675
|
+
ctx.fillStyle = '#000'
|
|
5676
|
+
ctx.fillRect(0, 0, SIZE, SIZE)
|
|
5677
|
+
ctx.drawImage(full, sx, sy, SIZE / FACTOR, SIZE / FACTOR, 0, 0, SIZE, SIZE)
|
|
5678
|
+
ctx.strokeStyle = 'rgba(255,255,255,0.85)'
|
|
5679
|
+
ctx.lineWidth = 1
|
|
5680
|
+
ctx.beginPath()
|
|
5681
|
+
ctx.moveTo(SIZE / 2, 0); ctx.lineTo(SIZE / 2, SIZE)
|
|
5682
|
+
ctx.moveTo(0, SIZE / 2); ctx.lineTo(SIZE, SIZE / 2)
|
|
5683
|
+
ctx.stroke()
|
|
5684
|
+
}, [mag, full])
|
|
5685
|
+
|
|
5686
|
+
const confirmCrop = () => {
|
|
5687
|
+
if (full === null || rect === null || rect.w < 0.02 || rect.h < 0.02) return
|
|
5688
|
+
setBusy(true)
|
|
5689
|
+
const FW = full.naturalWidth
|
|
5690
|
+
const FH = full.naturalHeight
|
|
5691
|
+
const sx = Math.round(rect.x * FW)
|
|
5692
|
+
const sy = Math.round(rect.y * FH)
|
|
5693
|
+
const sw = Math.max(1, Math.round(rect.w * FW))
|
|
5694
|
+
const sh = Math.max(1, Math.round(rect.h * FH))
|
|
5695
|
+
try {
|
|
5696
|
+
const canvas = document.createElement('canvas')
|
|
5697
|
+
canvas.width = sw
|
|
5698
|
+
canvas.height = sh
|
|
5699
|
+
const c2d = canvas.getContext('2d')
|
|
5700
|
+
if (c2d === null) throw new Error('no 2d context')
|
|
5701
|
+
c2d.drawImage(full, sx, sy, sw, sh, 0, 0, sw, sh)
|
|
5702
|
+
canvas.toBlob((blob) => {
|
|
5703
|
+
if (blob === null) { setBusy(false); return }
|
|
5704
|
+
const fr = new FileReader()
|
|
5705
|
+
fr.onload = () => {
|
|
5706
|
+
const dataUrl = String(fr.result)
|
|
5707
|
+
const comma = dataUrl.indexOf(',')
|
|
5708
|
+
const base64 = comma >= 0 ? dataUrl.slice(comma + 1) : dataUrl
|
|
5709
|
+
setBusy(false)
|
|
5710
|
+
onCrop(base64)
|
|
5711
|
+
}
|
|
5712
|
+
fr.onerror = () => setBusy(false)
|
|
5713
|
+
fr.readAsDataURL(blob)
|
|
5714
|
+
}, 'image/png')
|
|
5715
|
+
} catch (error) {
|
|
5716
|
+
setBusy(false)
|
|
5717
|
+
console.error('dsh-gallery: 裁剪失败', error)
|
|
5718
|
+
}
|
|
5719
|
+
}
|
|
5720
|
+
|
|
5721
|
+
const r = imgRect()
|
|
5722
|
+
const rectStyle = rect !== null && r !== null
|
|
5723
|
+
? {
|
|
5724
|
+
left: `${r.left + rect.x * r.width}px`,
|
|
5725
|
+
top: `${r.top + rect.y * r.height}px`,
|
|
5726
|
+
width: `${rect.w * r.width}px`,
|
|
5727
|
+
height: `${rect.h * r.height}px`,
|
|
5728
|
+
}
|
|
5729
|
+
: {}
|
|
5730
|
+
const magX = mag !== null ? clampNum(mag.cx + 72, 72, (typeof window !== 'undefined' ? window.innerWidth : 9999) - 72) : 0
|
|
5731
|
+
const magY = mag !== null ? clampNum(mag.cy - 72, 72, (typeof window !== 'undefined' ? window.innerHeight : 9999) - 72) : 0
|
|
5732
|
+
|
|
5733
|
+
/**
|
|
5734
|
+
* 操作面板跟着选区走(微信截图式):水平居中于裁剪框、贴在框下方 12px;
|
|
5735
|
+
* 若框贴着窗口下沿,翻到框上方。还没画框时贴在图片下沿中央。
|
|
5736
|
+
* 全部用视口坐标 —— cropLayer 是 fixed inset:0,absolute 子元素即相对视口。
|
|
5737
|
+
*/
|
|
5738
|
+
const PANEL_W = 300
|
|
5739
|
+
const PANEL_H = 118
|
|
5740
|
+
const panelStyle = (() => {
|
|
5741
|
+
const vw = Number(typeof window !== 'undefined' ? window.innerWidth : 0) || 1280
|
|
5742
|
+
const vh = Number(typeof window !== 'undefined' ? window.innerHeight : 0) || 800
|
|
5743
|
+
const maxLeft = Math.max(8, vw - PANEL_W - 8)
|
|
5744
|
+
const maxTop = Math.max(8, vh - PANEL_H - 8)
|
|
5745
|
+
const hasBox = rect !== null && r !== null && rect.w > 0.02 && rect.h > 0.02
|
|
5746
|
+
let cx
|
|
5747
|
+
let boxTop
|
|
5748
|
+
let boxBottom
|
|
5749
|
+
if (hasBox) {
|
|
5750
|
+
boxTop = r.top + rect.y * r.height
|
|
5751
|
+
const boxLeft = r.left + rect.x * r.width
|
|
5752
|
+
cx = boxLeft + (rect.w * r.width) / 2
|
|
5753
|
+
boxBottom = boxTop + rect.h * r.height
|
|
5754
|
+
} else if (r !== null) {
|
|
5755
|
+
cx = r.left + r.width / 2
|
|
5756
|
+
boxTop = r.top
|
|
5757
|
+
boxBottom = r.bottom - PANEL_H - 16
|
|
5758
|
+
} else {
|
|
5759
|
+
cx = vw / 2
|
|
5760
|
+
boxTop = 0
|
|
5761
|
+
boxBottom = vh - PANEL_H - 20
|
|
5762
|
+
}
|
|
5763
|
+
const left = clampNum(cx - PANEL_W / 2, 8, maxLeft)
|
|
5764
|
+
let top = hasBox ? boxBottom + 12 : boxBottom
|
|
5765
|
+
if (hasBox && top + PANEL_H > vh - 8) top = boxTop - PANEL_H - 12
|
|
5766
|
+
return { left: `${left}px`, top: `${clampNum(top, 8, maxTop)}px` }
|
|
5767
|
+
})()
|
|
5768
|
+
|
|
5769
|
+
return h('div', {
|
|
5770
|
+
className: 'dg-cropLayer',
|
|
5771
|
+
key: 'cropLayer',
|
|
5772
|
+
'data-gallery-crop-layer': true,
|
|
5773
|
+
onPointerDown: onLayerDown,
|
|
5774
|
+
onPointerMove: onLayerMove,
|
|
5775
|
+
onPointerUp: onLayerUp,
|
|
5776
|
+
onPointerLeave: () => { setMag(null); setDrag(null) },
|
|
5777
|
+
onContextMenu: (e) => e.preventDefault(),
|
|
5778
|
+
}, [
|
|
5779
|
+
rect !== null
|
|
5780
|
+
? h('div', {
|
|
5781
|
+
className: 'dg-cropRect',
|
|
5782
|
+
key: 'rect',
|
|
5783
|
+
'data-gallery-crop-rect': true,
|
|
5784
|
+
style: rectStyle,
|
|
5785
|
+
onPointerDown: (e) => startDrag(e, 'move'),
|
|
5786
|
+
}, [
|
|
5787
|
+
h('div', { className: 'dg-cropHandle dg-cropHandleNW', key: 'nw', onPointerDown: (e) => startDrag(e, 'nw') }),
|
|
5788
|
+
h('div', { className: 'dg-cropHandle dg-cropHandleNE', key: 'ne', onPointerDown: (e) => startDrag(e, 'ne') }),
|
|
5789
|
+
h('div', { className: 'dg-cropHandle dg-cropHandleSW', key: 'sw', onPointerDown: (e) => startDrag(e, 'sw') }),
|
|
5790
|
+
h('div', { className: 'dg-cropHandle dg-cropHandleSE', key: 'se', onPointerDown: (e) => startDrag(e, 'se') }),
|
|
5791
|
+
])
|
|
5792
|
+
: null,
|
|
5793
|
+
mag !== null && full !== null
|
|
5794
|
+
? h('div', {
|
|
5795
|
+
className: 'dg-cropMagnifier',
|
|
5796
|
+
key: 'mag',
|
|
5797
|
+
style: { left: `${magX}px`, top: `${magY}px` },
|
|
5798
|
+
}, h('canvas', { ref: magCanvasRef, width: 130, height: 130, key: 'magCanvas' }))
|
|
5799
|
+
: null,
|
|
5800
|
+
h('div', {
|
|
5801
|
+
className: 'dg-cropPanel',
|
|
5802
|
+
key: 'panel',
|
|
5803
|
+
'data-gallery-crop-panel': true,
|
|
5804
|
+
style: panelStyle,
|
|
5805
|
+
onPointerDown: (e) => e.stopPropagation(),
|
|
5806
|
+
onPointerMove: (e) => e.stopPropagation(), // 别让放大镜跟着面板上的鼠标乱跑
|
|
5807
|
+
onPointerEnter: () => setMag(null), // 鼠标进面板就收起放大镜
|
|
5808
|
+
}, [
|
|
5809
|
+
h('div', { className: 'dg-cropPanelHead', key: 'h' }, t('cropEnter')),
|
|
5810
|
+
h('div', { className: 'dg-cropPanelHint', key: 'hint' }, t('cropHint')),
|
|
5811
|
+
h('div', { className: 'dg-cropPanelOps', key: 'ops' }, [
|
|
5812
|
+
h('button', {
|
|
5813
|
+
type: 'button', className: 'dg-btn', key: 'cancel',
|
|
5814
|
+
onClick: (e) => { e.stopPropagation(); onCancel() },
|
|
5815
|
+
}, t('cropCancel')),
|
|
5816
|
+
h('button', {
|
|
5817
|
+
type: 'button', className: 'dg-btn dg-btnPrimary', key: 'ok', disabled: busy,
|
|
5818
|
+
'data-gallery-crop-confirm': true,
|
|
5819
|
+
onClick: (e) => { e.stopPropagation(); confirmCrop() },
|
|
5820
|
+
}, busy ? t('cropSaving') : t('cropConfirm')),
|
|
5821
|
+
]),
|
|
5822
|
+
]),
|
|
5823
|
+
])
|
|
5824
|
+
}
|
|
5825
|
+
|
|
5826
|
+
function Viewer({ items, index, onClose, onStep, t, initialOriginal, annotations, onAddAnnotation, onRemoveAnnotation, onExportMask, onCrop }) {
|
|
4299
5827
|
const item = items[index]
|
|
4300
5828
|
// 文件预览已改走 DSH 原生侧栏(session 地址);此处 Viewer 仅处理图片,isText 恒 false,
|
|
4301
5829
|
// 下面的文本分支保留为死代码,供 v0.2 若需插件内预览时复用
|
|
@@ -4384,6 +5912,7 @@ ${absPath}
|
|
|
4384
5912
|
originalRequestedRef.current = false
|
|
4385
5913
|
setOriginalMode(false)
|
|
4386
5914
|
setOriginal({ status: 'idle' })
|
|
5915
|
+
setCropActive(false)
|
|
4387
5916
|
return () => {
|
|
4388
5917
|
if (originalAbortRef.current !== null) originalAbortRef.current.abort()
|
|
4389
5918
|
}
|
|
@@ -4553,6 +6082,8 @@ ${absPath}
|
|
|
4553
6082
|
// 中键按下时阻止浏览器默认的自动滚动
|
|
4554
6083
|
/** 批注模式(v0.14.0):开启后指针交给 SVG 标注层,查看器的缩放/拖拽暂停。 */
|
|
4555
6084
|
const [annotActive, setAnnotActive] = React.useState(false)
|
|
6085
|
+
/** v0.16.0 裁剪模式:开启后覆盖裁剪层(含放大镜),查看器自身的平移/点击关闭暂停。 */
|
|
6086
|
+
const [cropActive, setCropActive] = React.useState(false)
|
|
4556
6087
|
const annotsOfCurrent = React.useMemo(
|
|
4557
6088
|
() => (typeof annotations === 'function' ? annotations(item) : []),
|
|
4558
6089
|
[annotations, item],
|
|
@@ -4568,6 +6099,8 @@ ${absPath}
|
|
|
4568
6099
|
const onClickBody = (event) => {
|
|
4569
6100
|
// 标注模式下点图只用于画框,不能顺手把大图关掉(老板 20:32 指出的冲突)
|
|
4570
6101
|
if (annotActive) return
|
|
6102
|
+
// v0.16.0 裁剪模式下点击被裁剪层接管,不应关闭查看器
|
|
6103
|
+
if (cropActive) return
|
|
4571
6104
|
event.preventDefault()
|
|
4572
6105
|
if (lastDragMoved.current === true) {
|
|
4573
6106
|
lastDragMoved.current = false
|
|
@@ -4839,6 +6372,36 @@ ${absPath}
|
|
|
4839
6372
|
setAnnotActive((v) => !v)
|
|
4840
6373
|
},
|
|
4841
6374
|
}, annotActive ? `✎ ${t('annotExit')}` : `✎ ${t('annotEnter')}`),
|
|
6375
|
+
// v0.16.0 裁剪:开关裁剪覆盖层(含放大镜);裁剪态下查看器自身的平移/关闭暂停
|
|
6376
|
+
// 只在图片真正就绪时出现 —— 二进制/加载失败没有可裁的像素
|
|
6377
|
+
state.status === 'ready'
|
|
6378
|
+
? h('button', {
|
|
6379
|
+
type: 'button',
|
|
6380
|
+
className: `dg-btn${cropActive ? ' dg-btnActive' : ''}`,
|
|
6381
|
+
key: 'crop',
|
|
6382
|
+
'data-gallery-crop-toggle': true,
|
|
6383
|
+
'aria-pressed': cropActive,
|
|
6384
|
+
title: cropActive ? t('cropExit') : t('cropEnter'),
|
|
6385
|
+
onClick: (event) => {
|
|
6386
|
+
event.stopPropagation()
|
|
6387
|
+
setCropActive((v) => !v)
|
|
6388
|
+
},
|
|
6389
|
+
}, cropActive ? `✂ ${t('cropExit')}` : `✂ ${t('cropEnter')}`)
|
|
6390
|
+
: null,
|
|
6391
|
+
// 「拖到对话」确定性入口:Viewer 大图本身 draggable:false + pointerEvents:none
|
|
6392
|
+
// (标注态的中键平移 / 右键多边形都依赖它),所以不改大图,另给一个可拖按钮。
|
|
6393
|
+
// 这个按钮同时是「新 client.js 已加载」的可见标记 —— 看到它就说明面板不是旧模块。
|
|
6394
|
+
h('button', {
|
|
6395
|
+
type: 'button',
|
|
6396
|
+
className: 'dg-btn',
|
|
6397
|
+
key: 'dragAsset',
|
|
6398
|
+
draggable: true,
|
|
6399
|
+
'data-gallery-relpath': item.relPath,
|
|
6400
|
+
'data-gallery-dragviewer': true,
|
|
6401
|
+
title: '拖到对话:附带绝对路径 / 主遮罩 / 填充预览文件名',
|
|
6402
|
+
'aria-label': '拖到对话',
|
|
6403
|
+
onClick: (event) => event.stopPropagation(),
|
|
6404
|
+
}, '⇥ 拖到对话'),
|
|
4842
6405
|
h('button', { type: 'button', className: 'dg-btn', key: 'prev', onClick: () => onStep(-1), 'aria-label': t('prev') }, '‹'),
|
|
4843
6406
|
h('button', { type: 'button', className: 'dg-btn', key: 'next', onClick: () => onStep(1), 'aria-label': t('next') }, '›'),
|
|
4844
6407
|
h('button', { type: 'button', className: 'dg-btn', key: 'close', onClick: onClose, 'aria-label': t('close') }, '✕'),
|
|
@@ -4908,6 +6471,10 @@ ${absPath}
|
|
|
4908
6471
|
transformTransition: dragRef.current === null ? 'transform 120ms ease-out' : 'none',
|
|
4909
6472
|
onAdd: (a) => { onAddAnnotation?.(item, a) },
|
|
4910
6473
|
onRemove: (id) => { onRemoveAnnotation?.(item, id) },
|
|
6474
|
+
// 遮罩导出:把「当前这张图 + 这条矩形批注」一起交给上层执行
|
|
6475
|
+
onExportMask: typeof onExportMask === 'function'
|
|
6476
|
+
? (a) => onExportMask(item, a)
|
|
6477
|
+
: undefined,
|
|
4911
6478
|
}),
|
|
4912
6479
|
]))
|
|
4913
6480
|
: state.status === 'binary'
|
|
@@ -4916,6 +6483,20 @@ ${absPath}
|
|
|
4916
6483
|
state.status === 'failed' ? `${t('thumbFailed')}:${item.name}` : t('loading')),
|
|
4917
6484
|
),
|
|
4918
6485
|
slider,
|
|
6486
|
+
// v0.16.0 裁剪覆盖层:裁剪模式开启时显示(含微信式放大镜),固定在视口上、与 pan/zoom 解耦
|
|
6487
|
+
cropActive && isImageItem(item) && !isText && state.status === 'ready'
|
|
6488
|
+
? h(ImageCropper, {
|
|
6489
|
+
key: 'cropper',
|
|
6490
|
+
t,
|
|
6491
|
+
item,
|
|
6492
|
+
imgRef,
|
|
6493
|
+
onCrop: (base64) => {
|
|
6494
|
+
setCropActive(false)
|
|
6495
|
+
if (typeof onCrop === 'function') onCrop(item.relPath, base64)
|
|
6496
|
+
},
|
|
6497
|
+
onCancel: () => setCropActive(false),
|
|
6498
|
+
})
|
|
6499
|
+
: null,
|
|
4919
6500
|
]),
|
|
4920
6501
|
// 原图加载状态条:失败要能看见并知道原因,否则用户以为图坏了
|
|
4921
6502
|
original.status === 'failed'
|
|
@@ -4969,19 +6550,106 @@ ${absPath}
|
|
|
4969
6550
|
* 不会被放大、也不会被平移拖出视野。
|
|
4970
6551
|
*/
|
|
4971
6552
|
|
|
4972
|
-
function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, frameMs, zoom, pan, imgBox, transformTransition }) {
|
|
6553
|
+
function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, frameMs, zoom, pan, imgBox, transformTransition, onExportMask }) {
|
|
4973
6554
|
const [draft, setDraft] = React.useState(null)
|
|
4974
6555
|
const [editing, setEditing] = React.useState(null)
|
|
4975
6556
|
/** 已点下第一下、等第二下定终点时的起点(归一化坐标)。 */
|
|
4976
6557
|
const [pendingStart, setPendingStart] = React.useState(null)
|
|
4977
6558
|
const svgRef = React.useRef(null)
|
|
6559
|
+
/** 遮罩导出提示(成功/失败),2.5 秒自动消失。 */
|
|
6560
|
+
const [maskNotice, setMaskNotice] = React.useState(null)
|
|
6561
|
+
const [maskBusy, setMaskBusy] = React.useState(false)
|
|
6562
|
+
/** 右键多边形选区:归一化顶点 / 橡皮筋终点 / 正在拖动的顶点下标。 */
|
|
6563
|
+
const [poly, setPoly] = React.useState([])
|
|
6564
|
+
const [polyHover, setPolyHover] = React.useState(null)
|
|
6565
|
+
const [polyDrag, setPolyDrag] = React.useState(null)
|
|
6566
|
+
/** v0.14.7 选区填充色(红 / 绿 / 蓝),同时决定导出的彩色预览图颜色。 */
|
|
6567
|
+
const [fillColor, setFillColor] = React.useState('green')
|
|
6568
|
+
/** SVG 无 viewBox,polygon 的 points 只认用户单位 px,不能用百分比。 */
|
|
6569
|
+
const boxW = Math.max(1, Number(imgBox?.width) || 1)
|
|
6570
|
+
const boxH = Math.max(1, Number(imgBox?.height) || 1)
|
|
4978
6571
|
|
|
4979
|
-
|
|
6572
|
+
React.useEffect(() => {
|
|
6573
|
+
if (maskNotice === null) return undefined
|
|
6574
|
+
const timer = setTimeout(() => setMaskNotice(null), 2500)
|
|
6575
|
+
return () => clearTimeout(timer)
|
|
6576
|
+
}, [maskNotice])
|
|
6577
|
+
|
|
6578
|
+
/** 只把「导出」交给上层;提示与防连点留在本层,查看器其余交互一概不动。 */
|
|
6579
|
+
const runExportMask = (a) => {
|
|
6580
|
+
if (typeof onExportMask !== 'function' || maskBusy) return
|
|
6581
|
+
setMaskBusy(true)
|
|
6582
|
+
setMaskNotice({ kind: 'ok', text: t('maskExporting') })
|
|
6583
|
+
Promise.resolve()
|
|
6584
|
+
.then(() => onExportMask(a))
|
|
6585
|
+
.then((r) => setMaskNotice({ kind: 'ok', text: format(t('maskSaved'), { name: r?.name ?? '' }) }))
|
|
6586
|
+
.catch((error) => setMaskNotice({
|
|
6587
|
+
kind: 'failed',
|
|
6588
|
+
text: format(t('maskFailed'), { message: String(error?.message ?? error) }),
|
|
6589
|
+
}))
|
|
6590
|
+
.finally(() => setMaskBusy(false))
|
|
6591
|
+
}
|
|
6592
|
+
|
|
6593
|
+
/**
|
|
6594
|
+
* 闭合多边形 → 立刻生成遮罩资产。老板要的是「右键点完就形成区域遮罩」,
|
|
6595
|
+
* 所以闭合即导出,不再多一层确认;顶点/面积在闭合前一直可见可撤销。
|
|
6596
|
+
* 零自动发送红线不变:这里只落盘遮罩资产,草稿仍要人工发送。
|
|
6597
|
+
*/
|
|
6598
|
+
const commitPolygon = (points) => {
|
|
6599
|
+
if (points.length < POLYGON_LIMITS.min) {
|
|
6600
|
+
setMaskNotice({ kind: 'failed', text: t('polyTooFew') })
|
|
6601
|
+
return
|
|
6602
|
+
}
|
|
6603
|
+
if (polygonArea(points) < POLYGON_LIMITS.minArea) {
|
|
6604
|
+
setMaskNotice({ kind: 'failed', text: t('polyDegenerate') })
|
|
6605
|
+
return
|
|
6606
|
+
}
|
|
6607
|
+
const box = polygonBBox(points)
|
|
6608
|
+
setPoly([])
|
|
6609
|
+
setPolyHover(null)
|
|
6610
|
+
setPolyDrag(null)
|
|
6611
|
+
runExportMask({
|
|
6612
|
+
shape: 'polygon',
|
|
6613
|
+
...box,
|
|
6614
|
+
polygon: points.map((p) => [clamp01(p.x), clamp01(p.y)]),
|
|
6615
|
+
fillColor,
|
|
6616
|
+
note: '',
|
|
6617
|
+
})
|
|
6618
|
+
}
|
|
6619
|
+
|
|
6620
|
+
/** Enter 闭合 / Backspace 撤销一点 / Escape 清空;输入框有焦点时不抢键。 */
|
|
6621
|
+
React.useEffect(() => {
|
|
6622
|
+
if (!active || poly.length === 0 || editing !== null) return undefined
|
|
6623
|
+
const onKey = (e) => {
|
|
6624
|
+
const el = e.target
|
|
6625
|
+
if (el !== null && (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA'
|
|
6626
|
+
|| (typeof el.closest === 'function' && el.closest('[data-gallery-annot-editor]') !== null))) return
|
|
6627
|
+
if (e.key === 'Enter' && poly.length >= POLYGON_LIMITS.min) {
|
|
6628
|
+
e.preventDefault()
|
|
6629
|
+
commitPolygon(poly)
|
|
6630
|
+
} else if (e.key === 'Backspace') {
|
|
6631
|
+
e.preventDefault()
|
|
6632
|
+
setPoly((cur) => cur.slice(0, -1))
|
|
6633
|
+
setPolyHover(null)
|
|
6634
|
+
} else if (e.key === 'Escape') {
|
|
6635
|
+
e.preventDefault()
|
|
6636
|
+
setPoly([])
|
|
6637
|
+
setPolyHover(null)
|
|
6638
|
+
}
|
|
6639
|
+
}
|
|
6640
|
+
window.addEventListener('keydown', onKey)
|
|
6641
|
+
return () => window.removeEventListener('keydown', onKey)
|
|
6642
|
+
}, [active, poly, editing])
|
|
6643
|
+
|
|
6644
|
+
/** 退出标注模式时清掉未保存的草稿,避免输入框/虚线框/顶点残留。 */
|
|
4980
6645
|
React.useEffect(() => {
|
|
4981
6646
|
if (active) return undefined
|
|
4982
6647
|
setDraft(null)
|
|
4983
6648
|
setEditing(null)
|
|
4984
6649
|
setPendingStart(null)
|
|
6650
|
+
setPoly([])
|
|
6651
|
+
setPolyHover(null)
|
|
6652
|
+
setPolyDrag(null)
|
|
4985
6653
|
return undefined
|
|
4986
6654
|
}, [active])
|
|
4987
6655
|
|
|
@@ -5039,10 +6707,35 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
5039
6707
|
// 放大状态下查看器会在 pointerdown 里 setPointerCapture;这里必须拦住冒泡,
|
|
5040
6708
|
// 否则弹窗内的输入框/按钮会被「拖图」逻辑抢走,表现为点击无反应。
|
|
5041
6709
|
event.stopPropagation()
|
|
6710
|
+
// 右键(2):多边形遮罩选区逐点采集(4–20 点)。点回首点 / 满 20 点 / Enter / 面板按钮都可闭合。
|
|
6711
|
+
// 左键矩形批注与右键多边形互斥,避免两套草稿同时活着互相打架。
|
|
6712
|
+
if (event.button === 2) {
|
|
6713
|
+
event.preventDefault()
|
|
6714
|
+
const p = toNorm(event)
|
|
6715
|
+
if (p === null) return
|
|
6716
|
+
clearPending()
|
|
6717
|
+
const first = poly[0]
|
|
6718
|
+
const nearFirst = poly.length >= POLYGON_LIMITS.min && first !== undefined
|
|
6719
|
+
&& Math.hypot(p.x - first.x, p.y - first.y) <= POLYGON_LIMITS.closeRadius
|
|
6720
|
+
if (nearFirst || poly.length >= POLYGON_LIMITS.max) {
|
|
6721
|
+
commitPolygon(poly)
|
|
6722
|
+
return
|
|
6723
|
+
}
|
|
6724
|
+
const next = [...poly, p]
|
|
6725
|
+
setPoly(next)
|
|
6726
|
+
setPolyHover(null)
|
|
6727
|
+
if (next.length >= POLYGON_LIMITS.max) commitPolygon(next)
|
|
6728
|
+
return
|
|
6729
|
+
}
|
|
5042
6730
|
if (event.button !== 0) return
|
|
5043
6731
|
const p = toNorm(event)
|
|
5044
6732
|
if (p === null) return
|
|
5045
6733
|
event.preventDefault()
|
|
6734
|
+
if (poly.length > 0) {
|
|
6735
|
+
setPoly([])
|
|
6736
|
+
setPolyHover(null)
|
|
6737
|
+
setPolyDrag(null)
|
|
6738
|
+
}
|
|
5046
6739
|
try { event.currentTarget.setPointerCapture(event.pointerId) } catch { /* 忽略 */ }
|
|
5047
6740
|
|
|
5048
6741
|
// 上一处还在输入 → 先存档,第二处才按得下去(v0.14.2 ⑤)
|
|
@@ -5068,6 +6761,13 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
5068
6761
|
const onPointerMove = (event) => {
|
|
5069
6762
|
// 中键平移进行中:标注层一律不插手,让查看器独占指针(双保险,主保险是中键不再拦冒泡)
|
|
5070
6763
|
if (dragging) return
|
|
6764
|
+
// 多边形橡皮筋:只在没有矩形草稿、没有说明弹窗时跟随指针
|
|
6765
|
+
if (active && poly.length > 0 && draft === null && editing === null) {
|
|
6766
|
+
event.stopPropagation()
|
|
6767
|
+
const p = toNorm(event)
|
|
6768
|
+
if (p !== null) setPolyHover(p)
|
|
6769
|
+
return
|
|
6770
|
+
}
|
|
5071
6771
|
if (!active || draft === null || editing !== null) return
|
|
5072
6772
|
event.stopPropagation()
|
|
5073
6773
|
const p = toNorm(event)
|
|
@@ -5138,6 +6838,22 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
5138
6838
|
}, a.note || ''),
|
|
5139
6839
|
])
|
|
5140
6840
|
}
|
|
6841
|
+
// v0.14.7 已保存的多边形批注:用选区当时的填充色画实心半透明多边形,
|
|
6842
|
+
// 而不是退化成外接矩形(矩形批注仍走下面的老分支)。
|
|
6843
|
+
if (a.shape === 'polygon') {
|
|
6844
|
+
const pts = normalizePolygon(a.polygon, boxW, boxH)
|
|
6845
|
+
if (pts !== null) {
|
|
6846
|
+
return h('g', { key: a.id }, [
|
|
6847
|
+
h('polygon', {
|
|
6848
|
+
key: 'fill',
|
|
6849
|
+
points: pts.map((q) => `${q[0]},${q[1]}`).join(' '),
|
|
6850
|
+
fill: maskFillRgba(a.fillColor, MASK_FILL_ALPHA),
|
|
6851
|
+
stroke: maskFillRgba(a.fillColor, MASK_STROKE_ALPHA), strokeWidth: MASK_STROKE_PX,
|
|
6852
|
+
vectorEffect: 'non-scaling-stroke',
|
|
6853
|
+
}),
|
|
6854
|
+
])
|
|
6855
|
+
}
|
|
6856
|
+
}
|
|
5141
6857
|
// rect / arrow 都先按矩形画(arrow 后续可细化),带编号与说明文字
|
|
5142
6858
|
return h('g', { key: a.id }, [
|
|
5143
6859
|
rectPair('r', a, '#4a7cff', false),
|
|
@@ -5180,6 +6896,62 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
5180
6896
|
])
|
|
5181
6897
|
: null)
|
|
5182
6898
|
|
|
6899
|
+
/** 右键多边形预览:实心半透明填充 + 可拖顶点 + 橡皮筋(点回首点即闭合)。 */
|
|
6900
|
+
const polyShape = poly.length === 0 ? null : (() => {
|
|
6901
|
+
const toPx = (p) => [p.x * boxW, p.y * boxH]
|
|
6902
|
+
const pts = poly.map(toPx)
|
|
6903
|
+
const tip = polyHover === null ? null : toPx(polyHover)
|
|
6904
|
+
const ready = poly.length >= POLYGON_LIMITS.min
|
|
6905
|
+
// 纯色半透明填充 0.4 / 轮廓 0.7,颜色跟操作条色板联动;主遮罩 alpha 契约不受影响。
|
|
6906
|
+
const strokeColor = maskFillRgba(fillColor, MASK_STROKE_ALPHA)
|
|
6907
|
+
const fillNode = h('polygon', {
|
|
6908
|
+
key: 'polyFill',
|
|
6909
|
+
points: pts.map((q) => `${q[0]},${q[1]}`).join(' '),
|
|
6910
|
+
fill: maskFillRgba(fillColor, ready ? MASK_FILL_ALPHA : 0.12),
|
|
6911
|
+
stroke: strokeColor, strokeWidth: MASK_STROKE_PX, vectorEffect: 'non-scaling-stroke',
|
|
6912
|
+
strokeDasharray: ready ? undefined : '6 4',
|
|
6913
|
+
'data-gallery-poly': String(poly.length),
|
|
6914
|
+
})
|
|
6915
|
+
const bandNode = tip === null ? null : h('polyline', {
|
|
6916
|
+
key: 'polyBand',
|
|
6917
|
+
points: [pts[pts.length - 1], tip, pts[0]].map((q) => `${q[0]},${q[1]}`).join(' '),
|
|
6918
|
+
fill: 'none', stroke: strokeColor, strokeWidth: 1.5, strokeDasharray: '5 4',
|
|
6919
|
+
vectorEffect: 'non-scaling-stroke', opacity: 0.85,
|
|
6920
|
+
})
|
|
6921
|
+
const dots = pts.map((q, idx) => h('circle', {
|
|
6922
|
+
key: 'v' + idx,
|
|
6923
|
+
cx: q[0], cy: q[1], r: idx === 0 && ready ? 6 : 4,
|
|
6924
|
+
fill: idx === 0 ? '#ffd166' : strokeColor, stroke: '#000', strokeWidth: 1,
|
|
6925
|
+
vectorEffect: 'non-scaling-stroke', style: { cursor: 'move' },
|
|
6926
|
+
'data-gallery-poly-vertex': String(idx),
|
|
6927
|
+
onPointerDown: (e) => {
|
|
6928
|
+
if (e.button !== 0) return
|
|
6929
|
+
e.stopPropagation()
|
|
6930
|
+
e.preventDefault()
|
|
6931
|
+
setPolyDrag(idx)
|
|
6932
|
+
try { e.currentTarget.setPointerCapture(e.pointerId) } catch { /* 忽略 */ }
|
|
6933
|
+
},
|
|
6934
|
+
onPointerMove: (e) => {
|
|
6935
|
+
if (polyDrag !== idx) return
|
|
6936
|
+
e.stopPropagation()
|
|
6937
|
+
e.preventDefault()
|
|
6938
|
+
const p = toNorm(e)
|
|
6939
|
+
if (p !== null) setPoly((cur) => cur.map((q2, i) => (i === idx ? p : q2)))
|
|
6940
|
+
},
|
|
6941
|
+
onPointerUp: (e) => {
|
|
6942
|
+
if (polyDrag !== idx) return
|
|
6943
|
+
e.stopPropagation()
|
|
6944
|
+
setPolyDrag(null)
|
|
6945
|
+
},
|
|
6946
|
+
}))
|
|
6947
|
+
const labels = pts.map((q, idx) => h('text', {
|
|
6948
|
+
key: 'n' + idx, x: q[0] + 7, y: q[1] - 7,
|
|
6949
|
+
fill: '#fff', fontSize: 11, stroke: '#000', strokeWidth: 0.6,
|
|
6950
|
+
style: { paintOrder: 'stroke', pointerEvents: 'none' },
|
|
6951
|
+
}, String(idx + 1)))
|
|
6952
|
+
return h('g', { key: 'poly' }, [fillNode, bandNode, ...dots, ...labels])
|
|
6953
|
+
})()
|
|
6954
|
+
|
|
5183
6955
|
/** 弹窗与批注列表:一律拦住指针/点击冒泡,别让查看器抢事件。 */
|
|
5184
6956
|
const blockViewer = {
|
|
5185
6957
|
onPointerDown: (e) => e.stopPropagation(),
|
|
@@ -5188,6 +6960,58 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
5188
6960
|
onContextMenu: (e) => { e.preventDefault(); e.stopPropagation() },
|
|
5189
6961
|
}
|
|
5190
6962
|
|
|
6963
|
+
/** 多边形选区操作条:点数 / 外接框 / 面积实时可见,闭合前可撤销、可清空。 */
|
|
6964
|
+
const polyBox = poly.length > 0 ? polygonBBox(poly) : null
|
|
6965
|
+
const polyPanel = poly.length === 0 ? null : h('div', {
|
|
6966
|
+
className: 'dg-polyPanel', key: 'polyPanel', 'data-gallery-poly-panel': true,
|
|
6967
|
+
...blockViewer,
|
|
6968
|
+
}, [
|
|
6969
|
+
h('div', { className: 'dg-polyPanelHead', key: 'h' }, t('polyTitle')),
|
|
6970
|
+
h('div', { className: 'dg-polyPanelInfo', key: 'i', 'data-gallery-poly-info': true },
|
|
6971
|
+
format(t('polyInfo'), {
|
|
6972
|
+
n: String(poly.length), max: String(POLYGON_LIMITS.max),
|
|
6973
|
+
x: Math.round(polyBox.x * 100), y: Math.round(polyBox.y * 100),
|
|
6974
|
+
w: Math.round(polyBox.w * 100), h: Math.round(polyBox.h * 100),
|
|
6975
|
+
area: (polygonArea(poly) * 100).toFixed(2),
|
|
6976
|
+
})),
|
|
6977
|
+
h('div', { className: 'dg-polyPanelHint', key: 'p' }, t('polyHint')),
|
|
6978
|
+
h('div', { className: 'dg-polyColors', key: 'colors', 'data-gallery-poly-colors': true }, [
|
|
6979
|
+
h('span', { className: 'dg-polyColorsLabel', key: 'l' }, t('polyFillColor')),
|
|
6980
|
+
...MASK_FILL_KEYS.map((k) => h('button', {
|
|
6981
|
+
type: 'button', key: k,
|
|
6982
|
+
className: `dg-polyColorBtn${fillColor === k ? ' is-active' : ''}`,
|
|
6983
|
+
'data-gallery-poly-color': k,
|
|
6984
|
+
style: { background: maskFillRgba(k, 0.9) },
|
|
6985
|
+
title: MASK_FILL_COLORS[k].label,
|
|
6986
|
+
onClick: (e) => { e.stopPropagation(); e.preventDefault(); setFillColor(k) },
|
|
6987
|
+
}, MASK_FILL_COLORS[k].label)),
|
|
6988
|
+
]),
|
|
6989
|
+
h('div', { className: 'dg-polyPanelOps', key: 'o' }, [
|
|
6990
|
+
h('button', {
|
|
6991
|
+
type: 'button', className: 'dg-btn dg-btnSmall', key: 'u',
|
|
6992
|
+
disabled: poly.length === 0,
|
|
6993
|
+
onClick: (e) => {
|
|
6994
|
+
e.stopPropagation(); e.preventDefault()
|
|
6995
|
+
setPoly((cur) => cur.slice(0, -1))
|
|
6996
|
+
setPolyHover(null)
|
|
6997
|
+
},
|
|
6998
|
+
}, t('polyUndo')),
|
|
6999
|
+
h('button', {
|
|
7000
|
+
type: 'button', className: 'dg-btn dg-btnSmall', key: 'c',
|
|
7001
|
+
onClick: (e) => {
|
|
7002
|
+
e.stopPropagation(); e.preventDefault()
|
|
7003
|
+
setPoly([]); setPolyHover(null); setPolyDrag(null)
|
|
7004
|
+
},
|
|
7005
|
+
}, t('polyClear')),
|
|
7006
|
+
h('button', {
|
|
7007
|
+
type: 'button', className: 'dg-btn dg-btnSmall dg-polyClose', key: 's',
|
|
7008
|
+
'data-gallery-poly-close': true,
|
|
7009
|
+
disabled: maskBusy || poly.length < POLYGON_LIMITS.min,
|
|
7010
|
+
onClick: (e) => { e.stopPropagation(); e.preventDefault(); commitPolygon(poly) },
|
|
7011
|
+
}, t('polyClose')),
|
|
7012
|
+
]),
|
|
7013
|
+
])
|
|
7014
|
+
|
|
5191
7015
|
const editor = editing === null ? null : h('div', {
|
|
5192
7016
|
className: 'dg-annotEditor',
|
|
5193
7017
|
key: 'editor',
|
|
@@ -5234,6 +7058,15 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
5234
7058
|
}, [
|
|
5235
7059
|
h('span', { className: 'dg-annotIndex', key: 'i' }, `#${idx + 1}`),
|
|
5236
7060
|
h('span', { className: 'dg-annotText', key: 't' }, a.note || t('annotNoNote')),
|
|
7061
|
+
// 只有矩形框能导出遮罩;箭头 / 纯文字标注没有面积,不显示这个动作。
|
|
7062
|
+
typeof onExportMask === 'function' && (a.shape ?? 'rect') === 'rect' && a.w > 0 && a.h > 0
|
|
7063
|
+
? h('button', {
|
|
7064
|
+
type: 'button', className: 'dg-btn dg-btnSmall', key: 'm',
|
|
7065
|
+
title: t('maskExportHint'), 'data-gallery-mask-export': a.id,
|
|
7066
|
+
disabled: maskBusy,
|
|
7067
|
+
onClick: (e) => { e.stopPropagation(); e.preventDefault(); runExportMask(a) },
|
|
7068
|
+
}, t('maskExport'))
|
|
7069
|
+
: null,
|
|
5237
7070
|
h('button', {
|
|
5238
7071
|
type: 'button', className: 'dg-btn dg-btnSmall dg-btnIcon', key: 'x',
|
|
5239
7072
|
title: t('delete'), 'data-gallery-annot-remove': a.id,
|
|
@@ -5241,6 +7074,12 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
5241
7074
|
}, '✕'),
|
|
5242
7075
|
])))
|
|
5243
7076
|
|
|
7077
|
+
const maskNoticeNode = maskNotice === null ? null : h('div', {
|
|
7078
|
+
className: `dg-maskNotice dg-maskNotice-${maskNotice.kind}`,
|
|
7079
|
+
key: 'maskNotice', 'data-gallery-mask-notice': true,
|
|
7080
|
+
...blockViewer,
|
|
7081
|
+
}, maskNotice.text)
|
|
7082
|
+
|
|
5244
7083
|
return h('div', {
|
|
5245
7084
|
className: 'dg-annotWrap', key: 'annot',
|
|
5246
7085
|
style: { position: 'absolute', inset: 0, zIndex: 3 },
|
|
@@ -5267,9 +7106,11 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
5267
7106
|
onPointerCancel: () => { setDraft(null); setPendingStart(null) },
|
|
5268
7107
|
onClick: active ? (event) => { event.preventDefault(); event.stopPropagation() } : undefined,
|
|
5269
7108
|
onContextMenu: active ? (event) => { event.preventDefault(); event.stopPropagation() } : undefined,
|
|
5270
|
-
}, [draftShape, ...shapes]),
|
|
7109
|
+
}, [draftShape, polyShape, ...shapes]),
|
|
5271
7110
|
list,
|
|
5272
7111
|
editor,
|
|
7112
|
+
polyPanel,
|
|
7113
|
+
maskNoticeNode,
|
|
5273
7114
|
])
|
|
5274
7115
|
}
|
|
5275
7116
|
|
|
@@ -5698,6 +7539,18 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
5698
7539
|
// 让回调(如右键菜单)读到最新值而不必重建依赖
|
|
5699
7540
|
const viewerRef = React.useRef(null)
|
|
5700
7541
|
React.useEffect(() => { viewerRef.current = viewer }, [viewer])
|
|
7542
|
+
/**
|
|
7543
|
+
* v0.16.2 粘贴落点:全局视图下「鼠标所在的对话组」。
|
|
7544
|
+
*
|
|
7545
|
+
* 此前粘贴固定用 slot 的 sessionId(= 当前对话),所以在任何历史对话组里
|
|
7546
|
+
* 粘贴都会跑到当前对话目录,老板报的正是这个。这里记下 hover / 右键所在的
|
|
7547
|
+
* 组(`{ sessionId, title, createdAt }`),粘贴时优先用它,没有才退回当前对话。
|
|
7548
|
+
*
|
|
7549
|
+
* 🔴 用 ref 不用 useState:onMouseEnter 改它不该触发整表重渲染;而且粘贴是
|
|
7550
|
+
* 异步事件,必须读「事件发生那一刻」的值,渲染期快照会过期。
|
|
7551
|
+
* null 表示「没有指定组」→ 落回当前对话。
|
|
7552
|
+
*/
|
|
7553
|
+
const pasteTargetRef = React.useRef(null)
|
|
5701
7554
|
const limiter = React.useRef(null)
|
|
5702
7555
|
if (limiter.current === null) limiter.current = createLimiter(MAX_CONCURRENT)
|
|
5703
7556
|
|
|
@@ -5767,6 +7620,12 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
5767
7620
|
* 不是需要长期保留的状态;确认态才落盘,两者语义不同)。
|
|
5768
7621
|
*/
|
|
5769
7622
|
const [annotations, setAnnotations] = React.useState({})
|
|
7623
|
+
/**
|
|
7624
|
+
* 遮罩「原图 → 资产」的内存兜底(v0.14.5):导出成功后立刻登记,
|
|
7625
|
+
* 不用等列表重扫 / 翻页,反馈草稿马上就能带出遮罩路径。刷新后由扫描侧
|
|
7626
|
+
* 的 maskOf 元数据继续兜底;这里同样只存内存,不写 marks、不落盘批注。
|
|
7627
|
+
*/
|
|
7628
|
+
const [maskOverrides, setMaskOverrides] = React.useState({})
|
|
5770
7629
|
const selectMode = batchMode || selected.size > 0
|
|
5771
7630
|
/** 清废稿确认对话框:null=关闭;{ phase:'list'|'deleting', files, digest, count } */
|
|
5772
7631
|
const [discardDialog, setDiscardDialog] = React.useState(null)
|
|
@@ -5781,6 +7640,8 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
5781
7640
|
// 组内中间产物开关已并入顶部分类筛选(v0.14.3)。
|
|
5782
7641
|
// 右键菜单:{ item, absPath, x, y }
|
|
5783
7642
|
const [ctxMenu, setCtxMenu] = React.useState(null)
|
|
7643
|
+
// v0.16.0:右键空白处(非卡片)弹出的「粘贴」菜单
|
|
7644
|
+
const [pasteMenu, setPasteMenu] = React.useState(null)
|
|
5784
7645
|
// 删除确认:{ item, busy }
|
|
5785
7646
|
const [deleteTarget, setDeleteTarget] = React.useState(null)
|
|
5786
7647
|
// ---- v0.14.2:反馈篮条目(marks,一次性)与手动标签(labels,持久)分开 ----
|
|
@@ -6076,7 +7937,7 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
6076
7937
|
* 分组视图下卡片来自 `data.groups[i].files`,与当前页的扁平 `items` 是两个不同的集合,
|
|
6077
7938
|
* 直接用全局 `items` 的索引会「点哪张都跳出同一张图」。
|
|
6078
7939
|
*/
|
|
6079
|
-
const openViewer = React.useCallback((index, anchorEl, list, asOriginal) => {
|
|
7940
|
+
const openViewer = React.useCallback((index, anchorEl, list, asOriginal, origin) => {
|
|
6080
7941
|
const scroller = bodyScrollRef.current
|
|
6081
7942
|
if (scroller !== null) {
|
|
6082
7943
|
savedScrollRef.current = {
|
|
@@ -6095,7 +7956,14 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
6095
7956
|
width: it.width ?? null,
|
|
6096
7957
|
height: it.height ?? null,
|
|
6097
7958
|
}))
|
|
6098
|
-
|
|
7959
|
+
// v0.16.2:记住这张图属于哪个对话组 —— 裁剪产物要跟着来源组落盘,
|
|
7960
|
+
// 不能一律塞进当前对话(否则在历史对话的图上裁剪,产物跑到别的对话)。
|
|
7961
|
+
const originSid = String(origin?.sessionId ?? '')
|
|
7962
|
+
setViewer({
|
|
7963
|
+
index, list: snapshot, asOriginal: asOriginal === true,
|
|
7964
|
+
originSid: originSid === '__history__' ? '' : originSid,
|
|
7965
|
+
originTitle: originSid === '__history__' ? '' : String(origin?.title ?? ''),
|
|
7966
|
+
})
|
|
6099
7967
|
}, [])
|
|
6100
7968
|
|
|
6101
7969
|
/**
|
|
@@ -6109,7 +7977,8 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
6109
7977
|
const cur = viewerRef.current
|
|
6110
7978
|
if (cur !== null && Array.isArray(cur.list)) {
|
|
6111
7979
|
const i = cur.list.findIndex(samePath)
|
|
6112
|
-
|
|
7980
|
+
// 沿用原查看器已记录的来源组,别因为「查看原图」把归属丢掉
|
|
7981
|
+
if (i >= 0) { openViewer(i, null, cur.list, true, { sessionId: cur.originSid ?? '' }); return }
|
|
6113
7982
|
}
|
|
6114
7983
|
// 2) 全局 items(扁平/分页视图)
|
|
6115
7984
|
const flat = itemsRef.current ?? []
|
|
@@ -6121,7 +7990,7 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
6121
7990
|
for (const g of groups) {
|
|
6122
7991
|
if (!Array.isArray(g.files)) continue
|
|
6123
7992
|
const k = g.files.findIndex(samePath)
|
|
6124
|
-
if (k >= 0) { openViewer(k, null, g.files, true); return }
|
|
7993
|
+
if (k >= 0) { openViewer(k, null, g.files, true, { sessionId: g.sessionId }); return }
|
|
6125
7994
|
}
|
|
6126
7995
|
}
|
|
6127
7996
|
// 4) 兜底:单独一张(此时左右切图无意义,但不至于打不开)
|
|
@@ -6192,6 +8061,259 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
6192
8061
|
}
|
|
6193
8062
|
}, [load, kind, scope, page])
|
|
6194
8063
|
|
|
8064
|
+
// ---- v0.16.0 粘贴:剪贴板图片/文件作为「素材」落当前对话 ----
|
|
8065
|
+
/** 粘贴提示(成功/失败),3 秒自动消失。 */
|
|
8066
|
+
const [pasteNotice, setPasteNotice] = React.useState(null)
|
|
8067
|
+
React.useEffect(() => {
|
|
8068
|
+
if (pasteNotice === null) return undefined
|
|
8069
|
+
const timer = setTimeout(() => setPasteNotice(null), 3000)
|
|
8070
|
+
return () => clearTimeout(timer)
|
|
8071
|
+
}, [pasteNotice])
|
|
8072
|
+
|
|
8073
|
+
const showPasteNotice = React.useCallback((kindOf, text) => {
|
|
8074
|
+
setPasteNotice({ kind: kindOf, text })
|
|
8075
|
+
}, [])
|
|
8076
|
+
|
|
8077
|
+
/**
|
|
8078
|
+
* v0.16.2 粘贴落点解析:hover 的对话组优先,否则 slot 的当前对话。
|
|
8079
|
+
*
|
|
8080
|
+
* 这两个 helper 必须在所有粘贴 / 裁剪回调**之前**声明 ——
|
|
8081
|
+
* 回调体内的引用是惰性的,但一旦写进 useCallback 依赖数组就会在渲染时
|
|
8082
|
+
* 立即求值,引用后面声明的 const 会直接 TDZ 白屏(v0.16.0 踩过)。
|
|
8083
|
+
*/
|
|
8084
|
+
const pasteTargetSid = () => {
|
|
8085
|
+
// 查看器打开时,图是从哪个组点开的就落哪个组 —— 覆盖层是模态的,
|
|
8086
|
+
// 此时鼠标已经不在组的 DOM 里,靠 hover 记的落点会被 mouseleave 清掉。
|
|
8087
|
+
const viewerSid = String(viewerRef.current?.originSid ?? '')
|
|
8088
|
+
if (viewerSid !== '') return viewerSid
|
|
8089
|
+
const sid = String(pasteTargetRef.current?.sessionId ?? '')
|
|
8090
|
+
return sid !== '' ? sid : String(sessionId ?? '')
|
|
8091
|
+
}
|
|
8092
|
+
/** 提示文案里的落点名:组标题,没有则「当前对话」。 */
|
|
8093
|
+
const pasteTargetLabel = () => {
|
|
8094
|
+
const viewerTitle = String(viewerRef.current?.originTitle ?? '')
|
|
8095
|
+
if (viewerTitle !== '') return viewerTitle
|
|
8096
|
+
const target = pasteTargetRef.current
|
|
8097
|
+
if (target === null || String(target.sessionId ?? '') === '') return t('pasteTargetCurrent')
|
|
8098
|
+
return String(target.title ?? '') || t('pasteTargetCurrent')
|
|
8099
|
+
}
|
|
8100
|
+
|
|
8101
|
+
/**
|
|
8102
|
+
* v0.16.0 从**系统剪贴板**读文件列表并落盘 —— 音视频 / 任意文件的主通道。
|
|
8103
|
+
*
|
|
8104
|
+
* Chromium 的 paste 事件对「资源管理器 Ctrl+C 的文件」不给 File 对象,
|
|
8105
|
+
* 只能让 Host 用 PowerShell 读 CF_HDROP;系统剪贴板没有文件列表时,
|
|
8106
|
+
* Host 会退回插件内部剪贴板(复制→粘贴仍闭环)。
|
|
8107
|
+
*
|
|
8108
|
+
* 🔴 必须定义在 doPasteEvent 之前:useCallback 的依赖数组是渲染时立即求值的。
|
|
8109
|
+
* @returns {Promise<boolean>} 是否确实粘进了文件
|
|
8110
|
+
*/
|
|
8111
|
+
const pasteFromClipboard = React.useCallback(async () => {
|
|
8112
|
+
let payload = null
|
|
8113
|
+
try { payload = await callHost('clipboardFiles') } catch (error) {
|
|
8114
|
+
showPasteNotice('fail', format(t('pasteFailed'), { message: String(error?.message ?? error) }))
|
|
8115
|
+
return false
|
|
8116
|
+
}
|
|
8117
|
+
const files = Array.isArray(payload?.files) ? payload.files : []
|
|
8118
|
+
if (files.length === 0) return false
|
|
8119
|
+
let result = null
|
|
8120
|
+
try {
|
|
8121
|
+
result = await callHost('pasteFiles', {
|
|
8122
|
+
// v0.16.2:落到「鼠标所在的对话组」,没指定才落当前对话
|
|
8123
|
+
sessionId: pasteTargetSid(),
|
|
8124
|
+
srcPaths: files.map((f) => f.path),
|
|
8125
|
+
})
|
|
8126
|
+
} catch (error) {
|
|
8127
|
+
showPasteNotice('fail', format(t('pasteFailed'), { message: String(error?.message ?? error) }))
|
|
8128
|
+
return false
|
|
8129
|
+
}
|
|
8130
|
+
const okFiles = Array.isArray(result?.files) ? result.files : []
|
|
8131
|
+
if (okFiles.length === 0) {
|
|
8132
|
+
showPasteNotice('fail', format(t('pasteFailed'), { message: result?.error?.message ?? 'pasteFiles failed' }))
|
|
8133
|
+
return false
|
|
8134
|
+
}
|
|
8135
|
+
showPasteNotice('ok', format(t('pasteFilesOk'), {
|
|
8136
|
+
n: okFiles.length, name: okFiles[0].name ?? '', target: pasteTargetLabel(),
|
|
8137
|
+
}))
|
|
8138
|
+
void refresh()
|
|
8139
|
+
if (Array.isArray(result?.failed) && result.failed.length > 0) {
|
|
8140
|
+
console.warn('dsh-gallery: 部分文件粘贴失败', result.failed)
|
|
8141
|
+
}
|
|
8142
|
+
return true
|
|
8143
|
+
}, [callHost, sessionId, refresh, t, showPasteNotice])
|
|
8144
|
+
|
|
8145
|
+
/** 真正落盘:把解析出的图片(images)/文件(files)交给 Host 分别粘贴。 */
|
|
8146
|
+
const ingestPasted = React.useCallback(async ({ images, files }) => {
|
|
8147
|
+
let okCount = 0
|
|
8148
|
+
let lastErr = ''
|
|
8149
|
+
let skipped = 0
|
|
8150
|
+
for (const file of images) {
|
|
8151
|
+
// 没有磁盘路径、MIME 又不是图片的(如复制的 PDF/zip):读成 base64 也过不了
|
|
8152
|
+
// PNG 校验,直接跳过并计数,最后给一条明确的「请拖入画廊」提示。
|
|
8153
|
+
const mime = typeof file?.type === 'string' ? file.type : ''
|
|
8154
|
+
if (mime !== '' && !mime.startsWith('image/')) { skipped += 1; continue }
|
|
8155
|
+
try {
|
|
8156
|
+
const dataUrl = await fileToDataUrl(file)
|
|
8157
|
+
const comma = dataUrl.indexOf(',')
|
|
8158
|
+
const base64 = comma >= 0 ? dataUrl.slice(comma + 1) : dataUrl
|
|
8159
|
+
const payload = await callHost('pasteImage', { sessionId: pasteTargetSid(), data: base64, ext: 'png' })
|
|
8160
|
+
if (payload?.ok === true) { okCount += 1; showPasteNotice('ok', format(t('pasteImageOk'), { name: payload.name ?? '', target: pasteTargetLabel() })) }
|
|
8161
|
+
else lastErr = payload?.error?.message ?? 'pasteImage failed'
|
|
8162
|
+
} catch (error) {
|
|
8163
|
+
lastErr = String(error?.message ?? error)
|
|
8164
|
+
}
|
|
8165
|
+
}
|
|
8166
|
+
for (const f of files) {
|
|
8167
|
+
try {
|
|
8168
|
+
const payload = await callHost('pasteFile', { sessionId: pasteTargetSid(), srcPath: f.path, name: f.name })
|
|
8169
|
+
if (payload?.ok === true) { okCount += 1; showPasteNotice('ok', format(t('pasteFileOk'), { name: payload.name ?? '', target: pasteTargetLabel() })) }
|
|
8170
|
+
else lastErr = payload?.error?.message ?? 'pasteFile failed'
|
|
8171
|
+
} catch (error) {
|
|
8172
|
+
lastErr = String(error?.message ?? error)
|
|
8173
|
+
}
|
|
8174
|
+
}
|
|
8175
|
+
if (okCount > 0) {
|
|
8176
|
+
void refresh()
|
|
8177
|
+
if (lastErr !== '') console.warn('dsh-gallery: 部分粘贴失败', lastErr)
|
|
8178
|
+
} else if (lastErr !== '') {
|
|
8179
|
+
showPasteNotice('fail', format(t('pasteFailed'), { message: lastErr }))
|
|
8180
|
+
} else if (skipped > 0) {
|
|
8181
|
+
showPasteNotice('fail', t('pasteUnsupported'))
|
|
8182
|
+
} else {
|
|
8183
|
+
showPasteNotice('fail', t('pasteEmpty'))
|
|
8184
|
+
}
|
|
8185
|
+
}, [sessionId, refresh, t, showPasteNotice])
|
|
8186
|
+
|
|
8187
|
+
/** Ctrl+V:优先吃 paste 事件的图片/文件;事件没给文件信息时退到系统剪贴板。 */
|
|
8188
|
+
const doPasteEvent = React.useCallback(async (clipboardData, event) => {
|
|
8189
|
+
const parsed = extractFromPasteEvent(clipboardData)
|
|
8190
|
+
if (parsed.images.length > 0 || parsed.files.length > 0) {
|
|
8191
|
+
if (typeof event?.preventDefault === 'function') event.preventDefault()
|
|
8192
|
+
await ingestPasted(parsed)
|
|
8193
|
+
return true
|
|
8194
|
+
}
|
|
8195
|
+
return pasteFromClipboard()
|
|
8196
|
+
}, [ingestPasted, pasteFromClipboard])
|
|
8197
|
+
|
|
8198
|
+
/** 右键菜单「粘贴」:先图片(Clipboard API),再文件(含音视频,系统剪贴板)。 */
|
|
8199
|
+
const doPaste = React.useCallback(async () => {
|
|
8200
|
+
const images = await readClipboardImages()
|
|
8201
|
+
if (images.length > 0) {
|
|
8202
|
+
await ingestPasted({ images, files: [] })
|
|
8203
|
+
return
|
|
8204
|
+
}
|
|
8205
|
+
const got = await pasteFromClipboard()
|
|
8206
|
+
if (!got) showPasteNotice('fail', t('pasteEmpty'))
|
|
8207
|
+
}, [ingestPasted, pasteFromClipboard, showPasteNotice, t])
|
|
8208
|
+
|
|
8209
|
+
/** v0.16.0 图片裁剪:把裁剪出的 PNG base64 交给 Host 落盘(源图同目录,标素材)。 */
|
|
8210
|
+
const doCrop = React.useCallback(async (relPath, base64) => {
|
|
8211
|
+
try {
|
|
8212
|
+
// v0.16.2:裁剪产物跟「这张图所属的对话」走,而不是永远跟当前对话。
|
|
8213
|
+
// 来源组由 openViewer 记在 viewer.originSid(打开查看器时就知道图来自哪个组);
|
|
8214
|
+
// 没有来源组(扁平视图)时才退回 hover 组 / 当前对话。
|
|
8215
|
+
// 组标题 / 创建时间由 Host 读会话日志补(这里读不到 `data`:声明在后面,会 TDZ)。
|
|
8216
|
+
const originSid = String(viewerRef.current?.originSid ?? '')
|
|
8217
|
+
const payload = await callHost('cropImage', {
|
|
8218
|
+
path: relPath, data: base64,
|
|
8219
|
+
sessionId: originSid !== '' ? originSid : pasteTargetSid(),
|
|
8220
|
+
})
|
|
8221
|
+
if (payload?.ok === false) throw new Error(payload?.error?.message ?? 'cropImage failed')
|
|
8222
|
+
const originLabel = String(viewerRef.current?.originTitle ?? '') || pasteTargetLabel()
|
|
8223
|
+
showPasteNotice('ok', format(t('cropSaved'), { name: payload?.name ?? '', target: originLabel }))
|
|
8224
|
+
void refresh()
|
|
8225
|
+
} catch (error) {
|
|
8226
|
+
showPasteNotice('fail', format(t('cropFailed'), { message: String(error?.message ?? error) }))
|
|
8227
|
+
}
|
|
8228
|
+
}, [callHost, refresh, t, showPasteNotice, sessionId])
|
|
8229
|
+
|
|
8230
|
+
/**
|
|
8231
|
+
* v0.16.0 粘贴事件接线。
|
|
8232
|
+
*
|
|
8233
|
+
* 🔴 这些必须放在 `doPasteEvent` **之后**:React 的 useCallback / useEffect
|
|
8234
|
+
* 会在渲染时立即求值依赖数组,若引用后面才声明的 const 会触发 TDZ
|
|
8235
|
+
* (ReferenceError: Cannot access 'doPasteEvent' before initialization),
|
|
8236
|
+
* 整个 GalleryBody 渲染抛错 → 面板白屏。v0.16.0 首版就踩过这个坑。
|
|
8237
|
+
*/
|
|
8238
|
+
/** 画廊区域粘贴(Ctrl+V)。只拦截含图片/文件的粘贴,纯文本放行。 */
|
|
8239
|
+
const onBodyPaste = React.useCallback((event) => {
|
|
8240
|
+
if (event?.defaultPrevented === true) return
|
|
8241
|
+
if (isPlainTextPaste(event.clipboardData)) return
|
|
8242
|
+
// 🔴 preventDefault 必须在同步阶段调用:后面读系统剪贴板是异步的
|
|
8243
|
+
event.preventDefault()
|
|
8244
|
+
void doPasteEvent(event.clipboardData, event)
|
|
8245
|
+
}, [doPasteEvent])
|
|
8246
|
+
|
|
8247
|
+
/**
|
|
8248
|
+
* Ctrl+V 的真正落点。
|
|
8249
|
+
*
|
|
8250
|
+
* 面板的 body 是普通 div,**不可聚焦**,浏览器不会把 paste 事件投给它。
|
|
8251
|
+
* 所以必须挂 document 级监听,再用两道门控避免抢走对话输入框的粘贴:
|
|
8252
|
+
* ① 鼠标必须悬停在本面板上(hoveringRef);
|
|
8253
|
+
* ② 焦点不能在 input/textarea/contenteditable 里。
|
|
8254
|
+
*/
|
|
8255
|
+
const hoveringRef = React.useRef(false)
|
|
8256
|
+
React.useEffect(() => {
|
|
8257
|
+
const onDocPaste = (event) => {
|
|
8258
|
+
if (hoveringRef.current !== true) return
|
|
8259
|
+
if (event?.defaultPrevented === true) return
|
|
8260
|
+
const ae = document.activeElement
|
|
8261
|
+
const tag = (ae?.tagName ?? '').toLowerCase()
|
|
8262
|
+
if (tag === 'input' || tag === 'textarea' || ae?.isContentEditable === true) return
|
|
8263
|
+
if (isPlainTextPaste(event.clipboardData)) return
|
|
8264
|
+
// 🔴 同步拦截:哪怕 paste 事件没给任何 File(资源管理器复制文件的常态),
|
|
8265
|
+
// 也要拦下来,异步让 Host 去读系统剪贴板的文件列表。
|
|
8266
|
+
event.preventDefault()
|
|
8267
|
+
void doPasteEvent(event.clipboardData, event)
|
|
8268
|
+
}
|
|
8269
|
+
document.addEventListener('paste', onDocPaste)
|
|
8270
|
+
return () => document.removeEventListener('paste', onDocPaste)
|
|
8271
|
+
}, [doPasteEvent])
|
|
8272
|
+
|
|
8273
|
+
/** 右键空白处(非卡片)弹出「粘贴」菜单。卡片已 stopPropagation,不会到这里。 */
|
|
8274
|
+
const onBodyContextMenu = React.useCallback((event) => {
|
|
8275
|
+
event.preventDefault()
|
|
8276
|
+
event.stopPropagation()
|
|
8277
|
+
// v0.16.2:右键落在哪个组里,粘贴就落到那个对话。
|
|
8278
|
+
// 卡片自己 stopPropagation,所以这里命中的是「组内空白处」。
|
|
8279
|
+
const el = typeof event.target?.closest === 'function'
|
|
8280
|
+
? event.target.closest('[data-gallery-group]')
|
|
8281
|
+
: null
|
|
8282
|
+
const sid = el === null ? '' : String(el.getAttribute('data-gallery-group') ?? '')
|
|
8283
|
+
const targetTitle = el === null ? '' : String(el.getAttribute('data-gallery-group-title') ?? '')
|
|
8284
|
+
pasteTargetRef.current = sid === '' || sid === '__history__'
|
|
8285
|
+
? null
|
|
8286
|
+
: { sessionId: sid, title: targetTitle, createdAt: 0 }
|
|
8287
|
+
setCtxMenu(null)
|
|
8288
|
+
setPasteMenu({
|
|
8289
|
+
x: event.clientX,
|
|
8290
|
+
y: event.clientY,
|
|
8291
|
+
targetLabel: pasteTargetRef.current === null ? t('pasteTargetCurrent') : (targetTitle || t('pasteTargetCurrent')),
|
|
8292
|
+
})
|
|
8293
|
+
}, [t])
|
|
8294
|
+
|
|
8295
|
+
/**
|
|
8296
|
+
* v0.16.0 复制资产(单文件右键 / 批量选择共用)。
|
|
8297
|
+
* 只把 roots 内的相对路径交给 Host,由 Host 解析绝对路径并写系统剪贴板;
|
|
8298
|
+
* 前端不拼路径、不改原文件、零自动发送。
|
|
8299
|
+
*/
|
|
8300
|
+
const doCopyAssets = React.useCallback(async (relPaths) => {
|
|
8301
|
+
const paths = [...new Set((Array.isArray(relPaths) ? relPaths : [])
|
|
8302
|
+
.map((p) => String(p ?? '')).filter((p) => p !== ''))]
|
|
8303
|
+
if (paths.length === 0) return
|
|
8304
|
+
try {
|
|
8305
|
+
const payload = await callHost('copyAssets', { paths })
|
|
8306
|
+
if (payload?.ok !== true) throw new Error(payload?.error?.message ?? 'copyAssets failed')
|
|
8307
|
+
const text = payload?.mode === 'internal' ? t('copyOkInternal') : t('copyOk')
|
|
8308
|
+
showPasteNotice('ok', format(text, { n: payload.count ?? paths.length }))
|
|
8309
|
+
if (Array.isArray(payload?.missed) && payload.missed.length > 0) {
|
|
8310
|
+
console.warn('dsh-gallery: 复制时跳过了不在扫描根目录内的项', payload.missed)
|
|
8311
|
+
}
|
|
8312
|
+
} catch (error) {
|
|
8313
|
+
showPasteNotice('fail', format(t('copyFailed'), { message: String(error?.message ?? error) }))
|
|
8314
|
+
}
|
|
8315
|
+
}, [callHost, t, showPasteNotice])
|
|
8316
|
+
|
|
6195
8317
|
React.useEffect(() => {
|
|
6196
8318
|
let alive = true
|
|
6197
8319
|
const controller = new AbortController()
|
|
@@ -6239,6 +8361,34 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
6239
8361
|
}, [viewer])
|
|
6240
8362
|
|
|
6241
8363
|
const data = view.status === 'ready' ? view.data : null
|
|
8364
|
+
// 拖图元信息索引:卡片容器只有 relPath,绝对路径与遮罩关联从这里查。
|
|
8365
|
+
// 分组视图的条目在 groups[].files 里,扁平视图在 items 里,两边都收。
|
|
8366
|
+
React.useEffect(() => {
|
|
8367
|
+
dragAssetIndex.clear()
|
|
8368
|
+
dragMaskByOverlay.clear()
|
|
8369
|
+
dragMaskBySource.clear()
|
|
8370
|
+
if (data === null) return undefined
|
|
8371
|
+
const root = String(data.root ?? '')
|
|
8372
|
+
const put = (it) => {
|
|
8373
|
+
// 主遮罩帧自带 maskOf,用它建立「预览图 / 原图 → 主遮罩」反查表。
|
|
8374
|
+
const m = it?.maskOf
|
|
8375
|
+
if (m !== null && m !== undefined && typeof m === 'object' && String(m.relPath ?? '') !== '') {
|
|
8376
|
+
const ov = String(m.overlayRelPath ?? '')
|
|
8377
|
+
const src = String(m.sourceRelPath ?? '')
|
|
8378
|
+
if (ov !== '' && !dragMaskByOverlay.has(ov)) dragMaskByOverlay.set(ov, m)
|
|
8379
|
+
if (src !== '' && !dragMaskBySource.has(src)) dragMaskBySource.set(src, m)
|
|
8380
|
+
}
|
|
8381
|
+
const rel = String(it?.relPath ?? '')
|
|
8382
|
+
if (rel !== '' && !dragAssetIndex.has(rel)) {
|
|
8383
|
+
dragAssetIndex.set(rel, { item: it, absPath: buildAbsPath(root, rel) })
|
|
8384
|
+
}
|
|
8385
|
+
}
|
|
8386
|
+
if (Array.isArray(data.items)) data.items.forEach(put)
|
|
8387
|
+
for (const g of (Array.isArray(data.groups) ? data.groups : [])) {
|
|
8388
|
+
if (Array.isArray(g.files)) g.files.forEach(put)
|
|
8389
|
+
}
|
|
8390
|
+
return undefined
|
|
8391
|
+
}, [data])
|
|
6242
8392
|
const items = data?.items ?? []
|
|
6243
8393
|
/**
|
|
6244
8394
|
* 顶部分类筛选后的可见条目(v0.14.3)。
|
|
@@ -6459,6 +8609,13 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
6459
8609
|
title: t('labelEditHint'),
|
|
6460
8610
|
onClick: () => openBatchLabelDialog(),
|
|
6461
8611
|
}, t('labelBatch')),
|
|
8612
|
+
// v0.16.0 批量复制:把选中资产写进系统剪贴板文件列表(跨对话调动)
|
|
8613
|
+
h('button', {
|
|
8614
|
+
type: 'button', className: 'dg-btn dg-btnSmall', key: 'copyFiles',
|
|
8615
|
+
'data-gallery-batch-copy': true,
|
|
8616
|
+
title: t('batchCopyHint'),
|
|
8617
|
+
onClick: () => { void doCopyAssets([...selected]) },
|
|
8618
|
+
}, `⧉ ${t('batchCopyFiles')} (${selectedCount})`),
|
|
6462
8619
|
h('button', {
|
|
6463
8620
|
type: 'button', className: 'dg-btn dg-btnSmall', key: 'clearmark',
|
|
6464
8621
|
'data-gallery-batch-clear': true,
|
|
@@ -6717,7 +8874,8 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
6717
8874
|
},
|
|
6718
8875
|
t,
|
|
6719
8876
|
absPath: buildAbsPath(data.root, item.relPath),
|
|
6720
|
-
|
|
8877
|
+
// v0.16.2:选段产物跟来源组走(groupKey 就是该组 sessionId)
|
|
8878
|
+
onPlay: (target) => setMediaPlaying({ ...target, originSessionId: groupKey }),
|
|
6721
8879
|
onContextMenu: openContextMenu,
|
|
6722
8880
|
}), item, `${groupKey}:${item.relPath}`)
|
|
6723
8881
|
}
|
|
@@ -6757,7 +8915,8 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
6757
8915
|
pos = list.findIndex((f) => f.relPath === opened.relPath)
|
|
6758
8916
|
}
|
|
6759
8917
|
if (pos < 0) return
|
|
6760
|
-
|
|
8918
|
+
// v0.16.2:把这张图所属的组带给查看器,裁剪产物要落回同一个对话
|
|
8919
|
+
openViewer(pos, anchorEl ?? null, list, false, { sessionId: groupKey, title: group.title ?? '' })
|
|
6761
8920
|
},
|
|
6762
8921
|
}), item, `${groupKey}:${item.relPath}`)
|
|
6763
8922
|
}
|
|
@@ -6800,6 +8959,17 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
6800
8959
|
key: `g:${headKey}`,
|
|
6801
8960
|
'data-gallery-group': headKey,
|
|
6802
8961
|
'data-gallery-group-current': group.isCurrent === true ? '1' : '0',
|
|
8962
|
+
'data-gallery-group-title': group.title ?? '',
|
|
8963
|
+
/**
|
|
8964
|
+
* v0.16.2:鼠标停在哪一组,粘贴/裁剪就落到那一组。
|
|
8965
|
+
* 「未归类」池没有对应会话,置 null 退回当前对话(不能凭空造目录)。
|
|
8966
|
+
*/
|
|
8967
|
+
onMouseEnter: () => {
|
|
8968
|
+
pasteTargetRef.current = group.isHistory === true || !group.sessionId
|
|
8969
|
+
? null
|
|
8970
|
+
: { sessionId: group.sessionId, title: group.title ?? '', createdAt: group.createdAt ?? 0 }
|
|
8971
|
+
},
|
|
8972
|
+
onMouseLeave: () => { pasteTargetRef.current = null },
|
|
6803
8973
|
}, [
|
|
6804
8974
|
h('button', {
|
|
6805
8975
|
type: 'button',
|
|
@@ -6857,12 +9027,21 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
6857
9027
|
])
|
|
6858
9028
|
})
|
|
6859
9029
|
|
|
6860
|
-
body = h('div', {
|
|
9030
|
+
body = h('div', {
|
|
9031
|
+
className: 'dg-bodyWrap',
|
|
9032
|
+
key: 'bodyWrap',
|
|
9033
|
+
// hover 门控:Ctrl+V 只在鼠标位于面板上时接管(见 hoveringRef 注释)
|
|
9034
|
+
onMouseEnter: () => { hoveringRef.current = true },
|
|
9035
|
+
// 离开面板就清掉粘贴落点,绝不让「上次 hover 的组」残留
|
|
9036
|
+
onMouseLeave: () => { hoveringRef.current = false; pasteTargetRef.current = null },
|
|
9037
|
+
}, [
|
|
6861
9038
|
h('div', {
|
|
6862
9039
|
className: `dg-body${kind === 'file' ? ' dg-fileBody' : ''}`,
|
|
6863
9040
|
key: 'body',
|
|
6864
9041
|
'data-gallery-body': true,
|
|
6865
9042
|
ref: bodyScrollRef,
|
|
9043
|
+
onPaste: onBodyPaste,
|
|
9044
|
+
onContextMenu: onBodyContextMenu,
|
|
6866
9045
|
}, [
|
|
6867
9046
|
h('div', { className: 'dg-groups', key: 'groups' }, groupNodes),
|
|
6868
9047
|
data.scan.capped
|
|
@@ -6916,12 +9095,19 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
6916
9095
|
onOpen: (opened, anchorEl) => openViewer(visibleItems.indexOf(opened), anchorEl, visibleItems),
|
|
6917
9096
|
}), item, item.relPath))
|
|
6918
9097
|
// 网格区外面包一层定位容器,右侧放滚动辅助列(无滚轮场景用)
|
|
6919
|
-
body = h('div', {
|
|
9098
|
+
body = h('div', {
|
|
9099
|
+
className: 'dg-bodyWrap',
|
|
9100
|
+
key: 'bodyWrap',
|
|
9101
|
+
onMouseEnter: () => { hoveringRef.current = true },
|
|
9102
|
+
onMouseLeave: () => { hoveringRef.current = false; pasteTargetRef.current = null },
|
|
9103
|
+
}, [
|
|
6920
9104
|
h('div', {
|
|
6921
9105
|
className: `dg-body${kind === 'file' ? ' dg-fileBody' : ''}`,
|
|
6922
9106
|
key: 'body',
|
|
6923
9107
|
'data-gallery-body': true,
|
|
6924
9108
|
ref: bodyScrollRef,
|
|
9109
|
+
onPaste: onBodyPaste,
|
|
9110
|
+
onContextMenu: onBodyContextMenu,
|
|
6925
9111
|
}, [
|
|
6926
9112
|
(visibleItems.length > 0
|
|
6927
9113
|
? h('div', { className: `dg-grid${kind === 'file' ? ' dg-fileGrid' : ''}${kind === 'media' ? ' dg-mediaGrid' : ''}`, key: 'grid' }, kind === 'media' ? mediaGridChildren : gridChildren)
|
|
@@ -6964,36 +9150,104 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
6964
9150
|
})
|
|
6965
9151
|
: null
|
|
6966
9152
|
|
|
9153
|
+
/**
|
|
9154
|
+
* 矩形框选批注 → ComfyUI 遮罩(v0.14.5 本机 MVP)。
|
|
9155
|
+
* 全链路只碰插件自己的 Host 路由:取原图 → 生成 RGBA PNG → 落盘 → 登记当前对话 → 重扫。
|
|
9156
|
+
* 不自动发送、不写会话日志、不请求 ComfyUI;路径由「遮罩来源」随反馈草稿带出,
|
|
9157
|
+
* 由老板确认后手动发送,模型再拿去回填 #369.image / #193.image。
|
|
9158
|
+
*/
|
|
9159
|
+
const exportMask = React.useCallback(async (item, annotation) => {
|
|
9160
|
+
const built = await buildMaskPng(item.relPath, annotation)
|
|
9161
|
+
// 右键多边形选区带归一化顶点;矩形批注没有这个字段,Host 仍按老契约处理。
|
|
9162
|
+
const isPolygon = Array.isArray(annotation?.polygon) && annotation.polygon.length >= 4
|
|
9163
|
+
const payload = await callHost('saveMask', {
|
|
9164
|
+
path: item.relPath,
|
|
9165
|
+
data: built.data,
|
|
9166
|
+
region: {
|
|
9167
|
+
x: clamp01(annotation.x), y: clamp01(annotation.y),
|
|
9168
|
+
w: clamp01(annotation.w), h: clamp01(annotation.h),
|
|
9169
|
+
},
|
|
9170
|
+
...(isPolygon ? {
|
|
9171
|
+
polygon: annotation.polygon,
|
|
9172
|
+
width: built.width,
|
|
9173
|
+
height: built.height,
|
|
9174
|
+
} : {}),
|
|
9175
|
+
// v0.14.7 彩色填充预览图:与主 alpha 遮罩一起落盘,颜色由界面色板决定
|
|
9176
|
+
...(built.overlay ? {
|
|
9177
|
+
overlay: built.overlay,
|
|
9178
|
+
fillColor: String(built.fillColor ?? 'green'),
|
|
9179
|
+
fillLabel: String(built.fillLabel ?? '绿'),
|
|
9180
|
+
} : {}),
|
|
9181
|
+
sessionId: sessionId ?? '',
|
|
9182
|
+
sessionTitle: Array.isArray(data?.groups)
|
|
9183
|
+
? (data.groups.find((g) => g.isCurrent)?.title ?? '')
|
|
9184
|
+
: '',
|
|
9185
|
+
sessionCreatedAt: Array.isArray(data?.groups)
|
|
9186
|
+
? (data.groups.find((g) => g.isCurrent)?.createdAt ?? 0)
|
|
9187
|
+
: 0,
|
|
9188
|
+
})
|
|
9189
|
+
if (payload?.ok === false) throw new Error(payload?.error?.message ?? 'saveMask failed')
|
|
9190
|
+
setMaskOverrides((prev) => ({
|
|
9191
|
+
...prev,
|
|
9192
|
+
[item.relPath]: {
|
|
9193
|
+
relPath: String(payload?.relPath ?? ''),
|
|
9194
|
+
sourceName: String(item.name ?? ''),
|
|
9195
|
+
region: payload?.region ?? null,
|
|
9196
|
+
polygon: payload?.polygon ?? null,
|
|
9197
|
+
polygonPx: payload?.polygonPx ?? null,
|
|
9198
|
+
bboxPx: payload?.bboxPx ?? null,
|
|
9199
|
+
areaPx: Number(payload?.areaPx) || 0,
|
|
9200
|
+
overlayRelPath: String(payload?.overlayRelPath ?? ''),
|
|
9201
|
+
fillColor: String(built.fillColor ?? 'green'),
|
|
9202
|
+
},
|
|
9203
|
+
}))
|
|
9204
|
+
void refresh()
|
|
9205
|
+
return payload
|
|
9206
|
+
}, [sessionId, data, refresh])
|
|
9207
|
+
|
|
6967
9208
|
// 查看器作为覆盖层叠在网格上:网格保持挂载,滚动位置不丢
|
|
6968
9209
|
// 用 viewer.list(打开时记录的快照),不用全局 items:
|
|
6969
9210
|
// 分组视图下卡片来自组内文件,与当前页的扁平 items 不是同一个集合。
|
|
6970
9211
|
const viewerLayer = viewer !== null && Array.isArray(viewer.list) && viewer.list.length > 0
|
|
6971
|
-
? h('div', {
|
|
9212
|
+
? h('div', {
|
|
9213
|
+
className: 'dg-viewerLayer',
|
|
9214
|
+
key: 'viewerLayer',
|
|
9215
|
+
// 查看器是独立覆盖层:鼠标在这里也要让 Ctrl+V 生效,
|
|
9216
|
+
// 落点由 pasteTargetSid 从 viewer.originSid 取(不是 hover 的组)。
|
|
9217
|
+
onMouseEnter: () => { hoveringRef.current = true },
|
|
9218
|
+
onMouseLeave: () => { hoveringRef.current = false },
|
|
9219
|
+
},
|
|
6972
9220
|
h(Viewer, {
|
|
6973
9221
|
annotations: annotationsOf,
|
|
6974
9222
|
onAddAnnotation: addAnnotation,
|
|
6975
9223
|
onRemoveAnnotation: removeAnnotation,
|
|
9224
|
+
onExportMask: exportMask,
|
|
6976
9225
|
items: viewer.list,
|
|
6977
9226
|
index: Math.min(viewer.index, viewer.list.length - 1),
|
|
6978
9227
|
t,
|
|
6979
9228
|
initialOriginal: viewer.asOriginal === true,
|
|
6980
9229
|
onClose: closeViewer,
|
|
6981
9230
|
onStep: step,
|
|
9231
|
+
onCrop: doCrop,
|
|
6982
9232
|
}))
|
|
6983
9233
|
: null
|
|
6984
9234
|
|
|
6985
9235
|
// 音视频播放器层(v0.10.1)
|
|
6986
|
-
//
|
|
9236
|
+
// 截帧要挂到对话:把当前组标题 / 创建时间一并传下去登记
|
|
9237
|
+
// v0.16.2:在全局视图里播放别组的素材,产物要落回**那个组**,
|
|
9238
|
+
// 标题/创建时间留空由 Host 读会话日志补(传错组名比留空更糟)。
|
|
9239
|
+
const mediaOriginSid = mediaPlaying === null ? '' : String(mediaPlaying.originSessionId ?? '')
|
|
9240
|
+
const mediaSameAsCurrent = mediaOriginSid === '' || mediaOriginSid === String(sessionId ?? '')
|
|
6987
9241
|
const mediaLayer = mediaPlaying !== null
|
|
6988
9242
|
? h(MediaPlayer, {
|
|
6989
9243
|
key: 'mediaPlayer',
|
|
6990
9244
|
item: mediaPlaying,
|
|
6991
9245
|
t,
|
|
6992
|
-
sessionId: sessionId ?? '',
|
|
6993
|
-
sessionTitle: Array.isArray(data?.groups)
|
|
9246
|
+
sessionId: mediaSameAsCurrent ? (sessionId ?? '') : mediaOriginSid,
|
|
9247
|
+
sessionTitle: mediaSameAsCurrent && Array.isArray(data?.groups)
|
|
6994
9248
|
? (data.groups.find((g) => g.isCurrent)?.title ?? '')
|
|
6995
9249
|
: '',
|
|
6996
|
-
sessionCreatedAt: Array.isArray(data?.groups)
|
|
9250
|
+
sessionCreatedAt: mediaSameAsCurrent && Array.isArray(data?.groups)
|
|
6997
9251
|
? (data.groups.find((g) => g.isCurrent)?.createdAt ?? 0)
|
|
6998
9252
|
: 0,
|
|
6999
9253
|
onSaved: () => { void refresh() },
|
|
@@ -7014,6 +9268,39 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
7014
9268
|
// 反馈篮(S3-a):列出已标记条目 + 带批注的卡片(v0.14.3 批注并入反馈)
|
|
7015
9269
|
// 批注属于「反馈」这一侧:它不进 marks(不参与表态/筛选/落盘),
|
|
7016
9270
|
// 但只要有批注,这张卡就出现在反馈篮里,草稿会按 Annotation N 逐条带出说明与框选区域。
|
|
9271
|
+
// 遮罩资产由扫描侧透传 maskOf.sourceRelPath;反查成「原图 → 遮罩」映射,
|
|
9272
|
+
// 这样原图的反馈条目(带框选批注)在草稿里会自动带出「遮罩来源」,无需人工复制路径。
|
|
9273
|
+
const allAssets = [
|
|
9274
|
+
...(Array.isArray(data?.items) ? data.items : []),
|
|
9275
|
+
...(Array.isArray(data?.groups)
|
|
9276
|
+
? data.groups.flatMap((gr) => (Array.isArray(gr.files) ? gr.files : []))
|
|
9277
|
+
: []),
|
|
9278
|
+
]
|
|
9279
|
+
const maskBySource = new Map()
|
|
9280
|
+
for (const asset of allAssets) {
|
|
9281
|
+
const m = asset.maskOf
|
|
9282
|
+
const source = String(m?.sourceRelPath ?? '')
|
|
9283
|
+
if (source === '' || maskBySource.has(source)) continue
|
|
9284
|
+
maskBySource.set(source, {
|
|
9285
|
+
relPath: String(asset.relPath ?? ''),
|
|
9286
|
+
sourceName: String(m?.sourceName ?? ''),
|
|
9287
|
+
region: m?.region ?? null,
|
|
9288
|
+
// v0.14.6/14.7:源图的反馈条目也要带全顶点坐标与彩色预览路径,
|
|
9289
|
+
// 否则这里会把扫描元数据「压扁」,草稿里只剩 region。
|
|
9290
|
+
polygon: Array.isArray(m?.polygon) ? m.polygon : null,
|
|
9291
|
+
polygonPx: Array.isArray(m?.polygonPx) ? m.polygonPx : null,
|
|
9292
|
+
bboxPx: Array.isArray(m?.bboxPx) ? m.bboxPx : null,
|
|
9293
|
+
areaPx: Number(m?.areaPx) || 0,
|
|
9294
|
+
imageWidth: Number(m?.imageWidth) || 0,
|
|
9295
|
+
imageHeight: Number(m?.imageHeight) || 0,
|
|
9296
|
+
overlayRelPath: String(m?.overlayRelPath ?? ''),
|
|
9297
|
+
fillColor: String(m?.fillColor ?? ''),
|
|
9298
|
+
})
|
|
9299
|
+
}
|
|
9300
|
+
// 刚导出、列表还没刷出遮罩时,用内存兜底覆盖(刷新后由扫描元数据接管)
|
|
9301
|
+
for (const [source, meta] of Object.entries(maskOverrides)) {
|
|
9302
|
+
if (String(meta?.relPath ?? '') !== '') maskBySource.set(source, meta)
|
|
9303
|
+
}
|
|
7017
9304
|
const feedbackPaths = Array.from(new Set([
|
|
7018
9305
|
...Object.keys(marks),
|
|
7019
9306
|
...Object.keys(annotations),
|
|
@@ -7031,6 +9318,7 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
7031
9318
|
category: found?.category ?? 'unknown',
|
|
7032
9319
|
sessionId: found?.sessionId,
|
|
7033
9320
|
frameOf: found?.frameOf,
|
|
9321
|
+
maskOf: maskBySource.get(relPath) ?? found?.maskOf,
|
|
7034
9322
|
mark: marks[relPath],
|
|
7035
9323
|
annotations: Array.isArray(annotations[relPath]) ? annotations[relPath] : [],
|
|
7036
9324
|
}
|
|
@@ -7092,7 +9380,11 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
7092
9380
|
onHide: () => { void doHide(ctxMenu.item) },
|
|
7093
9381
|
onReveal: () => { void doReveal(ctxMenu.item) },
|
|
7094
9382
|
onCopy: () => { void copyText(ctxMenu.absPath) },
|
|
9383
|
+
// v0.16.0 单文件复制:写系统剪贴板文件列表,可在别的对话粘贴
|
|
9384
|
+
onCopyFile: () => { void doCopyAssets([ctxMenu.item.relPath]) },
|
|
7095
9385
|
onDelete: () => setDeleteTarget({ item: ctxMenu.item, busy: false }),
|
|
9386
|
+
// v0.16.0:列表级粘贴(右键任意卡片也出现)
|
|
9387
|
+
onPaste: () => { void doPaste() },
|
|
7096
9388
|
// v0.14.2:改标签 / 恢复自动,表态层已下线
|
|
7097
9389
|
onEditLabel: () => {
|
|
7098
9390
|
const target = ctxMenu.item
|
|
@@ -7119,6 +9411,20 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
7119
9411
|
})
|
|
7120
9412
|
: null
|
|
7121
9413
|
|
|
9414
|
+
// v0.16.0:右键空白处(非卡片)的「粘贴」菜单;只给粘贴一项
|
|
9415
|
+
const pasteLayer = pasteMenu !== null
|
|
9416
|
+
? h(ContextMenu, {
|
|
9417
|
+
key: 'pasteMenu',
|
|
9418
|
+
t,
|
|
9419
|
+
x: pasteMenu.x,
|
|
9420
|
+
y: pasteMenu.y,
|
|
9421
|
+
// 菜单标题直接写清落点,避免「粘到哪去了」说不清
|
|
9422
|
+
item: { relPath: '', name: format(t('pasteTo'), { target: pasteMenu.targetLabel ?? t('pasteTargetCurrent') }) },
|
|
9423
|
+
onClose: () => setPasteMenu(null),
|
|
9424
|
+
actions: { onPaste: () => { void doPaste() } },
|
|
9425
|
+
})
|
|
9426
|
+
: null
|
|
9427
|
+
|
|
7122
9428
|
// 删除确认弹窗
|
|
7123
9429
|
const confirmLayer = deleteTarget !== null
|
|
7124
9430
|
? h(ConfirmDialog, {
|
|
@@ -7144,9 +9450,19 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
7144
9450
|
mediaLayer,
|
|
7145
9451
|
settingsLayer,
|
|
7146
9452
|
ctxLayer,
|
|
9453
|
+
pasteLayer,
|
|
7147
9454
|
confirmLayer,
|
|
7148
9455
|
feedbackLayer,
|
|
7149
9456
|
labelLayer,
|
|
9457
|
+
// v0.16.0 粘贴结果提示(贴在根级,跟随画廊不随网格滚动)
|
|
9458
|
+
pasteNotice === null
|
|
9459
|
+
? null
|
|
9460
|
+
: h('div', {
|
|
9461
|
+
className: 'dg-pasteNotice'
|
|
9462
|
+
+ (pasteNotice.kind === 'ok' ? ' dg-pasteOk' : ' dg-pasteFail'),
|
|
9463
|
+
key: 'pasteNotice',
|
|
9464
|
+
'data-gallery-paste-notice': pasteNotice.kind,
|
|
9465
|
+
}, pasteNotice.text),
|
|
7150
9466
|
])
|
|
7151
9467
|
}
|
|
7152
9468
|
|
|
@@ -7363,17 +9679,41 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
7363
9679
|
])
|
|
7364
9680
|
}
|
|
7365
9681
|
|
|
7366
|
-
/**
|
|
9682
|
+
/**
|
|
9683
|
+
* 画廊图标(相框 + 山 + 闪光)—— tab 标题、侧栏脚部入口、输入框入口三处复用,
|
|
9684
|
+
* 保证同一份图形不漂移。内联 SVG、跟随 currentColor、尺寸由外层 className 决定;
|
|
9685
|
+
* 不依赖包内 icon.svg(其首行是非法的 <!DOCTYPE html>,且从未被引用过)。
|
|
9686
|
+
*/
|
|
9687
|
+
function GalleryGlyph({ className }) {
|
|
9688
|
+
return h('svg', {
|
|
9689
|
+
className: className || 'dg-tabTitleIcon',
|
|
9690
|
+
viewBox: '0 0 64 64',
|
|
9691
|
+
width: 16, height: 16,
|
|
9692
|
+
'aria-hidden': 'true', focusable: 'false',
|
|
9693
|
+
}, [
|
|
9694
|
+
// 叠层工作台:后层只露右上边角。三层完整轮廓在 16px 下会互相交叠糊成一块,
|
|
9695
|
+
// 故减为「一张主卡片 + 露角」,线宽加粗到 4(缩到 16px 约 1px 实线)。
|
|
9696
|
+
h('path', { key: 'back', d: 'M22 8h28a4 4 0 0 1 4 4v24', fill: 'none', stroke: 'currentColor', strokeWidth: 4, strokeLinecap: 'round', opacity: 0.55 }),
|
|
9697
|
+
h('rect', { key: 'card', x: 6, y: 16, width: 42, height: 38, rx: 6, fill: 'none', stroke: 'currentColor', strokeWidth: 4 }),
|
|
9698
|
+
h('path', { key: 'check', d: 'M15 35l6 6 12-13', fill: 'none', stroke: 'currentColor', strokeWidth: 4.5, strokeLinecap: 'round', strokeLinejoin: 'round' }),
|
|
9699
|
+
])
|
|
9700
|
+
}
|
|
7367
9701
|
|
|
9702
|
+
/** tab 标题(chip / 浮动面板标题):图标 + 名称。 */
|
|
7368
9703
|
function GalleryTitle({ t }) {
|
|
7369
|
-
|
|
9704
|
+
// 包成单个 inline-flex 容器:宿主 chip 会把 slot 的每个子节点当 flex item 并额外插一份 gap,
|
|
9705
|
+
// 若用 Fragment 返回 [图标, 文字],间距会与图标自带的 margin-right 叠加而过大,还挤压名称。
|
|
9706
|
+
return h('span', { className: 'dg-titleWrap' },
|
|
9707
|
+
h(GalleryGlyph),
|
|
9708
|
+
h('span', { className: 'dg-title' }, t('type.label')))
|
|
7370
9709
|
}
|
|
7371
9710
|
|
|
7372
|
-
/** 侧栏脚部入口按钮:点击打开画廊 tab
|
|
9711
|
+
/** 侧栏脚部入口按钮:点击打开画廊 tab。展开态「图标 + 名称」,收窄态仅图标。 */
|
|
7373
9712
|
function OpenButton({ t, wide }) {
|
|
9713
|
+
const glyph = h(GalleryGlyph, { className: 'dg-sideIcon' })
|
|
7374
9714
|
return h('button', {
|
|
7375
9715
|
type: 'button',
|
|
7376
|
-
className:
|
|
9716
|
+
className: `dg-btn dg-open ${wide ? 'dg-openWide' : 'dg-openNarrow'}`,
|
|
7377
9717
|
'data-gallery-open': true,
|
|
7378
9718
|
title: t('type.label'),
|
|
7379
9719
|
'aria-label': t('type.label'),
|
|
@@ -7384,7 +9724,9 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
7384
9724
|
console.error('dsh-gallery: open failed', error)
|
|
7385
9725
|
}
|
|
7386
9726
|
},
|
|
7387
|
-
}, wide
|
|
9727
|
+
}, wide
|
|
9728
|
+
? h(React.Fragment, null, glyph, h('span', { className: 'dg-openLabel' }, t('type.label')))
|
|
9729
|
+
: glyph)
|
|
7388
9730
|
}
|
|
7389
9731
|
|
|
7390
9732
|
|
|
@@ -7486,7 +9828,7 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
7486
9828
|
console.error('dsh-gallery: open failed', error)
|
|
7487
9829
|
}
|
|
7488
9830
|
},
|
|
7489
|
-
}, '
|
|
9831
|
+
}, h(GalleryGlyph, { className: 'dg-sideIcon' }))
|
|
7490
9832
|
}
|
|
7491
9833
|
|
|
7492
9834
|
return {
|
|
@@ -7510,6 +9852,53 @@ function AnnotationLayer({ t, annotations, onAdd, onRemove, active, dragging, fr
|
|
|
7510
9852
|
}
|
|
7511
9853
|
}, 'dsh-gallery: seam')
|
|
7512
9854
|
|
|
9855
|
+
// 拖图带位置信息:捕获阶段拦截画廊卡片的 dragstart,把
|
|
9856
|
+
// 「文件名 + 绝对路径 + 主遮罩」写进 File.name(DSH 会原样带进消息)。
|
|
9857
|
+
// 不改图片节点、不改 draggable、不碰 Viewer 的缩放/批注交互、不自动发送。
|
|
9858
|
+
ctx.effect(() => {
|
|
9859
|
+
const onDragStart = (event) => {
|
|
9860
|
+
const dt = event.dataTransfer
|
|
9861
|
+
const target = event.target
|
|
9862
|
+
if (dt === null || dt === undefined || !(target instanceof Element)) return
|
|
9863
|
+
const card = target.closest('[data-gallery-relpath]')
|
|
9864
|
+
if (card === null) return
|
|
9865
|
+
const relPath = String(card.getAttribute('data-gallery-relpath') ?? '')
|
|
9866
|
+
const entry = dragAssetIndex.get(relPath)
|
|
9867
|
+
if (entry === undefined) return
|
|
9868
|
+
// 预览图 / 原图没有 maskOf,按 overlayRelPath / sourceRelPath 反查主遮罩,
|
|
9869
|
+
// 把「主遮罩=<文件名> | 工作流请用主遮罩,预览仅供目视」写进拖拽文本。
|
|
9870
|
+
const linked = dragMaskByOverlay.get(relPath) ?? dragMaskBySource.get(relPath) ?? null
|
|
9871
|
+
const hasMask = entry.item?.maskOf !== null && entry.item?.maskOf !== undefined
|
|
9872
|
+
const item = (!hasMask && linked !== null)
|
|
9873
|
+
? { ...entry.item, maskOf: { ...linked, linkedMask: true } }
|
|
9874
|
+
: entry.item
|
|
9875
|
+
const name = dragNameOf(item, entry.absPath)
|
|
9876
|
+
// 自证 + 兜底双用途:无论 File 替换是否成功,都把同一串信息写进剪贴板。
|
|
9877
|
+
// 拖完在输入框 Ctrl+V 能粘出「产出台·…」= 新代码确实执行过、dragstart 命中;
|
|
9878
|
+
// 粘不出 = 面板仍加载旧模块,需重载窗口。编码 / 写入失败时它就是唯一可用通道。
|
|
9879
|
+
void copyText(name)
|
|
9880
|
+
// 从 Viewer「拖到对话」按钮拖出时,取预览大图而不是按钮内的节点;
|
|
9881
|
+
// 列表卡片则仍用自己那张缩略图。两者都是同源 blob URL,canvas 不会被污染。
|
|
9882
|
+
const img = card.hasAttribute('data-gallery-dragviewer')
|
|
9883
|
+
? document.querySelector('.dg-viewerBody img')
|
|
9884
|
+
: (target.matches('img') ? target : target.querySelector('img'))
|
|
9885
|
+
const bytes = syncImagePngBytes(img)
|
|
9886
|
+
if (bytes === null) {
|
|
9887
|
+
// 编码失败:保留浏览器原生拖图,信息已在剪贴板供手动粘贴
|
|
9888
|
+
return
|
|
9889
|
+
}
|
|
9890
|
+
try {
|
|
9891
|
+
dt.clearData()
|
|
9892
|
+
dt.effectAllowed = 'copy'
|
|
9893
|
+
dt.items.add(new File([bytes], name, { type: 'image/png' }))
|
|
9894
|
+
} catch (error) {
|
|
9895
|
+
console.warn('dsh-gallery: 拖图元信息写入失败,信息已在剪贴板', error)
|
|
9896
|
+
}
|
|
9897
|
+
}
|
|
9898
|
+
document.addEventListener('dragstart', onDragStart, true)
|
|
9899
|
+
return () => document.removeEventListener('dragstart', onDragStart, true)
|
|
9900
|
+
}, 'dsh-gallery: drag asset metadata')
|
|
9901
|
+
|
|
7513
9902
|
ctx.effect(() => {
|
|
7514
9903
|
const tag = document.createElement('style')
|
|
7515
9904
|
tag.dataset.plugin = 'dsh-gallery'
|