Skip to content

Commit 4ac7a6b

Browse files
committed
feat(diff): Diff view
1 parent a50ed57 commit 4ac7a6b

File tree

8 files changed

+61
-42
lines changed

8 files changed

+61
-42
lines changed

src/components/PendingStatsView.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ import { CheckSettingsModal } from './CheckSettingsModal';
1717
export const PendingStatsViewComponent = (props: { plugin: InvioPlugin }) => {
1818
const { record, toLocalSelected, toRemoteSelected, getToLocalFileList, getToRemoteFileList, updateSelectedToLocalFileList, updateSelectedToRemoteFileList } = useStore();
1919
const toLocalTouched = getToLocalFileList();
20-
console.log('toLocaltouched - ', toLocalTouched)
2120

2221
const toRemoteTouched = getToRemoteFileList();
23-
console.log('toRemoteTouched - ', toRemoteTouched)
2422

2523
const treeToLocalData: DataNode[] = toLocalTouched?.map((item: any) => {
2624
item.title = item.key
@@ -34,9 +32,14 @@ export const PendingStatsViewComponent = (props: { plugin: InvioPlugin }) => {
3432
})
3533

3634
const onSelect = (selectedKeys: any, info: any, type: `ToLocal` | `ToRemote`) => {
37-
console.log('selected', selectedKeys, info);
3835
const key = selectedKeys[0]
3936
props.plugin.viewFileDiff(key, type === 'ToLocal' ? 'RemoteToLocal' : 'LocalToRemote')
37+
.then(file => {
38+
log.info('diff file changed - ', file);
39+
if (file) {
40+
props.plugin.pendingView()
41+
}
42+
})
4043
setTimeout(() => {
4144
selectedKeys = []
4245
}, 300)
@@ -51,22 +54,18 @@ export const PendingStatsViewComponent = (props: { plugin: InvioPlugin }) => {
5154
}
5255

5356
const onToLocalCheck: TreeProps['onCheck'] = (checkedKeys: string[], info: any) => {
54-
console.log('onLocalCheck', checkedKeys, info);
5557
updateSelectedToLocalFileList(checkedKeys.filter(key => toLocalTouched.find(t => t.key === key)))
5658
};
5759

5860
const onToRemoteCheck: TreeProps['onCheck'] = (checkedKeys: string[], info: any) => {
59-
console.log('onToRemoteCheck', checkedKeys, info);
6061
updateSelectedToRemoteFileList(checkedKeys.filter(key => toRemoteTouched.find(t => t.key === key)))
6162
};
6263

6364
const startSync = async () => {
64-
console.log('start syncing...', [...toLocalSelected, ...toRemoteSelected])
6565
await props.plugin.syncRun('manual', [...toLocalSelected, ...toRemoteSelected])
6666
}
6767

6868
const openSettings = () => {
69-
console.log('open settings');
7069
const modal = new CheckSettingsModal(props.plugin.app, props.plugin);
7170
modal.open();
7271
}

src/diff/abstract_diff_view.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import type { LangType, LangTypeAndAuto, TransItemType } from "../i18n";
88

99
type TviewOutputFormat = `side-by-side` | `line-by-line`
1010
export type TDiffType = `LocalToRemote` | `RemoteToLocal` | `Conflict`
11-
11+
export interface IRemoteFile {
12+
data: string;
13+
ts: number;
14+
path: string;
15+
deleted?: boolean;
16+
}
1217
export default abstract class DiffView extends Modal {
1318
plugin: InvioPlugin;
1419
app: App;
@@ -161,8 +166,12 @@ export default abstract class DiffView extends Modal {
161166
return this.plugin.i18n.t(x, vars);
162167
}
163168

164-
public async changeFileAndCloseModal(contents: string) {
165-
await this.app.vault.modify(this.file, contents);
169+
public async changeFileAndCloseModal(contents: string, deleted?: boolean) {
170+
if (deleted) {
171+
await this.app.vault.adapter.trashLocal(this.file.path);
172+
} else {
173+
await this.app.vault.modify(this.file, contents);
174+
}
166175
this.fileChangedHook && this.fileChangedHook(this.file);
167176
this.silentClose = true
168177

src/diff/conflict_diff_view.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import {
99
import type InvioPlugin from '../main';
1010
import type { recResult, vRecoveryItem } from './interfaces';
1111
import { FILE_REC_WARNING } from './constants';
12-
import DiffView, { TDiffType } from './abstract_diff_view';
12+
import DiffView, { IRemoteFile } from './abstract_diff_view';
1313
import { Diff2HtmlConfig, html } from 'diff2html';
1414
export default class ConflictDiffView extends DiffView {
15-
remote: recResult
15+
remote: IRemoteFile
1616
versions: recResult[];
1717
rightVList: vRecoveryItem[];
18-
constructor(plugin: InvioPlugin, app: App, file: TFile, remoteFile: recResult, fileChangedHook: (f: TFile) => void, cancelHook: () => void) {
18+
constructor(plugin: InvioPlugin, app: App, file: TFile, remoteFile: IRemoteFile, fileChangedHook: (f: TFile) => void, cancelHook: () => void) {
1919
super(plugin, app, file, fileChangedHook, cancelHook);
2020
this.versions = [];
2121
this.rightVList = [];
@@ -52,6 +52,7 @@ export default class ConflictDiffView extends DiffView {
5252
<table class="d2h-diff-table">
5353
<tbody class="d2h-diff-tbody">
5454
{{{diffs.left}}}
55+
${this.remote.deleted ? '<div class="d2h-code-title">The file does not exist</div>' : ''}
5556
</tbody>
5657
</table>
5758
</div>
@@ -92,7 +93,6 @@ export default class ConflictDiffView extends DiffView {
9293
public basicHtml(diff: string): void {
9394
// set title
9495
this.titleEl.setText(this.t('diff_view_conflict_title'));
95-
// set top action bar
9696

9797
const contentParent = this.syncHistoryContentContainer.parentElement;
9898
const topAction = contentParent.createDiv({
@@ -108,7 +108,7 @@ export default class ConflictDiffView extends DiffView {
108108
});
109109
diffResetBtn.addEventListener('click', e => {
110110
e.preventDefault();
111-
this.changeFileAndCloseModal(this.leftContent);
111+
this.changeFileAndCloseModal(this.leftContent, this.remote.deleted);
112112

113113
new Notice(
114114
`The ${this.file.basename} file has been overwritten with the online remote version.`
@@ -162,7 +162,7 @@ export default class ConflictDiffView extends DiffView {
162162
this.versions.push(version);
163163
}
164164
}
165-
if (!(this.versions.length > 1)) {
165+
if (!(this.versions.length > 0)) {
166166
this.close();
167167
new Notice(
168168
'There is not at least on version in the file recovery.'

src/diff/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { App, TFile, Notice, Vault } from 'obsidian';
2-
import LocalToRemoteDiffView, { IRemoteFile } from './local_to_remote_diff_view';
2+
import LocalToRemoteDiffView from './local_to_remote_diff_view';
33
import RemoteToLocalDiffView from './remote_to_local_diff_view';
44
import ConflictDiffView from './conflict_diff_view';
5-
import { TDiffType } from './abstract_diff_view';
5+
import { TDiffType, IRemoteFile } from './abstract_diff_view';
66
import type InvioPlugin from '../main';
77
import { diff_match_patch } from './effective_diff.js';
88
import { log } from '../moreOnLog'

src/diff/local_to_remote_diff_view.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@ import { Diff2HtmlConfig, html } from 'diff2html';
1010
import type InvioPlugin from '../main';
1111
import type { recResult, vRecoveryItem } from './interfaces';
1212
import { FILE_REC_WARNING } from './constants';
13-
import DiffView, { TDiffType } from './abstract_diff_view';
14-
export interface IRemoteFile {
15-
data: string;
16-
ts: number;
17-
path: string;
18-
}
13+
import DiffView, { IRemoteFile } from './abstract_diff_view';
14+
1915
export default class LocalToRemoteDiffView extends DiffView {
2016
remote: IRemoteFile
2117
versions: recResult[];
@@ -57,6 +53,7 @@ export default class LocalToRemoteDiffView extends DiffView {
5753
<table class="d2h-diff-table">
5854
<tbody class="d2h-diff-tbody">
5955
{{{diffs.left}}}
56+
${this.remote.deleted ? '<div class="d2h-code-title">The file does not exist</div>' : ''}
6057
</tbody>
6158
</table>
6259
</div>
@@ -132,8 +129,7 @@ export default class LocalToRemoteDiffView extends DiffView {
132129
});
133130
diffResetBtn.addEventListener('click', e => {
134131
e.preventDefault();
135-
this.changeFileAndCloseModal(this.leftContent);
136-
132+
this.changeFileAndCloseModal(this.leftContent, this.remote.deleted);
137133
new Notice(
138134
`The ${this.file.basename} file has been overwritten with the online remote version.`
139135
);
@@ -166,7 +162,7 @@ export default class LocalToRemoteDiffView extends DiffView {
166162
this.versions.push(version);
167163
}
168164
}
169-
if (!(this.versions.length > 1)) {
165+
if (!(this.versions.length > 0)) {
170166
this.close();
171167
new Notice(
172168
'There is not at least on version in the file recovery.'

src/diff/remote_to_local_diff_view.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@ import { Diff2HtmlConfig, html } from 'diff2html';
55
import type InvioPlugin from '../main';
66
import type { recResult, vRecoveryItem } from './interfaces';
77
import { FILE_REC_WARNING } from './constants';
8-
import DiffView, { TDiffType } from './abstract_diff_view';
9-
export interface IRemoteFile {
10-
data: string;
11-
ts: number;
12-
path: string;
13-
}
8+
import DiffView, { IRemoteFile } from './abstract_diff_view';
9+
1410
export default class RemoteToLocalDiffView extends DiffView {
1511
remote: IRemoteFile
1612
versions: recResult[];
@@ -62,6 +58,7 @@ export default class RemoteToLocalDiffView extends DiffView {
6258
<table class="d2h-diff-table">
6359
<tbody class="d2h-diff-tbody">
6460
{{{diffs.right}}}
61+
${this.remote.deleted ? '<div class="d2h-code-title">The file does not exist</div>' : ''}
6562
</tbody>
6663
</table>
6764
</div>

src/main.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,21 @@ export default class InvioPlugin extends Plugin {
130130
return null
131131
})
132132
log.info('remote md: ', remoteMD);
133-
if (!remoteMD?.data) {
134-
new Notice(`The file(${filePath}) does not exist remotely`)
135-
return;
133+
134+
if (remoteMD.deleted) {
135+
remoteMD.data = '';
136+
remoteMD.lastModified = Date.valueOf();
137+
}
138+
let remoteData = remoteMD?.data || '';
139+
let remoteModiDate = remoteMD?.lastModified || Date.valueOf();
140+
if (!remoteData) {
141+
// new Notice(`The file(${filePath}) does not exist remotely`)
136142
}
137143
return new Promise((resolve, reject) => {
138144
openDiffModal(this.app, this, file, {
139-
data: remoteMD.data,
140-
ts: remoteMD?.lastModified,
145+
deleted: remoteMD.deleted,
146+
data: remoteData,
147+
ts: remoteModiDate,
141148
path: filePath
142149
}, diffType, (file: TFile) => {
143150
if (file) {
@@ -886,7 +893,7 @@ export default class InvioPlugin extends Plugin {
886893
await this.doSyncRunAbort(8000);
887894
}
888895
const { toRemoteFiles, toLocalFiles } = await this.syncRun('pre') || {};
889-
log.info('to remote files: ', toRemoteFiles, toLocalFiles);
896+
log.info('toughed files: ', toRemoteFiles, toLocalFiles);
890897
const touched = [ ...(toRemoteFiles || []), ...(toLocalFiles || []) ]
891898
await StatsView.activateStatsView(this);
892899
const view = StatsView.getStatsView(this, 'PendingStats');

src/sync.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,15 +339,26 @@ export const fetchRemoteFileMD = async (
339339
}
340340

341341
log.info('fetch remote file path: ', remoteFilePath);
342-
const buf = await client.downloadFromRemote(
342+
const buf: any = await client.downloadFromRemote(
343343
remoteFilePath,
344344
RemoteSrcPrefix,
345345
vault,
346346
null,
347347
password,
348348
'',
349349
true
350-
);
350+
).catch(err => {
351+
if (err?.status === 404) {
352+
return {
353+
deleted: true,
354+
data: ''
355+
}
356+
}
357+
throw err;
358+
})
359+
if (buf?.deleted) {
360+
return buf;
361+
}
351362
const resp: RemoteItem = await client.getRemoteMeta(RemoteSrcPrefix + remoteFilePath)
352363

353364
let data;
@@ -440,7 +451,7 @@ export const pruneTouchedFiles = async (vault: Vault, client: RemoteClient, list
440451
return null;
441452
})
442453
log.info('remote md: ', remoteMD);
443-
if (!remoteMD) {
454+
if (!remoteMD || remoteMD.deleted) {
444455
return;
445456
}
446457
const diff = await isRemoteFileDiff(vault, item.key, remoteMD.data);

0 commit comments

Comments
 (0)