2025-01-09 20:12:39 +00:00
|
|
|
import { OmnibusRegistrator } from "@hypersphere/omnibus";
|
2025-01-15 17:48:33 +00:00
|
|
|
import { App, MarkdownRenderChild } from "obsidian";
|
2025-01-29 22:36:19 +00:00
|
|
|
import { SqlSealDatabase } from "../../database/database";
|
|
|
|
|
import { Sync } from "../../datamodel/sync";
|
|
|
|
|
import { RenderReturn } from "../../renderer/rendererRegistry";
|
|
|
|
|
import { transformQuery } from "../../sql/sqlTransformer";
|
|
|
|
|
import { registerObservers } from "../../utils/registerObservers";
|
|
|
|
|
import { displayError } from "../../utils/ui";
|
2025-01-30 13:16:52 +00:00
|
|
|
import SqlSealPlugin from "../../main";
|
2025-01-09 20:12:39 +00:00
|
|
|
|
|
|
|
|
export class InlineProcessor extends MarkdownRenderChild {
|
|
|
|
|
private registrator: OmnibusRegistrator;
|
|
|
|
|
private renderer: RenderReturn;
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
private el: HTMLElement,
|
|
|
|
|
private query: string,
|
|
|
|
|
private sourcePath: string,
|
|
|
|
|
private db: SqlSealDatabase,
|
2025-01-30 13:16:52 +00:00
|
|
|
private plugin: SqlSealPlugin,
|
2025-01-09 20:12:39 +00:00
|
|
|
private app: App,
|
|
|
|
|
private sync: Sync
|
|
|
|
|
) {
|
|
|
|
|
super(el);
|
|
|
|
|
this.registrator = this.sync.getRegistrator();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async onload() {
|
|
|
|
|
try {
|
|
|
|
|
await this.render();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
displayError(this.el, e.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onunload() {
|
|
|
|
|
this.registrator.offAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async render() {
|
|
|
|
|
try {
|
|
|
|
|
const registeredTablesForContext = await this.sync.getTablesMappingForContext(this.sourcePath);
|
|
|
|
|
const transformedQuery = transformQuery(this.query, registeredTablesForContext);
|
|
|
|
|
|
2025-01-30 13:16:52 +00:00
|
|
|
if (this.plugin.settings.enableDynamicUpdates) {
|
|
|
|
|
registerObservers({
|
|
|
|
|
bus: this.registrator,
|
|
|
|
|
tables: transformedQuery.mappedTables,
|
|
|
|
|
callback: () => this.render(),
|
|
|
|
|
fileName: this.sourcePath
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-01-09 20:12:39 +00:00
|
|
|
|
|
|
|
|
const file = this.app.vault.getFileByPath(this.sourcePath);
|
|
|
|
|
if (!file) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fileCache = this.app.metadataCache.getFileCache(file);
|
|
|
|
|
const { data, columns } = await this.db.select(
|
2025-01-15 17:48:33 +00:00
|
|
|
transformedQuery.sql,
|
2025-01-09 20:12:39 +00:00
|
|
|
fileCache?.frontmatter ?? {}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this.el.empty()
|
|
|
|
|
this.el.createSpan({ text: data[0][columns[0]] ?? '' })
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
displayError(this.el, e.toString())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|