aboutsummaryrefslogtreecommitdiffstats
path: root/packages/excalidraw/actions/actionAddToLibrary.ts
diff options
context:
space:
mode:
authorkj_sh6042026-03-15 16:19:35 -0400
committerkj_sh6042026-03-15 16:19:35 -0400
commit6ec259a0e71174651bae95d4628138bf6fd68742 (patch)
tree5e33c6a5ec091ecabfcb257fdc7b6a88ed8754ac /packages/excalidraw/actions/actionAddToLibrary.ts
parent16c8578b15c727f22921f8a80a56ee4d4e7f2272 (diff)
refactor: packages/
Diffstat (limited to 'packages/excalidraw/actions/actionAddToLibrary.ts')
-rw-r--r--packages/excalidraw/actions/actionAddToLibrary.ts63
1 files changed, 63 insertions, 0 deletions
diff --git a/packages/excalidraw/actions/actionAddToLibrary.ts b/packages/excalidraw/actions/actionAddToLibrary.ts
new file mode 100644
index 0000000..3186e3b
--- /dev/null
+++ b/packages/excalidraw/actions/actionAddToLibrary.ts
@@ -0,0 +1,63 @@
+import { register } from "./register";
+import { deepCopyElement } from "../element/newElement";
+import { randomId } from "../random";
+import { t } from "../i18n";
+import { LIBRARY_DISABLED_TYPES } from "../constants";
+import { CaptureUpdateAction } from "../store";
+
+export const actionAddToLibrary = register({
+ name: "addToLibrary",
+ trackEvent: { category: "element" },
+ perform: (elements, appState, _, app) => {
+ const selectedElements = app.scene.getSelectedElements({
+ selectedElementIds: appState.selectedElementIds,
+ includeBoundTextElement: true,
+ includeElementsInFrames: true,
+ });
+
+ for (const type of LIBRARY_DISABLED_TYPES) {
+ if (selectedElements.some((element) => element.type === type)) {
+ return {
+ captureUpdate: CaptureUpdateAction.EVENTUALLY,
+ appState: {
+ ...appState,
+ errorMessage: t(`errors.libraryElementTypeError.${type}`),
+ },
+ };
+ }
+ }
+
+ return app.library
+ .getLatestLibrary()
+ .then((items) => {
+ return app.library.setLibrary([
+ {
+ id: randomId(),
+ status: "unpublished",
+ elements: selectedElements.map(deepCopyElement),
+ created: Date.now(),
+ },
+ ...items,
+ ]);
+ })
+ .then(() => {
+ return {
+ captureUpdate: CaptureUpdateAction.EVENTUALLY,
+ appState: {
+ ...appState,
+ toast: { message: t("toast.addedToLibrary") },
+ },
+ };
+ })
+ .catch((error) => {
+ return {
+ captureUpdate: CaptureUpdateAction.EVENTUALLY,
+ appState: {
+ ...appState,
+ errorMessage: error.message,
+ },
+ };
+ });
+ },
+ label: "labels.addToLibrary",
+});