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

This commit is contained in:
ypshidifu
2026-09-22 22:04:02 +08:00
parent 6d59c24126
commit 92fee7511f
+60 -69
View File
@@ -30,100 +30,91 @@ function delay(ms) {
export default Plugin.define({
id: "oc-session-all",
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")
// 延时 5 秒,避免 toast 被覆盖(别人插件也会弹)
await delay(5000)
// 延时 3 秒,避免 toast 被覆盖
await delay(3000)
// ── 探测 context.ui.slot ──────────────────────────────────────
const report = []
if (context.ui && context.ui.slot) {
report.push("ui.slot exists")
log("[oc-session-all] ui.slot type: " + typeof context.ui.slot)
// 尝试列出 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")
}
}
// ── 探测 ui.slot 函数本身的元信息 ──────────────────────────────────────
if (context.ui && typeof context.ui.slot === "function") {
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)
} 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 {
// 创建一个简单的组件,尝试在渲染时注册命令
const testComponent = () => {
log("[oc-session-all] Test component rendered!")
return null
}
// 尝试不同的注册方法
if (context.ui.slot && typeof context.ui.slot.register === "function") {
context.ui.slot.register(testComponent)
report.push("registered via slot.register()")
log("[oc-session-all] Registered component via slot.register()")
} 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) {
report.push("slot registration ERROR: " + e.message)
log("[oc-session-all] slot registration ERROR: " + e.message)
}
// ── 尝试直接使用 keymap.layer(可能需要等待) ──────────────────────────────────────
// 组件在 SolidJS 渲染上下文内执行,此时 Keymap.Provider 应该可用
const ProbeComponent = () => {
log("[oc-session-all] ProbeComponent RENDERED inside slot!")
// 在组件渲染阶段尝试 keymap.layer(此时应在 Provider 内)
try {
// 等待更长时间,让 Provider 可用
await delay(2000)
if (context.keymap && typeof context.keymap.layer === "function") {
log("[oc-session-all] Attempting keymap.layer call...")
try {
// 尝试注册一个测试命令
const layer = context.keymap.layer({
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",
}
]
},
],
})
report.push("keymap.layer() SUCCESS")
log("[oc-session-all] keymap.layer() SUCCESS")
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) {
report.push("keymap.layer() ERROR: " + e.message)
log("[oc-session-all] keymap.layer() ERROR: " + e.message)
log("[oc-session-all] keymap.layer() inside component ERROR: " + e.message)
report.push("layer-in-component ERROR: " + e.message)
}
// 组件不渲染任何可见内容
return null
}
// 直接调用 slot 函数注册组件
const ret = context.ui.slot(ProbeComponent)
slotReturn = typeof ret
report.push("ui.slot(component) returned type=" + typeof ret)
log("[oc-session-all] ui.slot(component) called, returned type=" + typeof ret + ", value=" + String(ret))
} catch (e) {
report.push("ui.slot(component) ERROR: " + e.message)
log("[oc-session-all] ui.slot(component) ERROR: " + e.message)
}
// 等待组件渲染
await delay(3000)
// ── 验证命令是否注册成功 ──────────────────────────────────────
try {
if (context.keymap && typeof context.keymap.commands === "function") {
const cmds = context.keymap.commands()
const cmdStr = JSON.stringify(cmds)
log("[oc-session-all] commands() after slot: " + cmdStr.substring(0, 800))
if (cmdStr.includes("sessions-all")) {
report.push("sessions-all REGISTERED!")
log("[oc-session-all] === sessions-all command REGISTERED SUCCESSFULLY ===")
} else {
report.push("sessions-all NOT in commands()")
}
}
} catch (e) {
report.push("layer attempt ERROR: " + e.message)
log("[oc-session-all] layer attempt ERROR: " + e.message)
log("[oc-session-all] commands() check ERROR: " + e.message)
}
// 最终报告