obsidian-text-format icon indicating copy to clipboard operation
obsidian-text-format copied to clipboard

Added the option to toggle the case of the selected text.

Open doodo477 opened this issue 2 years ago • 9 comments

Hi.

Love your plugin but I was a bit frustrated having to map the keyboard shortcuts to uppercase and lower case. So I added the option just to toggle the case. The logic of choosing if the selected text is upper case or lower case is just to find the first character that does has a differing case character then covert the selected text the the opposite case of the character. This could be improved to be modern JavaScript but it gets the job done.

doodo477 avatar Feb 25 '22 00:02 doodo477

Hi, thank you for your PR.

Take an example, if we selected, say, "Obsidian plugin", what you expected is "obsidian plugin" if toggle once and "OBSIDIAN PLUGIN" if toggle twice. Is that right?

Benature avatar Feb 25 '22 01:02 Benature

Yes that is correct.

doodo477 avatar Feb 25 '22 01:02 doodo477

I think using a regex to get the first letter (or any letter) is better than a for loop.

Benature avatar Feb 25 '22 06:02 Benature

BTW, I am a bit curious about your use case. Do you only consider the uppercase and lowercase and no others like Capitalecase? Will you use this command to a whole sentence? Like "Obsidian is an app", whose first letter is uppercase usually.

Benature avatar Feb 25 '22 06:02 Benature

I was thinking the same thing but regexp would only cover the Latin alphabetical glyph [a-z|A-Z] excluding non Latin alphabetical glyphs. Understandably I don't know the whole Unicode code points (UTF-8) that have corresponding upper case or lower case equivalents, so I decided just to do a simple for loop checking the first glyph/codepoint that have a differing codepoint after converting the case.

doodo477 avatar Feb 25 '22 06:02 doodo477

For now, I only consider Latin and Cyrillic

"[\\w\\u0400-\\u04FF]"

Benature avatar Feb 25 '22 06:02 Benature

The use case would be to select a range of characters, and then toggle the case of the selected range of characters. For example selecting [Obsidian is an app], I would want it to be toggled to lower case so it would be [obsidian is an app] and if I used the keyboard shortcut again it would toggle the case to [OBSIDIAN IS AN APP], and vis versa to [obsidian is an app].

doodo477 avatar Feb 25 '22 06:02 doodo477

Obsidian_tGrThWECMV

doodo477 avatar Feb 25 '22 06:02 doodo477

For now, I only consider Latin and Cyrillic

"[\\w\\u0400-\\u04FF]"

I still think it is better to use regex to get the first letter, if regex fails to get, then use the for loop to search one. 🤔

Benature avatar Feb 25 '22 06:02 Benature

Add a setting to configure the toggle sequence loop as users like.

Benature avatar Dec 09 '22 17:12 Benature