Skip to content

Commit

Permalink
maintain selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Benature committed Jun 12, 2021
1 parent 1e2dcd0 commit 9f8b9ed
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ Press <kbd>cmd/ctrl+P</kbd> 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

Expand All @@ -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)
![demo](https://user-images.githubusercontent.com/35028647/121776728-149ea500-cbc1-11eb-89ee-f4afcb0816ed.gif)
Binary file removed demo.gif
Binary file not shown.
20 changes: 15 additions & 5 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export default class Underline extends Plugin {
replacedText = toTitleCase(selectedText);
break;
case "titlecase":
// @ts-ignore
replacedText = selectedText.toTitleCase();
break;
case "spaces":
Expand All @@ -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);
}
}
}
}
Expand All @@ -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(" ");
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 9f8b9ed

Please sign in to comment.