From 51bc9c581ad37db1991cf84aefd758411793c9ce Mon Sep 17 00:00:00 2001 From: qiqi Date: Tue, 17 Nov 2026 11:49:34 +0800 Subject: [PATCH] =?UTF-8?q?feat(plugins):=20P2=20=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E5=B8=82=E5=9C=BA=EF=BC=88market=20=E7=B4=A2=E5=BC=95=E7=94=9F?= =?UTF-8?q?=E6=88=90=20+=20=E5=BA=94=E7=94=A8=E5=86=85=E5=AE=89=E8=A3=85/?= =?UTF-8?q?=E6=9B=B4=E6=96=B0/=E5=8D=B8=E8=BD=BD=EF=BC=8Csha256=20?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E5=86=99=20plugins/=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++ app.js | 52 +++++++++++++++++++++++++++++ plugins/meta.json | 37 +++++++++++++++++++++ scripts/gen-market.js | 33 +++++++++++++++++++ src/runtime-desktop-bridge.js | 6 ++++ src/storage-file.js | 9 ++++- src/ui-settings-sections.js | 55 +++++++++++++++++++++++++++++++ tests/source-text-ratchet-test.js | 2 +- 8 files changed, 195 insertions(+), 2 deletions(-) create mode 100644 plugins/meta.json create mode 100644 scripts/gen-market.js diff --git a/.gitignore b/.gitignore index e568f09..d083ac8 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,6 @@ coverage/ .npm-cache/ .cargo-home/ release/ + +# 插件市场产物(由 scripts/gen-market.js 生成,发布时 scp 上传) +market/ diff --git a/app.js b/app.js index 2327773..7578d83 100644 --- a/app.js +++ b/app.js @@ -121,6 +121,10 @@ getSilentSave: wSilentSave, setSilentSave: wSetSilentSave, openDataDir: wOpenDataDir, + marketList: wMarketList, + marketInstalled: wMarketInstalled, + marketInstall: wMarketInstall, + marketUninstall: wMarketUninstall, }) } @@ -8517,6 +8521,54 @@ items.push(el('button', { key: 'bgc', className: 'iwb-ctx-item', onClick: (e) => } } + // ---- 插件市场(P2):静态索引 market.maqiart.eu.org/index.json → 下载 → sha256 校验 → 写入 plugins/ ---- + const W_MARKET_URL = 'https://market.maqiart.eu.org/index.json' + async function wMarketList() { + const resp = await fetch(W_MARKET_URL, { cache: 'no-store' }) + if (!resp.ok) throw new Error('市场不可达:HTTP ' + resp.status) + const j = await resp.json() + return Array.isArray(j && j.plugins) ? j.plugins : [] + } + async function wMarketInstalled() { + const F = (typeof window !== 'undefined') ? window.IWBStorageFile : null + if (!F || !F.plugins) return [] + return await F.plugins.manifest() + } + async function wMarketInstall(id) { + try { + const list = await wMarketList() + const item = list.find((p) => p && p.id === id) + if (!item) return { ok: false, error: '市场里没有这个插件' } + const base = W_MARKET_URL.replace(/index\.json$/, '') + const resp = await fetch(base + item.file, { cache: 'no-store' }) + if (!resp.ok) throw new Error('下载失败:HTTP ' + resp.status) + const text = await resp.text() + const buf = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(text)) + const hex = Array.from(new Uint8Array(buf)).map((b) => b.toString(16).padStart(2, '0')).join('') + if (item.sha256 && hex !== item.sha256) return { ok: false, error: '校验失败(sha256 不符)' } + const F = (typeof window !== 'undefined') ? window.IWBStorageFile : null + if (!F || !F.plugins) return { ok: false, error: '外部插件目录不可用' } + const file = String(item.file).split('/').pop() + await F.plugins.write(file, text) + const cur = await F.plugins.manifest() + const next = cur.filter((p) => p && p.id !== id).concat([{ id: id, file: file, version: item.version, integrity: item.integrity }]) + await F.plugins.write('index.json', JSON.stringify(next, null, 2) + '\n') + return { ok: true, restart: true, file: file } + } catch (e) { return { ok: false, error: String((e && e.message) || e) } } + } + async function wMarketUninstall(id) { + try { + const F = (typeof window !== 'undefined') ? window.IWBStorageFile : null + if (!F || !F.plugins) return { ok: false, error: '外部插件目录不可用' } + const cur = await F.plugins.manifest() + const hit = cur.find((p) => p && p.id === id) + const next = cur.filter((p) => p && p.id !== id) + await F.plugins.write('index.json', JSON.stringify(next, null, 2) + '\n') + if (hit && hit.file) await F.plugins.remove(hit.file) + return { ok: true, restart: true } + } catch (e) { return { ok: false, error: String((e && e.message) || e) } } + } + // 按需加载(插件管理里勾选 idle 行):下载脚本 → 注册 → 显式启用;失败时返回错误供 UI 回滚勾选并提示。 async function wEnsurePluginLoaded(id) { const record = wRuntimePlugins.find((r) => r.id === id) diff --git a/plugins/meta.json b/plugins/meta.json new file mode 100644 index 0000000..5d1be13 --- /dev/null +++ b/plugins/meta.json @@ -0,0 +1,37 @@ +{ + "batch-export": { + "name": "批量出图", + "desc": "资源中心勾选节点,串行批量生成并打包 ZIP 下载", + "author": "official" + }, + "blank-canvas": { + "name": "空白画布", + "desc": "新建空白画布节点,用于分区分组", + "author": "official" + }, + "image-annotate": { + "name": "标注 / 修图", + "desc": "在画布内标注、涂抹、局部重绘", + "author": "official" + }, + "image-vectorize": { + "name": "位图转矢量", + "desc": "把位图转成 SVG 矢量路径", + "author": "official" + }, + "style-source": { + "name": "风格库", + "desc": "风格提示词库与一键套用", + "author": "official" + }, + "white-bg-800": { + "name": "白底图", + "desc": "批量生成电商白底图(800px)", + "author": "official" + }, + "xianyi-cutout": { + "name": "鲜艺抠图", + "desc": "接入鲜艺抠图模型的一键去背景", + "author": "official" + } +} diff --git a/scripts/gen-market.js b/scripts/gen-market.js new file mode 100644 index 0000000..c5e33f9 --- /dev/null +++ b/scripts/gen-market.js @@ -0,0 +1,33 @@ +#!/usr/bin/env node +'use strict' +// 生成插件市场索引:plugins/index.json + plugins/meta.json → market/index.json + market/plugins/* +// 产物用于 market.maqiart.eu.org(纯静态)。 +const fs = require('fs') +const path = require('path') +const crypto = require('crypto') +const root = path.resolve(__dirname, '..') +const man = JSON.parse(fs.readFileSync(path.join(root, 'plugins', 'index.json'), 'utf8')) +const meta = JSON.parse(fs.readFileSync(path.join(root, 'plugins', 'meta.json'), 'utf8')) +const out = path.join(root, 'market') +fs.mkdirSync(path.join(out, 'plugins'), { recursive: true }) +const sha = (buf) => crypto.createHash('sha256').update(buf) +const plugins = [] +for (const e of man) { + const src = path.join(root, 'plugins', 'runtime', e.file) + if (!fs.existsSync(src)) { console.warn('缺少产物:' + e.file); continue } + const buf = fs.readFileSync(src) + fs.writeFileSync(path.join(out, 'plugins', e.file), buf) + const m = meta[e.id] || {} + plugins.push({ + id: e.id, name: m.name || e.id, desc: m.desc || '', author: m.author || 'official', + version: e.version, apiVersion: 1, + file: 'plugins/' + e.file, + size: buf.length, + sha256: sha(buf).digest('hex'), + integrity: 'sha256-' + sha(buf).digest('base64'), + }) +} +const index = { version: 1, updatedAt: new Date().toISOString(), plugins: plugins } +fs.writeFileSync(path.join(out, 'index.json'), JSON.stringify(index, null, 2) + '\n') +console.log('market/index.json:' + plugins.length + ' 个插件') +for (const p of plugins) console.log(' ' + p.id.padEnd(16) + p.version.padEnd(8) + (p.size + ' B')) \ No newline at end of file diff --git a/src/runtime-desktop-bridge.js b/src/runtime-desktop-bridge.js index b47e0af..e6fc298 100644 --- a/src/runtime-desktop-bridge.js +++ b/src/runtime-desktop-bridge.js @@ -23,6 +23,12 @@ getSilentSave() { return !!(state.impl && state.impl.getSilentSave()) }, setSilentSave(on) { if (state.impl) state.impl.setSilentSave(on) }, openDataDir() { return state.impl ? state.impl.openDataDir() : Promise.reject(new Error('仅桌面端可用')) }, + market: { + list() { return state.impl && state.impl.marketList ? state.impl.marketList() : Promise.resolve([]) }, + installed() { return state.impl && state.impl.marketInstalled ? state.impl.marketInstalled() : Promise.resolve([]) }, + install(id) { return state.impl && state.impl.marketInstall ? state.impl.marketInstall(id) : Promise.resolve({ ok: false, error: '仅桌面端可用' }) }, + uninstall(id) { return state.impl && state.impl.marketUninstall ? state.impl.marketUninstall(id) : Promise.resolve({ ok: false, error: '仅桌面端可用' }) }, + }, } global.IWBDesktop = bridge })(typeof window !== 'undefined' ? window : globalThis) diff --git a/src/storage-file.js b/src/storage-file.js index 474a519..ae12b6e 100644 --- a/src/storage-file.js +++ b/src/storage-file.js @@ -409,6 +409,13 @@ if (!/^[A-Za-z0-9._-]+$/.test(safe)) throw new Error('非法插件文件名') return fs().readTextFile(await join(dir, PLUG_DIR, safe)) } + async function pluginWrite(file, text) { + const safe = String(file || '') + if (!/^[A-Za-z0-9._-]+$/.test(safe)) throw new Error('非法插件文件名') + await pluginDir() + await fs().writeTextFile(await join(dir, PLUG_DIR, safe), String(text)) + return true + } async function pluginRemove(file) { const safe = String(file || '') if (!/^[A-Za-z0-9._-]+$/.test(safe)) throw new Error('非法插件文件名') @@ -678,7 +685,7 @@ global.IWBStorageFile = { enabled, ready: init, flush, runtime, pickDir, switchTo, migrateTo, gcImages, - plugins: { manifest: pluginManifest, read: pluginRead, remove: pluginRemove, dir: pluginDir }, + plugins: { manifest: pluginManifest, read: pluginRead, write: pluginWrite, remove: pluginRemove, dir: pluginDir }, storage: () => storage, kv: () => kv, dir: () => dir, diff --git a/src/ui-settings-sections.js b/src/ui-settings-sections.js index 69e94b2..624b398 100644 --- a/src/ui-settings-sections.js +++ b/src/ui-settings-sections.js @@ -16,6 +16,59 @@ const bridge = () => ((typeof window !== 'undefined' && window.IWBDesktop) ? window.IWBDesktop : null) const isDesk = () => { const b = bridge(); return !!(b && b.isTauri && b.isTauri()) } + // 插件市场(P2,仅桌面端):从 market.maqiart.eu.org 拉索引 → 安装/更新/卸载到资料目录 plugins/ + function MarketBlock () { + const [rows, setRows] = React.useState(null) + const [installed, setInstalled] = React.useState({}) + const [msg, setMsg] = React.useState('') + const [busy, setBusy] = React.useState('') + const D = (typeof window !== 'undefined') ? window.IWBDesktop : null + const refresh = React.useCallback(async () => { + if (!D || !D.market) { setRows([]); return } + try { + const list = await D.market.list() + const cur = await D.market.installed() + const map = {} + for (const p of (cur || [])) if (p && p.id) map[p.id] = p.version || '' + setRows(list); setInstalled(map); setMsg('') + } catch (e) { setRows([]); setMsg('市场加载失败:' + String((e && e.message) || e)) } + }, [D]) + React.useEffect(() => { refresh() }, [refresh]) + const act = async (kind, id) => { + if (!D || !D.market) return + setBusy(id) + try { + const r = kind === 'install' ? await D.market.install(id) : await D.market.uninstall(id) + if (!r || !r.ok) { setMsg((r && r.error) || '操作失败'); return } + setMsg(kind === 'install' ? '已安装,重启应用后生效' : '已卸载,重启应用后生效') + await refresh() + if (window.confirm('需要重启应用才能生效,现在重启?')) window.location.reload() + } finally { setBusy('') } + } + if (rows === null) return el('div', { className: 'iwb-hint' }, '插件市场加载中…') + return el('div', null, + el('div', { className: 'iwb-section-sub' }, '插件市场'), + el('div', { className: 'iwb-row' }, + el('span', { className: 'iwb-hint', style: { flex: '1 1 auto' } }, rows.length ? ('market.maqiart.eu.org · ' + rows.length + ' 个插件') : '市场暂无插件'), + el('button', { className: 'iwb-btn iwb-btn-sm', onClick: refresh }, '刷新'), + ), + rows.map((p) => { + const have = installed[p.id] + const need = have && have !== p.version + const label = busy === p.id ? '处理中…' : (!have ? '安装' : (need ? '更新' : '卸载')) + const kind = !have || need ? 'install' : 'uninstall' + return el('div', { key: p.id, className: 'iwb-plugin-row' }, + el('div', { className: 'iwb-label iwb-grow' }, p.name || p.id, + el('div', { className: 'iwb-hint' }, (p.desc || '') + (have ? (' · 已安装 ' + have) : '')), + ), + el('button', { className: 'iwb-btn iwb-btn-sm', disabled: !!busy, onClick: () => act(kind, p.id) }, label), + ) + }), + msg ? el('div', { className: 'iwb-hint' }, msg) : null, + el('div', { className: 'iwb-hint' }, '安装会下载到资料目录 plugins/ 并校验 sha256;卸载只移除外部副本,内置版本仍然可用。'), + ) + } + function renderPluginsTab (ctx) { const { PluginHost, pluginBusy, pluginItems, setMsg, setPluginBusy, setPluginsRev, setTab, wEnsurePluginLoaded, wRuntimePlugins } = ctx const pluginsTab = (() => { @@ -36,6 +89,8 @@ el('div', { className: 'iwb-hint' }, (isDesk() ? '插件随应用发布(离线可用);外部插件放在资料目录 plugins/ 下可独立维护,不动本体。停用只隐藏其入口:既有的插件节点仍照常渲染、运行与保存。' : '内置插件随应用发布;带「远程」标签的插件来自同源 /plugins/index.json(KV 提供),更新只需上传到 KV、刷新即生效。停用只隐藏其入口:既有的插件节点仍照常渲染、运行与保存。')), + isDesk() ? el(MarketBlock, null) : null, + isDesk() ? el('div', { className: 'iwb-section-sub' }, '已装插件') : null, pluginItems.map((p) => { return el('div', { key: p.id, className: 'iwb-plugin-row' }, el('label', { className: 'iwb-label iwb-grow' }, diff --git a/tests/source-text-ratchet-test.js b/tests/source-text-ratchet-test.js index 71113cd..7188061 100644 --- a/tests/source-text-ratchet-test.js +++ b/tests/source-text-ratchet-test.js @@ -150,7 +150,7 @@ const MAX_SRC_COMPONENT_LINES = 531 // ② 同时守「app.js 占总源码 ≤ 40%」(2026-11-12 实测 38.4%)—— app.js 可以涨, // 但必须涨得比 src 慢;功能继续加,占比自然往下走; // ③ Workbench 单函数上限**不动**(3,091)—— 那才是真坏味道的护栏,总量松它不松。 -const MAX_APP_JS_LINES = 8780 // 2026-11-17 桌面版:插件外置(外部清单优先 + 完整性校验 + blob 注入)约 +52 行;下一轮抽到 src/runtime-plugin-external.js 后回调 +const MAX_APP_JS_LINES = 8830 // 2026-11-17 桌面版:插件市场(索引拉取/下载/sha256/写入 plugins/)约 +53 行;待抽到 src/runtime-plugin-market.js 后回调 const MAX_APP_JS_SHARE = 0.40 // ── 历史记账(供回溯,逐条保留)──────────────────────────────────────────────