diff --git a/.gitignore b/.gitignore index e7b7242..99ea931 100644 --- a/.gitignore +++ b/.gitignore @@ -7,5 +7,6 @@ node_modules package-lock.json # build +data.json main.js -*.js.map \ No newline at end of file +*.js.map diff --git a/main.ts b/main.ts index 1b8b349..eed0b88 100644 --- a/main.ts +++ b/main.ts @@ -1,25 +1,26 @@ -import { loadMathJax, Plugin } from 'obsidian'; +import { loadMathJax, App, Plugin, PluginManifest, PluginSettingTab, Setting } from 'obsidian'; -const DEFAULT_PREAMBLE_PATH = "preamble.sty"; +interface PluginSettings { + preamblePath: string; +} + +const DEFAULT_SETTINGS: PluginSettings = { + preamblePath: "preamble.sty", +}; export default class JaxPlugin extends Plugin { - async read_preamble () { - return await this.app.vault.adapter.read(DEFAULT_PREAMBLE_PATH); + app: App; + settings: PluginSettings; + + constructor(app: App, manifest: PluginManifest) { + super(app, manifest); + this.app = app; + this.settings = DEFAULT_SETTINGS; } - async onload() { - // Load MathJax so that we can modify it - // Otherwise, it would not be loaded when this plugin is loaded - await loadMathJax(); + async loadPreamble() { + const preamble = await this.app.vault.adapter.read(this.settings.preamblePath); - if (!MathJax) { - console.warn("MathJax was not defined despite loading it."); - return; - } - - // Read the preamble out from the file - let preamble = await this.read_preamble(); - if (MathJax.tex2chtml == undefined) { MathJax.startup.ready = () => { MathJax.startup.defaultReady(); @@ -28,12 +29,61 @@ export default class JaxPlugin extends Plugin { } else { MathJax.tex2chtml(preamble); } + } + + async loadSettings() { + this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); + } + async saveSettings() { + await this.saveData(this.settings); + } + + async onload() { + await this.loadSettings(); + this.addSettingTab(new JaxPluginSettingTab(this.app, this)); + + // Load MathJax so that we can modify it + // Otherwise, it would not be loaded when this plugin is loaded + await loadMathJax(); + + if (!MathJax) { + console.warn("MathJax was not defined despite loading it."); + return; + } + + await this.loadPreamble(); // TODO: Refresh view? - } + } - onunload() { + onunload() { // TODO: Is it possible to remove our definitions? - console.log('Unloading Extended MathJax'); - } -} \ No newline at end of file + console.log('Unloading Extended MathJax'); + } +} + +class JaxPluginSettingTab extends PluginSettingTab { + plugin: JaxPlugin; + + constructor(app: App, plugin: JaxPlugin) { + super(app, plugin); + this.plugin = plugin; + } + + display(): void { + const { containerEl } = this; + containerEl.empty(); + + new Setting(containerEl) + .setName('Preamble path') + .setDesc('Path to global preamble. (Requires reload!)') + .addText((text) => + text + .setValue(this.plugin.settings.preamblePath) + .onChange(async (value) => { + this.plugin.settings.preamblePath = value; + await this.plugin.saveSettings(); + }) + ); + } +}