stfrigerio_sqliteDB/main.ts

163 lines
4.9 KiB
TypeScript
Raw Normal View History

import {
Plugin,
FileSystemAdapter,
Editor,
MarkdownView,
2025-04-13 12:31:35 +00:00
MarkdownPostProcessorContext,
2025-04-13 12:57:11 +00:00
Notice,
2025-01-29 18:20:48 +00:00
} from "obsidian";
2025-01-29 19:58:04 +00:00
import { DBService } from "./src/DBService";
import { SQLiteDBSettingTab } from "./src/settingTab";
2025-04-13 12:57:11 +00:00
import { inspectTableStructure, convertEntriesInNotes, syncDBToJournals, syncJournalsToDB } from "./src/commands";
2025-04-05 14:50:18 +00:00
import { processSqlBlock, processSqlChartBlock, DateNavigatorRenderer } from "./src/codeblocks";
2025-04-13 12:57:11 +00:00
import { pickTableName, replacePlaceholders } from "./src/helpers";
import { injectDatePickerStyles, injectDateNavigatorStyles, injectTimePickerStyles, removeDateNavigatorStyles } from "src/styles";
import {
registerHabitCounter,
registerBooleanSwitch,
registerTextInput,
registerTimestampUpdaterButton,
registerSqlChartRenderer,
registerMoodNoteButtonProcessor
} from "./src/webcomponents";
import { SQLiteDBSettings, DEFAULT_SETTINGS } from "./src/types";
import { pluginState } from "src/pluginState";
export default class SQLiteDBPlugin extends Plugin {
settings: SQLiteDBSettings;
private dbService: DBService;
2025-04-06 15:06:42 +00:00
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();
pluginState.initialize(this.app);
injectDatePickerStyles();
2025-04-05 14:50:18 +00:00
injectDateNavigatorStyles();
2025-04-13 12:57:11 +00:00
injectTimePickerStyles();
2025-04-05 15:55:00 +00:00
//? Components
this.registerMarkdownPostProcessor((el, ctx) => {
2025-04-06 15:28:15 +00:00
registerHabitCounter(el, this.dbService);
2025-04-05 15:55:00 +00:00
registerBooleanSwitch(el, this.dbService);
2025-04-07 16:11:01 +00:00
registerTextInput(el, this.app, this.dbService);
});
2025-04-13 10:41:56 +00:00
registerSqlChartRenderer(this, this.dbService);
2025-04-10 15:37:14 +00:00
registerTimestampUpdaterButton(this);
2025-04-13 12:31:35 +00:00
registerMoodNoteButtonProcessor(this, this.dbService);
2025-04-10 15:37:14 +00:00
//? Codeblocks
this.registerMarkdownCodeBlockProcessor(
"sql",
async (source: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
const processedSource = replacePlaceholders(source);
await processSqlBlock(this.dbService, processedSource, el);
}
);
this.registerMarkdownCodeBlockProcessor(
"sql-chart",
async (source: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
const processedSource = replacePlaceholders(source);
await processSqlChartBlock(this.dbService, processedSource, el);
}
);
this.registerMarkdownCodeBlockProcessor(
"date-header",
(source: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
const processedSource = replacePlaceholders(source);
ctx.addChild(new DateNavigatorRenderer(el, this.app, processedSource));
}
);
2025-04-05 15:55:00 +00:00
//? Commands
this.addCommand({
id: "inspect-table-structure",
name: "Inspect table structure",
editorCallback: async (editor: Editor, view: MarkdownView) => {
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-04-06 15:28:15 +00:00
callback: async () => {
// 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
2025-04-13 12:57:11 +00:00
this.addCommand({
id: 'sync-journal-notes-to-db',
name: 'Sync Journal Notes -> Database',
callback: async () => {
if (!this.settings.journalFolderPath || !this.settings.journalTableName) {
new Notice("Please configure Journal folder path and table name in settings.");
return;
}
new Notice("Starting sync: Notes -> DB...");
await syncJournalsToDB(this.dbService, this.settings.journalFolderPath, this.settings.journalTableName, this.app);
}
});
this.addCommand({
id: 'sync-journal-db-to-notes',
name: 'Sync Database -> Journal Notes',
callback: async () => {
if (!this.settings.journalFolderPath || !this.settings.journalTableName) {
new Notice("Please configure Journal folder path and table name in settings.");
return;
}
new Notice("Starting sync: DB -> Notes...");
await syncDBToJournals(this.dbService, this.settings.journalTableName, this.settings.journalFolderPath, this.app);
}
});
this.addSettingTab(new SQLiteDBSettingTab(this.app, this));
2025-01-29 17:35:31 +00:00
}
onunload() {
2025-04-05 14:50:18 +00:00
removeDateNavigatorStyles();
2025-01-29 18:20:48 +00:00
}
private async openDatabase(forceReload = true) {
const adapter = this.app.vault.adapter;
let basePath: string;
2025-04-06 15:28:15 +00:00
if (adapter instanceof FileSystemAdapter) {
basePath = adapter.getBasePath();
} else {
basePath = (adapter as any).getFullPath("");
2025-01-29 18:20:48 +00:00
}
2025-04-06 15:28:15 +00:00
if (this.settings.mode === "local") {
await this.dbService.ensureDBLoaded(this.settings, basePath, forceReload);
} else {
await this.dbService.ensureDBLoaded(this.settings, basePath, false);
}
}
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);
}
}