@qzsy/dsh-image-viewer 0.2.1 → 0.3.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.
Files changed (3) hide show
  1. package/README.md +12 -2
  2. package/dist/client.js +147 -7
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -48,16 +48,26 @@ DSH 桌面应用(Electron)会把 Windows 右上角的窗口按钮、macOS
48
48
  - 点 ✓ 后导出 PNG(超过 8 MB 改为 JPEG),以粘贴的方式放进对话输入框,查看器关闭,光标留在输入框,补充提示词后即可发送
49
49
  - 找不到输入框时改为下载图片;来自其他站点且不允许跨域的图片,浏览器会禁止导出,会给出提示
50
50
 
51
+ ## 图片文件链接
52
+
53
+ 消息里的 Markdown 链接如果指向图片文件(例如 `[选品中心](docs/acceptance/prod/mobile-leader-products.png)`),点击也走这个查看器,而不是打开文件面板:
54
+
55
+ - 悬停时鼠标变成放大镜,提示这里可以点开看图
56
+ - 同一段消息里的多个图片链接组成一个相册,可以左右滑动切换
57
+ - 图片地址直接复用 DSH 已解析好的文件地址;桌面端把 `dsh-app://` 地址改写成同源 http 地址,标注后导出不会被 canvas 跨域拦下
58
+ - 目标不是图片的普通文件链接、以及输入框里的 `@文件` 引用,仍按原样打开
59
+
51
60
  ## 工作方式
52
61
 
53
62
  插件只有客户端部分,不注册任何 Host 能力,不发起网络请求。它在 `document` 捕获阶段监听点击:
54
63
 
55
64
  - 带 `cursor: zoom-in` 的按钮(对话中的 Markdown 图片、消息附件图片、输入框附件缩略图)
65
+ - 指向图片文件的文件链接按钮(见上一节)
56
66
  - 或面积不小于 96×64 的裸 `<img>`
57
67
 
58
- 命中后阻止内置查看器,并收集同一滚动区域内的全部可预览图片作为相册。
68
+ 命中后阻止内置查看器(或文件面板),并收集同一滚动区域内的全部可预览图片作为相册。
59
69
 
60
- 以下情况保持原样:按住 Ctrl / ⌘ / Shift / Alt 点击、图片在链接内、位于导航栏 / 工具栏 / 菜单中、在可编辑区域内,或元素(及其祖先)带有 `data-qiv-ignore` 属性。
70
+ 以下情况保持原样:按住 Ctrl / ⌘ / Shift / Alt 点击、图片在链接内、位于导航栏 / 工具栏 / 菜单中、在可编辑区域内或输入框里,或元素(及其祖先)带有 `data-qiv-ignore` 属性。
61
71
 
62
72
  ## 开发
63
73
 
package/dist/client.js CHANGED
@@ -29,6 +29,8 @@ window.__ModuleLoader__.load({
29
29
  const ROOT_ATTR = 'data-qiv-root';
30
30
  const STYLE_ATTR = 'data-qiv-style';
31
31
  const IGNORE_ATTR = 'data-qiv-ignore';
32
+ /** Attribute set on image file links so they read as zoomable. */
33
+ const LINK_ATTR = 'data-qiv-link';
32
34
 
33
35
  /** Horizontal gap between slides, in CSS px. */
34
36
  const GAP = 24;
@@ -432,6 +434,7 @@ window.__ModuleLoader__.load({
432
434
  .qiv-thumb img{display:block;width:100%;height:100%;object-fit:cover;pointer-events:none}
433
435
  .qiv-sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
434
436
  @media (prefers-reduced-motion:reduce){.qiv-chrome,.qiv-thumb{transition:none}}
437
+ [${LINK_ATTR}]{cursor:zoom-in}
435
438
  `;
436
439
 
437
440
  function installStyles() {
@@ -456,6 +459,74 @@ window.__ModuleLoader__.load({
456
459
 
457
460
  const srcOf = (img) => img.currentSrc || img.src || '';
458
461
 
462
+ /**
463
+ * Desktop resolves workspace files to `dsh-app://app/api/file?…`. When the
464
+ * page itself is served over http(s), prefer that same origin so the canvas
465
+ * used by the annotation editor is not tainted and stays exportable.
466
+ */
467
+ function normalizeSrc(src) {
468
+ try {
469
+ if (src.startsWith('dsh-app://app/')) {
470
+ const base = document.baseURI;
471
+ if (/^https?:/i.test(base)) return new URL(src.slice('dsh-app://app/'.length), base).href;
472
+ }
473
+ } catch {
474
+ // keep the original
475
+ }
476
+ return src;
477
+ }
478
+
479
+ function fiberOf(node) {
480
+ for (const key of Object.keys(node)) {
481
+ if (key.startsWith('__reactFiber$') || key.startsWith('__reactInternalInstance$')) return node[key];
482
+ }
483
+ return null;
484
+ }
485
+
486
+ /** Depth-limited scan of a React element tree for a string `src` prop. */
487
+ function srcInElement(value, depth = 0) {
488
+ if (depth > 6 || value === null || value === undefined || typeof value !== 'object') return null;
489
+ const props = value.props;
490
+ if (props !== undefined && props !== null && typeof props === 'object') {
491
+ if (typeof props.src === 'string' && props.src !== '') return props.src;
492
+ const nested = srcInElement(props.children, depth + 1);
493
+ if (nested !== null) return nested;
494
+ }
495
+ if (Array.isArray(value)) {
496
+ for (const child of value) {
497
+ const found = srcInElement(child, depth + 1);
498
+ if (found !== null) return found;
499
+ }
500
+ }
501
+ return null;
502
+ }
503
+
504
+ /**
505
+ * DSH renders a markdown link that points at a workspace file as a
506
+ * `<button class="…fileLink">` with no href; clicking it opens the file
507
+ * panel. For image files the hover card wrapped around that button is
508
+ * built eagerly with the already-resolved image URL, so the URL can be read
509
+ * from the React fiber props instead of re-implementing the app's
510
+ * workspace-relative path resolution. Returns null for anything else.
511
+ */
512
+ function imageLinkOf(host) {
513
+ try {
514
+ if (host.getElementsByTagName('img').length !== 0) return null;
515
+ let fiber = fiberOf(host);
516
+ for (let up = 0; up < 14 && fiber !== null && fiber !== undefined; up += 1) {
517
+ const props = fiber.memoizedProps;
518
+ if (props !== null && props !== undefined && typeof props === 'object') {
519
+ const src = srcInElement(props.content);
520
+ if (src !== null) return { src: normalizeSrc(src), label: (host.textContent || '').trim() };
521
+ }
522
+ fiber = fiber.return;
523
+ }
524
+ } catch {
525
+ return null;
526
+ }
527
+ return null;
528
+ }
529
+
459
530
  /**
460
531
  * Map a click target to a previewable image, or null to let the click
461
532
  * through. Buttons qualify only when they wrap exactly one image and show
@@ -470,11 +541,18 @@ window.__ModuleLoader__.load({
470
541
  if (host !== null) {
471
542
  if (host.closest('a[href]') !== null) return null;
472
543
  const imgs = host.getElementsByTagName('img');
473
- if (imgs.length !== 1) return null;
474
- const img = imgs[0];
475
- if (!isZoomCursor(host) && !isZoomCursor(img)) return null;
476
- if (srcOf(img) === '') return null;
477
- return { host, img };
544
+ if (imgs.length === 1) {
545
+ const img = imgs[0];
546
+ if (!isZoomCursor(host) && !isZoomCursor(img)) return null;
547
+ if (srcOf(img) === '') return null;
548
+ return { host, img };
549
+ }
550
+ // A markdown link to an image file: the app would open the file panel.
551
+ if (imgs.length === 0 && host.closest('[data-composer-seat]') === null) {
552
+ const link = imageLinkOf(host);
553
+ if (link !== null) return { host, kind: 'link', src: link.src, label: link.label };
554
+ }
555
+ return null;
478
556
  }
479
557
  if (node.tagName !== 'IMG') return null;
480
558
  const img = node;
@@ -503,12 +581,31 @@ window.__ModuleLoader__.load({
503
581
  }
504
582
 
505
583
  function makeItem(trigger, scope) {
584
+ if (trigger.kind === 'link') {
585
+ // A link chip is not a thumbnail: it keeps its own size until the image
586
+ // loads, and it stays visible behind the viewer.
587
+ return {
588
+ el: trigger.host,
589
+ host: trigger.host,
590
+ src: trigger.src,
591
+ alt: trigger.label || '',
592
+ referrerPolicy: 'no-referrer',
593
+ w: 0,
594
+ h: 0,
595
+ fw0: 0,
596
+ fh0: 0,
597
+ scope,
598
+ inset: { x: 0, y: 0 },
599
+ index: 0,
600
+ transient: true,
601
+ };
602
+ }
506
603
  const img = trigger.img;
507
604
  const rect = img.getBoundingClientRect();
508
605
  return {
509
606
  el: img,
510
607
  host: trigger.host,
511
- src: srcOf(img),
608
+ src: normalizeSrc(srcOf(img)),
512
609
  alt: img.getAttribute('alt') || '',
513
610
  referrerPolicy: img.referrerPolicy || '',
514
611
  w: img.naturalWidth || 0,
@@ -523,6 +620,28 @@ window.__ModuleLoader__.load({
523
620
 
524
621
  function collect(trigger) {
525
622
  const scope = scrollerOf(trigger.host);
623
+ // Image links in one scroll area form one gallery, like inline images do.
624
+ if (trigger.kind === 'link') {
625
+ const seen = new Set();
626
+ const items = [];
627
+ const hosts = [...scope.querySelectorAll('button,[role="button"]')];
628
+ if (!hosts.includes(trigger.host)) hosts.unshift(trigger.host);
629
+ for (const host of hosts) {
630
+ if (items.length >= MAX_ITEMS) break;
631
+ if (seen.has(host)) continue;
632
+ if (host !== trigger.host && (host.closest('[data-composer-seat]') !== null || host.getElementsByTagName('img').length !== 0)) continue;
633
+ const link = host === trigger.host ? { src: trigger.src, label: trigger.label } : imageLinkOf(host);
634
+ if (link === null) continue;
635
+ seen.add(host);
636
+ const entry = host === trigger.host ? trigger : { host, kind: 'link', src: link.src, label: link.label };
637
+ items.push(makeItem(entry, scope));
638
+ }
639
+ items.forEach((item, i) => {
640
+ item.index = i;
641
+ });
642
+ const at = items.findIndex((item) => item.el === trigger.host);
643
+ return { items, index: at < 0 ? 0 : at };
644
+ }
526
645
  const hasLayout = trigger.img.getBoundingClientRect().width > 0;
527
646
  const seen = new Set();
528
647
  const items = [];
@@ -819,7 +938,8 @@ window.__ModuleLoader__.load({
819
938
  function hideSource(item) {
820
939
  if (hiddenItem === item) return;
821
940
  showSource();
822
- if (item && item.el.isConnected) {
941
+ // Link chips stay visible: hiding a text link would look like a gap.
942
+ if (item && item.transient !== true && item.el.isConnected) {
823
943
  item.prevVisibility = item.el.style.visibility;
824
944
  item.el.style.visibility = 'hidden';
825
945
  hiddenItem = item;
@@ -2537,6 +2657,25 @@ window.__ModuleLoader__.load({
2537
2657
  //#endregion
2538
2658
 
2539
2659
  //#region click interception
2660
+ /**
2661
+ * Mark image file links with a zoom cursor before the first click, so the
2662
+ * transcript reads as "this opens the viewer" rather than "this opens a file".
2663
+ * Resolved lazily on hover and cached on the element itself.
2664
+ */
2665
+ function installLinkAffordance() {
2666
+ const seen = new WeakSet();
2667
+ const onOver = (event) => {
2668
+ const node = event.target;
2669
+ if (!(node instanceof Element)) return;
2670
+ const host = node.closest('button,[role="button"]');
2671
+ if (host === null || seen.has(host) || host.closest(`[${ROOT_ATTR}],[${EDIT_ATTR}]`) !== null) return;
2672
+ seen.add(host);
2673
+ if (imageLinkOf(host) !== null) host.setAttribute(LINK_ATTR, '');
2674
+ };
2675
+ document.addEventListener('pointerover', onOver, true);
2676
+ return () => document.removeEventListener('pointerover', onOver, true);
2677
+ }
2678
+
2540
2679
  function installInterceptor(t) {
2541
2680
  let viewer = null;
2542
2681
  const onClick = (e) => {
@@ -2590,6 +2729,7 @@ window.__ModuleLoader__.load({
2590
2729
  }
2591
2730
  ctx.effect(() => installStyles(), 'image-viewer: styles');
2592
2731
  ctx.effect(() => installInterceptor(t), 'image-viewer: click interceptor');
2732
+ ctx.effect(() => installLinkAffordance(), 'image-viewer: image-link affordance');
2593
2733
  }
2594
2734
  //#endregion
2595
2735
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qzsy/dsh-image-viewer",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "iPhone Photos-style full-screen image viewer for the DSH conversation: zoom from thumbnail, pinch/double-tap zoom, momentum pan, swipe between images, swipe down to close; annotate an image and drop it into the chat box.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -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"
60
+ "test": "node test/smoke.mjs && node test/layout.mjs && node test/editor.mjs && node test/link.mjs"
61
61
  },
62
62
  "dshBuild": {
63
63
  "host": "src/index.ts",