Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
63 lines (58 sloc)
2.6 KB
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
| import { App, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian'; | |
| export default class MyPlugin extends Plugin { | |
| onload() { | |
| console.log('Loading the macOS Keyboard Navigation plugin.'); | |
| this.registerDomEvent(document, 'keydown', (keyboardPressEvent: KeyboardEvent) => { | |
| if (keyboardPressEvent.getModifierState("Alt") && !((keyboardPressEvent.getModifierState("Control") || (keyboardPressEvent.getModifierState("Meta"))))) { | |
| if (keyboardPressEvent.key == "ArrowUp") { | |
| let editor = this.app.workspace.activeLeaf.view.sourceMode.cmEditor; | |
| let cursorHead = editor.getCursor("head"); | |
| let cursorAnchor = editor.getCursor("anchor"); | |
| if (keyboardPressEvent.getModifierState("Shift")) { // select up | |
| let doc = editor.getDoc(); | |
| if (cursorHead.ch != 0) { | |
| doc.setSelection({line:cursorAnchor.line, ch:cursorAnchor.ch}, {line:cursorHead.line, ch:0}, {scroll: true}); | |
| } else { | |
| doc.setSelection({line:cursorAnchor.line, ch:cursorAnchor.ch}, {line:cursorHead.line - 1, ch:0}, {scroll: true}); | |
| } | |
| } else { | |
| // move up | |
| if (cursorHead.ch != 0) { | |
| editor.setCursor(cursorHead.line, 0); | |
| } else { | |
| editor.setCursor((cursorHead.line - 1), 0); | |
| } | |
| } | |
| } | |
| } | |
| }); | |
| this.registerDomEvent(document, 'keydown', (keyboardPressEvent: KeyboardEvent) => { | |
| if (keyboardPressEvent.getModifierState("Alt") && !((keyboardPressEvent.getModifierState("Control") || (keyboardPressEvent.getModifierState("Meta"))))) { | |
| if (keyboardPressEvent.key == "ArrowDown") { | |
| let editor = this.app.workspace.activeLeaf.view.sourceMode.cmEditor; | |
| let cursorHead = editor.getCursor("head"); | |
| let cursorAnchor = editor.getCursor("anchor"); | |
| let doc = editor.getDoc(); | |
| let lineLength = doc.getLine(cursorHead.line).length; | |
| if (keyboardPressEvent.getModifierState("Shift")) { // select down | |
| console.log("alt and shift are held"); | |
| if (cursorHead.ch != lineLength) { | |
| doc.setSelection({line:cursorAnchor.line, ch:cursorAnchor.ch}, {line:cursorHead.line + 1, ch:0}, {scroll: true}); | |
| } else { | |
| doc.setSelection({line:cursorAnchor.line, ch:cursorAnchor.ch}, {line:cursorHead.line + 1, ch:0}, {scroll:true}); | |
| } | |
| } else { // move down | |
| if (cursorHead.ch != lineLength) { | |
| editor.setCursor(cursorHead.line, lineLength); | |
| } else { | |
| editor.setCursor((cursorHead.line + 1), doc.getLine(cursorHead.line + 1).length); | |
| } | |
| } | |
| } | |
| } | |
| }); | |
| } | |
| onunload() { | |
| console.log('Unloading the macOS Keyboard Navigation plugin.'); | |
| } | |
| } |