Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected output using replaceWith() from a plugin #20

Open
bholmesdev opened this issue Aug 31, 2024 · 0 comments
Open

Unexpected output using replaceWith() from a plugin #20

bholmesdev opened this issue Aug 31, 2024 · 0 comments

Comments

@bholmesdev
Copy link

bholmesdev commented Aug 31, 2024

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.

@bholmesdev bholmesdev changed the title Unexpected output using replaceWith() Unexpected output using replaceWith() from a plugin Aug 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant