@bhxch/vviewer-lightbox 0.1.1 → 0.1.4

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
@@ -1,10 +1,13 @@
1
1
  # @bhxch/vviewer-lightbox
2
2
 
3
3
  零依赖灯箱组件包:body 级单例 overlay——画廊导航(←/→ 环绕)、键盘集、光标锚定
4
- 缩放平移(滚轮/按钮/+− 键)、owner 代币防多实例互删。自 render-text 管线迁出的
4
+ 缩放平移(滚轮/按钮/+− 键;Pointer Events 统一鼠标/触屏/笔:单指针拖拽平移、双指
5
+ 以中点为锚捏合缩放)、owner 代币防多实例互删。自 render-text 管线迁出的
5
6
  全自研组件,供任何需要图片/SVG 灯箱的查看器复用。组件只负责 DOM 装配与交互;
6
- 样式由宿主按类名约定自备(web 端示例见 `apps/web/src/app.css` 的 `.md-lightbox-*`
7
- 规则组,app.css:217-237)。
7
+ 样式由宿主按类名约定自备(web 端示例见 `@bhxch/vviewer-markdown` 的
8
+ `RUNTIME_LIGHTBOX_CSS` / `markdown.css` 中 `.md-lightbox-*` 规则组)。触屏手势要求
9
+ 宿主给 overlay 与 stage 设 `touch-action: none`——否则浏览器原生平移/缩放会与指针
10
+ 手势竞争,并以 pointercancel 打断拖拽/捏合。
8
11
 
9
12
  ## 安装
10
13
 
package/index.d.ts CHANGED
@@ -1 +1 @@
1
- export { clampZoom, collectGallery, LIGHTBOX_OVERLAY_ID, LIGHTBOX_OWNER_ATTR, navigate, openLightbox, removeLightboxOverlay, zoomAt, type GalleryItem, type LightboxCtx, type LightboxState, } from './lightbox.js';
1
+ export { clampZoom, collectGallery, lightboxUiText, LIGHTBOX_OVERLAY_ID, LIGHTBOX_OWNER_ATTR, navigate, openLightbox, removeLightboxOverlay, setLightboxUiTexts, zoomAt, type GalleryItem, type LightboxCtx, type LightboxState, type LightboxUiKey, type LightboxUiTextGetter, } from './lightbox.js';
package/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // index.ts — @bhxch/vviewer-lightbox 包根导出面:零依赖灯箱组件(画廊收集 / 键盘集 /
2
2
  // 光标锚定缩放平移 / owner 代币防互删),自 render-text 管线迁出的全自研组件。
3
3
  // 跨包消费一律经包根 import(含主应用改造点所需的全部符号:
4
- // collectGallery/openLightbox/removeLightboxOverlay/LIGHTBOX_OVERLAY_ID/LIGHTBOX_OWNER_ATTR)。
5
- export { clampZoom, collectGallery, LIGHTBOX_OVERLAY_ID, LIGHTBOX_OWNER_ATTR, navigate, openLightbox, removeLightboxOverlay, zoomAt, } from './lightbox.js';
4
+ // collectGallery/openLightbox/removeLightboxOverlay/LIGHTBOX_OVERLAY_ID/LIGHTBOX_OWNER_ATTR;
5
+ // setLightboxUiTexts/lightboxUiText 为 L1 族③ closure Task 11 的控制钮 aria 注入缝)。
6
+ export { clampZoom, collectGallery, lightboxUiText, LIGHTBOX_OVERLAY_ID, LIGHTBOX_OWNER_ATTR, navigate, openLightbox, removeLightboxOverlay, setLightboxUiTexts, zoomAt, } from './lightbox.js';
package/lightbox.d.ts CHANGED
@@ -43,6 +43,17 @@ export declare function navigate(state: {
43
43
  }, delta: number): {
44
44
  index: number;
45
45
  };
46
+ /** 灯箱控制钮 aria-label 键(6 键:导航 2 + 缩放 2 + 重置/关闭 2)。 */
47
+ export type LightboxUiKey = 'prevAria' | 'nextAria' | 'zoomOutAria' | 'zoomInAria' | 'resetAria' | 'closeAria';
48
+ /** 文案 getter:本缝 6 键均无占位符(`() => string` 无参 getter 形态)。 */
49
+ export type LightboxUiTextGetter = () => string;
50
+ /**
51
+ * 注入灯箱控制钮 aria getter 表(宿主装配时调用;传 null 解绑回缺省英文)。
52
+ * Partial 语义:只覆盖所列键,未列键回落缺省——供 locale 子集与测试选择性注入。
53
+ */
54
+ export declare function setLightboxUiTexts(texts: Partial<Record<LightboxUiKey, LightboxUiTextGetter>> | null): void;
55
+ /** 当前生效 aria 文案(缺省表与注入表按键取用)。供包内赋值点与单测直读。 */
56
+ export declare function lightboxUiText(key: LightboxUiKey): string;
46
57
  /**
47
58
  * 收集渲染根下的画廊条目(文档序:img 在前、图表 svg 在后):img 跳过两类——
48
59
  * 宽高属性均在且均 <32px 的微缩图(图标/表情)、KaTeX 排版产物内的 img;图表
package/lightbox.js CHANGED
@@ -5,11 +5,12 @@
5
5
  // 对宿主管线上下文只依赖最小局部接口 LightboxCtx(类型擦除,无运行时环)。
6
6
  // - 状态机纯函数(clampZoom/zoomAt/navigate)单测直测:zoomAt 光标中心锚定——
7
7
  // 屏幕点 p = 内容点 c × zoom + t(stage transform-origin 0 0 同构),令光标处
8
- // 缩放前后 p 不变解出 t' = (cx,cy) − applied × ((cx,cy) − t)。
8
+ // 缩放前后 p 不变解出 t' = (cx,cy) − applied × ((cx,cy) − t)。双指捏合同以 zoomAt
9
+ // 锚两指中点,再叠加中点位移。
9
10
  // - 关闭语义(T1 裁决):点背景(overlay 本体/stage 空白)或关闭钮关;点内容与
10
11
  // 控制件不关——比原「点 overlay 任意处关」多一层 target 判定,原有语义测试
11
12
  // (overlay.click())不受影响。
12
- // - 拖拽 pan 与关闭的冲突(T2 裁决):拖拽位移超阈值置 dragMoved,吞掉 mouseup
13
+ // - 拖拽 pan 与关闭的冲突(T2 裁决):拖拽位移超阈值置 dragMoved,吞掉 pointerup
13
14
  // 后收尾的 click(该 click 经冒泡会落在 overlay 上,否则拖完即误关)。
14
15
  // - 递延清偿(5-2/5-3/5-5):输入控件内按键直通不劫持;overlay 具对话框语义
15
16
  // (role/aria-modal/开启焦点入/关闭焦点还原/计数 aria-live);画廊按元素身份定位
@@ -35,6 +36,28 @@ export function navigate(state, delta) {
35
36
  return { index: state.index };
36
37
  return { index: ((state.index + delta) % state.count + state.count) % state.count };
37
38
  }
39
+ /** 缺省英文 aria 表(包独立消费缺省,非显示面契约)。 */
40
+ const LIGHTBOX_UI_TEXTS_DEFAULT = {
41
+ prevAria: () => 'Previous image',
42
+ nextAria: () => 'Next image',
43
+ zoomOutAria: () => 'Zoom out',
44
+ zoomInAria: () => 'Zoom in',
45
+ resetAria: () => 'Reset view',
46
+ closeAria: () => 'Close'
47
+ };
48
+ let lightboxUiTextsOverride = null;
49
+ /**
50
+ * 注入灯箱控制钮 aria getter 表(宿主装配时调用;传 null 解绑回缺省英文)。
51
+ * Partial 语义:只覆盖所列键,未列键回落缺省——供 locale 子集与测试选择性注入。
52
+ */
53
+ export function setLightboxUiTexts(texts) {
54
+ lightboxUiTextsOverride = texts;
55
+ }
56
+ /** 当前生效 aria 文案(缺省表与注入表按键取用)。供包内赋值点与单测直读。 */
57
+ export function lightboxUiText(key) {
58
+ const getter = lightboxUiTextsOverride?.[key] ?? LIGHTBOX_UI_TEXTS_DEFAULT[key];
59
+ return getter();
60
+ }
38
61
  /** 画廊条目 → 收集时源元素:同 src 多图按元素身份定位用。收集即注册(点击时现场
39
62
  * 收集,引用恒新鲜);条目为一次性对象,WeakMap 弱引用随条目回收,无泄漏面。 */
40
63
  const itemEls = new WeakMap();
@@ -230,12 +253,12 @@ function buildOverlay(doc, ctx) {
230
253
  b.addEventListener('click', onClick);
231
254
  return b;
232
255
  };
233
- const prev = mkBtn('btnPrev', '‹', '上一张', () => go(-1));
234
- const next = mkBtn('btnNext', '›', '下一张', () => go(1));
235
- const zoomOut = mkBtn('btnZoomOut', '−', '缩小', () => centerZoom(1 / ZOOM_STEP));
236
- const zoomIn = mkBtn('btnZoomIn', '+', '放大', () => centerZoom(ZOOM_STEP));
237
- const reset = mkBtn('btnReset', '⟲', '重置视图', resetView);
238
- const closeBtn = mkBtn('btnClose', '×', '关闭', () => removeLightboxOverlay(doc));
256
+ const prev = mkBtn('btnPrev', '‹', lightboxUiText('prevAria'), () => go(-1));
257
+ const next = mkBtn('btnNext', '›', lightboxUiText('nextAria'), () => go(1));
258
+ const zoomOut = mkBtn('btnZoomOut', '−', lightboxUiText('zoomOutAria'), () => centerZoom(1 / ZOOM_STEP));
259
+ const zoomIn = mkBtn('btnZoomIn', '+', lightboxUiText('zoomInAria'), () => centerZoom(ZOOM_STEP));
260
+ const reset = mkBtn('btnReset', '⟲', lightboxUiText('resetAria'), resetView);
261
+ const closeBtn = mkBtn('btnClose', '×', lightboxUiText('closeAria'), () => removeLightboxOverlay(doc));
239
262
  bar.append(prev, next, count, zoomOut, zoomIn, reset, closeBtn);
240
263
  overlay.append(stage, bar);
241
264
  // 键盘集(document 级,escHandlers 既有模式:关闭随之摘除防监听器累积)。
@@ -280,33 +303,93 @@ function buildOverlay(doc, ctx) {
280
303
  state = zoomAt(state, ev.deltaY < 0 ? ZOOM_STEP : 1 / ZOOM_STEP, ev.clientX - rect.left, ev.clientY - rect.top);
281
304
  applyState();
282
305
  }, { passive: false });
283
- // 拖拽平移:mousedown 记起点与基准位移,document 级 mousemove 增量写 tx/ty(按下
284
- // 态才挂监听),mouseup 收监听;位移超阈值置 dragMoved(恒允许 pan,zoom=1 时
285
- // 拖动即平移、无边界回弹——实现取简)
286
- stage.addEventListener('mousedown', (ev) => {
287
- if (ev.button !== 0)
306
+ // 指针手势(Pointer Events 统一鼠标/触屏/笔;活动指针记于 pointers,Map 插入序即
307
+ // 按下序):
308
+ // - 单指针拖拽平移:增量相对按下点(panFrom 基准 + 位移),位移超阈值置 dragMoved
309
+ // (恒允许 pan,zoom=1 时拖动即平移、无边界回弹——实现取简);
310
+ // - 双指针捏合:zoomAt 以上一帧两指中点为锚按指距比缩放,再随中点位移平移;
311
+ // - 指针数回落到 1(捏合抬一指)即以剩余指针现位置重置基准,接续平移不跳变。
312
+ // move/up/cancel 挂 document(首指按下才挂、全抬起即收);按下元素 setPointerCapture:
313
+ // 指针出窗口仍持续送达,且 click 目标保持为按下元素——若改捕获到 stage,点内容的
314
+ // click 目标会被改写成 stage 而误关(T1 裁决)。
315
+ const pointers = new Map();
316
+ let panFrom = { x: 0, y: 0, tx: 0, ty: 0 };
317
+ const rebase = () => {
318
+ const p = pointers.values().next().value;
319
+ if (p)
320
+ panFrom = { x: p.x, y: p.y, tx: state.tx, ty: state.ty };
321
+ };
322
+ const onPointerMove = (ev) => {
323
+ if (!pointers.has(ev.pointerId))
288
324
  return;
289
- ev.preventDefault(); // 防原生 img 拖拽与选中
290
- dragMoved = false;
291
- const startX = ev.clientX;
292
- const startY = ev.clientY;
293
- const base = { tx: state.tx, ty: state.ty };
294
- const onMove = (me) => {
295
- const dx = me.clientX - startX;
296
- const dy = me.clientY - startY;
325
+ const cur = { x: ev.clientX, y: ev.clientY };
326
+ if (pointers.size === 1) {
327
+ pointers.set(ev.pointerId, cur);
328
+ const dx = cur.x - panFrom.x;
329
+ const dy = cur.y - panFrom.y;
297
330
  if (Math.abs(dx) + Math.abs(dy) > DRAG_THRESHOLD)
298
331
  dragMoved = true;
299
- state = { ...state, tx: base.tx + dx, ty: base.ty + dy };
332
+ state = { ...state, tx: panFrom.tx + dx, ty: panFrom.ty + dy };
300
333
  applyState();
301
- };
302
- const onUp = () => {
303
- stage.classList.remove(cls(ctx, 'dragging'));
304
- doc.removeEventListener('mousemove', onMove);
305
- doc.removeEventListener('mouseup', onUp);
306
- };
307
- stage.classList.add(cls(ctx, 'dragging'));
308
- doc.addEventListener('mousemove', onMove);
309
- doc.addEventListener('mouseup', onUp);
334
+ return;
335
+ }
336
+ // 捏合:仅按下序前两个指针参与(第三指起只记位置)
337
+ const [a0, b0] = [...pointers.values()];
338
+ pointers.set(ev.pointerId, cur);
339
+ const [a1, b1] = [...pointers.values()];
340
+ if (!a0 || !b0 || !a1 || !b1 || (a0 === a1 && b0 === b1))
341
+ return;
342
+ dragMoved = true;
343
+ const rect = overlay.getBoundingClientRect();
344
+ const m0x = (a0.x + b0.x) / 2 - rect.left;
345
+ const m0y = (a0.y + b0.y) / 2 - rect.top;
346
+ const m1x = (a1.x + b1.x) / 2 - rect.left;
347
+ const m1y = (a1.y + b1.y) / 2 - rect.top;
348
+ const d0 = Math.hypot(b0.x - a0.x, b0.y - a0.y);
349
+ const d1 = Math.hypot(b1.x - a1.x, b1.y - a1.y);
350
+ const zoomed = zoomAt(state, d0 > 0 ? d1 / d0 : 1, m0x, m0y);
351
+ state = { ...zoomed, tx: zoomed.tx + m1x - m0x, ty: zoomed.ty + m1y - m0y };
352
+ applyState();
353
+ };
354
+ const onPointerUp = (ev) => {
355
+ if (!pointers.delete(ev.pointerId))
356
+ return;
357
+ if (pointers.size === 1)
358
+ rebase(); // 捏合抬一指:剩余指针接续平移
359
+ if (pointers.size > 0)
360
+ return;
361
+ stage.classList.remove(cls(ctx, 'dragging'));
362
+ doc.removeEventListener('pointermove', onPointerMove);
363
+ doc.removeEventListener('pointerup', onPointerUp);
364
+ doc.removeEventListener('pointercancel', onPointerUp);
365
+ };
366
+ stage.addEventListener('pointerdown', (ev) => {
367
+ if (ev.pointerType === 'mouse') {
368
+ if (ev.button !== 0)
369
+ return; // 主键过滤只对鼠标(触屏/笔的接触即主键)
370
+ // 防原生 img 拖拽与选中(同原 mousedown 语义);触屏不取消——原生手势交 CSS
371
+ // touch-action,免得波及 tap 合成的兼容鼠标事件与 click(点背景关闭)
372
+ ev.preventDefault();
373
+ }
374
+ const target = ev.target;
375
+ if (target instanceof Element) {
376
+ try {
377
+ target.setPointerCapture(ev.pointerId);
378
+ }
379
+ catch {
380
+ // jsdom 未实现 / 合成事件无活动指针时抛错:不捕获,document 级监听照常收 move/up
381
+ }
382
+ }
383
+ if (pointers.size === 0) {
384
+ dragMoved = false;
385
+ stage.classList.add(cls(ctx, 'dragging'));
386
+ doc.addEventListener('pointermove', onPointerMove);
387
+ doc.addEventListener('pointerup', onPointerUp);
388
+ doc.addEventListener('pointercancel', onPointerUp);
389
+ }
390
+ pointers.set(ev.pointerId, { x: ev.clientX, y: ev.clientY });
391
+ if (pointers.size === 1)
392
+ rebase();
310
393
  });
311
394
  // HTML5 原生拖拽与指针平移冲突,禁掉
312
395
  stage.addEventListener('dragstart', (ev) => ev.preventDefault());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bhxch/vviewer-lightbox",
3
- "version": "0.1.1",
3
+ "version": "0.1.4",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -25,5 +25,5 @@
25
25
  "directory": "packages/vviewer-lightbox"
26
26
  },
27
27
  "license": "Apache-2.0",
28
- "contentHash": "703f278b5a226118"
28
+ "contentHash": "97c223b3b033d526"
29
29
  }