2020-07-10 02:20:23 -07:00
|
|
|
import { register } from "./register";
|
|
|
|
import { getSelectedElements } from "../scene";
|
|
|
|
import { getNonDeletedElements } from "../element";
|
|
|
|
import { deepCopyElement } from "../element/newElement";
|
2021-11-17 23:53:43 +05:30
|
|
|
import { randomId } from "../random";
|
|
|
|
import { t } from "../i18n";
|
2020-07-10 02:20:23 -07:00
|
|
|
|
|
|
|
export const actionAddToLibrary = register({
|
|
|
|
name: "addToLibrary",
|
2021-04-21 23:38:24 +05:30
|
|
|
perform: (elements, appState, _, app) => {
|
2021-12-04 11:59:37 +01:00
|
|
|
const selectedElements = getSelectedElements(
|
|
|
|
getNonDeletedElements(elements),
|
|
|
|
appState,
|
2021-12-16 21:14:03 +05:30
|
|
|
true,
|
2021-12-04 11:59:37 +01:00
|
|
|
);
|
|
|
|
if (selectedElements.some((element) => element.type === "image")) {
|
2021-11-17 21:06:26 +01:00
|
|
|
return {
|
|
|
|
commitToHistory: false,
|
|
|
|
appState: {
|
|
|
|
...appState,
|
|
|
|
errorMessage: "Support for adding images to the library coming soon!",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-17 23:53:43 +05:30
|
|
|
return app.library
|
|
|
|
.loadLibrary()
|
|
|
|
.then((items) => {
|
|
|
|
return app.library.saveLibrary([
|
|
|
|
{
|
|
|
|
id: randomId(),
|
|
|
|
status: "unpublished",
|
2021-12-04 11:59:37 +01:00
|
|
|
elements: selectedElements.map(deepCopyElement),
|
2021-11-17 23:53:43 +05:30
|
|
|
created: Date.now(),
|
|
|
|
},
|
|
|
|
...items,
|
|
|
|
]);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return {
|
|
|
|
commitToHistory: false,
|
|
|
|
appState: {
|
|
|
|
...appState,
|
|
|
|
toastMessage: t("toast.addedToLibrary"),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
return {
|
|
|
|
commitToHistory: false,
|
|
|
|
appState: {
|
|
|
|
...appState,
|
|
|
|
errorMessage: error.message,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
2020-07-10 02:20:23 -07:00
|
|
|
},
|
|
|
|
contextItemLabel: "labels.addToLibrary",
|
|
|
|
});
|