2020-07-10 09:20:23 +00:00
|
|
|
import { register } from "./register";
|
|
|
|
import { deepCopyElement } from "../element/newElement";
|
2021-11-17 18:23:43 +00:00
|
|
|
import { randomId } from "../random";
|
|
|
|
import { t } from "../i18n";
|
2023-07-24 14:51:53 +00:00
|
|
|
import { LIBRARY_DISABLED_TYPES } from "../constants";
|
2020-07-10 09:20:23 +00:00
|
|
|
|
|
|
|
export const actionAddToLibrary = register({
|
|
|
|
name: "addToLibrary",
|
2022-03-28 12:46:40 +00:00
|
|
|
trackEvent: { category: "element" },
|
2021-04-21 18:08:24 +00:00
|
|
|
perform: (elements, appState, _, app) => {
|
2023-07-16 23:09:44 +00:00
|
|
|
const selectedElements = app.scene.getSelectedElements({
|
|
|
|
selectedElementIds: appState.selectedElementIds,
|
|
|
|
includeBoundTextElement: true,
|
|
|
|
includeElementsInFrames: true,
|
|
|
|
});
|
2023-07-24 14:51:53 +00:00
|
|
|
|
|
|
|
for (const type of LIBRARY_DISABLED_TYPES) {
|
|
|
|
if (selectedElements.some((element) => element.type === type)) {
|
|
|
|
return {
|
|
|
|
commitToHistory: false,
|
|
|
|
appState: {
|
|
|
|
...appState,
|
|
|
|
errorMessage: t(`errors.libraryElementTypeError.${type}`),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2021-11-17 20:06:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-17 18:23:43 +00:00
|
|
|
return app.library
|
2022-04-29 14:45:02 +00:00
|
|
|
.getLatestLibrary()
|
2021-11-17 18:23:43 +00:00
|
|
|
.then((items) => {
|
2022-04-29 14:45:02 +00:00
|
|
|
return app.library.setLibrary([
|
2021-11-17 18:23:43 +00:00
|
|
|
{
|
|
|
|
id: randomId(),
|
|
|
|
status: "unpublished",
|
2021-12-04 10:59:37 +00:00
|
|
|
elements: selectedElements.map(deepCopyElement),
|
2021-11-17 18:23:43 +00:00
|
|
|
created: Date.now(),
|
|
|
|
},
|
|
|
|
...items,
|
|
|
|
]);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return {
|
|
|
|
commitToHistory: false,
|
|
|
|
appState: {
|
|
|
|
...appState,
|
2022-07-11 12:41:13 +00:00
|
|
|
toast: { message: t("toast.addedToLibrary") },
|
2021-11-17 18:23:43 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
return {
|
|
|
|
commitToHistory: false,
|
|
|
|
appState: {
|
|
|
|
...appState,
|
|
|
|
errorMessage: error.message,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
2020-07-10 09:20:23 +00:00
|
|
|
},
|
2024-03-28 16:16:32 +00:00
|
|
|
label: "labels.addToLibrary",
|
2020-07-10 09:20:23 +00:00
|
|
|
});
|