@qzsy/dsh-image-viewer 0.3.1 → 0.4.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 CHANGED
@@ -33,6 +33,17 @@ 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
+
45
+ 触屏与触控笔(`pointer: coarse`,包括 iPad 和手机浏览器)改用底部操作面板:半透明遮罩之上滑出圆角面板,顶部是抓手与图片文件名,下面两行操作加一行“取消”,底部按 `env(safe-area-inset-bottom)` 让开 iPhone 手势条。长按 500ms 触发并带一次轻震动;点遮罩、点“取消”、下拉面板超过 72px 或按 `Esc` 都收起面板,查看器保持打开。
46
+
36
47
  ## 桌面端窗口边缘
37
48
 
38
49
  DSH 桌面应用(Electron)会把 Windows 右上角的窗口按钮、macOS 左上角的红绿灯画在网页之上。查看器和标注编辑器的顶栏按宿主发布的安全边距(`--dsh-frame-overlay-top`,Windows 为标题栏高度 + 20px,macOS 为 68px,原生全屏时降到 20px)下移,不会落在窗口按钮下面;同时图片的最大显示宽度固定为视口的 90%,标注画布同理,任何窗口尺寸下都不会被顶栏或窗口控件遮挡。
@@ -77,6 +88,7 @@ bun run typecheck
77
88
  node test/smoke.mjs # jsdom 冒烟测试:查看器
78
89
  node test/layout.mjs # jsdom 布局测试:90% 宽度上限与桌面窗口留白
79
90
  node test/editor.mjs # jsdom 冒烟测试:标注与注入输入框
91
+ node test/menu.mjs # jsdom 冒烟测试:右键复制图片 / 复制路径
80
92
  ~~~
81
93
 
82
94
  ## 许可
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,15 @@ 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': '当前环境不支持复制图片',
109
+ 'menu.cancel': '取消',
99
110
  };
100
111
  const en = {
101
112
  dialog: 'Image viewer',
@@ -138,6 +149,15 @@ window.__ModuleLoader__.load({
138
149
  'edit.tainted': 'This image comes from another site and the browser blocks exporting it.',
139
150
  'edit.taintedHint': 'Image from another site; export may be blocked.',
140
151
  'edit.exportFailed': 'Could not export the image.',
152
+ 'menu.label': 'Image actions',
153
+ 'menu.copyImage': 'Copy image',
154
+ 'menu.copyPath': 'Copy image path',
155
+ 'menu.copiedImage': 'Image copied',
156
+ 'menu.copiedPath': 'Image path copied',
157
+ 'menu.copyFailed': 'Copy failed',
158
+ 'menu.blocked': 'This image comes from another site and the browser blocks copying it.',
159
+ 'menu.unsupported': 'This environment cannot copy images.',
160
+ 'menu.cancel': 'Cancel',
141
161
  };
142
162
 
143
163
  //#region utilities
@@ -392,6 +412,8 @@ window.__ModuleLoader__.load({
392
412
  ellipse: svg('<ellipse cx="12" cy="12" rx="8.5" ry="6"/>'),
393
413
  arrow: svg('<path d="M5 19L19 5M10 5h9v9"/>'),
394
414
  line: svg('<path d="M5 19L19 5"/>'),
415
+ copy: svg('<rect x="9" y="9" width="11" height="11" rx="2.5"/><path d="M15.5 9V6.5A2.5 2.5 0 0013 4H6.5A2.5 2.5 0 004 6.5V13a2.5 2.5 0 002.5 2.5H9"/>'),
416
+ path: svg('<path d="M10.2 13.8a4.5 4.5 0 006.4 0l2.6-2.6a4.5 4.5 0 00-6.4-6.4l-1.5 1.5"/><path d="M13.8 10.2a4.5 4.5 0 00-6.4 0l-2.6 2.6a4.5 4.5 0 006.4 6.4l1.5-1.5"/>'),
395
417
  };
396
418
  //#endregion
397
419
 
@@ -432,8 +454,26 @@ window.__ModuleLoader__.load({
432
454
  .qiv-thumb:hover{opacity:.8}
433
455
  .qiv-thumb[aria-current=true]{opacity:1;transform:scale(1.1);box-shadow:0 0 0 2px #fff}
434
456
  .qiv-thumb img{display:block;width:100%;height:100%;object-fit:cover;pointer-events:none}
457
+ .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}
458
+ @keyframes qiv-menu-in{from{opacity:0;transform:scale(.96)}to{opacity:1;transform:none}}
459
+ .qiv-menu-item{appearance:none;display:flex;align-items:center;gap:9px;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}
460
+ .qiv-menu-item:hover,.qiv-menu-item:focus-visible{background:rgba(255,255,255,.14);outline:none}
461
+ .qiv-menu-item[disabled]{opacity:.5;cursor:default}
462
+ .qiv-menu-glyph{flex:none;display:grid;place-items:center;opacity:.85}
463
+ .qiv-menu-glyph svg{width:18px;height:18px}
464
+ .qiv-menu-text{min-width:0;overflow:hidden;text-overflow:ellipsis}
465
+ .qiv-sheet-scrim{position:fixed;inset:0;z-index:2147483004;display:flex;align-items:flex-end;justify-content:center;box-sizing:border-box;background:rgba(0,0,0,.42);animation:qiv-scrim-in .18s ease-out}
466
+ @keyframes qiv-scrim-in{from{opacity:0}to{opacity:1}}
467
+ .qiv-sheet{position:relative;left:auto;top:auto;width:100%;min-width:0;max-width:520px;box-sizing:border-box;max-height:min(72vh,520px);overflow-y:auto;overscroll-behavior:contain;touch-action:pan-y;padding:6px 8px calc(env(safe-area-inset-bottom,0px) + 8px);border-radius:18px 18px 0 0;background:rgba(38,38,40,.98);box-shadow:0 -18px 48px rgba(0,0,0,.55),0 0 0 1px rgba(255,255,255,.08);animation:qiv-sheet-in .22s cubic-bezier(.2,.8,.2,1)}
468
+ @keyframes qiv-sheet-in{from{transform:translateY(100%)}to{transform:translateY(0)}}
469
+ .qiv-sheet-grab{width:36px;height:4px;margin:6px auto 8px;border-radius:999px;background:rgba(255,255,255,.28)}
470
+ .qiv-sheet-head{padding:0 10px 8px;font-size:12px;font-weight:600;letter-spacing:.02em;color:rgba(255,255,255,.62);overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
471
+ .qiv-sheet-items{display:flex;flex-direction:column;gap:1px}
472
+ .qiv-sheet .qiv-menu-item{height:46px;padding:0 10px;border-radius:10px;font-size:14px}
473
+ .qiv-sheet-cancel{justify-content:center;height:46px;margin-top:6px;border-radius:10px;background:rgba(255,255,255,.1);font-weight:600}
474
+ .qiv-sheet-cancel:hover,.qiv-sheet-cancel:focus-visible{background:rgba(255,255,255,.18)}
435
475
  .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}}
476
+ @media (prefers-reduced-motion:reduce){.qiv-chrome,.qiv-thumb{transition:none}.qiv-menu,.qiv-sheet,.qiv-sheet-scrim{animation:none}}
437
477
  [${LINK_ATTR}]{cursor:zoom-in}
438
478
  `;
439
479
 
@@ -686,9 +726,18 @@ window.__ModuleLoader__.load({
686
726
  let destroyed = false;
687
727
  /** Open annotation editor; the viewer pauses keyboard input under it. */
688
728
  let editor = null;
729
+ /** Open context menu, or null. It lives in <body>, above the viewer. */
730
+ let menu = null;
731
+ /** Dimmed backdrop of the bottom sheet, or null. */
732
+ let menuScrim = null;
733
+ /** Last pointer kind on the image: touch/pen menus open as a bottom sheet. */
734
+ let menuTouch = false;
735
+ /** Ignore the contextmenu that touch screens fire after a long press. */
736
+ let suppressContextUntil = 0;
689
737
  let gesture = null;
690
738
  let samples = [];
691
739
  let tapTimer = 0;
740
+ let longPress = 0;
692
741
  let lastTap = null;
693
742
  let lastZoomed = null;
694
743
  let hiddenItem = null;
@@ -744,6 +793,160 @@ window.__ModuleLoader__.load({
744
793
  strip.append(inner);
745
794
  root.append(strip);
746
795
  }
796
+ //#region context menu
797
+ function closeMenu(refocus) {
798
+ if (menuScrim !== null) {
799
+ menuScrim.remove();
800
+ menuScrim = null;
801
+ }
802
+ if (menu === null) return;
803
+ const node = menu;
804
+ menu = null;
805
+ node.remove();
806
+ if (refocus === true && !closing && !destroyed) {
807
+ try {
808
+ root.focus({ preventScroll: true });
809
+ } catch {
810
+ // focus is best-effort
811
+ }
812
+ }
813
+ }
814
+
815
+ function menuItem(label, run, icon) {
816
+ const button = h('button', 'qiv-menu-item', { type: 'button', role: 'menuitem', tabindex: '-1' });
817
+ if (icon) {
818
+ const glyph = h('span', 'qiv-menu-glyph');
819
+ glyph.innerHTML = icon;
820
+ button.append(glyph);
821
+ }
822
+ const text = h('span', 'qiv-menu-text');
823
+ text.textContent = label;
824
+ button.append(text);
825
+ button.addEventListener('click', () => {
826
+ if (menu === null) return;
827
+ run(button);
828
+ });
829
+ return button;
830
+ }
831
+
832
+ function menuAction(kind) {
833
+ const item = items[index];
834
+ return async (button) => {
835
+ button.disabled = true;
836
+ closeMenu();
837
+ if (kind === 'copy') {
838
+ const result = await copyImage(item.src);
839
+ toast(t(result === 'ok' ? 'menu.copiedImage' : result === 'blocked' ? 'menu.blocked' : result === 'unsupported' ? 'menu.unsupported' : 'menu.copyFailed'));
840
+ return;
841
+ }
842
+ const address = addressOf(item.src);
843
+ const ok = address !== null && (await copyText(address));
844
+ toast(t(ok ? 'menu.copiedPath' : 'menu.copyFailed'));
845
+ };
846
+ }
847
+
848
+ /**
849
+ * Slide the sheet away on a downward drag, like a native action sheet.
850
+ * Runs before the tap logic because the sheet swallows its own pointers.
851
+ */
852
+ function sheetDrag(node, scrim, cancel) {
853
+ let drag = null;
854
+ node.addEventListener('pointerdown', (e) => {
855
+ if (e.pointerType === 'mouse' && e.button !== 0) return;
856
+ if (e.target instanceof Element && e.target.closest('.qiv-menu-item') !== null) return;
857
+ drag = { id: e.pointerId, y: e.clientY, dy: 0, t: now() };
858
+ try {
859
+ node.setPointerCapture(e.pointerId);
860
+ } catch {
861
+ // capture is best-effort
862
+ }
863
+ });
864
+ node.addEventListener('pointermove', (e) => {
865
+ if (drag === null || e.pointerId !== drag.id) return;
866
+ drag.dy = Math.max(0, e.clientY - drag.y);
867
+ node.style.animation = 'none';
868
+ node.style.transform = `translateY(${drag.dy}px)`;
869
+ scrim.style.opacity = String(clamp(1 - drag.dy / Math.max(160, vh * 0.32), 0.2, 1));
870
+ });
871
+ const end = (e) => {
872
+ if (drag === null || e.pointerId !== drag.id) return;
873
+ const dy = drag.dy;
874
+ const v = dy / Math.max(1, now() - drag.t);
875
+ drag = null;
876
+ try {
877
+ node.releasePointerCapture(e.pointerId);
878
+ } catch {
879
+ // already released
880
+ }
881
+ if (dy > 72 || v > 0.5) {
882
+ closeMenu(true);
883
+ return;
884
+ }
885
+ node.style.transform = '';
886
+ scrim.style.opacity = '';
887
+ };
888
+ node.addEventListener('pointerup', end);
889
+ node.addEventListener('pointercancel', end);
890
+ cancel.addEventListener('click', () => closeMenu(true));
891
+ }
892
+
893
+ /**
894
+ * Open the image actions. Right click on a pointer device anchors a popover
895
+ * at the cursor; touch and pen get a bottom sheet with a scrim instead.
896
+ */
897
+ function openMenu(x, y, sheet) {
898
+ closeMenu();
899
+ const node = sheet === true ? sheetNode() : anchoredNode(x, y);
900
+ document.body.append(node);
901
+ menu = node.classList.contains('qiv-menu') ? node : node.querySelector('.qiv-menu');
902
+ const first = menu.querySelector('.qiv-menu-item');
903
+ try {
904
+ first.focus({ preventScroll: true });
905
+ } catch {
906
+ // focus is best-effort
907
+ }
908
+ }
909
+
910
+ function anchoredNode(x, y) {
911
+ const node = h('div', 'qiv-menu', { role: 'menu', 'aria-label': t('menu.label') });
912
+ node.append(menuItem(t('menu.copyImage'), menuAction('copy'), ICON.copy), menuItem(t('menu.copyPath'), menuAction('path'), ICON.path));
913
+ // Keep the popover on screen: clamp after it has its own size.
914
+ const rect = node.getBoundingClientRect();
915
+ node.style.left = clamp(x, 8, Math.max(8, window.innerWidth - rect.width - 8)) + 'px';
916
+ node.style.top = clamp(y, 8, Math.max(8, window.innerHeight - rect.height - 8)) + 'px';
917
+ return node;
918
+ }
919
+
920
+ function sheetNode() {
921
+ const scrim = h('div', 'qiv-sheet-scrim');
922
+ const node = h('div', 'qiv-menu qiv-sheet', { role: 'menu', 'aria-label': t('menu.label'), tabindex: '-1' });
923
+ const head = h('div', 'qiv-sheet-head');
924
+ head.textContent = fileName(items[index].src) || t('menu.label');
925
+ const body = h('div', 'qiv-sheet-items');
926
+ body.append(menuItem(t('menu.copyImage'), menuAction('copy'), ICON.copy), menuItem(t('menu.copyPath'), menuAction('path'), ICON.path));
927
+ const cancel = menuItem(t('menu.cancel'), () => {});
928
+ cancel.classList.add('qiv-sheet-cancel');
929
+ node.append(h('div', 'qiv-sheet-grab'), head, body, cancel);
930
+ scrim.append(node);
931
+ menuScrim = scrim;
932
+ sheetDrag(node, scrim, cancel);
933
+ return scrim;
934
+ }
935
+
936
+ function onDocDown(e) {
937
+ if (menu === null) return;
938
+ if (e.target instanceof Node && menu.contains(e.target)) return;
939
+ closeMenu();
940
+ }
941
+
942
+ root.addEventListener('contextmenu', (e) => {
943
+ if (closing || destroyed || editor !== null) return;
944
+ e.preventDefault();
945
+ // A long press already opened the sheet; ignore the follow-up event.
946
+ if (menu !== null || now() < suppressContextUntil) return;
947
+ openMenu(e.clientX || vw / 2, e.clientY || vh / 2, menuTouch || coarse.matches);
948
+ });
949
+ document.addEventListener('pointerdown', onDocDown, true);
747
950
  //#endregion
748
951
 
749
952
  //#region geometry
@@ -957,6 +1160,7 @@ window.__ModuleLoader__.load({
957
1160
  }
958
1161
 
959
1162
  function updateChrome() {
1163
+ closeMenu();
960
1164
  const item = items[index];
961
1165
  const text = n > 1 ? t('counter', { i: index + 1, n }) : '';
962
1166
  counter.textContent = text;
@@ -1102,6 +1306,11 @@ window.__ModuleLoader__.load({
1102
1306
  lastTap = null;
1103
1307
  }
1104
1308
 
1309
+ function clearLongPress() {
1310
+ if (longPress !== 0) clearTimeout(longPress);
1311
+ longPress = 0;
1312
+ }
1313
+
1105
1314
  function velocity() {
1106
1315
  const tNow = now();
1107
1316
  const recent = samples.filter((p) => tNow - p.t <= 100);
@@ -1125,6 +1334,7 @@ window.__ModuleLoader__.load({
1125
1334
 
1126
1335
  function startPinch() {
1127
1336
  clearTap();
1337
+ clearLongPress();
1128
1338
  const [a, b] = pointPair();
1129
1339
  gesture = {
1130
1340
  mode: 'pinch',
@@ -1174,10 +1384,32 @@ window.__ModuleLoader__.load({
1174
1384
  // capture is best-effort
1175
1385
  }
1176
1386
  const p = { x: e.clientX, y: e.clientY, type: e.pointerType || 'mouse' };
1387
+ menuTouch = p.type !== 'mouse';
1177
1388
  pointers.set(e.pointerId, p);
1178
1389
  anim.stop(['x', 'y', 'scale', 'bg', 'alpha', 'trackX']);
1179
- if (pointers.size === 1) startSingle(e.pointerId, p);
1180
- else if (pointers.size === 2) startPinch();
1390
+ if (pointers.size === 1) {
1391
+ startSingle(e.pointerId, p);
1392
+ // Touch screens have no right click: a still finger opens the actions.
1393
+ if (p.type !== 'mouse') {
1394
+ const x = p.x;
1395
+ const y = p.y;
1396
+ clearLongPress();
1397
+ longPress = setTimeout(() => {
1398
+ longPress = 0;
1399
+ if (gesture === null || gesture.mode !== 'pending' || pointers.size !== 1) return;
1400
+ gesture.mode = 'menu';
1401
+ setGesture(null);
1402
+ suppressContextUntil = now() + 900;
1403
+ try {
1404
+ // A short tick confirms the press; iOS Safari has no vibrate.
1405
+ window.navigator.vibrate?.(10);
1406
+ } catch {
1407
+ // haptics are best-effort
1408
+ }
1409
+ openMenu(x, y, true);
1410
+ }, LONG_PRESS_MS);
1411
+ }
1412
+ } else if (pointers.size === 2) startPinch();
1181
1413
  }
1182
1414
 
1183
1415
  function movePinch() {
@@ -1223,6 +1455,7 @@ window.__ModuleLoader__.load({
1223
1455
  let dy = p.y - g.sy;
1224
1456
  if (g.mode === 'pending') {
1225
1457
  if (Math.hypot(dx, dy) < SLOP) return;
1458
+ clearLongPress();
1226
1459
  clearTap();
1227
1460
  g.mode = classify(dx, dy);
1228
1461
  // Rebase so the drag starts from here instead of jumping by SLOP.
@@ -1323,6 +1556,7 @@ window.__ModuleLoader__.load({
1323
1556
  }
1324
1557
 
1325
1558
  function onUp(e) {
1559
+ clearLongPress();
1326
1560
  if (!pointers.has(e.pointerId)) return;
1327
1561
  pointers.delete(e.pointerId);
1328
1562
  try {
@@ -1332,6 +1566,12 @@ window.__ModuleLoader__.load({
1332
1566
  }
1333
1567
  const g = gesture;
1334
1568
  if (g === null) return;
1569
+ if (g.mode === 'menu') {
1570
+ // The press became a long-press menu: it is not a tap.
1571
+ gesture = null;
1572
+ setGesture(null);
1573
+ return;
1574
+ }
1335
1575
  const cancelled = e.type === 'pointercancel';
1336
1576
  if (g.mode === 'pinch') {
1337
1577
  if (pointers.size === 1) {
@@ -1378,6 +1618,7 @@ window.__ModuleLoader__.load({
1378
1618
  //#region wheel + keyboard + resize
1379
1619
  function onWheel(e) {
1380
1620
  e.preventDefault();
1621
+ closeMenu();
1381
1622
  if (closing) return;
1382
1623
  const unit = e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? vh : 1;
1383
1624
  const dx = e.deltaX * unit;
@@ -1432,6 +1673,29 @@ window.__ModuleLoader__.load({
1432
1673
 
1433
1674
  function onKey(e) {
1434
1675
  if (closing || destroyed || editor !== null) return;
1676
+ if (menu !== null) {
1677
+ // The menu owns its keys: Escape/Tab dismiss, arrows move, Enter presses.
1678
+ if (e.key === 'Escape' || e.key === 'Tab') {
1679
+ e.preventDefault();
1680
+ e.stopPropagation();
1681
+ closeMenu(true);
1682
+ return;
1683
+ }
1684
+ if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
1685
+ e.preventDefault();
1686
+ e.stopPropagation();
1687
+ const list = [...menu.querySelectorAll('.qiv-menu-item')];
1688
+ const at = list.indexOf(document.activeElement);
1689
+ list[(at + (e.key === 'ArrowDown' ? 1 : -1) + list.length) % list.length].focus();
1690
+ return;
1691
+ }
1692
+ if (e.key === 'Enter' || e.key === ' ') {
1693
+ e.preventDefault();
1694
+ e.stopPropagation();
1695
+ if (document.activeElement instanceof HTMLElement) document.activeElement.click();
1696
+ return;
1697
+ }
1698
+ }
1435
1699
  if (e.ctrlKey || e.metaKey || e.altKey) return;
1436
1700
  let handled = true;
1437
1701
  switch (e.key) {
@@ -1467,6 +1731,7 @@ window.__ModuleLoader__.load({
1467
1731
  }
1468
1732
 
1469
1733
  function onResize() {
1734
+ closeMenu();
1470
1735
  vw = root.clientWidth || window.innerWidth;
1471
1736
  vh = root.clientHeight || window.innerHeight;
1472
1737
  // Native fullscreen drops the shell's chrome strip, so re-read it.
@@ -1487,6 +1752,8 @@ window.__ModuleLoader__.load({
1487
1752
  if (closing || destroyed) return;
1488
1753
  closing = true;
1489
1754
  clearTap();
1755
+ clearLongPress();
1756
+ closeMenu();
1490
1757
  root.dataset.state = 'closing';
1491
1758
  root.style.pointerEvents = 'none';
1492
1759
  pointers.clear();
@@ -1522,7 +1789,10 @@ window.__ModuleLoader__.load({
1522
1789
  }
1523
1790
  anim.destroy();
1524
1791
  clearTap();
1792
+ clearLongPress();
1525
1793
  showSource();
1794
+ closeMenu();
1795
+ document.removeEventListener('pointerdown', onDocDown, true);
1526
1796
  window.removeEventListener('keydown', onKey, true);
1527
1797
  window.removeEventListener('resize', onResize);
1528
1798
  root.remove();
@@ -1553,6 +1823,7 @@ window.__ModuleLoader__.load({
1553
1823
  function openEdit() {
1554
1824
  if (closing || destroyed || editor !== null) return;
1555
1825
  clearTap();
1826
+ closeMenu();
1556
1827
  editor = openEditor(items[index], t, {
1557
1828
  cancel: () => {
1558
1829
  editor = null;
@@ -1731,6 +2002,114 @@ window.__ModuleLoader__.load({
1731
2002
  }
1732
2003
  //#endregion
1733
2004
 
2005
+ //#region clipboard
2006
+ /**
2007
+ * The file on disk behind an image URL, when there is one. DSH serves
2008
+ * workspace files as `…/api/file?path=…`, so the native path is read back
2009
+ * from the query string; any other image keeps its absolute URL.
2010
+ */
2011
+ function addressOf(src) {
2012
+ if (typeof src !== 'string' || src === '') return null;
2013
+ if (src.startsWith('data:')) return null;
2014
+ try {
2015
+ const url = new URL(src, location.href);
2016
+ if (url.protocol === 'blob:') return null;
2017
+ if (/\/api\/file$/.test(url.pathname)) {
2018
+ const raw = url.searchParams.get('path');
2019
+ if (raw !== null && raw !== '') return raw;
2020
+ }
2021
+ if (url.protocol === 'dsh-resource:') {
2022
+ // dsh-resource://file/absolute/C:/… and dsh-resource://file/session/<id>/…
2023
+ const match = /^dsh-resource:\/\/file\/(?:absolute\/|session\/[^/]+\/)(.+)$/i.exec(url.href);
2024
+ if (match !== null) return decodeURIComponent(match[1]);
2025
+ }
2026
+ return url.href;
2027
+ } catch {
2028
+ return src;
2029
+ }
2030
+ }
2031
+
2032
+ function copyText(text) {
2033
+ const legacy = () => {
2034
+ try {
2035
+ const area = h('textarea', '', { readonly: '', 'aria-hidden': 'true', [IGNORE_ATTR]: '' });
2036
+ area.value = text;
2037
+ area.style.cssText = 'position:fixed;top:-1000px;left:0;width:1px;height:1px;opacity:0';
2038
+ document.body.appendChild(area);
2039
+ area.select();
2040
+ const ok = typeof document.execCommand === 'function' && document.execCommand('copy');
2041
+ area.remove();
2042
+ return ok === true;
2043
+ } catch {
2044
+ return false;
2045
+ }
2046
+ };
2047
+ const clipboard = navigator.clipboard;
2048
+ if (clipboard === undefined || typeof clipboard.writeText !== 'function') return Promise.resolve(legacy());
2049
+ return clipboard.writeText(text).then(
2050
+ () => true,
2051
+ () => legacy(),
2052
+ );
2053
+ }
2054
+
2055
+ /** Re-encode an image blob as PNG, the only type every clipboard accepts. */
2056
+ function pngOf(blob) {
2057
+ return new Promise((resolve, reject) => {
2058
+ const url = URL.createObjectURL(blob);
2059
+ const img = new Image();
2060
+ const settle = (fn, value) => {
2061
+ URL.revokeObjectURL(url);
2062
+ fn(value);
2063
+ };
2064
+ img.onload = () => {
2065
+ try {
2066
+ const canvas = document.createElement('canvas');
2067
+ canvas.width = img.naturalWidth || img.width;
2068
+ canvas.height = img.naturalHeight || img.height;
2069
+ const ctx = canvas.getContext('2d');
2070
+ if (ctx === null || canvas.width === 0 || canvas.height === 0) throw new Error('no canvas');
2071
+ ctx.drawImage(img, 0, 0);
2072
+ canvas.toBlob(
2073
+ (png) => (png === null ? settle(reject, new Error('encode failed')) : settle(resolve, png)),
2074
+ 'image/png',
2075
+ );
2076
+ } catch (error) {
2077
+ settle(reject, error);
2078
+ }
2079
+ };
2080
+ img.onerror = () => settle(reject, new Error('decode failed'));
2081
+ img.src = url;
2082
+ });
2083
+ }
2084
+
2085
+ /** Read the image bytes fresh, so a cross-origin taint cannot reach the clipboard. */
2086
+ async function pngOfSource(src) {
2087
+ if (typeof fetch !== 'function') throw new Error('no fetch');
2088
+ const response = await fetch(src, { credentials: 'same-origin' });
2089
+ if (!response.ok) throw new Error('fetch failed');
2090
+ const blob = await response.blob();
2091
+ return blob.type === 'image/png' ? blob : await pngOf(blob);
2092
+ }
2093
+
2094
+ /**
2095
+ * Copy an image to the system clipboard as PNG.
2096
+ * @returns 'ok' | 'blocked' (cross-origin taint) | 'unsupported' | 'failed'
2097
+ */
2098
+ async function copyImage(src) {
2099
+ if (typeof window.ClipboardItem !== 'function') return 'unsupported';
2100
+ const clipboard = navigator.clipboard;
2101
+ if (clipboard === undefined || typeof clipboard.write !== 'function') return 'unsupported';
2102
+ try {
2103
+ // Hand over the bytes as a promise, so write() still runs inside the
2104
+ // click's activation window while the image is being fetched.
2105
+ await clipboard.write([new window.ClipboardItem({ 'image/png': pngOfSource(src) })]);
2106
+ return 'ok';
2107
+ } catch {
2108
+ return needsCors(src) ? 'blocked' : 'failed';
2109
+ }
2110
+ }
2111
+ //#endregion
2112
+
1734
2113
  //#region annotation editor
1735
2114
  const EDIT_ATTR = 'data-qiv-edit';
1736
2115
  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 or long-press on touch to copy the image or its path (a bottom sheet on touch); 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.3.1",
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.1",
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 or long-press to copy the image or its path (a bottom sheet on touch); 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",