Trigger Input Rules at all Time whenever a match is found

Hello guys,

I’m having a trouble that, I can’t get my input rules triggered at anytime.

for instace, I have an input rule for /{{[a-z]+}}/, it will work fine it I type a "}" right after "{{hello}", it will just get triggered.

But if have "{hello}}" in my conext, and I put a "{" in front of "{hello}}", it won’t get triggered because the new char comes at the begining of the match.

I wonder is there any way or workaround that I can use to trigger input rules at anytime? I’ve tried tweaking the code from prosemirror-inputrules repo but it’s really hard to handle the cursors and matches :cry: .

Thanks for reading!

You might have to fork prosemirror-inputrules to handle this (?):

The logic right now only looks for text before the current cursor location ($from), so to do what you want you’ll have to allow for a forward offset as well, e.g.

let textToMatch = $from.parent.textBetween(
  Math.max(0, $from.parentOffset - MAX_MATCH),
  Math.min($from.parentOffset + MAX_MATCH, $from.parent.textContent.length)
  null, 
  "\ufffc"
) + text;
2 Likes

Yeah,

I’m tweaking the function to make it also look forward to see the match strings.

So far so good, but just need to be able to find the right from and to pos for passing to the handler, still figuring it out.

FYI, I don’t think the code below works, coz the inserted text can be anywhere in the textToMatch, not just at the end.

let textToMatch = $from.parent.textBetween(
    Math.max(0, $from.parentOffset - MAX_MATCH),
    Math.min($from.parentOffset + MAX_MATCH, $from.parent.textContent.length)
    null, 
    "\ufffc"
) + text;

So here’s what I do, textFrom is the start pos of view.state.doc.resolve(from); and textTo is the end pos of view.state.doc.resolve(to);

let textToMatch = $from.parent.textBetween(textFrom, from) + text + $from.parent.textBetween(to, textTo);