fix: harden panel entry bootstrap

This commit is contained in:
Youzini-afk
2026-04-14 16:59:26 +08:00
parent 663bf2ad50
commit 6c808ed5ad
2 changed files with 82 additions and 34 deletions

View File

@@ -27,44 +27,86 @@ export function openPanelController(runtime) {
} }
function injectOptionsMenuEntry(runtime) { function injectOptionsMenuEntry(runtime) {
if (runtime.document.getElementById("option_st_bme_panel")) { const doc = runtime.document;
if (!doc || doc.getElementById("option_st_bme_panel")) {
return true; return true;
} }
const menuItem = doc.createElement("a");
const $menuItem = runtime.$(` menuItem.id = "option_st_bme_panel";
<a id="option_st_bme_panel"> menuItem.innerHTML =
<i class="fa-lg fa-solid fa-brain"></i> '<i class="fa-lg fa-solid fa-brain"></i><span>记忆图谱</span>';
<span>记忆图谱</span> menuItem.addEventListener("click", async () => {
</a>
`).on("click", async () => {
try { try {
await ensurePanelBridgeReady(runtime); await ensurePanelBridgeReady(runtime);
openPanelController(runtime); openPanelController(runtime);
runtime.$("#options").hide(); runtime.$?.("#options")?.hide?.();
} catch (error) { } catch (error) {
runtime.console.error("[ST-BME] 点击菜单打开面板失败:", error); runtime.console.error("[ST-BME] 点击菜单打开面板失败:", error);
globalThis.toastr?.error?.("记忆图谱面板加载失败,请查看控制台报错", "ST-BME"); globalThis.toastr?.error?.("记忆图谱面板加载失败,请查看控制台报错", "ST-BME");
} }
}); });
const $optionsContent = runtime.$("#options .options-content"); const anchor = doc.getElementById("option_toggle_logprobs");
const $anchor = runtime.$("#option_toggle_logprobs"); const optionsContent = doc.querySelector("#options .options-content");
if ($anchor.length > 0) { if (anchor?.parentNode) {
$anchor.after($menuItem); anchor.parentNode.insertBefore(menuItem, anchor.nextSibling);
return true; return true;
} else if ($optionsContent.length > 0) { }
$optionsContent.append($menuItem); if (optionsContent) {
optionsContent.appendChild(menuItem);
return true; return true;
} }
return false; return false;
} }
function injectFloatingBootstrap(runtime) {
const doc = runtime.document;
if (!doc) return false;
let fab = doc.getElementById("bme-floating-ball");
if (!fab) {
fab = doc.createElement("div");
fab.id = "bme-floating-ball";
fab.setAttribute("data-status", "idle");
fab.setAttribute("data-bme-bootstrap", "true");
fab.innerHTML = `
<i class="fa-solid fa-brain bme-fab-icon"></i>
<span class="bme-fab-tooltip">BME 记忆图谱</span>
`;
const mountTarget = doc.body || doc.documentElement;
if (!mountTarget) return false;
mountTarget.appendChild(fab);
}
if (fab.dataset.bmeBridgeBound === "true") {
return true;
}
fab.dataset.bmeBridgeBound = "true";
fab.addEventListener("click", async () => {
try {
await ensurePanelBridgeReady(runtime);
openPanelController(runtime);
} catch (error) {
runtime.console.error("[ST-BME] 点击悬浮球打开面板失败:", error);
globalThis.toastr?.error?.("记忆图谱面板加载失败,请查看控制台报错", "ST-BME");
}
});
return true;
}
function scheduleOptionsMenuInjection(runtime, attempt = 0) { function scheduleOptionsMenuInjection(runtime, attempt = 0) {
try {
injectFloatingBootstrap(runtime);
} catch (error) {
runtime.console.warn("[ST-BME] 悬浮球入口预注入失败:", error);
}
try {
if (injectOptionsMenuEntry(runtime)) { if (injectOptionsMenuEntry(runtime)) {
return; return;
} }
} catch (error) {
runtime.console.warn("[ST-BME] 菜单入口注入失败,稍后重试:", error);
}
if (attempt >= MENU_ENTRY_MAX_ATTEMPTS) { if (attempt >= MENU_ENTRY_MAX_ATTEMPTS) {
runtime.console.warn( runtime.console.warn(

View File

@@ -1021,21 +1021,21 @@ function _bindFabToggle() {
} }
function _initFloatingBall() { function _initFloatingBall() {
const existing = document.getElementById("bme-floating-ball"); let fab = document.getElementById("bme-floating-ball");
if (existing) { if (!fab) {
_fabEl = existing; fab = document.createElement("div");
ensureFabMountedAtRoot();
syncFabPosition();
return;
}
const fab = document.createElement("div");
fab.id = "bme-floating-ball"; fab.id = "bme-floating-ball";
fab.setAttribute("data-status", "idle"); fab.setAttribute("data-status", "idle");
fab.innerHTML = ` fab.innerHTML = `
<i class="fa-solid fa-brain bme-fab-icon"></i> <i class="fa-solid fa-brain bme-fab-icon"></i>
<span class="bme-fab-tooltip">BME 记忆图谱</span> <span class="bme-fab-tooltip">BME 记忆图谱</span>
`; `;
} else if (!fab.querySelector(".bme-fab-icon")) {
fab.innerHTML = `
<i class="fa-solid fa-brain bme-fab-icon"></i>
<span class="bme-fab-tooltip">BME 记忆图谱</span>
`;
}
_fabEl = fab; _fabEl = fab;
ensureFabMountedAtRoot(); ensureFabMountedAtRoot();
@@ -1047,11 +1047,17 @@ function _initFloatingBall() {
if (saved) { if (saved) {
fab.dataset.positionMode = "saved"; fab.dataset.positionMode = "saved";
applyFabPosition(saved, fab); applyFabPosition(saved, fab);
} else { } else if (!fab.style.left || !fab.style.top) {
fab.dataset.positionMode = "default"; fab.dataset.positionMode = "default";
syncFabPosition(); syncFabPosition();
} }
if (fab.dataset.bmeFabBound === "true") {
return;
}
fab.dataset.bmeFabBound = "true";
delete fab.dataset.bmeBootstrap;
// 拖拽 + 点击逻辑 // 拖拽 + 点击逻辑
let isDragging = false; let isDragging = false;
let hasMoved = false; let hasMoved = false;