From 61b279a2cae46b1192432e3dad2337be503ed967 Mon Sep 17 00:00:00 2001 From: qiqi Date: Fri, 20 Nov 2026 14:20:41 +0800 Subject: [PATCH] =?UTF-8?q?feat(gallery):=20=E6=90=9C=E7=B4=A2=20+=20?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E7=BB=9F=E8=AE=A1/=E5=85=A8=E9=83=A8?= =?UTF-8?q?=E9=87=8D=E8=AF=95=20+=20=E5=8F=B3=E9=94=AE=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=20+=20=E5=A4=9A=E9=80=89=E6=8B=96=E6=8B=BD=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E6=8F=92=E5=85=A5=20v1.6.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gallery/package.json | 2 +- gallery/src/index.js | 134 ++++++++++++++++++++++++++++++++++++++++--- gallery/styles.css | 11 +++- 3 files changed, 138 insertions(+), 9 deletions(-) diff --git a/gallery/package.json b/gallery/package.json index ac0b64c..99230e1 100644 --- a/gallery/package.json +++ b/gallery/package.json @@ -1,7 +1,7 @@ { "name": "@iwb-plugin/gallery", "private": true, - "version": "1.5.1", + "version": "1.6.0", "description": "图库:右栏图片墙(筛选/多选/预览/插入画布/下载/删除),数据由宿主 gallery 能力提供", "ibwPlugin": { "id": "gallery", diff --git a/gallery/src/index.js b/gallery/src/index.js index 95e2c02..be4eab5 100644 --- a/gallery/src/index.js +++ b/gallery/src/index.js @@ -83,8 +83,18 @@ var picked = {} var gridW = 320 var raf = 0 + var query = '' // v1.6.0:搜索(名称/标签),不持久化,重开面板即清空 + var failed = {} // v1.6.0:缩略图仍失败的 name 集合(据此显示「N 张失败」+ 全部重试) var head = el('div', { class: 'iwb-gal-head' }) + var searchIn = el('input', { class: 'iwb-input iwb-gal-search', placeholder: '搜索图片名…', value: '' }) + var searchTimer = 0 + searchIn.addEventListener('input', function () { + query = searchIn.value + clearTimeout(searchTimer) + searchTimer = setTimeout(function () { limit = 100; load() }, 220) + }) + head.appendChild(searchIn) var actions = el('div', { class: 'iwb-gal-actions' }) head.appendChild(actions) @@ -95,6 +105,9 @@ var countEl = el('span', { class: 'iwb-gal-count', text: '0 / 0 张' }) var pickedEl = el('span', { class: 'iwb-gal-sel', text: '' }) var statusEl = el('span', { class: 'iwb-gal-status', text: '' }) + var failEl = el('span', { class: 'iwb-gal-fail', text: '' }) + failEl.style.display = 'none' + failEl.addEventListener('click', function () { retryAll() }) var refreshBtn = iconBtn('刷新', function (svg) { svgPath(svg, 'M13.2 6.4A5.4 5.4 0 1 0 13.4 9.4') svgPath(svg, 'M13.2 2.6v3.8H9.4') @@ -124,7 +137,7 @@ // v1.4.2:刷新与设置同放右侧工具组(此前刷新在左侧信息区,空间不足时会向右溢出、压住设置图标) var tools = el('div', { class: 'iwb-gal-tools' }, refreshBtn, setBtn) var foot = el('div', { class: 'iwb-gal-foot' }, - el('div', { class: 'iwb-gal-foot-left' }, countEl, pickedEl, statusEl), tools) + el('div', { class: 'iwb-gal-foot-left' }, countEl, pickedEl, failEl, statusEl), tools) var wrap = el('div', { class: 'iwb-history-embedded' }, [head, grid, more, empty, foot, settings]) function setOpen (open) { @@ -142,6 +155,91 @@ host.appendChild(wrap) function count () { return Object.keys(picked).length } + + // ---- v1.6.0 右键菜单:单张操作(插入 / 下载 / 预览 / 复制文件名 / 删除)---- + var menuEl = null + function closeMenu () { + if (menuEl && menuEl.parentNode) menuEl.parentNode.removeChild(menuEl) + menuEl = null + document.removeEventListener('pointerdown', onMenuDown, true) + document.removeEventListener('keydown', onMenuKey, true) + } + function onMenuDown (e) { if (menuEl && !menuEl.contains(e.target)) closeMenu() } + function onMenuKey (e) { if (e.key === 'Escape') closeMenu() } + function openMenu (e, it) { + e.preventDefault(); e.stopPropagation() + closeMenu() + var menu = el('div', { class: 'iwb-ctxmenu iwb-gal-menu', style: 'left:' + e.clientX + 'px;top:' + e.clientY + 'px' }) + menu.appendChild(el('div', { class: 'iwb-ctx-title', text: it.name })) + function item (label, fn) { + var b = el('button', { class: 'iwb-ctx-item', text: label }) + b.addEventListener('click', function (ev) { ev.stopPropagation(); closeMenu(); fn() }) + menu.appendChild(b) + } + item('插入画布', function () { + Promise.resolve(ctx.gallery.insert([it.name], { insertK: cfg.insertK || '1k' })) + .then(function () { setStatus('已插入:' + it.name) }) + .catch(function () { setStatus('插入失败') }) + }) + item('下载', function () { doDownload([it.name]) }) + item('预览大图', function () { try { ctx.gallery.preview(it.name) } catch (err) { /* ignore */ } }) + item('复制文件名', function () { + try { + var p = global.navigator && global.navigator.clipboard && global.navigator.clipboard.writeText(it.name) + if (p && p.then) p.then(function () { setStatus('已复制文件名') }).catch(function () { setStatus('复制失败(剪贴板不可用)') }) + else setStatus('复制失败(剪贴板不可用)') + } catch (err) { setStatus('复制失败(剪贴板不可用)') } + }) + item('删除这张', function () { + var C = global.IWBUIConfirm + var ask = (C && C.askOrNative) ? C.askOrNative('删除「' + it.name + '」?不可恢复。', { title: '删除图片', okText: '删除' }) : Promise.resolve(global.confirm('删除「' + it.name + '」?')) + ask.then(function (ok) { + if (!ok) return + items = items.filter(function (x) { return x.name !== it.name }) + total = Math.max(0, total - 1) + delete picked[it.name] + render() + Promise.resolve(ctx.gallery.remove([it.name])).then(function () { setTimeout(load, 250) }).catch(function () {}) + }).catch(function () {}) + }) + document.body.appendChild(menu) + menuEl = menu + setTimeout(function () { + document.addEventListener('pointerdown', onMenuDown, true) + document.addEventListener('keydown', onMenuKey, true) + }, 0) + } + + // ---- v1.6.0 多选拖入画布:宿主 drop 只认单张,这里在捕获阶段接管整批 ---- + // 仅当 dataTransfer 带 application/x-iwb-gallery-batch 时生效,其余拖放行为完全不变。 + function onDocDrop (e) { + var raw = '' + try { raw = (e.dataTransfer && e.dataTransfer.getData('application/x-iwb-gallery-batch')) || '' } catch (err) { return } + if (!raw) return + var names = [] + try { names = JSON.parse(raw) } catch (err) { return } + if (!names || names.length < 2) return + e.preventDefault(); e.stopPropagation() + Promise.resolve(ctx.gallery.insert(names, { insertK: cfg.insertK || '1k' })).then(function (r) { + if (r && r.ok === false) { setStatus('批量插入失败'); return } + setStatus('已插入 ' + names.length + ' 张') + }).catch(function () { setStatus('批量插入失败') }) + } + document.addEventListener('drop', onDocDrop, true) + // v1.6.0:失败计数集中在底栏;点它 = 全部重试(整墙重挂,重新向宿主取缩略图) + function updateFail () { + var n = Object.keys(failed).length + if (!n) { failEl.style.display = 'none'; failEl.textContent = ''; return } + failEl.style.display = '' + failEl.textContent = n + ' 张失败 · 重试' + failEl.title = '点击重新加载这 ' + n + ' 张的缩略图' + } + function retryAll () { + failed = {} + updateFail() + setStatus('重试中…') + render() + } function pickedNames () { return Object.keys(picked) } function renderHead () { @@ -156,6 +254,14 @@ } actions.appendChild(btn('全选', '全选当前筛选结果', function () { for (var i = 0; i < items.length; i++) picked[items[i].name] = 1; render() })) actions.appendChild(btn('反选', '反选当前筛选结果', function () { for (var i = 0; i < items.length; i++) { var k = items[i].name; if (picked[k]) delete picked[k]; else picked[k] = 1 } render() })) + actions.appendChild(btn('插入', '把选中的图片插入画布(尺寸按「设置 → 插入尺寸」)', function () { + var names = pickedNames() + var k = cfg.insertK || '1k' + Promise.resolve(ctx.gallery.insert(names, { insertK: k })).then(function (r) { + if (r && r.ok === false) { setStatus('插入失败'); return } + setStatus('已插入 ' + names.length + ' 张(' + String(k).toUpperCase() + ')') + }).catch(function () { setStatus('插入失败') }) + }, true)) actions.appendChild(btn('下载', '下载选中的图片', function () { doDownload(pickedNames()) }, true)) actions.appendChild(btn('删除', '删除选中的图片(不可恢复)', function () { var C = global.IWBUIConfirm @@ -237,7 +343,9 @@ function fail (text) { holder.textContent = text || '缩略图加载失败(点击重试)' holder.style.cursor = 'pointer' - holder.onclick = function () { tries = 0; boot() } + holder.onclick = function () { failed[it.name] = 1; tries = 0; boot() } + failed[it.name] = 1 + updateFail() } function boot () { holder.style.cursor = '' @@ -252,6 +360,7 @@ // naturalWidth=0 说明拿到的是坏图(空 data URL / 截断文件):按失败处理,别留白块 if (!img.naturalWidth) { tries++; if (tries <= 2) { setTimeout(boot, 400 * tries); return } if (img.parentNode === box) box.replaceChild(holder, img); fail('缩略图加载失败(点击重试)'); return } if (holder.parentNode === box) box.replaceChild(img, holder) + if (failed[it.name]) { delete failed[it.name]; updateFail() } }) img.addEventListener('error', function () { tries++ @@ -263,8 +372,14 @@ img.addEventListener('dblclick', function () { ctx.gallery.preview(it.name) }) img.addEventListener('dragstart', function (e) { try { - e.dataTransfer.setData('application/x-iwb-localref', it.name) - e.dataTransfer.setData('application/x-iwb-gallery-orig', it.name) + var names = (picked[it.name] && count() > 1) ? pickedNames() : [it.name] + e.dataTransfer.setData('application/x-iwb-localref', names[0]) + e.dataTransfer.setData('application/x-iwb-gallery-orig', names[0]) + // 多选:额外带一批名字,由本插件在 document 捕获阶段接管(宿主 drop 只认单张) + if (names.length > 1) { + e.dataTransfer.setData('application/x-iwb-gallery-batch', JSON.stringify(names)) + setStatus('拖动 ' + names.length + ' 张') + } e.dataTransfer.effectAllowed = 'copy' } catch (err) { /* ignore */ } }) @@ -276,6 +391,7 @@ }).catch(function () { fail('缩略图加载失败(点击重试)') }) } boot() + box.addEventListener('contextmenu', function (e) { openMenu(e, it) }) var acts = el('div', { class: 'iwb-history-actions' }) function act (label, title, fn) { var b = el('button', { class: 'iwb-btn iwb-btn-sm', text: label, title: title }) @@ -313,10 +429,11 @@ function load (force) { if (force) setStatus('刷新中…') - return Promise.resolve(ctx.gallery.list({ range: cfg.range, limit: limit })).then(function (r) { + return Promise.resolve(ctx.gallery.list({ query: query, range: cfg.range, limit: limit })).then(function (r) { items = (r && r.items) || [] total = (r && r.total) || items.length lastLoadAt = Date.now() + failed = {} render() }).catch(function () { items = []; total = 0; render() }) } @@ -365,14 +482,17 @@ return function dispose () { if (ro) { try { ro.disconnect() } catch (e) { /* ignore */ } } - try { document.removeEventListener('pointerdown', onDocDown, true) } catch (e) { /* ignore */ } + try { document.removeEventListener('pointerdown', onMenuDown, true) } catch (e) { /* ignore */ } + try { document.removeEventListener('keydown', onMenuKey, true) } catch (e) { /* ignore */ } + try { document.removeEventListener('drop', onDocDrop, true) } catch (e) { /* ignore */ } + closeMenu() } } global.IWBPluginHost.register({ id: 'gallery', name: '图库', - version: '1.5.1', + version: '1.6.0', apiVersion: 1, capabilities: ['panels', 'gallery'], data: { schemaVersion: 1, maxBytes: 65536 }, diff --git a/gallery/styles.css b/gallery/styles.css index 0efd471..fe2f804 100644 --- a/gallery/styles.css +++ b/gallery/styles.css @@ -66,4 +66,13 @@ .iwb-history-actions .iwb-btn-sm { padding: 1px 6px; font-size: 10px; } .iwb-history-more { display: flex; justify-content: center; padding: 4px 0 2px; } /* 只作用于图库自己的空态提示:不加前缀会泄漏到设置页等使用 .iwb-hint 的地方(文字被居中) */ -.iwb-history-embedded .iwb-hint { font-size: 11px; color: var(--iwb-text2); text-align: center; padding: 12px 6px; } \ No newline at end of file +.iwb-history-embedded .iwb-hint { font-size: 11px; color: var(--iwb-text2); text-align: center; padding: 12px 6px; } +/* ---- v1.6.0:搜索框 / 失败统计 / 右键菜单标题 ---- */ +.iwb-gal-head { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; } +.iwb-gal-search { flex: 1 1 90px; min-width: 60px; height: 24px; font-size: 12px; } +.iwb-gal-fail { + font-size: 11px; color: var(--iwb-err); cursor: pointer; white-space: nowrap; + text-decoration: underline dotted; text-underline-offset: 2px; +} +.iwb-gal-fail:hover { color: var(--iwb-err); opacity: .85; } +.iwb-gal-menu .iwb-ctx-title { max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }