typewriter-editor / typewriter

A rich text editor based off of Quill.js and Ultradom, and using Svelte for UI.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Text insertion deltas are incorrect when inserting a character before an identical character

taylorhadden opened this issue · comments

This is occurring on commit 2585594.

Here is an example text change caused by typing characters. | references the editing carat position, and spaces are inserted between characters for the sake of consistency with the usage of the pipe.

// before
d i|n e r

// after
d i n|n e r

With this edit, one would expect a delta of:

{
  ops: [
    {
      retain: 2
    },
    {
      insert: 'n'
    }
  ]
}

However, the actual change event that is propagated is:

{
  ops: [
    {
      retain: 3
    },
    {
      insert: 'n'
    }
  ]
}

This can be reproduced by creating a blank editor, typing the requisite text into that editor, and observing the resulting change events.

The error is occurring in input.ts::getTextChange() where fast-diff is presented with the strings diner and dinner and (reasonably) sees the second "n" as being inserted rather than the first.

This behavior causes problems when trying to track and adjust an editing range based on inserted & deleted characters. If my editing range is [0, 2] at the start of the example, the retain 3, insert 'n' delta occurs outside of that editing range.

This same behavior is not present when deleting, as that is handled through the keyboard.js editing module.

This also occurs when inserting text in a range. For example replacing "mm" with an "m":

// before
h |m m|m

// after
h  m|m

In this example, the diff says that hmm is the same and that the last "m" is removed.

Fixed by #75