
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
233 lines
7.1 KiB
TypeScript
233 lines
7.1 KiB
TypeScript
import { alignElements, Alignment } from "../align";
|
|
import {
|
|
AlignBottomIcon,
|
|
AlignLeftIcon,
|
|
AlignRightIcon,
|
|
AlignTopIcon,
|
|
CenterHorizontallyIcon,
|
|
CenterVerticallyIcon,
|
|
} from "../components/icons";
|
|
import { ToolButton } from "../components/ToolButton";
|
|
import { getNonDeletedElements } from "../element";
|
|
import { isFrameLikeElement } from "../element/typeChecks";
|
|
import { ExcalidrawElement } from "../element/types";
|
|
import { updateFrameMembershipOfSelectedElements } from "../frame";
|
|
import { t } from "../i18n";
|
|
import { KEYS } from "../keys";
|
|
import { isSomeElementSelected } from "../scene";
|
|
import { AppClassProperties, AppState } from "../types";
|
|
import { arrayToMap, getShortcutKey } from "../utils";
|
|
import { register } from "./register";
|
|
import { StoreAction } from "./types";
|
|
|
|
const alignActionsPredicate = (
|
|
elements: readonly ExcalidrawElement[],
|
|
appState: AppState,
|
|
_: unknown,
|
|
app: AppClassProperties,
|
|
) => {
|
|
const selectedElements = app.scene.getSelectedElements(appState);
|
|
return (
|
|
selectedElements.length > 1 &&
|
|
// TODO enable aligning frames when implemented properly
|
|
!selectedElements.some((el) => isFrameLikeElement(el))
|
|
);
|
|
};
|
|
|
|
const alignSelectedElements = (
|
|
elements: readonly ExcalidrawElement[],
|
|
appState: Readonly<AppState>,
|
|
app: AppClassProperties,
|
|
alignment: Alignment,
|
|
) => {
|
|
const selectedElements = app.scene.getSelectedElements(appState);
|
|
|
|
const updatedElements = alignElements(selectedElements, alignment);
|
|
|
|
const updatedElementsMap = arrayToMap(updatedElements);
|
|
|
|
return updateFrameMembershipOfSelectedElements(
|
|
elements.map((element) => updatedElementsMap.get(element.id) || element),
|
|
appState,
|
|
app,
|
|
);
|
|
};
|
|
|
|
export const actionAlignTop = register({
|
|
name: "alignTop",
|
|
trackEvent: { category: "element" },
|
|
predicate: alignActionsPredicate,
|
|
perform: (elements, appState, _, app) => {
|
|
return {
|
|
appState,
|
|
elements: alignSelectedElements(elements, appState, app, {
|
|
position: "start",
|
|
axis: "y",
|
|
}),
|
|
storeAction: StoreAction.CAPTURE,
|
|
};
|
|
},
|
|
keyTest: (event) =>
|
|
event[KEYS.CTRL_OR_CMD] && event.shiftKey && event.key === KEYS.ARROW_UP,
|
|
PanelComponent: ({ elements, appState, updateData, app }) => (
|
|
<ToolButton
|
|
hidden={!alignActionsPredicate(elements, appState, null, app)}
|
|
type="button"
|
|
icon={AlignTopIcon}
|
|
onClick={() => updateData(null)}
|
|
title={`${t("labels.alignTop")} — ${getShortcutKey(
|
|
"CtrlOrCmd+Shift+Up",
|
|
)}`}
|
|
aria-label={t("labels.alignTop")}
|
|
visible={isSomeElementSelected(getNonDeletedElements(elements), appState)}
|
|
/>
|
|
),
|
|
});
|
|
|
|
export const actionAlignBottom = register({
|
|
name: "alignBottom",
|
|
trackEvent: { category: "element" },
|
|
predicate: alignActionsPredicate,
|
|
perform: (elements, appState, _, app) => {
|
|
return {
|
|
appState,
|
|
elements: alignSelectedElements(elements, appState, app, {
|
|
position: "end",
|
|
axis: "y",
|
|
}),
|
|
storeAction: StoreAction.CAPTURE,
|
|
};
|
|
},
|
|
keyTest: (event) =>
|
|
event[KEYS.CTRL_OR_CMD] && event.shiftKey && event.key === KEYS.ARROW_DOWN,
|
|
PanelComponent: ({ elements, appState, updateData, app }) => (
|
|
<ToolButton
|
|
hidden={!alignActionsPredicate(elements, appState, null, app)}
|
|
type="button"
|
|
icon={AlignBottomIcon}
|
|
onClick={() => updateData(null)}
|
|
title={`${t("labels.alignBottom")} — ${getShortcutKey(
|
|
"CtrlOrCmd+Shift+Down",
|
|
)}`}
|
|
aria-label={t("labels.alignBottom")}
|
|
visible={isSomeElementSelected(getNonDeletedElements(elements), appState)}
|
|
/>
|
|
),
|
|
});
|
|
|
|
export const actionAlignLeft = register({
|
|
name: "alignLeft",
|
|
trackEvent: { category: "element" },
|
|
predicate: alignActionsPredicate,
|
|
perform: (elements, appState, _, app) => {
|
|
return {
|
|
appState,
|
|
elements: alignSelectedElements(elements, appState, app, {
|
|
position: "start",
|
|
axis: "x",
|
|
}),
|
|
storeAction: StoreAction.CAPTURE,
|
|
};
|
|
},
|
|
keyTest: (event) =>
|
|
event[KEYS.CTRL_OR_CMD] && event.shiftKey && event.key === KEYS.ARROW_LEFT,
|
|
PanelComponent: ({ elements, appState, updateData, app }) => (
|
|
<ToolButton
|
|
hidden={!alignActionsPredicate(elements, appState, null, app)}
|
|
type="button"
|
|
icon={AlignLeftIcon}
|
|
onClick={() => updateData(null)}
|
|
title={`${t("labels.alignLeft")} — ${getShortcutKey(
|
|
"CtrlOrCmd+Shift+Left",
|
|
)}`}
|
|
aria-label={t("labels.alignLeft")}
|
|
visible={isSomeElementSelected(getNonDeletedElements(elements), appState)}
|
|
/>
|
|
),
|
|
});
|
|
|
|
export const actionAlignRight = register({
|
|
name: "alignRight",
|
|
trackEvent: { category: "element" },
|
|
predicate: alignActionsPredicate,
|
|
perform: (elements, appState, _, app) => {
|
|
return {
|
|
appState,
|
|
elements: alignSelectedElements(elements, appState, app, {
|
|
position: "end",
|
|
axis: "x",
|
|
}),
|
|
storeAction: StoreAction.CAPTURE,
|
|
};
|
|
},
|
|
keyTest: (event) =>
|
|
event[KEYS.CTRL_OR_CMD] && event.shiftKey && event.key === KEYS.ARROW_RIGHT,
|
|
PanelComponent: ({ elements, appState, updateData, app }) => (
|
|
<ToolButton
|
|
hidden={!alignActionsPredicate(elements, appState, null, app)}
|
|
type="button"
|
|
icon={AlignRightIcon}
|
|
onClick={() => updateData(null)}
|
|
title={`${t("labels.alignRight")} — ${getShortcutKey(
|
|
"CtrlOrCmd+Shift+Right",
|
|
)}`}
|
|
aria-label={t("labels.alignRight")}
|
|
visible={isSomeElementSelected(getNonDeletedElements(elements), appState)}
|
|
/>
|
|
),
|
|
});
|
|
|
|
export const actionAlignVerticallyCentered = register({
|
|
name: "alignVerticallyCentered",
|
|
trackEvent: { category: "element" },
|
|
predicate: alignActionsPredicate,
|
|
perform: (elements, appState, _, app) => {
|
|
return {
|
|
appState,
|
|
elements: alignSelectedElements(elements, appState, app, {
|
|
position: "center",
|
|
axis: "y",
|
|
}),
|
|
storeAction: StoreAction.CAPTURE,
|
|
};
|
|
},
|
|
PanelComponent: ({ elements, appState, updateData, app }) => (
|
|
<ToolButton
|
|
hidden={!alignActionsPredicate(elements, appState, null, app)}
|
|
type="button"
|
|
icon={CenterVerticallyIcon}
|
|
onClick={() => updateData(null)}
|
|
title={t("labels.centerVertically")}
|
|
aria-label={t("labels.centerVertically")}
|
|
visible={isSomeElementSelected(getNonDeletedElements(elements), appState)}
|
|
/>
|
|
),
|
|
});
|
|
|
|
export const actionAlignHorizontallyCentered = register({
|
|
name: "alignHorizontallyCentered",
|
|
trackEvent: { category: "element" },
|
|
predicate: alignActionsPredicate,
|
|
perform: (elements, appState, _, app) => {
|
|
return {
|
|
appState,
|
|
elements: alignSelectedElements(elements, appState, app, {
|
|
position: "center",
|
|
axis: "x",
|
|
}),
|
|
storeAction: StoreAction.CAPTURE,
|
|
};
|
|
},
|
|
PanelComponent: ({ elements, appState, updateData, app }) => (
|
|
<ToolButton
|
|
hidden={!alignActionsPredicate(elements, appState, null, app)}
|
|
type="button"
|
|
icon={CenterHorizontallyIcon}
|
|
onClick={() => updateData(null)}
|
|
title={t("labels.centerHorizontally")}
|
|
aria-label={t("labels.centerHorizontally")}
|
|
visible={isSomeElementSelected(getNonDeletedElements(elements), appState)}
|
|
/>
|
|
),
|
|
});
|