
Introducing independent change detection for appState and elements Generalizing object change, cleanup, refactoring, comments, solving typing issues Shaping increment, change, delta hierarchy Structural clone of elements Introducing store and incremental API Disabling buttons for canvas actions, smaller store and changes improvements Update history entry based on latest changes, iterate through the stack for visible changes to limit empty commands Solving concurrency issues, solving (partly) linear element issues, introducing commitToStore breaking change Fixing existing tests, updating snapshots Trying to be smarter on the appstate change detection Extending collab test, refactoring action / updateScene params, bugfixes Resetting snapshots Resetting snapshots UI / API tests for history - WIP Changing actions related to the observed appstate to at least update the store snapshot - WIP Adding skipping of snapshot update flag for most no-breaking changes compatible solution Ignoring uncomitted elements from local async actions, updating store directly in updateScene Bound element issues - WIP
218 lines
6.5 KiB
TypeScript
218 lines
6.5 KiB
TypeScript
import { KEYS } from "../keys";
|
|
import { isInvisiblySmallElement } from "../element";
|
|
import { updateActiveTool } from "../utils";
|
|
import { ToolButton } from "../components/ToolButton";
|
|
import { done } from "../components/icons";
|
|
import { t } from "../i18n";
|
|
import { register } from "./register";
|
|
import { mutateElement } from "../element/mutateElement";
|
|
import { isPathALoop } from "../math";
|
|
import { LinearElementEditor } from "../element/linearElementEditor";
|
|
import Scene from "../scene/Scene";
|
|
import {
|
|
maybeBindLinearElement,
|
|
bindOrUnbindLinearElement,
|
|
} from "../element/binding";
|
|
import { isBindingElement, isLinearElement } from "../element/typeChecks";
|
|
import { AppState } from "../types";
|
|
import { resetCursor } from "../cursor";
|
|
import { StoreAction } from "./types";
|
|
|
|
export const actionFinalize = register({
|
|
name: "finalize",
|
|
trackEvent: false,
|
|
perform: (
|
|
elements,
|
|
appState,
|
|
_,
|
|
{ interactiveCanvas, focusContainer, scene },
|
|
) => {
|
|
if (appState.editingLinearElement) {
|
|
const { elementId, startBindingElement, endBindingElement } =
|
|
appState.editingLinearElement;
|
|
const element = LinearElementEditor.getElement(elementId);
|
|
|
|
if (element) {
|
|
if (isBindingElement(element)) {
|
|
bindOrUnbindLinearElement(
|
|
element,
|
|
startBindingElement,
|
|
endBindingElement,
|
|
);
|
|
}
|
|
return {
|
|
elements:
|
|
element.points.length < 2 || isInvisiblySmallElement(element)
|
|
? elements.filter((el) => el.id !== element.id)
|
|
: undefined,
|
|
appState: {
|
|
...appState,
|
|
cursorButton: "up",
|
|
editingLinearElement: null,
|
|
},
|
|
storeAction: StoreAction.CAPTURE,
|
|
};
|
|
}
|
|
}
|
|
|
|
let newElements = elements;
|
|
|
|
const pendingImageElement =
|
|
appState.pendingImageElementId &&
|
|
scene.getElement(appState.pendingImageElementId);
|
|
|
|
if (pendingImageElement) {
|
|
mutateElement(pendingImageElement, { isDeleted: true }, false);
|
|
}
|
|
|
|
if (window.document.activeElement instanceof HTMLElement) {
|
|
focusContainer();
|
|
}
|
|
|
|
const multiPointElement = appState.multiElement
|
|
? appState.multiElement
|
|
: appState.editingElement?.type === "freedraw"
|
|
? appState.editingElement
|
|
: null;
|
|
|
|
if (multiPointElement) {
|
|
// pen and mouse have hover
|
|
if (
|
|
multiPointElement.type !== "freedraw" &&
|
|
appState.lastPointerDownWith !== "touch"
|
|
) {
|
|
const { points, lastCommittedPoint } = multiPointElement;
|
|
if (
|
|
!lastCommittedPoint ||
|
|
points[points.length - 1] !== lastCommittedPoint
|
|
) {
|
|
mutateElement(multiPointElement, {
|
|
points: multiPointElement.points.slice(0, -1),
|
|
});
|
|
}
|
|
}
|
|
if (isInvisiblySmallElement(multiPointElement)) {
|
|
newElements = newElements.filter(
|
|
(el) => el.id !== multiPointElement.id,
|
|
);
|
|
}
|
|
|
|
// If the multi point line closes the loop,
|
|
// set the last point to first point.
|
|
// This ensures that loop remains closed at different scales.
|
|
const isLoop = isPathALoop(multiPointElement.points, appState.zoom.value);
|
|
if (
|
|
multiPointElement.type === "line" ||
|
|
multiPointElement.type === "freedraw"
|
|
) {
|
|
if (isLoop) {
|
|
const linePoints = multiPointElement.points;
|
|
const firstPoint = linePoints[0];
|
|
mutateElement(multiPointElement, {
|
|
points: linePoints.map((point, index) =>
|
|
index === linePoints.length - 1
|
|
? ([firstPoint[0], firstPoint[1]] as const)
|
|
: point,
|
|
),
|
|
});
|
|
}
|
|
}
|
|
|
|
if (
|
|
isBindingElement(multiPointElement) &&
|
|
!isLoop &&
|
|
multiPointElement.points.length > 1
|
|
) {
|
|
const [x, y] = LinearElementEditor.getPointAtIndexGlobalCoordinates(
|
|
multiPointElement,
|
|
-1,
|
|
);
|
|
maybeBindLinearElement(
|
|
multiPointElement,
|
|
appState,
|
|
Scene.getScene(multiPointElement)!,
|
|
{ x, y },
|
|
);
|
|
}
|
|
}
|
|
|
|
if (
|
|
(!appState.activeTool.locked &&
|
|
appState.activeTool.type !== "freedraw") ||
|
|
!multiPointElement
|
|
) {
|
|
resetCursor(interactiveCanvas);
|
|
}
|
|
|
|
let activeTool: AppState["activeTool"];
|
|
if (appState.activeTool.type === "eraser") {
|
|
activeTool = updateActiveTool(appState, {
|
|
...(appState.activeTool.lastActiveTool || {
|
|
type: "selection",
|
|
}),
|
|
lastActiveToolBeforeEraser: null,
|
|
});
|
|
} else {
|
|
activeTool = updateActiveTool(appState, {
|
|
type: "selection",
|
|
});
|
|
}
|
|
|
|
return {
|
|
elements: newElements,
|
|
appState: {
|
|
...appState,
|
|
cursorButton: "up",
|
|
activeTool:
|
|
(appState.activeTool.locked ||
|
|
appState.activeTool.type === "freedraw") &&
|
|
multiPointElement
|
|
? appState.activeTool
|
|
: activeTool,
|
|
activeEmbeddable: null,
|
|
draggingElement: null,
|
|
multiElement: null,
|
|
editingElement: null,
|
|
startBoundElement: null,
|
|
suggestedBindings: [],
|
|
selectedElementIds:
|
|
multiPointElement &&
|
|
!appState.activeTool.locked &&
|
|
appState.activeTool.type !== "freedraw"
|
|
? {
|
|
...appState.selectedElementIds,
|
|
[multiPointElement.id]: true,
|
|
}
|
|
: appState.selectedElementIds,
|
|
// To select the linear element when user has finished mutipoint editing
|
|
selectedLinearElement:
|
|
multiPointElement && isLinearElement(multiPointElement)
|
|
? new LinearElementEditor(multiPointElement, scene)
|
|
: appState.selectedLinearElement,
|
|
pendingImageElementId: null,
|
|
},
|
|
storeAction:
|
|
appState.activeTool.type === "freedraw"
|
|
? StoreAction.CAPTURE
|
|
: StoreAction.UPDATE,
|
|
};
|
|
},
|
|
keyTest: (event, appState) =>
|
|
(event.key === KEYS.ESCAPE &&
|
|
(appState.editingLinearElement !== null ||
|
|
(!appState.draggingElement && appState.multiElement === null))) ||
|
|
((event.key === KEYS.ESCAPE || event.key === KEYS.ENTER) &&
|
|
appState.multiElement !== null),
|
|
PanelComponent: ({ appState, updateData, data }) => (
|
|
<ToolButton
|
|
type="button"
|
|
icon={done}
|
|
title={t("buttons.done")}
|
|
aria-label={t("buttons.done")}
|
|
onClick={updateData}
|
|
visible={appState.multiElement != null}
|
|
size={data?.size || "medium"}
|
|
/>
|
|
),
|
|
});
|