ProseMirror / prosemirror

The ProseMirror WYSIWYM editor

Home Page:http://prosemirror.net/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Distinguish between start of paragraph and start of newline in InputRule regex

delashum opened this issue · comments

Problem

From what I can see it's impossible to detect the position directly after a hard_break using just an InputRules regular expression.

Many users of editors don't understand the difference between a hard_break and a new paragraph and I would like most of our input-rules to support either position (with a bit of extra logic to bump it down to new paragraph if it is activated on a newline).

The hard_break node is represented as the object replacement character, which is the same character used to represent any block-level node. Because this character is hard-coded here it's also impossible to override this for certain nodes to give them a unique representation that can be picked up in regex.

Proposal

Either introduce a new property on NodeSpec to allow the regex representation of a node to be overwritten or special-case the hard_break to use a distinct symbol from other nodes.

I'm happy to open a pull request if one of these solutions is acceptable.

Reproduction

If you want to reproduce it's easy to see this behavior in the example on the prosemirror home page by typing Shift+Enter before > followed by a space. This will not automatically convert to a block quote like it would if it was a new paragraph.

This would be useful, could the hard_break special case literally be a newline character? 🤔

A way to implement what you are describing is to make the regexp less strict, allowing it to match everywhere, and have your input rule handler function check whether the match is in a valid position, and return null if it isn't.

Just making the change you describe wouldn't automatically make rules work the way you seem to expect—neither wrappingInputRule nor textblockTypeInputRule would split the textblock in the way that would make the updated document match your expectation here.

In general, ProseMirror as a library isn't really targeting situations where the difference between a hard break and a newly started textblock can be ignored. You can probably add your own code to get where you want to be, but the out-of-the-box behavior isn't going to cater to that.