diff --git a/README.md b/README.md index c4e785d..0602d6c 100644 --- a/README.md +++ b/README.md @@ -12,14 +12,14 @@ Press cmd/ctrl+P to enter the command. 👇 Or you can consider to bind custom hotkeys to those commands. -| Command | Description | -| :----------------------------------------- | ------------------------------------------------------------------- | -| Lowercase selected text | Lowercase all letters in selection | -| Uppercase selected text | Uppercase all letters in selection | -| Capitalize selected text | Capitalize all words in selection | -| Title case selected text | Capitalize words but leave certain words in lower case in selection | -| Remove redundant spaces in selection | Ensure only one space between words | -| Remove all newline characters in selection | Change multi-line selection into single-line text | +| Command | Description | +| :--------------------------------------------- | ------------------------------------------------------------------- | +| **Lowercase** selected text | Lowercase all letters in selection | +| **Uppercase** selected text | Uppercase all letters in selection | +| **Capitalize** selected text | Capitalize all words in selection | +| **Title case** selected text | Capitalize words but leave certain words in lower case in selection | +| Remove redundant **spaces** in selection | Ensure only one space between words | +| Remove all **newline characters** in selection | Change multi-line selection into single-line text | ## Example @@ -28,6 +28,10 @@ Or you can consider to bind custom hotkeys to those commands. - `uppercase`: "Hello, I am using Obsidian." -> "HELLO, I AM USING OBSIDIAN." - `capitalize`: "Hello, I am using Obsidian." -> "Hello, I Am Using Obsidian." - `titlecase`: "Obsidian is a good app." -> "Obsidian Is a Good App." -- `remove blanks`: "There are so many redundant blanks" -> "There are so many redundant blanks" +- `remove blanks` + ```diff + - There are so many redundant blanks + + There are so many redundant blanks" + ``` -![demo](demo.gif) \ No newline at end of file +![demo](https://user-images.githubusercontent.com/35028647/121776728-149ea500-cbc1-11eb-89ee-f4afcb0816ed.gif) diff --git a/demo.gif b/demo.gif deleted file mode 100644 index cfcadf6..0000000 Binary files a/demo.gif and /dev/null differ diff --git a/main.ts b/main.ts index 42f93ce..a411461 100644 --- a/main.ts +++ b/main.ts @@ -57,6 +57,7 @@ export default class Underline extends Plugin { replacedText = toTitleCase(selectedText); break; case "titlecase": + // @ts-ignore replacedText = selectedText.toTitleCase(); break; case "spaces": @@ -70,9 +71,23 @@ export default class Underline extends Plugin { default: return; } + if (replacedText != selectedText) { editor.replaceSelection(replacedText); } + + if (cmd != "newline") { + const tos = editor.posToOffset(editor.getCursor("to")); // to offset + editor.setSelection( + editor.offsetToPos(tos - replacedText.length), + editor.offsetToPos(tos) + ); + } else { + let anchor = editor.getCursor("anchor"); + let head = editor.getCursor("head"); + anchor.ch = 0; + editor.setSelection(anchor, head); + } } } } @@ -81,9 +96,4 @@ function toTitleCase(s: string): string { return s.replace(/\w\S*/g, function (t) { return t.charAt(0).toUpperCase() + t.substr(1).toLowerCase(); }); - // return s - // .toLowerCase() - // .split(" ") - // .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) - // .join(" "); } diff --git a/manifest.json b/manifest.json index b58c476..4b59167 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "obsidian-text-format", "name": "Text Format", - "version": "0.2.0", + "version": "0.2.1", "minAppVersion": "0.9.7", "description": "Format selected text lowercase/uppercase/capitalize/titlecase or remove redundant spaces/newline characters.", "author": "Benature",