prosemirror-typerighter icon indicating copy to clipboard operation
prosemirror-typerighter copied to clipboard

feat!: Handle multi-range matches for ignored ranges.

Open jonathonherbert opened this issue 1 year ago • 0 comments

What does this change?

Adds internal support for multi-range matches for ignored ranges. Matches can now span an ignored range. Suggestions applied to that range will map around the ignored range, preserving styling where possible.

styling

Ignored ranges are not passed on to the Typerighter service, so this change only affects plugin consumers. For them, this is a breaking change, as some public apis that expect a single IRange now receive IRange[].

How does suggestion insertion work?

If the match is split into multiple ranges, the code attempts to preserve a reasonable split between ranges by allocating the suggestion characters across the original ranges. Extra characters are appended.

For example, given:

  • the match ex-a-mple, where dashes denote splits in the match
  • the suggestion ample

We get the result am-p-le, preserving the position of the ignored ranges, rather than something like ample--.

How does suggestion styling work?

When inserting new content, we map the positions from the insertion back to the original document, and use those positions and the original document to derive marks.

For example, the following text:

<strong>exampl</strong> suggestion
        |    |
        1    5

might produce the suggestion example, spanning 1,6. This produces the patch insert e at 6. We use the inverse of the transaction mapping produced by the edit to map the position6 to the original position 5, from which we can copy the new marks.

The mappings account for ignored ranges, so we only derive styling from unignored copy. See the GIF for examples of this behaviour in practice.

(In adding more tests for this method, I've found that this catches an edge case in our previous code where we were ignoring styling in single width ranges.)

How to test

  • The automated tests should pass, testing the new functionality.
  • Have a play in the sandbox environment – Typerighter now ignores any text that is italic. You should be able to see suggestions being applied correctly, with styling preserved:

Todo

  • [x] ~Test in a consuming application.~ Tested in Composer.

jonathonherbert avatar Nov 30 '23 10:11 jonathonherbert