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

feat: diff, revertTo, and applyDiff #610

Merged
merged 14 commits into from
Jan 9, 2025
Merged

feat: diff, revertTo, and applyDiff #610

merged 14 commits into from
Jan 9, 2025

Conversation

zxch3n
Copy link
Member

@zxch3n zxch3n commented Jan 6, 2025

Add new version-control-related primitives:

  • diff(from, to): calculate the difference between two versions. The returned results have similar structures to the differences in events.
  • revertTo(targetVersion): revert the document back to the target version. The difference between this and checkout(targetVersion) is this method will generate a series of new operations, which will transform the current doc into the same as the target version.
  • applyDiff(diff): you can use it to apply the differences generated from diff(from, to).

You can use these primitives to implement version-control functions like squash and revert.

Examples

revertTo

const doc = new LoroDoc();
doc.setPeerId("1");
doc.getText("text").update("Hello");
doc.commit();
doc.revertTo([{ peer: "1", counter: 1 }]);
expect(doc.getText("text").toString()).toBe("He");

diff

const doc = new LoroDoc();
doc.setPeerId("1");
// Text edits with formatting
const text = doc.getText("text");
text.update("Hello");
text.mark({ start: 0, end: 5 }, "bold", true);
doc.commit();

// Map edits
const map = doc.getMap("map");
map.set("key1", "value1");
map.set("key2", 42);
doc.commit();

// List edits
const list = doc.getList("list");
list.insert(0, "item1");
list.insert(1, "item2");
list.delete(1, 1);
doc.commit();

// Tree edits
const tree = doc.getTree("tree");
const a = tree.createNode();
a.createNode();
doc.commit();

const diff = doc.diff([], doc.frontiers());
expect(diff).toMatchSnapshot()
{
  "cid:root-list:List": {
    "diff": [
      {
        "insert": [
          "item1",
        ],
      },
    ],
    "type": "list",
  },
  "cid:root-map:Map": {
    "type": "map",
    "updated": {
      "key1": "value1",
      "key2": 42,
    },
  },
  "cid:root-text:Text": {
    "diff": [
      {
        "attributes": {
          "bold": true,
        },
        "insert": "Hello",
      },
    ],
    "type": "text",
  },
  "cid:root-tree:Tree": {
    "diff": [
      {
        "action": "create",
        "fractionalIndex": "80",
        "index": 0,
        "parent": undefined,
        "target": "12@1",
      },
      {
        "action": "create",
        "fractionalIndex": "80",
        "index": 0,
        "parent": "12@1",
        "target": "13@1",
      },
    ],
    "type": "tree",
  },
}

@zxch3n zxch3n requested a review from Leeeon233 January 6, 2025 17:20
@jamesgibson14
Copy link

Does, revert also "commit" the doc?

@zxch3n
Copy link
Member Author

zxch3n commented Jan 7, 2025

@jamesgibson14 yes, it's like a revert commit in Git.

@zxch3n zxch3n merged commit ddafb7e into main Jan 9, 2025
1 check passed
@zxch3n zxch3n deleted the feat-diff-and-revert branch January 9, 2025 04:39
@github-actions github-actions bot mentioned this pull request Jan 9, 2025
@mwildehahn mwildehahn mentioned this pull request Jan 15, 2025
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

Successfully merging this pull request may close these issues.

2 participants