2023-05-31 10:22:02 +02:00
|
|
|
import React, { useCallback, useEffect, useRef, useState } from "react";
|
2022-10-18 06:59:14 +02:00
|
|
|
import { serializeLibraryAsJSON } from "../data/json";
|
2021-11-17 23:53:43 +05:30
|
|
|
import { t } from "../i18n";
|
2023-05-08 10:14:02 +02:00
|
|
|
import {
|
|
|
|
ExcalidrawProps,
|
|
|
|
LibraryItem,
|
|
|
|
LibraryItems,
|
|
|
|
UIAppState,
|
|
|
|
} from "../types";
|
2023-05-24 16:40:20 +02:00
|
|
|
import { arrayToMap } from "../utils";
|
2021-11-17 23:53:43 +05:30
|
|
|
import Stack from "./Stack";
|
2022-10-18 06:59:14 +02:00
|
|
|
import { MIME_TYPES } from "../constants";
|
2022-04-29 16:45:02 +02:00
|
|
|
import Spinner from "./Spinner";
|
2023-04-16 17:09:51 +02:00
|
|
|
import { duplicateElements } from "../element/newElement";
|
2023-05-04 19:33:31 +02:00
|
|
|
import { LibraryMenuControlButtons } from "./LibraryMenuControlButtons";
|
2023-05-13 13:18:14 +02:00
|
|
|
import { LibraryDropdownMenu } from "./LibraryMenuHeaderContent";
|
2023-05-24 16:40:20 +02:00
|
|
|
import LibraryMenuSection from "./LibraryMenuSection";
|
2023-05-31 10:22:02 +02:00
|
|
|
import { useScrollPosition } from "../hooks/useScrollPosition";
|
2023-05-29 16:01:44 +02:00
|
|
|
import { useLibraryCache } from "../hooks/useLibraryItemSvg";
|
2022-10-18 06:59:14 +02:00
|
|
|
|
2023-05-08 10:14:02 +02:00
|
|
|
import "./LibraryMenuItems.scss";
|
|
|
|
|
2023-05-24 16:40:20 +02:00
|
|
|
export default function LibraryMenuItems({
|
2022-04-29 16:45:02 +02:00
|
|
|
isLoading,
|
2021-11-17 23:53:43 +05:30
|
|
|
libraryItems,
|
|
|
|
onAddToLibrary,
|
2022-05-11 15:51:02 +02:00
|
|
|
onInsertLibraryItems,
|
2021-11-17 23:53:43 +05:30
|
|
|
pendingElements,
|
2022-11-01 17:29:58 +01:00
|
|
|
theme,
|
|
|
|
id,
|
|
|
|
libraryReturnUrl,
|
2021-11-17 23:53:43 +05:30
|
|
|
}: {
|
2022-04-29 16:45:02 +02:00
|
|
|
isLoading: boolean;
|
2021-11-17 23:53:43 +05:30
|
|
|
libraryItems: LibraryItems;
|
|
|
|
pendingElements: LibraryItem["elements"];
|
2022-05-11 15:51:02 +02:00
|
|
|
onInsertLibraryItems: (libraryItems: LibraryItems) => void;
|
2021-11-17 23:53:43 +05:30
|
|
|
onAddToLibrary: (elements: LibraryItem["elements"]) => void;
|
2022-11-01 17:29:58 +01:00
|
|
|
libraryReturnUrl: ExcalidrawProps["libraryReturnUrl"];
|
2023-05-08 10:14:02 +02:00
|
|
|
theme: UIAppState["theme"];
|
2022-11-01 17:29:58 +01:00
|
|
|
id: string;
|
2023-05-24 16:40:20 +02:00
|
|
|
}) {
|
|
|
|
const [selectedItems, setSelectedItems] = useState<LibraryItem["id"][]>([]);
|
2023-05-31 10:22:02 +02:00
|
|
|
const libraryContainerRef = useRef<HTMLDivElement>(null);
|
|
|
|
const scrollPosition = useScrollPosition<HTMLDivElement>(libraryContainerRef);
|
|
|
|
|
|
|
|
// This effect has to be called only on first render, therefore `scrollPosition` isn't in the dependency array
|
|
|
|
useEffect(() => {
|
|
|
|
if (scrollPosition > 0) {
|
|
|
|
libraryContainerRef.current?.scrollTo(0, scrollPosition);
|
|
|
|
}
|
|
|
|
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
2023-05-29 16:01:44 +02:00
|
|
|
const { svgCache } = useLibraryCache();
|
2023-05-24 16:40:20 +02:00
|
|
|
|
|
|
|
const unpublishedItems = libraryItems.filter(
|
|
|
|
(item) => item.status !== "published",
|
|
|
|
);
|
|
|
|
const publishedItems = libraryItems.filter(
|
|
|
|
(item) => item.status === "published",
|
|
|
|
);
|
|
|
|
|
|
|
|
const showBtn = !libraryItems.length && !pendingElements.length;
|
|
|
|
|
|
|
|
const isLibraryEmpty =
|
|
|
|
!pendingElements.length &&
|
|
|
|
!unpublishedItems.length &&
|
|
|
|
!publishedItems.length;
|
|
|
|
|
2022-05-07 19:13:10 +02:00
|
|
|
const [lastSelectedItem, setLastSelectedItem] = useState<
|
|
|
|
LibraryItem["id"] | null
|
|
|
|
>(null);
|
|
|
|
|
|
|
|
const onItemSelectToggle = (
|
|
|
|
id: LibraryItem["id"],
|
|
|
|
event: React.MouseEvent,
|
|
|
|
) => {
|
|
|
|
const shouldSelect = !selectedItems.includes(id);
|
|
|
|
|
|
|
|
const orderedItems = [...unpublishedItems, ...publishedItems];
|
|
|
|
|
|
|
|
if (shouldSelect) {
|
|
|
|
if (event.shiftKey && lastSelectedItem) {
|
|
|
|
const rangeStart = orderedItems.findIndex(
|
|
|
|
(item) => item.id === lastSelectedItem,
|
|
|
|
);
|
|
|
|
const rangeEnd = orderedItems.findIndex((item) => item.id === id);
|
|
|
|
|
|
|
|
if (rangeStart === -1 || rangeEnd === -1) {
|
2023-05-24 16:40:20 +02:00
|
|
|
setSelectedItems([...selectedItems, id]);
|
2022-05-07 19:13:10 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const selectedItemsMap = arrayToMap(selectedItems);
|
|
|
|
const nextSelectedIds = orderedItems.reduce(
|
|
|
|
(acc: LibraryItem["id"][], item, idx) => {
|
|
|
|
if (
|
|
|
|
(idx >= rangeStart && idx <= rangeEnd) ||
|
|
|
|
selectedItemsMap.has(item.id)
|
|
|
|
) {
|
|
|
|
acc.push(item.id);
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
},
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
|
2023-05-24 16:40:20 +02:00
|
|
|
setSelectedItems(nextSelectedIds);
|
2022-05-07 19:13:10 +02:00
|
|
|
} else {
|
2023-05-24 16:40:20 +02:00
|
|
|
setSelectedItems([...selectedItems, id]);
|
2022-05-07 19:13:10 +02:00
|
|
|
}
|
|
|
|
setLastSelectedItem(id);
|
|
|
|
} else {
|
|
|
|
setLastSelectedItem(null);
|
2023-05-24 16:40:20 +02:00
|
|
|
setSelectedItems(selectedItems.filter((_id) => _id !== id));
|
2022-05-07 19:13:10 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-24 16:40:20 +02:00
|
|
|
const getInsertedElements = useCallback(
|
|
|
|
(id: string) => {
|
|
|
|
let targetElements;
|
|
|
|
if (selectedItems.includes(id)) {
|
|
|
|
targetElements = libraryItems.filter((item) =>
|
|
|
|
selectedItems.includes(item.id),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
targetElements = libraryItems.filter((item) => item.id === id);
|
|
|
|
}
|
|
|
|
return targetElements.map((item) => {
|
|
|
|
return {
|
|
|
|
...item,
|
|
|
|
// duplicate each library item before inserting on canvas to confine
|
|
|
|
// ids and bindings to each library item. See #6465
|
|
|
|
elements: duplicateElements(item.elements, { randomizeSeed: true }),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[libraryItems, selectedItems],
|
|
|
|
);
|
2022-05-11 15:51:02 +02:00
|
|
|
|
2023-05-24 16:40:20 +02:00
|
|
|
const onItemDrag = (id: LibraryItem["id"], event: React.DragEvent) => {
|
|
|
|
event.dataTransfer.setData(
|
|
|
|
MIME_TYPES.excalidrawlib,
|
|
|
|
serializeLibraryAsJSON(getInsertedElements(id)),
|
2021-11-17 23:53:43 +05:30
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-05-24 16:40:20 +02:00
|
|
|
const isItemSelected = (id: LibraryItem["id"] | null) => {
|
|
|
|
if (!id) {
|
|
|
|
return false;
|
2021-11-17 23:53:43 +05:30
|
|
|
}
|
|
|
|
|
2023-05-24 16:40:20 +02:00
|
|
|
return selectedItems.includes(id);
|
2021-11-17 23:53:43 +05:30
|
|
|
};
|
|
|
|
|
2023-05-24 16:40:20 +02:00
|
|
|
const onItemClick = useCallback(
|
|
|
|
(id: LibraryItem["id"] | null) => {
|
|
|
|
if (!id) {
|
|
|
|
onAddToLibrary(pendingElements);
|
|
|
|
} else {
|
|
|
|
onInsertLibraryItems(getInsertedElements(id));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[
|
|
|
|
getInsertedElements,
|
|
|
|
onAddToLibrary,
|
|
|
|
onInsertLibraryItems,
|
|
|
|
pendingElements,
|
|
|
|
],
|
2021-11-17 23:53:43 +05:30
|
|
|
);
|
|
|
|
|
2022-10-18 06:59:14 +02:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className="library-menu-items-container"
|
|
|
|
style={
|
2022-11-01 17:29:58 +01:00
|
|
|
pendingElements.length ||
|
|
|
|
unpublishedItems.length ||
|
|
|
|
publishedItems.length
|
|
|
|
? { justifyContent: "flex-start" }
|
2023-05-04 19:33:31 +02:00
|
|
|
: { borderBottom: 0 }
|
2022-10-18 06:59:14 +02:00
|
|
|
}
|
|
|
|
>
|
2023-05-13 13:18:14 +02:00
|
|
|
{!isLibraryEmpty && (
|
|
|
|
<LibraryDropdownMenu
|
|
|
|
selectedItems={selectedItems}
|
2023-05-24 16:40:20 +02:00
|
|
|
onSelectItems={setSelectedItems}
|
2023-05-13 13:18:14 +02:00
|
|
|
className="library-menu-dropdown-container--in-heading"
|
|
|
|
/>
|
|
|
|
)}
|
2021-11-17 23:53:43 +05:30
|
|
|
<Stack.Col
|
|
|
|
className="library-menu-items-container__items"
|
|
|
|
align="start"
|
|
|
|
gap={1}
|
2022-06-21 20:03:23 +05:00
|
|
|
style={{
|
2022-06-25 20:10:53 +02:00
|
|
|
flex: publishedItems.length > 0 ? 1 : "0 1 auto",
|
2022-06-21 20:03:23 +05:00
|
|
|
marginBottom: 0,
|
|
|
|
}}
|
2023-05-31 10:22:02 +02:00
|
|
|
ref={libraryContainerRef}
|
2021-11-17 23:53:43 +05:30
|
|
|
>
|
|
|
|
<>
|
2023-05-13 13:18:14 +02:00
|
|
|
{!isLibraryEmpty && (
|
|
|
|
<div className="library-menu-items-container__header">
|
|
|
|
{t("labels.personalLib")}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{isLoading && (
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
position: "absolute",
|
|
|
|
top: "var(--container-padding-y)",
|
|
|
|
right: "var(--container-padding-x)",
|
|
|
|
transform: "translateY(50%)",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
)}
|
2023-05-24 16:40:20 +02:00
|
|
|
{!pendingElements.length && !unpublishedItems.length ? (
|
|
|
|
<div className="library-menu-items__no-items">
|
|
|
|
<div className="library-menu-items__no-items__label">
|
|
|
|
{t("library.noItems")}
|
2022-06-21 20:03:23 +05:00
|
|
|
</div>
|
2023-05-24 16:40:20 +02:00
|
|
|
<div className="library-menu-items__no-items__hint">
|
|
|
|
{publishedItems.length > 0
|
|
|
|
? t("library.hint_emptyPrivateLibrary")
|
|
|
|
: t("library.hint_emptyLibrary")}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<LibraryMenuSection
|
|
|
|
items={[
|
2023-05-13 13:18:14 +02:00
|
|
|
// append pending library item
|
|
|
|
...(pendingElements.length
|
|
|
|
? [{ id: null, elements: pendingElements }]
|
|
|
|
: []),
|
|
|
|
...unpublishedItems,
|
2023-05-24 16:40:20 +02:00
|
|
|
]}
|
|
|
|
onItemSelectToggle={onItemSelectToggle}
|
|
|
|
onItemDrag={onItemDrag}
|
|
|
|
onClick={onItemClick}
|
|
|
|
isItemSelected={isItemSelected}
|
2023-05-29 16:01:44 +02:00
|
|
|
svgCache={svgCache}
|
2023-05-24 16:40:20 +02:00
|
|
|
/>
|
|
|
|
)}
|
2021-11-17 23:53:43 +05:30
|
|
|
</>
|
|
|
|
|
|
|
|
<>
|
2022-06-21 20:03:23 +05:00
|
|
|
{(publishedItems.length > 0 ||
|
2022-10-18 06:59:14 +02:00
|
|
|
pendingElements.length > 0 ||
|
|
|
|
unpublishedItems.length > 0) && (
|
2022-11-01 17:29:58 +01:00
|
|
|
<div className="library-menu-items-container__header library-menu-items-container__header--excal">
|
|
|
|
{t("labels.excalidrawLib")}
|
|
|
|
</div>
|
2022-06-21 20:03:23 +05:00
|
|
|
)}
|
2022-06-25 20:10:53 +02:00
|
|
|
{publishedItems.length > 0 ? (
|
2023-05-24 16:40:20 +02:00
|
|
|
<LibraryMenuSection
|
|
|
|
items={publishedItems}
|
|
|
|
onItemSelectToggle={onItemSelectToggle}
|
|
|
|
onItemDrag={onItemDrag}
|
|
|
|
onClick={onItemClick}
|
|
|
|
isItemSelected={isItemSelected}
|
2023-05-29 16:01:44 +02:00
|
|
|
svgCache={svgCache}
|
2023-05-24 16:40:20 +02:00
|
|
|
/>
|
2022-06-25 20:10:53 +02:00
|
|
|
) : unpublishedItems.length > 0 ? (
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
margin: "1rem 0",
|
|
|
|
display: "flex",
|
|
|
|
flexDirection: "column",
|
|
|
|
alignItems: "center",
|
|
|
|
justifyContent: "center",
|
|
|
|
width: "100%",
|
|
|
|
fontSize: ".9rem",
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("library.noItems")}
|
|
|
|
</div>
|
|
|
|
) : null}
|
2021-11-17 23:53:43 +05:30
|
|
|
</>
|
2022-11-01 17:29:58 +01:00
|
|
|
|
|
|
|
{showBtn && (
|
2023-05-04 19:33:31 +02:00
|
|
|
<LibraryMenuControlButtons
|
|
|
|
style={{ padding: "16px 0", width: "100%" }}
|
2022-11-01 17:29:58 +01:00
|
|
|
id={id}
|
|
|
|
libraryReturnUrl={libraryReturnUrl}
|
|
|
|
theme={theme}
|
2023-05-13 13:18:14 +02:00
|
|
|
>
|
|
|
|
<LibraryDropdownMenu
|
|
|
|
selectedItems={selectedItems}
|
2023-05-24 16:40:20 +02:00
|
|
|
onSelectItems={setSelectedItems}
|
2023-05-13 13:18:14 +02:00
|
|
|
/>
|
|
|
|
</LibraryMenuControlButtons>
|
2022-11-01 17:29:58 +01:00
|
|
|
)}
|
2021-11-17 23:53:43 +05:30
|
|
|
</Stack.Col>
|
|
|
|
</div>
|
|
|
|
);
|
2023-05-24 16:40:20 +02:00
|
|
|
}
|