diff --git a/.changeset/chilly-dodos-attend.md b/.changeset/chilly-dodos-attend.md index cc9ffe3..db964a4 100644 --- a/.changeset/chilly-dodos-attend.md +++ b/.changeset/chilly-dodos-attend.md @@ -1,5 +1,5 @@ --- -"sqlseal": minor +"sqlseal": major --- adding support for .sql and .sqlseal files diff --git a/src/modules/explorer/InitFactory.ts b/src/modules/explorer/InitFactory.ts index 6111eec..bf5297e 100644 --- a/src/modules/explorer/InitFactory.ts +++ b/src/modules/explorer/InitFactory.ts @@ -64,8 +64,7 @@ export class InitFactory { return new SQLSealFileView(leaf, dbManager, viewPluginGenerator, rendererRegistry, cellParser, settings, sync, db) }) - // Register extensions for SQLSeal file view - plugin.registerExtensions(['sql', 'sqlseal', 'sqlite', 'db'], SQLSEAL_FILE_VIEW) + // Extensions for SQLSeal file view are registered by SettingsSQLControls plugin.addCommand({ id: 'sqlseal-command-explorer', diff --git a/src/modules/settings/SQLSealSettingsTab.ts b/src/modules/settings/SQLSealSettingsTab.ts index 432f903..daec150 100644 --- a/src/modules/settings/SQLSealSettingsTab.ts +++ b/src/modules/settings/SQLSealSettingsTab.ts @@ -8,6 +8,7 @@ export interface SQLSealSettings { enableViewer: boolean; enableEditing: boolean; enableJSONViewer: boolean; + enableSQLViewer: boolean; enableDynamicUpdates: boolean; enableSyntaxHighlighting: boolean; defaultView: 'grid' | 'markdown' | 'html'; @@ -18,6 +19,7 @@ export const DEFAULT_SETTINGS: SQLSealSettings = { enableViewer: true, enableEditing: true, enableJSONViewer: true, + enableSQLViewer: true, enableDynamicUpdates: true, enableSyntaxHighlighting: true, defaultView: 'grid', diff --git a/src/modules/settings/init.ts b/src/modules/settings/init.ts index 9f034e7..fb02f9f 100644 --- a/src/modules/settings/init.ts +++ b/src/modules/settings/init.ts @@ -5,6 +5,7 @@ import { SQLSealSettingsTab } from "./SQLSealSettingsTab"; import { Settings } from "./Settings"; import { SettingsCSVControls } from "./settingsTabSection/SettingsCSVControls"; import { SettingsJsonControls } from "./settingsTabSection/SettingsJsonControls"; +import { SettingsSQLControls } from "./settingsTabSection/SettingsSQLControls"; import { ViewPluginGeneratorType } from "../syntaxHighlight/viewPluginGenerator"; @(makeInjector()(["plugin", "settingsTab", "app", "settings", "viewPluginGenerator"])) @@ -18,8 +19,9 @@ export class SettingsInit { ) { const csvControl = new SettingsCSVControls(settings, app, plugin, viewPluginGenerator); const jsonControl = new SettingsJsonControls(settings, app, plugin, viewPluginGenerator); + const sqlControl = new SettingsSQLControls(settings, app, plugin); - const controls = [csvControl, jsonControl]; + const controls = [csvControl, jsonControl, sqlControl]; settingsTab.registerControls(...controls); diff --git a/src/modules/settings/settingsTabSection/SettingsSQLControls.ts b/src/modules/settings/settingsTabSection/SettingsSQLControls.ts new file mode 100644 index 0000000..bb1043e --- /dev/null +++ b/src/modules/settings/settingsTabSection/SettingsSQLControls.ts @@ -0,0 +1,70 @@ +import { App, Plugin, Setting } from "obsidian"; +import { Settings } from "../Settings"; +import { + checkTypeViewAvaiability, +} from "../utils/viewInspector"; +import { SettingsControls } from "./SettingsControls"; +import { SQLSEAL_FILE_VIEW } from "../../explorer/SQLSealFileView"; + +export class SettingsSQLControls extends SettingsControls { + private registeredView: string | null = null; + + constructor(settings: Settings, app: App, plugin: Plugin) { + super(settings, app, plugin) + } + + register() { + if (this.settings.get("enableSQLViewer")) { + const view = checkTypeViewAvaiability(this.app, 'sql'); + if (view && view !== SQLSEAL_FILE_VIEW) { + this.registeredView = view; + return; + } + + // Register extensions when enabling + this.plugin.registerExtensions(['sql', 'sqlseal', 'sqlite', 'db'], SQLSEAL_FILE_VIEW); + } + } + + unregister() { + this.app.workspace.detachLeavesOfType(SQLSEAL_FILE_VIEW); + (this.app as any).viewRegistry.unregisterExtensions([ + 'sql', 'sqlseal', 'sqlite', 'db' + ]); + } + + display(el: HTMLDivElement) { + el.empty(); + el.createEl("h2", { text: "SQL Explorer" }); + + const view = checkTypeViewAvaiability(this.app, "sql"); + + if (view && view !== SQLSEAL_FILE_VIEW) { + el.createDiv({ + text: "SQL files are already handled by different plugin. To enable SQLSeal SQL viewer, disable other plugin that handles it", + cls: "sqlseal-settings-warn", + }); + return; + } + + new Setting(el) + .setName("Enable SQL Explorer") + .setDesc( + "Enables SQL, SQLSeal, SQLite and DB files in your vault to preview then and execture queries.", + ) + .addToggle((toggle) => + toggle + .setValue(this.settings.get("enableSQLViewer")) + .onChange(async (value) => { + this.settings.set("enableSQLViewer", !!value); + if (!!value) { + // Enabled + this.register(); + } else { + // Disabled + this.unregister(); + } + }), + ); + } +} \ No newline at end of file