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

feature: Able to match newline in onTextInput handler #481

Closed
eguitarz opened this issue Aug 31, 2016 · 9 comments
Closed

feature: Able to match newline in onTextInput handler #481

eguitarz opened this issue Aug 31, 2016 · 9 comments

Comments

@eguitarz
Copy link
Contributor

When implementing heading (H1, H2, H3) in WYSIWYG markdown editor, it would requires detecting if newline is entered. So I can do something like this:

editor.onTextInput({
  // "# " -> h1, "## " -> h2, "### " -> h3
  match: /^(#{1,3})(.*)\n/,
  run() {
    // magic happens here
  }
});

Is this possible in current structure?

@bantic
Copy link
Collaborator

bantic commented Aug 31, 2016

@eguitarz Is this because you want to do the conversion only after hitting enter on the line? Would it work for your purposes to convert to h* after the user hits the space? e.g.:

  • user types "##"
  • user types " " -> "##" is deleted and section is converted to an empty h2 section with the cursor at the start of it

To answer you question, the "\n" is not matched by the onTextInputenter is handled by editor.insertNewline() instead. We can certainly add hooks similar to the delete hooks for before/after inserting a newline. As discussed here (#451 (comment)), it would be useful for some folks to be able to have a little more control over what the insertNewline functionality means (the ability to keep or change the current markup section tagname, e.g. — right now any newline resets to a standard "p" markup section).

@bantic
Copy link
Collaborator

bantic commented Aug 31, 2016

There's also a request for the ability to special-case newline-handling for different section types here: #432 (comment)

@eguitarz
Copy link
Contributor Author

eguitarz commented Sep 1, 2016

@bantic I agree this could be a temporary solution, but if we can control insertNewline would be great. Because when implementing image or link markdown syntax, it would be nice to insert a card or atom after you press enter.

 ![alt text](https://github.com/adam-p/markdown-here/raw/master/src/common/images/icon48.png)

I believe this could make mobiledoc allows more interesting customizations. This might also be helpful if we are supporting pre tag. We can enter code editing mode when detected + enter. In code editing mode, every `enter` will be converted to `\n` instead of adding new section. Once users finished editing code, they can type + enter to exit coding mode.

@bantic
Copy link
Collaborator

bantic commented Sep 1, 2016

+1 yes, if you are interested in making a PR that'd be great. Otherwise I'll mark this as a feature request and we'll get it in as soon as we can.

@eguitarz
Copy link
Contributor Author

eguitarz commented Sep 1, 2016

Thanks, will see what I can do to make this happen!

@eguitarz
Copy link
Contributor Author

eguitarz commented Sep 5, 2016

@bantic After thinking about this feature, I'm proposing two modifications.

  1. Add willHandleNewline/didHandleNewline hooks.
  2. Add disableNewline function in editor

This could grant fully controls on handling newline. For user to define their own newline behavior, they can disableNewline in willHandleNewline hook, perform customized function, then enable newline again in didHandleNewline.

How do you think about this?

@bantic
Copy link
Collaborator

bantic commented Sep 6, 2016

@eguitarz How about if willHandleNewline returns an explicit false, mobiledoc-kit skips the default handling? So if you want to do something special you do it in willHandleNewline and just return false?

@eguitarz
Copy link
Contributor Author

eguitarz commented Sep 6, 2016

@bantic That sounds perfect to me! I'll work on this - Add willHandleNewline hook prior to handleNewLine and will stop handling new line if willHandleNewline returns explicit false (It works as default if willHandleNewline didn't return or it returns true).

@bantic
Copy link
Collaborator

bantic commented Sep 9, 2016

I believe this is closed by #489. thanks @eguitarz ! Feel free to reopen if necessary.

@bantic bantic closed this as completed Sep 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants