From 9f9898abac1c7bbd951a192181c05767e0558a02 Mon Sep 17 00:00:00 2001 From: Erez Shermer Date: Sun, 10 Apr 2022 20:37:02 +0300 Subject: [PATCH] Fixed surround with backward selection --- README.md | 6 +++++- main.ts | 4 ++++ manifest.json | 2 +- package.json | 2 +- 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 570eec7..3d947ac 100644 --- a/README.md +++ b/README.md @@ -218,7 +218,7 @@ If you understand the risks and choose to use this feature, turn on "Support JS There are two ways to define JS-based commands. **The `jscommand` Ex command** defines a JS function that has an `editor: Editor` and `view: MarkdownView` arguments (see the [Obsidian API](https://github.com/obsidianmd/obsidian-api/blob/master/obsidian.d.ts) if you're not sure what these are). -You define only the syntax of the function, in a single line wrapped by curly braces, e.g.: +You define only the body of the function, in a single line wrapped by curly braces, e.g.: ``` jscommand { console.log(editor.getCursor()); } @@ -250,6 +250,10 @@ See [here](JsSnippets.md) for the full example, and please contribute your own! ## Changelog +### 0.6.1 + +- Fixed backward selection error in `surround` (https://github.com/esm7/obsidian-vimrc-support/issues/91) + ### 0.6.0 - The `surround` and `pasteinto` commands now work with the new (CM6-based) editor. diff --git a/main.ts b/main.ts index 2bb84d8..3a0a731 100644 --- a/main.ts +++ b/main.ts @@ -400,6 +400,10 @@ export default class VimrcPlugin extends Plugin { head: {line: chosenSelection.head.line, ch: wordEnd} }; } + // If the selection is reverse, switch the variables + if (chosenSelection.anchor.line > chosenSelection.head.line || + (chosenSelection.anchor.line == chosenSelection.head.line && chosenSelection.anchor.ch > chosenSelection.head.ch)) + [chosenSelection.anchor, chosenSelection.head] = [chosenSelection.head, chosenSelection.anchor]; let currText = editor.getRange(chosenSelection.anchor, chosenSelection.head); editor.replaceRange(beginning + currText + ending, chosenSelection.anchor, chosenSelection.head); } diff --git a/manifest.json b/manifest.json index 4c67893..5bad67c 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-vimrc-support", "name": "Vimrc Support", - "version": "0.6.0", + "version": "0.6.1", "description": "Auto-load a startup file with Obsidian Vim commands.", "author": "esm", "authorUrl": "", diff --git a/package.json b/package.json index 701e42d..97ff89a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-vimrc-support", - "version": "0.6.0", + "version": "0.6.1", "description": "Auto-load a startup file with Obsidian Vim commands.", "main": "main.js", "scripts": {