From 9b247494812a4dd9eba649b74dc004b59bac24ae Mon Sep 17 00:00:00 2001 From: qiqi Date: Tue, 17 Nov 2026 16:39:26 +0800 Subject: [PATCH] =?UTF-8?q?fix(plugins):=20=E6=9C=AA=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=90=AF=E7=94=A8=20+=20=E5=BC=80=E5=85=B3?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E5=8A=A0=E8=BD=BD=20+=20=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E6=88=90=E5=8A=9F=E5=9B=9E=E5=86=99=E5=90=AF=E7=94=A8=20+=20?= =?UTF-8?q?=E5=85=A8=E9=83=A8=E5=90=AF=E7=94=A8=EF=BC=9B=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E8=AF=A6=E6=83=85=E9=A1=B5=E6=94=B9=E6=89=81=E5=B9=B3=E8=A1=8C?= =?UTF-8?q?=EF=BC=9B=E5=B8=82=E5=9C=BA=E8=AF=B7=E6=B1=82=E5=8F=8C=E9=80=9A?= =?UTF-8?q?=E9=81=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 20 ----------- src/ui-settings-sections.js | 71 +++++++++++++++++++++++-------------- tests/plugin-host-test.js | 3 +- types/iwb-globals.d.ts | 1 + 4 files changed, 47 insertions(+), 48 deletions(-) diff --git a/app.js b/app.js index d4f4a47..39a6247 100644 --- a/app.js +++ b/app.js @@ -8539,26 +8539,6 @@ items.push(el('button', { key: 'bgc', className: 'iwb-ctx-item', onClick: (e) => return L.loadLocal(record) } - // 运行期启用(插件管理里把「未加载」的插件打开开关时调用):先加载脚本,再写启用状态 - async function wEnablePluginRuntime(id) { - try { - const rec = (wRuntimePlugins || []).find(function (r) { return r && r.id === id }) - if (!rec) return { ok: false, error: '插件清单里没有该 id' } - if (rec.status === 'loaded') { - const r0 = PluginHost && typeof PluginHost.setEnabled === 'function' ? PluginHost.setEnabled(id, true) : null - return r0 && r0.ok ? { ok: true } : { ok: false, error: (r0 && r0.error) || '启用失败' } - } - let loaded = false - if (rec.source === 'local') loaded = await wLoadLocalPluginEntry(rec) - else if (rec.source === 'external') loaded = await wLoadExternalPluginEntry(rec) - else loaded = await wLoadPluginEntry(rec) - if (!loaded) return { ok: false, error: rec.error || '脚本加载失败' } - const r = PluginHost && typeof PluginHost.setEnabled === 'function' ? PluginHost.setEnabled(id, true) : null - return r && r.ok ? { ok: true } : { ok: false, error: (r && r.error) || '启用失败' } - } catch (e) { return { ok: false, error: String((e && e.message) || e) } } - } - try { window.IWBPluginRuntime = { enable: wEnablePluginRuntime } } catch (e) { /* ignore */ } - // 按需加载(插件管理里勾选 idle 行):下载脚本 → 注册 → 显式启用;失败时返回错误供 UI 回滚勾选并提示。 async function wEnsurePluginLoaded(id) { const record = wRuntimePlugins.find((r) => r.id === id) diff --git a/src/ui-settings-sections.js b/src/ui-settings-sections.js index 2eb5eb1..15b6176 100644 --- a/src/ui-settings-sections.js +++ b/src/ui-settings-sections.js @@ -159,15 +159,27 @@ if (kind === 'install') setRestart(true) } finally { setBusy('') } } + // 用 src 模块加载脚本(不引 app.js 全局),成功后再写启用状态 + const loadRuntimePlugin = async function (id) { + const rec = (wRuntimePlugins || []).find(function (r) { return r && r.id === id }) + const L = (typeof window !== 'undefined') ? window.IWBRuntimePluginLoader : null + if (!rec || !L) { setMsg('插件清单里没有该 id'); return false } + let ok = false + try { + if (rec.source === 'local') ok = await L.loadLocal(rec) + else if (rec.source === 'external') ok = await L.loadExternal(rec) + else ok = await L.loadBuiltin(rec) + } catch (e) { setMsg('加载失败:' + String((e && e.message) || e)); return false } + if (!ok) { setMsg('加载失败:' + (rec.error || '脚本已加载但插件未注册')); return false } + return true + } const toggle = async function (id, on, registered) { setBusy(id) try { // 打开一个「未加载」的插件:先加载脚本,再写启用状态(否则只是记了个开关,插件依然不生效) - if (on && !registered && typeof window !== 'undefined' && window.IWBPluginRuntime) { - const r1 = await window.IWBPluginRuntime.enable(id) - if (!r1 || !r1.ok) { setMsg('启用失败:' + ((r1 && r1.error) || '未知错误')); return } - setMsg('已启用') - return + if (on && !registered) { + const okLoad = await loadRuntimePlugin(id) + if (!okLoad) return } const r = PluginHost.setEnabled(id, on) if (!r.ok) setMsg('切换失败:' + r.error) @@ -253,9 +265,9 @@ onClick: async function () { try { const F = (typeof window !== 'undefined') ? window.IWBStorageFile : null - let manifest = [] + let manifest = /** @type {any} */ ([]) try { manifest = await F.plugins.manifest() } catch (e) { manifest = String(e && e.message) } - let marketInfo = '' + let marketInfo = null try { const mm = await D.market.list(); marketInfo = { ok: true, count: (mm || []).length } } catch (e) { marketInfo = { ok: false, error: String((e && e.message) || e) } } const payload = { market: marketInfo, @@ -280,7 +292,7 @@ setBusy('__all__') let ok = 0 for (const p of list) { - try { const r = await window.IWBPluginRuntime.enable(p.id); if (r && r.ok) ok++ } catch (e) { /* ignore */ } + try { const good = await loadRuntimePlugin(p.id); if (good) { PluginHost.setEnabled(p.id, true); ok++ } } catch (e) { /* ignore */ } } setBusy('') setPluginsRev(function (v) { return v + 1 }) @@ -689,35 +701,40 @@ function renderPluginSettingsPage (ctx) { const { PluginHost, activePlugin, setMsg, setPluginsRev, setTab } = ctx const pluginSettingsPage = activePlugin ? el('div', { className: 'iwb-set-page' }, - el('div', { className: 'iwb-set-page-head' }, - el('div', { className: 'iwb-set-page-headtext' }, - el('div', { className: 'iwb-set-page-title' }, activePlugin.name), - el('div', { className: 'iwb-hint' }, activePlugin.id + ' · v' + activePlugin.version + (activePlugin.compatible ? '' : ' · apiVersion 不兼容') + (activePlugin.enabled ? ' · 已启用' : ' · 未启用')), + el('div', { className: 'iwb-mk-setrow' }, + el('div', { className: 'iwb-mk-setrow-main' }, + el('div', { className: 'iwb-mk-setrow-title' }, activePlugin.name, + el('span', { className: 'iwb-plugin-tag' + (activePlugin.enabled ? '' : ' iwb-plugin-tag-warn'), style: { marginLeft: 8 } }, activePlugin.enabled ? '已启用' : '未启用'), + activePlugin.compatible ? null : el('span', { className: 'iwb-plugin-tag iwb-plugin-tag-warn', style: { marginLeft: 4 } }, 'apiVersion 不兼容'), + ), + el('div', { className: 'iwb-hint' }, activePlugin.id + ' · v' + activePlugin.version), ), - el('button', { className: 'iwb-btn iwb-btn-sm', disabled: !activePlugin.compatible, onClick: () => { const r = PluginHost.setEnabled(activePlugin.id, !activePlugin.enabled); setPluginsRev((v) => v + 1); if (!r.ok) setMsg({ type: 'err', text: r.error }) } }, activePlugin.enabled ? '停用' : '启用'), + el('button', { className: 'iwb-btn iwb-btn-sm', onClick: function () { setTab('plugins') } }, '返回'), ), activePlugin.error ? el('div', { className: 'iwb-status-err' }, activePlugin.error) : null, - Object.keys(activePlugin.settingsSchema || {}).length ? Object.keys(activePlugin.settingsSchema).map((key) => { + el('div', { className: 'iwb-section-sub' }, '设置'), + Object.keys(activePlugin.settingsSchema || {}).length ? Object.keys(activePlugin.settingsSchema).map(function (key) { const spec = activePlugin.settingsSchema[key] || {} const value = activePlugin.settings && activePlugin.settings[key] !== undefined ? activePlugin.settings[key] : spec.default - const commit = (next) => { PluginHost.setSetting(activePlugin.id, key, next); setPluginsRev((v) => v + 1) } - return el('div', { key, className: 'iwb-set-row' }, - el('div', { className: 'iwb-set-row-text' }, - el('div', { className: 'iwb-set-row-title' }, spec.label || key), - spec.desc ? el('div', { className: 'iwb-set-row-desc' }, spec.desc) : null, + const commit = function (next) { PluginHost.setSetting(activePlugin.id, key, next); setPluginsRev(function (v) { return v + 1 }) } + return el('div', { key: key, className: 'iwb-mk-setrow' }, + el('div', { className: 'iwb-mk-setrow-main' }, + el('div', { className: 'iwb-mk-setrow-title' }, spec.label || key), + spec.desc ? el('div', { className: 'iwb-hint' }, spec.desc) : null, ), spec.type === 'boolean' - ? el('input', { type: 'checkbox', checked: Boolean(value), onChange: (e) => commit(e.target.checked) }) + ? el('label', { className: 'iwb-switch' }, + el('input', { type: 'checkbox', checked: Boolean(value), onChange: function (e) { commit(e.target.checked) } }), + el('span', { className: 'iwb-switch-track' }, el('span', { className: 'iwb-switch-dot' })), + ) : el('input', { - className: 'iwb-input', type: spec.type === 'number' ? 'number' : 'text', - value: value === undefined ? '' : String(value), - onChange: (e) => commit(spec.type === 'number' ? Number(e.target.value) : e.target.value), - }), + className: 'iwb-input', type: spec.type === 'number' ? 'number' : 'text', + style: { maxWidth: '260px' }, + value: value === undefined ? '' : String(value), + onChange: function (e) { commit(spec.type === 'number' ? Number(e.target.value) : e.target.value) }, + }), ) }) : el('div', { className: 'iwb-hint' }, '该插件没有可配置项。'), - el('div', { className: 'iwb-row', style: { marginTop: '12px' } }, - el('button', { className: 'iwb-btn iwb-btn-sm', onClick: () => setTab('plugins') }, '← 返回插件列表'), - ), ) : el('div', { className: 'iwb-hint' }, '插件不存在或已卸载。') return pluginSettingsPage } diff --git a/tests/plugin-host-test.js b/tests/plugin-host-test.js index 5fa9337..f33c188 100644 --- a/tests/plugin-host-test.js +++ b/tests/plugin-host-test.js @@ -227,7 +227,8 @@ const lazyHost = sandbox.IWBPluginHostFactory() const lazyState = { 'lazy-plugin': { enabled: true, settings: {}, data: {} } } lazyHost.init({ readState: () => lazyState, writeState: () => {}, log: () => {} }) assert.strictEqual(lazyHost.isEnabled('lazy-plugin'), true, '未注册插件也应读出持久化的启用开关') -assert.strictEqual(lazyHost.isEnabled('never-heard'), false, '持久化里没有的未注册插件应为 false') +// 2026-11-17:改为默认 true —— 启动早期持久化还没读到时,若按 false 处理会把所有插件判成"已停用"而进 idle +assert.strictEqual(lazyHost.isEnabled('never-heard'), true, '持久化里没有记录的未注册插件按默认启用(true)') assert.strictEqual(Object.keys(lazyState).join(','), 'lazy-plugin', 'isEnabled 查询不得为未注册插件创建 state 条目') console.log('plugin host checks passed') diff --git a/types/iwb-globals.d.ts b/types/iwb-globals.d.ts index f57b535..07b5578 100644 --- a/types/iwb-globals.d.ts +++ b/types/iwb-globals.d.ts @@ -29,6 +29,7 @@ interface Window { IWBRuntimePluginLoader?: any IWBRuntimePluginMarket?: any IWBRuntimePluginBackup?: any + IWBPluginRuntime?: any IWBDesktop?: any IWBStorageFallbackIndexedDb?: any // 画布 / 工作台