|
|
@ -1,15 +1,13 @@
|
|
|
|
import { PRECEDING_ELEMENT_KEY } from "../../src/constants";
|
|
|
|
|
|
|
|
import { ExcalidrawElement } from "../../src/element/types";
|
|
|
|
import { ExcalidrawElement } from "../../src/element/types";
|
|
|
|
import { AppState } from "../../src/types";
|
|
|
|
import { AppState } from "../../src/types";
|
|
|
|
import { arrayToMapWithIndex } from "../../src/utils";
|
|
|
|
import { arrayToMap, arrayToMapWithIndex } from "../../src/utils";
|
|
|
|
|
|
|
|
import { orderByFractionalIndex } from "../../src/zindex";
|
|
|
|
|
|
|
|
|
|
|
|
export type ReconciledElements = readonly ExcalidrawElement[] & {
|
|
|
|
export type ReconciledElements = readonly ExcalidrawElement[] & {
|
|
|
|
_brand: "reconciledElements";
|
|
|
|
_brand: "reconciledElements";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export type BroadcastedExcalidrawElement = ExcalidrawElement & {
|
|
|
|
export type BroadcastedExcalidrawElement = ExcalidrawElement;
|
|
|
|
[PRECEDING_ELEMENT_KEY]?: string;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const shouldDiscardRemoteElement = (
|
|
|
|
const shouldDiscardRemoteElement = (
|
|
|
|
localAppState: AppState,
|
|
|
|
localAppState: AppState,
|
|
|
@ -39,116 +37,43 @@ export const reconcileElements = (
|
|
|
|
remoteElements: readonly BroadcastedExcalidrawElement[],
|
|
|
|
remoteElements: readonly BroadcastedExcalidrawElement[],
|
|
|
|
localAppState: AppState,
|
|
|
|
localAppState: AppState,
|
|
|
|
): ReconciledElements => {
|
|
|
|
): ReconciledElements => {
|
|
|
|
const localElementsData =
|
|
|
|
const localElementsData = arrayToMap(localElements);
|
|
|
|
arrayToMapWithIndex<ExcalidrawElement>(localElements);
|
|
|
|
const reconciledElements: ExcalidrawElement[] = [];
|
|
|
|
|
|
|
|
const added = new Set<string>();
|
|
|
|
const reconciledElements: ExcalidrawElement[] = localElements.slice();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const duplicates = new WeakMap<ExcalidrawElement, true>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let cursor = 0;
|
|
|
|
// process remote elements
|
|
|
|
let offset = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let remoteElementIdx = -1;
|
|
|
|
|
|
|
|
for (const remoteElement of remoteElements) {
|
|
|
|
for (const remoteElement of remoteElements) {
|
|
|
|
remoteElementIdx++;
|
|
|
|
if (localElementsData.has(remoteElement.id)) {
|
|
|
|
|
|
|
|
const localElement = localElementsData.get(remoteElement.id);
|
|
|
|
const local = localElementsData.get(remoteElement.id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (shouldDiscardRemoteElement(localAppState, local?.[0], remoteElement)) {
|
|
|
|
if (
|
|
|
|
if (remoteElement[PRECEDING_ELEMENT_KEY]) {
|
|
|
|
localElement &&
|
|
|
|
delete remoteElement[PRECEDING_ELEMENT_KEY];
|
|
|
|
shouldDiscardRemoteElement(localAppState, localElement, remoteElement)
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Mark duplicate for removal as it'll be replaced with the remote element
|
|
|
|
|
|
|
|
if (local) {
|
|
|
|
|
|
|
|
// Unless the remote and local elements are the same element in which case
|
|
|
|
|
|
|
|
// we need to keep it as we'd otherwise discard it from the resulting
|
|
|
|
|
|
|
|
// array.
|
|
|
|
|
|
|
|
if (local[0] === remoteElement) {
|
|
|
|
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
duplicates.set(local[0], true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// parent may not be defined in case the remote client is running an older
|
|
|
|
|
|
|
|
// excalidraw version
|
|
|
|
|
|
|
|
const parent =
|
|
|
|
|
|
|
|
remoteElement[PRECEDING_ELEMENT_KEY] ||
|
|
|
|
|
|
|
|
remoteElements[remoteElementIdx - 1]?.id ||
|
|
|
|
|
|
|
|
null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (parent != null) {
|
|
|
|
|
|
|
|
delete remoteElement[PRECEDING_ELEMENT_KEY];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ^ indicates the element is the first in elements array
|
|
|
|
|
|
|
|
if (parent === "^") {
|
|
|
|
|
|
|
|
offset++;
|
|
|
|
|
|
|
|
if (cursor === 0) {
|
|
|
|
|
|
|
|
reconciledElements.unshift(remoteElement);
|
|
|
|
|
|
|
|
localElementsData.set(remoteElement.id, [
|
|
|
|
|
|
|
|
remoteElement,
|
|
|
|
|
|
|
|
cursor - offset,
|
|
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
reconciledElements.splice(cursor + 1, 0, remoteElement);
|
|
|
|
|
|
|
|
localElementsData.set(remoteElement.id, [
|
|
|
|
|
|
|
|
remoteElement,
|
|
|
|
|
|
|
|
cursor + 1 - offset,
|
|
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
cursor++;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
let idx = localElementsData.has(parent)
|
|
|
|
if (!added.has(remoteElement.id)) {
|
|
|
|
? localElementsData.get(parent)![1]
|
|
|
|
|
|
|
|
: null;
|
|
|
|
|
|
|
|
if (idx != null) {
|
|
|
|
|
|
|
|
idx += offset;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idx != null && idx >= cursor) {
|
|
|
|
|
|
|
|
reconciledElements.splice(idx + 1, 0, remoteElement);
|
|
|
|
|
|
|
|
offset++;
|
|
|
|
|
|
|
|
localElementsData.set(remoteElement.id, [
|
|
|
|
|
|
|
|
remoteElement,
|
|
|
|
|
|
|
|
idx + 1 - offset,
|
|
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
cursor = idx + 1;
|
|
|
|
|
|
|
|
} else if (idx != null) {
|
|
|
|
|
|
|
|
reconciledElements.splice(cursor + 1, 0, remoteElement);
|
|
|
|
|
|
|
|
offset++;
|
|
|
|
|
|
|
|
localElementsData.set(remoteElement.id, [
|
|
|
|
|
|
|
|
remoteElement,
|
|
|
|
|
|
|
|
cursor + 1 - offset,
|
|
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
cursor++;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
reconciledElements.push(remoteElement);
|
|
|
|
reconciledElements.push(remoteElement);
|
|
|
|
localElementsData.set(remoteElement.id, [
|
|
|
|
added.add(remoteElement.id);
|
|
|
|
remoteElement,
|
|
|
|
|
|
|
|
reconciledElements.length - 1 - offset,
|
|
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// no parent z-index information, local element exists → replace in place
|
|
|
|
|
|
|
|
} else if (local) {
|
|
|
|
|
|
|
|
reconciledElements[local[1]] = remoteElement;
|
|
|
|
|
|
|
|
localElementsData.set(remoteElement.id, [remoteElement, local[1]]);
|
|
|
|
|
|
|
|
// otherwise push to the end
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
reconciledElements.push(remoteElement);
|
|
|
|
if (!added.has(remoteElement.id)) {
|
|
|
|
localElementsData.set(remoteElement.id, [
|
|
|
|
reconciledElements.push(remoteElement);
|
|
|
|
remoteElement,
|
|
|
|
added.add(remoteElement.id);
|
|
|
|
reconciledElements.length - 1 - offset,
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const ret: readonly ExcalidrawElement[] = reconciledElements.filter(
|
|
|
|
// process local elements
|
|
|
|
(element) => !duplicates.has(element),
|
|
|
|
for (const localElement of localElements) {
|
|
|
|
);
|
|
|
|
if (!added.has(localElement.id)) {
|
|
|
|
|
|
|
|
reconciledElements.push(localElement);
|
|
|
|
|
|
|
|
added.add(localElement.id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ret as ReconciledElements;
|
|
|
|
return orderByFractionalIndex(
|
|
|
|
|
|
|
|
reconciledElements,
|
|
|
|
|
|
|
|
) as readonly ExcalidrawElement[] as ReconciledElements;
|
|
|
|
};
|
|
|
|
};
|
|
|
|