Fixing most tests for now

This commit is contained in:
Marcel Mraz 2025-04-16 16:26:40 +02:00
parent d9b96e8f6c
commit d17c29358b
No known key found for this signature in database
GPG key ID: 4EBD6E62DC830CD2
16 changed files with 111 additions and 96 deletions

View file

@ -12,6 +12,7 @@ import { isFrameLikeElement } from "@excalidraw/element/typeChecks";
import { getElementsInGroup } from "@excalidraw/element/groups";
import {
orderByFractionalIndex,
syncInvalidIndices,
syncMovedIndices,
validateFractionalIndices,
@ -34,7 +35,6 @@ import type {
NonDeletedSceneElementsMap,
OrderedExcalidrawElement,
Ordered,
ElementsMap,
} from "@excalidraw/element/types";
import type {
@ -167,9 +167,9 @@ class Scene {
return this.frames;
}
constructor(elementsMap: ElementsMap | null = null) {
if (elementsMap) {
this.replaceAllElements(elementsMap);
constructor(elements: ElementsMapOrArray | null = null) {
if (elements) {
this.replaceAllElements(elements);
}
}
@ -267,16 +267,19 @@ class Scene {
}
replaceAllElements(nextElements: ElementsMapOrArray) {
const _nextElements =
// ts doesn't like `Array.isArray` of `instanceof Map`
nextElements instanceof Array
? nextElements
: Array.from(nextElements.values());
// ts doesn't like `Array.isArray` of `instanceof Map`
if (!(nextElements instanceof Array)) {
// need to order by fractional indices to get the correct order
nextElements = orderByFractionalIndex(
Array.from(nextElements.values()) as OrderedExcalidrawElement[],
);
}
const nextFrameLikes: ExcalidrawFrameLikeElement[] = [];
validateIndicesThrottled(_nextElements);
validateIndicesThrottled(nextElements);
this.elements = syncInvalidIndices(_nextElements);
this.elements = syncInvalidIndices(nextElements);
this.elementsMap.clear();
this.elements.forEach((element) => {
if (isFrameLikeElement(element)) {
@ -439,7 +442,18 @@ class Scene {
) {
const elementsMap = this.getNonDeletedElementsMap();
mutateElement(element, elementsMap, updates, options);
const { version: prevVersion } = element;
const { version: nextVersion } = mutateElement(
element,
elementsMap,
updates,
options,
);
// skip triggerUpdate if the element version hasn't changed
if (prevVersion === nextVersion) {
return element;
}
if (options.informMutation) {
this.triggerUpdate();

View file

@ -156,6 +156,7 @@ export const syncMovedIndices = (
movedElements: ElementsMap,
): OrderedExcalidrawElement[] => {
try {
const elementsMap = arrayToMap(elements);
const indicesGroups = getMovedIndicesGroups(elements, movedElements);
// try generatating indices, throws on invalid movedElements
@ -177,7 +178,7 @@ export const syncMovedIndices = (
// split mutation so we don't end up in an incosistent state
for (const [element, update] of elementsUpdates) {
mutateElement(element, arrayToMap(elements), update);
mutateElement(element, elementsMap, update);
}
} catch (e) {
// fallback to default sync
@ -195,10 +196,12 @@ export const syncMovedIndices = (
export const syncInvalidIndices = (
elements: readonly ExcalidrawElement[],
): OrderedExcalidrawElement[] => {
const elementsMap = arrayToMap(elements);
const indicesGroups = getInvalidIndicesGroups(elements);
const elementsUpdates = generateIndices(elements, indicesGroups);
for (const [element, update] of elementsUpdates) {
mutateElement(element, arrayToMap(elements), update);
mutateElement(element, elementsMap, update);
}
return elements as OrderedExcalidrawElement[];

View file

@ -8,7 +8,7 @@ import {
isPrimitive,
} from "@excalidraw/common";
import { Excalidraw } from "@excalidraw/excalidraw";
import { Excalidraw, mutateElement } from "@excalidraw/excalidraw";
import { actionDuplicateSelection } from "@excalidraw/excalidraw/actions";
@ -62,7 +62,7 @@ describe("duplicating single elements", () => {
// @ts-ignore
element.__proto__ = { hello: "world" };
h.app.scene.mutateElement(element, {
mutateElement(element, new Map(), {
points: [pointFrom<LocalPoint>(1, 2), pointFrom<LocalPoint>(3, 4)],
});

View file

@ -1,5 +1,7 @@
import { API } from "@excalidraw/excalidraw/tests/helpers/api";
import { mutateElement } from "@excalidraw/element/mutateElement";
import { normalizeElementOrder } from "../src/sortElements";
import type { ExcalidrawElement } from "../src/types";
@ -35,7 +37,7 @@ describe("normalizeElementsOrder", () => {
boundElements: [],
});
h.app.scene.mutateElement(container, {
mutateElement(container, new Map(), {
boundElements: [{ type: "text", id: boundText.id }],
});