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

Exit code on right arrow #1200

Closed

Conversation

techstonia
Copy link

Hey 👋

This PR adds functionality to extension-code and allows the user to exit by pressing RightArrow:

leave-code

@philippkuehn
Copy link
Contributor

Hey, thanks for that PR.

I think your gif doesn't demonstrate your fix very well because after applying a mark input rule, the mark is always removed. It’s just a rendering issue that you think you are still in the mark. But if you type something it's without the mark. See:

Bildschirmaufnahme.2021-04-23.um.22.09.57.mp4

You maybe want to improve the behavior when a code mark is at the end of a line?

Bildschirmaufnahme.2021-04-23.um.22.23.03.mp4

There is also a inclusive: false option for marks which can change that behavior.

Bildschirmaufnahme.2021-04-23.um.22.23.03.mp4

Or is there any other case you wanted to improve?

@techstonia
Copy link
Author

I think I left out some context. 😬 Sorry about that.

It's difficult to leave code after editing it (and code is the last "thing" in editor):
codeess

@m1911star
Copy link

m1911star commented Apr 30, 2021

Hello~I need that. It's hard for me to append another kind of element after previous code block.So, I need arrow to skip out code block to continue input

@philippkuehn
Copy link
Contributor

I’m thinking of moving this into its own commands. It’s also important to say that this solved only the right side of code. I’m also not sure about adding a whitespace. Maybe just remove storedMarks is enough.

So maybe a leaveMarkLeft and leaveMarkRight would be nice? This affects also other marks like bold, italic...

Sorry about that but there are so many cases 🙃

@m1911star
Copy link

m1911star commented Apr 30, 2021

@philippkuehn I'll try, thanks a lot~

@martinthenth
Copy link

The option to add an extra space would be great. I currently have an implementation of Code that exits the Mark on two consecutive spaces, and then adds an extra space after the mark; but it relies on Transaction (i.e. return state.tr.insertText(' '), which is not available with keyboard shortcuts.

I believe the requested implementation does not actually insert a "space" character, because in the live editor it seems this.editor.insertContent(' ') filters the initial and final "space" characters from the value.

For example, this.editor.insertContent(' $ ') (space, $ sign, space) returns only `$'.

Is there a workaround to add the space character?

@techstonia
Copy link
Author

techstonia commented May 19, 2021

I think there were some changes in what insertContent accepts as parameters. Now

if (codeOnLeft && noCodeUnderCursor && nothingOnRight) {
            return this.editor
              .chain()
              .unsetCode()
              .insertContent([{ type: 'text', text: ' ' }])
              .run()
          }

this works instead.

EDIT: Updated the PR as well.

@techstonia techstonia force-pushed the exit-code-on-right-arrow branch from cc79e45 to 039dee2 Compare May 19, 2021 10:10
@philippkuehn
Copy link
Contributor

but it relies on Transaction (i.e. return state.tr.insertText(' '), which is not available with keyboard shortcuts.

don’t understand that 🤔

@philippkuehn
Copy link
Contributor

I think there were some changes in what insertContent accepts as parameters.

I changed some code there but it should still work with simple strings: https://www.tiptap.dev/api/commands/insert-content

But maybe there is a bug when using trailing or leading spaces.

@philippkuehn
Copy link
Contributor

With this fix this.editor.insertContent(' $ ') should work as expected again.

@martinthenth
Copy link

but it relies on Transaction (i.e. return state.tr.insertText(' '), which is not available with keyboard shortcuts.

don’t understand that 🤔

I should have mentioned I'm using a custom InputRule to match on "two consecutive spaces". InputRules have a variable tr available, which in turn has ProseMirror's insertText() function. I don't think addKeyboardShortcuts has this available.

The fix works great, exactly as expected. Thanks.

@philippkuehn
Copy link
Contributor

@martinthenth Within addKeyboardShortcuts you can literally do everything. A plain transaction (tr) is available everywhere.

addKeyboardShortcuts() {
  return {
    // work with plain transactions
    Enter: () => {
      const { tr } = this.editor.state

      tr.insertText('whatever')

      this.editor.view.dispatch(tr)

      return true
    },

    // or use commands as abstraction layer
    Enter: () => {
      return this.editor.commands.insertContent('whatever')
    },

    // or use the generic `command()` command
    Enter: () => {
      return this.editor.commands.command(({ tr }) => {
        tr.insertText('whatever')

        return true
      })
    },
  }
}

@rowanc1
Copy link

rowanc1 commented Nov 12, 2021

Might be useful to see another library prosemirror-codemark that solve these challenges around code-marks specifically and handles a lot of other use cases (clicking, arrow up/down, start-of-line quirks) through a fake cursor to indicate the next character insertion.
https://github.com/curvenote/prosemirror-codemark

Let me know if there is anything in there that might be helpful.

@uinz
Copy link

uinz commented Dec 24, 2021

Might be useful to see another library prosemirror-codemark that solve these challenges around code-marks specifically and handles a lot of other use cases (clicking, arrow up/down, start-of-line quirks) through a fake cursor to indicate the next character insertion. https://github.com/curvenote/prosemirror-codemark

Let me know if there is anything in there that might be helpful.

prosemirror-codemark not working with composetion input

@bdbch
Copy link
Member

bdbch commented Jun 25, 2022

May not be the best way to handle this, but it works for now and should help with exiting code marks in general with other marks - I added a new attribute to the Mark object that handles this on ArrowRight. See #2925

This would solve this issue for code marks that are at the end of a node (for example paragraph, where it's hard to exit the mark).

What I could now imagine as an improvement would be to "save" the last exit transaction and see if the selection moves out of the current node to remove the empty whitespace at the end of the node.

@github-actions github-actions bot added the Info: Stale The issue or pullrequest has not been updated in a while and might be stale label Aug 20, 2022
@github-actions github-actions bot closed this Aug 27, 2022
@techstonia techstonia deleted the exit-code-on-right-arrow branch October 26, 2022 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Info: Stale The issue or pullrequest has not been updated in a while and might be stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants