v4: 直接调用 ui.slot(component) 注册组件,在组件渲染阶段调用 keymap.layer 注册命令

This commit is contained in:
ypshidifu
2026-09-22 22:04:02 +08:00
parent 6d59c24126
commit 92fee7511f
+71 -80
View File
@@ -30,106 +30,97 @@ function delay(ms) {
export default Plugin.define({ export default Plugin.define({
id: "oc-session-all", id: "oc-session-all",
async setup(context) { async setup(context) {
log("[oc-session-all] v3 Plugin loaded! Trying to register command via ui.slot...") log("[oc-session-all] v4 Plugin loaded! Calling ui.slot(component) directly...")
// 先写入测试日志,确保 fs 可用
log("[oc-session-all] FS test: log file created successfully")
// 延时 3 秒,避免 toast 被覆盖 // 延时 5 秒,避免 toast 被覆盖(别人插件也会弹)
await delay(3000) await delay(5000)
// ── 探测 context.ui.slot ──────────────────────────────────────
const report = [] const report = []
if (context.ui && context.ui.slot) { // ── 探测 ui.slot 函数本身的元信息 ──────────────────────────────────────
report.push("ui.slot exists") if (context.ui && typeof context.ui.slot === "function") {
log("[oc-session-all] ui.slot type: " + typeof context.ui.slot) report.push("ui.slot fn name=" + context.ui.slot.name + " arity=" + context.ui.slot.length)
log("[oc-session-all] ui.slot is a function, name=" + context.ui.slot.name + ", arity=" + context.ui.slot.length)
// 尝试列出 slot 的所有方法
const slotMethods = Object.getOwnPropertyNames(context.ui.slot)
report.push("slot methods: " + slotMethods.join(", "))
log("[oc-session-all] slot methods: " + slotMethods.join(", "))
// 尝试调用 slot 的各种方法
for (const method of slotMethods) {
if (typeof context.ui.slot[method] === "function") {
log("[oc-session-all] slot." + method + " is a function")
}
}
} else { } else {
report.push("ui.slot NOT found") report.push("ui.slot NOT a function")
log("[oc-session-all] ui.slot is NOT a function, type=" + typeof (context.ui && context.ui.slot))
} }
// ── 尝试通过 ui.slot 注册组件 ────────────────────────────────────── // ── 直接调用 ui.slot(component) 注册组件 ──────────────────────────────────────
let slotReturn = "not called"
try { try {
// 创建一个简单的组件,尝试在渲染时注册命令 // 组件在 SolidJS 渲染上下文内执行,此时 Keymap.Provider 应该可用
const testComponent = () => { const ProbeComponent = () => {
log("[oc-session-all] Test component rendered!") log("[oc-session-all] ProbeComponent RENDERED inside slot!")
// 在组件渲染阶段尝试 keymap.layer(此时应在 Provider 内)
try {
if (context.keymap && typeof context.keymap.layer === "function") {
const layerResult = context.keymap.layer({
commands: [
{
id: "sessions-all",
title: "Show all sessions",
group: "Session",
run: () => {
log("[oc-session-all] /sessions-all command RUN!")
},
},
],
bindings: [
{
key: "ctrl+o",
command: "sessions-all",
},
],
})
log("[oc-session-all] keymap.layer() inside component: SUCCESS, result type=" + typeof layerResult)
report.push("layer-in-component: SUCCESS")
} else {
log("[oc-session-all] keymap.layer not available inside component")
report.push("layer-in-component: keymap.layer missing")
}
} catch (e) {
log("[oc-session-all] keymap.layer() inside component ERROR: " + e.message)
report.push("layer-in-component ERROR: " + e.message)
}
// 组件不渲染任何可见内容
return null return null
} }
// 尝试不同的注册方法 // 直接调用 slot 函数注册组件
if (context.ui.slot && typeof context.ui.slot.register === "function") { const ret = context.ui.slot(ProbeComponent)
context.ui.slot.register(testComponent) slotReturn = typeof ret
report.push("registered via slot.register()") report.push("ui.slot(component) returned type=" + typeof ret)
log("[oc-session-all] Registered component via slot.register()") log("[oc-session-all] ui.slot(component) called, returned type=" + typeof ret + ", value=" + String(ret))
} else if (context.ui.slot && typeof context.ui.slot.add === "function") {
context.ui.slot.add(testComponent)
report.push("registered via slot.add()")
log("[oc-session-all] Registered component via slot.add()")
} else if (context.ui.slot && typeof context.ui.slot.append === "function") {
context.ui.slot.append(testComponent)
report.push("registered via slot.append()")
log("[oc-session-all] Registered component via slot.append()")
} else {
report.push("no known slot registration method")
log("[oc-session-all] No known slot registration method found")
}
} catch (e) { } catch (e) {
report.push("slot registration ERROR: " + e.message) report.push("ui.slot(component) ERROR: " + e.message)
log("[oc-session-all] slot registration ERROR: " + e.message) log("[oc-session-all] ui.slot(component) ERROR: " + e.message)
} }
// ── 尝试直接使用 keymap.layer(可能需要等待) ────────────────────────────────────── // 等待组件渲染
await delay(3000)
// ── 验证命令是否注册成功 ──────────────────────────────────────
try { try {
// 等待更长时间,让 Provider 可用 if (context.keymap && typeof context.keymap.commands === "function") {
await delay(2000) const cmds = context.keymap.commands()
const cmdStr = JSON.stringify(cmds)
if (context.keymap && typeof context.keymap.layer === "function") { log("[oc-session-all] commands() after slot: " + cmdStr.substring(0, 800))
log("[oc-session-all] Attempting keymap.layer call...") if (cmdStr.includes("sessions-all")) {
try { report.push("sessions-all REGISTERED!")
// 尝试注册一个测试命令 log("[oc-session-all] === sessions-all command REGISTERED SUCCESSFULLY ===")
const layer = context.keymap.layer({ } else {
commands: [ report.push("sessions-all NOT in commands()")
{
id: "sessions-all",
title: "Show all sessions",
group: "Session",
}
],
bindings: [
{
key: "ctrl+o",
command: "sessions-all",
}
]
})
report.push("keymap.layer() SUCCESS")
log("[oc-session-all] keymap.layer() SUCCESS")
} catch (e) {
report.push("keymap.layer() ERROR: " + e.message)
log("[oc-session-all] keymap.layer() ERROR: " + e.message)
} }
} }
} catch (e) { } catch (e) {
report.push("layer attempt ERROR: " + e.message) log("[oc-session-all] commands() check ERROR: " + e.message)
log("[oc-session-all] layer attempt ERROR: " + e.message)
} }
// 最终报告 // 最终报告
const finalReport = report.join(" | ") const finalReport = report.join(" | ")
log("[oc-session-all] SLOT PROBE: " + finalReport) log("[oc-session-all] SLOT PROBE: " + finalReport)
// 延时后显示 toast // 延时后显示 toast
await delay(2000) await delay(2000)
try { try {
@@ -142,4 +133,4 @@ export default Plugin.define({
log("[oc-session-all] toast.show ERROR: " + e.message) log("[oc-session-all] toast.show ERROR: " + e.message)
} }
}, },
}) })