From ee0e9069c5c89b61fca3d478eb435167e148a455 Mon Sep 17 00:00:00 2001 From: Mateusz Tymek Date: Wed, 7 Jan 2026 21:10:58 +0000 Subject: [PATCH] Code cleanup Code cleanup --- main.js | 53 +++++++++---------------------------- src/OpenCodeView.ts | 15 ----------- src/SettingsTab.ts | 9 ------- src/main.ts | 64 +++++++++------------------------------------ 4 files changed, 24 insertions(+), 117 deletions(-) diff --git a/main.js b/main.js index 3128343..ca7e670 100644 --- a/main.js +++ b/main.js @@ -158,13 +158,6 @@ var OpenCodeView = class extends import_obsidian2.ItemView { reloadButton.addEventListener("click", () => { this.reloadIframe(); }); - const externalButton = actionsEl.createEl("button", { - attr: { "aria-label": "Open in browser" } - }); - (0, import_obsidian2.setIcon)(externalButton, "external-link"); - externalButton.addEventListener("click", () => { - window.open(this.plugin.getServerUrl(), "_blank"); - }); const stopButton = actionsEl.createEl("button", { attr: { "aria-label": "Stop server" } }); @@ -585,7 +578,6 @@ var OpenCodePlugin = class extends import_obsidian4.Plugin { constructor() { super(...arguments); this.settings = DEFAULT_SETTINGS; - this.processManager = null; this.stateChangeCallbacks = []; } async onload() { @@ -602,6 +594,7 @@ var OpenCodePlugin = class extends import_obsidian4.Plugin { ); console.log("[OpenCode] Configured with project directory:", projectDirectory); this.registerView(OPENCODE_VIEW_TYPE, (leaf) => new OpenCodeView(leaf, this)); + this.addSettingTab(new OpenCodeSettingTab(this.app, this)); this.addRibbonIcon(OPENCODE_ICON_NAME, "OpenCode", () => { this.activateView(); }); @@ -632,7 +625,6 @@ var OpenCodePlugin = class extends import_obsidian4.Plugin { this.stopServer(); } }); - this.addSettingTab(new OpenCodeSettingTab(this.app, this)); if (this.settings.autoStart) { this.app.workspace.onLayoutReady(async () => { await this.startServer(); @@ -641,30 +633,24 @@ var OpenCodePlugin = class extends import_obsidian4.Plugin { console.log("OpenCode plugin loaded"); } async onunload() { - console.log("Unloading OpenCode plugin"); this.stopServer(); this.app.workspace.detachLeavesOfType(OPENCODE_VIEW_TYPE); - console.log("OpenCode plugin unloaded"); } async loadSettings() { this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); } async saveSettings() { await this.saveData(this.settings); - if (this.processManager) { - this.processManager.updateSettings(this.settings); - } + this.processManager.updateSettings(this.settings); } // Update project directory and restart server if running async updateProjectDirectory(directory) { this.settings.projectDirectory = directory; await this.saveData(this.settings); - if (this.processManager) { - this.processManager.updateProjectDirectory(this.getProjectDirectory()); - if (this.getProcessState() === "running") { - this.stopServer(); - await this.startServer(); - } + this.processManager.updateProjectDirectory(this.getProjectDirectory()); + if (this.getProcessState() === "running") { + this.stopServer(); + await this.startServer(); } } // Get existing view leaf if any @@ -702,41 +688,29 @@ var OpenCodePlugin = class extends import_obsidian4.Plugin { await this.activateView(); } } - // Start the OpenCode server async startServer() { - if (!this.processManager) { - new import_obsidian4.Notice("OpenCode: Process manager not initialized"); - return false; - } const success = await this.processManager.start(); if (success) { new import_obsidian4.Notice("OpenCode server started"); } return success; } - // Stop the OpenCode server stopServer() { - if (this.processManager) { - this.processManager.stop(); - new import_obsidian4.Notice("OpenCode server stopped"); - } + this.processManager.stop(); + new import_obsidian4.Notice("OpenCode server stopped"); } - // Get the current process state getProcessState() { var _a, _b; return (_b = (_a = this.processManager) == null ? void 0 : _a.getState()) != null ? _b : "stopped"; } - // Get the last error message from the process manager getLastError() { - var _a, _b; - return (_b = (_a = this.processManager) == null ? void 0 : _a.getLastError()) != null ? _b : null; + var _a; + return (_a = this.processManager.getLastError()) != null ? _a : null; } - // Get the server URL getServerUrl() { - var _a, _b; - return (_b = (_a = this.processManager) == null ? void 0 : _a.getUrl()) != null ? _b : `http://127.0.0.1:${this.settings.port}`; + var _a; + return (_a = this.processManager.getUrl()) != null ? _a : `http://127.0.0.1:${this.settings.port}`; } - // Subscribe to process state changes, returns unsubscribe function onProcessStateChange(callback) { this.stateChangeCallbacks.push(callback); return () => { @@ -746,13 +720,11 @@ var OpenCodePlugin = class extends import_obsidian4.Plugin { } }; } - // Notify all subscribers of state change notifyStateChange(state) { for (const callback of this.stateChangeCallbacks) { callback(state); } } - // Get the vault path - this is the root directory of the Obsidian vault getVaultPath() { const adapter = this.app.vault.adapter; const vaultPath = adapter.basePath || ""; @@ -761,7 +733,6 @@ var OpenCodePlugin = class extends import_obsidian4.Plugin { } return vaultPath; } - // Get the project directory - uses the configured setting if set, otherwise vault path getProjectDirectory() { if (this.settings.projectDirectory) { return this.settings.projectDirectory; diff --git a/src/OpenCodeView.ts b/src/OpenCodeView.ts index b16c089..6100c02 100644 --- a/src/OpenCodeView.ts +++ b/src/OpenCodeView.ts @@ -123,7 +123,6 @@ export class OpenCodeView extends ItemView { private renderRunningState(): void { this.contentEl.empty(); - // Create header with controls const headerEl = this.contentEl.createDiv({ cls: "opencode-header" }); const titleSection = headerEl.createDiv({ cls: "opencode-header-title" }); @@ -133,7 +132,6 @@ export class OpenCodeView extends ItemView { const actionsEl = headerEl.createDiv({ cls: "opencode-header-actions" }); - // Reload button const reloadButton = actionsEl.createEl("button", { attr: { "aria-label": "Reload" }, }); @@ -142,16 +140,6 @@ export class OpenCodeView extends ItemView { this.reloadIframe(); }); - // Open in browser button - const externalButton = actionsEl.createEl("button", { - attr: { "aria-label": "Open in browser" }, - }); - setIcon(externalButton, "external-link"); - externalButton.addEventListener("click", () => { - window.open(this.plugin.getServerUrl(), "_blank"); - }); - - // Stop button const stopButton = actionsEl.createEl("button", { attr: { "aria-label": "Stop server" }, }); @@ -160,7 +148,6 @@ export class OpenCodeView extends ItemView { this.plugin.stopServer(); }); - // Create iframe container const iframeContainer = this.contentEl.createDiv({ cls: "opencode-iframe-container", }); @@ -174,7 +161,6 @@ export class OpenCodeView extends ItemView { }, }); - // Handle iframe load errors this.iframeEl.addEventListener("error", () => { console.error("Failed to load OpenCode iframe"); }); @@ -192,7 +178,6 @@ export class OpenCodeView extends ItemView { statusContainer.createEl("h3", { text: "Failed to start OpenCode" }); - // Display specific error message if available const errorMessage = this.plugin.getLastError(); if (errorMessage) { statusContainer.createEl("p", { diff --git a/src/SettingsTab.ts b/src/SettingsTab.ts index f0874ef..89b9aa6 100644 --- a/src/SettingsTab.ts +++ b/src/SettingsTab.ts @@ -25,10 +25,7 @@ export class OpenCodeSettingTab extends PluginSettingTab { display(): void { const { containerEl } = this; containerEl.empty(); - containerEl.createEl("h2", { text: "OpenCode Settings" }); - - // Server settings section containerEl.createEl("h3", { text: "Server Configuration" }); new Setting(containerEl) @@ -95,7 +92,6 @@ export class OpenCodeSettingTab extends PluginSettingTab { }) ); - // Behavior settings section containerEl.createEl("h3", { text: "Behavior" }); new Setting(containerEl) @@ -112,7 +108,6 @@ export class OpenCodeSettingTab extends PluginSettingTab { }) ); - // Server status section containerEl.createEl("h3", { text: "Server Status" }); const statusContainer = containerEl.createDiv({ cls: "opencode-settings-status" }); @@ -134,10 +129,8 @@ export class OpenCodeSettingTab extends PluginSettingTab { return; } - // Expand tilde for validation const expanded = expandTilde(trimmed); - // Validate path exists and is a directory try { if (!existsSync(expanded)) { new Notice("Project directory does not exist"); @@ -153,7 +146,6 @@ export class OpenCodeSettingTab extends PluginSettingTab { return; } - // Store the expanded path await this.plugin.updateProjectDirectory(expanded); } @@ -195,7 +187,6 @@ export class OpenCodeSettingTab extends PluginSettingTab { }); } - // Control buttons const buttonContainer = container.createDiv({ cls: "opencode-settings-buttons" }); if (state === "stopped" || state === "error") { diff --git a/src/main.ts b/src/main.ts index 1fa4a4f..d5c7f1e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -7,23 +7,19 @@ import { registerOpenCodeIcons, OPENCODE_ICON_NAME } from "./icons"; export default class OpenCodePlugin extends Plugin { settings: OpenCodeSettings = DEFAULT_SETTINGS; - private processManager: ProcessManager | null = null; + private processManager: ProcessManager; private stateChangeCallbacks: Array<(state: ProcessState) => void> = []; async onload(): Promise { console.log("Loading OpenCode plugin"); - // Register custom icons registerOpenCodeIcons(); await this.loadSettings(); - // Get the vault directory path to pass to OpenCode const vaultPath = this.getVaultPath(); const projectDirectory = this.getProjectDirectory(); - // Initialize process manager with vault as the working directory - // and either the configured project directory or vault as the project this.processManager = new ProcessManager( this.settings, vaultPath, @@ -33,15 +29,13 @@ export default class OpenCodePlugin extends Plugin { console.log("[OpenCode] Configured with project directory:", projectDirectory); - // Register the OpenCode view this.registerView(OPENCODE_VIEW_TYPE, (leaf) => new OpenCodeView(leaf, this)); + this.addSettingTab(new OpenCodeSettingTab(this.app, this)); - // Add ribbon icon this.addRibbonIcon(OPENCODE_ICON_NAME, "OpenCode", () => { this.activateView(); }); - // Add command to toggle view this.addCommand({ id: "toggle-opencode-view", name: "Toggle OpenCode panel", @@ -56,7 +50,6 @@ export default class OpenCodePlugin extends Plugin { ], }); - // Add command to start server this.addCommand({ id: "start-opencode-server", name: "Start OpenCode server", @@ -65,7 +58,6 @@ export default class OpenCodePlugin extends Plugin { }, }); - // Add command to stop server this.addCommand({ id: "stop-opencode-server", name: "Stop OpenCode server", @@ -74,10 +66,6 @@ export default class OpenCodePlugin extends Plugin { }, }); - // Register settings tab - this.addSettingTab(new OpenCodeSettingTab(this.app, this)); - - // Auto-start if enabled if (this.settings.autoStart) { this.app.workspace.onLayoutReady(async () => { await this.startServer(); @@ -88,15 +76,8 @@ export default class OpenCodePlugin extends Plugin { } async onunload(): Promise { - console.log("Unloading OpenCode plugin"); - - // Stop the server this.stopServer(); - - // Detach all views this.app.workspace.detachLeavesOfType(OPENCODE_VIEW_TYPE); - - console.log("OpenCode plugin unloaded"); } async loadSettings(): Promise { @@ -105,10 +86,7 @@ export default class OpenCodePlugin extends Plugin { async saveSettings(): Promise { await this.saveData(this.settings); - // Update process manager with new settings - if (this.processManager) { - this.processManager.updateSettings(this.settings); - } + this.processManager.updateSettings(this.settings); } // Update project directory and restart server if running @@ -116,14 +94,12 @@ export default class OpenCodePlugin extends Plugin { this.settings.projectDirectory = directory; await this.saveData(this.settings); - if (this.processManager) { - this.processManager.updateProjectDirectory(this.getProjectDirectory()); + this.processManager.updateProjectDirectory(this.getProjectDirectory()); - // Restart server if it's currently running - if (this.getProcessState() === "running") { - this.stopServer(); - await this.startServer(); - } + // Restart server if it's currently running + if (this.getProcessState() === "running") { + this.stopServer(); + await this.startServer(); } } @@ -170,13 +146,7 @@ export default class OpenCodePlugin extends Plugin { } } - // Start the OpenCode server async startServer(): Promise { - if (!this.processManager) { - new Notice("OpenCode: Process manager not initialized"); - return false; - } - const success = await this.processManager.start(); if (success) { new Notice("OpenCode server started"); @@ -184,30 +154,23 @@ export default class OpenCodePlugin extends Plugin { return success; } - // Stop the OpenCode server stopServer(): void { - if (this.processManager) { - this.processManager.stop(); - new Notice("OpenCode server stopped"); - } + this.processManager.stop(); + new Notice("OpenCode server stopped"); } - // Get the current process state getProcessState(): ProcessState { return this.processManager?.getState() ?? "stopped"; } - // Get the last error message from the process manager getLastError(): string | null { - return this.processManager?.getLastError() ?? null; + return this.processManager.getLastError() ?? null; } - // Get the server URL getServerUrl(): string { - return this.processManager?.getUrl() ?? `http://127.0.0.1:${this.settings.port}`; + return this.processManager.getUrl() ?? `http://127.0.0.1:${this.settings.port}`; } - // Subscribe to process state changes, returns unsubscribe function onProcessStateChange(callback: (state: ProcessState) => void): () => void { this.stateChangeCallbacks.push(callback); return () => { @@ -218,14 +181,12 @@ export default class OpenCodePlugin extends Plugin { }; } - // Notify all subscribers of state change private notifyStateChange(state: ProcessState): void { for (const callback of this.stateChangeCallbacks) { callback(state); } } - // Get the vault path - this is the root directory of the Obsidian vault private getVaultPath(): string { const adapter = this.app.vault.adapter as any; const vaultPath = adapter.basePath || ""; @@ -235,7 +196,6 @@ export default class OpenCodePlugin extends Plugin { return vaultPath; } - // Get the project directory - uses the configured setting if set, otherwise vault path getProjectDirectory(): string { if (this.settings.projectDirectory) { return this.settings.projectDirectory;