import { fireAndForget } from "octagonal-wheels/promises"; import { addIcon, type Editor, type MarkdownFileInfo, type MarkdownView } from "../../deps.ts"; import { LOG_LEVEL_NOTICE, type FilePathWithPrefix } from "../../lib/src/common/types.ts"; import { AbstractObsidianModule } from "../AbstractObsidianModule.ts"; import { $msg } from "src/lib/src/common/i18n.ts"; import type { LiveSyncCore } from "../../main.ts"; export class ModuleObsidianMenu extends AbstractObsidianModule { _everyOnloadStart(): Promise { // UI addIcon( "replicate", ` ` ); this.addRibbonIcon("replicate", $msg("moduleObsidianMenu.replicate"), async () => { await this.services.replication.replicate(true); }).addClass("livesync-ribbon-replicate"); this.addCommand({ id: "livesync-replicate", name: "Replicate now", callback: async () => { await this.services.replication.replicate(); }, }); this.addCommand({ id: "livesync-dump", name: "Dump information of this doc ", callback: () => { const file = this.services.vault.getActiveFilePath(); if (!file) return; fireAndForget(() => this.localDatabase.getDBEntry(file, {}, true, false)); }, }); this.addCommand({ id: "livesync-checkdoc-conflicted", name: "Resolve if conflicted.", editorCallback: (editor: Editor, view: MarkdownView | MarkdownFileInfo) => { const file = view.file; if (!file) return; void this.services.conflict.queueCheckForIfOpen(file.path as FilePathWithPrefix); }, }); this.addCommand({ id: "livesync-toggle", name: "Toggle LiveSync", callback: async () => { if (this.settings.liveSync) { this.settings.liveSync = false; this._log("LiveSync Disabled.", LOG_LEVEL_NOTICE); } else { this.settings.liveSync = true; this._log("LiveSync Enabled.", LOG_LEVEL_NOTICE); } await this.services.setting.onRealiseSetting(); await this.services.setting.saveSettingData(); }, }); this.addCommand({ id: "livesync-suspendall", name: "Toggle All Sync.", callback: async () => { if (this.services.appLifecycle.isSuspended()) { this.services.appLifecycle.setSuspended(false); this._log("Self-hosted LiveSync resumed", LOG_LEVEL_NOTICE); } else { this.services.appLifecycle.setSuspended(true); this._log("Self-hosted LiveSync suspended", LOG_LEVEL_NOTICE); } await this.services.setting.onRealiseSetting(); await this.services.setting.saveSettingData(); }, }); this.addCommand({ id: "livesync-scan-files", name: "Scan storage and database again", callback: async () => { await this.services.vault.scanVault(true); }, }); this.addCommand({ id: "livesync-runbatch", name: "Run pended batch processes", callback: async () => { await this.services.fileProcessing.commitPendingFileEvents(); }, }); // TODO, Replicator is possibly one of features. It should be moved to features. this.addCommand({ id: "livesync-abortsync", name: "Abort synchronization immediately", callback: () => { this.core.replicator.terminateSync(); }, }); return Promise.resolve(true); } _onWorkspaceReady() { void this.services.appLifecycle.onReady(); } private _everyOnload(): Promise { this.app.workspace.onLayoutReady(this._onWorkspaceReady.bind(this)); return Promise.resolve(true); } private async _showView(viewType: string) { const leaves = this.app.workspace.getLeavesOfType(viewType); if (leaves.length == 0) { await this.app.workspace.getLeaf(true).setViewState({ type: viewType, active: true, }); } else { await leaves[0].setViewState({ type: viewType, active: true, }); } if (leaves.length > 0) { await this.app.workspace.revealLeaf(leaves[0]); } } onBindFunction(core: LiveSyncCore, services: typeof core.services): void { services.appLifecycle.handleOnInitialise(this._everyOnloadStart.bind(this)); services.appLifecycle.handleOnLoaded(this._everyOnload.bind(this)); services.API.handleShowWindow(this._showView.bind(this)); } }