Skip to content

Commit d0f04d5

Browse files
committed
Add utility buttons in settings to copy or clean logs
1 parent 7e7edda commit d0f04d5

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

src/logger.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ export default class Logger {
4040
);
4141
}
4242

43+
async read(): Promise<string> {
44+
return await this.vault.adapter.read(this.logFile);
45+
}
46+
47+
async clean(): Promise<void> {
48+
return await this.vault.adapter.write(this.logFile, "");
49+
}
50+
4351
enable(): void {
4452
this.enabled = true;
4553
}

src/settings/tab.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
import { PluginSettingTab, App, Setting, TextComponent, Modal } from "obsidian";
1+
import {
2+
PluginSettingTab,
3+
App,
4+
Setting,
5+
TextComponent,
6+
Modal,
7+
Notice,
8+
} from "obsidian";
29
import GitHubSyncPlugin from "src/main";
10+
import { copyToClipboard } from "src/utils";
311

412
export default class GitHubSyncSettingsTab extends PluginSettingTab {
513
plugin: GitHubSyncPlugin;
@@ -268,6 +276,30 @@ export default class GitHubSyncSettingsTab extends PluginSettingTab {
268276
});
269277
});
270278

279+
new Setting(containerEl)
280+
.setName("Copy logs")
281+
.setDesc("Copy the log file content, this is useful to report bugs.")
282+
.addButton((button) => {
283+
button.setButtonText("Copy").onClick(async () => {
284+
const logs: string = await this.plugin.logger.read();
285+
try {
286+
await copyToClipboard(logs);
287+
new Notice("Logs copied", 5000);
288+
} catch (err) {
289+
new Notice(`Failed copying logs: ${err}`, 10000);
290+
}
291+
});
292+
});
293+
294+
new Setting(containerEl)
295+
.setName("Clean logs")
296+
.setDesc("Delete all existing logs.")
297+
.addButton((button) => {
298+
button.setButtonText("Clean").onClick(async () => {
299+
await this.plugin.logger.clean();
300+
});
301+
});
302+
271303
new Setting(containerEl)
272304
.setName("Reset")
273305
.setDesc("Reset the plugin settings and metadata")

src/utils.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,28 @@ export function decodeBase64String(s: string): string {
1212
const decoder = new TextDecoder();
1313
return decoder.decode(buffer);
1414
}
15+
16+
/**
17+
* Copies the provided text to the system clipboard.
18+
* Uses the modern Clipboard API with a fallback to older APIs.
19+
*
20+
* @param text The string to be copied to clipboard
21+
* @returns A promise that resolves when the text has been copied
22+
*/
23+
export async function copyToClipboard(text: string) {
24+
try {
25+
await navigator.clipboard.writeText(text);
26+
} catch (err) {
27+
// Fallback for devices like iOS that don't support Clipboard API
28+
const textarea = document.createElement("textarea");
29+
textarea.value = text;
30+
textarea.setAttribute("readonly", "");
31+
textarea.style.position = "absolute";
32+
textarea.style.left = "-9999px";
33+
document.body.appendChild(textarea);
34+
35+
textarea.select();
36+
document.execCommand("copy");
37+
document.body.removeChild(textarea);
38+
}
39+
}

0 commit comments

Comments
 (0)