bustle / mobiledoc-kit

A toolkit for building WYSIWYG editors with Mobiledoc
https://bustle.github.io/mobiledoc-kit/demo/
MIT License
1.55k stars 151 forks source link

feature: Able to match newline in onTextInput handler #481

Closed eguitarz closed 8 years ago

eguitarz commented 8 years ago

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 commented 8 years ago

@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.:

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 (https://github.com/bustlelabs/mobiledoc-kit/issues/451#issuecomment-239449216), 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 commented 8 years ago

There's also a request for the ability to special-case newline-handling for different section types here: https://github.com/bustlelabs/mobiledoc-kit/issues/432#issuecomment-239194830

eguitarz commented 8 years ago

@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 commented 8 years ago

+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 commented 8 years ago

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

eguitarz commented 8 years ago

@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 commented 8 years ago

@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 commented 8 years ago

@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 commented 8 years ago

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