@qzsy/dsh-image-viewer 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -1
- package/dist/client.js +1213 -9
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +3 -3
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
|
|
@@ -106,6 +166,51 @@ window.__ModuleLoader__.load({
|
|
|
106
166
|
|
|
107
167
|
const reduceMotion = () => media('(prefers-reduced-motion: reduce)');
|
|
108
168
|
|
|
169
|
+
/**
|
|
170
|
+
* Top strip reserved by the host shell: on the Desktop app the Windows
|
|
171
|
+
* caption buttons / macOS traffic lights are drawn above the page, so the
|
|
172
|
+
* shell publishes `--dsh-frame-overlay-top` (clearance + 20px) for dialogs
|
|
173
|
+
* and menus. A plain browser publishes nothing, so this returns 0.
|
|
174
|
+
*/
|
|
175
|
+
function frameTop() {
|
|
176
|
+
try {
|
|
177
|
+
const html = document.documentElement;
|
|
178
|
+
// Unregistered custom properties keep `calc(a + b)`; resolve the px terms.
|
|
179
|
+
const raw = String(getComputedStyle(html).getPropertyValue('--dsh-frame-overlay-top') || '').trim();
|
|
180
|
+
const resolved = calcPx(raw);
|
|
181
|
+
if (resolved > 0) return resolved;
|
|
182
|
+
// Fallback: the caption height the Desktop shell writes inline.
|
|
183
|
+
const caption = parseFloat(html.style.getPropertyValue('--dsh-windows-titlebar-height') || '');
|
|
184
|
+
if (Number.isFinite(caption) && caption > 0) return caption + 20;
|
|
185
|
+
if (html.getAttribute('data-platform') === 'darwin') return 68;
|
|
186
|
+
return 0;
|
|
187
|
+
} catch {
|
|
188
|
+
return 0;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** `48px`, `calc(40px + 20px)` or `calc(48px + 20px)` -> number of px. */
|
|
193
|
+
function calcPx(text) {
|
|
194
|
+
if (text === '') return 0;
|
|
195
|
+
if (/^-?\d*\.?\d+(px)?$/.test(text)) return parseFloat(text);
|
|
196
|
+
const body = text.replace(/^calc\(/, '').replace(/\)$/, '');
|
|
197
|
+
const terms = body.match(/[+-]?\s*\d*\.?\d+px/g);
|
|
198
|
+
if (terms === null) return 0;
|
|
199
|
+
let total = 0;
|
|
200
|
+
for (const term of terms) {
|
|
201
|
+
const value = parseFloat(term.replace(/\s/g, ''));
|
|
202
|
+
if (!Number.isFinite(value)) return 0;
|
|
203
|
+
total += value;
|
|
204
|
+
}
|
|
205
|
+
return total;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Keep the overlay's own top bar below the shell's window chrome. */
|
|
209
|
+
function chromeInset(root) {
|
|
210
|
+
const top = frameTop();
|
|
211
|
+
root.style.setProperty('--qiv-top-inset', (top > 0 ? top : 10) + 'px');
|
|
212
|
+
}
|
|
213
|
+
|
|
109
214
|
function format(text, params) {
|
|
110
215
|
return String(text).replace(/\{(\w+)\}/g, (match, key) =>
|
|
111
216
|
params !== undefined && params !== null && key in params ? String(params[key]) : match,
|
|
@@ -273,6 +378,18 @@ window.__ModuleLoader__.load({
|
|
|
273
378
|
zoomIn: svg('<circle cx="11" cy="11" r="6.5"/><path d="M20 20l-4.2-4.2M8.5 11h5M11 8.5v5"/>'),
|
|
274
379
|
zoomOut: svg('<circle cx="11" cy="11" r="6.5"/><path d="M20 20l-4.2-4.2M8.5 11h5"/>'),
|
|
275
380
|
download: svg('<path d="M12 4v11M7 10.5l5 5 5-5M5 20h14"/>'),
|
|
381
|
+
edit: svg('<path d="M4 20h4L19 9l-4-4L4 16v4z"/><path d="M13.5 6.5l4 4"/>'),
|
|
382
|
+
back: svg('<path d="M15 5l-7 7 7 7"/>'),
|
|
383
|
+
pen: svg('<path d="M3 17c3-1 4-6 7-6s1 6 4 6 4-8 7-9"/>'),
|
|
384
|
+
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"/>'),
|
|
385
|
+
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"/>'),
|
|
386
|
+
undo: svg('<path d="M9 7H5V3"/><path d="M5.5 7a8 8 0 11-1.3 7"/>'),
|
|
387
|
+
redo: svg('<path d="M15 7h4V3"/><path d="M18.5 7a8 8 0 101.3 7"/>'),
|
|
388
|
+
check: svg('<path d="M5 12.5l4.5 4.5L19 7.5"/>'),
|
|
389
|
+
rect: svg('<rect x="4" y="6" width="16" height="12" rx="1.5"/>'),
|
|
390
|
+
ellipse: svg('<ellipse cx="12" cy="12" rx="8.5" ry="6"/>'),
|
|
391
|
+
arrow: svg('<path d="M5 19L19 5M10 5h9v9"/>'),
|
|
392
|
+
line: svg('<path d="M5 19L19 5"/>'),
|
|
276
393
|
};
|
|
277
394
|
//#endregion
|
|
278
395
|
|
|
@@ -294,7 +411,7 @@ window.__ModuleLoader__.load({
|
|
|
294
411
|
@keyframes qiv-rot{to{transform:rotate(360deg)}}
|
|
295
412
|
.qiv-chrome{transition:opacity .22s ease}
|
|
296
413
|
[${ROOT_ATTR}][data-chrome=hidden] .qiv-chrome,[${ROOT_ATTR}][data-state=closing] .qiv-chrome,[${ROOT_ATTR}][data-state=opening] .qiv-chrome,[${ROOT_ATTR}][data-gesture=dismiss] .qiv-chrome,[${ROOT_ATTR}][data-gesture=pinch] .qiv-chrome{opacity:0;pointer-events:none}
|
|
297
|
-
.qiv-top{position:absolute;left:0;right:0;top:0;display:flex;align-items:center;justify-content:space-between;gap:12px;padding:calc(env(safe-area-inset-top,0px) + 10px) calc(env(safe-area-inset-right,0px) + 12px) 18px calc(env(safe-area-inset-left,0px) + 18px);background:linear-gradient(rgba(0,0,0,.5),rgba(0,0,0,0))}
|
|
414
|
+
.qiv-top{position:absolute;left:0;right:0;top:0;display:flex;align-items:center;justify-content:space-between;gap:12px;padding:calc(env(safe-area-inset-top,0px) + var(--qiv-top-inset,10px)) calc(env(safe-area-inset-right,0px) + 12px) 18px calc(env(safe-area-inset-left,0px) + 18px);background:linear-gradient(rgba(0,0,0,.5),rgba(0,0,0,0))}
|
|
298
415
|
.qiv-count{font-size:14px;font-weight:600;letter-spacing:.02em;font-variant-numeric:tabular-nums;text-shadow:0 1px 2px rgba(0,0,0,.5)}
|
|
299
416
|
.qiv-actions{display:flex;gap:8px;margin-left:auto}
|
|
300
417
|
.qiv-btn{appearance:none;width:40px;height:40px;padding:0;border:0;border-radius:999px;background:rgba(255,255,255,.14);color:inherit;display:grid;place-items:center;cursor:pointer;text-decoration:none;-webkit-backdrop-filter:blur(14px);backdrop-filter:blur(14px);transition:background-color .15s,opacity .15s}
|
|
@@ -322,7 +439,7 @@ window.__ModuleLoader__.load({
|
|
|
322
439
|
if (document.querySelector(`style[${STYLE_ATTR}]`) !== null) return () => {};
|
|
323
440
|
const tag = document.createElement('style');
|
|
324
441
|
tag.setAttribute(STYLE_ATTR, '');
|
|
325
|
-
tag.textContent = CSS;
|
|
442
|
+
tag.textContent = CSS + EDIT_CSS;
|
|
326
443
|
document.head.appendChild(tag);
|
|
327
444
|
return () => tag.remove();
|
|
328
445
|
}
|
|
@@ -348,7 +465,7 @@ window.__ModuleLoader__.load({
|
|
|
348
465
|
*/
|
|
349
466
|
function resolveTrigger(node) {
|
|
350
467
|
if (!(node instanceof Element)) return null;
|
|
351
|
-
if (node.closest(`[${ROOT_ATTR}],[${IGNORE_ATTR}],[aria-modal="true"],[contenteditable="true"]`) !== null) return null;
|
|
468
|
+
if (node.closest(`[${ROOT_ATTR}],[${EDIT_ATTR}],[${IGNORE_ATTR}],[aria-modal="true"],[contenteditable="true"]`) !== null) return null;
|
|
352
469
|
const host = node.closest('button,[role="button"]');
|
|
353
470
|
if (host !== null) {
|
|
354
471
|
if (host.closest('a[href]') !== null) return null;
|
|
@@ -432,6 +549,8 @@ window.__ModuleLoader__.load({
|
|
|
432
549
|
function openViewer(items, startIndex, t, onDestroyed) {
|
|
433
550
|
const n = items.length;
|
|
434
551
|
const coarse = media('(pointer: coarse)');
|
|
552
|
+
/** Window-chrome strip the shell paints above the page (Desktop app only). */
|
|
553
|
+
let chromeTop = frameTop();
|
|
435
554
|
const restoreFocus = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
|
436
555
|
const s = { x: 0, y: 0, scale: 1, clip: 0, radius: 0, bg: 0, alpha: 1, trackX: 0 };
|
|
437
556
|
const slides = new Map();
|
|
@@ -442,6 +561,8 @@ window.__ModuleLoader__.load({
|
|
|
442
561
|
let outgoing = null;
|
|
443
562
|
let closing = false;
|
|
444
563
|
let destroyed = false;
|
|
564
|
+
/** Open annotation editor; the viewer pauses keyboard input under it. */
|
|
565
|
+
let editor = null;
|
|
445
566
|
let gesture = null;
|
|
446
567
|
let samples = [];
|
|
447
568
|
let tapTimer = 0;
|
|
@@ -455,6 +576,7 @@ window.__ModuleLoader__.load({
|
|
|
455
576
|
//#region dom
|
|
456
577
|
const root = h('div', 'qiv', { role: 'dialog', 'aria-modal': 'true', 'aria-label': t('dialog'), tabindex: '-1' });
|
|
457
578
|
root.setAttribute(ROOT_ATTR, '');
|
|
579
|
+
chromeInset(root);
|
|
458
580
|
root.dataset.state = 'opening';
|
|
459
581
|
if (coarse) root.setAttribute('data-coarse', '');
|
|
460
582
|
if (n <= 1) root.setAttribute('data-single', '');
|
|
@@ -472,7 +594,9 @@ window.__ModuleLoader__.load({
|
|
|
472
594
|
const download = h('a', 'qiv-btn', { 'aria-label': t('download'), title: t('download'), rel: 'noopener', target: '_blank' });
|
|
473
595
|
download.innerHTML = ICON.download;
|
|
474
596
|
const closeBtn = iconButton('qiv-btn', t('close'), ICON.close);
|
|
475
|
-
|
|
597
|
+
const editBtn = canEdit() ? iconButton('qiv-btn', t('edit'), ICON.edit) : null;
|
|
598
|
+
if (editBtn !== null) actions.append(zoomBtn, editBtn, download, closeBtn);
|
|
599
|
+
else actions.append(zoomBtn, download, closeBtn);
|
|
476
600
|
top.append(counter, actions);
|
|
477
601
|
const prevBtn = iconButton('qiv-btn qiv-nav qiv-prev qiv-chrome', t('prev'), ICON.prev);
|
|
478
602
|
const nextBtn = iconButton('qiv-btn qiv-nav qiv-next qiv-chrome', t('next'), ICON.next);
|
|
@@ -501,8 +625,11 @@ window.__ModuleLoader__.load({
|
|
|
501
625
|
|
|
502
626
|
//#region geometry
|
|
503
627
|
function pads() {
|
|
504
|
-
|
|
505
|
-
|
|
628
|
+
// The image never exceeds 90% of the viewport width, and never slides
|
|
629
|
+
// under the shell's window chrome or our own top bar.
|
|
630
|
+
const x = Math.max(coarse ? 8 : 24, vw * 0.05);
|
|
631
|
+
const y = Math.max(coarse ? 12 : n > 1 ? 84 : 64, chromeTop + 28);
|
|
632
|
+
return { x, y };
|
|
506
633
|
}
|
|
507
634
|
|
|
508
635
|
/** Displayed size at scale 1, and its ratio to natural pixels. */
|
|
@@ -1180,7 +1307,7 @@ window.__ModuleLoader__.load({
|
|
|
1180
1307
|
}
|
|
1181
1308
|
|
|
1182
1309
|
function onKey(e) {
|
|
1183
|
-
if (closing || destroyed) return;
|
|
1310
|
+
if (closing || destroyed || editor !== null) return;
|
|
1184
1311
|
if (e.ctrlKey || e.metaKey || e.altKey) return;
|
|
1185
1312
|
let handled = true;
|
|
1186
1313
|
switch (e.key) {
|
|
@@ -1218,6 +1345,9 @@ window.__ModuleLoader__.load({
|
|
|
1218
1345
|
function onResize() {
|
|
1219
1346
|
vw = root.clientWidth || window.innerWidth;
|
|
1220
1347
|
vh = root.clientHeight || window.innerHeight;
|
|
1348
|
+
// Native fullscreen drops the shell's chrome strip, so re-read it.
|
|
1349
|
+
chromeTop = frameTop();
|
|
1350
|
+
chromeInset(root);
|
|
1221
1351
|
anim.stop(['x', 'y', 'scale', 'trackX']);
|
|
1222
1352
|
s.trackX = 0;
|
|
1223
1353
|
clearOutgoing();
|
|
@@ -1259,16 +1389,20 @@ window.__ModuleLoader__.load({
|
|
|
1259
1389
|
}
|
|
1260
1390
|
}
|
|
1261
1391
|
|
|
1262
|
-
function finish() {
|
|
1392
|
+
function finish(restore = true) {
|
|
1263
1393
|
if (destroyed) return;
|
|
1264
1394
|
destroyed = true;
|
|
1395
|
+
if (editor !== null) {
|
|
1396
|
+
editor.destroy();
|
|
1397
|
+
editor = null;
|
|
1398
|
+
}
|
|
1265
1399
|
anim.destroy();
|
|
1266
1400
|
clearTap();
|
|
1267
1401
|
showSource();
|
|
1268
1402
|
window.removeEventListener('keydown', onKey, true);
|
|
1269
1403
|
window.removeEventListener('resize', onResize);
|
|
1270
1404
|
root.remove();
|
|
1271
|
-
if (restoreFocus !== null && restoreFocus.isConnected) {
|
|
1405
|
+
if (restore && restoreFocus !== null && restoreFocus.isConnected) {
|
|
1272
1406
|
try {
|
|
1273
1407
|
restoreFocus.focus({ preventScroll: true });
|
|
1274
1408
|
} catch {
|
|
@@ -1292,6 +1426,26 @@ window.__ModuleLoader__.load({
|
|
|
1292
1426
|
if (s.scale > 1.01) anim.to({ scale: 1, x: 0, y: 0 }, { spring: SPRING.snap });
|
|
1293
1427
|
else zoomBy(doubleTapScale(), vw / 2, vh / 2, true);
|
|
1294
1428
|
});
|
|
1429
|
+
function openEdit() {
|
|
1430
|
+
if (closing || destroyed || editor !== null) return;
|
|
1431
|
+
clearTap();
|
|
1432
|
+
editor = openEditor(items[index], t, {
|
|
1433
|
+
cancel: () => {
|
|
1434
|
+
editor = null;
|
|
1435
|
+
try {
|
|
1436
|
+
editBtn.focus({ preventScroll: true });
|
|
1437
|
+
} catch {
|
|
1438
|
+
// focus is best-effort
|
|
1439
|
+
}
|
|
1440
|
+
},
|
|
1441
|
+
done: (injected) => {
|
|
1442
|
+
editor = null;
|
|
1443
|
+
// Injected: focus stays in the composer. Downloaded: return to the thumbnail.
|
|
1444
|
+
finish(!injected);
|
|
1445
|
+
},
|
|
1446
|
+
});
|
|
1447
|
+
}
|
|
1448
|
+
if (editBtn !== null) editBtn.addEventListener('click', openEdit);
|
|
1295
1449
|
closeBtn.addEventListener('click', () => close());
|
|
1296
1450
|
prevBtn.addEventListener('click', () => go(-1));
|
|
1297
1451
|
nextBtn.addEventListener('click', () => go(1));
|
|
@@ -1332,6 +1486,1056 @@ window.__ModuleLoader__.load({
|
|
|
1332
1486
|
}
|
|
1333
1487
|
//#endregion
|
|
1334
1488
|
|
|
1489
|
+
//#region composer injection
|
|
1490
|
+
/** Canvas export is required for the annotation editor. */
|
|
1491
|
+
function canEdit() {
|
|
1492
|
+
try {
|
|
1493
|
+
const canvas = document.createElement('canvas');
|
|
1494
|
+
return typeof canvas.getContext === 'function' && typeof canvas.toBlob === 'function' && canvas.getContext('2d') !== null;
|
|
1495
|
+
} catch {
|
|
1496
|
+
return false;
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
function visible(el) {
|
|
1501
|
+
const rect = el.getBoundingClientRect();
|
|
1502
|
+
return rect.width > 0 && rect.height > 0;
|
|
1503
|
+
}
|
|
1504
|
+
|
|
1505
|
+
/** The chat composer is a Lexical root marked `data-composer-input`. */
|
|
1506
|
+
function findComposer() {
|
|
1507
|
+
const all = [...document.querySelectorAll('[data-composer-input]')].filter(
|
|
1508
|
+
(el) => el.isConnected && el.closest(`[${ROOT_ATTR}],[${EDIT_ATTR}]`) === null,
|
|
1509
|
+
);
|
|
1510
|
+
const editable = all.filter((el) => el.getAttribute('contenteditable') === 'true');
|
|
1511
|
+
const pool = editable.length > 0 ? editable : all;
|
|
1512
|
+
return pool.find(visible) || pool[0] || null;
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1515
|
+
/** A real DataTransfer when the browser allows it, else a structural stand-in. */
|
|
1516
|
+
function transferOf(file) {
|
|
1517
|
+
try {
|
|
1518
|
+
const dt = new DataTransfer();
|
|
1519
|
+
dt.items.add(file);
|
|
1520
|
+
if (dt.files.length === 1) return dt;
|
|
1521
|
+
} catch {
|
|
1522
|
+
// fall through to the stand-in
|
|
1523
|
+
}
|
|
1524
|
+
const item = { kind: 'file', type: file.type, getAsFile: () => file, webkitGetAsEntry: () => null };
|
|
1525
|
+
return {
|
|
1526
|
+
files: [file],
|
|
1527
|
+
items: [item],
|
|
1528
|
+
types: ['Files'],
|
|
1529
|
+
dropEffect: 'copy',
|
|
1530
|
+
effectAllowed: 'all',
|
|
1531
|
+
getData: () => '',
|
|
1532
|
+
setData() {},
|
|
1533
|
+
clearData() {},
|
|
1534
|
+
};
|
|
1535
|
+
}
|
|
1536
|
+
|
|
1537
|
+
function synthetic(type, field, transfer, Ctor) {
|
|
1538
|
+
let ev = null;
|
|
1539
|
+
try {
|
|
1540
|
+
if (typeof Ctor === 'function') ev = new Ctor(type, { bubbles: true, cancelable: true, [field]: transfer });
|
|
1541
|
+
} catch {
|
|
1542
|
+
ev = null;
|
|
1543
|
+
}
|
|
1544
|
+
if (ev === null) ev = new Event(type, { bubbles: true, cancelable: true });
|
|
1545
|
+
if (ev[field] !== transfer) {
|
|
1546
|
+
try {
|
|
1547
|
+
Object.defineProperty(ev, field, { value: transfer, configurable: true });
|
|
1548
|
+
} catch {
|
|
1549
|
+
// keep whatever the constructor produced
|
|
1550
|
+
}
|
|
1551
|
+
}
|
|
1552
|
+
return ev;
|
|
1553
|
+
}
|
|
1554
|
+
|
|
1555
|
+
/**
|
|
1556
|
+
* Hand one image to the composer the way a paste would. The composer's own
|
|
1557
|
+
* paste command calls preventDefault after it accepts files; the attachment
|
|
1558
|
+
* view's document drop listener is the fallback.
|
|
1559
|
+
* @returns 'composer' | 'none'
|
|
1560
|
+
*/
|
|
1561
|
+
function injectFile(file) {
|
|
1562
|
+
const composer = findComposer();
|
|
1563
|
+
if (composer !== null && composer.getAttribute('contenteditable') === 'true') {
|
|
1564
|
+
try {
|
|
1565
|
+
composer.focus({ preventScroll: false });
|
|
1566
|
+
} catch {
|
|
1567
|
+
// focus is best-effort
|
|
1568
|
+
}
|
|
1569
|
+
const paste = synthetic('paste', 'clipboardData', transferOf(file), window.ClipboardEvent);
|
|
1570
|
+
composer.dispatchEvent(paste);
|
|
1571
|
+
if (paste.defaultPrevented) return 'composer';
|
|
1572
|
+
}
|
|
1573
|
+
if (composer !== null) {
|
|
1574
|
+
const drop = synthetic('drop', 'dataTransfer', transferOf(file), window.DragEvent);
|
|
1575
|
+
document.body.dispatchEvent(drop);
|
|
1576
|
+
if (drop.defaultPrevented) {
|
|
1577
|
+
try {
|
|
1578
|
+
composer.focus({ preventScroll: false });
|
|
1579
|
+
} catch {
|
|
1580
|
+
// focus is best-effort
|
|
1581
|
+
}
|
|
1582
|
+
return 'composer';
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
return 'none';
|
|
1586
|
+
}
|
|
1587
|
+
|
|
1588
|
+
function toast(text) {
|
|
1589
|
+
const node = h('div', 'qiv-toast', { role: 'status', [IGNORE_ATTR]: '' });
|
|
1590
|
+
node.textContent = text;
|
|
1591
|
+
document.body.appendChild(node);
|
|
1592
|
+
requestAnimationFrame(() => node.setAttribute('data-show', ''));
|
|
1593
|
+
setTimeout(() => {
|
|
1594
|
+
node.removeAttribute('data-show');
|
|
1595
|
+
setTimeout(() => node.remove(), 300);
|
|
1596
|
+
}, 2600);
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
function downloadFile(file) {
|
|
1600
|
+
const url = URL.createObjectURL(file);
|
|
1601
|
+
const a = h('a', '', { href: url, download: file.name, [IGNORE_ATTR]: '' });
|
|
1602
|
+
a.style.display = 'none';
|
|
1603
|
+
document.body.appendChild(a);
|
|
1604
|
+
a.click();
|
|
1605
|
+
a.remove();
|
|
1606
|
+
setTimeout(() => URL.revokeObjectURL(url), 30000);
|
|
1607
|
+
}
|
|
1608
|
+
//#endregion
|
|
1609
|
+
|
|
1610
|
+
//#region annotation editor
|
|
1611
|
+
const EDIT_ATTR = 'data-qiv-edit';
|
|
1612
|
+
const COLORS = [
|
|
1613
|
+
['#ffffff', 'white'],
|
|
1614
|
+
['#000000', 'black'],
|
|
1615
|
+
['#ff3b30', 'red'],
|
|
1616
|
+
['#ff9500', 'orange'],
|
|
1617
|
+
['#ffcc00', 'yellow'],
|
|
1618
|
+
['#34c759', 'green'],
|
|
1619
|
+
['#007aff', 'blue'],
|
|
1620
|
+
['#af52de', 'purple'],
|
|
1621
|
+
];
|
|
1622
|
+
const SIZE_MIN = 2;
|
|
1623
|
+
const SIZE_MAX = 36;
|
|
1624
|
+
/** Working canvas limits; keeps mobile Safari under its canvas memory cap. */
|
|
1625
|
+
const EDIT_MAX_EDGE = 4096;
|
|
1626
|
+
const EDIT_MAX_PIXELS = 12e6;
|
|
1627
|
+
const EXPORT_MAX_BYTES = 8 * 1024 * 1024;
|
|
1628
|
+
|
|
1629
|
+
function needsCors(src) {
|
|
1630
|
+
try {
|
|
1631
|
+
const url = new URL(src, location.href);
|
|
1632
|
+
if (url.protocol === 'data:' || url.protocol === 'blob:') return false;
|
|
1633
|
+
return url.protocol !== location.protocol || url.host !== location.host;
|
|
1634
|
+
} catch {
|
|
1635
|
+
return false;
|
|
1636
|
+
}
|
|
1637
|
+
}
|
|
1638
|
+
|
|
1639
|
+
function loadImage(src, cors) {
|
|
1640
|
+
return new Promise((resolve, reject) => {
|
|
1641
|
+
const img = new Image();
|
|
1642
|
+
if (cors) img.crossOrigin = 'anonymous';
|
|
1643
|
+
img.decoding = 'async';
|
|
1644
|
+
img.onload = () => resolve(img);
|
|
1645
|
+
img.onerror = () => reject(new Error('load failed'));
|
|
1646
|
+
img.src = src;
|
|
1647
|
+
});
|
|
1648
|
+
}
|
|
1649
|
+
|
|
1650
|
+
/** Prefer a CORS load so export works; fall back to a tainted copy so drawing still does. */
|
|
1651
|
+
async function loadEditable(src) {
|
|
1652
|
+
if (!needsCors(src)) return { img: await loadImage(src, false), tainted: false };
|
|
1653
|
+
try {
|
|
1654
|
+
return { img: await loadImage(src, true), tainted: false };
|
|
1655
|
+
} catch {
|
|
1656
|
+
return { img: await loadImage(src, false), tainted: true };
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
|
|
1660
|
+
const dist = (a, b) => Math.hypot(a.x - b.x, a.y - b.y);
|
|
1661
|
+
function segDist(p, a, b) {
|
|
1662
|
+
const dx = b.x - a.x;
|
|
1663
|
+
const dy = b.y - a.y;
|
|
1664
|
+
const len = dx * dx + dy * dy;
|
|
1665
|
+
const k = len === 0 ? 0 : clamp(((p.x - a.x) * dx + (p.y - a.y) * dy) / len, 0, 1);
|
|
1666
|
+
return Math.hypot(p.x - (a.x + k * dx), p.y - (a.y + k * dy));
|
|
1667
|
+
}
|
|
1668
|
+
|
|
1669
|
+
function arrowHead(a, b, w) {
|
|
1670
|
+
const len = dist(a, b);
|
|
1671
|
+
const size = Math.min(Math.max(w * 3.2, 14), len * 0.45);
|
|
1672
|
+
const ang = Math.atan2(b.y - a.y, b.x - a.x);
|
|
1673
|
+
const spread = Math.PI / 7;
|
|
1674
|
+
return [
|
|
1675
|
+
{ x: b.x - size * Math.cos(ang - spread), y: b.y - size * Math.sin(ang - spread) },
|
|
1676
|
+
{ x: b.x - size * Math.cos(ang + spread), y: b.y - size * Math.sin(ang + spread) },
|
|
1677
|
+
];
|
|
1678
|
+
}
|
|
1679
|
+
|
|
1680
|
+
/** Polylines used for hit-testing every stroke kind with one segment test. */
|
|
1681
|
+
function outlines(st) {
|
|
1682
|
+
if (st.kind === 'pen') return [st.points];
|
|
1683
|
+
const [a, b] = [st.a, st.b];
|
|
1684
|
+
if (st.kind === 'line') return [[a, b]];
|
|
1685
|
+
if (st.kind === 'arrow') {
|
|
1686
|
+
const [l, r] = arrowHead(a, b, st.w);
|
|
1687
|
+
return [[a, b], [l, b, r]];
|
|
1688
|
+
}
|
|
1689
|
+
if (st.kind === 'rect') {
|
|
1690
|
+
return [[a, { x: b.x, y: a.y }, b, { x: a.x, y: b.y }, a]];
|
|
1691
|
+
}
|
|
1692
|
+
const cx = (a.x + b.x) / 2;
|
|
1693
|
+
const cy = (a.y + b.y) / 2;
|
|
1694
|
+
const rx = Math.abs(b.x - a.x) / 2;
|
|
1695
|
+
const ry = Math.abs(b.y - a.y) / 2;
|
|
1696
|
+
const pts = [];
|
|
1697
|
+
for (let i = 0; i <= 48; i++) {
|
|
1698
|
+
const t = (i / 48) * Math.PI * 2;
|
|
1699
|
+
pts.push({ x: cx + rx * Math.cos(t), y: cy + ry * Math.sin(t) });
|
|
1700
|
+
}
|
|
1701
|
+
return [pts];
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
function hitStroke(st, p, tol) {
|
|
1705
|
+
const reach = tol + st.w / 2;
|
|
1706
|
+
for (const line of outlines(st)) {
|
|
1707
|
+
if (line.length === 1 && dist(line[0], p) <= reach) return true;
|
|
1708
|
+
for (let i = 1; i < line.length; i++) if (segDist(p, line[i - 1], line[i]) <= reach) return true;
|
|
1709
|
+
}
|
|
1710
|
+
return false;
|
|
1711
|
+
}
|
|
1712
|
+
|
|
1713
|
+
function drawStroke(ctx, st) {
|
|
1714
|
+
ctx.save();
|
|
1715
|
+
ctx.strokeStyle = st.color;
|
|
1716
|
+
ctx.fillStyle = st.color;
|
|
1717
|
+
ctx.lineWidth = st.w;
|
|
1718
|
+
ctx.lineCap = 'round';
|
|
1719
|
+
ctx.lineJoin = 'round';
|
|
1720
|
+
ctx.beginPath();
|
|
1721
|
+
if (st.kind === 'pen') {
|
|
1722
|
+
const pts = st.points;
|
|
1723
|
+
if (pts.length === 1) {
|
|
1724
|
+
ctx.arc(pts[0].x, pts[0].y, st.w / 2, 0, Math.PI * 2);
|
|
1725
|
+
ctx.fill();
|
|
1726
|
+
ctx.restore();
|
|
1727
|
+
return;
|
|
1728
|
+
}
|
|
1729
|
+
ctx.moveTo(pts[0].x, pts[0].y);
|
|
1730
|
+
for (let i = 1; i < pts.length - 1; i++) {
|
|
1731
|
+
const mx = (pts[i].x + pts[i + 1].x) / 2;
|
|
1732
|
+
const my = (pts[i].y + pts[i + 1].y) / 2;
|
|
1733
|
+
ctx.quadraticCurveTo(pts[i].x, pts[i].y, mx, my);
|
|
1734
|
+
}
|
|
1735
|
+
const last = pts[pts.length - 1];
|
|
1736
|
+
ctx.lineTo(last.x, last.y);
|
|
1737
|
+
} else if (st.kind === 'line' || st.kind === 'arrow') {
|
|
1738
|
+
ctx.moveTo(st.a.x, st.a.y);
|
|
1739
|
+
ctx.lineTo(st.b.x, st.b.y);
|
|
1740
|
+
if (st.kind === 'arrow') {
|
|
1741
|
+
const [l, r] = arrowHead(st.a, st.b, st.w);
|
|
1742
|
+
ctx.moveTo(l.x, l.y);
|
|
1743
|
+
ctx.lineTo(st.b.x, st.b.y);
|
|
1744
|
+
ctx.lineTo(r.x, r.y);
|
|
1745
|
+
}
|
|
1746
|
+
} else if (st.kind === 'rect') {
|
|
1747
|
+
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));
|
|
1748
|
+
} else {
|
|
1749
|
+
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);
|
|
1750
|
+
}
|
|
1751
|
+
ctx.stroke();
|
|
1752
|
+
ctx.restore();
|
|
1753
|
+
}
|
|
1754
|
+
|
|
1755
|
+
function stemOf(src) {
|
|
1756
|
+
const base = fileName(src).replace(/\.[a-z0-9]+$/i, '');
|
|
1757
|
+
const safe = base.replace(/[^\w\u4e00-\u9fff-]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 60);
|
|
1758
|
+
return safe || 'image';
|
|
1759
|
+
}
|
|
1760
|
+
|
|
1761
|
+
/**
|
|
1762
|
+
* Full-screen markup editor (pen / shapes / eraser, colours, width,
|
|
1763
|
+
* undo/redo, pinch zoom). Done exports PNG and pastes it into the composer.
|
|
1764
|
+
* @param item - viewer item ({ src, alt }).
|
|
1765
|
+
* @param hooks - { cancel(), done() }.
|
|
1766
|
+
*/
|
|
1767
|
+
function openEditor(item, t, hooks) {
|
|
1768
|
+
const root = h('div', 'qie', { [EDIT_ATTR]: '', [IGNORE_ATTR]: '', role: 'dialog', 'aria-modal': 'true', 'aria-label': t('edit.dialog'), tabindex: '-1' });
|
|
1769
|
+
chromeInset(root);
|
|
1770
|
+
const btn = (className, label, icon, extra) => {
|
|
1771
|
+
const b = h('button', className, { type: 'button', 'aria-label': label, title: label, ...(extra || {}) });
|
|
1772
|
+
b.innerHTML = icon;
|
|
1773
|
+
return b;
|
|
1774
|
+
};
|
|
1775
|
+
|
|
1776
|
+
//#region dom
|
|
1777
|
+
const top = h('div', 'qie-top');
|
|
1778
|
+
const backBtn = btn('qie-round', t('edit.cancel'), ICON.back);
|
|
1779
|
+
const tools = h('div', 'qie-tools', { role: 'toolbar', 'aria-label': t('edit.tools') });
|
|
1780
|
+
const penBtn = btn('qie-tool', t('edit.pen'), ICON.pen, { 'aria-pressed': 'true' });
|
|
1781
|
+
const shapeBtn = btn('qie-tool', t('edit.shapes'), ICON.shapes, { 'aria-pressed': 'false', 'aria-haspopup': 'true', 'aria-expanded': 'false' });
|
|
1782
|
+
const eraseBtn = btn('qie-tool', t('edit.eraser'), ICON.eraser, { 'aria-pressed': 'false' });
|
|
1783
|
+
tools.append(penBtn, shapeBtn, eraseBtn);
|
|
1784
|
+
const history = h('div', 'qie-history');
|
|
1785
|
+
const undoBtn = btn('qie-round', t('edit.undo'), ICON.undo);
|
|
1786
|
+
const redoBtn = btn('qie-round', t('edit.redo'), ICON.redo);
|
|
1787
|
+
history.append(undoBtn, redoBtn);
|
|
1788
|
+
const pop = h('div', 'qie-pop', { role: 'group', 'aria-label': t('edit.shapes') });
|
|
1789
|
+
pop.hidden = true;
|
|
1790
|
+
const shapeButtons = {};
|
|
1791
|
+
for (const kind of ['arrow', 'rect', 'ellipse', 'line']) {
|
|
1792
|
+
const b = btn('qie-shape', t('edit.shape.' + kind), ICON[kind], { 'data-kind': kind, 'aria-pressed': 'false' });
|
|
1793
|
+
shapeButtons[kind] = b;
|
|
1794
|
+
pop.appendChild(b);
|
|
1795
|
+
}
|
|
1796
|
+
top.append(backBtn, tools, history, pop);
|
|
1797
|
+
|
|
1798
|
+
const area = h('div', 'qie-area');
|
|
1799
|
+
const stage = h('div', 'qie-stage');
|
|
1800
|
+
let base = h('img', 'qie-base', { alt: item.alt || '', draggable: 'false' });
|
|
1801
|
+
const ink = h('canvas', 'qie-ink');
|
|
1802
|
+
const live = h('canvas', 'qie-live');
|
|
1803
|
+
stage.append(base, ink, live);
|
|
1804
|
+
const status = h('div', 'qie-status');
|
|
1805
|
+
status.innerHTML = '<div class="qiv-spin"></div>';
|
|
1806
|
+
const note = h('div', 'qie-note');
|
|
1807
|
+
note.hidden = true;
|
|
1808
|
+
note.textContent = t('edit.taintedHint');
|
|
1809
|
+
area.append(stage, status, note);
|
|
1810
|
+
|
|
1811
|
+
const slider = h('div', 'qie-size', {
|
|
1812
|
+
role: 'slider',
|
|
1813
|
+
tabindex: '0',
|
|
1814
|
+
'aria-label': t('edit.size'),
|
|
1815
|
+
'aria-orientation': 'vertical',
|
|
1816
|
+
'aria-valuemin': String(SIZE_MIN),
|
|
1817
|
+
'aria-valuemax': String(SIZE_MAX),
|
|
1818
|
+
});
|
|
1819
|
+
const rail = h('div', 'qie-rail');
|
|
1820
|
+
const knob = h('div', 'qie-knob');
|
|
1821
|
+
const preview = h('div', 'qie-preview', { 'aria-hidden': 'true' });
|
|
1822
|
+
rail.appendChild(knob);
|
|
1823
|
+
slider.append(rail, preview);
|
|
1824
|
+
|
|
1825
|
+
const bottom = h('div', 'qie-bottom');
|
|
1826
|
+
const palette = h('div', 'qie-palette', { role: 'radiogroup', 'aria-label': t('edit.color') });
|
|
1827
|
+
const customWrap = h('label', 'qie-swatch qie-custom', { title: t('edit.custom') });
|
|
1828
|
+
const customInput = h('input', '', { type: 'color', 'aria-label': t('edit.custom') });
|
|
1829
|
+
customWrap.appendChild(customInput);
|
|
1830
|
+
palette.appendChild(customWrap);
|
|
1831
|
+
const swatches = [];
|
|
1832
|
+
for (const [hex, name] of COLORS) {
|
|
1833
|
+
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 });
|
|
1834
|
+
sw.style.setProperty('--qie-c', hex);
|
|
1835
|
+
swatches.push(sw);
|
|
1836
|
+
palette.appendChild(sw);
|
|
1837
|
+
}
|
|
1838
|
+
const doneBtn = btn('qie-done', t('edit.done'), ICON.check);
|
|
1839
|
+
bottom.append(palette, doneBtn);
|
|
1840
|
+
root.append(top, area, slider, bottom);
|
|
1841
|
+
//#endregion
|
|
1842
|
+
|
|
1843
|
+
//#region state
|
|
1844
|
+
let W = 0;
|
|
1845
|
+
let H = 0;
|
|
1846
|
+
let img = null;
|
|
1847
|
+
let tainted = false;
|
|
1848
|
+
let inkCtx = null;
|
|
1849
|
+
let liveCtx = null;
|
|
1850
|
+
let strokes = [];
|
|
1851
|
+
const past = [];
|
|
1852
|
+
const future = [];
|
|
1853
|
+
let tool = 'pen';
|
|
1854
|
+
let shape = 'arrow';
|
|
1855
|
+
let color = '#ff3b30';
|
|
1856
|
+
let size = 6;
|
|
1857
|
+
let customColor = null;
|
|
1858
|
+
const view = { z: 1, x: 0, y: 0 };
|
|
1859
|
+
let fitW = 0;
|
|
1860
|
+
let fitH = 0;
|
|
1861
|
+
const pts = new Map();
|
|
1862
|
+
let op = null;
|
|
1863
|
+
let pinch = null;
|
|
1864
|
+
let sawPen = false;
|
|
1865
|
+
let busy = false;
|
|
1866
|
+
let ready = false;
|
|
1867
|
+
let destroyed = false;
|
|
1868
|
+
let raf = 0;
|
|
1869
|
+
//#endregion
|
|
1870
|
+
|
|
1871
|
+
//#region layout + view
|
|
1872
|
+
function layout() {
|
|
1873
|
+
const aw = area.clientWidth || window.innerWidth;
|
|
1874
|
+
const ah = area.clientHeight || window.innerHeight * 0.7;
|
|
1875
|
+
if (W === 0 || H === 0) return;
|
|
1876
|
+
// The canvas keeps the same 90% width cap as the viewer.
|
|
1877
|
+
const k = Math.min((aw * 0.9) / W, (ah - 24) / H);
|
|
1878
|
+
fitW = Math.max(1, W * k);
|
|
1879
|
+
fitH = Math.max(1, H * k);
|
|
1880
|
+
stage.style.width = fitW + 'px';
|
|
1881
|
+
stage.style.height = fitH + 'px';
|
|
1882
|
+
applyView();
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
function viewBounds(z) {
|
|
1886
|
+
const aw = area.clientWidth || window.innerWidth;
|
|
1887
|
+
const ah = area.clientHeight || window.innerHeight * 0.7;
|
|
1888
|
+
return { x: Math.max(0, (fitW * z - aw) / 2 + 12), y: Math.max(0, (fitH * z - ah) / 2 + 12) };
|
|
1889
|
+
}
|
|
1890
|
+
|
|
1891
|
+
function applyView() {
|
|
1892
|
+
stage.style.transform = `translate(-50%,-50%) translate(${view.x}px,${view.y}px) scale(${view.z})`;
|
|
1893
|
+
root.toggleAttribute('data-zoomed', view.z > 1.01);
|
|
1894
|
+
}
|
|
1895
|
+
|
|
1896
|
+
function settleView() {
|
|
1897
|
+
view.z = clamp(view.z, 1, 8);
|
|
1898
|
+
if (view.z <= 1.01) {
|
|
1899
|
+
view.z = 1;
|
|
1900
|
+
view.x = 0;
|
|
1901
|
+
view.y = 0;
|
|
1902
|
+
} else {
|
|
1903
|
+
const b = viewBounds(view.z);
|
|
1904
|
+
view.x = clamp(view.x, -b.x, b.x);
|
|
1905
|
+
view.y = clamp(view.y, -b.y, b.y);
|
|
1906
|
+
}
|
|
1907
|
+
root.setAttribute('data-settle', '');
|
|
1908
|
+
applyView();
|
|
1909
|
+
setTimeout(() => root.removeAttribute('data-settle'), 260);
|
|
1910
|
+
}
|
|
1911
|
+
|
|
1912
|
+
/** Zoom so the content point under (cx, cy) stays put. */
|
|
1913
|
+
function zoomAround(z, cx, cy) {
|
|
1914
|
+
const r = area.getBoundingClientRect();
|
|
1915
|
+
const ox = cx - (r.left + r.width / 2);
|
|
1916
|
+
const oy = cy - (r.top + r.height / 2);
|
|
1917
|
+
const k = z / view.z;
|
|
1918
|
+
view.x = ox - (ox - view.x) * k;
|
|
1919
|
+
view.y = oy - (oy - view.y) * k;
|
|
1920
|
+
view.z = z;
|
|
1921
|
+
applyView();
|
|
1922
|
+
}
|
|
1923
|
+
|
|
1924
|
+
/** Client point -> working-canvas pixel. */
|
|
1925
|
+
function toImage(e) {
|
|
1926
|
+
const r = stage.getBoundingClientRect();
|
|
1927
|
+
const rw = r.width || fitW * view.z || 1;
|
|
1928
|
+
const rh = r.height || fitH * view.z || 1;
|
|
1929
|
+
return { x: ((e.clientX - r.left) / rw) * W, y: ((e.clientY - r.top) / rh) * H };
|
|
1930
|
+
}
|
|
1931
|
+
|
|
1932
|
+
/** Screen px -> image px at the current zoom. */
|
|
1933
|
+
function imagePx(px) {
|
|
1934
|
+
return (px * W) / Math.max(1, fitW * view.z);
|
|
1935
|
+
}
|
|
1936
|
+
//#endregion
|
|
1937
|
+
|
|
1938
|
+
//#region drawing
|
|
1939
|
+
function redraw() {
|
|
1940
|
+
if (inkCtx === null) return;
|
|
1941
|
+
inkCtx.clearRect(0, 0, W, H);
|
|
1942
|
+
for (const st of strokes) drawStroke(inkCtx, st);
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1945
|
+
function paintLive() {
|
|
1946
|
+
raf = 0;
|
|
1947
|
+
if (liveCtx === null) return;
|
|
1948
|
+
liveCtx.clearRect(0, 0, W, H);
|
|
1949
|
+
if (op !== null && op.stroke) drawStroke(liveCtx, op.stroke);
|
|
1950
|
+
}
|
|
1951
|
+
|
|
1952
|
+
function scheduleLive() {
|
|
1953
|
+
if (raf === 0) raf = requestAnimationFrame(paintLive);
|
|
1954
|
+
}
|
|
1955
|
+
|
|
1956
|
+
function syncHistory() {
|
|
1957
|
+
undoBtn.disabled = past.length === 0;
|
|
1958
|
+
redoBtn.disabled = future.length === 0;
|
|
1959
|
+
}
|
|
1960
|
+
|
|
1961
|
+
function eraseAt(p) {
|
|
1962
|
+
const tol = imagePx(Math.max(8, size));
|
|
1963
|
+
const kept = strokes.filter((st) => !hitStroke(st, p, tol));
|
|
1964
|
+
if (kept.length === strokes.length) return;
|
|
1965
|
+
if (op.before === null) op.before = strokes;
|
|
1966
|
+
strokes = kept;
|
|
1967
|
+
redraw();
|
|
1968
|
+
}
|
|
1969
|
+
|
|
1970
|
+
function undo() {
|
|
1971
|
+
if (past.length === 0) return;
|
|
1972
|
+
future.push(strokes);
|
|
1973
|
+
strokes = past.pop();
|
|
1974
|
+
redraw();
|
|
1975
|
+
syncHistory();
|
|
1976
|
+
}
|
|
1977
|
+
|
|
1978
|
+
function redo() {
|
|
1979
|
+
if (future.length === 0) return;
|
|
1980
|
+
past.push(strokes);
|
|
1981
|
+
strokes = future.pop();
|
|
1982
|
+
redraw();
|
|
1983
|
+
syncHistory();
|
|
1984
|
+
}
|
|
1985
|
+
|
|
1986
|
+
/** Abandon the in-flight operation (a second finger arrived). */
|
|
1987
|
+
function cancelOp() {
|
|
1988
|
+
if (op === null) return;
|
|
1989
|
+
if (op.kind === 'erase' && op.before !== null) {
|
|
1990
|
+
past.push(op.before);
|
|
1991
|
+
future.length = 0;
|
|
1992
|
+
syncHistory();
|
|
1993
|
+
}
|
|
1994
|
+
op = null;
|
|
1995
|
+
if (raf !== 0) cancelAnimationFrame(raf);
|
|
1996
|
+
paintLive();
|
|
1997
|
+
}
|
|
1998
|
+
//#endregion
|
|
1999
|
+
|
|
2000
|
+
//#region pointer input
|
|
2001
|
+
function onDown(e) {
|
|
2002
|
+
if (!ready || busy) return;
|
|
2003
|
+
if (e.pointerType === 'mouse' && e.button !== 0 && e.button !== 1) return;
|
|
2004
|
+
if (e.pointerType === 'pen') sawPen = true;
|
|
2005
|
+
// Palm rejection: with an Apple Pencil in use, fingers only navigate.
|
|
2006
|
+
const navOnly = sawPen && e.pointerType === 'touch';
|
|
2007
|
+
e.preventDefault();
|
|
2008
|
+
try {
|
|
2009
|
+
area.setPointerCapture(e.pointerId);
|
|
2010
|
+
} catch {
|
|
2011
|
+
// capture is best-effort
|
|
2012
|
+
}
|
|
2013
|
+
pts.set(e.pointerId, { x: e.clientX, y: e.clientY, type: e.pointerType });
|
|
2014
|
+
closePop();
|
|
2015
|
+
if (pts.size >= 2) {
|
|
2016
|
+
cancelOp();
|
|
2017
|
+
const [a, b] = [...pts.values()];
|
|
2018
|
+
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 };
|
|
2019
|
+
return;
|
|
2020
|
+
}
|
|
2021
|
+
if (e.button === 1 || navOnly || (e.pointerType === 'mouse' && e.altKey && view.z > 1)) {
|
|
2022
|
+
op = { kind: 'pan', id: e.pointerId, sx: e.clientX, sy: e.clientY, vx: view.x, vy: view.y };
|
|
2023
|
+
return;
|
|
2024
|
+
}
|
|
2025
|
+
const p = toImage(e);
|
|
2026
|
+
if (tool === 'erase') {
|
|
2027
|
+
op = { kind: 'erase', id: e.pointerId, before: null };
|
|
2028
|
+
eraseAt(p);
|
|
2029
|
+
return;
|
|
2030
|
+
}
|
|
2031
|
+
const w = imagePx(size);
|
|
2032
|
+
const stroke = tool === 'pen' ? { kind: 'pen', color, w, points: [p] } : { kind: shape, color, w, a: p, b: p };
|
|
2033
|
+
op = { kind: 'draw', id: e.pointerId, stroke, sx: e.clientX, sy: e.clientY, moved: 0 };
|
|
2034
|
+
scheduleLive();
|
|
2035
|
+
}
|
|
2036
|
+
|
|
2037
|
+
function onMove(e) {
|
|
2038
|
+
const p0 = pts.get(e.pointerId);
|
|
2039
|
+
if (p0 === undefined) return;
|
|
2040
|
+
p0.x = e.clientX;
|
|
2041
|
+
p0.y = e.clientY;
|
|
2042
|
+
if (pinch !== null) {
|
|
2043
|
+
if (pts.size < 2) return;
|
|
2044
|
+
const [a, b] = [...pts.values()];
|
|
2045
|
+
const z = clamp(pinch.z * (dist(a, b) / pinch.d), 0.6, 10);
|
|
2046
|
+
const mx = (a.x + b.x) / 2;
|
|
2047
|
+
const my = (a.y + b.y) / 2;
|
|
2048
|
+
const r = area.getBoundingClientRect();
|
|
2049
|
+
const ox = pinch.mx - (r.left + r.width / 2);
|
|
2050
|
+
const oy = pinch.my - (r.top + r.height / 2);
|
|
2051
|
+
const k = z / pinch.z;
|
|
2052
|
+
view.z = z;
|
|
2053
|
+
view.x = ox - (ox - pinch.vx) * k + (mx - pinch.mx);
|
|
2054
|
+
view.y = oy - (oy - pinch.vy) * k + (my - pinch.my);
|
|
2055
|
+
applyView();
|
|
2056
|
+
return;
|
|
2057
|
+
}
|
|
2058
|
+
if (op === null || op.id !== e.pointerId) return;
|
|
2059
|
+
if (op.kind === 'pan') {
|
|
2060
|
+
view.x = op.vx + e.clientX - op.sx;
|
|
2061
|
+
view.y = op.vy + e.clientY - op.sy;
|
|
2062
|
+
applyView();
|
|
2063
|
+
return;
|
|
2064
|
+
}
|
|
2065
|
+
const events = typeof e.getCoalescedEvents === 'function' ? e.getCoalescedEvents() : [];
|
|
2066
|
+
const list = events.length > 0 ? events : [e];
|
|
2067
|
+
if (op.kind === 'erase') {
|
|
2068
|
+
for (const ev of list) eraseAt(toImage(ev));
|
|
2069
|
+
return;
|
|
2070
|
+
}
|
|
2071
|
+
op.moved = Math.max(op.moved, Math.hypot(e.clientX - op.sx, e.clientY - op.sy));
|
|
2072
|
+
const st = op.stroke;
|
|
2073
|
+
if (st.kind === 'pen') {
|
|
2074
|
+
const min = imagePx(1.2);
|
|
2075
|
+
for (const ev of list) {
|
|
2076
|
+
const p = toImage(ev);
|
|
2077
|
+
if (dist(p, st.points[st.points.length - 1]) >= min) st.points.push(p);
|
|
2078
|
+
}
|
|
2079
|
+
} else {
|
|
2080
|
+
let b = toImage(e);
|
|
2081
|
+
if (e.shiftKey) {
|
|
2082
|
+
const dx = b.x - st.a.x;
|
|
2083
|
+
const dy = b.y - st.a.y;
|
|
2084
|
+
if (st.kind === 'rect' || st.kind === 'ellipse') {
|
|
2085
|
+
const m = Math.max(Math.abs(dx), Math.abs(dy));
|
|
2086
|
+
b = { x: st.a.x + Math.sign(dx || 1) * m, y: st.a.y + Math.sign(dy || 1) * m };
|
|
2087
|
+
} else {
|
|
2088
|
+
const ang = Math.round(Math.atan2(dy, dx) / (Math.PI / 4)) * (Math.PI / 4);
|
|
2089
|
+
const len = Math.hypot(dx, dy);
|
|
2090
|
+
b = { x: st.a.x + len * Math.cos(ang), y: st.a.y + len * Math.sin(ang) };
|
|
2091
|
+
}
|
|
2092
|
+
}
|
|
2093
|
+
st.b = b;
|
|
2094
|
+
}
|
|
2095
|
+
scheduleLive();
|
|
2096
|
+
}
|
|
2097
|
+
|
|
2098
|
+
function onUp(e) {
|
|
2099
|
+
if (!pts.has(e.pointerId)) return;
|
|
2100
|
+
pts.delete(e.pointerId);
|
|
2101
|
+
if (pinch !== null) {
|
|
2102
|
+
if (pts.size < 2) {
|
|
2103
|
+
pinch = null;
|
|
2104
|
+
settleView();
|
|
2105
|
+
}
|
|
2106
|
+
return;
|
|
2107
|
+
}
|
|
2108
|
+
if (op === null || op.id !== e.pointerId) return;
|
|
2109
|
+
const done = op;
|
|
2110
|
+
op = null;
|
|
2111
|
+
if (done.kind === 'pan') {
|
|
2112
|
+
settleView();
|
|
2113
|
+
return;
|
|
2114
|
+
}
|
|
2115
|
+
if (done.kind === 'erase') {
|
|
2116
|
+
if (done.before !== null) {
|
|
2117
|
+
past.push(done.before);
|
|
2118
|
+
future.length = 0;
|
|
2119
|
+
syncHistory();
|
|
2120
|
+
}
|
|
2121
|
+
return;
|
|
2122
|
+
}
|
|
2123
|
+
if (raf !== 0) {
|
|
2124
|
+
cancelAnimationFrame(raf);
|
|
2125
|
+
raf = 0;
|
|
2126
|
+
}
|
|
2127
|
+
liveCtx.clearRect(0, 0, W, H);
|
|
2128
|
+
const st = done.stroke;
|
|
2129
|
+
if (e.type === 'pointercancel') return;
|
|
2130
|
+
if (st.kind !== 'pen' && dist(st.a, st.b) < imagePx(4)) return;
|
|
2131
|
+
past.push(strokes);
|
|
2132
|
+
future.length = 0;
|
|
2133
|
+
strokes = strokes.concat([st]);
|
|
2134
|
+
drawStroke(inkCtx, st);
|
|
2135
|
+
syncHistory();
|
|
2136
|
+
}
|
|
2137
|
+
|
|
2138
|
+
function onWheel(e) {
|
|
2139
|
+
if (!ready) return;
|
|
2140
|
+
e.preventDefault();
|
|
2141
|
+
if (e.ctrlKey || e.metaKey || view.z <= 1.01) {
|
|
2142
|
+
const unit = e.deltaMode === 1 ? 16 : 1;
|
|
2143
|
+
const z = clamp(view.z * Math.exp((-e.deltaY * unit) / (e.ctrlKey ? 100 : 400)), 1, 8);
|
|
2144
|
+
zoomAround(z, e.clientX, e.clientY);
|
|
2145
|
+
if (z <= 1.01) settleView();
|
|
2146
|
+
} else {
|
|
2147
|
+
view.x -= e.deltaX;
|
|
2148
|
+
view.y -= e.deltaY;
|
|
2149
|
+
const b = viewBounds(view.z);
|
|
2150
|
+
view.x = clamp(view.x, -b.x, b.x);
|
|
2151
|
+
view.y = clamp(view.y, -b.y, b.y);
|
|
2152
|
+
applyView();
|
|
2153
|
+
}
|
|
2154
|
+
}
|
|
2155
|
+
//#endregion
|
|
2156
|
+
|
|
2157
|
+
//#region tools, colour, size
|
|
2158
|
+
function closePop() {
|
|
2159
|
+
if (pop.hidden) return;
|
|
2160
|
+
pop.hidden = true;
|
|
2161
|
+
shapeBtn.setAttribute('aria-expanded', 'false');
|
|
2162
|
+
}
|
|
2163
|
+
|
|
2164
|
+
function setTool(next) {
|
|
2165
|
+
tool = next;
|
|
2166
|
+
penBtn.setAttribute('aria-pressed', String(next === 'pen'));
|
|
2167
|
+
shapeBtn.setAttribute('aria-pressed', String(next === 'shape'));
|
|
2168
|
+
eraseBtn.setAttribute('aria-pressed', String(next === 'erase'));
|
|
2169
|
+
root.setAttribute('data-tool', next);
|
|
2170
|
+
shapeBtn.innerHTML = ICON[shape];
|
|
2171
|
+
for (const [kind, b] of Object.entries(shapeButtons)) b.setAttribute('aria-pressed', String(kind === shape));
|
|
2172
|
+
}
|
|
2173
|
+
|
|
2174
|
+
function setColor(hex, custom) {
|
|
2175
|
+
color = hex;
|
|
2176
|
+
for (const sw of swatches) sw.setAttribute('aria-checked', String(!custom && sw.dataset.color === hex));
|
|
2177
|
+
customWrap.toggleAttribute('data-checked', custom === true);
|
|
2178
|
+
root.style.setProperty('--qie-ink', hex);
|
|
2179
|
+
if (tool === 'erase') setTool('pen');
|
|
2180
|
+
syncSize();
|
|
2181
|
+
}
|
|
2182
|
+
|
|
2183
|
+
function syncSize() {
|
|
2184
|
+
const k = Math.sqrt((size - SIZE_MIN) / (SIZE_MAX - SIZE_MIN));
|
|
2185
|
+
slider.style.setProperty('--qie-k', String(k));
|
|
2186
|
+
slider.setAttribute('aria-valuenow', String(Math.round(size)));
|
|
2187
|
+
const d = Math.max(4, size);
|
|
2188
|
+
preview.style.width = d + 'px';
|
|
2189
|
+
preview.style.height = d + 'px';
|
|
2190
|
+
}
|
|
2191
|
+
|
|
2192
|
+
function sizeFromPointer(e) {
|
|
2193
|
+
const r = rail.getBoundingClientRect();
|
|
2194
|
+
const k = clamp(1 - (e.clientY - r.top) / Math.max(1, r.height), 0, 1);
|
|
2195
|
+
size = SIZE_MIN + k * k * (SIZE_MAX - SIZE_MIN);
|
|
2196
|
+
syncSize();
|
|
2197
|
+
}
|
|
2198
|
+
|
|
2199
|
+
let sizeDrag = null;
|
|
2200
|
+
function onSizeDown(e) {
|
|
2201
|
+
e.preventDefault();
|
|
2202
|
+
e.stopPropagation();
|
|
2203
|
+
sizeDrag = e.pointerId;
|
|
2204
|
+
try {
|
|
2205
|
+
slider.setPointerCapture(e.pointerId);
|
|
2206
|
+
} catch {
|
|
2207
|
+
// capture is best-effort
|
|
2208
|
+
}
|
|
2209
|
+
slider.setAttribute('data-active', '');
|
|
2210
|
+
sizeFromPointer(e);
|
|
2211
|
+
}
|
|
2212
|
+
function onSizeMove(e) {
|
|
2213
|
+
if (sizeDrag === e.pointerId) sizeFromPointer(e);
|
|
2214
|
+
}
|
|
2215
|
+
function onSizeUp(e) {
|
|
2216
|
+
if (sizeDrag !== e.pointerId) return;
|
|
2217
|
+
sizeDrag = null;
|
|
2218
|
+
slider.removeAttribute('data-active');
|
|
2219
|
+
}
|
|
2220
|
+
function onSizeKey(e) {
|
|
2221
|
+
const step = e.shiftKey ? 4 : 1;
|
|
2222
|
+
if (e.key === 'ArrowUp' || e.key === 'ArrowRight') size = clamp(size + step, SIZE_MIN, SIZE_MAX);
|
|
2223
|
+
else if (e.key === 'ArrowDown' || e.key === 'ArrowLeft') size = clamp(size - step, SIZE_MIN, SIZE_MAX);
|
|
2224
|
+
else return;
|
|
2225
|
+
e.preventDefault();
|
|
2226
|
+
e.stopPropagation();
|
|
2227
|
+
syncSize();
|
|
2228
|
+
}
|
|
2229
|
+
//#endregion
|
|
2230
|
+
|
|
2231
|
+
//#region keyboard + focus
|
|
2232
|
+
function trap(e) {
|
|
2233
|
+
const list = [...root.querySelectorAll('button:not([disabled]),input,[tabindex="0"]')].filter(
|
|
2234
|
+
(el) => el.closest('[hidden]') === null,
|
|
2235
|
+
);
|
|
2236
|
+
if (list.length === 0) return;
|
|
2237
|
+
const first = list[0];
|
|
2238
|
+
const last = list[list.length - 1];
|
|
2239
|
+
const active = document.activeElement;
|
|
2240
|
+
if (!root.contains(active)) {
|
|
2241
|
+
e.preventDefault();
|
|
2242
|
+
first.focus();
|
|
2243
|
+
} else if (e.shiftKey && active === first) {
|
|
2244
|
+
e.preventDefault();
|
|
2245
|
+
last.focus();
|
|
2246
|
+
} else if (!e.shiftKey && active === last) {
|
|
2247
|
+
e.preventDefault();
|
|
2248
|
+
first.focus();
|
|
2249
|
+
}
|
|
2250
|
+
}
|
|
2251
|
+
|
|
2252
|
+
function onKey(e) {
|
|
2253
|
+
if (destroyed) return;
|
|
2254
|
+
const mod = e.ctrlKey || e.metaKey;
|
|
2255
|
+
if (e.key === 'Escape') {
|
|
2256
|
+
e.preventDefault();
|
|
2257
|
+
if (!pop.hidden) closePop();
|
|
2258
|
+
else cancel();
|
|
2259
|
+
} else if (mod && (e.key === 'z' || e.key === 'Z')) {
|
|
2260
|
+
e.preventDefault();
|
|
2261
|
+
if (e.shiftKey) redo();
|
|
2262
|
+
else undo();
|
|
2263
|
+
} else if (mod && (e.key === 'y' || e.key === 'Y')) {
|
|
2264
|
+
e.preventDefault();
|
|
2265
|
+
redo();
|
|
2266
|
+
} else if (e.key === 'Enter' && !mod) {
|
|
2267
|
+
const target = e.target;
|
|
2268
|
+
if (target instanceof Element && target.closest('button,input,[role="slider"]') !== null) return;
|
|
2269
|
+
e.preventDefault();
|
|
2270
|
+
confirm();
|
|
2271
|
+
} else if (e.key === 'Tab') {
|
|
2272
|
+
trap(e);
|
|
2273
|
+
}
|
|
2274
|
+
// The editor is modal: nothing behind it reacts to keys.
|
|
2275
|
+
e.stopPropagation();
|
|
2276
|
+
}
|
|
2277
|
+
//#endregion
|
|
2278
|
+
|
|
2279
|
+
//#region export + finish
|
|
2280
|
+
function blobOf(canvas, type, quality) {
|
|
2281
|
+
return new Promise((resolve, reject) => {
|
|
2282
|
+
try {
|
|
2283
|
+
canvas.toBlob((blob) => (blob ? resolve(blob) : reject(new Error('export failed'))), type, quality);
|
|
2284
|
+
} catch (error) {
|
|
2285
|
+
reject(error);
|
|
2286
|
+
}
|
|
2287
|
+
});
|
|
2288
|
+
}
|
|
2289
|
+
|
|
2290
|
+
async function exportFile() {
|
|
2291
|
+
const out = document.createElement('canvas');
|
|
2292
|
+
out.width = W;
|
|
2293
|
+
out.height = H;
|
|
2294
|
+
const ctx = out.getContext('2d');
|
|
2295
|
+
ctx.drawImage(img, 0, 0, W, H);
|
|
2296
|
+
ctx.drawImage(ink, 0, 0);
|
|
2297
|
+
let blob = await blobOf(out, 'image/png');
|
|
2298
|
+
let ext = 'png';
|
|
2299
|
+
if (blob.size > EXPORT_MAX_BYTES) {
|
|
2300
|
+
const flat = document.createElement('canvas');
|
|
2301
|
+
flat.width = W;
|
|
2302
|
+
flat.height = H;
|
|
2303
|
+
const fctx = flat.getContext('2d');
|
|
2304
|
+
fctx.fillStyle = '#fff';
|
|
2305
|
+
fctx.fillRect(0, 0, W, H);
|
|
2306
|
+
fctx.drawImage(out, 0, 0);
|
|
2307
|
+
blob = await blobOf(flat, 'image/jpeg', 0.9);
|
|
2308
|
+
ext = 'jpg';
|
|
2309
|
+
flat.width = 0;
|
|
2310
|
+
flat.height = 0;
|
|
2311
|
+
}
|
|
2312
|
+
out.width = 0;
|
|
2313
|
+
out.height = 0;
|
|
2314
|
+
const name = stemOf(item.src) + '-annotated.' + ext;
|
|
2315
|
+
try {
|
|
2316
|
+
return new File([blob], name, { type: blob.type, lastModified: Date.now() });
|
|
2317
|
+
} catch {
|
|
2318
|
+
blob.name = name;
|
|
2319
|
+
return blob;
|
|
2320
|
+
}
|
|
2321
|
+
}
|
|
2322
|
+
|
|
2323
|
+
async function confirm() {
|
|
2324
|
+
if (!ready || busy || destroyed) return;
|
|
2325
|
+
busy = true;
|
|
2326
|
+
root.setAttribute('data-busy', '');
|
|
2327
|
+
doneBtn.disabled = true;
|
|
2328
|
+
let file;
|
|
2329
|
+
try {
|
|
2330
|
+
file = await exportFile();
|
|
2331
|
+
} catch (error) {
|
|
2332
|
+
busy = false;
|
|
2333
|
+
root.removeAttribute('data-busy');
|
|
2334
|
+
doneBtn.disabled = false;
|
|
2335
|
+
toast(error && error.name === 'SecurityError' ? t('edit.tainted') : t('edit.exportFailed'));
|
|
2336
|
+
return;
|
|
2337
|
+
}
|
|
2338
|
+
if (destroyed) return;
|
|
2339
|
+
const where = injectFile(file);
|
|
2340
|
+
if (where === 'composer') {
|
|
2341
|
+
toast(t('edit.injected'));
|
|
2342
|
+
} else {
|
|
2343
|
+
downloadFile(file);
|
|
2344
|
+
toast(t('edit.noComposer'));
|
|
2345
|
+
}
|
|
2346
|
+
destroy(false);
|
|
2347
|
+
hooks.done(where === 'composer');
|
|
2348
|
+
}
|
|
2349
|
+
|
|
2350
|
+
function cancel() {
|
|
2351
|
+
if (busy) return;
|
|
2352
|
+
destroy(true);
|
|
2353
|
+
hooks.cancel();
|
|
2354
|
+
}
|
|
2355
|
+
|
|
2356
|
+
function destroy(restore) {
|
|
2357
|
+
if (destroyed) return;
|
|
2358
|
+
destroyed = true;
|
|
2359
|
+
if (raf !== 0) cancelAnimationFrame(raf);
|
|
2360
|
+
window.removeEventListener('keydown', onKey, true);
|
|
2361
|
+
window.removeEventListener('resize', layout);
|
|
2362
|
+
for (const c of [ink, live]) {
|
|
2363
|
+
c.width = 0;
|
|
2364
|
+
c.height = 0;
|
|
2365
|
+
}
|
|
2366
|
+
root.remove();
|
|
2367
|
+
void restore;
|
|
2368
|
+
}
|
|
2369
|
+
//#endregion
|
|
2370
|
+
|
|
2371
|
+
//#region load
|
|
2372
|
+
async function load() {
|
|
2373
|
+
let loaded;
|
|
2374
|
+
try {
|
|
2375
|
+
loaded = await loadEditable(item.src);
|
|
2376
|
+
} catch {
|
|
2377
|
+
if (destroyed) return;
|
|
2378
|
+
status.textContent = t('failed');
|
|
2379
|
+
doneBtn.disabled = true;
|
|
2380
|
+
return;
|
|
2381
|
+
}
|
|
2382
|
+
if (destroyed) return;
|
|
2383
|
+
img = loaded.img;
|
|
2384
|
+
tainted = loaded.tainted;
|
|
2385
|
+
const nw = img.naturalWidth || img.width || item.w || 1024;
|
|
2386
|
+
const nh = img.naturalHeight || img.height || item.h || 768;
|
|
2387
|
+
const k = Math.min(1, EDIT_MAX_EDGE / Math.max(nw, nh), Math.sqrt(EDIT_MAX_PIXELS / (nw * nh)));
|
|
2388
|
+
W = Math.max(1, Math.round(nw * k));
|
|
2389
|
+
H = Math.max(1, Math.round(nh * k));
|
|
2390
|
+
for (const c of [ink, live]) {
|
|
2391
|
+
c.width = W;
|
|
2392
|
+
c.height = H;
|
|
2393
|
+
}
|
|
2394
|
+
inkCtx = ink.getContext('2d');
|
|
2395
|
+
liveCtx = live.getContext('2d');
|
|
2396
|
+
img.className = 'qie-base';
|
|
2397
|
+
img.alt = item.alt || '';
|
|
2398
|
+
img.draggable = false;
|
|
2399
|
+
base.replaceWith(img);
|
|
2400
|
+
base = img;
|
|
2401
|
+
status.hidden = true;
|
|
2402
|
+
note.hidden = !tainted;
|
|
2403
|
+
ready = true;
|
|
2404
|
+
root.setAttribute('data-ready', '');
|
|
2405
|
+
layout();
|
|
2406
|
+
}
|
|
2407
|
+
//#endregion
|
|
2408
|
+
|
|
2409
|
+
area.addEventListener('pointerdown', onDown);
|
|
2410
|
+
area.addEventListener('pointermove', onMove);
|
|
2411
|
+
area.addEventListener('pointerup', onUp);
|
|
2412
|
+
area.addEventListener('pointercancel', onUp);
|
|
2413
|
+
area.addEventListener('wheel', onWheel, { passive: false });
|
|
2414
|
+
root.addEventListener('dragstart', (e) => e.preventDefault());
|
|
2415
|
+
root.addEventListener('gesturestart', (e) => e.preventDefault());
|
|
2416
|
+
root.addEventListener('contextmenu', (e) => {
|
|
2417
|
+
if (area.contains(e.target)) e.preventDefault();
|
|
2418
|
+
});
|
|
2419
|
+
slider.addEventListener('pointerdown', onSizeDown);
|
|
2420
|
+
slider.addEventListener('pointermove', onSizeMove);
|
|
2421
|
+
slider.addEventListener('pointerup', onSizeUp);
|
|
2422
|
+
slider.addEventListener('pointercancel', onSizeUp);
|
|
2423
|
+
slider.addEventListener('keydown', onSizeKey);
|
|
2424
|
+
window.addEventListener('keydown', onKey, true);
|
|
2425
|
+
window.addEventListener('resize', layout);
|
|
2426
|
+
backBtn.addEventListener('click', cancel);
|
|
2427
|
+
undoBtn.addEventListener('click', undo);
|
|
2428
|
+
redoBtn.addEventListener('click', redo);
|
|
2429
|
+
penBtn.addEventListener('click', () => {
|
|
2430
|
+
closePop();
|
|
2431
|
+
setTool('pen');
|
|
2432
|
+
});
|
|
2433
|
+
eraseBtn.addEventListener('click', () => {
|
|
2434
|
+
closePop();
|
|
2435
|
+
setTool('erase');
|
|
2436
|
+
});
|
|
2437
|
+
shapeBtn.addEventListener('click', () => {
|
|
2438
|
+
if (tool === 'shape' && pop.hidden) {
|
|
2439
|
+
pop.hidden = false;
|
|
2440
|
+
shapeBtn.setAttribute('aria-expanded', 'true');
|
|
2441
|
+
} else if (!pop.hidden) {
|
|
2442
|
+
closePop();
|
|
2443
|
+
} else {
|
|
2444
|
+
setTool('shape');
|
|
2445
|
+
}
|
|
2446
|
+
});
|
|
2447
|
+
for (const [kind, b] of Object.entries(shapeButtons)) {
|
|
2448
|
+
b.addEventListener('click', () => {
|
|
2449
|
+
shape = kind;
|
|
2450
|
+
setTool('shape');
|
|
2451
|
+
closePop();
|
|
2452
|
+
});
|
|
2453
|
+
}
|
|
2454
|
+
for (const sw of swatches) sw.addEventListener('click', () => setColor(sw.dataset.color, false));
|
|
2455
|
+
customInput.addEventListener('input', () => {
|
|
2456
|
+
customColor = customInput.value;
|
|
2457
|
+
customWrap.style.setProperty('--qie-c', customColor);
|
|
2458
|
+
setColor(customColor, true);
|
|
2459
|
+
});
|
|
2460
|
+
customWrap.addEventListener('click', () => {
|
|
2461
|
+
if (customColor !== null) setColor(customColor, true);
|
|
2462
|
+
});
|
|
2463
|
+
doneBtn.addEventListener('click', confirm);
|
|
2464
|
+
|
|
2465
|
+
setTool('pen');
|
|
2466
|
+
setColor(color, false);
|
|
2467
|
+
syncHistory();
|
|
2468
|
+
document.body.appendChild(root);
|
|
2469
|
+
try {
|
|
2470
|
+
doneBtn.focus({ preventScroll: true });
|
|
2471
|
+
} catch {
|
|
2472
|
+
// focus is best-effort
|
|
2473
|
+
}
|
|
2474
|
+
load();
|
|
2475
|
+
|
|
2476
|
+
return {
|
|
2477
|
+
destroy: () => destroy(false),
|
|
2478
|
+
/** Test hooks: inspect strokes without reaching into closures. */
|
|
2479
|
+
get strokes() {
|
|
2480
|
+
return strokes.length;
|
|
2481
|
+
},
|
|
2482
|
+
};
|
|
2483
|
+
}
|
|
2484
|
+
|
|
2485
|
+
const EDIT_CSS = `
|
|
2486
|
+
[${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}
|
|
2487
|
+
[${EDIT_ATTR}] *{box-sizing:border-box}
|
|
2488
|
+
.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) + var(--qiv-top-inset,10px)) calc(env(safe-area-inset-right,0px) + 14px) 10px calc(env(safe-area-inset-left,0px) + 14px)}
|
|
2489
|
+
.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}
|
|
2490
|
+
.qie-round{width:44px;height:44px;border-radius:999px;background:rgba(255,255,255,.14)}
|
|
2491
|
+
.qie-round:hover{background:rgba(255,255,255,.24)}
|
|
2492
|
+
.qie-round[disabled]{opacity:.35;cursor:default}
|
|
2493
|
+
.qie-top>.qie-round{justify-self:start}
|
|
2494
|
+
.qie-history{justify-self:end;display:flex;gap:8px}
|
|
2495
|
+
.qie-tools{display:flex;gap:4px;padding:4px;border-radius:999px;background:rgba(255,255,255,.14)}
|
|
2496
|
+
.qie-tool{width:52px;height:40px;border-radius:999px;background:transparent}
|
|
2497
|
+
.qie-tool:hover{background:rgba(255,255,255,.12)}
|
|
2498
|
+
.qie-tool[aria-pressed=true]{background:rgba(255,255,255,.26)}
|
|
2499
|
+
.qie-tool svg,.qie-round svg{width:22px;height:22px}
|
|
2500
|
+
.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)}
|
|
2501
|
+
.qie-pop[hidden]{display:none}
|
|
2502
|
+
.qie-shape{width:48px;height:44px;border-radius:12px;background:transparent}
|
|
2503
|
+
.qie-shape:hover{background:rgba(255,255,255,.12)}
|
|
2504
|
+
.qie-shape[aria-pressed=true]{background:rgba(255,255,255,.24)}
|
|
2505
|
+
.qie-area{position:relative;flex:1;min-height:0;overflow:hidden;touch-action:none;cursor:crosshair}
|
|
2506
|
+
[${EDIT_ATTR}][data-tool=erase] .qie-area{cursor:cell}
|
|
2507
|
+
.qie-stage{position:absolute;left:50%;top:50%;transform-origin:50% 50%;box-shadow:0 0 0 1px rgba(255,255,255,.08)}
|
|
2508
|
+
[${EDIT_ATTR}][data-settle] .qie-stage{transition:transform .24s cubic-bezier(.2,.8,.2,1)}
|
|
2509
|
+
.qie-stage>img,.qie-stage>canvas{position:absolute;inset:0;display:block;width:100%;height:100%;-webkit-user-drag:none;pointer-events:none}
|
|
2510
|
+
.qie-status{position:absolute;inset:0;display:grid;place-items:center;pointer-events:none;opacity:.85}
|
|
2511
|
+
.qie-status[hidden],.qie-note[hidden]{display:none}
|
|
2512
|
+
.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}
|
|
2513
|
+
.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}
|
|
2514
|
+
.qie-rail{position:relative;width:4px;height:100%;border-radius:2px;background:rgba(255,255,255,.35)}
|
|
2515
|
+
.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)}
|
|
2516
|
+
.qie-size:focus-visible .qie-knob{outline:2px solid #fff;outline-offset:3px}
|
|
2517
|
+
.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}
|
|
2518
|
+
.qie-size[data-active] .qie-preview{opacity:1}
|
|
2519
|
+
.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)}
|
|
2520
|
+
.qie-palette{flex:1;min-width:0;display:flex;align-items:center;gap:10px;overflow-x:auto;scrollbar-width:none;padding:6px 4px}
|
|
2521
|
+
.qie-palette::-webkit-scrollbar{display:none}
|
|
2522
|
+
.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}
|
|
2523
|
+
.qie-swatch[aria-checked=true],.qie-custom[data-checked]{box-shadow:0 0 0 2px #000,0 0 0 4px #0a84ff}
|
|
2524
|
+
.qie-custom{background:conic-gradient(#ff3b30,#ffcc00,#34c759,#5ac8fa,#007aff,#af52de,#ff3b30)}
|
|
2525
|
+
.qie-custom::after{content:"";position:absolute;inset:5px;border-radius:50%;background:var(--qie-c,#fff);box-shadow:0 0 0 2px #000}
|
|
2526
|
+
.qie-custom input{position:absolute;inset:0;width:100%;height:100%;opacity:0;border:0;padding:0;cursor:pointer}
|
|
2527
|
+
.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}
|
|
2528
|
+
.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}
|
|
2529
|
+
.qie-done svg{width:26px;height:26px}
|
|
2530
|
+
.qie-done[disabled]{opacity:.5;cursor:default}
|
|
2531
|
+
[${EDIT_ATTR}][data-busy] .qie-done svg{display:none}
|
|
2532
|
+
[${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}
|
|
2533
|
+
.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}
|
|
2534
|
+
.qiv-toast[data-show]{opacity:1;transform:translate(-50%,0)}
|
|
2535
|
+
@media (prefers-reduced-motion:reduce){[${EDIT_ATTR}][data-settle] .qie-stage,.qiv-toast{transition:none}}
|
|
2536
|
+
`;
|
|
2537
|
+
//#endregion
|
|
2538
|
+
|
|
1335
2539
|
//#region click interception
|
|
1336
2540
|
function installInterceptor(t) {
|
|
1337
2541
|
let viewer = null;
|