From a472ef880cb3fb3031dbf73027a2cc38c7ec3238 Mon Sep 17 00:00:00 2001 From: joshuakto <34743132+joshuakto@users.noreply.github.com> Date: Mon, 4 Mar 2024 17:45:47 +0800 Subject: [PATCH 1/3] added tools for debugging on dev branch --- main.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/main.ts b/main.ts index 23de926..0f8c9bb 100644 --- a/main.ts +++ b/main.ts @@ -151,6 +151,17 @@ export default class FitPlugin extends Plugin { } }); + // Command for computing an inputed file path's local sha for debugging purposes + this.addCommand({ + id: 'recompute-local-sha', + name: 'Update local store with new local sha, essentially ignoring local changes when pulling/pushing (Debug)', + callback: async () => { + this.localStore.localSha = await this.fit.computeLocalSha() + this.saveLocalStore() + new Notice("Local sha recomputed and stored, they will not be considered in future push/pull.") + } + }); + // This adds a settings tab so the user can configure various aspects of the plugin this.addSettingTab(new FitSettingTab(this.app, this)); From 8d55d6fcbd3ce46d01551c8374ac64e4cf4d3d67 Mon Sep 17 00:00:00 2001 From: joshuakto <34743132+joshuakto@users.noreply.github.com> Date: Mon, 4 Mar 2024 19:13:40 +0800 Subject: [PATCH 2/3] fixed pull file encoding issues by unifying to handling github response as base64 --- main.ts | 25 +++++++++++++++++++++++-- src/fitPull.ts | 7 ++----- src/pluginModal.ts | 36 ++++++++++++++++++++++++++++++++++++ src/vaultOps.ts | 21 +++++---------------- 4 files changed, 66 insertions(+), 23 deletions(-) diff --git a/main.ts b/main.ts index 0f8c9bb..5dcd56e 100644 --- a/main.ts +++ b/main.ts @@ -1,5 +1,5 @@ -import { Notice, Platform, Plugin } from 'obsidian'; -import { ComputeFileLocalShaModal } from 'src/pluginModal'; +import { Notice, Platform, Plugin, base64ToArrayBuffer } from 'obsidian'; +import { ComputeFileLocalShaModal, DebugModal } from 'src/pluginModal'; import { Fit } from 'src/fit'; import { FitPull } from 'src/fitPull'; import { FitPush } from 'src/fitPush'; @@ -162,6 +162,27 @@ export default class FitPlugin extends Plugin { } }); + + // Command for computing an inputed file path's local sha for debugging purposes + this.addCommand({ + id: 'pull-file', + name: 'Pull a file from remote for debugging purpose (Debug)', + callback: async () => { + new DebugModal( + this.app, + async (debugInput) => { + console.log(debugInput) + console.log("Getting blob for ") + const fileSha = this.localStore.lastFetchedRemoteSha[debugInput] + const content = await this.fit.getBlob(fileSha) + this.vaultOps.vault.createBinary('testing123.md', base64ToArrayBuffer(content)) + console.log(content) + } + ).open(); + } + }); + + // This adds a settings tab so the user can configure various aspects of the plugin this.addSettingTab(new FitSettingTab(this.app, this)); diff --git a/src/fitPull.ts b/src/fitPull.ts index 8db763b..1162670 100644 --- a/src/fitPull.ts +++ b/src/fitPull.ts @@ -1,5 +1,5 @@ import { VaultOperations } from "./vaultOps"; -import { ChangeType, compareSha, getFileEncoding } from "./utils"; +import { ChangeType, compareSha } from "./utils"; import { Fit } from "./fit"; import { Notice } from "obsidian"; import { LocalStores } from "main"; @@ -89,11 +89,8 @@ export class FitPull implements IFitPull { // Get changes from remote, pathShaMap is coupled to the Fit plugin design async getRemoteNonDeletionChangesContent(pathShaMap: Record) { const remoteChanges = Object.entries(pathShaMap).map(async ([path, file_sha]) => { - const encoding = getFileEncoding(path) const content = await this.fit.getBlob(file_sha); - const isBinary = encoding == "base64" - const decodedContent = isBinary ? content : atob(content); - return {path, content: decodedContent, encoding}; + return {path, content}; }) return await Promise.all(remoteChanges) } diff --git a/src/pluginModal.ts b/src/pluginModal.ts index ef1bb76..915a200 100644 --- a/src/pluginModal.ts +++ b/src/pluginModal.ts @@ -30,6 +30,42 @@ export class ComputeFileLocalShaModal extends Modal { })); } + onClose() { + const {contentEl} = this; + contentEl.empty(); + } +} + +export class DebugModal extends Modal { + debugInput: string; + onSubmit: (result: string) => void; + + constructor(app: App, onSubmit: (result: string) => void) { + super(app); + this.onSubmit = onSubmit; + } + + onOpen() { + const {contentEl} = this; + contentEl.createEl("h1", { text: "Debug input:" }); + new Setting(contentEl) + .setName("input") + .addText((text) => + text.onChange((value) => { + this.debugInput = value + })); + + new Setting(contentEl) + .addButton((btn) => + btn + .setButtonText("Submit") + .setCta() + .onClick(() => { + this.close(); + this.onSubmit(this.debugInput); + })); + } + onClose() { const {contentEl} = this; contentEl.empty(); diff --git a/src/vaultOps.ts b/src/vaultOps.ts index 5c6b614..f8a1054 100644 --- a/src/vaultOps.ts +++ b/src/vaultOps.ts @@ -1,6 +1,5 @@ import { warn } from "console"; import { Notice, TFile, Vault, base64ToArrayBuffer } from "obsidian"; -import { getFileEncoding } from "./utils"; export interface IVaultOperations { vault: Vault @@ -30,8 +29,8 @@ export class VaultOperations implements IVaultOperations { } async ensureFolderExists(path: string): Promise { - // extract folder path, return empty string is no folder path is matched - const folderPath = path.match(/^(.*\/)/)?.[1] || ''; + // extract folder path, return empty string is no folder path is matched (exclude the last /) + const folderPath = path.match(/^(.*)\//)?.[1] || ''; if (folderPath != "" && !(this.vault.getFolderByPath(folderPath))) { await this.vault.createFolder(folderPath) } @@ -41,28 +40,18 @@ export class VaultOperations implements IVaultOperations { // adopted getAbstractFileByPath for mobile compatiability, TODO: check whether additional checks needed to validate instance of TFile // temporary fix that works temporarily since path are garanteed to be for files not folders const file = this.vault.getAbstractFileByPath(path) as TFile - const encoding = getFileEncoding(path) - const isBinary = encoding === "base64" if (file) { - if (isBinary) { - await this.vault.modifyBinary(file, base64ToArrayBuffer(content)) - } else { - await this.vault.modify(file, content) - } + await this.vault.modifyBinary(file, base64ToArrayBuffer(content)) } else { this.ensureFolderExists(path) - if (isBinary) { - await this.vault.createBinary(path, base64ToArrayBuffer(content)) - } else { - await this.vault.create(path, content) - } + await this.vault.createBinary(path, base64ToArrayBuffer(content)) } new Notice(`${path} ${file ? 'updated' : 'copied'} to local drive.`, this.noticeDuration); return } async updateLocalFiles( - addToLocal: {path: string, content: string, encoding: string}[], + addToLocal: {path: string, content: string}[], deleteFromLocal: Array) { // Process file additions or updates const writeOperations = addToLocal.map(async ({path, content}) => { From e9d154e5e4482c26e6c3fcdcf91d45b2cc0824e3 Mon Sep 17 00:00:00 2001 From: joshuakto <34743132+joshuakto@users.noreply.github.com> Date: Mon, 4 Mar 2024 23:01:23 +0800 Subject: [PATCH 3/3] removed dev tools --- main.ts | 51 +--------------------------------------------- src/pluginModal.ts | 36 -------------------------------- 2 files changed, 1 insertion(+), 86 deletions(-) diff --git a/main.ts b/main.ts index 5dcd56e..981efec 100644 --- a/main.ts +++ b/main.ts @@ -1,5 +1,4 @@ -import { Notice, Platform, Plugin, base64ToArrayBuffer } from 'obsidian'; -import { ComputeFileLocalShaModal, DebugModal } from 'src/pluginModal'; +import { Notice, Platform, Plugin } from 'obsidian'; import { Fit } from 'src/fit'; import { FitPull } from 'src/fitPull'; import { FitPush } from 'src/fitPush'; @@ -135,54 +134,6 @@ export default class FitPlugin extends Plugin { // add class to ribbon element to afford styling, refer to styles.css this.fitPushRibbonIconEl.addClass('fit-push-ribbon-el'); - // This adds a status bar item to the bottom of the app. Does not work on mobile apps. - const statusBarItemEl = this.addStatusBarItem(); - statusBarItemEl.setText('Status Bar Text'); - - // Command for computing an inputed file path's local sha for debugging purposes - this.addCommand({ - id: 'compute-file-local-sha', - name: 'Compute local sha for file (Debug)', - callback: () => { - new ComputeFileLocalShaModal( - this.app, - async (queryFile) => console.log(await this.fit.computeFileLocalSha(queryFile)) - ).open(); - } - }); - - // Command for computing an inputed file path's local sha for debugging purposes - this.addCommand({ - id: 'recompute-local-sha', - name: 'Update local store with new local sha, essentially ignoring local changes when pulling/pushing (Debug)', - callback: async () => { - this.localStore.localSha = await this.fit.computeLocalSha() - this.saveLocalStore() - new Notice("Local sha recomputed and stored, they will not be considered in future push/pull.") - } - }); - - - // Command for computing an inputed file path's local sha for debugging purposes - this.addCommand({ - id: 'pull-file', - name: 'Pull a file from remote for debugging purpose (Debug)', - callback: async () => { - new DebugModal( - this.app, - async (debugInput) => { - console.log(debugInput) - console.log("Getting blob for ") - const fileSha = this.localStore.lastFetchedRemoteSha[debugInput] - const content = await this.fit.getBlob(fileSha) - this.vaultOps.vault.createBinary('testing123.md', base64ToArrayBuffer(content)) - console.log(content) - } - ).open(); - } - }); - - // This adds a settings tab so the user can configure various aspects of the plugin this.addSettingTab(new FitSettingTab(this.app, this)); diff --git a/src/pluginModal.ts b/src/pluginModal.ts index 915a200..ef1bb76 100644 --- a/src/pluginModal.ts +++ b/src/pluginModal.ts @@ -30,42 +30,6 @@ export class ComputeFileLocalShaModal extends Modal { })); } - onClose() { - const {contentEl} = this; - contentEl.empty(); - } -} - -export class DebugModal extends Modal { - debugInput: string; - onSubmit: (result: string) => void; - - constructor(app: App, onSubmit: (result: string) => void) { - super(app); - this.onSubmit = onSubmit; - } - - onOpen() { - const {contentEl} = this; - contentEl.createEl("h1", { text: "Debug input:" }); - new Setting(contentEl) - .setName("input") - .addText((text) => - text.onChange((value) => { - this.debugInput = value - })); - - new Setting(contentEl) - .addButton((btn) => - btn - .setButtonText("Submit") - .setCta() - .onClick(() => { - this.close(); - this.onSubmit(this.debugInput); - })); - } - onClose() { const {contentEl} = this; contentEl.empty();