mirror of
https://github.com/vrtmrz/obsidian-livesync.git
synced 2026-03-28 12:45:17 +00:00
Now, Self-hosted LiveSync has finally begun to be split into the Self-hosted LiveSync plugin for Obsidian, and a properly abstracted version of it. This may not offer much benefit to Obsidian plugin users, or might even cause a slight inconvenience, but I believe it will certainly help improve testability and make the ecosystem better. However, I do not see the point in putting something with little benefit into beta, so I am handling this on the alpha branch. I would actually preferred to create an R&D branch, but I was not keen on the ampersand, and I feel it will eventually become a proper beta anyway. ### Refactored - Separated `ObsidianLiveSyncPlugin` into `ObsidianLiveSyncPlugin` and `LiveSyncBaseCore`. - Now `LiveSyncCore` indicates the type specified version of `LiveSyncBaseCore`. - Referencing `plugin.xxx` has been rewritten to referencing the corresponding service or `core.xxx`. ### Internal API changes - Storage Access APIs are now yielding Promises. This is to allow more limited storage platforms to be supported. ### R&D - Browser-version of Self-hosted LiveSync is now in development. This is not intended for public use now, but I will eventually make it available for testing. - We can see the code in `src/apps/webapp` for the browser version.
57 lines
2.4 KiB
TypeScript
57 lines
2.4 KiB
TypeScript
import { type TFile } from "@/deps.ts";
|
|
import { eventHub } from "../../common/events.ts";
|
|
import { EVENT_REQUEST_SHOW_HISTORY } from "../../common/obsidianEvents.ts";
|
|
import type { FilePathWithPrefix, LoadedEntry, DocumentID } from "../../lib/src/common/types.ts";
|
|
import { AbstractObsidianModule } from "../AbstractObsidianModule.ts";
|
|
import { DocumentHistoryModal } from "./DocumentHistory/DocumentHistoryModal.ts";
|
|
import { fireAndForget } from "octagonal-wheels/promises";
|
|
|
|
export class ModuleObsidianDocumentHistory extends AbstractObsidianModule {
|
|
_everyOnloadStart(): Promise<boolean> {
|
|
this.addCommand({
|
|
id: "livesync-history",
|
|
name: "Show history",
|
|
callback: () => {
|
|
const file = this.services.vault.getActiveFilePath();
|
|
if (file) this.showHistory(file, undefined);
|
|
},
|
|
});
|
|
|
|
this.addCommand({
|
|
id: "livesync-filehistory",
|
|
name: "Pick a file to show history",
|
|
callback: () => {
|
|
fireAndForget(async () => await this.fileHistory());
|
|
},
|
|
});
|
|
eventHub.onEvent(
|
|
EVENT_REQUEST_SHOW_HISTORY,
|
|
({ file, fileOnDB }: { file: TFile | FilePathWithPrefix; fileOnDB: LoadedEntry }) => {
|
|
this.showHistory(file, fileOnDB._id);
|
|
}
|
|
);
|
|
return Promise.resolve(true);
|
|
}
|
|
|
|
showHistory(file: TFile | FilePathWithPrefix, id?: DocumentID) {
|
|
new DocumentHistoryModal(this.app, this.core, this.plugin, file, id).open();
|
|
}
|
|
|
|
async fileHistory() {
|
|
const notes: { id: DocumentID; path: FilePathWithPrefix; dispPath: string; mtime: number }[] = [];
|
|
for await (const doc of this.localDatabase.findAllDocs()) {
|
|
notes.push({ id: doc._id, path: this.getPath(doc), dispPath: this.getPath(doc), mtime: doc.mtime });
|
|
}
|
|
notes.sort((a, b) => b.mtime - a.mtime);
|
|
const notesList = notes.map((e) => e.dispPath);
|
|
const target = await this.core.confirm.askSelectString("File to view History", notesList);
|
|
if (target) {
|
|
const targetId = notes.find((e) => e.dispPath == target)!;
|
|
this.showHistory(targetId.path, targetId.id);
|
|
}
|
|
}
|
|
override onBindFunction(core: typeof this.core, services: typeof core.services): void {
|
|
services.appLifecycle.onInitialise.addHandler(this._everyOnloadStart.bind(this));
|
|
}
|
|
}
|