Skip to content

Commit

Permalink
Merge pull request #1 from ozntel/settingstab
Browse files Browse the repository at this point in the history
Settings Tab with Ribbon Icon
  • Loading branch information
NomarCub authored Jul 17, 2021
2 parents 42df1f9 + e1d4d0f commit 3132378
Showing 1 changed file with 64 additions and 4 deletions.
68 changes: 64 additions & 4 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { addIcon, FileSystemAdapter, Plugin } from 'obsidian';
import {
addIcon, FileSystemAdapter, Plugin,
PluginSettingTab, App, Setting
} from 'obsidian';

let svg = `
<path
Expand All @@ -9,11 +12,14 @@ let svg = `
addIcon('vscode-logo', svg);
export default class OpenVSCode extends Plugin {
ribbonIcon: HTMLElement;
settings: OpenVSCodeSettings;

async onload() {
this.ribbonIcon = this.addRibbonIcon('vscode-logo', 'VSCode', () => {
this.openVSCode();
});

this.addSettingTab(new OpenVSCodeSettingsTab(this.app, this));
await this.loadSettings();

this.refreshIconRibbon();

this.addCommand({
id: 'open-vscode',
Expand All @@ -31,4 +37,58 @@ export default class OpenVSCode extends Plugin {
let url = "vscode://file/" + path;
window.open(url, "_blank");
}

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}

async saveSettings() {
await this.saveData(this.settings);
}

refreshIconRibbon = () => {
this.ribbonIcon?.remove();
if (this.settings.ribbonIcon) {
this.ribbonIcon = this.addRibbonIcon('vscode-logo', 'VSCode', () => {
this.openVSCode();
});
}
}
}

interface OpenVSCodeSettings {
ribbonIcon: boolean,
}

const DEFAULT_SETTINGS: OpenVSCodeSettings = {
ribbonIcon: true,
}

class OpenVSCodeSettingsTab extends PluginSettingTab {

plugin: OpenVSCode;

constructor(app: App, plugin: OpenVSCode) {
super(app, plugin);
this.plugin = plugin;
}

display(): void {
let { containerEl } = this;
containerEl.empty();
containerEl.createEl("h2", { text: "Settings" });

new Setting(containerEl)
.setName('Ribbon Icon')
.setDesc('Turn on if you want to have a Ribbon Icon.')
.addToggle((toggle) => toggle
.setValue(this.plugin.settings.ribbonIcon)
.onChange((value) => {
this.plugin.settings.ribbonIcon = value;
this.plugin.saveSettings();
this.plugin.refreshIconRibbon();
})
)
}

}

0 comments on commit 3132378

Please sign in to comment.