Skip to content

Instantly share code, notes, and snippets.

@danihodovic
Created June 28, 2023 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danihodovic/7a8c55cc53c3d4c4790a08959a96998c to your computer and use it in GitHub Desktop.
Save danihodovic/7a8c55cc53c3d4c4790a08959a96998c to your computer and use it in GitHub Desktop.
Obsidian command palette remap Ctrl+P / Ctrl+J
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
// Remember to rename these classes and interfaces!
interface MyPluginSettings {
mySetting: string;
}
const DEFAULT_SETTINGS: MyPluginSettings = {
mySetting: 'default'
}
function isKeyRelevant(document, event) {
return document.activeElement && document.activeElement.hasClass('prompt-input') && event.ctrlKey
}
export default class MyPlugin extends Plugin {
settings: MyPluginSettings;
async onload() {
await this.loadSettings();
document.addEventListener('keydown',(e) =>{
if (isKeyRelevant(document, e) && e.code == "KeyJ"){
e.preventDefault();
document.dispatchEvent(new KeyboardEvent("keydown",{"key":"ArrowDown","code":"ArrowDown"}))
}
});
document.addEventListener('keydown',(e) =>{
if (isKeyRelevant(document, e) && e.code == "KeyK"){
e.preventDefault();
document.dispatchEvent(new KeyboardEvent("keydown",{"key":"ArrowUp","code":"ArrowUp"}))
}
});
}
onunload() {
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class SampleModal extends Modal {
constructor(app: App) {
super(app);
}
onOpen() {
const {contentEl} = this;
contentEl.setText('Woah!');
}
onClose() {
const {contentEl} = this;
contentEl.empty();
}
}
class SampleSettingTab extends PluginSettingTab {
plugin: MyPlugin;
constructor(app: App, plugin: MyPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'Settings for my awesome plugin.'});
new Setting(containerEl)
.setName('Setting #1')
.setDesc('It\'s a secret')
.addText(text => text
.setPlaceholder('Enter your secret')
.setValue(this.plugin.settings.mySetting)
.onChange(async (value) => {
console.log('Secret: ' + value);
this.plugin.settings.mySetting = value;
await this.plugin.saveSettings();
}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment