From 8f8b1636151e8ce1ca03c8de448efdab0352e265 Mon Sep 17 00:00:00 2001 From: Salman AlSaigal Date: Wed, 14 Apr 2021 06:08:04 +0300 Subject: [PATCH 1/2] Add settings tab --- main.ts | 40 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/main.ts b/main.ts index bbc9224..8e4390c 100644 --- a/main.ts +++ b/main.ts @@ -1,14 +1,25 @@ -import { App, Plugin, TFile, MarkdownView } from 'obsidian'; +import { App, Plugin, TFile, MarkdownView, PluginSettingTab, Setting } from 'obsidian'; declare const CodeMirror: any; +interface Settings { +} + +const DEFAULT_SETTINGS: Settings = { +} + export default class VimrcPlugin extends Plugin { + settings: Settings; + private lastYankBuffer = new Array(0); private lastSystemClipboard = ""; private yankToSystemClipboard: boolean = false; - onload() { + async onload() { console.log('loading Vimrc plugin'); + await this.loadSettings(); + this.addSettingTab(new SettingsTab(this.app, this)) + this.registerEvent(this.app.workspace.on('file-open', (file: TFile) => { const VIMRC_FILE_NAME = '.obsidian.vimrc'; this.app.vault.adapter.read(VIMRC_FILE_NAME). @@ -27,6 +38,15 @@ export default class VimrcPlugin extends Plugin { }) } + async loadSettings(){ + const data = await this.loadData(); + this.settings = Object.assign({}, DEFAULT_SETTINGS, data); + } + + async saveSettings(){ + await this.saveData(this.settings); + } + onunload() { console.log('unloading Vimrc plugin (but Vim commands that were already loaded will still work)'); } @@ -110,3 +130,19 @@ export default class VimrcPlugin extends Plugin { } } +class SettingsTab extends PluginSettingTab { + plugin: VimrcPlugin; + + constructor(app: App, plugin: VimrcPlugin) { + super(app, plugin); + this.plugin = plugin; + } + + display(): void { + let {containerEl} = this; + + containerEl.empty(); + + containerEl.createEl('h2', {text: 'Obsidian Vimrc Support Settings'}); + } +} \ No newline at end of file From 4116fe9fbe275926915d134169cc9feb8fdc4620 Mon Sep 17 00:00:00 2001 From: Salman AlSaigal Date: Wed, 14 Apr 2021 06:53:09 +0300 Subject: [PATCH 2/2] Add setting to change file name closes #15 File can only be inside the vault for some reason. --- main.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/main.ts b/main.ts index 8e4390c..cbd2590 100644 --- a/main.ts +++ b/main.ts @@ -2,9 +2,11 @@ import { App, Plugin, TFile, MarkdownView, PluginSettingTab, Setting } from 'obs declare const CodeMirror: any; interface Settings { + VIMRC_NAME: string; } const DEFAULT_SETTINGS: Settings = { + VIMRC_NAME: ".obsidian.vimrc" } export default class VimrcPlugin extends Plugin { @@ -21,7 +23,7 @@ export default class VimrcPlugin extends Plugin { this.addSettingTab(new SettingsTab(this.app, this)) this.registerEvent(this.app.workspace.on('file-open', (file: TFile) => { - const VIMRC_FILE_NAME = '.obsidian.vimrc'; + const VIMRC_FILE_NAME = this.settings.VIMRC_NAME; this.app.vault.adapter.read(VIMRC_FILE_NAME). then((lines) => this.readVimInit(lines)). catch(error => { console.log('Error loading vimrc file', VIMRC_FILE_NAME, 'from the vault root') }); @@ -144,5 +146,18 @@ class SettingsTab extends PluginSettingTab { containerEl.empty(); containerEl.createEl('h2', {text: 'Obsidian Vimrc Support Settings'}); + + new Setting(containerEl) + .setName('Vimrc file name') + .setDesc('Relative to vault directory (requires restart)') + .addText((text) => { + text.setPlaceholder(DEFAULT_SETTINGS.VIMRC_NAME); + if(this.plugin.settings.VIMRC_NAME !== DEFAULT_SETTINGS.VIMRC_NAME) + text.setValue(this.plugin.settings.VIMRC_NAME) + text.onChange(value => { + this.plugin.settings.VIMRC_NAME = value || DEFAULT_SETTINGS.VIMRC_NAME; + this.plugin.saveSettings(); + }) + }); } } \ No newline at end of file