@qzsy/dsh-image-viewer 0.2.1 → 0.3.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 +12 -2
- package/dist/client.js +151 -7
- 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 点击、图片在链接内、位于导航栏 / 工具栏 /
|
|
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,78 @@ 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
|
+
// Only DSH's file-link chips: other buttons can sit inside hover cards
|
|
515
|
+
// whose content also carries an image, and those must keep their click.
|
|
516
|
+
const className = typeof host.className === 'string' ? host.className : '';
|
|
517
|
+
if (!/fileLink|fileMention/i.test(className)) return null;
|
|
518
|
+
if (host.getElementsByTagName('img').length !== 0) return null;
|
|
519
|
+
let fiber = fiberOf(host);
|
|
520
|
+
for (let up = 0; up < 14 && fiber !== null && fiber !== undefined; up += 1) {
|
|
521
|
+
const props = fiber.memoizedProps;
|
|
522
|
+
if (props !== null && props !== undefined && typeof props === 'object') {
|
|
523
|
+
const src = srcInElement(props.content);
|
|
524
|
+
if (src !== null) return { src: normalizeSrc(src), label: (host.textContent || '').trim() };
|
|
525
|
+
}
|
|
526
|
+
fiber = fiber.return;
|
|
527
|
+
}
|
|
528
|
+
} catch {
|
|
529
|
+
return null;
|
|
530
|
+
}
|
|
531
|
+
return null;
|
|
532
|
+
}
|
|
533
|
+
|
|
459
534
|
/**
|
|
460
535
|
* Map a click target to a previewable image, or null to let the click
|
|
461
536
|
* through. Buttons qualify only when they wrap exactly one image and show
|
|
@@ -470,11 +545,18 @@ window.__ModuleLoader__.load({
|
|
|
470
545
|
if (host !== null) {
|
|
471
546
|
if (host.closest('a[href]') !== null) return null;
|
|
472
547
|
const imgs = host.getElementsByTagName('img');
|
|
473
|
-
if (imgs.length
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
548
|
+
if (imgs.length === 1) {
|
|
549
|
+
const img = imgs[0];
|
|
550
|
+
if (!isZoomCursor(host) && !isZoomCursor(img)) return null;
|
|
551
|
+
if (srcOf(img) === '') return null;
|
|
552
|
+
return { host, img };
|
|
553
|
+
}
|
|
554
|
+
// A markdown link to an image file: the app would open the file panel.
|
|
555
|
+
if (imgs.length === 0 && host.closest('[data-composer-seat]') === null) {
|
|
556
|
+
const link = imageLinkOf(host);
|
|
557
|
+
if (link !== null) return { host, kind: 'link', src: link.src, label: link.label };
|
|
558
|
+
}
|
|
559
|
+
return null;
|
|
478
560
|
}
|
|
479
561
|
if (node.tagName !== 'IMG') return null;
|
|
480
562
|
const img = node;
|
|
@@ -503,12 +585,31 @@ window.__ModuleLoader__.load({
|
|
|
503
585
|
}
|
|
504
586
|
|
|
505
587
|
function makeItem(trigger, scope) {
|
|
588
|
+
if (trigger.kind === 'link') {
|
|
589
|
+
// A link chip is not a thumbnail: it keeps its own size until the image
|
|
590
|
+
// loads, and it stays visible behind the viewer.
|
|
591
|
+
return {
|
|
592
|
+
el: trigger.host,
|
|
593
|
+
host: trigger.host,
|
|
594
|
+
src: trigger.src,
|
|
595
|
+
alt: trigger.label || '',
|
|
596
|
+
referrerPolicy: 'no-referrer',
|
|
597
|
+
w: 0,
|
|
598
|
+
h: 0,
|
|
599
|
+
fw0: 0,
|
|
600
|
+
fh0: 0,
|
|
601
|
+
scope,
|
|
602
|
+
inset: { x: 0, y: 0 },
|
|
603
|
+
index: 0,
|
|
604
|
+
transient: true,
|
|
605
|
+
};
|
|
606
|
+
}
|
|
506
607
|
const img = trigger.img;
|
|
507
608
|
const rect = img.getBoundingClientRect();
|
|
508
609
|
return {
|
|
509
610
|
el: img,
|
|
510
611
|
host: trigger.host,
|
|
511
|
-
src: srcOf(img),
|
|
612
|
+
src: normalizeSrc(srcOf(img)),
|
|
512
613
|
alt: img.getAttribute('alt') || '',
|
|
513
614
|
referrerPolicy: img.referrerPolicy || '',
|
|
514
615
|
w: img.naturalWidth || 0,
|
|
@@ -523,6 +624,28 @@ window.__ModuleLoader__.load({
|
|
|
523
624
|
|
|
524
625
|
function collect(trigger) {
|
|
525
626
|
const scope = scrollerOf(trigger.host);
|
|
627
|
+
// Image links in one scroll area form one gallery, like inline images do.
|
|
628
|
+
if (trigger.kind === 'link') {
|
|
629
|
+
const seen = new Set();
|
|
630
|
+
const items = [];
|
|
631
|
+
const hosts = [...scope.querySelectorAll('button,[role="button"]')];
|
|
632
|
+
if (!hosts.includes(trigger.host)) hosts.unshift(trigger.host);
|
|
633
|
+
for (const host of hosts) {
|
|
634
|
+
if (items.length >= MAX_ITEMS) break;
|
|
635
|
+
if (seen.has(host)) continue;
|
|
636
|
+
if (host !== trigger.host && (host.closest('[data-composer-seat]') !== null || host.getElementsByTagName('img').length !== 0)) continue;
|
|
637
|
+
const link = host === trigger.host ? { src: trigger.src, label: trigger.label } : imageLinkOf(host);
|
|
638
|
+
if (link === null) continue;
|
|
639
|
+
seen.add(host);
|
|
640
|
+
const entry = host === trigger.host ? trigger : { host, kind: 'link', src: link.src, label: link.label };
|
|
641
|
+
items.push(makeItem(entry, scope));
|
|
642
|
+
}
|
|
643
|
+
items.forEach((item, i) => {
|
|
644
|
+
item.index = i;
|
|
645
|
+
});
|
|
646
|
+
const at = items.findIndex((item) => item.el === trigger.host);
|
|
647
|
+
return { items, index: at < 0 ? 0 : at };
|
|
648
|
+
}
|
|
526
649
|
const hasLayout = trigger.img.getBoundingClientRect().width > 0;
|
|
527
650
|
const seen = new Set();
|
|
528
651
|
const items = [];
|
|
@@ -819,7 +942,8 @@ window.__ModuleLoader__.load({
|
|
|
819
942
|
function hideSource(item) {
|
|
820
943
|
if (hiddenItem === item) return;
|
|
821
944
|
showSource();
|
|
822
|
-
|
|
945
|
+
// Link chips stay visible: hiding a text link would look like a gap.
|
|
946
|
+
if (item && item.transient !== true && item.el.isConnected) {
|
|
823
947
|
item.prevVisibility = item.el.style.visibility;
|
|
824
948
|
item.el.style.visibility = 'hidden';
|
|
825
949
|
hiddenItem = item;
|
|
@@ -2537,6 +2661,25 @@ window.__ModuleLoader__.load({
|
|
|
2537
2661
|
//#endregion
|
|
2538
2662
|
|
|
2539
2663
|
//#region click interception
|
|
2664
|
+
/**
|
|
2665
|
+
* Mark image file links with a zoom cursor before the first click, so the
|
|
2666
|
+
* transcript reads as "this opens the viewer" rather than "this opens a file".
|
|
2667
|
+
* Resolved lazily on hover and cached on the element itself.
|
|
2668
|
+
*/
|
|
2669
|
+
function installLinkAffordance() {
|
|
2670
|
+
const seen = new WeakSet();
|
|
2671
|
+
const onOver = (event) => {
|
|
2672
|
+
const node = event.target;
|
|
2673
|
+
if (!(node instanceof Element)) return;
|
|
2674
|
+
const host = node.closest('button,[role="button"]');
|
|
2675
|
+
if (host === null || seen.has(host) || host.closest(`[${ROOT_ATTR}],[${EDIT_ATTR}]`) !== null) return;
|
|
2676
|
+
seen.add(host);
|
|
2677
|
+
if (imageLinkOf(host) !== null) host.setAttribute(LINK_ATTR, '');
|
|
2678
|
+
};
|
|
2679
|
+
document.addEventListener('pointerover', onOver, true);
|
|
2680
|
+
return () => document.removeEventListener('pointerover', onOver, true);
|
|
2681
|
+
}
|
|
2682
|
+
|
|
2540
2683
|
function installInterceptor(t) {
|
|
2541
2684
|
let viewer = null;
|
|
2542
2685
|
const onClick = (e) => {
|
|
@@ -2590,6 +2733,7 @@ window.__ModuleLoader__.load({
|
|
|
2590
2733
|
}
|
|
2591
2734
|
ctx.effect(() => installStyles(), 'image-viewer: styles');
|
|
2592
2735
|
ctx.effect(() => installInterceptor(t), 'image-viewer: click interceptor');
|
|
2736
|
+
ctx.effect(() => installLinkAffordance(), 'image-viewer: image-link affordance');
|
|
2593
2737
|
}
|
|
2594
2738
|
//#endregion
|
|
2595
2739
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@qzsy/dsh-image-viewer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
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",
|