fix: incorrectly duplicating items on paste/library insert (#6467

* fix: incorrectly duplicating items on paste/library insert

* fix: deduplicate element ids on restore

* tests
This commit is contained in:
David Luzar 2023-04-16 17:22:16 +02:00 committed by GitHub
parent e7e54814e7
commit f640ddc2aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 153 additions and 43 deletions

View file

@ -13431,7 +13431,7 @@ Object {
"boundElements": null,
"fillStyle": "hachure",
"groupIds": Array [
"id6",
"id4_copy",
],
"height": 10,
"id": "id0_copy",
@ -13464,7 +13464,7 @@ Object {
"boundElements": null,
"fillStyle": "hachure",
"groupIds": Array [
"id6",
"id4_copy",
],
"height": 10,
"id": "id1_copy",
@ -13497,7 +13497,7 @@ Object {
"boundElements": null,
"fillStyle": "hachure",
"groupIds": Array [
"id6",
"id4_copy",
],
"height": 10,
"id": "id2_copy",
@ -13981,7 +13981,7 @@ Object {
"boundElements": null,
"fillStyle": "hachure",
"groupIds": Array [
"id6",
"id4_copy",
],
"height": 10,
"id": "id0_copy",
@ -14011,7 +14011,7 @@ Object {
"boundElements": null,
"fillStyle": "hachure",
"groupIds": Array [
"id6",
"id4_copy",
],
"height": 10,
"id": "id1_copy",
@ -14041,7 +14041,7 @@ Object {
"boundElements": null,
"fillStyle": "hachure",
"groupIds": Array [
"id6",
"id4_copy",
],
"height": 10,
"id": "id2_copy",

View file

@ -211,7 +211,10 @@ export class API {
type,
startArrowhead: null,
endArrowhead: null,
points: rest.points ?? [],
points: rest.points ?? [
[0, 0],
[100, 100],
],
});
break;
case "image":

View file

@ -72,6 +72,100 @@ describe("library", () => {
});
});
it("should regenerate ids but retain bindings on library insert", async () => {
const rectangle = API.createElement({
id: "rectangle1",
type: "rectangle",
boundElements: [
{ type: "text", id: "text1" },
{ type: "arrow", id: "arrow1" },
],
});
const text = API.createElement({
id: "text1",
type: "text",
text: "ola",
containerId: "rectangle1",
});
const arrow = API.createElement({
id: "arrow1",
type: "arrow",
endBinding: { elementId: "rectangle1", focus: -1, gap: 0 },
});
await API.drop(
new Blob(
[
serializeLibraryAsJSON([
{
id: "item1",
status: "published",
elements: [rectangle, text, arrow],
created: 1,
},
]),
],
{
type: MIME_TYPES.excalidrawlib,
},
),
);
await waitFor(() => {
expect(h.elements).toEqual([
expect.objectContaining({
id: "rectangle1_copy",
boundElements: expect.arrayContaining([
{ type: "text", id: "text1_copy" },
{ type: "arrow", id: "arrow1_copy" },
]),
}),
expect.objectContaining({
id: "text1_copy",
containerId: "rectangle1_copy",
}),
expect.objectContaining({
id: "arrow1_copy",
endBinding: expect.objectContaining({ elementId: "rectangle1_copy" }),
}),
]);
});
});
it("should fix duplicate ids between items on insert", async () => {
// note, we're not testing for duplicate group ids and such because
// deduplication of that happens upstream in the library component
// which would be very hard to orchestrate in this test
const elem1 = API.createElement({
id: "elem1",
type: "rectangle",
});
const item1: LibraryItem = {
id: "item1",
status: "published",
elements: [elem1],
created: 1,
};
await API.drop(
new Blob([serializeLibraryAsJSON([item1, item1])], {
type: MIME_TYPES.excalidrawlib,
}),
);
await waitFor(() => {
expect(h.elements).toEqual([
expect.objectContaining({
id: "elem1_copy",
}),
expect.objectContaining({
id: expect.not.stringMatching(/^(elem1_copy|elem1)$/),
}),
]);
});
});
it("inserting library item should revert to selection tool", async () => {
UI.clickTool("rectangle");
expect(h.elements).toEqual([]);