diff --git a/plugins/album/main.js b/plugins/album/main.js index f0e4efe..28d8c92 100644 --- a/plugins/album/main.js +++ b/plugins/album/main.js @@ -377,11 +377,11 @@ update(st.props, st.node, { items: items, page: Math.min(pageNow, pagesNow - 1) }, true) }) btn.appendChild(del) - // 放大镜(右下角):点击 → 宿主全屏预览。1.1.1 去掉了 hover 预览(会盖住放大镜导致点不到, - // 用户实测),只留「点一下看大图」这一条路径:宿主预览点图片外的空白或按 Esc 自动退出。 - const zoom = el('span', { className: 'iwb-album-zoom', title: '查看大图(点击空白处或按 Esc 关闭)' }, zoomIcon()) + // 放大镜(右下角):点开插件自带的全屏查看器,可 ← → 切换整本相册(1.1.2)。 + // 1.1.1 的 hover 预览已废(那层会盖住放大镜本身)。 + const zoom = el('span', { className: 'iwb-album-zoom', title: '查看大图(← → 切换,点击空白处或 Esc 关闭)' }, zoomIcon()) zoom.addEventListener('pointerdown', (e) => e.stopPropagation()) - zoom.addEventListener('click', (e) => { e.stopPropagation(); openFull(ctx, st, it) }) + zoom.addEventListener('click', (e) => { e.stopPropagation(); openFull(ctx, st, index) }) btn.appendChild(zoom) btn.addEventListener('pointerdown', (e) => e.stopPropagation()) btn.addEventListener('click', (e) => { @@ -475,23 +475,170 @@ return thumb } - // ── 看大图:只走宿主全屏预览(1.1.1 定了唯一交互)──────────── - // 历史:1.1.0 曾做「hover 放大镜 → 节点框内浮大图(pointer-events:none)+ 点击全屏」两套。 - // 实测否决:hover 一进就把整块节点压暗(预览层 z-index 9),放大镜本身被盖在下面 ⇒ - // 用户看到的是「滑过去就不能点了」。现在删掉 A,只保留 B: - // 点放大镜 → props.onPreview 打开宿主预览层(铺满窗口); - // 点图片外的空白 / 按 Esc → 宿主自己 onClose(见 src/ui-canvas-view.js 的 ImagePreview)。 - // 代价是「看一眼」也要点两下,但少一层状态,点到哪就是哪,不会再互相打架。 - function openFull (ctx, st, it) { - const props = st.props || {} - if (typeof props.onPreview !== 'function') return - const fallback = st.thumbs.get(thumbKeyOf(it)) || '' - const run = (url) => { try { props.onPreview(url || fallback) } catch (e) { /* ignore */ } } - if (!ctx.images || typeof ctx.images.dataUrl !== 'function') { run(fallback); return } - try { - // 先给缩略图/原图都行,宿主预览层会再按需解析;读盘失败退回缩略图 - Promise.resolve(ctx.images.dataUrl(it && it.src)).then((u) => run(u)).catch(() => run('')) - } catch (e) { run(fallback) } + // ── 看大图:插件自带全屏查看器(1.1.2 起支持 ← → 切换)────────── + // 历史:1.1.0 做过「hover 节点内浮大图 + 点击全屏」两套,hover 层把放大镜盖住了 ⇒ 1.1.1 删掉。 + // 1.1.2:宿主 props.onPreview 只收「已解析好的 URL 列表」,一本 30 张的相册要左右切换, + // 就得开窗前把 30 张原图全转成 dataURL(几十上百 MB,还可能卡住主线程)。所以改成自建查看器: + // 只解码当前这张 + 预取左右各一张(最多 3 张常驻),切换即时、内存可控。 + // 交互沿用宿主那套肌肉记忆:点图片外的空白 / Esc 退出,← → 切换,滚轮也切。 + const FS_KEEP = 3 // 同时保留的原图张数(当前 + 左右各一) + + function closeViewer (st) { + const v = st && st.viewer + if (!v) return + st.viewer = null + try { window.removeEventListener('keydown', v.onKey, true) } catch (e) { /* ignore */ } + try { window.removeEventListener('wheel', v.onWheel, true) } catch (e) { /* ignore */ } + if (v.root && v.root.parentNode) v.root.parentNode.removeChild(v.root) + } + + function openFull (ctx, st, index) { + closeViewer(st) + const items = itemsOf(st.node) + const total = items.length + if (!total) return + const pick = (i) => items[((i % total) + total) % total] + const keyAt = (i) => thumbKeyOf(pick(i)) + let cur = Math.max(0, Math.min(total - 1, Number(index) || 0)) + let curUrl = '' // 当前显示图的地址(复制/下载用) + let token = 0 + const cache = new Map() // key → 原图 URL + const pending = new Map() // key → Promise(预取与切页会同时要同一张) + + const img = el('img', { alt: '', draggable: 'false' }) + const count = el('span', { className: 'iwb-album-fs-count' }) + const name = el('span', { className: 'iwb-album-fs-name' }) + const btnCopy = el('button', { className: 'iwb-album-fs-btn', title: '复制当前图片到剪贴板(PNG)' }, '复制') + const btnDl = el('button', { className: 'iwb-album-fs-btn', title: '下载当前图片' }, '下载') + const bar = el('div', { className: 'iwb-album-fs-bar' }, [count, name, btnCopy, btnDl]) + const hint = el('div', { className: 'iwb-album-fs-hint' }, '点击空白处或按 Esc 关闭 · ← → 切换') + const btnPrev = el('button', { className: 'iwb-album-fs-nav prev', title: '上一张(←)' }, '‹') + const btnNext = el('button', { className: 'iwb-album-fs-nav next', title: '下一张(→)' }, '›') + const root = el('div', { className: 'iwb-album-fs' + (total > 1 ? '' : ' single') }, [img, btnPrev, btnNext, bar, hint]) + + // 原图按需读:命中缓存直接用;读盘失败就保持缩略图(不白屏、不报错) + const fullOf = (i) => { + const it = pick(i) + const key = keyAt(i) + if (cache.has(key)) return Promise.resolve(cache.get(key)) + const flight = pending.get(key) + if (flight) return flight + if (!ctx.images || typeof ctx.images.dataUrl !== 'function') return Promise.resolve('') + const p = Promise.resolve(ctx.images.dataUrl(it && it.src)).then((u) => { + pending.delete(key) + if (!u) return '' + cache.set(key, u) + // 只留当前 + 左右各一张:4K 图的 dataURL 一张就几 MB,全书缓存直接爆内存 + const keep = new Set([keyAt(cur), keyAt(cur + 1), keyAt(cur - 1)]) + if (cache.size > FS_KEEP) for (const k of Array.from(cache.keys())) if (!keep.has(k)) cache.delete(k) + return u + }).catch(() => { pending.delete(key); return '' }) + pending.set(key, p) + return p + } + const paint = (i) => { + const it = pick(i) + const my = ++token + const cached = cache.get(keyAt(i)) + const thumb = st.thumbs.get(keyAt(i)) || '' + // 先用缩略图占位(切换时不会闪一块黑),原图到了再换上;缩略图也还没好就现做 + if (cached) { img.src = cached; curUrl = cached } + else if (thumb) { img.src = thumb; curUrl = thumb } + else { + img.removeAttribute('src'); curUrl = '' + loadThumb(ctx, st, it, keyAt(i)).then((u) => { if (u && my === token) { img.src = u; curUrl = u } }).catch(() => {}) + } + fullOf(i).then((u) => { if (u && my === token) { img.src = u; curUrl = u } }).catch(() => {}) + count.textContent = (i + 1) + ' / ' + total + name.textContent = (it && it.name) || ('图 ' + (i + 1)) + if (total > 1) { fullOf(i + 1).catch(() => {}); fullOf(i - 1).catch(() => {}) } + } + const go = (delta) => { cur = ((cur + delta) % total + total) % total; paint(cur) } + const close = () => closeViewer(st) + + img.addEventListener('pointerdown', (e) => e.stopPropagation()) + img.addEventListener('click', (e) => e.stopPropagation()) + for (const [btn, delta] of [[btnPrev, -1], [btnNext, 1]]) { + btn.addEventListener('pointerdown', (e) => e.stopPropagation()) + btn.addEventListener('click', (e) => { e.stopPropagation(); go(delta) }) + } + // 复制 / 下载:宿主预览层原本有这两个按钮,不能因为换成自建查看器就丢掉 + const flash = (msg) => { + hint.textContent = msg + clearTimeout(flash.timer) + flash.timer = setTimeout(() => { hint.textContent = '点击空白处或按 Esc 关闭 · ← → 切换' }, 1800) + } + const toPngBlob = (url) => new Promise((resolve, reject) => { + const im = new Image() + im.onload = () => { + try { + const cv = document.createElement('canvas') + cv.width = im.naturalWidth || 1; cv.height = im.naturalHeight || 1 + cv.getContext('2d').drawImage(im, 0, 0) + cv.toBlob((b) => b ? resolve(b) : reject(new Error('toBlob 失败')), 'image/png') + } catch (e) { reject(e) } + } + im.onerror = () => reject(new Error('图片加载失败')) + im.src = url + }) + for (const btn of [btnCopy, btnDl]) btn.addEventListener('pointerdown', (e) => e.stopPropagation()) + btnDl.addEventListener('click', (e) => { + e.stopPropagation() + if (!curUrl) return + try { + const a = document.createElement('a') + a.href = curUrl + a.download = (pick(cur) && pick(cur).name) || ('image-' + (cur + 1) + '.png') + document.body.appendChild(a); a.click() + if (a.parentNode) a.parentNode.removeChild(a) + } catch (err) { flash('下载失败:' + String((err && err.message) || err)) } + }) + btnCopy.addEventListener('click', async (e) => { + e.stopPropagation() + if (!curUrl) return + try { + const png = await toPngBlob(curUrl) + if (navigator.clipboard && typeof navigator.clipboard.write === 'function' && typeof ClipboardItem !== 'undefined') { + await navigator.clipboard.write([new ClipboardItem({ 'image/png': png })]) + } else { + // WKWebView 常没有 ClipboardItem ⇒ 走宿主的原生 arboard 通道(与图库预览同一实现) + const D = window.IWBDesktop + if (!D || !D.clipboardWriteImage || !(await D.clipboardWriteImage(png))) throw new Error('当前环境不支持图片剪贴板') + } + flash('已复制图片到剪贴板(PNG)') + } catch (err) { flash('复制失败:' + String((err && err.message) || err)) } + }) + + // 整个浮层吃掉指针事件:否则点空白会把画布的「取消选中/框选」一起触发 + root.addEventListener('pointerdown', (e) => { e.stopPropagation(); if (e.target === root || e.target === bar) close() }) + root.addEventListener('click', (e) => e.stopPropagation()) + root.addEventListener('pointerup', (e) => e.stopPropagation()) + root.addEventListener('dblclick', (e) => e.stopPropagation()) + + // 键盘:捕获阶段吃掉,避免宿主把 ← → 当成「微调节点」 + const onKey = (e) => { + if (e.key === 'Escape') { e.preventDefault(); e.stopPropagation(); close() } + else if (e.key === 'ArrowLeft') { e.preventDefault(); e.stopPropagation(); go(-1) } + else if (e.key === 'ArrowRight') { e.preventDefault(); e.stopPropagation(); go(1) } + } + // 滚轮也切(大图查看器的通用手势);40ms 节流,避免一次滑到底翻好几张 + let wheelLock = 0 + const onWheel = (e) => { + if (total < 2) return + e.preventDefault(); e.stopPropagation() + const now = Date.now() + if (now - wheelLock < 40) return + const d = Math.abs(e.deltaY) > Math.abs(e.deltaX) ? e.deltaY : e.deltaX + if (Math.abs(d) < 2) return + wheelLock = now + go(d > 0 ? 1 : -1) + } + + document.body.appendChild(root) + paint(cur) + window.addEventListener('keydown', onKey, true) + window.addEventListener('wheel', onWheel, { passive: false, capture: true }) + st.viewer = { root, onKey, onWheel } } // ── 一键清空 ──────────────────────────────────────────── @@ -500,6 +647,7 @@ if (!items.length) return st.thumbs.clear(); st.pending.clear(); st.missing.clear() st.checkedMissing = false // 清空后再导入要重新做一次失联检测 + closeViewer(st) // 正在看大图时清空 ⇒ 先把浮层收掉(别留个没有数据的空查看器) update(st.props, st.node, { items: [], page: 0, pageRange: null }, true) st.sig = '' sync(ctx, st, st.node, st.props) @@ -664,7 +812,7 @@ global.IWBPluginHost.register({ id: 'album', name: '相册', - version: '1.1.1', + version: '1.1.2', apiVersion: 1, capabilities: ['nodeTypes', 'images', 'files'], settings: { diff --git a/plugins/album/manifest.json b/plugins/album/manifest.json index fe3997f..366314e 100644 --- a/plugins/album/manifest.json +++ b/plugins/album/manifest.json @@ -1,7 +1,7 @@ { "id": "album", "name": "相册", - "version": "1.1.1", + "version": "1.1.2", "apiVersion": 1, "main": "main.js", "style": "styles.css", @@ -14,7 +14,7 @@ "出图" ], "files": { - "main.js": "7504415606e494d7f9c3dafc67aff032fb7f5a18b7851ab897fcf303567b04c0", - "styles.css": "02ffd5a87281c955aa10f323b0f36487dc8e2a8dd394bc500273cb4e6f5db509" + "main.js": "3e60b95d30bce1b734c3c1a251fa1091b8d54786eb609737f1eec9ffce3d9b22", + "styles.css": "b61e806abee9dd8889aa1a12c8791d428feacab93fac64f89c18c589de390706" } } diff --git a/plugins/album/styles.css b/plugins/album/styles.css index 49646ba..e734747 100644 --- a/plugins/album/styles.css +++ b/plugins/album/styles.css @@ -51,6 +51,23 @@ .iwb-album-cell:hover .iwb-album-zoom, .iwb-album-cell:focus-within .iwb-album-zoom { opacity: 1; } .iwb-album-zoom:hover { color: var(--iwb-brand); } .iwb-album-zoom svg { width: 12px; height: 12px; display: block; } +/* 全屏查看器(1.1.2):挂在 document.body 上,所以是 position: fixed + 自备层级。 + 810 = 宿主图片预览层(--iwb-z-preview: 800)之上一层,盖住画布但不抢系统弹窗。 + 为什么不用宿主 onPreview:那个接口只收「已解析好的 URL 列表」,整本相册要一次全转 dataURL。 */ +.iwb-album-fs { position: fixed; inset: 0; z-index: 810; display: flex; align-items: center; justify-content: center; background: rgba(10, 12, 18, .92); cursor: zoom-out; } +.iwb-album-fs img { max-width: 92vw; max-height: 88vh; width: auto; height: auto; object-fit: contain; display: block; border-radius: 8px; box-shadow: 0 8px 40px rgba(0, 0, 0, .5); cursor: default; } +.iwb-album-fs-nav { position: fixed; top: 50%; transform: translateY(-50%); width: 46px; height: 64px; padding: 0; border: none; border-radius: 8px; background: rgba(255, 255, 255, .1); color: #fff; font-size: 30px; line-height: 1; display: flex; align-items: center; justify-content: center; cursor: pointer; } +.iwb-album-fs-nav:hover { background: rgba(255, 255, 255, .22); } +.iwb-album-fs-nav.prev { left: 18px; } +.iwb-album-fs-nav.next { right: 18px; } +.iwb-album-fs.single .iwb-album-fs-nav { display: none; } +/* 计数条要放「复制/下载」按钮,所以本身吃事件;点条上空白仍然算点外部(关闭) */ +.iwb-album-fs-bar { position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%); display: flex; align-items: center; gap: 10px; max-width: 80vw; padding: 6px 14px; border-radius: 999px; background: rgba(255, 255, 255, .12); color: #fff; font-size: 12px; } +.iwb-album-fs-btn { height: 22px; padding: 0 9px; border: 1px solid rgba(255, 255, 255, .22); border-radius: 999px; background: rgba(255, 255, 255, .08); color: #fff; font-size: 11px; cursor: pointer; flex: 0 0 auto; } +.iwb-album-fs-btn:hover { background: rgba(255, 255, 255, .22); } +.iwb-album-fs-count { font-variant-numeric: tabular-nums; } +.iwb-album-fs-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; opacity: .72; } +.iwb-album-fs-hint { position: fixed; left: 50%; bottom: 6px; transform: translateX(-50%); color: rgba(255, 255, 255, .45); font-size: 11px; white-space: nowrap; pointer-events: none; } .iwb-album-add { border: 1px dashed var(--iwb-border); background: transparent; color: var(--iwb-node-text2); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: var(--iwb-space-2); font-size: var(--iwb-font-2xs); border-radius: var(--iwb-radius-chip); cursor: pointer; } .iwb-album-add b { font-size: 15px; line-height: 1; font-weight: 600; } .iwb-album-pager { border-top: 1px solid color-mix(in srgb, var(--iwb-border) 55%, transparent); height: 26px; } diff --git a/plugins/index.json b/plugins/index.json index 0463d5e..30343c3 100644 --- a/plugins/index.json +++ b/plugins/index.json @@ -4,10 +4,10 @@ "dir": "album", "main": "main.js", "style": "styles.css", - "version": "1.1.1", + "version": "1.1.2", "files": { - "main.js": "7504415606e494d7f9c3dafc67aff032fb7f5a18b7851ab897fcf303567b04c0", - "styles.css": "02ffd5a87281c955aa10f323b0f36487dc8e2a8dd394bc500273cb4e6f5db509" + "main.js": "3e60b95d30bce1b734c3c1a251fa1091b8d54786eb609737f1eec9ffce3d9b22", + "styles.css": "b61e806abee9dd8889aa1a12c8791d428feacab93fac64f89c18c589de390706" } }, { diff --git a/site/dev/index.html b/site/dev/index.html index 843e98c..02c9c7c 100644 --- a/site/dev/index.html +++ b/site/dev/index.html @@ -3,7 +3,7 @@ 插件开发规范 · Image Workbench - +