@qzsy/dsh-image-viewer 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -1
- package/dist/client.js +1154 -6
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -33,6 +33,17 @@ dsh plugin add @qzsy/dsh-image-viewer
|
|
|
33
33
|
|
|
34
34
|
系统开启“减少动态效果”时,所有动画直接跳到终态。
|
|
35
35
|
|
|
36
|
+
## 标注后发送
|
|
37
|
+
|
|
38
|
+
查看器顶部的铅笔按钮打开标注编辑器(类似 ChatGPT / iOS 标记):
|
|
39
|
+
|
|
40
|
+
- 工具:画笔、形状(矩形 / 椭圆 / 箭头 / 直线,按住 Shift 画正方形或 45° 线)、橡皮擦(按笔画整笔擦除)
|
|
41
|
+
- 左侧竖条调粗细,底部选颜色,最左侧色环可自定义颜色
|
|
42
|
+
- 撤销 / 重做(`Ctrl/⌘+Z`、`Ctrl/⌘+Shift+Z`、`Ctrl+Y`),`Enter` 完成,`Esc` 返回查看器
|
|
43
|
+
- 双指缩放 / 平移画布;桌面上 Ctrl+滚轮缩放,中键拖动平移;用 Apple Pencil 时手指只用于缩放平移
|
|
44
|
+
- 点 ✓ 后导出 PNG(超过 8 MB 改为 JPEG),以粘贴的方式放进对话输入框,查看器关闭,光标留在输入框,补充提示词后即可发送
|
|
45
|
+
- 找不到输入框时改为下载图片;来自其他站点且不允许跨域的图片,浏览器会禁止导出,会给出提示
|
|
46
|
+
|
|
36
47
|
## 工作方式
|
|
37
48
|
|
|
38
49
|
插件只有客户端部分,不注册任何 Host 能力,不发起网络请求。它在 `document` 捕获阶段监听点击:
|
|
@@ -49,7 +60,8 @@ dsh plugin add @qzsy/dsh-image-viewer
|
|
|
49
60
|
~~~bash
|
|
50
61
|
bun run build # dist/index.js + dist/client.js
|
|
51
62
|
bun run typecheck
|
|
52
|
-
node test/smoke.mjs
|
|
63
|
+
node test/smoke.mjs # jsdom 冒烟测试:查看器
|
|
64
|
+
node test/editor.mjs # jsdom 冒烟测试:标注与注入输入框
|
|
53
65
|
~~~
|
|
54
66
|
|
|
55
67
|
## 许可
|
package/dist/client.js
CHANGED
|
@@ -64,6 +64,36 @@ window.__ModuleLoader__.load({
|
|
|
64
64
|
counter: '{i} / {n}',
|
|
65
65
|
failed: '图片加载失败',
|
|
66
66
|
thumb: '第 {i} 张',
|
|
67
|
+
edit: '编辑标注',
|
|
68
|
+
'edit.dialog': '标注图片',
|
|
69
|
+
'edit.tools': '标注工具',
|
|
70
|
+
'edit.pen': '画笔',
|
|
71
|
+
'edit.shapes': '形状',
|
|
72
|
+
'edit.eraser': '橡皮擦',
|
|
73
|
+
'edit.undo': '撤销',
|
|
74
|
+
'edit.redo': '重做',
|
|
75
|
+
'edit.cancel': '返回',
|
|
76
|
+
'edit.done': '完成并添加到输入框',
|
|
77
|
+
'edit.size': '笔触粗细',
|
|
78
|
+
'edit.color': '颜色',
|
|
79
|
+
'edit.custom': '自定义颜色',
|
|
80
|
+
'edit.color.white': '白色',
|
|
81
|
+
'edit.color.black': '黑色',
|
|
82
|
+
'edit.color.red': '红色',
|
|
83
|
+
'edit.color.orange': '橙色',
|
|
84
|
+
'edit.color.yellow': '黄色',
|
|
85
|
+
'edit.color.green': '绿色',
|
|
86
|
+
'edit.color.blue': '蓝色',
|
|
87
|
+
'edit.color.purple': '紫色',
|
|
88
|
+
'edit.shape.rect': '矩形',
|
|
89
|
+
'edit.shape.ellipse': '椭圆',
|
|
90
|
+
'edit.shape.arrow': '箭头',
|
|
91
|
+
'edit.shape.line': '直线',
|
|
92
|
+
'edit.injected': '已添加到输入框,补充说明后发送',
|
|
93
|
+
'edit.noComposer': '没有找到输入框,已下载标注后的图片',
|
|
94
|
+
'edit.tainted': '这张图片来自其他站点,浏览器不允许导出',
|
|
95
|
+
'edit.taintedHint': '图片来自其他站点,可能无法导出',
|
|
96
|
+
'edit.exportFailed': '导出图片失败',
|
|
67
97
|
};
|
|
68
98
|
const en = {
|
|
69
99
|
dialog: 'Image viewer',
|
|
@@ -76,6 +106,36 @@ window.__ModuleLoader__.load({
|
|
|
76
106
|
counter: '{i} / {n}',
|
|
77
107
|
failed: 'Image failed to load',
|
|
78
108
|
thumb: 'Image {i}',
|
|
109
|
+
edit: 'Edit and annotate',
|
|
110
|
+
'edit.dialog': 'Annotate image',
|
|
111
|
+
'edit.tools': 'Annotation tools',
|
|
112
|
+
'edit.pen': 'Pen',
|
|
113
|
+
'edit.shapes': 'Shapes',
|
|
114
|
+
'edit.eraser': 'Eraser',
|
|
115
|
+
'edit.undo': 'Undo',
|
|
116
|
+
'edit.redo': 'Redo',
|
|
117
|
+
'edit.cancel': 'Back',
|
|
118
|
+
'edit.done': 'Done, add to message',
|
|
119
|
+
'edit.size': 'Stroke width',
|
|
120
|
+
'edit.color': 'Colour',
|
|
121
|
+
'edit.custom': 'Custom colour',
|
|
122
|
+
'edit.color.white': 'White',
|
|
123
|
+
'edit.color.black': 'Black',
|
|
124
|
+
'edit.color.red': 'Red',
|
|
125
|
+
'edit.color.orange': 'Orange',
|
|
126
|
+
'edit.color.yellow': 'Yellow',
|
|
127
|
+
'edit.color.green': 'Green',
|
|
128
|
+
'edit.color.blue': 'Blue',
|
|
129
|
+
'edit.color.purple': 'Purple',
|
|
130
|
+
'edit.shape.rect': 'Rectangle',
|
|
131
|
+
'edit.shape.ellipse': 'Ellipse',
|
|
132
|
+
'edit.shape.arrow': 'Arrow',
|
|
133
|
+
'edit.shape.line': 'Line',
|
|
134
|
+
'edit.injected': 'Added to the message box. Add a prompt and send.',
|
|
135
|
+
'edit.noComposer': 'No message box found; the annotated image was downloaded.',
|
|
136
|
+
'edit.tainted': 'This image comes from another site and the browser blocks exporting it.',
|
|
137
|
+
'edit.taintedHint': 'Image from another site; export may be blocked.',
|
|
138
|
+
'edit.exportFailed': 'Could not export the image.',
|
|
79
139
|
};
|
|
80
140
|
|
|
81
141
|
//#region utilities
|
|
@@ -273,6 +333,18 @@ window.__ModuleLoader__.load({
|
|
|
273
333
|
zoomIn: svg('<circle cx="11" cy="11" r="6.5"/><path d="M20 20l-4.2-4.2M8.5 11h5M11 8.5v5"/>'),
|
|
274
334
|
zoomOut: svg('<circle cx="11" cy="11" r="6.5"/><path d="M20 20l-4.2-4.2M8.5 11h5"/>'),
|
|
275
335
|
download: svg('<path d="M12 4v11M7 10.5l5 5 5-5M5 20h14"/>'),
|
|
336
|
+
edit: svg('<path d="M4 20h4L19 9l-4-4L4 16v4z"/><path d="M13.5 6.5l4 4"/>'),
|
|
337
|
+
back: svg('<path d="M15 5l-7 7 7 7"/>'),
|
|
338
|
+
pen: svg('<path d="M3 17c3-1 4-6 7-6s1 6 4 6 4-8 7-9"/>'),
|
|
339
|
+
shapes: svg('<path d="M12 3l4 6H8z"/><rect x="3.5" y="13" width="7" height="7" rx="1.5"/><circle cx="17" cy="16.5" r="3.5"/>'),
|
|
340
|
+
eraser: svg('<path d="M14.5 4.5l5 5-9 9H6l-2.5-2.5a1.5 1.5 0 010-2.1z"/><path d="M9 10l5 5M10.5 18.5H20"/>'),
|
|
341
|
+
undo: svg('<path d="M9 7H5V3"/><path d="M5.5 7a8 8 0 11-1.3 7"/>'),
|
|
342
|
+
redo: svg('<path d="M15 7h4V3"/><path d="M18.5 7a8 8 0 101.3 7"/>'),
|
|
343
|
+
check: svg('<path d="M5 12.5l4.5 4.5L19 7.5"/>'),
|
|
344
|
+
rect: svg('<rect x="4" y="6" width="16" height="12" rx="1.5"/>'),
|
|
345
|
+
ellipse: svg('<ellipse cx="12" cy="12" rx="8.5" ry="6"/>'),
|
|
346
|
+
arrow: svg('<path d="M5 19L19 5M10 5h9v9"/>'),
|
|
347
|
+
line: svg('<path d="M5 19L19 5"/>'),
|
|
276
348
|
};
|
|
277
349
|
//#endregion
|
|
278
350
|
|
|
@@ -322,7 +394,7 @@ window.__ModuleLoader__.load({
|
|
|
322
394
|
if (document.querySelector(`style[${STYLE_ATTR}]`) !== null) return () => {};
|
|
323
395
|
const tag = document.createElement('style');
|
|
324
396
|
tag.setAttribute(STYLE_ATTR, '');
|
|
325
|
-
tag.textContent = CSS;
|
|
397
|
+
tag.textContent = CSS + EDIT_CSS;
|
|
326
398
|
document.head.appendChild(tag);
|
|
327
399
|
return () => tag.remove();
|
|
328
400
|
}
|
|
@@ -348,7 +420,7 @@ window.__ModuleLoader__.load({
|
|
|
348
420
|
*/
|
|
349
421
|
function resolveTrigger(node) {
|
|
350
422
|
if (!(node instanceof Element)) return null;
|
|
351
|
-
if (node.closest(`[${ROOT_ATTR}],[${IGNORE_ATTR}],[aria-modal="true"],[contenteditable="true"]`) !== null) return null;
|
|
423
|
+
if (node.closest(`[${ROOT_ATTR}],[${EDIT_ATTR}],[${IGNORE_ATTR}],[aria-modal="true"],[contenteditable="true"]`) !== null) return null;
|
|
352
424
|
const host = node.closest('button,[role="button"]');
|
|
353
425
|
if (host !== null) {
|
|
354
426
|
if (host.closest('a[href]') !== null) return null;
|
|
@@ -442,6 +514,8 @@ window.__ModuleLoader__.load({
|
|
|
442
514
|
let outgoing = null;
|
|
443
515
|
let closing = false;
|
|
444
516
|
let destroyed = false;
|
|
517
|
+
/** Open annotation editor; the viewer pauses keyboard input under it. */
|
|
518
|
+
let editor = null;
|
|
445
519
|
let gesture = null;
|
|
446
520
|
let samples = [];
|
|
447
521
|
let tapTimer = 0;
|
|
@@ -472,7 +546,9 @@ window.__ModuleLoader__.load({
|
|
|
472
546
|
const download = h('a', 'qiv-btn', { 'aria-label': t('download'), title: t('download'), rel: 'noopener', target: '_blank' });
|
|
473
547
|
download.innerHTML = ICON.download;
|
|
474
548
|
const closeBtn = iconButton('qiv-btn', t('close'), ICON.close);
|
|
475
|
-
|
|
549
|
+
const editBtn = canEdit() ? iconButton('qiv-btn', t('edit'), ICON.edit) : null;
|
|
550
|
+
if (editBtn !== null) actions.append(zoomBtn, editBtn, download, closeBtn);
|
|
551
|
+
else actions.append(zoomBtn, download, closeBtn);
|
|
476
552
|
top.append(counter, actions);
|
|
477
553
|
const prevBtn = iconButton('qiv-btn qiv-nav qiv-prev qiv-chrome', t('prev'), ICON.prev);
|
|
478
554
|
const nextBtn = iconButton('qiv-btn qiv-nav qiv-next qiv-chrome', t('next'), ICON.next);
|
|
@@ -1180,7 +1256,7 @@ window.__ModuleLoader__.load({
|
|
|
1180
1256
|
}
|
|
1181
1257
|
|
|
1182
1258
|
function onKey(e) {
|
|
1183
|
-
if (closing || destroyed) return;
|
|
1259
|
+
if (closing || destroyed || editor !== null) return;
|
|
1184
1260
|
if (e.ctrlKey || e.metaKey || e.altKey) return;
|
|
1185
1261
|
let handled = true;
|
|
1186
1262
|
switch (e.key) {
|
|
@@ -1259,16 +1335,20 @@ window.__ModuleLoader__.load({
|
|
|
1259
1335
|
}
|
|
1260
1336
|
}
|
|
1261
1337
|
|
|
1262
|
-
function finish() {
|
|
1338
|
+
function finish(restore = true) {
|
|
1263
1339
|
if (destroyed) return;
|
|
1264
1340
|
destroyed = true;
|
|
1341
|
+
if (editor !== null) {
|
|
1342
|
+
editor.destroy();
|
|
1343
|
+
editor = null;
|
|
1344
|
+
}
|
|
1265
1345
|
anim.destroy();
|
|
1266
1346
|
clearTap();
|
|
1267
1347
|
showSource();
|
|
1268
1348
|
window.removeEventListener('keydown', onKey, true);
|
|
1269
1349
|
window.removeEventListener('resize', onResize);
|
|
1270
1350
|
root.remove();
|
|
1271
|
-
if (restoreFocus !== null && restoreFocus.isConnected) {
|
|
1351
|
+
if (restore && restoreFocus !== null && restoreFocus.isConnected) {
|
|
1272
1352
|
try {
|
|
1273
1353
|
restoreFocus.focus({ preventScroll: true });
|
|
1274
1354
|
} catch {
|
|
@@ -1292,6 +1372,26 @@ window.__ModuleLoader__.load({
|
|
|
1292
1372
|
if (s.scale > 1.01) anim.to({ scale: 1, x: 0, y: 0 }, { spring: SPRING.snap });
|
|
1293
1373
|
else zoomBy(doubleTapScale(), vw / 2, vh / 2, true);
|
|
1294
1374
|
});
|
|
1375
|
+
function openEdit() {
|
|
1376
|
+
if (closing || destroyed || editor !== null) return;
|
|
1377
|
+
clearTap();
|
|
1378
|
+
editor = openEditor(items[index], t, {
|
|
1379
|
+
cancel: () => {
|
|
1380
|
+
editor = null;
|
|
1381
|
+
try {
|
|
1382
|
+
editBtn.focus({ preventScroll: true });
|
|
1383
|
+
} catch {
|
|
1384
|
+
// focus is best-effort
|
|
1385
|
+
}
|
|
1386
|
+
},
|
|
1387
|
+
done: (injected) => {
|
|
1388
|
+
editor = null;
|
|
1389
|
+
// Injected: focus stays in the composer. Downloaded: return to the thumbnail.
|
|
1390
|
+
finish(!injected);
|
|
1391
|
+
},
|
|
1392
|
+
});
|
|
1393
|
+
}
|
|
1394
|
+
if (editBtn !== null) editBtn.addEventListener('click', openEdit);
|
|
1295
1395
|
closeBtn.addEventListener('click', () => close());
|
|
1296
1396
|
prevBtn.addEventListener('click', () => go(-1));
|
|
1297
1397
|
nextBtn.addEventListener('click', () => go(1));
|
|
@@ -1332,6 +1432,1054 @@ window.__ModuleLoader__.load({
|
|
|
1332
1432
|
}
|
|
1333
1433
|
//#endregion
|
|
1334
1434
|
|
|
1435
|
+
//#region composer injection
|
|
1436
|
+
/** Canvas export is required for the annotation editor. */
|
|
1437
|
+
function canEdit() {
|
|
1438
|
+
try {
|
|
1439
|
+
const canvas = document.createElement('canvas');
|
|
1440
|
+
return typeof canvas.getContext === 'function' && typeof canvas.toBlob === 'function' && canvas.getContext('2d') !== null;
|
|
1441
|
+
} catch {
|
|
1442
|
+
return false;
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1446
|
+
function visible(el) {
|
|
1447
|
+
const rect = el.getBoundingClientRect();
|
|
1448
|
+
return rect.width > 0 && rect.height > 0;
|
|
1449
|
+
}
|
|
1450
|
+
|
|
1451
|
+
/** The chat composer is a Lexical root marked `data-composer-input`. */
|
|
1452
|
+
function findComposer() {
|
|
1453
|
+
const all = [...document.querySelectorAll('[data-composer-input]')].filter(
|
|
1454
|
+
(el) => el.isConnected && el.closest(`[${ROOT_ATTR}],[${EDIT_ATTR}]`) === null,
|
|
1455
|
+
);
|
|
1456
|
+
const editable = all.filter((el) => el.getAttribute('contenteditable') === 'true');
|
|
1457
|
+
const pool = editable.length > 0 ? editable : all;
|
|
1458
|
+
return pool.find(visible) || pool[0] || null;
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
/** A real DataTransfer when the browser allows it, else a structural stand-in. */
|
|
1462
|
+
function transferOf(file) {
|
|
1463
|
+
try {
|
|
1464
|
+
const dt = new DataTransfer();
|
|
1465
|
+
dt.items.add(file);
|
|
1466
|
+
if (dt.files.length === 1) return dt;
|
|
1467
|
+
} catch {
|
|
1468
|
+
// fall through to the stand-in
|
|
1469
|
+
}
|
|
1470
|
+
const item = { kind: 'file', type: file.type, getAsFile: () => file, webkitGetAsEntry: () => null };
|
|
1471
|
+
return {
|
|
1472
|
+
files: [file],
|
|
1473
|
+
items: [item],
|
|
1474
|
+
types: ['Files'],
|
|
1475
|
+
dropEffect: 'copy',
|
|
1476
|
+
effectAllowed: 'all',
|
|
1477
|
+
getData: () => '',
|
|
1478
|
+
setData() {},
|
|
1479
|
+
clearData() {},
|
|
1480
|
+
};
|
|
1481
|
+
}
|
|
1482
|
+
|
|
1483
|
+
function synthetic(type, field, transfer, Ctor) {
|
|
1484
|
+
let ev = null;
|
|
1485
|
+
try {
|
|
1486
|
+
if (typeof Ctor === 'function') ev = new Ctor(type, { bubbles: true, cancelable: true, [field]: transfer });
|
|
1487
|
+
} catch {
|
|
1488
|
+
ev = null;
|
|
1489
|
+
}
|
|
1490
|
+
if (ev === null) ev = new Event(type, { bubbles: true, cancelable: true });
|
|
1491
|
+
if (ev[field] !== transfer) {
|
|
1492
|
+
try {
|
|
1493
|
+
Object.defineProperty(ev, field, { value: transfer, configurable: true });
|
|
1494
|
+
} catch {
|
|
1495
|
+
// keep whatever the constructor produced
|
|
1496
|
+
}
|
|
1497
|
+
}
|
|
1498
|
+
return ev;
|
|
1499
|
+
}
|
|
1500
|
+
|
|
1501
|
+
/**
|
|
1502
|
+
* Hand one image to the composer the way a paste would. The composer's own
|
|
1503
|
+
* paste command calls preventDefault after it accepts files; the attachment
|
|
1504
|
+
* view's document drop listener is the fallback.
|
|
1505
|
+
* @returns 'composer' | 'none'
|
|
1506
|
+
*/
|
|
1507
|
+
function injectFile(file) {
|
|
1508
|
+
const composer = findComposer();
|
|
1509
|
+
if (composer !== null && composer.getAttribute('contenteditable') === 'true') {
|
|
1510
|
+
try {
|
|
1511
|
+
composer.focus({ preventScroll: false });
|
|
1512
|
+
} catch {
|
|
1513
|
+
// focus is best-effort
|
|
1514
|
+
}
|
|
1515
|
+
const paste = synthetic('paste', 'clipboardData', transferOf(file), window.ClipboardEvent);
|
|
1516
|
+
composer.dispatchEvent(paste);
|
|
1517
|
+
if (paste.defaultPrevented) return 'composer';
|
|
1518
|
+
}
|
|
1519
|
+
if (composer !== null) {
|
|
1520
|
+
const drop = synthetic('drop', 'dataTransfer', transferOf(file), window.DragEvent);
|
|
1521
|
+
document.body.dispatchEvent(drop);
|
|
1522
|
+
if (drop.defaultPrevented) {
|
|
1523
|
+
try {
|
|
1524
|
+
composer.focus({ preventScroll: false });
|
|
1525
|
+
} catch {
|
|
1526
|
+
// focus is best-effort
|
|
1527
|
+
}
|
|
1528
|
+
return 'composer';
|
|
1529
|
+
}
|
|
1530
|
+
}
|
|
1531
|
+
return 'none';
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
function toast(text) {
|
|
1535
|
+
const node = h('div', 'qiv-toast', { role: 'status', [IGNORE_ATTR]: '' });
|
|
1536
|
+
node.textContent = text;
|
|
1537
|
+
document.body.appendChild(node);
|
|
1538
|
+
requestAnimationFrame(() => node.setAttribute('data-show', ''));
|
|
1539
|
+
setTimeout(() => {
|
|
1540
|
+
node.removeAttribute('data-show');
|
|
1541
|
+
setTimeout(() => node.remove(), 300);
|
|
1542
|
+
}, 2600);
|
|
1543
|
+
}
|
|
1544
|
+
|
|
1545
|
+
function downloadFile(file) {
|
|
1546
|
+
const url = URL.createObjectURL(file);
|
|
1547
|
+
const a = h('a', '', { href: url, download: file.name, [IGNORE_ATTR]: '' });
|
|
1548
|
+
a.style.display = 'none';
|
|
1549
|
+
document.body.appendChild(a);
|
|
1550
|
+
a.click();
|
|
1551
|
+
a.remove();
|
|
1552
|
+
setTimeout(() => URL.revokeObjectURL(url), 30000);
|
|
1553
|
+
}
|
|
1554
|
+
//#endregion
|
|
1555
|
+
|
|
1556
|
+
//#region annotation editor
|
|
1557
|
+
const EDIT_ATTR = 'data-qiv-edit';
|
|
1558
|
+
const COLORS = [
|
|
1559
|
+
['#ffffff', 'white'],
|
|
1560
|
+
['#000000', 'black'],
|
|
1561
|
+
['#ff3b30', 'red'],
|
|
1562
|
+
['#ff9500', 'orange'],
|
|
1563
|
+
['#ffcc00', 'yellow'],
|
|
1564
|
+
['#34c759', 'green'],
|
|
1565
|
+
['#007aff', 'blue'],
|
|
1566
|
+
['#af52de', 'purple'],
|
|
1567
|
+
];
|
|
1568
|
+
const SIZE_MIN = 2;
|
|
1569
|
+
const SIZE_MAX = 36;
|
|
1570
|
+
/** Working canvas limits; keeps mobile Safari under its canvas memory cap. */
|
|
1571
|
+
const EDIT_MAX_EDGE = 4096;
|
|
1572
|
+
const EDIT_MAX_PIXELS = 12e6;
|
|
1573
|
+
const EXPORT_MAX_BYTES = 8 * 1024 * 1024;
|
|
1574
|
+
|
|
1575
|
+
function needsCors(src) {
|
|
1576
|
+
try {
|
|
1577
|
+
const url = new URL(src, location.href);
|
|
1578
|
+
if (url.protocol === 'data:' || url.protocol === 'blob:') return false;
|
|
1579
|
+
return url.protocol !== location.protocol || url.host !== location.host;
|
|
1580
|
+
} catch {
|
|
1581
|
+
return false;
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
|
|
1585
|
+
function loadImage(src, cors) {
|
|
1586
|
+
return new Promise((resolve, reject) => {
|
|
1587
|
+
const img = new Image();
|
|
1588
|
+
if (cors) img.crossOrigin = 'anonymous';
|
|
1589
|
+
img.decoding = 'async';
|
|
1590
|
+
img.onload = () => resolve(img);
|
|
1591
|
+
img.onerror = () => reject(new Error('load failed'));
|
|
1592
|
+
img.src = src;
|
|
1593
|
+
});
|
|
1594
|
+
}
|
|
1595
|
+
|
|
1596
|
+
/** Prefer a CORS load so export works; fall back to a tainted copy so drawing still does. */
|
|
1597
|
+
async function loadEditable(src) {
|
|
1598
|
+
if (!needsCors(src)) return { img: await loadImage(src, false), tainted: false };
|
|
1599
|
+
try {
|
|
1600
|
+
return { img: await loadImage(src, true), tainted: false };
|
|
1601
|
+
} catch {
|
|
1602
|
+
return { img: await loadImage(src, false), tainted: true };
|
|
1603
|
+
}
|
|
1604
|
+
}
|
|
1605
|
+
|
|
1606
|
+
const dist = (a, b) => Math.hypot(a.x - b.x, a.y - b.y);
|
|
1607
|
+
function segDist(p, a, b) {
|
|
1608
|
+
const dx = b.x - a.x;
|
|
1609
|
+
const dy = b.y - a.y;
|
|
1610
|
+
const len = dx * dx + dy * dy;
|
|
1611
|
+
const k = len === 0 ? 0 : clamp(((p.x - a.x) * dx + (p.y - a.y) * dy) / len, 0, 1);
|
|
1612
|
+
return Math.hypot(p.x - (a.x + k * dx), p.y - (a.y + k * dy));
|
|
1613
|
+
}
|
|
1614
|
+
|
|
1615
|
+
function arrowHead(a, b, w) {
|
|
1616
|
+
const len = dist(a, b);
|
|
1617
|
+
const size = Math.min(Math.max(w * 3.2, 14), len * 0.45);
|
|
1618
|
+
const ang = Math.atan2(b.y - a.y, b.x - a.x);
|
|
1619
|
+
const spread = Math.PI / 7;
|
|
1620
|
+
return [
|
|
1621
|
+
{ x: b.x - size * Math.cos(ang - spread), y: b.y - size * Math.sin(ang - spread) },
|
|
1622
|
+
{ x: b.x - size * Math.cos(ang + spread), y: b.y - size * Math.sin(ang + spread) },
|
|
1623
|
+
];
|
|
1624
|
+
}
|
|
1625
|
+
|
|
1626
|
+
/** Polylines used for hit-testing every stroke kind with one segment test. */
|
|
1627
|
+
function outlines(st) {
|
|
1628
|
+
if (st.kind === 'pen') return [st.points];
|
|
1629
|
+
const [a, b] = [st.a, st.b];
|
|
1630
|
+
if (st.kind === 'line') return [[a, b]];
|
|
1631
|
+
if (st.kind === 'arrow') {
|
|
1632
|
+
const [l, r] = arrowHead(a, b, st.w);
|
|
1633
|
+
return [[a, b], [l, b, r]];
|
|
1634
|
+
}
|
|
1635
|
+
if (st.kind === 'rect') {
|
|
1636
|
+
return [[a, { x: b.x, y: a.y }, b, { x: a.x, y: b.y }, a]];
|
|
1637
|
+
}
|
|
1638
|
+
const cx = (a.x + b.x) / 2;
|
|
1639
|
+
const cy = (a.y + b.y) / 2;
|
|
1640
|
+
const rx = Math.abs(b.x - a.x) / 2;
|
|
1641
|
+
const ry = Math.abs(b.y - a.y) / 2;
|
|
1642
|
+
const pts = [];
|
|
1643
|
+
for (let i = 0; i <= 48; i++) {
|
|
1644
|
+
const t = (i / 48) * Math.PI * 2;
|
|
1645
|
+
pts.push({ x: cx + rx * Math.cos(t), y: cy + ry * Math.sin(t) });
|
|
1646
|
+
}
|
|
1647
|
+
return [pts];
|
|
1648
|
+
}
|
|
1649
|
+
|
|
1650
|
+
function hitStroke(st, p, tol) {
|
|
1651
|
+
const reach = tol + st.w / 2;
|
|
1652
|
+
for (const line of outlines(st)) {
|
|
1653
|
+
if (line.length === 1 && dist(line[0], p) <= reach) return true;
|
|
1654
|
+
for (let i = 1; i < line.length; i++) if (segDist(p, line[i - 1], line[i]) <= reach) return true;
|
|
1655
|
+
}
|
|
1656
|
+
return false;
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
function drawStroke(ctx, st) {
|
|
1660
|
+
ctx.save();
|
|
1661
|
+
ctx.strokeStyle = st.color;
|
|
1662
|
+
ctx.fillStyle = st.color;
|
|
1663
|
+
ctx.lineWidth = st.w;
|
|
1664
|
+
ctx.lineCap = 'round';
|
|
1665
|
+
ctx.lineJoin = 'round';
|
|
1666
|
+
ctx.beginPath();
|
|
1667
|
+
if (st.kind === 'pen') {
|
|
1668
|
+
const pts = st.points;
|
|
1669
|
+
if (pts.length === 1) {
|
|
1670
|
+
ctx.arc(pts[0].x, pts[0].y, st.w / 2, 0, Math.PI * 2);
|
|
1671
|
+
ctx.fill();
|
|
1672
|
+
ctx.restore();
|
|
1673
|
+
return;
|
|
1674
|
+
}
|
|
1675
|
+
ctx.moveTo(pts[0].x, pts[0].y);
|
|
1676
|
+
for (let i = 1; i < pts.length - 1; i++) {
|
|
1677
|
+
const mx = (pts[i].x + pts[i + 1].x) / 2;
|
|
1678
|
+
const my = (pts[i].y + pts[i + 1].y) / 2;
|
|
1679
|
+
ctx.quadraticCurveTo(pts[i].x, pts[i].y, mx, my);
|
|
1680
|
+
}
|
|
1681
|
+
const last = pts[pts.length - 1];
|
|
1682
|
+
ctx.lineTo(last.x, last.y);
|
|
1683
|
+
} else if (st.kind === 'line' || st.kind === 'arrow') {
|
|
1684
|
+
ctx.moveTo(st.a.x, st.a.y);
|
|
1685
|
+
ctx.lineTo(st.b.x, st.b.y);
|
|
1686
|
+
if (st.kind === 'arrow') {
|
|
1687
|
+
const [l, r] = arrowHead(st.a, st.b, st.w);
|
|
1688
|
+
ctx.moveTo(l.x, l.y);
|
|
1689
|
+
ctx.lineTo(st.b.x, st.b.y);
|
|
1690
|
+
ctx.lineTo(r.x, r.y);
|
|
1691
|
+
}
|
|
1692
|
+
} else if (st.kind === 'rect') {
|
|
1693
|
+
ctx.rect(Math.min(st.a.x, st.b.x), Math.min(st.a.y, st.b.y), Math.abs(st.b.x - st.a.x), Math.abs(st.b.y - st.a.y));
|
|
1694
|
+
} else {
|
|
1695
|
+
ctx.ellipse((st.a.x + st.b.x) / 2, (st.a.y + st.b.y) / 2, Math.abs(st.b.x - st.a.x) / 2, Math.abs(st.b.y - st.a.y) / 2, 0, 0, Math.PI * 2);
|
|
1696
|
+
}
|
|
1697
|
+
ctx.stroke();
|
|
1698
|
+
ctx.restore();
|
|
1699
|
+
}
|
|
1700
|
+
|
|
1701
|
+
function stemOf(src) {
|
|
1702
|
+
const base = fileName(src).replace(/\.[a-z0-9]+$/i, '');
|
|
1703
|
+
const safe = base.replace(/[^\w\u4e00-\u9fff-]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 60);
|
|
1704
|
+
return safe || 'image';
|
|
1705
|
+
}
|
|
1706
|
+
|
|
1707
|
+
/**
|
|
1708
|
+
* Full-screen markup editor (pen / shapes / eraser, colours, width,
|
|
1709
|
+
* undo/redo, pinch zoom). Done exports PNG and pastes it into the composer.
|
|
1710
|
+
* @param item - viewer item ({ src, alt }).
|
|
1711
|
+
* @param hooks - { cancel(), done() }.
|
|
1712
|
+
*/
|
|
1713
|
+
function openEditor(item, t, hooks) {
|
|
1714
|
+
const root = h('div', 'qie', { [EDIT_ATTR]: '', [IGNORE_ATTR]: '', role: 'dialog', 'aria-modal': 'true', 'aria-label': t('edit.dialog'), tabindex: '-1' });
|
|
1715
|
+
const btn = (className, label, icon, extra) => {
|
|
1716
|
+
const b = h('button', className, { type: 'button', 'aria-label': label, title: label, ...(extra || {}) });
|
|
1717
|
+
b.innerHTML = icon;
|
|
1718
|
+
return b;
|
|
1719
|
+
};
|
|
1720
|
+
|
|
1721
|
+
//#region dom
|
|
1722
|
+
const top = h('div', 'qie-top');
|
|
1723
|
+
const backBtn = btn('qie-round', t('edit.cancel'), ICON.back);
|
|
1724
|
+
const tools = h('div', 'qie-tools', { role: 'toolbar', 'aria-label': t('edit.tools') });
|
|
1725
|
+
const penBtn = btn('qie-tool', t('edit.pen'), ICON.pen, { 'aria-pressed': 'true' });
|
|
1726
|
+
const shapeBtn = btn('qie-tool', t('edit.shapes'), ICON.shapes, { 'aria-pressed': 'false', 'aria-haspopup': 'true', 'aria-expanded': 'false' });
|
|
1727
|
+
const eraseBtn = btn('qie-tool', t('edit.eraser'), ICON.eraser, { 'aria-pressed': 'false' });
|
|
1728
|
+
tools.append(penBtn, shapeBtn, eraseBtn);
|
|
1729
|
+
const history = h('div', 'qie-history');
|
|
1730
|
+
const undoBtn = btn('qie-round', t('edit.undo'), ICON.undo);
|
|
1731
|
+
const redoBtn = btn('qie-round', t('edit.redo'), ICON.redo);
|
|
1732
|
+
history.append(undoBtn, redoBtn);
|
|
1733
|
+
const pop = h('div', 'qie-pop', { role: 'group', 'aria-label': t('edit.shapes') });
|
|
1734
|
+
pop.hidden = true;
|
|
1735
|
+
const shapeButtons = {};
|
|
1736
|
+
for (const kind of ['arrow', 'rect', 'ellipse', 'line']) {
|
|
1737
|
+
const b = btn('qie-shape', t('edit.shape.' + kind), ICON[kind], { 'data-kind': kind, 'aria-pressed': 'false' });
|
|
1738
|
+
shapeButtons[kind] = b;
|
|
1739
|
+
pop.appendChild(b);
|
|
1740
|
+
}
|
|
1741
|
+
top.append(backBtn, tools, history, pop);
|
|
1742
|
+
|
|
1743
|
+
const area = h('div', 'qie-area');
|
|
1744
|
+
const stage = h('div', 'qie-stage');
|
|
1745
|
+
let base = h('img', 'qie-base', { alt: item.alt || '', draggable: 'false' });
|
|
1746
|
+
const ink = h('canvas', 'qie-ink');
|
|
1747
|
+
const live = h('canvas', 'qie-live');
|
|
1748
|
+
stage.append(base, ink, live);
|
|
1749
|
+
const status = h('div', 'qie-status');
|
|
1750
|
+
status.innerHTML = '<div class="qiv-spin"></div>';
|
|
1751
|
+
const note = h('div', 'qie-note');
|
|
1752
|
+
note.hidden = true;
|
|
1753
|
+
note.textContent = t('edit.taintedHint');
|
|
1754
|
+
area.append(stage, status, note);
|
|
1755
|
+
|
|
1756
|
+
const slider = h('div', 'qie-size', {
|
|
1757
|
+
role: 'slider',
|
|
1758
|
+
tabindex: '0',
|
|
1759
|
+
'aria-label': t('edit.size'),
|
|
1760
|
+
'aria-orientation': 'vertical',
|
|
1761
|
+
'aria-valuemin': String(SIZE_MIN),
|
|
1762
|
+
'aria-valuemax': String(SIZE_MAX),
|
|
1763
|
+
});
|
|
1764
|
+
const rail = h('div', 'qie-rail');
|
|
1765
|
+
const knob = h('div', 'qie-knob');
|
|
1766
|
+
const preview = h('div', 'qie-preview', { 'aria-hidden': 'true' });
|
|
1767
|
+
rail.appendChild(knob);
|
|
1768
|
+
slider.append(rail, preview);
|
|
1769
|
+
|
|
1770
|
+
const bottom = h('div', 'qie-bottom');
|
|
1771
|
+
const palette = h('div', 'qie-palette', { role: 'radiogroup', 'aria-label': t('edit.color') });
|
|
1772
|
+
const customWrap = h('label', 'qie-swatch qie-custom', { title: t('edit.custom') });
|
|
1773
|
+
const customInput = h('input', '', { type: 'color', 'aria-label': t('edit.custom') });
|
|
1774
|
+
customWrap.appendChild(customInput);
|
|
1775
|
+
palette.appendChild(customWrap);
|
|
1776
|
+
const swatches = [];
|
|
1777
|
+
for (const [hex, name] of COLORS) {
|
|
1778
|
+
const sw = h('button', 'qie-swatch', { type: 'button', role: 'radio', 'aria-checked': 'false', 'aria-label': t('edit.color.' + name), title: t('edit.color.' + name), 'data-color': hex });
|
|
1779
|
+
sw.style.setProperty('--qie-c', hex);
|
|
1780
|
+
swatches.push(sw);
|
|
1781
|
+
palette.appendChild(sw);
|
|
1782
|
+
}
|
|
1783
|
+
const doneBtn = btn('qie-done', t('edit.done'), ICON.check);
|
|
1784
|
+
bottom.append(palette, doneBtn);
|
|
1785
|
+
root.append(top, area, slider, bottom);
|
|
1786
|
+
//#endregion
|
|
1787
|
+
|
|
1788
|
+
//#region state
|
|
1789
|
+
let W = 0;
|
|
1790
|
+
let H = 0;
|
|
1791
|
+
let img = null;
|
|
1792
|
+
let tainted = false;
|
|
1793
|
+
let inkCtx = null;
|
|
1794
|
+
let liveCtx = null;
|
|
1795
|
+
let strokes = [];
|
|
1796
|
+
const past = [];
|
|
1797
|
+
const future = [];
|
|
1798
|
+
let tool = 'pen';
|
|
1799
|
+
let shape = 'arrow';
|
|
1800
|
+
let color = '#ff3b30';
|
|
1801
|
+
let size = 6;
|
|
1802
|
+
let customColor = null;
|
|
1803
|
+
const view = { z: 1, x: 0, y: 0 };
|
|
1804
|
+
let fitW = 0;
|
|
1805
|
+
let fitH = 0;
|
|
1806
|
+
const pts = new Map();
|
|
1807
|
+
let op = null;
|
|
1808
|
+
let pinch = null;
|
|
1809
|
+
let sawPen = false;
|
|
1810
|
+
let busy = false;
|
|
1811
|
+
let ready = false;
|
|
1812
|
+
let destroyed = false;
|
|
1813
|
+
let raf = 0;
|
|
1814
|
+
//#endregion
|
|
1815
|
+
|
|
1816
|
+
//#region layout + view
|
|
1817
|
+
function layout() {
|
|
1818
|
+
const aw = area.clientWidth || window.innerWidth;
|
|
1819
|
+
const ah = area.clientHeight || window.innerHeight * 0.7;
|
|
1820
|
+
if (W === 0 || H === 0) return;
|
|
1821
|
+
const k = Math.min((aw - 24) / W, (ah - 24) / H);
|
|
1822
|
+
fitW = Math.max(1, W * k);
|
|
1823
|
+
fitH = Math.max(1, H * k);
|
|
1824
|
+
stage.style.width = fitW + 'px';
|
|
1825
|
+
stage.style.height = fitH + 'px';
|
|
1826
|
+
applyView();
|
|
1827
|
+
}
|
|
1828
|
+
|
|
1829
|
+
function viewBounds(z) {
|
|
1830
|
+
const aw = area.clientWidth || window.innerWidth;
|
|
1831
|
+
const ah = area.clientHeight || window.innerHeight * 0.7;
|
|
1832
|
+
return { x: Math.max(0, (fitW * z - aw) / 2 + 12), y: Math.max(0, (fitH * z - ah) / 2 + 12) };
|
|
1833
|
+
}
|
|
1834
|
+
|
|
1835
|
+
function applyView() {
|
|
1836
|
+
stage.style.transform = `translate(-50%,-50%) translate(${view.x}px,${view.y}px) scale(${view.z})`;
|
|
1837
|
+
root.toggleAttribute('data-zoomed', view.z > 1.01);
|
|
1838
|
+
}
|
|
1839
|
+
|
|
1840
|
+
function settleView() {
|
|
1841
|
+
view.z = clamp(view.z, 1, 8);
|
|
1842
|
+
if (view.z <= 1.01) {
|
|
1843
|
+
view.z = 1;
|
|
1844
|
+
view.x = 0;
|
|
1845
|
+
view.y = 0;
|
|
1846
|
+
} else {
|
|
1847
|
+
const b = viewBounds(view.z);
|
|
1848
|
+
view.x = clamp(view.x, -b.x, b.x);
|
|
1849
|
+
view.y = clamp(view.y, -b.y, b.y);
|
|
1850
|
+
}
|
|
1851
|
+
root.setAttribute('data-settle', '');
|
|
1852
|
+
applyView();
|
|
1853
|
+
setTimeout(() => root.removeAttribute('data-settle'), 260);
|
|
1854
|
+
}
|
|
1855
|
+
|
|
1856
|
+
/** Zoom so the content point under (cx, cy) stays put. */
|
|
1857
|
+
function zoomAround(z, cx, cy) {
|
|
1858
|
+
const r = area.getBoundingClientRect();
|
|
1859
|
+
const ox = cx - (r.left + r.width / 2);
|
|
1860
|
+
const oy = cy - (r.top + r.height / 2);
|
|
1861
|
+
const k = z / view.z;
|
|
1862
|
+
view.x = ox - (ox - view.x) * k;
|
|
1863
|
+
view.y = oy - (oy - view.y) * k;
|
|
1864
|
+
view.z = z;
|
|
1865
|
+
applyView();
|
|
1866
|
+
}
|
|
1867
|
+
|
|
1868
|
+
/** Client point -> working-canvas pixel. */
|
|
1869
|
+
function toImage(e) {
|
|
1870
|
+
const r = stage.getBoundingClientRect();
|
|
1871
|
+
const rw = r.width || fitW * view.z || 1;
|
|
1872
|
+
const rh = r.height || fitH * view.z || 1;
|
|
1873
|
+
return { x: ((e.clientX - r.left) / rw) * W, y: ((e.clientY - r.top) / rh) * H };
|
|
1874
|
+
}
|
|
1875
|
+
|
|
1876
|
+
/** Screen px -> image px at the current zoom. */
|
|
1877
|
+
function imagePx(px) {
|
|
1878
|
+
return (px * W) / Math.max(1, fitW * view.z);
|
|
1879
|
+
}
|
|
1880
|
+
//#endregion
|
|
1881
|
+
|
|
1882
|
+
//#region drawing
|
|
1883
|
+
function redraw() {
|
|
1884
|
+
if (inkCtx === null) return;
|
|
1885
|
+
inkCtx.clearRect(0, 0, W, H);
|
|
1886
|
+
for (const st of strokes) drawStroke(inkCtx, st);
|
|
1887
|
+
}
|
|
1888
|
+
|
|
1889
|
+
function paintLive() {
|
|
1890
|
+
raf = 0;
|
|
1891
|
+
if (liveCtx === null) return;
|
|
1892
|
+
liveCtx.clearRect(0, 0, W, H);
|
|
1893
|
+
if (op !== null && op.stroke) drawStroke(liveCtx, op.stroke);
|
|
1894
|
+
}
|
|
1895
|
+
|
|
1896
|
+
function scheduleLive() {
|
|
1897
|
+
if (raf === 0) raf = requestAnimationFrame(paintLive);
|
|
1898
|
+
}
|
|
1899
|
+
|
|
1900
|
+
function syncHistory() {
|
|
1901
|
+
undoBtn.disabled = past.length === 0;
|
|
1902
|
+
redoBtn.disabled = future.length === 0;
|
|
1903
|
+
}
|
|
1904
|
+
|
|
1905
|
+
function eraseAt(p) {
|
|
1906
|
+
const tol = imagePx(Math.max(8, size));
|
|
1907
|
+
const kept = strokes.filter((st) => !hitStroke(st, p, tol));
|
|
1908
|
+
if (kept.length === strokes.length) return;
|
|
1909
|
+
if (op.before === null) op.before = strokes;
|
|
1910
|
+
strokes = kept;
|
|
1911
|
+
redraw();
|
|
1912
|
+
}
|
|
1913
|
+
|
|
1914
|
+
function undo() {
|
|
1915
|
+
if (past.length === 0) return;
|
|
1916
|
+
future.push(strokes);
|
|
1917
|
+
strokes = past.pop();
|
|
1918
|
+
redraw();
|
|
1919
|
+
syncHistory();
|
|
1920
|
+
}
|
|
1921
|
+
|
|
1922
|
+
function redo() {
|
|
1923
|
+
if (future.length === 0) return;
|
|
1924
|
+
past.push(strokes);
|
|
1925
|
+
strokes = future.pop();
|
|
1926
|
+
redraw();
|
|
1927
|
+
syncHistory();
|
|
1928
|
+
}
|
|
1929
|
+
|
|
1930
|
+
/** Abandon the in-flight operation (a second finger arrived). */
|
|
1931
|
+
function cancelOp() {
|
|
1932
|
+
if (op === null) return;
|
|
1933
|
+
if (op.kind === 'erase' && op.before !== null) {
|
|
1934
|
+
past.push(op.before);
|
|
1935
|
+
future.length = 0;
|
|
1936
|
+
syncHistory();
|
|
1937
|
+
}
|
|
1938
|
+
op = null;
|
|
1939
|
+
if (raf !== 0) cancelAnimationFrame(raf);
|
|
1940
|
+
paintLive();
|
|
1941
|
+
}
|
|
1942
|
+
//#endregion
|
|
1943
|
+
|
|
1944
|
+
//#region pointer input
|
|
1945
|
+
function onDown(e) {
|
|
1946
|
+
if (!ready || busy) return;
|
|
1947
|
+
if (e.pointerType === 'mouse' && e.button !== 0 && e.button !== 1) return;
|
|
1948
|
+
if (e.pointerType === 'pen') sawPen = true;
|
|
1949
|
+
// Palm rejection: with an Apple Pencil in use, fingers only navigate.
|
|
1950
|
+
const navOnly = sawPen && e.pointerType === 'touch';
|
|
1951
|
+
e.preventDefault();
|
|
1952
|
+
try {
|
|
1953
|
+
area.setPointerCapture(e.pointerId);
|
|
1954
|
+
} catch {
|
|
1955
|
+
// capture is best-effort
|
|
1956
|
+
}
|
|
1957
|
+
pts.set(e.pointerId, { x: e.clientX, y: e.clientY, type: e.pointerType });
|
|
1958
|
+
closePop();
|
|
1959
|
+
if (pts.size >= 2) {
|
|
1960
|
+
cancelOp();
|
|
1961
|
+
const [a, b] = [...pts.values()];
|
|
1962
|
+
pinch = { d: Math.max(1, dist(a, b)), z: view.z, vx: view.x, vy: view.y, mx: (a.x + b.x) / 2, my: (a.y + b.y) / 2 };
|
|
1963
|
+
return;
|
|
1964
|
+
}
|
|
1965
|
+
if (e.button === 1 || navOnly || (e.pointerType === 'mouse' && e.altKey && view.z > 1)) {
|
|
1966
|
+
op = { kind: 'pan', id: e.pointerId, sx: e.clientX, sy: e.clientY, vx: view.x, vy: view.y };
|
|
1967
|
+
return;
|
|
1968
|
+
}
|
|
1969
|
+
const p = toImage(e);
|
|
1970
|
+
if (tool === 'erase') {
|
|
1971
|
+
op = { kind: 'erase', id: e.pointerId, before: null };
|
|
1972
|
+
eraseAt(p);
|
|
1973
|
+
return;
|
|
1974
|
+
}
|
|
1975
|
+
const w = imagePx(size);
|
|
1976
|
+
const stroke = tool === 'pen' ? { kind: 'pen', color, w, points: [p] } : { kind: shape, color, w, a: p, b: p };
|
|
1977
|
+
op = { kind: 'draw', id: e.pointerId, stroke, sx: e.clientX, sy: e.clientY, moved: 0 };
|
|
1978
|
+
scheduleLive();
|
|
1979
|
+
}
|
|
1980
|
+
|
|
1981
|
+
function onMove(e) {
|
|
1982
|
+
const p0 = pts.get(e.pointerId);
|
|
1983
|
+
if (p0 === undefined) return;
|
|
1984
|
+
p0.x = e.clientX;
|
|
1985
|
+
p0.y = e.clientY;
|
|
1986
|
+
if (pinch !== null) {
|
|
1987
|
+
if (pts.size < 2) return;
|
|
1988
|
+
const [a, b] = [...pts.values()];
|
|
1989
|
+
const z = clamp(pinch.z * (dist(a, b) / pinch.d), 0.6, 10);
|
|
1990
|
+
const mx = (a.x + b.x) / 2;
|
|
1991
|
+
const my = (a.y + b.y) / 2;
|
|
1992
|
+
const r = area.getBoundingClientRect();
|
|
1993
|
+
const ox = pinch.mx - (r.left + r.width / 2);
|
|
1994
|
+
const oy = pinch.my - (r.top + r.height / 2);
|
|
1995
|
+
const k = z / pinch.z;
|
|
1996
|
+
view.z = z;
|
|
1997
|
+
view.x = ox - (ox - pinch.vx) * k + (mx - pinch.mx);
|
|
1998
|
+
view.y = oy - (oy - pinch.vy) * k + (my - pinch.my);
|
|
1999
|
+
applyView();
|
|
2000
|
+
return;
|
|
2001
|
+
}
|
|
2002
|
+
if (op === null || op.id !== e.pointerId) return;
|
|
2003
|
+
if (op.kind === 'pan') {
|
|
2004
|
+
view.x = op.vx + e.clientX - op.sx;
|
|
2005
|
+
view.y = op.vy + e.clientY - op.sy;
|
|
2006
|
+
applyView();
|
|
2007
|
+
return;
|
|
2008
|
+
}
|
|
2009
|
+
const events = typeof e.getCoalescedEvents === 'function' ? e.getCoalescedEvents() : [];
|
|
2010
|
+
const list = events.length > 0 ? events : [e];
|
|
2011
|
+
if (op.kind === 'erase') {
|
|
2012
|
+
for (const ev of list) eraseAt(toImage(ev));
|
|
2013
|
+
return;
|
|
2014
|
+
}
|
|
2015
|
+
op.moved = Math.max(op.moved, Math.hypot(e.clientX - op.sx, e.clientY - op.sy));
|
|
2016
|
+
const st = op.stroke;
|
|
2017
|
+
if (st.kind === 'pen') {
|
|
2018
|
+
const min = imagePx(1.2);
|
|
2019
|
+
for (const ev of list) {
|
|
2020
|
+
const p = toImage(ev);
|
|
2021
|
+
if (dist(p, st.points[st.points.length - 1]) >= min) st.points.push(p);
|
|
2022
|
+
}
|
|
2023
|
+
} else {
|
|
2024
|
+
let b = toImage(e);
|
|
2025
|
+
if (e.shiftKey) {
|
|
2026
|
+
const dx = b.x - st.a.x;
|
|
2027
|
+
const dy = b.y - st.a.y;
|
|
2028
|
+
if (st.kind === 'rect' || st.kind === 'ellipse') {
|
|
2029
|
+
const m = Math.max(Math.abs(dx), Math.abs(dy));
|
|
2030
|
+
b = { x: st.a.x + Math.sign(dx || 1) * m, y: st.a.y + Math.sign(dy || 1) * m };
|
|
2031
|
+
} else {
|
|
2032
|
+
const ang = Math.round(Math.atan2(dy, dx) / (Math.PI / 4)) * (Math.PI / 4);
|
|
2033
|
+
const len = Math.hypot(dx, dy);
|
|
2034
|
+
b = { x: st.a.x + len * Math.cos(ang), y: st.a.y + len * Math.sin(ang) };
|
|
2035
|
+
}
|
|
2036
|
+
}
|
|
2037
|
+
st.b = b;
|
|
2038
|
+
}
|
|
2039
|
+
scheduleLive();
|
|
2040
|
+
}
|
|
2041
|
+
|
|
2042
|
+
function onUp(e) {
|
|
2043
|
+
if (!pts.has(e.pointerId)) return;
|
|
2044
|
+
pts.delete(e.pointerId);
|
|
2045
|
+
if (pinch !== null) {
|
|
2046
|
+
if (pts.size < 2) {
|
|
2047
|
+
pinch = null;
|
|
2048
|
+
settleView();
|
|
2049
|
+
}
|
|
2050
|
+
return;
|
|
2051
|
+
}
|
|
2052
|
+
if (op === null || op.id !== e.pointerId) return;
|
|
2053
|
+
const done = op;
|
|
2054
|
+
op = null;
|
|
2055
|
+
if (done.kind === 'pan') {
|
|
2056
|
+
settleView();
|
|
2057
|
+
return;
|
|
2058
|
+
}
|
|
2059
|
+
if (done.kind === 'erase') {
|
|
2060
|
+
if (done.before !== null) {
|
|
2061
|
+
past.push(done.before);
|
|
2062
|
+
future.length = 0;
|
|
2063
|
+
syncHistory();
|
|
2064
|
+
}
|
|
2065
|
+
return;
|
|
2066
|
+
}
|
|
2067
|
+
if (raf !== 0) {
|
|
2068
|
+
cancelAnimationFrame(raf);
|
|
2069
|
+
raf = 0;
|
|
2070
|
+
}
|
|
2071
|
+
liveCtx.clearRect(0, 0, W, H);
|
|
2072
|
+
const st = done.stroke;
|
|
2073
|
+
if (e.type === 'pointercancel') return;
|
|
2074
|
+
if (st.kind !== 'pen' && dist(st.a, st.b) < imagePx(4)) return;
|
|
2075
|
+
past.push(strokes);
|
|
2076
|
+
future.length = 0;
|
|
2077
|
+
strokes = strokes.concat([st]);
|
|
2078
|
+
drawStroke(inkCtx, st);
|
|
2079
|
+
syncHistory();
|
|
2080
|
+
}
|
|
2081
|
+
|
|
2082
|
+
function onWheel(e) {
|
|
2083
|
+
if (!ready) return;
|
|
2084
|
+
e.preventDefault();
|
|
2085
|
+
if (e.ctrlKey || e.metaKey || view.z <= 1.01) {
|
|
2086
|
+
const unit = e.deltaMode === 1 ? 16 : 1;
|
|
2087
|
+
const z = clamp(view.z * Math.exp((-e.deltaY * unit) / (e.ctrlKey ? 100 : 400)), 1, 8);
|
|
2088
|
+
zoomAround(z, e.clientX, e.clientY);
|
|
2089
|
+
if (z <= 1.01) settleView();
|
|
2090
|
+
} else {
|
|
2091
|
+
view.x -= e.deltaX;
|
|
2092
|
+
view.y -= e.deltaY;
|
|
2093
|
+
const b = viewBounds(view.z);
|
|
2094
|
+
view.x = clamp(view.x, -b.x, b.x);
|
|
2095
|
+
view.y = clamp(view.y, -b.y, b.y);
|
|
2096
|
+
applyView();
|
|
2097
|
+
}
|
|
2098
|
+
}
|
|
2099
|
+
//#endregion
|
|
2100
|
+
|
|
2101
|
+
//#region tools, colour, size
|
|
2102
|
+
function closePop() {
|
|
2103
|
+
if (pop.hidden) return;
|
|
2104
|
+
pop.hidden = true;
|
|
2105
|
+
shapeBtn.setAttribute('aria-expanded', 'false');
|
|
2106
|
+
}
|
|
2107
|
+
|
|
2108
|
+
function setTool(next) {
|
|
2109
|
+
tool = next;
|
|
2110
|
+
penBtn.setAttribute('aria-pressed', String(next === 'pen'));
|
|
2111
|
+
shapeBtn.setAttribute('aria-pressed', String(next === 'shape'));
|
|
2112
|
+
eraseBtn.setAttribute('aria-pressed', String(next === 'erase'));
|
|
2113
|
+
root.setAttribute('data-tool', next);
|
|
2114
|
+
shapeBtn.innerHTML = ICON[shape];
|
|
2115
|
+
for (const [kind, b] of Object.entries(shapeButtons)) b.setAttribute('aria-pressed', String(kind === shape));
|
|
2116
|
+
}
|
|
2117
|
+
|
|
2118
|
+
function setColor(hex, custom) {
|
|
2119
|
+
color = hex;
|
|
2120
|
+
for (const sw of swatches) sw.setAttribute('aria-checked', String(!custom && sw.dataset.color === hex));
|
|
2121
|
+
customWrap.toggleAttribute('data-checked', custom === true);
|
|
2122
|
+
root.style.setProperty('--qie-ink', hex);
|
|
2123
|
+
if (tool === 'erase') setTool('pen');
|
|
2124
|
+
syncSize();
|
|
2125
|
+
}
|
|
2126
|
+
|
|
2127
|
+
function syncSize() {
|
|
2128
|
+
const k = Math.sqrt((size - SIZE_MIN) / (SIZE_MAX - SIZE_MIN));
|
|
2129
|
+
slider.style.setProperty('--qie-k', String(k));
|
|
2130
|
+
slider.setAttribute('aria-valuenow', String(Math.round(size)));
|
|
2131
|
+
const d = Math.max(4, size);
|
|
2132
|
+
preview.style.width = d + 'px';
|
|
2133
|
+
preview.style.height = d + 'px';
|
|
2134
|
+
}
|
|
2135
|
+
|
|
2136
|
+
function sizeFromPointer(e) {
|
|
2137
|
+
const r = rail.getBoundingClientRect();
|
|
2138
|
+
const k = clamp(1 - (e.clientY - r.top) / Math.max(1, r.height), 0, 1);
|
|
2139
|
+
size = SIZE_MIN + k * k * (SIZE_MAX - SIZE_MIN);
|
|
2140
|
+
syncSize();
|
|
2141
|
+
}
|
|
2142
|
+
|
|
2143
|
+
let sizeDrag = null;
|
|
2144
|
+
function onSizeDown(e) {
|
|
2145
|
+
e.preventDefault();
|
|
2146
|
+
e.stopPropagation();
|
|
2147
|
+
sizeDrag = e.pointerId;
|
|
2148
|
+
try {
|
|
2149
|
+
slider.setPointerCapture(e.pointerId);
|
|
2150
|
+
} catch {
|
|
2151
|
+
// capture is best-effort
|
|
2152
|
+
}
|
|
2153
|
+
slider.setAttribute('data-active', '');
|
|
2154
|
+
sizeFromPointer(e);
|
|
2155
|
+
}
|
|
2156
|
+
function onSizeMove(e) {
|
|
2157
|
+
if (sizeDrag === e.pointerId) sizeFromPointer(e);
|
|
2158
|
+
}
|
|
2159
|
+
function onSizeUp(e) {
|
|
2160
|
+
if (sizeDrag !== e.pointerId) return;
|
|
2161
|
+
sizeDrag = null;
|
|
2162
|
+
slider.removeAttribute('data-active');
|
|
2163
|
+
}
|
|
2164
|
+
function onSizeKey(e) {
|
|
2165
|
+
const step = e.shiftKey ? 4 : 1;
|
|
2166
|
+
if (e.key === 'ArrowUp' || e.key === 'ArrowRight') size = clamp(size + step, SIZE_MIN, SIZE_MAX);
|
|
2167
|
+
else if (e.key === 'ArrowDown' || e.key === 'ArrowLeft') size = clamp(size - step, SIZE_MIN, SIZE_MAX);
|
|
2168
|
+
else return;
|
|
2169
|
+
e.preventDefault();
|
|
2170
|
+
e.stopPropagation();
|
|
2171
|
+
syncSize();
|
|
2172
|
+
}
|
|
2173
|
+
//#endregion
|
|
2174
|
+
|
|
2175
|
+
//#region keyboard + focus
|
|
2176
|
+
function trap(e) {
|
|
2177
|
+
const list = [...root.querySelectorAll('button:not([disabled]),input,[tabindex="0"]')].filter(
|
|
2178
|
+
(el) => el.closest('[hidden]') === null,
|
|
2179
|
+
);
|
|
2180
|
+
if (list.length === 0) return;
|
|
2181
|
+
const first = list[0];
|
|
2182
|
+
const last = list[list.length - 1];
|
|
2183
|
+
const active = document.activeElement;
|
|
2184
|
+
if (!root.contains(active)) {
|
|
2185
|
+
e.preventDefault();
|
|
2186
|
+
first.focus();
|
|
2187
|
+
} else if (e.shiftKey && active === first) {
|
|
2188
|
+
e.preventDefault();
|
|
2189
|
+
last.focus();
|
|
2190
|
+
} else if (!e.shiftKey && active === last) {
|
|
2191
|
+
e.preventDefault();
|
|
2192
|
+
first.focus();
|
|
2193
|
+
}
|
|
2194
|
+
}
|
|
2195
|
+
|
|
2196
|
+
function onKey(e) {
|
|
2197
|
+
if (destroyed) return;
|
|
2198
|
+
const mod = e.ctrlKey || e.metaKey;
|
|
2199
|
+
if (e.key === 'Escape') {
|
|
2200
|
+
e.preventDefault();
|
|
2201
|
+
if (!pop.hidden) closePop();
|
|
2202
|
+
else cancel();
|
|
2203
|
+
} else if (mod && (e.key === 'z' || e.key === 'Z')) {
|
|
2204
|
+
e.preventDefault();
|
|
2205
|
+
if (e.shiftKey) redo();
|
|
2206
|
+
else undo();
|
|
2207
|
+
} else if (mod && (e.key === 'y' || e.key === 'Y')) {
|
|
2208
|
+
e.preventDefault();
|
|
2209
|
+
redo();
|
|
2210
|
+
} else if (e.key === 'Enter' && !mod) {
|
|
2211
|
+
const target = e.target;
|
|
2212
|
+
if (target instanceof Element && target.closest('button,input,[role="slider"]') !== null) return;
|
|
2213
|
+
e.preventDefault();
|
|
2214
|
+
confirm();
|
|
2215
|
+
} else if (e.key === 'Tab') {
|
|
2216
|
+
trap(e);
|
|
2217
|
+
}
|
|
2218
|
+
// The editor is modal: nothing behind it reacts to keys.
|
|
2219
|
+
e.stopPropagation();
|
|
2220
|
+
}
|
|
2221
|
+
//#endregion
|
|
2222
|
+
|
|
2223
|
+
//#region export + finish
|
|
2224
|
+
function blobOf(canvas, type, quality) {
|
|
2225
|
+
return new Promise((resolve, reject) => {
|
|
2226
|
+
try {
|
|
2227
|
+
canvas.toBlob((blob) => (blob ? resolve(blob) : reject(new Error('export failed'))), type, quality);
|
|
2228
|
+
} catch (error) {
|
|
2229
|
+
reject(error);
|
|
2230
|
+
}
|
|
2231
|
+
});
|
|
2232
|
+
}
|
|
2233
|
+
|
|
2234
|
+
async function exportFile() {
|
|
2235
|
+
const out = document.createElement('canvas');
|
|
2236
|
+
out.width = W;
|
|
2237
|
+
out.height = H;
|
|
2238
|
+
const ctx = out.getContext('2d');
|
|
2239
|
+
ctx.drawImage(img, 0, 0, W, H);
|
|
2240
|
+
ctx.drawImage(ink, 0, 0);
|
|
2241
|
+
let blob = await blobOf(out, 'image/png');
|
|
2242
|
+
let ext = 'png';
|
|
2243
|
+
if (blob.size > EXPORT_MAX_BYTES) {
|
|
2244
|
+
const flat = document.createElement('canvas');
|
|
2245
|
+
flat.width = W;
|
|
2246
|
+
flat.height = H;
|
|
2247
|
+
const fctx = flat.getContext('2d');
|
|
2248
|
+
fctx.fillStyle = '#fff';
|
|
2249
|
+
fctx.fillRect(0, 0, W, H);
|
|
2250
|
+
fctx.drawImage(out, 0, 0);
|
|
2251
|
+
blob = await blobOf(flat, 'image/jpeg', 0.9);
|
|
2252
|
+
ext = 'jpg';
|
|
2253
|
+
flat.width = 0;
|
|
2254
|
+
flat.height = 0;
|
|
2255
|
+
}
|
|
2256
|
+
out.width = 0;
|
|
2257
|
+
out.height = 0;
|
|
2258
|
+
const name = stemOf(item.src) + '-annotated.' + ext;
|
|
2259
|
+
try {
|
|
2260
|
+
return new File([blob], name, { type: blob.type, lastModified: Date.now() });
|
|
2261
|
+
} catch {
|
|
2262
|
+
blob.name = name;
|
|
2263
|
+
return blob;
|
|
2264
|
+
}
|
|
2265
|
+
}
|
|
2266
|
+
|
|
2267
|
+
async function confirm() {
|
|
2268
|
+
if (!ready || busy || destroyed) return;
|
|
2269
|
+
busy = true;
|
|
2270
|
+
root.setAttribute('data-busy', '');
|
|
2271
|
+
doneBtn.disabled = true;
|
|
2272
|
+
let file;
|
|
2273
|
+
try {
|
|
2274
|
+
file = await exportFile();
|
|
2275
|
+
} catch (error) {
|
|
2276
|
+
busy = false;
|
|
2277
|
+
root.removeAttribute('data-busy');
|
|
2278
|
+
doneBtn.disabled = false;
|
|
2279
|
+
toast(error && error.name === 'SecurityError' ? t('edit.tainted') : t('edit.exportFailed'));
|
|
2280
|
+
return;
|
|
2281
|
+
}
|
|
2282
|
+
if (destroyed) return;
|
|
2283
|
+
const where = injectFile(file);
|
|
2284
|
+
if (where === 'composer') {
|
|
2285
|
+
toast(t('edit.injected'));
|
|
2286
|
+
} else {
|
|
2287
|
+
downloadFile(file);
|
|
2288
|
+
toast(t('edit.noComposer'));
|
|
2289
|
+
}
|
|
2290
|
+
destroy(false);
|
|
2291
|
+
hooks.done(where === 'composer');
|
|
2292
|
+
}
|
|
2293
|
+
|
|
2294
|
+
function cancel() {
|
|
2295
|
+
if (busy) return;
|
|
2296
|
+
destroy(true);
|
|
2297
|
+
hooks.cancel();
|
|
2298
|
+
}
|
|
2299
|
+
|
|
2300
|
+
function destroy(restore) {
|
|
2301
|
+
if (destroyed) return;
|
|
2302
|
+
destroyed = true;
|
|
2303
|
+
if (raf !== 0) cancelAnimationFrame(raf);
|
|
2304
|
+
window.removeEventListener('keydown', onKey, true);
|
|
2305
|
+
window.removeEventListener('resize', layout);
|
|
2306
|
+
for (const c of [ink, live]) {
|
|
2307
|
+
c.width = 0;
|
|
2308
|
+
c.height = 0;
|
|
2309
|
+
}
|
|
2310
|
+
root.remove();
|
|
2311
|
+
void restore;
|
|
2312
|
+
}
|
|
2313
|
+
//#endregion
|
|
2314
|
+
|
|
2315
|
+
//#region load
|
|
2316
|
+
async function load() {
|
|
2317
|
+
let loaded;
|
|
2318
|
+
try {
|
|
2319
|
+
loaded = await loadEditable(item.src);
|
|
2320
|
+
} catch {
|
|
2321
|
+
if (destroyed) return;
|
|
2322
|
+
status.textContent = t('failed');
|
|
2323
|
+
doneBtn.disabled = true;
|
|
2324
|
+
return;
|
|
2325
|
+
}
|
|
2326
|
+
if (destroyed) return;
|
|
2327
|
+
img = loaded.img;
|
|
2328
|
+
tainted = loaded.tainted;
|
|
2329
|
+
const nw = img.naturalWidth || img.width || item.w || 1024;
|
|
2330
|
+
const nh = img.naturalHeight || img.height || item.h || 768;
|
|
2331
|
+
const k = Math.min(1, EDIT_MAX_EDGE / Math.max(nw, nh), Math.sqrt(EDIT_MAX_PIXELS / (nw * nh)));
|
|
2332
|
+
W = Math.max(1, Math.round(nw * k));
|
|
2333
|
+
H = Math.max(1, Math.round(nh * k));
|
|
2334
|
+
for (const c of [ink, live]) {
|
|
2335
|
+
c.width = W;
|
|
2336
|
+
c.height = H;
|
|
2337
|
+
}
|
|
2338
|
+
inkCtx = ink.getContext('2d');
|
|
2339
|
+
liveCtx = live.getContext('2d');
|
|
2340
|
+
img.className = 'qie-base';
|
|
2341
|
+
img.alt = item.alt || '';
|
|
2342
|
+
img.draggable = false;
|
|
2343
|
+
base.replaceWith(img);
|
|
2344
|
+
base = img;
|
|
2345
|
+
status.hidden = true;
|
|
2346
|
+
note.hidden = !tainted;
|
|
2347
|
+
ready = true;
|
|
2348
|
+
root.setAttribute('data-ready', '');
|
|
2349
|
+
layout();
|
|
2350
|
+
}
|
|
2351
|
+
//#endregion
|
|
2352
|
+
|
|
2353
|
+
area.addEventListener('pointerdown', onDown);
|
|
2354
|
+
area.addEventListener('pointermove', onMove);
|
|
2355
|
+
area.addEventListener('pointerup', onUp);
|
|
2356
|
+
area.addEventListener('pointercancel', onUp);
|
|
2357
|
+
area.addEventListener('wheel', onWheel, { passive: false });
|
|
2358
|
+
root.addEventListener('dragstart', (e) => e.preventDefault());
|
|
2359
|
+
root.addEventListener('gesturestart', (e) => e.preventDefault());
|
|
2360
|
+
root.addEventListener('contextmenu', (e) => {
|
|
2361
|
+
if (area.contains(e.target)) e.preventDefault();
|
|
2362
|
+
});
|
|
2363
|
+
slider.addEventListener('pointerdown', onSizeDown);
|
|
2364
|
+
slider.addEventListener('pointermove', onSizeMove);
|
|
2365
|
+
slider.addEventListener('pointerup', onSizeUp);
|
|
2366
|
+
slider.addEventListener('pointercancel', onSizeUp);
|
|
2367
|
+
slider.addEventListener('keydown', onSizeKey);
|
|
2368
|
+
window.addEventListener('keydown', onKey, true);
|
|
2369
|
+
window.addEventListener('resize', layout);
|
|
2370
|
+
backBtn.addEventListener('click', cancel);
|
|
2371
|
+
undoBtn.addEventListener('click', undo);
|
|
2372
|
+
redoBtn.addEventListener('click', redo);
|
|
2373
|
+
penBtn.addEventListener('click', () => {
|
|
2374
|
+
closePop();
|
|
2375
|
+
setTool('pen');
|
|
2376
|
+
});
|
|
2377
|
+
eraseBtn.addEventListener('click', () => {
|
|
2378
|
+
closePop();
|
|
2379
|
+
setTool('erase');
|
|
2380
|
+
});
|
|
2381
|
+
shapeBtn.addEventListener('click', () => {
|
|
2382
|
+
if (tool === 'shape' && pop.hidden) {
|
|
2383
|
+
pop.hidden = false;
|
|
2384
|
+
shapeBtn.setAttribute('aria-expanded', 'true');
|
|
2385
|
+
} else if (!pop.hidden) {
|
|
2386
|
+
closePop();
|
|
2387
|
+
} else {
|
|
2388
|
+
setTool('shape');
|
|
2389
|
+
}
|
|
2390
|
+
});
|
|
2391
|
+
for (const [kind, b] of Object.entries(shapeButtons)) {
|
|
2392
|
+
b.addEventListener('click', () => {
|
|
2393
|
+
shape = kind;
|
|
2394
|
+
setTool('shape');
|
|
2395
|
+
closePop();
|
|
2396
|
+
});
|
|
2397
|
+
}
|
|
2398
|
+
for (const sw of swatches) sw.addEventListener('click', () => setColor(sw.dataset.color, false));
|
|
2399
|
+
customInput.addEventListener('input', () => {
|
|
2400
|
+
customColor = customInput.value;
|
|
2401
|
+
customWrap.style.setProperty('--qie-c', customColor);
|
|
2402
|
+
setColor(customColor, true);
|
|
2403
|
+
});
|
|
2404
|
+
customWrap.addEventListener('click', () => {
|
|
2405
|
+
if (customColor !== null) setColor(customColor, true);
|
|
2406
|
+
});
|
|
2407
|
+
doneBtn.addEventListener('click', confirm);
|
|
2408
|
+
|
|
2409
|
+
setTool('pen');
|
|
2410
|
+
setColor(color, false);
|
|
2411
|
+
syncHistory();
|
|
2412
|
+
document.body.appendChild(root);
|
|
2413
|
+
try {
|
|
2414
|
+
doneBtn.focus({ preventScroll: true });
|
|
2415
|
+
} catch {
|
|
2416
|
+
// focus is best-effort
|
|
2417
|
+
}
|
|
2418
|
+
load();
|
|
2419
|
+
|
|
2420
|
+
return {
|
|
2421
|
+
destroy: () => destroy(false),
|
|
2422
|
+
/** Test hooks: inspect strokes without reaching into closures. */
|
|
2423
|
+
get strokes() {
|
|
2424
|
+
return strokes.length;
|
|
2425
|
+
},
|
|
2426
|
+
};
|
|
2427
|
+
}
|
|
2428
|
+
|
|
2429
|
+
const EDIT_CSS = `
|
|
2430
|
+
[${EDIT_ATTR}]{position:fixed;inset:0;z-index:2147483001;display:flex;flex-direction:column;background:#000;color:#fff;font:13px/1.4 system-ui,-apple-system,"Segoe UI",Roboto,sans-serif;user-select:none;-webkit-user-select:none;-webkit-touch-callout:none;-webkit-tap-highlight-color:transparent;overscroll-behavior:contain;outline:none;--qie-ink:#ff3b30}
|
|
2431
|
+
[${EDIT_ATTR}] *{box-sizing:border-box}
|
|
2432
|
+
.qie-top{position:relative;flex:none;display:grid;grid-template-columns:1fr auto 1fr;align-items:center;gap:8px;padding:calc(env(safe-area-inset-top,0px) + 10px) calc(env(safe-area-inset-right,0px) + 14px) 10px calc(env(safe-area-inset-left,0px) + 14px)}
|
|
2433
|
+
.qie-round,.qie-tool,.qie-shape{appearance:none;border:0;padding:0;color:inherit;display:grid;place-items:center;cursor:pointer;transition:background-color .15s,opacity .15s}
|
|
2434
|
+
.qie-round{width:44px;height:44px;border-radius:999px;background:rgba(255,255,255,.14)}
|
|
2435
|
+
.qie-round:hover{background:rgba(255,255,255,.24)}
|
|
2436
|
+
.qie-round[disabled]{opacity:.35;cursor:default}
|
|
2437
|
+
.qie-top>.qie-round{justify-self:start}
|
|
2438
|
+
.qie-history{justify-self:end;display:flex;gap:8px}
|
|
2439
|
+
.qie-tools{display:flex;gap:4px;padding:4px;border-radius:999px;background:rgba(255,255,255,.14)}
|
|
2440
|
+
.qie-tool{width:52px;height:40px;border-radius:999px;background:transparent}
|
|
2441
|
+
.qie-tool:hover{background:rgba(255,255,255,.12)}
|
|
2442
|
+
.qie-tool[aria-pressed=true]{background:rgba(255,255,255,.26)}
|
|
2443
|
+
.qie-tool svg,.qie-round svg{width:22px;height:22px}
|
|
2444
|
+
.qie-pop{position:absolute;top:100%;left:50%;transform:translateX(-50%);z-index:2;display:flex;gap:4px;padding:6px;border-radius:16px;background:rgba(40,40,42,.96);box-shadow:0 8px 28px rgba(0,0,0,.5)}
|
|
2445
|
+
.qie-pop[hidden]{display:none}
|
|
2446
|
+
.qie-shape{width:48px;height:44px;border-radius:12px;background:transparent}
|
|
2447
|
+
.qie-shape:hover{background:rgba(255,255,255,.12)}
|
|
2448
|
+
.qie-shape[aria-pressed=true]{background:rgba(255,255,255,.24)}
|
|
2449
|
+
.qie-area{position:relative;flex:1;min-height:0;overflow:hidden;touch-action:none;cursor:crosshair}
|
|
2450
|
+
[${EDIT_ATTR}][data-tool=erase] .qie-area{cursor:cell}
|
|
2451
|
+
.qie-stage{position:absolute;left:50%;top:50%;transform-origin:50% 50%;box-shadow:0 0 0 1px rgba(255,255,255,.08)}
|
|
2452
|
+
[${EDIT_ATTR}][data-settle] .qie-stage{transition:transform .24s cubic-bezier(.2,.8,.2,1)}
|
|
2453
|
+
.qie-stage>img,.qie-stage>canvas{position:absolute;inset:0;display:block;width:100%;height:100%;-webkit-user-drag:none;pointer-events:none}
|
|
2454
|
+
.qie-status{position:absolute;inset:0;display:grid;place-items:center;pointer-events:none;opacity:.85}
|
|
2455
|
+
.qie-status[hidden],.qie-note[hidden]{display:none}
|
|
2456
|
+
.qie-note{position:absolute;left:50%;top:10px;transform:translateX(-50%);max-width:calc(100% - 32px);padding:6px 12px;border-radius:999px;background:rgba(255,255,255,.16);font-size:12px;text-align:center;pointer-events:none}
|
|
2457
|
+
.qie-size{position:fixed;left:calc(env(safe-area-inset-left,0px) + 8px);top:50%;transform:translateY(-50%);z-index:1;width:36px;height:min(38vh,260px);display:flex;justify-content:center;touch-action:none;cursor:ns-resize;outline:none}
|
|
2458
|
+
.qie-rail{position:relative;width:4px;height:100%;border-radius:2px;background:rgba(255,255,255,.35)}
|
|
2459
|
+
.qie-knob{position:absolute;left:50%;top:calc((1 - var(--qie-k,0.3)) * 100%);width:26px;height:26px;margin:-13px 0 0 -13px;border-radius:50%;background:#fff;box-shadow:0 1px 6px rgba(0,0,0,.5)}
|
|
2460
|
+
.qie-size:focus-visible .qie-knob{outline:2px solid #fff;outline-offset:3px}
|
|
2461
|
+
.qie-preview{position:absolute;left:52px;top:50%;transform:translateY(-50%);border-radius:50%;background:var(--qie-ink);box-shadow:0 0 0 2px rgba(255,255,255,.7);opacity:0;transition:opacity .15s;pointer-events:none}
|
|
2462
|
+
.qie-size[data-active] .qie-preview{opacity:1}
|
|
2463
|
+
.qie-bottom{flex:none;display:flex;align-items:center;gap:12px;padding:10px calc(env(safe-area-inset-right,0px) + 14px) calc(env(safe-area-inset-bottom,0px) + 12px) calc(env(safe-area-inset-left,0px) + 14px)}
|
|
2464
|
+
.qie-palette{flex:1;min-width:0;display:flex;align-items:center;gap:10px;overflow-x:auto;scrollbar-width:none;padding:6px 4px}
|
|
2465
|
+
.qie-palette::-webkit-scrollbar{display:none}
|
|
2466
|
+
.qie-swatch{appearance:none;position:relative;flex:none;width:32px;height:32px;padding:0;border:0;border-radius:50%;background:var(--qie-c);box-shadow:inset 0 0 0 1px rgba(255,255,255,.25);cursor:pointer}
|
|
2467
|
+
.qie-swatch[aria-checked=true],.qie-custom[data-checked]{box-shadow:0 0 0 2px #000,0 0 0 4px #0a84ff}
|
|
2468
|
+
.qie-custom{background:conic-gradient(#ff3b30,#ffcc00,#34c759,#5ac8fa,#007aff,#af52de,#ff3b30)}
|
|
2469
|
+
.qie-custom::after{content:"";position:absolute;inset:5px;border-radius:50%;background:var(--qie-c,#fff);box-shadow:0 0 0 2px #000}
|
|
2470
|
+
.qie-custom input{position:absolute;inset:0;width:100%;height:100%;opacity:0;border:0;padding:0;cursor:pointer}
|
|
2471
|
+
.qie-swatch:focus-visible,.qie-tool:focus-visible,.qie-round:focus-visible,.qie-shape:focus-visible,.qie-done:focus-visible{outline:2px solid #fff;outline-offset:2px}
|
|
2472
|
+
.qie-done{appearance:none;flex:none;width:52px;height:52px;padding:0;border:0;border-radius:50%;background:#0a84ff;color:#fff;display:grid;place-items:center;cursor:pointer}
|
|
2473
|
+
.qie-done svg{width:26px;height:26px}
|
|
2474
|
+
.qie-done[disabled]{opacity:.5;cursor:default}
|
|
2475
|
+
[${EDIT_ATTR}][data-busy] .qie-done svg{display:none}
|
|
2476
|
+
[${EDIT_ATTR}][data-busy] .qie-done::after{content:"";width:22px;height:22px;border-radius:50%;border:2.5px solid rgba(255,255,255,.35);border-top-color:#fff;animation:qiv-rot .8s linear infinite}
|
|
2477
|
+
.qiv-toast{position:fixed;left:50%;bottom:calc(env(safe-area-inset-bottom,0px) + 96px);z-index:2147483002;transform:translate(-50%,8px);max-width:calc(100vw - 32px);padding:10px 16px;border-radius:12px;background:rgba(30,30,32,.94);color:#fff;font:13px/1.45 system-ui,-apple-system,"Segoe UI",Roboto,sans-serif;box-shadow:0 8px 24px rgba(0,0,0,.35);opacity:0;transition:opacity .2s,transform .2s;pointer-events:none}
|
|
2478
|
+
.qiv-toast[data-show]{opacity:1;transform:translate(-50%,0)}
|
|
2479
|
+
@media (prefers-reduced-motion:reduce){[${EDIT_ATTR}][data-settle] .qie-stage,.qiv-toast{transition:none}}
|
|
2480
|
+
`;
|
|
2481
|
+
//#endregion
|
|
2482
|
+
|
|
1335
2483
|
//#region click interception
|
|
1336
2484
|
function installInterceptor(t) {
|
|
1337
2485
|
let viewer = null;
|
package/locale/en.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"meta": {
|
|
3
3
|
"title": "Image Viewer",
|
|
4
|
-
"description": "Full-screen viewer for conversation images with iPhone Photos-style gestures: zoom from thumbnail, pinch and double-tap zoom, momentum pan, swipe between images, swipe down to close."
|
|
4
|
+
"description": "Full-screen viewer for conversation images with iPhone Photos-style gestures: zoom from thumbnail, pinch and double-tap zoom, momentum pan, swipe between images, swipe down to close; annotate with pen and shapes, then drop the result into the chat box."
|
|
5
5
|
}
|
|
6
6
|
}
|
package/locale/zh.json
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@qzsy/dsh-image-viewer",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "iPhone Photos-style full-screen image viewer for the DSH conversation: zoom from thumbnail, pinch/double-tap zoom, momentum pan, swipe between images, swipe down to close.",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "iPhone Photos-style full-screen image viewer for the DSH conversation: zoom from thumbnail, pinch/double-tap zoom, momentum pan, swipe between images, swipe down to close; annotate an image and drop it into the chat box.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "qzsy",
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"scripts": {
|
|
58
58
|
"build": "bun run ../../scripts/build-package.mjs .",
|
|
59
59
|
"typecheck": "bun exec tsc -p tsconfig.json --noEmit",
|
|
60
|
-
"test": "node test/smoke.mjs"
|
|
60
|
+
"test": "node test/smoke.mjs && node test/editor.mjs"
|
|
61
61
|
},
|
|
62
62
|
"dshBuild": {
|
|
63
63
|
"host": "src/index.ts",
|