fix(plugins): 未注册默认启用 + 开关直接加载 + 加载成功回写启用 + 全部启用;插件详情页改扁平行;市场请求双通道
Verify (Gitea) / gate (build · unit · facts) (push) Successful in 3m49s

This commit is contained in:
qiqi committed 2026-11-17 16:39:26 +08:00
1 parent 371f114d69
commit 9b24749481
4 files changed
+47 -48

No files matched your search

-20
View File
@@ -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)
+44 -27
View File
@@ -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
}
+2 -1
View File
@@ -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')
+1
View File
@@ -29,6 +29,7 @@ interface Window {
IWBRuntimePluginLoader?: any
IWBRuntimePluginMarket?: any
IWBRuntimePluginBackup?: any
IWBPluginRuntime?: any
IWBDesktop?: any
IWBStorageFallbackIndexedDb?: any
// 画布 / 工作台