Skip to content

Virtual whiteboard for sketching hand-drawn like diagrams

fix(editor): normalize container + bound text order on restore (#11827)

David Luzarc9c96f619cbc
diff --git a/packages/element/src/sortElements.ts b/packages/element/src/sortElements.ts
index 0f9e8da0f1..581541c2ed 100644
--- a/packages/element/src/sortElements.ts
+++ b/packages/element/src/sortElements.ts
@@ -62,12 +62,12 @@ const defragmentGroups = (elements: readonly ExcalidrawElement[]) => {
* original z-index of container (i.e. it moves bound text elements after
* containers).
*/
-const normalizeBoundElementsOrder = (
- elements: readonly ExcalidrawElement[],
+export const normalizeBoundElementsOrder = <T extends ExcalidrawElement>(
+ elements: readonly T[],
) => {
const elementsMap = arrayToMap(elements);
- const sortedElements = new Set<ExcalidrawElement>();
+ const sortedElements = new Set<T>();
for (const element of elements) {
if (sortedElements.has(element)) {
diff --git a/packages/excalidraw/actions/actionGroup.tsx b/packages/excalidraw/actions/actionGroup.tsx
index 9a21e8277f..0bc711044a 100644
--- a/packages/excalidraw/actions/actionGroup.tsx
+++ b/packages/excalidraw/actions/actionGroup.tsx
@@ -49,16 +49,20 @@ import { register } from "./register";
import type { AppClassProperties, UIAppState } from "../types";
-const allElementsInSameGroup = (elements: readonly ExcalidrawElement[]) => {
+const allElementsInSameGroup = (
+ elements: readonly ExcalidrawElement[],
+ editingGroupId: UIAppState["editingGroupId"],
+) => {
if (elements.length >= 2) {
- const groupIds = elements[0].groupIds;
+ const editingGroupIndex = editingGroupId
+ ? elements[0].groupIds.indexOf(editingGroupId)
+ : -1;
+ const groupIds =
+ editingGroupIndex > -1
+ ? elements[0].groupIds.slice(0, editingGroupIndex)
+ : elements[0].groupIds;
for (const groupId of groupIds) {
- if (
- elements.reduce(
- (acc, element) => acc && isElementInGroup(element, groupId),
- true,
- )
- ) {
+ if (elements.every((element) => isElementInGroup(element, groupId))) {
return true;
}
}
@@ -66,19 +70,15 @@ const allElementsInSameGroup = (elements: readonly ExcalidrawElement[]) => {
return false;
};
-const enableActionGroup = (
- elements: readonly ExcalidrawElement[],
- appState: UIAppState,
- app: AppClassProperties,
-) => {
+const enableActionGroup = (appState: UIAppState, app: AppClassProperties) => {
const selectedElements = app.scene.getSelectedElements({
selectedElementIds: appState.selectedElementIds,
- includeBoundTextElement: true,
+ includeBoundTextElement: false,
});
return (
selectedElements.length >= 2 &&
- !allElementsInSameGroup(selectedElements) &&
+ !allElementsInSameGroup(selectedElements, appState.editingGroupId) &&
!frameAndChildrenSelectedTogether(selectedElements)
);
};
@@ -89,6 +89,10 @@ export const actionGroup = register({
icon: (appState) => <GroupIcon theme={appState.theme} />,
trackEvent: { category: "element" },
perform: (elements, appState, _, app) => {
+ if (!enableActionGroup(appState, app)) {
+ return false;
+ }
+
const selectedElements = getRootElements(
app.scene.getSelectedElements({
selectedElementIds: appState.selectedElementIds,
@@ -194,13 +198,12 @@ export const actionGroup = register({
captureUpdate: CaptureUpdateAction.IMMEDIATELY,
};
},
- predicate: (elements, appState, _, app) =>
- enableActionGroup(elements, appState, app),
+ predicate: (elements, appState, _, app) => enableActionGroup(appState, app),
keyTest: (event) =>
!event.shiftKey && event[KEYS.CTRL_OR_CMD] && event.key === KEYS.G,
PanelComponent: ({ elements, appState, updateData, app }) => (
<IconButton
- hidden={!enableActionGroup(elements, appState, app)}
+ hidden={!enableActionGroup(appState, app)}
type="button"
icon={<GroupIcon theme={appState.theme} />}
onClick={() => updateData(null)}
diff --git a/packages/excalidraw/data/restore.ts b/packages/excalidraw/data/restore.ts
index 2f51abd833..300ee8a3d0 100644
--- a/packages/excalidraw/data/restore.ts
+++ b/packages/excalidraw/data/restore.ts
@@ -51,7 +51,11 @@ import {
isUsingAdaptiveRadius,
} from "@excalidraw/element";
-import { syncInvalidIndices } from "@excalidraw/element";
+import {
+ normalizeBoundElementsOrder,
+ syncInvalidIndices,
+ syncMovedIndices,
+} from "@excalidraw/element";
import { refreshTextDimensions } from "@excalidraw/element";
@@ -798,6 +802,34 @@ const repairBoundElement = (
}
};
+/**
+ * Places bound text directly after its container while preserving the
+ * container's fractional index.
+ *
+ * NOTE mutates indices of reordered bound text elements.
+ */
+const repairBoundTextElementOrder = (
+ elements: readonly ExcalidrawElement[],
+) => {
+ const originalPositions = new Map(
+ elements.map((element, index) => [element.id, index]),
+ );
+ const normalizedElements = normalizeBoundElementsOrder(elements);
+ const reorderedBoundTextElements = normalizedElements.filter(
+ (element, index) =>
+ isTextElement(element) &&
+ element.containerId &&
+ originalPositions.get(element.id) !== index,
+ );
+
+ return reorderedBoundTextElements.length
+ ? syncMovedIndices(
+ normalizedElements,
+ arrayToMap(reorderedBoundTextElements),
+ )
+ : normalizedElements;
+};
+
/**
* Remove an element's frameId if its containing frame is non-existent
*
@@ -931,9 +963,11 @@ export const restoreElements = <T extends ExcalidrawElement>(
}
}
+ const repairedElements = repairBoundTextElementOrder(restoredElements);
+
// NOTE (mtolmacs): Temporary fix for invalid/self-bound elbow arrows
// Need to iterate again so we have attached text nodes in elementsMap
- return restoredElements.map((element) => {
+ return repairedElements.map((element) => {
if (
isElbowArrow(element) &&
!isArrowBoundToElement(element) &&
diff --git a/packages/excalidraw/tests/data/restore.test.ts b/packages/excalidraw/tests/data/restore.test.ts
index 310b001a73..ab113d5aa8 100644
--- a/packages/excalidraw/tests/data/restore.test.ts
+++ b/packages/excalidraw/tests/data/restore.test.ts
@@ -972,6 +972,41 @@ describe("restoreAppState", () => {
});
describe("repairing bindings", () => {
+ it.each(["arrow", "rectangle"] as const)(
+ "should repair bound %s label order and fractional index",
+ (containerType) => {
+ const container = API.createElement({
+ type: containerType,
+ id: "container",
+ index: "b2f" as ExcalidrawElement["index"],
+ boundElements: [{ type: "text", id: "label" }],
+ });
+ const label = API.createElement({
+ type: "text",
+ id: "label",
+ index: "b2a" as ExcalidrawElement["index"],
+ containerId: container.id,
+ });
+
+ const restoredElements = restore.restoreElements(
+ [label, container],
+ null,
+ {
+ repairBindings: true,
+ },
+ );
+
+ expect(restoredElements.map((element) => element.id)).toEqual([
+ container.id,
+ label.id,
+ ]);
+ expect(restoredElements[0].index).toBe(container.index);
+ expect(restoredElements[1].index! > restoredElements[0].index!).toBe(
+ true,
+ );
+ },
+ );
+
it("should strip arrow binding if repair throws", () => {
const container = API.createElement({
type: "rectangle",