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

Rebasing does not work on a very basic example #874

Open
mitar opened this issue Nov 23, 2018 · 3 comments
Open

Rebasing does not work on a very basic example #874

mitar opened this issue Nov 23, 2018 · 3 comments

Comments

@mitar
Copy link

mitar commented Nov 23, 2018

Issue details

When trying to rebase three sets of steps (initial, fork1 which adds at the end, fork2 which replaces at the beginning), rebasing drops content from fork2, instead of combining those steps together.

Steps to reproduce

const {rebaseSteps} = require('prosemirror-collab');
const {Step, Transform} = require('prosemirror-transform');
const {Schema} = require('prosemirror-model');

const nodes = {
  doc: {
    content: 'title (paragraph|block)+',
  },

  title: {
    content: 'inline*',
    parseDOM: [{
      tag: 'h1',
    }],
    toDOM(node) {
      return ['h1', 0];
    },
  },

  paragraph: {
    content: 'inline*',
    group: 'block',
    parseDOM: [{
      tag: 'p',
    }],
    toDOM(node) {
      return ['p', 0];
    },
  },

  text: {
    group: 'inline',
  },
};

const marks = {};

const schema = new Schema({nodes, marks});

const INITIAL_STEPS = [{
  stepType: 'replace',
  from: 3,
  to: 3,
  slice: {
    content: [{
      type: 'text',
      text: 't',
    }],
  },
}, {
  stepType: 'replace',
  from: 4,
  to: 4,
  slice: {
    content: [{
      type: 'text',
      text: 'e',
    }],
  },
}, {
  stepType: 'replace',
  from: 5,
  to: 5,
  slice: {
    content: [{
      type: 'text',
      text: 's',
    }],
  },
}, {
  stepType: 'replace',
  from: 6,
  to: 6,
  slice: {
    content: [{
      type: 'text',
      text: 't',
    }],
  },
}];

const FORK1_STEPS = [{
  stepType: 'replace',
  from: 7,
  to: 7,
  slice: {
    content: [{
      type: 'text',
      text: '.',
    }],
  },
}];

const FORK2_STEPS = [{
  stepType: 'replace',
  from: 3,
  to: 4,
  slice: {
    content: [{
      type: 'text',
      text: 'T',
    }],
  },
}];

const doc = schema.topNodeType.createAndFill();
const transform = new Transform(doc);

const initialSteps = [];
for (const step of INITIAL_STEPS) {
  initialSteps.push(Step.fromJSON(schema, step));
  transform.step(initialSteps[initialSteps.length - 1]);
}

const fork2steps = [];
for (const step of FORK2_STEPS) {
  fork2steps.push(Step.fromJSON(schema, step));
  transform.step(fork2steps[fork2steps.length - 1]);
}

const fork1steps = [];
for (const step of FORK1_STEPS) {
  fork1steps.push(Step.fromJSON(schema, step));
}

rebaseSteps(fork2steps.map((s, i) => {
  return {
    step: s,
    inverted: s.invert(transform.docs[initialSteps.length + i]),
  };
}), fork1steps, transform);

console.log(transform.doc.toJSON().content[1]);

I would expect the output to be text Test. and not test. which I get when running the script above. So only appending . is preserved, but the change at the beginning from t to T is not. If I instrument rebaseSteps function and add some prints to the console, I can see that the step to convert t to T is not mapped (mapped is null).

ProseMirror version

"prosemirror-collab": "~1.1.1",
@mitar
Copy link
Author

mitar commented Nov 23, 2018

But if I do rebase in this way:

const transform2 = new Transform(transform.doc);

rebaseSteps(fork2steps.map((s, i) => {
  return {
    step: s,
    inverted: s.invert(transform.docs[initialSteps.length + i]),
  };
}), fork1steps, transform2);

console.log(transform2.doc.toJSON().content[1]);

Then the output is correct.

Why it matters if the provided transform is empty or not? Maybe mapping is not correctly sliced like I observed here as well?

@jure
Copy link

jure commented Jan 27, 2019

@mitar I've tried the code in your first post and it still produces test., which makes sense given that the dependencies haven't changed since. Just randomly stumbled across this issue and was wondering if you've figured it in the mean time?

@mitar
Copy link
Author

mitar commented Jan 28, 2019

Yes, it seems slicing is not correct. I linked from my last comment.

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

2 participants