loro/loro-js/tests/richtext.test.ts

271 lines
7.5 KiB
TypeScript
Raw Normal View History

import { describe, expect, it } from "vitest";
import { Delta, Loro, TextDiff } from "../src";
import { Cursor, OpId, setDebug } from "loro-wasm";
describe("richtext", () => {
it("mark", () => {
const doc = new Loro();
doc.configTextStyle({
bold: { expand: "after" },
link: { expand: "before" },
});
const text = doc.getText("text");
text.insert(0, "Hello World!");
text.mark({ start: 0, end: 5 }, "bold", true);
expect(text.toDelta()).toStrictEqual([
{
insert: "Hello",
attributes: {
bold: true,
},
},
{
insert: " World!",
},
] as Delta<string>[]);
});
2023-11-04 04:24:05 +00:00
it("insert after emoji", () => {
const doc = new Loro();
const text = doc.getText("text");
text.insert(0, "👨‍👩‍👦");
text.insert(8, "a");
expect(text.toString()).toBe("👨👩👦a");
});
2023-11-04 04:24:05 +00:00
it("emit event correctly", async () => {
const doc = new Loro();
const text = doc.getText("text");
let triggered = false;
text.subscribe(doc, (e) => {
for (const event of e.events) {
if (event.diff.type == "text") {
expect(event.diff.diff).toStrictEqual([
{
insert: "Hello",
attributes: {
bold: true,
},
},
{
insert: " World!",
},
] as Delta<string>[]);
triggered = true;
}
}
});
text.insert(0, "Hello World!");
text.mark({ start: 0, end: 5 }, "bold", true);
doc.commit();
await new Promise((r) => setTimeout(r, 1));
expect(triggered).toBeTruthy();
});
it("emit event from merging doc correctly", async () => {
const doc = new Loro();
const text = doc.getText("text");
let called = false;
text.subscribe(doc, (event) => {
if (event.events[0].diff.type == "text") {
called = true;
expect(event.events[0].diff.diff).toStrictEqual([
{
insert: "Hello",
attributes: {
bold: true,
},
},
{
insert: " World!",
},
] as Delta<string>[]);
}
});
const docB = new Loro();
const textB = docB.getText("text");
textB.insert(0, "Hello World!");
textB.mark({ start: 0, end: 5 }, "bold", true);
doc.import(docB.exportFrom());
await new Promise((r) => setTimeout(r, 1));
expect(called).toBeTruthy();
});
it("Delete emoji", async () => {
const doc = new Loro();
const text = doc.getText("text");
text.insert(0, "012345👨👩👦6789");
doc.commit();
text.mark({ start: 0, end: 18 }, "bold", true);
doc.commit();
expect(text.toDelta()).toStrictEqual([
{
insert: "012345👨👩👦6789",
attributes: { bold: true },
},
]);
text.delete(6, 8);
doc.commit();
expect(text.toDelta()).toStrictEqual([
{
insert: "0123456789",
attributes: { bold: true },
},
]);
});
it("apply delta", async () => {
2024-01-22 08:00:32 +00:00
const doc1 = new Loro();
doc1.configTextStyle({
link: { expand: "none" },
bold: { expand: "after" },
});
2024-01-22 08:00:32 +00:00
const text1 = doc1.getText("text");
const doc2 = new Loro();
2024-01-22 08:00:32 +00:00
doc2.configTextStyle({
link: { expand: "none" },
bold: { expand: "after" },
});
const text2 = doc2.getText("text");
2024-01-22 08:00:32 +00:00
text1.subscribe(doc1, (event) => {
for (const containerDiff of event.events) {
const e = containerDiff.diff as TextDiff;
text2.applyDelta(e.diff);
}
});
2024-01-22 08:00:32 +00:00
text1.insert(0, "foo");
text1.mark({ start: 0, end: 3 }, "link", true);
doc1.commit();
await new Promise((r) => setTimeout(r, 1));
expect(text2.toDelta()).toStrictEqual(text1.toDelta());
text1.insert(3, "baz");
doc1.commit();
await new Promise((r) => setTimeout(r, 1));
expect(text2.toDelta()).toStrictEqual([
{ insert: "foo", attributes: { link: true } },
{ insert: "baz" },
]);
2024-01-22 08:00:32 +00:00
expect(text2.toDelta()).toStrictEqual(text1.toDelta());
text1.mark({ start: 2, end: 5 }, "bold", true);
doc1.commit();
await new Promise((r) => setTimeout(r, 1));
expect(text2.toDelta()).toStrictEqual(text1.toDelta());
});
it("custom richtext type", async () => {
const doc = new Loro();
doc.configTextStyle({
myStyle: {
expand: "none",
},
});
const text = doc.getText("text");
text.insert(0, "foo");
text.mark({ start: 0, end: 3 }, "myStyle", 123);
expect(text.toDelta()).toStrictEqual([
{ insert: "foo", attributes: { myStyle: 123 } },
]);
expect(() => {
text.mark({ start: 0, end: 3 }, "unknownStyle", 2);
}).toThrowError();
expect(() => {
// default style config should be overwritten
text.mark({ start: 0, end: 3 }, "bold", 2);
}).toThrowError();
});
it("allow overlapped styles", () => {
const doc = new Loro();
doc.configTextStyle({
comment: { expand: "none" },
});
const text = doc.getText("text");
text.insert(0, "The fox jumped.");
text.mark({ start: 0, end: 7 }, "comment:alice", "Hi");
text.mark({ start: 4, end: 14 }, "comment:bob", "Jump");
expect(text.toDelta()).toStrictEqual([
{
insert: "The ",
attributes: { "comment:alice": "Hi" },
},
{
insert: "fox",
attributes: { "comment:alice": "Hi", "comment:bob": "Jump" },
},
{
insert: " jumped",
attributes: { "comment:bob": "Jump" },
},
{
insert: ".",
},
]);
});
Cursors (#290) This PR introduces support for retrieving and querying cursors. ## Motivation Using "index" to denote cursor positions can be unstable, as positions may shift with document edits. To reliably represent a position or range within a document, it is more effective to leverage the unique ID of each item/character in a List CRDT or Text CRDT. ## Updating Cursors Loro optimizes State metadata by not storing the IDs of deleted elements. This approach, while efficient, complicates tracking cursor positions since they rely on these IDs for precise locations within the document. The solution recalculates position by replaying relevant history to update stable positions accurately. To minimize the performance impact of history replay, the system updates cursor info to reference only the IDs of currently present elements, thereby reducing the need for replay. Each position has a "Side" information, indicating the actual cursor position is on the left, right, or directly in the center of the target ID. Note: In JavaScript, the offset returned when querying a Stable Position is based on the UTF-16 index. # Example ```ts const loro = new Loro(); const list = loro.getList("list"); list.insert(0, "a"); const pos0 = list.getStablePos(0); list.insert(1, "b"); { const ans = loro.queryStablePos(pos0!); expect(ans.offset).toEqual(0); expect(ans.side).toEqual(0); expect(ans.update).toBeUndefined(); } list.insert(0, "c"); { const ans = loro.queryStablePos(pos0!); expect(ans.offset).toEqual(1); expect(ans.side).toEqual(0); expect(ans.update).toBeUndefined(); } list.delete(1, 1); { const ans = loro.queryStablePos(pos0!); expect(ans.offset).toEqual(1); expect(ans.side).toEqual(-1); expect(ans.update).toBeDefined(); } ```
2024-04-09 08:01:37 +00:00
it("Cursor example", () => {
const doc = new Loro();
const text = doc.getText("text");
text.insert(0, "123");
const pos0 = text.getCursor(0, 0);
{
const ans = doc.getCursorPos(pos0!);
expect(ans.offset).toBe(0);
}
text.insert(0, "1");
{
const ans = doc.getCursorPos(pos0!);
expect(ans.offset).toBe(1);
}
});
it("Get and query cursor", () => {
const doc = new Loro();
const text = doc.getText("text");
doc.setPeerId("1");
text.insert(0, "123");
const pos0 = text.getCursor(0, 0);
expect(pos0?.containerId()).toBe("cid:root-text:Text");
// pos0 points to the first character, i.e. the id of '1'
expect(pos0?.pos()).toStrictEqual({ peer: "1", counter: 0 } as OpId);
{
const ans = doc.getCursorPos(pos0!);
expect(ans.side).toBe(0);
expect(ans.offset).toBe(0);
expect(ans.update).toBeUndefined();
}
text.insert(0, "abc");
const bytes = pos0!.encode();
// Sending pos0 over the network
const pos0decoded = Cursor.decode(bytes);
const docA = new Loro();
docA.import(doc.exportFrom());
Cursors (#290) This PR introduces support for retrieving and querying cursors. ## Motivation Using "index" to denote cursor positions can be unstable, as positions may shift with document edits. To reliably represent a position or range within a document, it is more effective to leverage the unique ID of each item/character in a List CRDT or Text CRDT. ## Updating Cursors Loro optimizes State metadata by not storing the IDs of deleted elements. This approach, while efficient, complicates tracking cursor positions since they rely on these IDs for precise locations within the document. The solution recalculates position by replaying relevant history to update stable positions accurately. To minimize the performance impact of history replay, the system updates cursor info to reference only the IDs of currently present elements, thereby reducing the need for replay. Each position has a "Side" information, indicating the actual cursor position is on the left, right, or directly in the center of the target ID. Note: In JavaScript, the offset returned when querying a Stable Position is based on the UTF-16 index. # Example ```ts const loro = new Loro(); const list = loro.getList("list"); list.insert(0, "a"); const pos0 = list.getStablePos(0); list.insert(1, "b"); { const ans = loro.queryStablePos(pos0!); expect(ans.offset).toEqual(0); expect(ans.side).toEqual(0); expect(ans.update).toBeUndefined(); } list.insert(0, "c"); { const ans = loro.queryStablePos(pos0!); expect(ans.offset).toEqual(1); expect(ans.side).toEqual(0); expect(ans.update).toBeUndefined(); } list.delete(1, 1); { const ans = loro.queryStablePos(pos0!); expect(ans.offset).toEqual(1); expect(ans.side).toEqual(-1); expect(ans.update).toBeDefined(); } ```
2024-04-09 08:01:37 +00:00
{
const ans = docA.getCursorPos(pos0decoded!);
Cursors (#290) This PR introduces support for retrieving and querying cursors. ## Motivation Using "index" to denote cursor positions can be unstable, as positions may shift with document edits. To reliably represent a position or range within a document, it is more effective to leverage the unique ID of each item/character in a List CRDT or Text CRDT. ## Updating Cursors Loro optimizes State metadata by not storing the IDs of deleted elements. This approach, while efficient, complicates tracking cursor positions since they rely on these IDs for precise locations within the document. The solution recalculates position by replaying relevant history to update stable positions accurately. To minimize the performance impact of history replay, the system updates cursor info to reference only the IDs of currently present elements, thereby reducing the need for replay. Each position has a "Side" information, indicating the actual cursor position is on the left, right, or directly in the center of the target ID. Note: In JavaScript, the offset returned when querying a Stable Position is based on the UTF-16 index. # Example ```ts const loro = new Loro(); const list = loro.getList("list"); list.insert(0, "a"); const pos0 = list.getStablePos(0); list.insert(1, "b"); { const ans = loro.queryStablePos(pos0!); expect(ans.offset).toEqual(0); expect(ans.side).toEqual(0); expect(ans.update).toBeUndefined(); } list.insert(0, "c"); { const ans = loro.queryStablePos(pos0!); expect(ans.offset).toEqual(1); expect(ans.side).toEqual(0); expect(ans.update).toBeUndefined(); } list.delete(1, 1); { const ans = loro.queryStablePos(pos0!); expect(ans.offset).toEqual(1); expect(ans.side).toEqual(-1); expect(ans.update).toBeDefined(); } ```
2024-04-09 08:01:37 +00:00
expect(ans.side).toBe(0);
expect(ans.offset).toBe(3);
expect(ans.update).toBeUndefined();
}
Cursors (#290) This PR introduces support for retrieving and querying cursors. ## Motivation Using "index" to denote cursor positions can be unstable, as positions may shift with document edits. To reliably represent a position or range within a document, it is more effective to leverage the unique ID of each item/character in a List CRDT or Text CRDT. ## Updating Cursors Loro optimizes State metadata by not storing the IDs of deleted elements. This approach, while efficient, complicates tracking cursor positions since they rely on these IDs for precise locations within the document. The solution recalculates position by replaying relevant history to update stable positions accurately. To minimize the performance impact of history replay, the system updates cursor info to reference only the IDs of currently present elements, thereby reducing the need for replay. Each position has a "Side" information, indicating the actual cursor position is on the left, right, or directly in the center of the target ID. Note: In JavaScript, the offset returned when querying a Stable Position is based on the UTF-16 index. # Example ```ts const loro = new Loro(); const list = loro.getList("list"); list.insert(0, "a"); const pos0 = list.getStablePos(0); list.insert(1, "b"); { const ans = loro.queryStablePos(pos0!); expect(ans.offset).toEqual(0); expect(ans.side).toEqual(0); expect(ans.update).toBeUndefined(); } list.insert(0, "c"); { const ans = loro.queryStablePos(pos0!); expect(ans.offset).toEqual(1); expect(ans.side).toEqual(0); expect(ans.update).toBeUndefined(); } list.delete(1, 1); { const ans = loro.queryStablePos(pos0!); expect(ans.offset).toEqual(1); expect(ans.side).toEqual(-1); expect(ans.update).toBeDefined(); } ```
2024-04-09 08:01:37 +00:00
// If "1" is removed from the text, the stable position should be updated
text.delete(3, 1); // remove "1", "abc23"
doc.commit();
{
const ans = doc.getCursorPos(pos0!);
expect(ans.side).toBe(-1);
expect(ans.offset).toBe(3);
expect(ans.update).toBeDefined(); // The update of the stable position should be returned
// It points to "2" now so the pos should be { peer: "1", counter: 1 }
expect(ans.update?.pos()).toStrictEqual({
peer: "1",
counter: 1,
} as OpId);
// Side should be -1 because "1" was at the left side of "2"
expect(ans.update!.side()).toBe(-1);
expect(ans.update?.containerId()).toBe("cid:root-text:Text");
}
});
});