type EditorUI = { getEditorComponent(): unknown; setEditorComponent(factory: unknown): void; }; export type EditorInstallResult = "installed" | "occupied" | "incompatible" | "failed"; function asEditorUI(value: unknown): EditorUI | undefined { if (typeof value !== "object" || value === null) return undefined; const ui = value as Partial; return typeof ui.getEditorComponent === "function" && typeof ui.setEditorComponent === "function" ? ui as EditorUI : undefined; } /** Installs a replacement editor without displacing an existing one. */ export function installEditorComponent(ui: unknown, factory: unknown): EditorInstallResult { const editorUI = asEditorUI(ui); if (!editorUI || typeof factory !== "function") return "incompatible"; try { if (editorUI.getEditorComponent()) return "occupied"; } catch { return "incompatible"; } try { editorUI.setEditorComponent(factory); return "installed"; } catch { clearOwnedEditorComponent(editorUI, factory); return "failed"; } } /** Restores the stock editor only when this factory is still installed. */ export function clearOwnedEditorComponent(ui: unknown, factory: unknown): boolean { const editorUI = asEditorUI(ui); if (!editorUI || typeof factory !== "function") return false; try { if (editorUI.getEditorComponent() !== factory) return false; } catch { return false; } try { editorUI.setEditorComponent(undefined); return true; } catch { return false; } }