Description
I created a prosemirror plugin that replaces the current paragraph with a bulleted list using transaction.replaceWith()
. However, I unexpectedly do not see the list item applied. It remains a paragraph in prosemirror, even though I see a list-item
in the list of computed patches for automerge's intercept()
function.
Steps to reproduce
- Clone this minimal reproduction
- Follow the README instructions to install and start the dev server
- Attempt to type "- " before the phrase "Welcome" in the editor
- Notice the "- " is correctly removed, but the element type remains the same
I've also noticed exceptions thrown in the console attempting to delete nonexistent characters after the above reproduction. My guess is that automerge generated a tree that prosemirror failed to render correctly, so edits are running on a bad state.
Plugin implementation
See the plugin implementation here. Snippet pasted below for convenience:
const { $from } = view.state.selection;
let tr = view.state.tr;
const li = schema.node(
"list_item",
null,
schema.node(
"paragraph",
null,
$from.parent.content.cut(1, $from.parent.content.size)
)
);
const ul = schema.node("bullet_list", null, [li]);
const parentPos = $from.start(-1);
view.dispatch(
tr
.replaceWith(parentPos, parentPos + $from.parent.nodeSize, ul)
.setSelection(TextSelection.create(tr.doc, parentPos + 1))
);
This plugin was tested using prosemirror without automerge, and the list item was applied without issue.
Possible causes
It's possible the parent.content.cut
is causing automerge to create a diff that assumes the same text block has stayed the same, and it fails to create the correct parent for that text. Note that .cut(1, ...)
is used to omit the "- " in the outputted list item. I see this is interpreted as a delete patch in automerge's intercept()
function.
Activity