Skip to content

Commit

Permalink
feat: file recovery diff modal
Browse files Browse the repository at this point in the history
  • Loading branch information
kometenstaub committed Mar 13, 2022
1 parent 21d19c0 commit c7a2b74
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/diff_view.ts
Expand Up @@ -96,7 +96,7 @@ export default class DiffView extends Modal {
];
}

public appendSyncVersions() {
private appendSyncVersions() {
// add the inner HTML element (the sync list) and keep a record
// of references to the elements
this.leftVList.push(
Expand Down Expand Up @@ -252,7 +252,7 @@ export default class DiffView extends Modal {
if (left) {
const clickedEl = await this.generateVersionListener(
div,
this.leftVList as vList[],
this.leftVList,
this.leftActive,
left
);
Expand All @@ -261,7 +261,7 @@ export default class DiffView extends Modal {
} else {
const clickedEl = await this.generateVersionListener(
div,
this.rightVList as vList[],
this.rightVList,
this.rightActive
);
await this.getSyncContent(clickedEl);
Expand Down
27 changes: 27 additions & 0 deletions src/interfaces.ts
Expand Up @@ -12,6 +12,9 @@ declare module 'obsidian' {
sync: {
instance: syncInstance;
};
'file-recovery': {
instance: fileRInstance;
};
};
};
}
Expand Down Expand Up @@ -46,3 +49,27 @@ export interface vList {
html: HTMLElement;
v: item;
}

export interface rVList {
html: HTMLElement;
data: string;
}

export interface fileRInstance {
db: {
transaction(
type: 'backups',
access: 'readonly'
): {
store: {
index(key: 'path'): {
getAll(): Promise<
recResult[]
>;
};
};
};
};
}

export interface recResult { path: string; ts: number; data: string }
23 changes: 19 additions & 4 deletions src/main.ts
Expand Up @@ -3,6 +3,7 @@ import type { OpenSyncHistorySettings } from './interfaces';
import OpenSyncHistorySettingTab from './settings';
import DiffUtils from './diff_utils';
import DiffView from './diff_view';
import RecoveryDiffView from './recovery_diff_view';

const DEFAULT_SETTINGS: OpenSyncHistorySettings = {
//context: '3',
Expand All @@ -16,6 +17,10 @@ export default class OpenSyncHistoryPlugin extends Plugin {
settings: OpenSyncHistorySettings;
diff_utils = new DiffUtils(this, this.app);

openRecoveryDiffModal(file: TFile): void {
new RecoveryDiffView(this, this.app, file).open();
}

openDiffModal(file: TFile): void {
new DiffView(this, this.app, file).open();
}
Expand Down Expand Up @@ -52,25 +57,35 @@ export default class OpenSyncHistoryPlugin extends Plugin {
returnDiffCommand(): Command {
return {
id: 'open-diff-view',
name: 'Show diff view',
name: 'Show Sync diff view',
checkCallback: this.giveCallback(this.openDiffModal.bind(this)),
};
}

returnRecoveryDiffCommand(): Command {
return {
id: 'open-recovery-diff-view',
name: 'Show Recovery diff view',
checkCallback: this.giveCallback(
this.openRecoveryDiffModal.bind(this)
),
};
}

async onload() {
console.log('loading Sync Version History plugin');
console.log('loading Version History Diff plugin');

this.addCommand(this.returnOpenCommand());

this.addCommand(this.returnDiffCommand());
this.addCommand(this.returnRecoveryDiffCommand());

await this.loadSettings();

this.addSettingTab(new OpenSyncHistorySettingTab(this.app, this));
}

onunload() {
console.log('unloading Sync Version History plugin');
console.log('unloading Version History Diff plugin');
}

async loadSettings() {
Expand Down
98 changes: 95 additions & 3 deletions src/recovery_diff_view.ts
@@ -1,9 +1,101 @@
import DiffView from "./diff_view";
import type {Plugin, App, TFile} from 'obsidian';
import type OpenSyncHistoryPlugin from "./main";
import DiffView from './diff_view';
import {Plugin, App, TFile, Notice } from 'obsidian';
import type OpenSyncHistoryPlugin from './main';
import type {recResult, rVList } from './interfaces';

export default class RecoveryDiffView extends DiffView {
//@ts-expect-error, this class uses them differently
versions: recResult[];
//@ts-expect-error, tthis class uses them differently
leftVList: rVList[];
//@ts-expect-error, tthis class uses them differently
rightVList: rVList[];
constructor(plugin: OpenSyncHistoryPlugin, app: App, file: TFile) {
super(plugin, app, file);
this.versions = [];
this.leftVList = [];
this.rightVList = [];
}

async onOpen() {
await this.getVersions()
const diff = this.getDiff()
this.makeHistoryLists()
this.basicHtml(diff);
this.appendRecVersions()
this.makeMoreGeneralHtml()
}

async getVersions() {
const fileRecovery = await this.app.internalPlugins.plugins[
'file-recovery'
].instance.db
.transaction('backups', 'readonly')
.store.index('path')
.getAll();
const fileContent = await this.app.vault.read(this.file)
this.versions.push({path: this.file.path, ts: new Date().getMilliseconds(), data: fileContent})
for (const version of fileRecovery) {
if (version.path === this.file.path) {
this.versions.push(version)
}
}
if (!(this.versions.length > 1)){
this.close()
new Notice('There is not at least on version in the file recovery.')
return
}

[this.leftContent, this.rightContent] = [this.versions[1].data, this.versions[0].data]
}

private appendRecVersions() {
// add the inner HTML element (the sync list) and keep a record
// of references to the elements
this.leftVList.push(
...this.makeVersions(this.leftHistory[1], this.versions, true)
);
this.rightVList.push(
...this.makeVersions(this.rightHistory[1], this.versions, false)
);
}

private makeVersions(el: HTMLElement, versions: recResult[], left: boolean = false) {
const versionList: rVList[] = [];
for (const version of versions) {
const date = new Date(version.ts)
const div = el.createDiv({
cls: 'sync-history-list-item',
text: date.toDateString() + ', ' + date.toLocaleTimeString(),
});
versionList.push({
html: div,
data: version.data
})
div.addEventListener('click', async () => {
if (left) {
const clickedEl = await this.generateVersionListener(
div,
//@ts-expect-error, the object has an html property, the other one is different
this.leftVList,
this.leftActive,
left
);
this.leftContent = version.data
this.diffAndDiffHtml();
} else {
const clickedEl = await this.generateVersionListener(
div,
//@ts-expect-error, the object has an html property, the other one is different
this.rightVList,
this.rightActive
);
this.rightContent = version.data
this.diffAndDiffHtml();
}
});

}
return versionList
}
}

0 comments on commit c7a2b74

Please sign in to comment.