mirror of
https://github.com/esm7/obsidian-vimrc-support.git
synced 2026-07-22 05:00:25 +00:00
Merge pull request #25 from SalmanAlSaigal/master
Add settings and option to change file name
This commit is contained in:
commit
06d4016524
1 changed files with 54 additions and 3 deletions
57
main.ts
57
main.ts
|
|
@ -1,16 +1,29 @@
|
|||
import { App, Plugin, TFile, MarkdownView } from 'obsidian';
|
||||
import { App, Plugin, TFile, MarkdownView, PluginSettingTab, Setting } from 'obsidian';
|
||||
declare const CodeMirror: any;
|
||||
|
||||
interface Settings {
|
||||
VIMRC_NAME: string;
|
||||
}
|
||||
|
||||
const DEFAULT_SETTINGS: Settings = {
|
||||
VIMRC_NAME: ".obsidian.vimrc"
|
||||
}
|
||||
|
||||
export default class VimrcPlugin extends Plugin {
|
||||
settings: Settings;
|
||||
|
||||
private lastYankBuffer = new Array<string>(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';
|
||||
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') });
|
||||
|
|
@ -27,6 +40,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 +132,32 @@ 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'});
|
||||
|
||||
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();
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue