2023-02-18 01:54:30 +00:00
|
|
|
import { GeneratorModal } from 'editor/GeneratorModal';
|
2023-04-04 02:24:00 +00:00
|
|
|
import { MarkdownView, Notice, Plugin, Editor } from 'obsidian';
|
2023-02-18 01:54:30 +00:00
|
|
|
import { InlineGeneratorSuggester } from "editor/InlineGenerator";
|
2023-02-18 02:55:35 +00:00
|
|
|
import { FantasyPluginSettings, possibleOptions } from 'settings/Datatypes';
|
2023-02-18 01:20:36 +00:00
|
|
|
import { DEFAULT_SETTINGS } from 'settings/DefaultSetting';
|
|
|
|
|
import { SettingTab } from 'settings/SettingsTab';
|
2023-01-28 13:37:25 +00:00
|
|
|
|
2023-02-18 02:55:35 +00:00
|
|
|
export default class FantasyPlugin extends Plugin {
|
|
|
|
|
settings: FantasyPluginSettings;
|
2023-04-04 02:24:00 +00:00
|
|
|
currentEditor: Editor | null = null;
|
2023-01-28 13:37:25 +00:00
|
|
|
|
2023-02-18 01:20:36 +00:00
|
|
|
//Function used to return the array of options for the suggester.
|
2023-02-17 23:45:22 +00:00
|
|
|
getOptionsForSuggest(): string[] {
|
|
|
|
|
return possibleOptions;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-03 10:55:51 +00:00
|
|
|
async onload() {
|
2023-01-28 13:37:25 +00:00
|
|
|
await this.loadSettings();
|
2023-04-04 02:24:00 +00:00
|
|
|
|
|
|
|
|
this.app.workspace.on('active-leaf-change', (leaf) => {
|
|
|
|
|
if (leaf) {
|
|
|
|
|
const view = leaf.view;
|
|
|
|
|
if (view instanceof MarkdownView) {
|
|
|
|
|
this.currentEditor = view.editor;
|
|
|
|
|
} else {
|
|
|
|
|
this.currentEditor = null;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.currentEditor = null;
|
|
|
|
|
}
|
2023-02-17 23:45:22 +00:00
|
|
|
});
|
|
|
|
|
|
2024-05-27 12:50:15 +00:00
|
|
|
//Command to open Modal dialog
|
|
|
|
|
this.addCommand({
|
|
|
|
|
id: 'open-fantasy-generator',
|
|
|
|
|
name: 'Open Fantasy Generator',
|
|
|
|
|
callback: () => {
|
|
|
|
|
new GeneratorModal(this.app, (result) => {
|
|
|
|
|
const copyContent = async () => {
|
|
|
|
|
//Try to see if any generators spit out an Error or if copying the string fails.
|
|
|
|
|
try {
|
|
|
|
|
if (result instanceof Error) {
|
|
|
|
|
new Notice(`${result}`);
|
|
|
|
|
} else {
|
|
|
|
|
await navigator.clipboard.writeText(result);
|
|
|
|
|
new Notice(`${result} was copied to the clipboard.`);
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Failed to copy: ', err);
|
|
|
|
|
new Notice("Failed to copy, Check error in console.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
copyContent();
|
|
|
|
|
|
|
|
|
|
}, this).open();
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2023-04-04 02:24:00 +00:00
|
|
|
//Register the InlineGeneratorSuggester to the Editor suggester.
|
|
|
|
|
this.registerEditorSuggest(new InlineGeneratorSuggester(this.getOptionsForSuggest, this));
|
|
|
|
|
|
2023-02-18 01:20:36 +00:00
|
|
|
// This creates an icon in the left ribbon to access the modal for the Fantasy Content Generator.
|
2023-04-04 02:24:00 +00:00
|
|
|
this.addRibbonIcon('book', 'Fantasy Generators', (evt: MouseEvent) => {
|
2023-01-28 13:37:25 +00:00
|
|
|
// Called when the user clicks the icon.
|
2023-02-03 10:55:51 +00:00
|
|
|
new GeneratorModal(this.app, (result) => {
|
|
|
|
|
const copyContent = async () => {
|
2023-02-18 01:20:36 +00:00
|
|
|
//Try to see if any generators spit out an Error or if copying the string fails.
|
2023-02-03 10:55:51 +00:00
|
|
|
try {
|
2023-02-09 12:05:51 +00:00
|
|
|
if (result instanceof Error) {
|
|
|
|
|
new Notice(`${result}`);
|
|
|
|
|
} else {
|
|
|
|
|
await navigator.clipboard.writeText(result);
|
|
|
|
|
new Notice(`${result} was copied to the clipboard.`);
|
|
|
|
|
}
|
2023-02-03 10:55:51 +00:00
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Failed to copy: ', err);
|
|
|
|
|
new Notice("Failed to copy, Check error in console.");
|
2023-01-28 13:37:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-02-03 10:55:51 +00:00
|
|
|
|
|
|
|
|
copyContent();
|
|
|
|
|
|
|
|
|
|
}, this).open();
|
2023-01-28 13:37:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// This adds a settings tab so the user can configure various aspects of the plugin
|
2023-02-18 01:20:36 +00:00
|
|
|
this.addSettingTab(new SettingTab(this.app, this));
|
2023-01-28 13:37:25 +00:00
|
|
|
|
2023-02-18 01:20:36 +00:00
|
|
|
console.log("loaded Fantasy Content Generator");
|
2023-01-28 13:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onunload() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async loadSettings() {
|
|
|
|
|
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async saveSettings() {
|
|
|
|
|
await this.saveData(this.settings);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|