Skip to content

Commit 1d2906b

Browse files
committed
refactor: simplify the API further, implement SuggestionPlugin
1 parent 7ac9749 commit 1d2906b

File tree

3 files changed

+117
-153
lines changed

3 files changed

+117
-153
lines changed

packages/core/src/api/positionMapping.test.ts

Lines changed: 69 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,15 @@ describe("PositionStorage with local editor", () => {
3232

3333
describe("set and get positions", () => {
3434
it("should store and retrieve positions without Y.js", () => {
35-
positionStorage.set("test-id", 10);
36-
expect(positionStorage.get("test-id")).toBe(10);
37-
});
35+
const getPos = positionStorage.track(10);
3836

39-
it("should handle right side positions", () => {
40-
positionStorage.set("test-id", 10, "right");
41-
expect(positionStorage.get("test-id")).toBe(10);
37+
expect(getPos()).toBe(10);
4238
});
4339

44-
it("should be undefined when getting a non-existent position", () => {
45-
expect(positionStorage.get("non-existent")).toBeUndefined();
46-
});
40+
it("should handle right side positions", () => {
41+
const getPos = positionStorage.track(10, "right");
4742

48-
it("should remove positions", () => {
49-
positionStorage.set("test-id", 10);
50-
positionStorage.remove("test-id");
51-
expect(positionStorage.get("test-id")).toBeUndefined();
43+
expect(getPos()).toBe(10);
5244
});
5345
});
5446

@@ -73,7 +65,7 @@ describe("PositionStorage with local editor", () => {
7365
);
7466

7567
// Start tracking
76-
positionStorage.set("test-id", 10);
68+
const getPos = positionStorage.track(10);
7769

7870
// Move the cursor to the start of the document
7971
editor.setTextCursorPosition(editor.document[0], "start");
@@ -88,7 +80,7 @@ describe("PositionStorage with local editor", () => {
8880
]);
8981

9082
// Position should be updated according to mapping
91-
expect(positionStorage.get("test-id")).toBe(14);
83+
expect(getPos()).toBe(14);
9284
});
9385

9486
it("should not update mapping for local transactions after the position", () => {
@@ -111,7 +103,7 @@ describe("PositionStorage with local editor", () => {
111103
"before"
112104
);
113105
// Start tracking
114-
positionStorage.set("test-id", 10);
106+
const getPos = positionStorage.track(10);
115107

116108
// Move the cursor to the end of the document
117109
editor.setTextCursorPosition(editor.document[0], "end");
@@ -126,7 +118,7 @@ describe("PositionStorage with local editor", () => {
126118
]);
127119

128120
// Position should not be updated
129-
expect(positionStorage.get("test-id")).toBe(10);
121+
expect(getPos()).toBe(10);
130122
});
131123

132124
it("should track positions on each side", () => {
@@ -138,21 +130,20 @@ describe("PositionStorage with local editor", () => {
138130
]);
139131

140132
// Store position at "Hello| World"
141-
positionStorage.set("cursor", 6);
142-
positionStorage.set("start", 3);
143-
positionStorage.set("start-right", 3, "right");
144-
positionStorage.set("pos-after", 4);
145-
positionStorage.set("pos-after-right", 4, "right");
146-
133+
const getCursorPos = positionStorage.track(6);
134+
const getStartPos = positionStorage.track(3);
135+
const getStartRightPos = positionStorage.track(3, "right");
136+
const getPosAfterPos = positionStorage.track(4);
137+
const getPosAfterRightPos = positionStorage.track(4, "right");
147138
// Insert text at the beginning
148139
editor._tiptapEditor.commands.insertContentAt(3, "Test ");
149140

150141
// Position should be updated
151-
expect(positionStorage.get("cursor")).toBe(11); // 6 + 5 ("Test " length)
152-
expect(positionStorage.get("start")).toBe(3); // 3
153-
expect(positionStorage.get("start-right")).toBe(8); // 3 + 5 ("Test " length)
154-
expect(positionStorage.get("pos-after")).toBe(9); // 4 + 5 ("Test " length)
155-
expect(positionStorage.get("pos-after-right")).toBe(9); // 4 + 5 ("Test " length)
142+
expect(getCursorPos()).toBe(11); // 6 + 5 ("Test " length)
143+
expect(getStartPos()).toBe(3); // 3
144+
expect(getStartRightPos()).toBe(8); // 3 + 5 ("Test " length)
145+
expect(getPosAfterPos()).toBe(9); // 4 + 5 ("Test " length)
146+
expect(getPosAfterRightPos()).toBe(9); // 4 + 5 ("Test " length)
156147
});
157148

158149
it("should handle multiple transactions", () => {
@@ -164,11 +155,11 @@ describe("PositionStorage with local editor", () => {
164155
]);
165156

166157
// Store position at "Hello| World"
167-
positionStorage.set("cursor", 6);
168-
positionStorage.set("start", 3);
169-
positionStorage.set("start-right", 3, "right");
170-
positionStorage.set("pos-after", 4);
171-
positionStorage.set("pos-after-right", 4, "right");
158+
const getCursorPos = positionStorage.track(6);
159+
const getStartPos = positionStorage.track(3);
160+
const getStartRightPos = positionStorage.track(3, "right");
161+
const getPosAfterPos = positionStorage.track(4);
162+
const getPosAfterRightPos = positionStorage.track(4, "right");
172163

173164
// Insert text at the beginning
174165
editor._tiptapEditor.commands.insertContentAt(3, "T");
@@ -178,11 +169,11 @@ describe("PositionStorage with local editor", () => {
178169
editor._tiptapEditor.commands.insertContentAt(7, " ");
179170

180171
// Position should be updated
181-
expect(positionStorage.get("cursor")).toBe(11); // 6 + 5 ("Test " length)
182-
expect(positionStorage.get("start")).toBe(3); // 3
183-
expect(positionStorage.get("start-right")).toBe(8); // 3 + 5 ("Test " length)
184-
expect(positionStorage.get("pos-after")).toBe(9); // 4 + 5 ("Test " length)
185-
expect(positionStorage.get("pos-after-right")).toBe(9); // 4 + 5 ("Test " length)
172+
expect(getCursorPos()).toBe(11); // 6 + 5 ("Test " length)
173+
expect(getStartPos()).toBe(3); // 3
174+
expect(getStartRightPos()).toBe(8); // 3 + 5 ("Test " length)
175+
expect(getPosAfterPos()).toBe(9); // 4 + 5 ("Test " length)
176+
expect(getPosAfterRightPos()).toBe(9); // 4 + 5 ("Test " length)
186177
});
187178
});
188179

@@ -268,25 +259,25 @@ describe("PositionStorage with remote editor", () => {
268259
]);
269260

270261
// Store position at "Hello| World"
271-
localPositionStorage.set("cursor", 6);
262+
const getCursorPos = localPositionStorage.track(6);
272263
// Store position at "|Hello World"
273-
localPositionStorage.set("start", 3);
264+
const getStartPos = localPositionStorage.track(3);
274265
// Store position at "|Hello World" (but on the right side)
275-
localPositionStorage.set("start-right", 3, "right");
266+
const getStartRightPos = localPositionStorage.track(3, "right");
276267
// Store position at "H|ello World"
277-
localPositionStorage.set("pos-after", 4);
268+
const getPosAfterPos = localPositionStorage.track(4);
278269
// Store position at "H|ello World" (but on the right side)
279-
localPositionStorage.set("pos-after-right", 4, "right");
270+
const getPosAfterRightPos = localPositionStorage.track(4, "right");
280271

281272
// Insert text at the beginning
282273
localEditor._tiptapEditor.commands.insertContentAt(3, "Test ");
283274

284275
// Position should be updated
285-
expect(localPositionStorage.get("cursor")).toBe(11); // 6 + 5 ("Test " length)
286-
expect(localPositionStorage.get("start")).toBe(3); // 3
287-
expect(localPositionStorage.get("start-right")).toBe(8); // 3 + 5 ("Test " length)
288-
expect(localPositionStorage.get("pos-after")).toBe(9); // 4 + 5 ("Test " length)
289-
expect(localPositionStorage.get("pos-after-right")).toBe(9); // 4 + 5 ("Test " length)
276+
expect(getCursorPos()).toBe(11); // 6 + 5 ("Test " length)
277+
expect(getStartPos()).toBe(3); // 3
278+
expect(getStartRightPos()).toBe(8); // 3 + 5 ("Test " length)
279+
expect(getPosAfterPos()).toBe(9); // 4 + 5 ("Test " length)
280+
expect(getPosAfterRightPos()).toBe(9); // 4 + 5 ("Test " length)
290281
});
291282

292283
it("should handle multiple transactions when collaborating", () => {
@@ -298,15 +289,15 @@ describe("PositionStorage with remote editor", () => {
298289
]);
299290

300291
// Store position at "Hello| World"
301-
localPositionStorage.set("cursor", 6);
292+
const getCursorPos = localPositionStorage.track(6);
302293
// Store position at "|Hello World"
303-
localPositionStorage.set("start", 3);
294+
const getStartPos = localPositionStorage.track(3);
304295
// Store position at "|Hello World" (but on the right side)
305-
localPositionStorage.set("start-right", 3, "right");
296+
const getStartRightPos = localPositionStorage.track(3, "right");
306297
// Store position at "H|ello World"
307-
localPositionStorage.set("pos-after", 4);
298+
const getPosAfterPos = localPositionStorage.track(4);
308299
// Store position at "H|ello World" (but on the right side)
309-
localPositionStorage.set("pos-after-right", 4, "right");
300+
const getPosAfterRightPos = localPositionStorage.track(4, "right");
310301

311302
// Insert text at the beginning
312303
localEditor._tiptapEditor.commands.insertContentAt(3, "T");
@@ -316,11 +307,11 @@ describe("PositionStorage with remote editor", () => {
316307
localEditor._tiptapEditor.commands.insertContentAt(7, " ");
317308

318309
// Position should be updated
319-
expect(localPositionStorage.get("cursor")).toBe(11); // 6 + 5 ("Test " length)
320-
expect(localPositionStorage.get("start")).toBe(3); // 3
321-
expect(localPositionStorage.get("start-right")).toBe(8); // 3 + 5 ("Test " length)
322-
expect(localPositionStorage.get("pos-after")).toBe(9); // 4 + 5 ("Test " length)
323-
expect(localPositionStorage.get("pos-after-right")).toBe(9); // 4 + 5 ("Test " length)
310+
expect(getCursorPos()).toBe(11); // 6 + 5 ("Test " length)
311+
expect(getStartPos()).toBe(3); // 3
312+
expect(getStartRightPos()).toBe(8); // 3 + 5 ("Test " length)
313+
expect(getPosAfterPos()).toBe(9); // 4 + 5 ("Test " length)
314+
expect(getPosAfterRightPos()).toBe(9); // 4 + 5 ("Test " length)
324315
});
325316

326317
it("should update the local position from a remote transaction", () => {
@@ -332,25 +323,25 @@ describe("PositionStorage with remote editor", () => {
332323
]);
333324

334325
// Store position at "Hello| World"
335-
localPositionStorage.set("cursor", 6);
326+
const getCursorPos = localPositionStorage.track(6);
336327
// Store position at "|Hello World"
337-
localPositionStorage.set("start", 3);
328+
const getStartPos = localPositionStorage.track(3);
338329
// Store position at "|Hello World" (but on the right side)
339-
localPositionStorage.set("start-right", 3, "right");
330+
const getStartRightPos = localPositionStorage.track(3, "right");
340331
// Store position at "H|ello World"
341-
localPositionStorage.set("pos-after", 4);
332+
const getPosAfterPos = localPositionStorage.track(4);
342333
// Store position at "H|ello World" (but on the right side)
343-
localPositionStorage.set("pos-after-right", 4, "right");
334+
const getPosAfterRightPos = localPositionStorage.track(4, "right");
344335

345336
// Insert text at the beginning
346337
localEditor._tiptapEditor.commands.insertContentAt(3, "Test ");
347338

348339
// Position should be updated
349-
expect(localPositionStorage.get("cursor")).toBe(11); // 6 + 5 ("Test " length)
350-
expect(localPositionStorage.get("start")).toBe(3); // 3
351-
expect(localPositionStorage.get("start-right")).toBe(8); // 3 + 5 ("Test " length)
352-
expect(localPositionStorage.get("pos-after")).toBe(9); // 4 + 5 ("Test " length)
353-
expect(localPositionStorage.get("pos-after-right")).toBe(9); // 4 + 5 ("Test " length)
340+
expect(getCursorPos()).toBe(11); // 6 + 5 ("Test " length)
341+
expect(getStartPos()).toBe(3); // 3
342+
expect(getStartRightPos()).toBe(8); // 3 + 5 ("Test " length)
343+
expect(getPosAfterPos()).toBe(9); // 4 + 5 ("Test " length)
344+
expect(getPosAfterRightPos()).toBe(9); // 4 + 5 ("Test " length)
354345
});
355346

356347
it("should update the remote position from a remote transaction", () => {
@@ -362,25 +353,25 @@ describe("PositionStorage with remote editor", () => {
362353
]);
363354

364355
// Store position at "Hello| World"
365-
remotePositionStorage.set("cursor", 6);
356+
const getCursorPos = remotePositionStorage.track(6);
366357
// Store position at "|Hello World"
367-
remotePositionStorage.set("start", 3);
358+
const getStartPos = remotePositionStorage.track(3);
368359
// Store position at "|Hello World" (but on the right side)
369-
remotePositionStorage.set("start-right", 3, "right");
360+
const getStartRightPos = remotePositionStorage.track(3, "right");
370361
// Store position at "H|ello World"
371-
remotePositionStorage.set("pos-after", 4);
362+
const getPosAfterPos = remotePositionStorage.track(4);
372363
// Store position at "H|ello World" (but on the right side)
373-
remotePositionStorage.set("pos-after-right", 4, "right");
364+
const getPosAfterRightPos = remotePositionStorage.track(4, "right");
374365

375366
// Insert text at the beginning
376367
localEditor._tiptapEditor.commands.insertContentAt(3, "Test ");
377368

378369
// Position should be updated
379-
expect(remotePositionStorage.get("cursor")).toBe(11); // 6 + 5 ("Test " length)
380-
expect(remotePositionStorage.get("start")).toBe(3); // 3
381-
expect(remotePositionStorage.get("start-right")).toBe(8); // 3 + 5 ("Test " length)
382-
expect(remotePositionStorage.get("pos-after")).toBe(9); // 4 + 5 ("Test " length)
383-
expect(remotePositionStorage.get("pos-after-right")).toBe(9); // 4 + 5 ("Test " length)
370+
expect(getCursorPos()).toBe(11); // 6 + 5 ("Test " length)
371+
expect(getStartPos()).toBe(3); // 3
372+
expect(getStartRightPos()).toBe(8); // 3 + 5 ("Test " length)
373+
expect(getPosAfterPos()).toBe(9); // 4 + 5 ("Test " length)
374+
expect(getPosAfterRightPos()).toBe(9); // 4 + 5 ("Test " length)
384375
});
385376
});
386377
});

0 commit comments

Comments
 (0)