Skip to content

Commit

Permalink
bump versions on z-index change
Browse files Browse the repository at this point in the history
  • Loading branch information
dwelle committed Mar 20, 2021
1 parent ff78940 commit a9c73eb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
11 changes: 11 additions & 0 deletions src/element/mutateElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,14 @@ export const newElementWith = <TElement extends ExcalidrawElement>(
versionNonce: randomInteger(),
};
};

/**
* Mutates element and updates `version` & `versionNonce`.
*
* NOTE: does not trigger re-render.
*/
export const bumpVersion = (element: Mutable<ExcalidrawElement>) => {
element.version = element.version + 1;
element.versionNonce = randomInteger();
return element;
};
38 changes: 29 additions & 9 deletions src/zindex.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { bumpVersion } from "./element/mutateElement";
import { ExcalidrawElement } from "./element/types";
import { getElementsInGroup } from "./groups";
import { AppState } from "./types";
Expand Down Expand Up @@ -51,7 +52,7 @@ const toContiguousGroups = (array: number[]) => {
*/
const getTargetIndex = (
appState: AppState,
elements: ExcalidrawElement[],
elements: readonly ExcalidrawElement[],
boundaryIndex: number,
direction: "left" | "right",
) => {
Expand Down Expand Up @@ -117,12 +118,24 @@ const getTargetIndex = (
return candidateIndex;
};

const getTargetElementsMap = (
elements: readonly ExcalidrawElement[],
indices: number[],
) => {
return indices.reduce((acc, index) => {
const element = elements[index];
acc[element.id] = element;
return acc;
}, {} as Record<string, ExcalidrawElement>);
};

const shiftElements = (
appState: AppState,
elements: ExcalidrawElement[],
elements: readonly ExcalidrawElement[],
direction: "left" | "right",
) => {
const indicesToMove = getIndicesToMove(elements, appState);
const targetElementsMap = getTargetElementsMap(elements, indicesToMove);
let groupedIndices = toContiguousGroups(indicesToMove);

if (direction === "right") {
Expand Down Expand Up @@ -175,7 +188,12 @@ const shiftElements = (
];
});

return elements;
return elements.map((element) => {
if (targetElementsMap[element.id]) {
return bumpVersion(element);
}
return element;
});
};

const shiftElementsToEnd = (
Expand All @@ -184,7 +202,7 @@ const shiftElementsToEnd = (
direction: "left" | "right",
) => {
const indicesToMove = getIndicesToMove(elements, appState);
const targetElements: ExcalidrawElement[] = [];
const targetElementsMap = getTargetElementsMap(elements, indicesToMove);
const displacedElements: ExcalidrawElement[] = [];

let leadingIndex: number;
Expand Down Expand Up @@ -222,13 +240,15 @@ const shiftElementsToEnd = (
}

for (let index = leadingIndex; index < trailingIndex + 1; index++) {
if (indicesToMove.includes(index)) {
targetElements.push(elements[index]);
} else {
if (!indicesToMove.includes(index)) {
displacedElements.push(elements[index]);
}
}

const targetElements = Object.values(targetElementsMap).map((element) => {
return bumpVersion(element);
});

const leadingElements = elements.slice(0, leadingIndex);
const trailingElements = elements.slice(trailingIndex + 1);

Expand All @@ -254,14 +274,14 @@ export const moveOneLeft = (
elements: readonly ExcalidrawElement[],
appState: AppState,
) => {
return shiftElements(appState, elements.slice(), "left");
return shiftElements(appState, elements, "left");
};

export const moveOneRight = (
elements: readonly ExcalidrawElement[],
appState: AppState,
) => {
return shiftElements(appState, elements.slice(), "right");
return shiftElements(appState, elements, "right");
};

export const moveAllLeft = (
Expand Down

0 comments on commit a9c73eb

Please sign in to comment.