|
| 1 | +import { |
| 2 | + Plugin, |
| 3 | + App, |
| 4 | + TFile, |
| 5 | + Notice, |
| 6 | + // @ts-ignore |
| 7 | + setTooltip |
| 8 | +} from 'obsidian'; |
| 9 | +import type InvioPlugin from '../main'; |
| 10 | +import type { recResult, vRecoveryItem } from './interfaces'; |
| 11 | +import { FILE_REC_WARNING } from './constants'; |
| 12 | +import DiffView, { TDiffType } from './abstract_diff_view'; |
| 13 | + |
| 14 | +export default class ConflictDiffView extends DiffView { |
| 15 | + remote: recResult |
| 16 | + versions: recResult[]; |
| 17 | + rightVList: vRecoveryItem[]; |
| 18 | + constructor(plugin: InvioPlugin, app: App, file: TFile, remoteFile: recResult, fileChangedHook: (f: TFile) => void, cancelHook: () => void) { |
| 19 | + super(plugin, app, file, fileChangedHook, cancelHook); |
| 20 | + this.versions = []; |
| 21 | + this.rightVList = []; |
| 22 | + this.remote = remoteFile; |
| 23 | + this.leftContent = remoteFile?.data; |
| 24 | + this.leftName = 'remote version' |
| 25 | + this.rightName = 'local version' |
| 26 | + this.htmlConfig = { |
| 27 | + drawFileList: true, |
| 28 | + diffStyle: 'word', |
| 29 | + matchWordsThreshold: 0.25, |
| 30 | + outputFormat: this.viewOutputFormat, |
| 31 | + rawTemplates: { |
| 32 | + 'line-by-line-file-diff': `<div id="{{fileHtmlId}}" class="d2h-file-wrapper" data-lang="{{file.language}}"> |
| 33 | + <div class="d2h-file-header"> |
| 34 | + {{{filePath}}} |
| 35 | + </div> |
| 36 | + <div class="d2h-file-diff"> |
| 37 | + <div class="d2h-code-wrapper"> |
| 38 | + <table class="d2h-diff-table"> |
| 39 | + <tbody class="d2h-diff-tbody"> |
| 40 | + {{{diffs}}} |
| 41 | + </tbody> |
| 42 | + </table> |
| 43 | + </div> |
| 44 | + </div> |
| 45 | + </div>`, |
| 46 | + 'side-by-side-file-diff': `<div id="{{fileHtmlId}}" class="d2h-file-wrapper" data-lang="{{file.language}}"> |
| 47 | + <div class="d2h-files-diff"> |
| 48 | + <div class="d2h-file-side-diff"> |
| 49 | + <div class="d2h-code-title">${this.leftName}</div> |
| 50 | + <div class="d2h-code-wrapper"> |
| 51 | + <table class="d2h-diff-table"> |
| 52 | + <tbody class="d2h-diff-tbody"> |
| 53 | + {{{diffs.left}}} |
| 54 | + </tbody> |
| 55 | + </table> |
| 56 | + </div> |
| 57 | + </div> |
| 58 | + <div class="d2h-file-side-diff"> |
| 59 | + <div class="d2h-code-title">${this.rightName}</div> |
| 60 | + <div class="d2h-code-wrapper"> |
| 61 | + <table class="d2h-diff-table"> |
| 62 | + <tbody class="d2h-diff-tbody"> |
| 63 | + {{{diffs.right}}} |
| 64 | + </tbody> |
| 65 | + </table> |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + </div> |
| 69 | + </div>` |
| 70 | + } |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + async onOpen() { |
| 75 | + super.onOpen(); |
| 76 | + await this.getInitialVersions(); |
| 77 | + const diff = this.getDiff(); |
| 78 | + this.makeHistoryLists(FILE_REC_WARNING); |
| 79 | + this.basicHtml(diff as string); |
| 80 | + this.appendVersions(); |
| 81 | + this.makeMoreGeneralHtml(); |
| 82 | + } |
| 83 | + |
| 84 | + public basicHtml(diff: string): void { |
| 85 | + // set title |
| 86 | + this.titleEl.setText(this.t('diff_view_conflict_title')); |
| 87 | + // set top action bar |
| 88 | + |
| 89 | + const contentParent = this.syncHistoryContentContainer.parentElement; |
| 90 | + const topAction = contentParent.createDiv({ |
| 91 | + cls: 'sync-history-content-container-top' |
| 92 | + }); |
| 93 | + |
| 94 | + const viewChangeBtn = topAction.createDiv({ |
| 95 | + cls: ['view-action', 'btn'], |
| 96 | + text: this.t('view_change_btn') |
| 97 | + }) |
| 98 | + |
| 99 | + const diffResetBtn = topAction.createDiv({ |
| 100 | + cls: ['view-action', 'btn'], |
| 101 | + text: this.t('diff_reset_btn') |
| 102 | + }) |
| 103 | + setTooltip(diffResetBtn, 'Click to replace the file with online version', { |
| 104 | + placement: 'top', |
| 105 | + }); |
| 106 | + diffResetBtn.addEventListener('click', e => { |
| 107 | + e.preventDefault(); |
| 108 | + this.changeFileAndCloseModal(this.leftContent); |
| 109 | + |
| 110 | + new Notice( |
| 111 | + `The ${this.file.basename} file has been overwritten with the online remote version.` |
| 112 | + ); |
| 113 | + }) |
| 114 | + setTooltip(viewChangeBtn, 'Click to change diff view', { |
| 115 | + placement: 'top', |
| 116 | + }); |
| 117 | + viewChangeBtn.addEventListener('click', e => { |
| 118 | + e.preventDefault(); |
| 119 | + this.viewOutputFormat = ('line-by-line' === this.viewOutputFormat) ? 'side-by-side' : 'line-by-line'; |
| 120 | + console.log('diff styles changed to ', this.viewOutputFormat) |
| 121 | + this.reload({ |
| 122 | + outputFormat: this.viewOutputFormat |
| 123 | + }); |
| 124 | + }) |
| 125 | + |
| 126 | + // add diff to container |
| 127 | + this.syncHistoryContentContainer.innerHTML = diff; |
| 128 | + |
| 129 | + // add history lists and diff to DOM |
| 130 | + // this.contentEl.appendChild(this.leftHistory[0]); |
| 131 | + this.contentEl.appendChild(this.syncHistoryContentContainer?.parentNode); |
| 132 | + this.contentEl.appendChild(this.rightHistory[0]); |
| 133 | + } |
| 134 | + |
| 135 | + async getInitialVersions() { |
| 136 | + const fileRecovery = await this.app.internalPlugins.plugins[ |
| 137 | + 'file-recovery' |
| 138 | + ].instance.db |
| 139 | + .transaction('backups', 'readonly') |
| 140 | + .store.index('path') |
| 141 | + .getAll(); |
| 142 | + const fileContent = await this.app.vault.read(this.file); |
| 143 | + // correct date is calculated later |
| 144 | + this.versions.push({ path: this.file.path, ts: Date.now(), data: fileContent }); |
| 145 | + const len = fileRecovery.length - 1; |
| 146 | + for (let i = len; i >= 0; i--) { |
| 147 | + const version = fileRecovery[i]; |
| 148 | + if (version.path === this.file.path) { |
| 149 | + this.versions.push(version); |
| 150 | + } |
| 151 | + } |
| 152 | + if (!(this.versions.length > 1)) { |
| 153 | + this.close(); |
| 154 | + new Notice( |
| 155 | + 'There is not at least on version in the file recovery.' |
| 156 | + ); |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + // insert remote record |
| 161 | + const remoteUpdateTS = this.remote.ts; |
| 162 | + const idx = this.versions.findIndex(item => item.ts <= remoteUpdateTS) |
| 163 | + this.versions.splice(idx, 0, { |
| 164 | + ...this.remote, |
| 165 | + isRemote: true |
| 166 | + }) |
| 167 | + this.rightContent = this.versions[0].data; |
| 168 | + } |
| 169 | + |
| 170 | + appendVersions() { |
| 171 | + // add the inner HTML element (the sync list) and keep a record |
| 172 | + // of references to the elements |
| 173 | + this.rightVList.push( |
| 174 | + ...this.appendRecoveryVersions( |
| 175 | + this.rightHistory[1], |
| 176 | + this.versions, |
| 177 | + ) |
| 178 | + ); |
| 179 | + } |
| 180 | + |
| 181 | + private appendRecoveryVersions( |
| 182 | + el: HTMLElement, |
| 183 | + versions: recResult[], |
| 184 | + ): vRecoveryItem[] { |
| 185 | + const versionList: vRecoveryItem[] = []; |
| 186 | + for (let i = 0; i < versions.length; i++) { |
| 187 | + const version = versions[i]; |
| 188 | + let date = new Date(version.ts); |
| 189 | + |
| 190 | + let div = el.createDiv({ |
| 191 | + cls: 'sync-history-list-item', |
| 192 | + attr: { |
| 193 | + id: this.ids.right, |
| 194 | + }, |
| 195 | + }); |
| 196 | + |
| 197 | + this.ids.right += 1; |
| 198 | + if (i === 0) { |
| 199 | + div.createDiv({ text: this.t('diff_version_list_title') }); |
| 200 | + } else if (version.isRemote) { |
| 201 | + div.createDiv({ |
| 202 | + cls: 'sync-history-list-item-remote', |
| 203 | + text: 'Remote Updated At: ' + date.toLocaleString(), |
| 204 | + }); |
| 205 | + } else { |
| 206 | + div.createDiv({ |
| 207 | + text: date.toLocaleString(), |
| 208 | + }); |
| 209 | + } |
| 210 | + versionList.push({ |
| 211 | + html: div, |
| 212 | + data: version.data, |
| 213 | + }); |
| 214 | + if (!version.isRemote) { |
| 215 | + div.addEventListener('click', async () => { |
| 216 | + const clickedEl = (await this.generateVersionListener( |
| 217 | + div, |
| 218 | + this.rightVList, |
| 219 | + this.rightActive |
| 220 | + )) as vRecoveryItem; |
| 221 | + this.rightContent = version.data; |
| 222 | + this.syncHistoryContentContainer.innerHTML = |
| 223 | + this.getDiff() as string; |
| 224 | + }); |
| 225 | + } |
| 226 | + } |
| 227 | + return versionList; |
| 228 | + } |
| 229 | +} |
0 commit comments