Skip to content

Commit

Permalink
Migration from volcano
Browse files Browse the repository at this point in the history
  • Loading branch information
argenos committed Oct 26, 2020
1 parent a577988 commit 2df71cd
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 99 deletions.
27 changes: 9 additions & 18 deletions README.md
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).
231 changes: 155 additions & 76 deletions main.ts
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 "- [ ] ";
}
}
}
6 changes: 3 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-sample-plugin",
"name": "Sample Plugin",
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
"id": "hotkeysplus-obsidian",
"name": "Hotkeys++",
"description": "Additional hotkeys to do common things in Obsidian",
"isDesktopOnly": false,
"js": "main.js",
"css": "styles.css"
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "obsidian-sample-plugin",
"name": "hotkeysplus-obsidian",
"version": "0.9.7",
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
"description": "Hotkeys to do common stuff in Obsidian",
"main": "main.js",
"scripts": {
"dev": "rollup --config rollup.config.js -w",
Expand Down

0 comments on commit 2df71cd

Please sign in to comment.