Add copy to clipboard button

This commit is contained in:
Andras Schmelczer 2025-11-29 14:24:53 +00:00
parent 5417c1ddd0
commit 5905aa37b9
2 changed files with 53 additions and 4 deletions

View file

@ -14,8 +14,22 @@
margin: 0;
}
select {
cursor: pointer;
.logs-controls {
display: flex;
align-items: center;
gap: var(--size-4-2);
button {
display: flex;
align-items: center;
gap: var(--size-2-1);
padding: var(--size-2-2) var(--size-4-2);
cursor: pointer;
}
select {
cursor: pointer;
}
}
}

View file

@ -1,7 +1,7 @@
import "./logs-view.scss";
import type { WorkspaceLeaf } from "obsidian";
import { ItemView } from "obsidian";
import { ItemView, Notice, setIcon } from "obsidian";
import type { LogLine } from "sync-client";
import { LogLevel, type SyncClient } from "sync-client";
@ -78,7 +78,16 @@ export class LogsView extends ItemView {
text: "VaultLink logs"
});
verbositySection.createEl("select", {}, (dropdown) => {
const controls = verbositySection.createDiv({ cls: "logs-controls" });
const copyButton = controls.createEl("button", {
text: "Copy logs",
cls: "clickable-icon"
});
setIcon(copyButton, "clipboard-copy");
copyButton.addEventListener("click", () => this.copyLogsToClipboard());
controls.createEl("select", {}, (dropdown) => {
logLevels.forEach(({ label, value }) =>
dropdown.createEl("option", { text: label, value })
);
@ -102,6 +111,32 @@ export class LogsView extends ItemView {
this.updateView();
}
private copyLogsToClipboard(): void {
const logs = this.client.logger.getMessages(this.minLogLevel);
if (logs.length === 0) {
new Notice("No logs to copy");
return;
}
const formattedLogs = logs
.map((logLine) => {
const timestamp = logLine.timestamp.toLocaleString();
const level = logLine.level.toUpperCase();
return `[${timestamp}] ${level}: ${logLine.message}`;
})
.join("\n");
navigator.clipboard.writeText(formattedLogs)
.then(() => {
new Notice(`Copied ${logs.length} log entries to clipboard`);
})
.catch((error: unknown) => {
this.client.logger.error(`Failed to copy logs to clipboard: ${error}`);
new Notice("Failed to copy logs to clipboard");
});
}
private updateView(): void {
const container = this.logsContainer;
if (container === undefined) {