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, type IObsidianModule } from "../AbstractObsidianModule.ts"; import { $msg } from "src/lib/src/common/i18n.ts"; export class ModuleObsidianMenu extends AbstractObsidianModule implements IObsidianModule { $everyOnloadStart(): Promise { // UI addIcon( "replicate", ` ` ); this.addRibbonIcon("replicate", $msg("moduleObsidianMenu.replicate"), async () => { await this.core.$$replicate(true); }).addClass("livesync-ribbon-replicate"); this.addCommand({ id: "livesync-replicate", name: "Replicate now", callback: async () => { await this.core.$$replicate(); }, }); this.addCommand({ id: "livesync-dump", name: "Dump information of this doc ", callback: () => { const file = this.core.$$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.core.$$queueConflictCheckIfOpen(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.core.$$realizeSettingSyncMode(); await this.core.$$saveSettingData(); }, }); this.addCommand({ id: "livesync-suspendall", name: "Toggle All Sync.", callback: async () => { if (this.core.$$isSuspended()) { this.core.$$setSuspended(false); this._log("Self-hosted LiveSync resumed", LOG_LEVEL_NOTICE); } else { this.core.$$setSuspended(true); this._log("Self-hosted LiveSync suspended", LOG_LEVEL_NOTICE); } await this.core.$$realizeSettingSyncMode(); await this.core.$$saveSettingData(); }, }); this.addCommand({ id: "livesync-scan-files", name: "Scan storage and database again", callback: async () => { await this.core.$$performFullScan(true); }, }); this.addCommand({ id: "livesync-runbatch", name: "Run pended batch processes", callback: async () => { await this.core.$everyCommitPendingFileEvent(); }, }); // 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); } $everyOnload(): Promise { this.app.workspace.onLayoutReady(this.core.$$onLiveSyncReady.bind(this.core)); // eslint-disable-next-line no-unused-labels return Promise.resolve(true); } 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) { this.app.workspace.revealLeaf(leaves[0]); } } }