stfrigerio_sqliteDB/main.ts

135 lines
3.4 KiB
TypeScript
Raw Normal View History

import {
App,
Plugin,
PluginSettingTab,
Setting,
FileSystemAdapter,
Editor,
MarkdownView,
2025-01-29 19:58:04 +00:00
MarkdownPostProcessorContext
2025-01-29 18:20:48 +00:00
} from "obsidian";
2025-01-29 19:58:04 +00:00
import { DBService } from "./src/dbService";
import { inspectTableStructure, convertEntriesInNotes } from "./src/commands";
import { processSqlBlock, processSqlChartBlock, renderDatePicker } from "./src/codeblocks";
2025-02-04 16:08:15 +00:00
import { pickTableName } from "./src/helpers";
import { SQLiteDBSettings, DEFAULT_SETTINGS } from "./src/types";
import { injectDatePickerStyles } from "src/styles/datePickerInject";
2025-04-04 16:35:26 +00:00
import "./src/webcomponents/habitCounter";
2025-01-29 18:20:48 +00:00
export default class SQLiteDBPlugin extends Plugin {
settings: SQLiteDBSettings;
private dbService: DBService;
2025-01-29 17:35:31 +00:00
async onload() {
// init
2025-01-29 17:35:31 +00:00
await this.loadSettings();
2025-02-06 10:29:18 +00:00
this.dbService = new DBService(this.app);
2025-01-29 19:58:04 +00:00
await this.openDatabase();
injectDatePickerStyles();
this.addCommand({
id: "inspect-table-structure",
name: "Inspect table structure",
editorCallback: async (editor: Editor, view: MarkdownView) => {
// ensure DB is loaded (if not, load)
await this.openDatabase();
await inspectTableStructure(this.dbService, editor, this.app);
},
});
2025-01-29 17:35:31 +00:00
this.addCommand({
id: "dump-table-to-notes",
2025-02-22 15:25:13 +00:00
name: "Dump table to notes",
2025-01-29 18:20:48 +00:00
callback: async () => {
await this.openDatabase();
// 1) pick a table
const chosenTable = await pickTableName(this.dbService, this.app);
if (!chosenTable) {
return; // user canceled or no tables
}
// 2) Call the dump function with the chosen table
await convertEntriesInNotes(this.dbService, chosenTable, this.app);
2025-01-29 18:20:48 +00:00
},
2025-01-29 17:35:31 +00:00
});
2025-01-29 19:58:04 +00:00
this.registerMarkdownCodeBlockProcessor(
"sql",
2025-01-29 19:58:04 +00:00
async (source: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
await processSqlBlock(this.dbService, source, el);
}
);
2025-01-29 17:35:31 +00:00
2025-02-03 13:28:51 +00:00
this.registerMarkdownCodeBlockProcessor(
"sql-chart",
async (source: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
await processSqlChartBlock(this.dbService, source, el);
}
);
this.registerMarkdownCodeBlockProcessor(
"date-picker",
async (source: string, el: HTMLElement) => {
renderDatePicker(el, this.app);
}
);
this.addSettingTab(new SQLiteDBSettingTab(this.app, this));
2025-01-29 17:35:31 +00:00
}
onunload() {
console.log("Unloading SQLiteDBPlugin...");
2025-01-29 18:20:48 +00:00
}
private async openDatabase(forceReload = true) {
const adapter = this.app.vault.adapter;
let basePath: string;
if (adapter instanceof FileSystemAdapter) {
basePath = adapter.getBasePath();
} else {
basePath = (adapter as any).getFullPath("");
2025-01-29 18:20:48 +00:00
}
await this.dbService.ensureDBLoaded(this.settings, basePath, forceReload);
2025-01-29 17:35:31 +00:00
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class SQLiteDBSettingTab extends PluginSettingTab {
plugin: SQLiteDBPlugin;
2025-01-29 17:35:31 +00:00
constructor(app: App, plugin: SQLiteDBPlugin) {
2025-01-29 17:35:31 +00:00
super(app, plugin);
this.plugin = plugin;
}
display(): void {
2025-01-29 18:20:48 +00:00
const { containerEl } = this;
2025-01-29 17:35:31 +00:00
containerEl.empty();
new Setting(containerEl)
2025-02-22 15:25:13 +00:00
.setName("Database file path")
2025-01-29 18:20:48 +00:00
.setDesc("Absolute path to the .db file on disk.")
.addText((text) =>
text
.setPlaceholder("/home/user/path/to/your.db")
2025-01-29 18:20:48 +00:00
.setValue(this.plugin.settings.dbFilePath)
.onChange(async (value) => {
this.plugin.settings.dbFilePath = value;
await this.plugin.saveSettings();
})
);
2025-01-29 17:35:31 +00:00
}
}