2025-01-29 19:35:48 +00:00
|
|
|
import {
|
|
|
|
|
Plugin,
|
|
|
|
|
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
|
|
|
|
2025-04-05 11:56:27 +00:00
|
|
|
import { DBService } from "./src/DBService";
|
|
|
|
|
import { SQLiteDBSettingTab } from "./src/settingTab";
|
2025-01-29 19:35:48 +00:00
|
|
|
import { inspectTableStructure, convertEntriesInNotes } from "./src/commands";
|
2025-04-05 14:50:18 +00:00
|
|
|
import { processSqlBlock, processSqlChartBlock, DateNavigatorRenderer } from "./src/codeblocks";
|
2025-02-04 16:08:15 +00:00
|
|
|
import { pickTableName } from "./src/helpers";
|
2025-03-08 11:13:55 +00:00
|
|
|
import { SQLiteDBSettings, DEFAULT_SETTINGS } from "./src/types";
|
2025-04-05 11:56:27 +00:00
|
|
|
|
2025-04-04 16:09:53 +00:00
|
|
|
import { injectDatePickerStyles } from "src/styles/datePickerInject";
|
2025-04-05 14:50:18 +00:00
|
|
|
import { injectDateNavigatorStyles, removeDateNavigatorStyles } from './src/styles/dateNavigationInject';
|
2025-04-05 11:56:27 +00:00
|
|
|
|
|
|
|
|
import { registerHabitCounter } from "./src/webcomponents/HabitCounter/registerHabitCounter";
|
2025-04-05 15:55:00 +00:00
|
|
|
import { registerBooleanSwitch } from "src/webcomponents/BooleanSwitch/registerBooleanSwitch";
|
2025-01-29 18:20:48 +00:00
|
|
|
|
2025-03-08 11:13:55 +00:00
|
|
|
export default class SQLiteDBPlugin extends Plugin {
|
|
|
|
|
settings: SQLiteDBSettings;
|
2025-01-29 19:35:48 +00:00
|
|
|
private dbService: DBService;
|
2025-04-06 15:06:42 +00:00
|
|
|
|
2025-01-29 17:35:31 +00:00
|
|
|
async onload() {
|
2025-04-04 16:09:53 +00:00
|
|
|
// 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();
|
2025-01-29 19:35:48 +00:00
|
|
|
|
2025-04-04 16:09:53 +00:00
|
|
|
injectDatePickerStyles();
|
2025-04-05 14:50:18 +00:00
|
|
|
injectDateNavigatorStyles();
|
2025-04-04 16:09:53 +00:00
|
|
|
|
2025-04-05 15:55:00 +00:00
|
|
|
//? Components
|
2025-04-05 11:56:27 +00:00
|
|
|
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-05 11:56:27 +00:00
|
|
|
});
|
|
|
|
|
|
2025-04-05 15:55:00 +00:00
|
|
|
//? Commands
|
2025-01-29 19:35:48 +00:00
|
|
|
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({
|
2025-01-29 19:35:48 +00:00
|
|
|
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 () => {
|
2025-01-29 19:35:48 +00:00
|
|
|
// 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-05 15:55:00 +00:00
|
|
|
//? Codeblocks
|
2025-01-29 19:58:04 +00:00
|
|
|
this.registerMarkdownCodeBlockProcessor(
|
2025-04-04 16:09:53 +00:00
|
|
|
"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);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2025-04-05 14:50:18 +00:00
|
|
|
this.registerMarkdownCodeBlockProcessor(
|
|
|
|
|
"date-header",
|
|
|
|
|
(source: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
|
|
|
|
|
ctx.addChild(new DateNavigatorRenderer(el, this.app));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
);
|
2025-04-04 16:09:53 +00:00
|
|
|
|
2025-03-08 11:13:55 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2025-01-29 19:35:48 +00:00
|
|
|
private async openDatabase(forceReload = true) {
|
|
|
|
|
const adapter = this.app.vault.adapter;
|
2025-03-01 12:14:32 +00:00
|
|
|
let basePath: string;
|
2025-04-06 15:28:15 +00:00
|
|
|
|
2025-03-01 12:14:32 +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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|