Skip to content

Commit

Permalink
Add shortcut to insert line above/below (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 authored Jan 16, 2021
1 parent 9498f7a commit c4d2327
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This repository contains a few shortcuts that I use with [Obsidian](https://obsi
| <kbd>Ctrl</kbd> + <kbd><</kbd> | Toggle blockquotes. | |
| <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>1</kbd> | Toggle embeds for internal links. | [lynchjames](https://github.com/lynchjames) |
| Blank by default | Duplicate current line or selected lines | [NomarCub](https://github.com/NomarCub) |
| Blank by default | Insert line below/above current line | [Vinzent](https://github.com/Vinzent03) |

## Demo

Expand Down
54 changes: 44 additions & 10 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import {
App,
Modal,
Notice,
Plugin,
PluginSettingTab,
Setting,
MarkdownView,
Plugin
} from "obsidian";

export default class HotkeysPlus extends Plugin {
onInit() {}
onInit() { }

onload() {
console.log("Loading Hotkeys++ plugin");
Expand Down Expand Up @@ -66,6 +62,44 @@ export default class HotkeysPlus extends Plugin {
name: "Duplicate the current line or selected lines",
callback: () => this.duplicateLines(),
});

this.addCommand({
id: 'insert-line-above',
name: 'Insert line above current line',
callback: () => this.insertLine("above"),
});
this.addCommand({
id: 'insert-line-below',
name: 'Insert line below current line',
callback: () => this.insertLine("below"),
});
}
insertLine(mode: "above" | "below") {
const view = this.app.workspace.activeLeaf.view as MarkdownView;
const editor = view.sourceMode.cmEditor as CodeMirror.Editor;
const lineNumber = editor.getCursor().line;
const currentLineText = editor.getLine(lineNumber);
let newLineText = "";
if (currentLineText.trim().startsWith("- ")) {
newLineText = currentLineText.substring(0, currentLineText.indexOf("- ") + 2);
}
for (let i = 1; i < 30; i++) {
if (currentLineText.trim().startsWith(i.toString() + ". ")) {
let correction: number;
if (mode == "above")
correction = -1;
else
correction = 1;
newLineText = currentLineText.substring(0, currentLineText.indexOf(i.toString() + ". ")) + (i + correction).toString() + ". ";
}
}
if (mode == "above") {
editor.replaceRange(newLineText + "\n", { line: lineNumber, ch: 0 });
editor.setCursor({ line: lineNumber, ch: newLineText.length });
} else {
editor.replaceRange("\n" + newLineText, { line: lineNumber, ch: currentLineText.length });
editor.setCursor({ line: lineNumber + 1, ch: newLineText.length });
}
}

duplicateLines() {
Expand Down Expand Up @@ -150,7 +184,7 @@ export default class HotkeysPlus extends Plugin {

toggleEmbed() {
var re = /\S*\[\[/gim;
return this.toggleElement(re, this.replaceEmbed);
return this.toggleElement(re, this.replaceEmbed);
}

replaceListElement(startText: string) {
Expand Down Expand Up @@ -180,10 +214,10 @@ export default class HotkeysPlus extends Plugin {
return "[[";
}
else if (startText === "[[") {
return "![[";
return "![[";
}
else {
return "";
return "";
}
}

Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"id": "hotkeysplus-obsidian",
"name": "Hotkeys++",
"description": "Additional hotkeys to do common things in Obsidian",
"version": "0.2.3",
"version": "0.2.4",
"author": "Argentina Ortega Sainz",
"authorUrl": "",
"isDesktopOnly": false
}
}

0 comments on commit c4d2327

Please sign in to comment.