diff --git a/taskqueue/package.json b/taskqueue/package.json new file mode 100644 index 0000000..abd58fb --- /dev/null +++ b/taskqueue/package.json @@ -0,0 +1,12 @@ +{ + "name": "@iwb-plugin/taskqueue", + "private": true, + "version": "1.0.0", + "description": "任务队列:全局生图记录(链接/状态/画布节点),可下载落地图库、重试、复制链接、定位节点;数据由宿主保存", + "ibwPlugin": { + "id": "taskqueue", + "entry": "src/index.js", + "apiVersion": 1, + "style": "styles.css" + } +} diff --git a/taskqueue/src/index.js b/taskqueue/src/index.js new file mode 100644 index 0000000..809ce0a --- /dev/null +++ b/taskqueue/src/index.js @@ -0,0 +1,214 @@ +// iwb-plugin: 任务队列 —— 左栏整页 +// +// 数据全部在宿主:账本(image-ingest-store)、下载/重试(image-ingest)、落地图库、定位节点、记录上限。 +// 插件只做视图与手动操作;禁用插件时宿主仍记账(上限内)。 +;(function (global) { + 'use strict' + if (!global.IWBPluginHost) return + + var PAGE_ID = 'taskqueue-page' + var TABS = [['all', '全部'], ['ok', '已完成'], ['down', '下载中'], ['fail', '失败'], ['only', '仅记录']] + var STATUS = { ok: '已完成', down: '下载中', fail: '失败', only: '仅记录' } + var LIMITS = [200, 500, 1000, 2000, 5000] + + function el (tag, attrs) { + var node = document.createElement(tag) + if (attrs) { + for (var k in attrs) { + if (!Object.prototype.hasOwnProperty.call(attrs, k)) continue + var v = attrs[k] + if (v === null || v === undefined) continue + if (k === 'class') node.className = v + else if (k === 'text') node.textContent = v + else if (k === 'style') node.setAttribute('style', v) + else node.setAttribute(k, v) + } + } + for (var i = 2; i < arguments.length; i++) append(node, arguments[i]) + return node + } + function append (node, v) { + if (v === null || v === undefined || v === false) return + if (Array.isArray(v)) { for (var i = 0; i < v.length; i++) append(node, v[i]); return } + node.appendChild(typeof v === 'string' ? document.createTextNode(v) : v) + } + function clear (node) { while (node.firstChild) node.removeChild(node.firstChild) } + function fmtTime (ms) { + var d = new Date(Number(ms) || 0) + if (!isFinite(d.getTime())) return '' + var p = function (n) { return (n < 10 ? '0' : '') + n } + return p(d.getMonth() + 1) + '-' + p(d.getDate()) + ' ' + p(d.getHours()) + ':' + p(d.getMinutes()) + } + function fmtSize (w, h) { return (w > 0 && h > 0) ? (w + '×' + h) : '' } + + function mount (host, ctx) { + clear(host) + host.classList.add('iwb-plugin-node-body', 'iwb-tq') + var state = { filter: 'all', limit: 500, items: [], total: 0, loading: true, msg: '' } + var timer = 0 + var unsub = function () {} + + var head = el('div', { class: 'iwb-tq-head' }) + var sub = el('div', { class: 'iwb-tq-sub', text: '全局生图记录 · 数据由宿主保存,插件关闭也继续记录' }) + var tools = el('div', { class: 'iwb-tq-tools' }) + var limitSel = el('select', { class: 'iwb-input iwb-tq-limit', 'aria-label': '记录上限' }) + for (var li = 0; li < LIMITS.length; li++) limitSel.appendChild(el('option', { value: String(LIMITS[li]), text: String(LIMITS[li]) + ' 条' })) + var clearBtn = el('button', { class: 'iwb-btn iwb-btn-sm iwb-tq-ghost', text: '清空记录' }) + var refreshBtn = el('button', { class: 'iwb-btn iwb-btn-sm iwb-tq-ghost', text: '刷新' }) + tools.appendChild(el('span', { class: 'iwb-tq-tool-lbl', text: '记录上限' })) + tools.appendChild(limitSel) + tools.appendChild(clearBtn) + tools.appendChild(refreshBtn) + append(head, [sub, tools]) + + var tabs = el('div', { class: 'iwb-tq-tabs' }) + var tabEls = {} + for (var ti = 0; ti < TABS.length; ti++) { + var key = TABS[ti][0] + var t = el('button', { class: 'iwb-tq-tab' + (key === 'all' ? ' on' : ''), 'data-k': key }, + el('span', { text: TABS[ti][1] }), el('span', { class: 'iwb-tq-n', text: '0' })) + tabEls[key] = t + tabs.appendChild(t) + } + var list = el('div', { class: 'iwb-tq-list' }) + var foot = el('div', { class: 'iwb-tq-foot' }) + append(host, [head, tabs, list, foot]) + + function setMsg (m) { state.msg = m || ''; renderFoot() } + function renderFoot () { + clear(foot) + append(foot, [ + el('span', { text: '记录只保留链接与本机状态;上限内自动淘汰最旧记录,不影响 images/gallery 里的文件。' }), + el('span', { class: 'iwb-tq-hint', text: state.msg || '点击「下载」= 落地到图库(图库会自动刷新出现)' }), + ]) + } + + function setCounts (stats) { + var s = stats || {} + var map = { all: s.total || 0, ok: s.ok || 0, down: s.down || 0, fail: s.fail || 0, only: s.only || 0 } + for (var k in tabEls) { + var n = tabEls[k].querySelector('.iwb-tq-n') + if (n) n.textContent = String(map[k] || 0) + } + } + + function thumbOf (it) { + if (it.thumbUrl) return el('img', { class: 'iwb-tq-img', src: it.thumbUrl, alt: '', draggable: 'false' }) + return el('div', { class: 'iwb-tq-ph' }, + el('svg', { viewBox: '0 0 24 24', width: '20', height: '20', fill: 'none', stroke: 'currentColor', 'stroke-width': '1.6', 'aria-hidden': 'true' }, + el('path', { d: 'M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4' }), + el('polyline', { points: '7 10 12 15 17 10' }), + el('line', { x1: '12', x2: '12', y1: '15', y2: '3' })), + el('span', { text: '仅记录' })) + } + + function badgeOf (it) { + var label = STATUS[it.kind] || '记录' + if (it.kind === 'down' && it.progress > 0) label = '下载中 ' + Math.round(it.progress * 100) + '%' + return el('span', { class: 'iwb-tq-badge ' + (it.kind || ''), text: label }) + } + + function btn (label, cls, onClick) { + var b = el('button', { class: 'iwb-btn iwb-btn-sm iwb-tq-btn' + (cls ? ' ' + cls : ''), text: label }) + b.addEventListener('click', function (e) { e.stopPropagation(); onClick(b) }) + return b + } + + function rowOf (it) { + var meta = it.meta || {} + var thumb = el('div', { class: 'iwb-tq-thumb', title: '点击预览' }, thumbOf(it)) + thumb.addEventListener('click', function () { ctx.queue.preview(it.id) }) + var line1 = el('div', { class: 'iwb-tq-line1' }, el('span', { class: 'iwb-tq-name', text: String(meta.prompt || '生成结果') }), badgeOf(it)) + var bits = [] + if (meta.boardName) bits.push(meta.boardName) + if (it.createdAt) bits.push(fmtTime(it.createdAt)) + var sz = fmtSize(it.w, it.h); if (sz) bits.push(sz) + if (meta.model) bits.push(meta.model) + var metaLine = el('div', { class: 'iwb-tq-meta', text: bits.join(' · ') }) + var main = el('div', { class: 'iwb-tq-main' }, line1, metaLine) + if (it.kind === 'fail' && it.error) main.appendChild(el('div', { class: 'iwb-tq-err', text: it.error + (it.retries ? (' · 已重试 ' + it.retries + ' 次') : '') })) + var acts = el('div', { class: 'iwb-tq-actions' }) + if (it.kind === 'ok') acts.appendChild(el('button', { class: 'iwb-btn iwb-btn-sm iwb-tq-btn', text: '已在图库', disabled: 'disabled' })) + else if (it.kind === 'down') acts.appendChild(el('button', { class: 'iwb-btn iwb-btn-sm iwb-tq-btn', text: '下载中…', disabled: 'disabled' })) + else if (it.kind === 'fail') acts.appendChild(btn('重试', 'iwb-tq-primary', function () { ctx.queue.retry(it.id); setMsg('已重新排队'); schedule() })) + else acts.appendChild(btn('下载', 'iwb-tq-primary', function (b) { b.disabled = true; ctx.queue.land(it.id).then(function (r) { b.disabled = false; setMsg(r && r.already ? '已在图库' : (r && r.ok ? '已加入落地队列,图库会自动刷新' : '下载失败')) ; schedule() }) })) + acts.appendChild(btn('复制原始链接', '', function () { ctx.queue.copyLink(it.id).then(function (r) { setMsg(r && r.ok ? '链接已复制' : '复制失败') }) })) + var loc = btn('定位', '', function (b) { b.disabled = true; ctx.queue.locate(it.id).then(function (r) { b.disabled = false; setMsg(r && r.ok ? '已定位到节点' : ({ 'board-missing': '画布已删除', 'node-missing': '节点已删除', 'missing-meta': '没有定位信息' }[r && r.error] || '定位失败')) }) }) + if (!(meta.boardId && meta.nodeId)) { loc.disabled = true; loc.title = '没有画布/节点信息' } + acts.appendChild(loc) + return el('div', { class: 'iwb-tq-row ' + (it.kind || '') }, thumb, main, acts) + } + + function renderList () { + clear(list) + if (!state.items.length) { + list.appendChild(el('div', { class: 'iwb-tq-empty', text: state.loading ? '加载中…' : '暂无记录 · 生成后会自动出现在这里' })) + return + } + for (var i = 0; i < state.items.length; i++) list.appendChild(rowOf(state.items[i])) + } + + function load () { + state.loading = true + renderList() + Promise.all([ + ctx.queue.list({ status: state.filter, limit: state.limit }), + ctx.queue.stats(), + ctx.queue.getLimit(), + ]).then(function (rs) { + var r = rs[0] || {} + state.items = Array.isArray(r.items) ? r.items : [] + state.total = Number(r.total) || state.items.length + state.loading = false + setCounts(rs[1]) + var lim = Number(rs[2]) || 500 + if (LIMITS.indexOf(lim) < 0) lim = 500 + limitSel.value = String(lim) + state.limit = lim + renderList() + }).catch(function () { + state.loading = false; state.items = []; renderList(); setMsg('加载失败') + }) + } + function schedule () { clearTimeout(timer); timer = setTimeout(load, 250) } + + for (var k in tabEls) (function (key) { + tabEls[key].addEventListener('click', function () { + state.filter = key + for (var kk in tabEls) tabEls[kk].classList.toggle('on', kk === key) + load() + }) + })(k) + limitSel.addEventListener('change', function () { + var n = Number(limitSel.value) || 500 + state.limit = n + ctx.queue.setLimit(n).then(function () { setMsg('记录上限已改为 ' + n) ; load() }) + }) + clearBtn.addEventListener('click', function () { + if (!global.confirm('清空已完成/失败的记录?不会删除图库文件。')) return + ctx.queue.clear().then(function () { setMsg('已清空记录'); load() }) + }) + refreshBtn.addEventListener('click', function () { load() }) + + renderFoot() + load() + try { unsub = ctx.queue.onChanged(schedule) || function () {} } catch (e) { unsub = function () {} } + return function () { clearTimeout(timer); try { unsub() } catch (e) { /* ignore */ } } + } + + global.IWBPluginHost.register({ + id: 'taskqueue', + name: '任务队列', + version: '1.0.0', + apiVersion: 1, + capabilities: ['pages', 'queue'], + }, function (ctx) { + ctx.ui.registerPage({ + id: PAGE_ID, + title: '任务队列', + icon: 'queue', + mount: function (host) { return mount(host, ctx) }, + }) + return {} + }) +})(typeof window !== 'undefined' ? window : globalThis) diff --git a/taskqueue/styles.css b/taskqueue/styles.css new file mode 100644 index 0000000..40b243e --- /dev/null +++ b/taskqueue/styles.css @@ -0,0 +1,39 @@ +/* 任务队列页(插件)—— 颜色/字号/圆角/间距全部走宿主 token;结构尺寸允许字面量。 */ +.iwb-tq { display: flex; flex-direction: column; min-height: 100%; gap: var(--iwb-space-10); } +.iwb-tq-head { display: flex; align-items: flex-start; justify-content: space-between; gap: var(--iwb-space-16); flex-wrap: wrap; } +.iwb-tq-sub { color: var(--iwb-text2); font-size: var(--iwb-font-sm); } +.iwb-tq-tools { display: flex; align-items: center; gap: var(--iwb-space-8); } +.iwb-tq-tool-lbl { font-size: var(--iwb-font-sm); color: var(--iwb-text2); } +.iwb-tq-limit { height: 28px; padding: 0 var(--iwb-space-8); } +.iwb-tq-ghost { background: var(--iwb-panel); } +.iwb-tq-tabs { display: flex; gap: var(--iwb-space-4); border-bottom: 1px solid var(--iwb-border); } +.iwb-tq-tab { display: inline-flex; align-items: center; gap: var(--iwb-space-6); padding: var(--iwb-space-6) var(--iwb-space-10); border: 0; background: transparent; color: var(--iwb-text2); font: inherit; font-size: var(--iwb-font-sm); border-radius: var(--iwb-radius-card) var(--iwb-radius-card) 0 0; cursor: pointer; } +.iwb-tq-tab:hover { color: var(--iwb-text); background: var(--iwb-hover); } +.iwb-tq-tab.on { color: var(--iwb-accent-on-surface); background: var(--iwb-accent-tint); } +.iwb-tq-n { font-size: var(--iwb-font-2xs); line-height: 15px; padding: 0 var(--iwb-space-6); border-radius: var(--iwb-radius-full); border: 1px solid var(--iwb-border); color: var(--iwb-text2); } +.iwb-tq-tab.on .iwb-tq-n { border-color: var(--iwb-accent-on-surface); color: var(--iwb-accent-on-surface); background: transparent; } +.iwb-tq-list { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; gap: var(--iwb-space-8); } +.iwb-tq-row { display: grid; grid-template-columns: 96px minmax(0,1fr) auto; gap: var(--iwb-space-12); align-items: center; padding: var(--iwb-space-10); background: var(--iwb-panel); border: 1px solid var(--iwb-border); border-radius: var(--iwb-radius-card); } +.iwb-tq-row:hover { border-color: var(--iwb-hover-border); } +.iwb-tq-row.fail { border-color: var(--iwb-err-bd); background: color-mix(in srgb, var(--iwb-err) 9%, var(--iwb-panel)); } +.iwb-tq-thumb { width: 96px; height: 64px; border-radius: var(--iwb-radius-control); border: 1px solid var(--iwb-border); background: var(--iwb-layer2); overflow: hidden; display: grid; place-items: center; cursor: zoom-in; position: relative; } +.iwb-tq-img { width: 100%; height: 100%; object-fit: cover; display: block; } +.iwb-tq-ph { display: grid; place-items: center; gap: var(--iwb-space-2); color: var(--iwb-text2); font-size: var(--iwb-font-2xs); } +.iwb-tq-ph svg { width: 20px; height: 20px; } +.iwb-tq-main { min-width: 0; display: flex; flex-direction: column; gap: var(--iwb-space-4); } +.iwb-tq-line1 { display: flex; align-items: center; gap: var(--iwb-space-8); min-width: 0; } +.iwb-tq-name { font-size: var(--iwb-font-base); font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } +.iwb-tq-badge { flex: 0 0 auto; height: 18px; line-height: 18px; padding: 0 var(--iwb-space-8); border-radius: var(--iwb-radius-full); border: 1px solid var(--iwb-border2); background: var(--iwb-layer); color: var(--iwb-text2); font-size: var(--iwb-font-2xs); } +.iwb-tq-badge.ok { color: var(--iwb-ok); border-color: var(--iwb-ok-bd); background: var(--iwb-ok-bg); } +.iwb-tq-badge.fail { color: var(--iwb-err); border-color: var(--iwb-err-bd); background: var(--iwb-err-bg); } +.iwb-tq-badge.only { color: var(--iwb-warn); border-color: var(--iwb-warn-bd); background: var(--iwb-warn-bg); } +.iwb-tq-meta { font-size: var(--iwb-font-sm); color: var(--iwb-text2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } +.iwb-tq-err { font-size: var(--iwb-font-xs); color: var(--iwb-err); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } +.iwb-tq-actions { display: flex; align-items: center; gap: var(--iwb-space-6); flex-wrap: wrap; justify-content: flex-end; } +.iwb-tq-btn { white-space: nowrap; } +.iwb-tq-btn.iwb-tq-primary { background: var(--iwb-brand); color: var(--iwb-on-brand); border-color: var(--iwb-brand); } +.iwb-tq-btn.iwb-tq-primary:hover { filter: brightness(1.05); } +.iwb-tq-empty { padding: var(--iwb-space-32); text-align: center; color: var(--iwb-text2); font-size: var(--iwb-font-sm); } +.iwb-tq-foot { display: flex; justify-content: space-between; gap: var(--iwb-space-12); color: var(--iwb-text2); font-size: var(--iwb-font-xs); } +.iwb-tq-hint { color: var(--iwb-text-dim); } +@media (max-width: 900px) { .iwb-tq-row { grid-template-columns: 72px minmax(0,1fr); } .iwb-tq-actions { grid-column: 2; justify-content: flex-start; } .iwb-tq-thumb { width: 72px; height: 50px; } }