You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.7 KiB
TypeScript
63 lines
1.7 KiB
TypeScript
5 years ago
|
import { register } from "./register";
|
||
|
import { deepCopyElement } from "../element/newElement";
|
||
3 years ago
|
import { randomId } from "../random";
|
||
|
import { t } from "../i18n";
|
||
2 years ago
|
import { LIBRARY_DISABLED_TYPES } from "../constants";
|
||
5 years ago
|
|
||
|
export const actionAddToLibrary = register({
|
||
|
name: "addToLibrary",
|
||
3 years ago
|
trackEvent: { category: "element" },
|
||
4 years ago
|
perform: (elements, appState, _, app) => {
|
||
2 years ago
|
const selectedElements = app.scene.getSelectedElements({
|
||
|
selectedElementIds: appState.selectedElementIds,
|
||
|
includeBoundTextElement: true,
|
||
|
includeElementsInFrames: true,
|
||
|
});
|
||
2 years ago
|
|
||
|
for (const type of LIBRARY_DISABLED_TYPES) {
|
||
|
if (selectedElements.some((element) => element.type === type)) {
|
||
|
return {
|
||
|
commitToHistory: false,
|
||
|
appState: {
|
||
|
...appState,
|
||
|
errorMessage: t(`errors.libraryElementTypeError.${type}`),
|
||
|
},
|
||
|
};
|
||
|
}
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
return app.library
|
||
3 years ago
|
.getLatestLibrary()
|
||
3 years ago
|
.then((items) => {
|
||
3 years ago
|
return app.library.setLibrary([
|
||
3 years ago
|
{
|
||
|
id: randomId(),
|
||
|
status: "unpublished",
|
||
3 years ago
|
elements: selectedElements.map(deepCopyElement),
|
||
3 years ago
|
created: Date.now(),
|
||
|
},
|
||
|
...items,
|
||
|
]);
|
||
|
})
|
||
|
.then(() => {
|
||
|
return {
|
||
|
commitToHistory: false,
|
||
|
appState: {
|
||
|
...appState,
|
||
3 years ago
|
toast: { message: t("toast.addedToLibrary") },
|
||
3 years ago
|
},
|
||
|
};
|
||
|
})
|
||
|
.catch((error) => {
|
||
|
return {
|
||
|
commitToHistory: false,
|
||
|
appState: {
|
||
|
...appState,
|
||
|
errorMessage: error.message,
|
||
|
},
|
||
|
};
|
||
|
});
|
||
5 years ago
|
},
|
||
|
contextItemLabel: "labels.addToLibrary",
|
||
|
});
|