Files
nethys-obsidian-plugin/main.ts
2026-05-15 21:03:48 +02:00

77 lines
1.7 KiB
TypeScript

import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
import * as plugin from "./pkg/obsidian_rust_plugin.js";
import * as wasmbin from './pkg/obsidian_rust_plugin_bg.wasm';
interface NethysSettings {
// mySetting: string;
}
const DEFAULT_SETTINGS: NethysSettings = {
// mySetting: 'default'
}
export default class Nethys extends Plugin {
settings: NethysSettings;
async onload() {
await this.loadSettings();
// load the rust module
await plugin.default(Promise.resolve(wasmbin.default));
// This adds an editor command that can perform some operation on the current editor instance
this.addCommand({
id: 'nethys',
name: 'Embed Statblock',
editorCallback: (editor: Editor, view: MarkdownView) => {
new NethysSearch(this.app, (name) => {
const creature = plugin.find_creature(name);
if (creature) {
editor.replaceSelection(creature);
console.log(creature);
}
}).open();
}
});
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
export class NethysSearch extends Modal {
constructor(app: App, onSubmit: (result: string) => void) {
super(app);
this.setTitle('Enter creature name');
let name = '';
new Setting(this.contentEl)
.setName('Name')
.addText((text) =>
text.onChange((value) => {
name = value;
}));
new Setting(this.contentEl)
.addButton((btn) =>
btn
.setButtonText('Submit')
.setCta()
.onClick(() => {
this.close();
onSubmit(name);
}));
}
}