typewriter-editor / typewriter

A rich text editor based off of Quill.js and Ultradom, and using Svelte for UI.
MIT License
394 stars 32 forks source link

Inserting a string with a newline with a non-collapsed selection inverts the intended line placement. #78

Closed taylorhadden closed 2 years ago

taylorhadden commented 3 years ago

e.g. If you have a word selected and call editor.insert('first\nsecond'), you will get the following delta

[
  { retain: <num> },
  { insert: 'second' },
  { insert: '\n' },
  { insert: 'first' },
  { delete: <selection length> }
]

This does not happen if you make the same insert call with a collapsed selection.

This appears to be an artifact of how deltas compose themselves when target insertion indices overlap when the insertion position is behind the change's current delta.

Reversing this.delta.compose(applicator(new Delta().retain(at))) to applicator(new Delta().retain(at)).compose(this.delta) in TextChange.compose() causes the inserted text to be in the right order, but the start of the inserted text will be eaten by the delete Op.

taylorhadden commented 3 years ago

This is my current fallback:

const change = editor.change
if (isCollapsed) {
    change.insert(at, '\n' + newLinePrefix)
}
else {
    change.delete(selection)
    change.insert(at, '\n').insert(at + 1, newLinePrefix)
}
change.apply()
jacwright commented 2 years ago

Got a fix in. Releasing in 0.6.44