@qzsy/dsh-image-viewer 0.4.0 → 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,14 +33,16 @@ dsh plugin add @qzsy/dsh-image-viewer
33
33
 
34
34
  系统开启“减少动态效果”时,所有动画直接跳到终态。
35
35
 
36
- ## 右键菜单
36
+ ## 右键菜单 / 长按操作面板
37
37
 
38
- 在预览里右键(手机上长按)任意图片,弹出两项操作:
38
+ 在预览里右键任意图片,弹出两项操作(触屏上长按图片,从底部滑出操作面板):
39
39
 
40
40
  - **复制图片**:把图片以 PNG 写进系统剪贴板,可以直接粘贴到聊天框、文档或设计工具。原图不是 PNG 时会先转码;来自其他站点、浏览器禁止跨域读取的图片会给出提示。
41
41
  - **复制图片路径**:复制图片在磁盘上的真实路径(例如 `D:\shots\a.png`);不是本地文件(外链、data: 图片)时复制完整 URL。
42
42
 
43
- 菜单打开后 `Esc` 或 `Tab` 收起菜单(`Esc` 再按一次才关闭查看器),`↑` `↓` 在菜单项间移动,`Enter` 执行;点击菜单外任意位置、滚轮、翻页或窗口尺寸变化都会自动收起。
43
+ 桌面端是贴着鼠标的浮层菜单;`Esc` 或 `Tab` 收起菜单(`Esc` 再按一次才关闭查看器),`↑` `↓` 在菜单项间移动,`Enter` 执行,点击菜单外任意位置、滚轮、翻页或窗口尺寸变化都会自动收起。
44
+
45
+ 触屏与触控笔(`pointer: coarse`,包括 iPad 和手机浏览器)改用底部操作面板:半透明遮罩之上滑出圆角面板,顶部是抓手与图片文件名,下面两行操作加一行“取消”,底部按 `env(safe-area-inset-bottom)` 让开 iPhone 手势条。长按 500ms 触发并带一次轻震动;点遮罩、点“取消”、下拉面板超过 72px 或按 `Esc` 都收起面板,查看器保持打开。
44
46
 
45
47
  ## 桌面端窗口边缘
46
48
 
package/dist/client.js CHANGED
@@ -106,6 +106,7 @@ window.__ModuleLoader__.load({
106
106
  'menu.copyFailed': '复制失败',
107
107
  'menu.blocked': '图片来自其他站点,浏览器不允许复制',
108
108
  'menu.unsupported': '当前环境不支持复制图片',
109
+ 'menu.cancel': '取消',
109
110
  };
110
111
  const en = {
111
112
  dialog: 'Image viewer',
@@ -156,6 +157,7 @@ window.__ModuleLoader__.load({
156
157
  'menu.copyFailed': 'Copy failed',
157
158
  'menu.blocked': 'This image comes from another site and the browser blocks copying it.',
158
159
  'menu.unsupported': 'This environment cannot copy images.',
160
+ 'menu.cancel': 'Cancel',
159
161
  };
160
162
 
161
163
  //#region utilities
@@ -410,6 +412,8 @@ window.__ModuleLoader__.load({
410
412
  ellipse: svg('<ellipse cx="12" cy="12" rx="8.5" ry="6"/>'),
411
413
  arrow: svg('<path d="M5 19L19 5M10 5h9v9"/>'),
412
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"/>'),
413
417
  };
414
418
  //#endregion
415
419
 
@@ -452,11 +456,24 @@ window.__ModuleLoader__.load({
452
456
  .qiv-thumb img{display:block;width:100%;height:100%;object-fit:cover;pointer-events:none}
453
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}
454
458
  @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}
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}
456
460
  .qiv-menu-item:hover,.qiv-menu-item:focus-visible{background:rgba(255,255,255,.14);outline:none}
457
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)}
458
475
  .qiv-sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
459
- @media (prefers-reduced-motion:reduce){.qiv-chrome,.qiv-thumb{transition:none}.qiv-menu{animation:none}}
476
+ @media (prefers-reduced-motion:reduce){.qiv-chrome,.qiv-thumb{transition:none}.qiv-menu,.qiv-sheet,.qiv-sheet-scrim{animation:none}}
460
477
  [${LINK_ATTR}]{cursor:zoom-in}
461
478
  `;
462
479
 
@@ -711,6 +728,12 @@ window.__ModuleLoader__.load({
711
728
  let editor = null;
712
729
  /** Open context menu, or null. It lives in <body>, above the viewer. */
713
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;
714
737
  let gesture = null;
715
738
  let samples = [];
716
739
  let tapTimer = 0;
@@ -772,6 +795,10 @@ window.__ModuleLoader__.load({
772
795
  }
773
796
  //#region context menu
774
797
  function closeMenu(refocus) {
798
+ if (menuScrim !== null) {
799
+ menuScrim.remove();
800
+ menuScrim = null;
801
+ }
775
802
  if (menu === null) return;
776
803
  const node = menu;
777
804
  menu = null;
@@ -785,9 +812,16 @@ window.__ModuleLoader__.load({
785
812
  }
786
813
  }
787
814
 
788
- function menuItem(label, run) {
815
+ function menuItem(label, run, icon) {
789
816
  const button = h('button', 'qiv-menu-item', { type: 'button', role: 'menuitem', tabindex: '-1' });
790
- button.textContent = label;
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);
791
825
  button.addEventListener('click', () => {
792
826
  if (menu === null) return;
793
827
  run(button);
@@ -795,37 +829,110 @@ window.__ModuleLoader__.load({
795
829
  return button;
796
830
  }
797
831
 
798
- function openMenu(x, y) {
799
- closeMenu();
832
+ function menuAction(kind) {
800
833
  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) => {
834
+ return async (button) => {
809
835
  button.disabled = true;
810
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
+ }
811
842
  const address = addressOf(item.src);
812
843
  const ok = address !== null && (await copyText(address));
813
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
+ }
814
863
  });
815
- node.append(copy, path);
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);
816
900
  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';
901
+ menu = node.classList.contains('qiv-menu') ? node : node.querySelector('.qiv-menu');
902
+ const first = menu.querySelector('.qiv-menu-item');
822
903
  try {
823
- copy.focus({ preventScroll: true });
904
+ first.focus({ preventScroll: true });
824
905
  } catch {
825
906
  // focus is best-effort
826
907
  }
827
908
  }
828
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
+
829
936
  function onDocDown(e) {
830
937
  if (menu === null) return;
831
938
  if (e.target instanceof Node && menu.contains(e.target)) return;
@@ -835,7 +942,9 @@ window.__ModuleLoader__.load({
835
942
  root.addEventListener('contextmenu', (e) => {
836
943
  if (closing || destroyed || editor !== null) return;
837
944
  e.preventDefault();
838
- openMenu(e.clientX || vw / 2, e.clientY || vh / 2);
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);
839
948
  });
840
949
  document.addEventListener('pointerdown', onDocDown, true);
841
950
  //#endregion
@@ -1275,11 +1384,12 @@ window.__ModuleLoader__.load({
1275
1384
  // capture is best-effort
1276
1385
  }
1277
1386
  const p = { x: e.clientX, y: e.clientY, type: e.pointerType || 'mouse' };
1387
+ menuTouch = p.type !== 'mouse';
1278
1388
  pointers.set(e.pointerId, p);
1279
1389
  anim.stop(['x', 'y', 'scale', 'bg', 'alpha', 'trackX']);
1280
1390
  if (pointers.size === 1) {
1281
1391
  startSingle(e.pointerId, p);
1282
- // Touch screens have no right click: a still finger opens the same menu.
1392
+ // Touch screens have no right click: a still finger opens the actions.
1283
1393
  if (p.type !== 'mouse') {
1284
1394
  const x = p.x;
1285
1395
  const y = p.y;
@@ -1289,7 +1399,14 @@ window.__ModuleLoader__.load({
1289
1399
  if (gesture === null || gesture.mode !== 'pending' || pointers.size !== 1) return;
1290
1400
  gesture.mode = 'menu';
1291
1401
  setGesture(null);
1292
- openMenu(x, y);
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);
1293
1410
  }, LONG_PRESS_MS);
1294
1411
  }
1295
1412
  } else if (pointers.size === 2) startPinch();
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; 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."
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.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.",
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",