-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
169 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,13 @@ | ||
## Obsidian Sample Plugin | ||
# hotkeysplus-obsidian | ||
|
||
This is a sample plugin for Obsidian (https://obsidian.md). | ||
This repository contains a few shortcuts that I use with [Obsidian](https://obsidian.md/). The defaults hotkeys are: | ||
|
||
This project uses Typescript to provide type checking and documentation. | ||
The repo contains the latest plugin API (obsidian.d.ts) in Typescript Definition format, which contains TSDoc comments describing what it does. | ||
| Hotkey | Action | | ||
| ------------------------------------------------- | ------------------------------------------ | | ||
| <kbd>Ctrl</kbd> + <kbd>M</kbd> | Toggle between checkmarks. | | ||
| <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>M</kbd> | Toggle between ordered and unordered lists.| | ||
| <kbd>Ctrl</kbd> + <kbd><</kbd> | Toggle blockquotes. | | ||
|
||
**Note:** The Obsidian API is still in early alpha and is subject to change at any time! | ||
## Demo | ||
|
||
### How to use | ||
|
||
- Clone this repo. | ||
- `npm i` or `yarn` to install dependencies | ||
- `npm run dev` to start compilation in watch mode. | ||
|
||
### How to install the plugin | ||
|
||
- Copy over `main.js`, `styles.css`, `manifest.json` to your vault `vault/.obsidian/plugins/plugin-id/`. | ||
|
||
### API Documentation | ||
|
||
See https://github.com/obsidianmd/obsidian-api | ||
![toggle-todos](https://user-images.githubusercontent.com/5426039/89807985-b1278f00-db39-11ea-9cc1-7fc26fab6fd8.gif). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,156 @@ | ||
import { App, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian'; | ||
|
||
export default class MyPlugin extends Plugin { | ||
onInit() { | ||
|
||
} | ||
|
||
onload() { | ||
console.log('loading plugin'); | ||
|
||
this.addRibbonIcon('dice', 'Sample Plugin', () => { | ||
new Notice('This is a notice!'); | ||
}); | ||
|
||
this.addStatusBarItem().setText('Status Bar Text'); | ||
|
||
this.addCommand({ | ||
id: 'open-sample-modal', | ||
name: 'Open Sample Modal', | ||
// callback: () => { | ||
// console.log('Simple Callback'); | ||
// }, | ||
checkCallback: (checking: boolean) => { | ||
let leaf = this.app.workspace.activeLeaf; | ||
if (leaf) { | ||
if (!checking) { | ||
new SampleModal(this.app).open(); | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
}); | ||
|
||
this.addSettingTab(new SampleSettingTab(this.app, this)); | ||
} | ||
|
||
onunload() { | ||
console.log('unloading plugin'); | ||
} | ||
} | ||
|
||
class SampleModal extends Modal { | ||
constructor(app: App) { | ||
super(app); | ||
} | ||
|
||
onOpen() { | ||
let {contentEl} = this; | ||
contentEl.setText('Woah!'); | ||
} | ||
|
||
onClose() { | ||
let {contentEl} = this; | ||
contentEl.empty(); | ||
} | ||
} | ||
|
||
class SampleSettingTab extends PluginSettingTab { | ||
display(): void { | ||
let {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('') | ||
.onChange((value) => { | ||
console.log('Secret: ' + value); | ||
})); | ||
|
||
} | ||
import { | ||
App, | ||
Modal, | ||
Notice, | ||
Plugin, | ||
PluginSettingTab, | ||
Setting, | ||
} from "obsidian"; | ||
|
||
export default class HotkeysPlus extends Plugin { | ||
onInit() {} | ||
|
||
onload() { | ||
console.log("Loading Hotkeys++ plugin"); | ||
|
||
this.addCommand({ | ||
id: "better-toggle-todo", | ||
name: "Toggle to-do lists", | ||
callback: () => this.toggleTodos(), | ||
hotkeys: [ | ||
{ | ||
modifiers: ["Mod"], | ||
key: "m", | ||
}, | ||
], | ||
}); | ||
|
||
this.addCommand({ | ||
id: "toggle-bullet-number", | ||
name: "Toggle line to bulleted or numbered lists", | ||
callback: () => this.toggleLists(), | ||
hotkeys: [ | ||
{ | ||
modifiers: ["Mod", "Shift"], | ||
key: "m", | ||
}, | ||
], | ||
}); | ||
|
||
this.addCommand({ | ||
id: "toggle-block-quote", | ||
name: "Toggle line to block quote", | ||
callback: () => this.toggleBlockQuote(), | ||
hotkeys: [ | ||
{ | ||
modifiers: ["Mod"], | ||
key: "<", | ||
}, | ||
], | ||
}); | ||
} | ||
|
||
onunload() { | ||
console.log("Unloading Hotkeys++ plugin"); | ||
} | ||
|
||
getSelectedText(editor: any) { | ||
if (editor.somethingSelected()) { | ||
// Toggle to-dos under the selection | ||
let cursorStart = editor.getCursor(true); | ||
let cursorEnd = editor.getCursor(false); | ||
let content = editor.getRange( | ||
{ line: cursorStart.line, ch: 0 }, | ||
{ line: cursorEnd.line, ch: editor.getLine(cursorEnd.line).length } | ||
); | ||
|
||
return { | ||
start: { line: cursorStart.line, ch: 0 }, | ||
end: { | ||
line: cursorEnd.line, | ||
ch: editor.getLine(cursorEnd.line).length, | ||
}, | ||
content: content, | ||
}; | ||
} else { | ||
// Toggle the todo in the line | ||
var lineNr = editor.getCursor().line; | ||
var contents = editor.getDoc().getLine(lineNr); | ||
let cursorStart = { | ||
line: lineNr, | ||
ch: 0, | ||
}; | ||
let cursorEnd = { | ||
line: lineNr, | ||
ch: contents.length, | ||
}; | ||
let content = editor.getRange(cursorStart, cursorEnd); | ||
return { start: cursorStart, end: cursorEnd, content: content }; | ||
} | ||
} | ||
|
||
toggleElement(re: RegExp, subst: any) { | ||
var activeLeaf: any = this.app.workspace.activeLeaf; | ||
var editor = activeLeaf.view.sourceMode.cmEditor; | ||
var selection = editor.somethingSelected(); | ||
var selectedText = this.getSelectedText(editor); | ||
|
||
var newString = selectedText.content.replace(re, subst); | ||
editor.replaceRange(newString, selectedText.start, selectedText.end); | ||
|
||
// Keep cursor in the same place | ||
if (selection) { | ||
editor.setSelection(selectedText.start, { | ||
line: selectedText.end.line, | ||
ch: editor.getLine(selectedText.end.line).length, | ||
}); | ||
} | ||
} | ||
|
||
toggleTodos() { | ||
var re = /-\s\[ \]\s|-\s\[x\]\s|\*\s|-\s|\d+\.\s|^/gim; | ||
return this.toggleElement(re, this.replaceTodoElement); | ||
} | ||
|
||
toggleLists() { | ||
var re = /-\s\[ \]\s|-\s\[x\]\s|\*\s|-\s|\d+\.\s|^/gim; | ||
return this.toggleElement(re, this.replaceListElement); | ||
} | ||
|
||
toggleBlockQuote() { | ||
var re = />\s|^/gim; | ||
return this.toggleElement(re, this.replaceBlockQuote); | ||
} | ||
|
||
replaceListElement(startText: string) { | ||
if (startText === "- ") { | ||
return "1. "; | ||
} else if (startText === "") { | ||
return "- "; | ||
} else if (startText === "1. ") { | ||
return ""; | ||
} else { | ||
return "- "; | ||
} | ||
} | ||
|
||
replaceBlockQuote(startText: string) { | ||
if (startText === "> ") { | ||
return ""; | ||
} else if (startText === "") { | ||
return "> "; | ||
} else { | ||
return "> "; | ||
} | ||
} | ||
|
||
replaceTodoElement(startText: string) { | ||
if (startText === "- [ ] ") { | ||
return "- [x] "; | ||
} else if (startText === "- [x] ") { | ||
return "- "; | ||
} else { | ||
return "- [ ] "; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters