BitPhinix / slate-yjs

Yjs binding for Slate
https://docs.slate-yjs.dev
MIT License
514 stars 73 forks source link

#applySlateOp doesn't properly adjust indices for move_node operations #391

Open ezeidman opened 1 year ago

ezeidman commented 1 year ago

moveNode.ts doesn't properly handle the case when the origin and the target of the move operation are under the same parent and the target node is being moved forward to a later position under the parent.

Repro:

const paragraph: Paragraph = {
  type: "paragraph",
  elementId: "uuid-1",
  children: [
    {
      type: "text",
      text: "",
    },
    {
      type: "user-mention",
      elementId: "uuid-2",
      collaborator: {
        user: "bob",
        type: "user",
      },
      children: [{ text: "" } as any],
    },
    {
      type: "text",
      text: "abc",
    },
    {
      type: "text",
      text: "def",
      bold: true,
    },
  ],
};

describe("move inline forward", () => {
  it("move inline forward", () => {
    const { editor1 } = setupTreeTestEnvironment();

    editor1.apply({
      type: "insert_node",
      node: paragraph,
      path: [0],
    });
    editor1.flushLocalChanges();
    checkSlateMatchesYjs(editor1, createTreeSlateEditor);
    editor1.apply({
      type: "move_node",
      path: [0, 1],
      newPath: [0, 3],
    });
    editor1.flushLocalChanges();
    checkSlateMatchesYjs(editor1, createTreeSlateEditor);
  });
});
Screenshot 2023-05-05 at 5 21 32 PM
BitPhinix commented 1 year ago

Interesting find - happy to look into this in about a week. Super swamped at the moment. Glad to release a new version sooner if you put in a PR with a fix!