v3: 尝试通过ui.slot注册组件来获取Keymap Provider上下文,探索在SolidJS渲染阶段注册命令的可行性

This commit is contained in:
ypshidifu
2026-09-22 21:50:57 +08:00
parent f66cde6494
commit 6d59c24126
+90 -43
View File
@@ -30,64 +30,111 @@ function delay(ms) {
export default Plugin.define({
id: "oc-session-all",
async setup(context) {
log("[oc-session-all] v2 Plugin loaded! Exploring keymap.commands and dispatch...")
log("[oc-session-all] v3 Plugin loaded! Trying to register command via ui.slot...")
// 先写入测试日志,确保 fs 可用
log("[oc-session-all] FS test: log file created successfully")
// 延时 5 秒,避免 toast 被覆盖
await delay(5000)
// 延时 3 秒,避免 toast 被覆盖
await delay(3000)
// ── 探测 keymap.commands 和 dispatch ──────────────────────────────────────
// ── 探测 context.ui.slot ──────────────────────────────────────
const report = []
if (context.keymap) {
// 尝试 keymap.commands()
if (typeof context.keymap.commands === "function") {
try {
const commandsResult = context.keymap.commands()
report.push("keymap.commands() returned: " + JSON.stringify(commandsResult).substring(0, 200))
log("[oc-session-all] keymap.commands() result: " + JSON.stringify(commandsResult).substring(0, 500))
} catch (e) {
report.push("keymap.commands() ERROR: " + e.message)
log("[oc-session-all] keymap.commands() ERROR: " + e.message)
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")
}
} else {
report.push("keymap.commands is NOT a function")
}
// 尝试 keymap.dispatch
if (typeof context.keymap.dispatch === "function") {
report.push("keymap.dispatch is a function")
// 尝试触发一个已知的命令
try {
context.keymap.dispatch("command.palette.show")
report.push("dispatch command.palette.show: SUCCESS")
log("[oc-session-all] dispatch command.palette.show: SUCCESS")
} catch (e) {
report.push("dispatch command.palette.show ERROR: " + e.message)
log("[oc-session-all] dispatch command.palette.show ERROR: " + e.message)
}
} else {
report.push("keymap.dispatch is NOT a function")
}
// 尝试 keymap.layer 的不同调用方式
if (typeof context.keymap.layer === "function") {
// 不调用 layer,只记录它存在
report.push("keymap.layer exists but needs Provider context")
}
} else {
report.push("ui.slot NOT found")
}
const msg = report.join(" | ")
log("[oc-session-all] COMMANDS PROBE: " + msg)
// ── 尝试通过 ui.slot 注册组件 ──────────────────────────────────────
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(可能需要等待) ──────────────────────────────────────
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({
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) {
report.push("layer attempt ERROR: " + e.message)
log("[oc-session-all] layer attempt ERROR: " + e.message)
}
// 最终报告
const finalReport = report.join(" | ")
log("[oc-session-all] SLOT PROBE: " + finalReport)
// 延时后显示 toast
await delay(2000)
try {
context.ui.toast.show({
message: "Commands probe: " + msg,
message: "Slot probe: " + finalReport,
variant: "info",
duration: 25000,
})