Extract library from plugin
This commit is contained in:
parent
8374c971ee
commit
ae3acb9e1e
37 changed files with 61 additions and 77 deletions
120
obsidian-plugin/src/views/logs-view.ts
Normal file
120
obsidian-plugin/src/views/logs-view.ts
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
import type { WorkspaceLeaf } from "obsidian";
|
||||
import { ItemView } from "obsidian";
|
||||
import type VaultLinkPlugin from "src/vault-link-plugin";
|
||||
import type { Database } from "sync-client";
|
||||
import { Logger } from "sync-client";
|
||||
|
||||
export class LogsView extends ItemView {
|
||||
public static readonly TYPE = "logs-view";
|
||||
public static readonly ICON = "logs";
|
||||
|
||||
public constructor(
|
||||
private readonly plugin: VaultLinkPlugin,
|
||||
private readonly database: Database,
|
||||
leaf: WorkspaceLeaf
|
||||
) {
|
||||
super(leaf);
|
||||
this.icon = LogsView.ICON;
|
||||
Logger.getInstance().addOnMessageListener(() => {
|
||||
this.updateView();
|
||||
});
|
||||
|
||||
database.addOnSettingsChangeHandlers((newSettings, oldSettings) => {
|
||||
if (newSettings.minimumLogLevel !== oldSettings.minimumLogLevel) {
|
||||
this.updateView();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static formatTimestamp(timestamp: Date): string {
|
||||
return timestamp.toTimeString().split(" ")[0];
|
||||
}
|
||||
|
||||
public getViewType(): string {
|
||||
return LogsView.TYPE;
|
||||
}
|
||||
|
||||
public getDisplayText(): string {
|
||||
return "VaultLink logs";
|
||||
}
|
||||
|
||||
public async onOpen(): Promise<void> {
|
||||
this.updateView();
|
||||
|
||||
const container = this.containerEl.children[1];
|
||||
container.addClass("logs-view");
|
||||
}
|
||||
|
||||
private updateView(): void {
|
||||
const container = this.containerEl.children[1];
|
||||
|
||||
let logsContainer = container
|
||||
.getElementsByClassName("logs-container")
|
||||
.item(0);
|
||||
const scrollPosition = logsContainer?.scrollTop;
|
||||
|
||||
container.empty();
|
||||
|
||||
container.createEl("h4", { text: "VaultLink logs" });
|
||||
container.createEl(
|
||||
"p",
|
||||
{
|
||||
text: "This view displays logs generated by VaultLink. You can set the log level in the "
|
||||
},
|
||||
(p) => {
|
||||
p.createEl(
|
||||
"a",
|
||||
{
|
||||
text: "settings"
|
||||
},
|
||||
(button) => {
|
||||
button.addEventListener("click", () => {
|
||||
this.plugin.openSettings();
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
p.createSpan({ text: "." });
|
||||
}
|
||||
);
|
||||
|
||||
const logs = Logger.getInstance().getMessages(
|
||||
this.database.getSettings().minimumLogLevel
|
||||
);
|
||||
|
||||
if (logs.length === 0) {
|
||||
container.createEl("p", { text: "No logs available yet." });
|
||||
return;
|
||||
}
|
||||
|
||||
logsContainer = container.createDiv(
|
||||
{ cls: "logs-container" },
|
||||
(element) => {
|
||||
logs.forEach((message) =>
|
||||
element.createDiv(
|
||||
{
|
||||
cls: ["log-message", message.level]
|
||||
},
|
||||
(messageContainer) => {
|
||||
messageContainer.createEl("span", {
|
||||
text: LogsView.formatTimestamp(
|
||||
message.timestamp
|
||||
),
|
||||
cls: "timestamp"
|
||||
});
|
||||
messageContainer.createEl("span", {
|
||||
text: message.message
|
||||
});
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
if (scrollPosition !== undefined) {
|
||||
logsContainer.scrollTop = scrollPosition;
|
||||
} else {
|
||||
logsContainer.scrollTop = logsContainer.scrollHeight;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue