Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move cursor after bold or italic span without toggling, iff it's at the end of text right before the delimiter #1

Merged
merged 3 commits into from
Apr 12, 2017

Conversation

zkirkland
Copy link
Contributor

I wanted the bold and italics toggles to work more like the way they do in Google Docs where the user can toggle bold to "on", type the word or words they want, then toggle bold to "off" to keep typing in normal style.

The way I accomplished this in the code is to have the cursor jump to outside of the "*" or "**" in the markdown if the user hits the keyboard shortcut again while the cursor is at the end of the bold text but still inside of the asterisks.

In other words, if the user hits Ctrl+B when the cursor is at:

**bold text|**

the cursor will jump two places to the right:

**bold text**|

and the user can keep typing as normal without having to hit the "end" key or use the mouse.

@yzhang-gh
Copy link
Owner

Good idea 👍

if (selection.isEmpty) {
let position = selection.active;

selection = new Selection(new Position(position.line, position.character - startPattern.length), position);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this line will fail when the cursor is at an empty newline 😔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. I did not think to test that. I will try to fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it and pushed the change.

@yzhang-gh
Copy link
Owner

BTW, do you have a good description about the toggling behavior to put in the README?

I prefer this format,

  • If there is no selection, ...
  • If there is a selection, ...

But I don't know how to properly describe it 😅

@zkirkland
Copy link
Contributor Author

I am glad you like the idea. Here is how I describe the functionality:


  • If there is NO selection and the cursor is at the end of a Bold or Italic word but BEFORE the asterisk (*) as shown here:

      **Bold|** or *Italic|*
    

    Pressing the corresponding hotkey (Ctrl+B or Ctrl+I) will move the cursor to the outside of the asterisk(s) as shown here:

      **Bold**| or *Italic*|
    

    All other functionality regarding the Bold and Italic hotkeys remains the same. This is similar to a rich text editor where the user can press the Bold or Italic hotkey to "turn on" the styling, type some text, and press the hotkey again to "turn off" the Bold or Italic styling and continue typing as normal.


This does not cover underscore styles. I don't know if that matters. Also if the user presses a hotkey to make text bold or italic when no text is selected, the asterisks are placed around the cursor but if the user presses the hotkey again without typing anything in the text, the extension adds more asterisks around the cursor instead of removing the "unwanted" ones. I almost fixed this but then I realized how this can be useful in certain situations so I left it alone. I'm not sure how you want to handle that, if at all.

@yzhang-gh
Copy link
Owner

Thanks!
For the empty bold block **|** (or italic), I'd like to remove the asterisks, so it will behave like "turn off".
How do you plan to fix this? Adding another isEmptyBlock method?

@zkirkland
Copy link
Contributor Author

I'm not sure yet how I will fix that.

@yzhang-gh
Copy link
Owner

OK, I will work it out later. Thanks again.

@yzhang-gh yzhang-gh merged commit 9dce8c8 into yzhang-gh:master Apr 12, 2017
@Lemmingh Lemmingh changed the title Small change to bold and italics toggle. Move cursor after bold or italic span without toggling, iff it's at the end of text right before the delimiter Nov 24, 2021
@Lemmingh Lemmingh added Area: Basic formatting Toggling bold, italic, etc. (Basic rich text editing features) No sure if it's an intuitive name. Area: Input Related to editor input processing (key presses, key bindings). labels Nov 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Basic formatting Toggling bold, italic, etc. (Basic rich text editing features) No sure if it's an intuitive name. Area: Input Related to editor input processing (key presses, key bindings).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants