@qzsy/dsh-image-viewer 0.3.1 → 0.4.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 +10 -0
- package/dist/client.js +265 -3
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -33,6 +33,15 @@ dsh plugin add @qzsy/dsh-image-viewer
|
|
|
33
33
|
|
|
34
34
|
系统开启“减少动态效果”时,所有动画直接跳到终态。
|
|
35
35
|
|
|
36
|
+
## 右键菜单
|
|
37
|
+
|
|
38
|
+
在预览里右键(手机上长按)任意图片,弹出两项操作:
|
|
39
|
+
|
|
40
|
+
- **复制图片**:把图片以 PNG 写进系统剪贴板,可以直接粘贴到聊天框、文档或设计工具。原图不是 PNG 时会先转码;来自其他站点、浏览器禁止跨域读取的图片会给出提示。
|
|
41
|
+
- **复制图片路径**:复制图片在磁盘上的真实路径(例如 `D:\shots\a.png`);不是本地文件(外链、data: 图片)时复制完整 URL。
|
|
42
|
+
|
|
43
|
+
菜单打开后 `Esc` 或 `Tab` 收起菜单(`Esc` 再按一次才关闭查看器),`↑` `↓` 在菜单项间移动,`Enter` 执行;点击菜单外任意位置、滚轮、翻页或窗口尺寸变化都会自动收起。
|
|
44
|
+
|
|
36
45
|
## 桌面端窗口边缘
|
|
37
46
|
|
|
38
47
|
DSH 桌面应用(Electron)会把 Windows 右上角的窗口按钮、macOS 左上角的红绿灯画在网页之上。查看器和标注编辑器的顶栏按宿主发布的安全边距(`--dsh-frame-overlay-top`,Windows 为标题栏高度 + 20px,macOS 为 68px,原生全屏时降到 20px)下移,不会落在窗口按钮下面;同时图片的最大显示宽度固定为视口的 90%,标注画布同理,任何窗口尺寸下都不会被顶栏或窗口控件遮挡。
|
|
@@ -77,6 +86,7 @@ bun run typecheck
|
|
|
77
86
|
node test/smoke.mjs # jsdom 冒烟测试:查看器
|
|
78
87
|
node test/layout.mjs # jsdom 布局测试:90% 宽度上限与桌面窗口留白
|
|
79
88
|
node test/editor.mjs # jsdom 冒烟测试:标注与注入输入框
|
|
89
|
+
node test/menu.mjs # jsdom 冒烟测试:右键复制图片 / 复制路径
|
|
80
90
|
~~~
|
|
81
91
|
|
|
82
92
|
## 许可
|
package/dist/client.js
CHANGED
|
@@ -37,6 +37,8 @@ window.__ModuleLoader__.load({
|
|
|
37
37
|
/** Movement before a press becomes a drag. */
|
|
38
38
|
const SLOP = 8;
|
|
39
39
|
const TAP_MAX_MS = 500;
|
|
40
|
+
/** Press-and-hold opens the context menu on touch screens (Safari has no right click). */
|
|
41
|
+
const LONG_PRESS_MS = 500;
|
|
40
42
|
const DOUBLE_TAP_MS = 300;
|
|
41
43
|
const DOUBLE_TAP_PX = 32;
|
|
42
44
|
/** iOS UIScrollView "normal" deceleration rate, per millisecond. */
|
|
@@ -96,6 +98,14 @@ window.__ModuleLoader__.load({
|
|
|
96
98
|
'edit.tainted': '这张图片来自其他站点,浏览器不允许导出',
|
|
97
99
|
'edit.taintedHint': '图片来自其他站点,可能无法导出',
|
|
98
100
|
'edit.exportFailed': '导出图片失败',
|
|
101
|
+
'menu.label': '图片操作',
|
|
102
|
+
'menu.copyImage': '复制图片',
|
|
103
|
+
'menu.copyPath': '复制图片路径',
|
|
104
|
+
'menu.copiedImage': '已复制图片',
|
|
105
|
+
'menu.copiedPath': '已复制图片路径',
|
|
106
|
+
'menu.copyFailed': '复制失败',
|
|
107
|
+
'menu.blocked': '图片来自其他站点,浏览器不允许复制',
|
|
108
|
+
'menu.unsupported': '当前环境不支持复制图片',
|
|
99
109
|
};
|
|
100
110
|
const en = {
|
|
101
111
|
dialog: 'Image viewer',
|
|
@@ -138,6 +148,14 @@ window.__ModuleLoader__.load({
|
|
|
138
148
|
'edit.tainted': 'This image comes from another site and the browser blocks exporting it.',
|
|
139
149
|
'edit.taintedHint': 'Image from another site; export may be blocked.',
|
|
140
150
|
'edit.exportFailed': 'Could not export the image.',
|
|
151
|
+
'menu.label': 'Image actions',
|
|
152
|
+
'menu.copyImage': 'Copy image',
|
|
153
|
+
'menu.copyPath': 'Copy image path',
|
|
154
|
+
'menu.copiedImage': 'Image copied',
|
|
155
|
+
'menu.copiedPath': 'Image path copied',
|
|
156
|
+
'menu.copyFailed': 'Copy failed',
|
|
157
|
+
'menu.blocked': 'This image comes from another site and the browser blocks copying it.',
|
|
158
|
+
'menu.unsupported': 'This environment cannot copy images.',
|
|
141
159
|
};
|
|
142
160
|
|
|
143
161
|
//#region utilities
|
|
@@ -432,8 +450,13 @@ window.__ModuleLoader__.load({
|
|
|
432
450
|
.qiv-thumb:hover{opacity:.8}
|
|
433
451
|
.qiv-thumb[aria-current=true]{opacity:1;transform:scale(1.1);box-shadow:0 0 0 2px #fff}
|
|
434
452
|
.qiv-thumb img{display:block;width:100%;height:100%;object-fit:cover;pointer-events:none}
|
|
453
|
+
.qiv-menu{position:fixed;z-index:2147483005;min-width:188px;max-width:calc(100vw - 16px);padding:4px;border-radius:12px;background:rgba(38,38,40,.97);box-shadow:0 12px 32px rgba(0,0,0,.55),0 0 0 1px rgba(255,255,255,.08);color:#fff;font:13px/1.4 system-ui,-apple-system,"Segoe UI",Roboto,sans-serif;animation:qiv-menu-in .12s ease-out}
|
|
454
|
+
@keyframes qiv-menu-in{from{opacity:0;transform:scale(.96)}to{opacity:1;transform:none}}
|
|
455
|
+
.qiv-menu-item{appearance:none;display:block;width:100%;padding:9px 12px;border:0;border-radius:8px;background:transparent;color:inherit;font:inherit;text-align:left;cursor:pointer;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
|
|
456
|
+
.qiv-menu-item:hover,.qiv-menu-item:focus-visible{background:rgba(255,255,255,.14);outline:none}
|
|
457
|
+
.qiv-menu-item[disabled]{opacity:.5;cursor:default}
|
|
435
458
|
.qiv-sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
|
|
436
|
-
@media (prefers-reduced-motion:reduce){.qiv-chrome,.qiv-thumb{transition:none}}
|
|
459
|
+
@media (prefers-reduced-motion:reduce){.qiv-chrome,.qiv-thumb{transition:none}.qiv-menu{animation:none}}
|
|
437
460
|
[${LINK_ATTR}]{cursor:zoom-in}
|
|
438
461
|
`;
|
|
439
462
|
|
|
@@ -686,9 +709,12 @@ window.__ModuleLoader__.load({
|
|
|
686
709
|
let destroyed = false;
|
|
687
710
|
/** Open annotation editor; the viewer pauses keyboard input under it. */
|
|
688
711
|
let editor = null;
|
|
712
|
+
/** Open context menu, or null. It lives in <body>, above the viewer. */
|
|
713
|
+
let menu = null;
|
|
689
714
|
let gesture = null;
|
|
690
715
|
let samples = [];
|
|
691
716
|
let tapTimer = 0;
|
|
717
|
+
let longPress = 0;
|
|
692
718
|
let lastTap = null;
|
|
693
719
|
let lastZoomed = null;
|
|
694
720
|
let hiddenItem = null;
|
|
@@ -744,6 +770,74 @@ window.__ModuleLoader__.load({
|
|
|
744
770
|
strip.append(inner);
|
|
745
771
|
root.append(strip);
|
|
746
772
|
}
|
|
773
|
+
//#region context menu
|
|
774
|
+
function closeMenu(refocus) {
|
|
775
|
+
if (menu === null) return;
|
|
776
|
+
const node = menu;
|
|
777
|
+
menu = null;
|
|
778
|
+
node.remove();
|
|
779
|
+
if (refocus === true && !closing && !destroyed) {
|
|
780
|
+
try {
|
|
781
|
+
root.focus({ preventScroll: true });
|
|
782
|
+
} catch {
|
|
783
|
+
// focus is best-effort
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
function menuItem(label, run) {
|
|
789
|
+
const button = h('button', 'qiv-menu-item', { type: 'button', role: 'menuitem', tabindex: '-1' });
|
|
790
|
+
button.textContent = label;
|
|
791
|
+
button.addEventListener('click', () => {
|
|
792
|
+
if (menu === null) return;
|
|
793
|
+
run(button);
|
|
794
|
+
});
|
|
795
|
+
return button;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
function openMenu(x, y) {
|
|
799
|
+
closeMenu();
|
|
800
|
+
const item = items[index];
|
|
801
|
+
const node = h('div', 'qiv-menu', { role: 'menu', 'aria-label': t('menu.label') });
|
|
802
|
+
const copy = menuItem(t('menu.copyImage'), async (button) => {
|
|
803
|
+
button.disabled = true;
|
|
804
|
+
closeMenu();
|
|
805
|
+
const result = await copyImage(item.src);
|
|
806
|
+
toast(t(result === 'ok' ? 'menu.copiedImage' : result === 'blocked' ? 'menu.blocked' : result === 'unsupported' ? 'menu.unsupported' : 'menu.copyFailed'));
|
|
807
|
+
});
|
|
808
|
+
const path = menuItem(t('menu.copyPath'), async (button) => {
|
|
809
|
+
button.disabled = true;
|
|
810
|
+
closeMenu();
|
|
811
|
+
const address = addressOf(item.src);
|
|
812
|
+
const ok = address !== null && (await copyText(address));
|
|
813
|
+
toast(t(ok ? 'menu.copiedPath' : 'menu.copyFailed'));
|
|
814
|
+
});
|
|
815
|
+
node.append(copy, path);
|
|
816
|
+
document.body.append(node);
|
|
817
|
+
menu = node;
|
|
818
|
+
// Keep the menu on screen: clamp after it has its own size.
|
|
819
|
+
const rect = node.getBoundingClientRect();
|
|
820
|
+
node.style.left = clamp(x, 8, Math.max(8, window.innerWidth - rect.width - 8)) + 'px';
|
|
821
|
+
node.style.top = clamp(y, 8, Math.max(8, window.innerHeight - rect.height - 8)) + 'px';
|
|
822
|
+
try {
|
|
823
|
+
copy.focus({ preventScroll: true });
|
|
824
|
+
} catch {
|
|
825
|
+
// focus is best-effort
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
function onDocDown(e) {
|
|
830
|
+
if (menu === null) return;
|
|
831
|
+
if (e.target instanceof Node && menu.contains(e.target)) return;
|
|
832
|
+
closeMenu();
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
root.addEventListener('contextmenu', (e) => {
|
|
836
|
+
if (closing || destroyed || editor !== null) return;
|
|
837
|
+
e.preventDefault();
|
|
838
|
+
openMenu(e.clientX || vw / 2, e.clientY || vh / 2);
|
|
839
|
+
});
|
|
840
|
+
document.addEventListener('pointerdown', onDocDown, true);
|
|
747
841
|
//#endregion
|
|
748
842
|
|
|
749
843
|
//#region geometry
|
|
@@ -957,6 +1051,7 @@ window.__ModuleLoader__.load({
|
|
|
957
1051
|
}
|
|
958
1052
|
|
|
959
1053
|
function updateChrome() {
|
|
1054
|
+
closeMenu();
|
|
960
1055
|
const item = items[index];
|
|
961
1056
|
const text = n > 1 ? t('counter', { i: index + 1, n }) : '';
|
|
962
1057
|
counter.textContent = text;
|
|
@@ -1102,6 +1197,11 @@ window.__ModuleLoader__.load({
|
|
|
1102
1197
|
lastTap = null;
|
|
1103
1198
|
}
|
|
1104
1199
|
|
|
1200
|
+
function clearLongPress() {
|
|
1201
|
+
if (longPress !== 0) clearTimeout(longPress);
|
|
1202
|
+
longPress = 0;
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1105
1205
|
function velocity() {
|
|
1106
1206
|
const tNow = now();
|
|
1107
1207
|
const recent = samples.filter((p) => tNow - p.t <= 100);
|
|
@@ -1125,6 +1225,7 @@ window.__ModuleLoader__.load({
|
|
|
1125
1225
|
|
|
1126
1226
|
function startPinch() {
|
|
1127
1227
|
clearTap();
|
|
1228
|
+
clearLongPress();
|
|
1128
1229
|
const [a, b] = pointPair();
|
|
1129
1230
|
gesture = {
|
|
1130
1231
|
mode: 'pinch',
|
|
@@ -1176,8 +1277,22 @@ window.__ModuleLoader__.load({
|
|
|
1176
1277
|
const p = { x: e.clientX, y: e.clientY, type: e.pointerType || 'mouse' };
|
|
1177
1278
|
pointers.set(e.pointerId, p);
|
|
1178
1279
|
anim.stop(['x', 'y', 'scale', 'bg', 'alpha', 'trackX']);
|
|
1179
|
-
if (pointers.size === 1)
|
|
1180
|
-
|
|
1280
|
+
if (pointers.size === 1) {
|
|
1281
|
+
startSingle(e.pointerId, p);
|
|
1282
|
+
// Touch screens have no right click: a still finger opens the same menu.
|
|
1283
|
+
if (p.type !== 'mouse') {
|
|
1284
|
+
const x = p.x;
|
|
1285
|
+
const y = p.y;
|
|
1286
|
+
clearLongPress();
|
|
1287
|
+
longPress = setTimeout(() => {
|
|
1288
|
+
longPress = 0;
|
|
1289
|
+
if (gesture === null || gesture.mode !== 'pending' || pointers.size !== 1) return;
|
|
1290
|
+
gesture.mode = 'menu';
|
|
1291
|
+
setGesture(null);
|
|
1292
|
+
openMenu(x, y);
|
|
1293
|
+
}, LONG_PRESS_MS);
|
|
1294
|
+
}
|
|
1295
|
+
} else if (pointers.size === 2) startPinch();
|
|
1181
1296
|
}
|
|
1182
1297
|
|
|
1183
1298
|
function movePinch() {
|
|
@@ -1223,6 +1338,7 @@ window.__ModuleLoader__.load({
|
|
|
1223
1338
|
let dy = p.y - g.sy;
|
|
1224
1339
|
if (g.mode === 'pending') {
|
|
1225
1340
|
if (Math.hypot(dx, dy) < SLOP) return;
|
|
1341
|
+
clearLongPress();
|
|
1226
1342
|
clearTap();
|
|
1227
1343
|
g.mode = classify(dx, dy);
|
|
1228
1344
|
// Rebase so the drag starts from here instead of jumping by SLOP.
|
|
@@ -1323,6 +1439,7 @@ window.__ModuleLoader__.load({
|
|
|
1323
1439
|
}
|
|
1324
1440
|
|
|
1325
1441
|
function onUp(e) {
|
|
1442
|
+
clearLongPress();
|
|
1326
1443
|
if (!pointers.has(e.pointerId)) return;
|
|
1327
1444
|
pointers.delete(e.pointerId);
|
|
1328
1445
|
try {
|
|
@@ -1332,6 +1449,12 @@ window.__ModuleLoader__.load({
|
|
|
1332
1449
|
}
|
|
1333
1450
|
const g = gesture;
|
|
1334
1451
|
if (g === null) return;
|
|
1452
|
+
if (g.mode === 'menu') {
|
|
1453
|
+
// The press became a long-press menu: it is not a tap.
|
|
1454
|
+
gesture = null;
|
|
1455
|
+
setGesture(null);
|
|
1456
|
+
return;
|
|
1457
|
+
}
|
|
1335
1458
|
const cancelled = e.type === 'pointercancel';
|
|
1336
1459
|
if (g.mode === 'pinch') {
|
|
1337
1460
|
if (pointers.size === 1) {
|
|
@@ -1378,6 +1501,7 @@ window.__ModuleLoader__.load({
|
|
|
1378
1501
|
//#region wheel + keyboard + resize
|
|
1379
1502
|
function onWheel(e) {
|
|
1380
1503
|
e.preventDefault();
|
|
1504
|
+
closeMenu();
|
|
1381
1505
|
if (closing) return;
|
|
1382
1506
|
const unit = e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? vh : 1;
|
|
1383
1507
|
const dx = e.deltaX * unit;
|
|
@@ -1432,6 +1556,29 @@ window.__ModuleLoader__.load({
|
|
|
1432
1556
|
|
|
1433
1557
|
function onKey(e) {
|
|
1434
1558
|
if (closing || destroyed || editor !== null) return;
|
|
1559
|
+
if (menu !== null) {
|
|
1560
|
+
// The menu owns its keys: Escape/Tab dismiss, arrows move, Enter presses.
|
|
1561
|
+
if (e.key === 'Escape' || e.key === 'Tab') {
|
|
1562
|
+
e.preventDefault();
|
|
1563
|
+
e.stopPropagation();
|
|
1564
|
+
closeMenu(true);
|
|
1565
|
+
return;
|
|
1566
|
+
}
|
|
1567
|
+
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
|
|
1568
|
+
e.preventDefault();
|
|
1569
|
+
e.stopPropagation();
|
|
1570
|
+
const list = [...menu.querySelectorAll('.qiv-menu-item')];
|
|
1571
|
+
const at = list.indexOf(document.activeElement);
|
|
1572
|
+
list[(at + (e.key === 'ArrowDown' ? 1 : -1) + list.length) % list.length].focus();
|
|
1573
|
+
return;
|
|
1574
|
+
}
|
|
1575
|
+
if (e.key === 'Enter' || e.key === ' ') {
|
|
1576
|
+
e.preventDefault();
|
|
1577
|
+
e.stopPropagation();
|
|
1578
|
+
if (document.activeElement instanceof HTMLElement) document.activeElement.click();
|
|
1579
|
+
return;
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1435
1582
|
if (e.ctrlKey || e.metaKey || e.altKey) return;
|
|
1436
1583
|
let handled = true;
|
|
1437
1584
|
switch (e.key) {
|
|
@@ -1467,6 +1614,7 @@ window.__ModuleLoader__.load({
|
|
|
1467
1614
|
}
|
|
1468
1615
|
|
|
1469
1616
|
function onResize() {
|
|
1617
|
+
closeMenu();
|
|
1470
1618
|
vw = root.clientWidth || window.innerWidth;
|
|
1471
1619
|
vh = root.clientHeight || window.innerHeight;
|
|
1472
1620
|
// Native fullscreen drops the shell's chrome strip, so re-read it.
|
|
@@ -1487,6 +1635,8 @@ window.__ModuleLoader__.load({
|
|
|
1487
1635
|
if (closing || destroyed) return;
|
|
1488
1636
|
closing = true;
|
|
1489
1637
|
clearTap();
|
|
1638
|
+
clearLongPress();
|
|
1639
|
+
closeMenu();
|
|
1490
1640
|
root.dataset.state = 'closing';
|
|
1491
1641
|
root.style.pointerEvents = 'none';
|
|
1492
1642
|
pointers.clear();
|
|
@@ -1522,7 +1672,10 @@ window.__ModuleLoader__.load({
|
|
|
1522
1672
|
}
|
|
1523
1673
|
anim.destroy();
|
|
1524
1674
|
clearTap();
|
|
1675
|
+
clearLongPress();
|
|
1525
1676
|
showSource();
|
|
1677
|
+
closeMenu();
|
|
1678
|
+
document.removeEventListener('pointerdown', onDocDown, true);
|
|
1526
1679
|
window.removeEventListener('keydown', onKey, true);
|
|
1527
1680
|
window.removeEventListener('resize', onResize);
|
|
1528
1681
|
root.remove();
|
|
@@ -1553,6 +1706,7 @@ window.__ModuleLoader__.load({
|
|
|
1553
1706
|
function openEdit() {
|
|
1554
1707
|
if (closing || destroyed || editor !== null) return;
|
|
1555
1708
|
clearTap();
|
|
1709
|
+
closeMenu();
|
|
1556
1710
|
editor = openEditor(items[index], t, {
|
|
1557
1711
|
cancel: () => {
|
|
1558
1712
|
editor = null;
|
|
@@ -1731,6 +1885,114 @@ window.__ModuleLoader__.load({
|
|
|
1731
1885
|
}
|
|
1732
1886
|
//#endregion
|
|
1733
1887
|
|
|
1888
|
+
//#region clipboard
|
|
1889
|
+
/**
|
|
1890
|
+
* The file on disk behind an image URL, when there is one. DSH serves
|
|
1891
|
+
* workspace files as `…/api/file?path=…`, so the native path is read back
|
|
1892
|
+
* from the query string; any other image keeps its absolute URL.
|
|
1893
|
+
*/
|
|
1894
|
+
function addressOf(src) {
|
|
1895
|
+
if (typeof src !== 'string' || src === '') return null;
|
|
1896
|
+
if (src.startsWith('data:')) return null;
|
|
1897
|
+
try {
|
|
1898
|
+
const url = new URL(src, location.href);
|
|
1899
|
+
if (url.protocol === 'blob:') return null;
|
|
1900
|
+
if (/\/api\/file$/.test(url.pathname)) {
|
|
1901
|
+
const raw = url.searchParams.get('path');
|
|
1902
|
+
if (raw !== null && raw !== '') return raw;
|
|
1903
|
+
}
|
|
1904
|
+
if (url.protocol === 'dsh-resource:') {
|
|
1905
|
+
// dsh-resource://file/absolute/C:/… and dsh-resource://file/session/<id>/…
|
|
1906
|
+
const match = /^dsh-resource:\/\/file\/(?:absolute\/|session\/[^/]+\/)(.+)$/i.exec(url.href);
|
|
1907
|
+
if (match !== null) return decodeURIComponent(match[1]);
|
|
1908
|
+
}
|
|
1909
|
+
return url.href;
|
|
1910
|
+
} catch {
|
|
1911
|
+
return src;
|
|
1912
|
+
}
|
|
1913
|
+
}
|
|
1914
|
+
|
|
1915
|
+
function copyText(text) {
|
|
1916
|
+
const legacy = () => {
|
|
1917
|
+
try {
|
|
1918
|
+
const area = h('textarea', '', { readonly: '', 'aria-hidden': 'true', [IGNORE_ATTR]: '' });
|
|
1919
|
+
area.value = text;
|
|
1920
|
+
area.style.cssText = 'position:fixed;top:-1000px;left:0;width:1px;height:1px;opacity:0';
|
|
1921
|
+
document.body.appendChild(area);
|
|
1922
|
+
area.select();
|
|
1923
|
+
const ok = typeof document.execCommand === 'function' && document.execCommand('copy');
|
|
1924
|
+
area.remove();
|
|
1925
|
+
return ok === true;
|
|
1926
|
+
} catch {
|
|
1927
|
+
return false;
|
|
1928
|
+
}
|
|
1929
|
+
};
|
|
1930
|
+
const clipboard = navigator.clipboard;
|
|
1931
|
+
if (clipboard === undefined || typeof clipboard.writeText !== 'function') return Promise.resolve(legacy());
|
|
1932
|
+
return clipboard.writeText(text).then(
|
|
1933
|
+
() => true,
|
|
1934
|
+
() => legacy(),
|
|
1935
|
+
);
|
|
1936
|
+
}
|
|
1937
|
+
|
|
1938
|
+
/** Re-encode an image blob as PNG, the only type every clipboard accepts. */
|
|
1939
|
+
function pngOf(blob) {
|
|
1940
|
+
return new Promise((resolve, reject) => {
|
|
1941
|
+
const url = URL.createObjectURL(blob);
|
|
1942
|
+
const img = new Image();
|
|
1943
|
+
const settle = (fn, value) => {
|
|
1944
|
+
URL.revokeObjectURL(url);
|
|
1945
|
+
fn(value);
|
|
1946
|
+
};
|
|
1947
|
+
img.onload = () => {
|
|
1948
|
+
try {
|
|
1949
|
+
const canvas = document.createElement('canvas');
|
|
1950
|
+
canvas.width = img.naturalWidth || img.width;
|
|
1951
|
+
canvas.height = img.naturalHeight || img.height;
|
|
1952
|
+
const ctx = canvas.getContext('2d');
|
|
1953
|
+
if (ctx === null || canvas.width === 0 || canvas.height === 0) throw new Error('no canvas');
|
|
1954
|
+
ctx.drawImage(img, 0, 0);
|
|
1955
|
+
canvas.toBlob(
|
|
1956
|
+
(png) => (png === null ? settle(reject, new Error('encode failed')) : settle(resolve, png)),
|
|
1957
|
+
'image/png',
|
|
1958
|
+
);
|
|
1959
|
+
} catch (error) {
|
|
1960
|
+
settle(reject, error);
|
|
1961
|
+
}
|
|
1962
|
+
};
|
|
1963
|
+
img.onerror = () => settle(reject, new Error('decode failed'));
|
|
1964
|
+
img.src = url;
|
|
1965
|
+
});
|
|
1966
|
+
}
|
|
1967
|
+
|
|
1968
|
+
/** Read the image bytes fresh, so a cross-origin taint cannot reach the clipboard. */
|
|
1969
|
+
async function pngOfSource(src) {
|
|
1970
|
+
if (typeof fetch !== 'function') throw new Error('no fetch');
|
|
1971
|
+
const response = await fetch(src, { credentials: 'same-origin' });
|
|
1972
|
+
if (!response.ok) throw new Error('fetch failed');
|
|
1973
|
+
const blob = await response.blob();
|
|
1974
|
+
return blob.type === 'image/png' ? blob : await pngOf(blob);
|
|
1975
|
+
}
|
|
1976
|
+
|
|
1977
|
+
/**
|
|
1978
|
+
* Copy an image to the system clipboard as PNG.
|
|
1979
|
+
* @returns 'ok' | 'blocked' (cross-origin taint) | 'unsupported' | 'failed'
|
|
1980
|
+
*/
|
|
1981
|
+
async function copyImage(src) {
|
|
1982
|
+
if (typeof window.ClipboardItem !== 'function') return 'unsupported';
|
|
1983
|
+
const clipboard = navigator.clipboard;
|
|
1984
|
+
if (clipboard === undefined || typeof clipboard.write !== 'function') return 'unsupported';
|
|
1985
|
+
try {
|
|
1986
|
+
// Hand over the bytes as a promise, so write() still runs inside the
|
|
1987
|
+
// click's activation window while the image is being fetched.
|
|
1988
|
+
await clipboard.write([new window.ClipboardItem({ 'image/png': pngOfSource(src) })]);
|
|
1989
|
+
return 'ok';
|
|
1990
|
+
} catch {
|
|
1991
|
+
return needsCors(src) ? 'blocked' : 'failed';
|
|
1992
|
+
}
|
|
1993
|
+
}
|
|
1994
|
+
//#endregion
|
|
1995
|
+
|
|
1734
1996
|
//#region annotation editor
|
|
1735
1997
|
const EDIT_ATTR = 'data-qiv-edit';
|
|
1736
1998
|
const COLORS = [
|
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; annotate with pen and shapes, then drop the result into the chat box."
|
|
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; right-click (long-press on touch) to copy the image or its path; annotate with pen and shapes, then drop the result into the chat box."
|
|
5
5
|
}
|
|
6
6
|
}
|
package/locale/zh.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"meta": {
|
|
3
3
|
"title": "图片查看器",
|
|
4
|
-
"description": "点击对话中的图片全屏查看,交互接近 iPhone
|
|
4
|
+
"description": "点击对话中的图片全屏查看,交互接近 iPhone 相册:从缩略图放大打开,双指/双击缩放,惯性拖动,左右滑动切换图片,下滑关闭;右键(手机上长按)可复制图片本身或图片路径;可在图片上画笔、形状标注,完成后自动放进输入框。"
|
|
5
5
|
}
|
|
6
6
|
}
|
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; annotate an image and drop it into the chat box.",
|
|
3
|
+
"version": "0.4.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; right-click to copy the image or its path; 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 && node test/layout.mjs && node test/editor.mjs && node test/link.mjs"
|
|
60
|
+
"test": "node test/smoke.mjs && node test/layout.mjs && node test/editor.mjs && node test/link.mjs && node test/menu.mjs"
|
|
61
61
|
},
|
|
62
62
|
"dshBuild": {
|
|
63
63
|
"host": "src/index.ts",
|