2021-01-05 20:06:14 +02:00
|
|
|
import clsx from "clsx";
|
2020-07-10 02:20:23 -07:00
|
|
|
import React, {
|
|
|
|
RefObject,
|
|
|
|
useCallback,
|
2021-01-05 20:06:14 +02:00
|
|
|
useEffect,
|
|
|
|
useRef,
|
|
|
|
useState,
|
2020-07-10 02:20:23 -07:00
|
|
|
} from "react";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { ActionManager } from "../actions/manager";
|
|
|
|
import { CLASSES } from "../constants";
|
2020-03-07 10:20:38 -05:00
|
|
|
import { exportCanvas } from "../data";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { importLibraryFromJSON, saveLibraryAsJSON } from "../data/json";
|
2021-02-03 20:43:16 +01:00
|
|
|
import { isTextElement, showSelectedShapeActions } from "../element";
|
2020-07-27 15:29:19 +03:00
|
|
|
import { NonDeletedExcalidrawElement } from "../element/types";
|
2021-01-04 02:21:52 +05:30
|
|
|
import { Language, t } from "../i18n";
|
2021-04-08 19:54:50 +02:00
|
|
|
import { useIsMobile } from "../components/App";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { calculateScrollCenter, getSelectedElements } from "../scene";
|
2020-03-07 10:20:38 -05:00
|
|
|
import { ExportType } from "../scene/types";
|
2021-04-04 15:57:14 +05:30
|
|
|
import {
|
|
|
|
AppProps,
|
|
|
|
AppState,
|
|
|
|
ExcalidrawProps,
|
|
|
|
LibraryItem,
|
|
|
|
LibraryItems,
|
|
|
|
} from "../types";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { muteFSAbortError } from "../utils";
|
|
|
|
import { SelectedShapeActions, ShapesSwitcher, ZoomActions } from "./Actions";
|
|
|
|
import { BackgroundPickerAndDarkModeToggle } from "./BackgroundPickerAndDarkModeToggle";
|
2020-12-05 20:00:53 +05:30
|
|
|
import CollabButton from "./CollabButton";
|
2020-04-03 12:50:51 +01:00
|
|
|
import { ErrorDialog } from "./ErrorDialog";
|
2021-05-25 21:37:14 +02:00
|
|
|
import { ExportCB, ImageExportDialog } from "./ImageExportDialog";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { FixedSideContainer } from "./FixedSideContainer";
|
|
|
|
import { HintViewer } from "./HintViewer";
|
2021-05-15 14:49:58 +05:30
|
|
|
import { exportFile, load, trash } from "./icons";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { Island } from "./Island";
|
2020-04-18 02:09:15 +05:30
|
|
|
import "./LayerUI.scss";
|
2020-07-10 02:20:23 -07:00
|
|
|
import { LibraryUnit } from "./LibraryUnit";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { LoadingMessage } from "./LoadingMessage";
|
|
|
|
import { LockIcon } from "./LockIcon";
|
|
|
|
import { MobileMenu } from "./MobileMenu";
|
2020-12-27 18:26:30 +02:00
|
|
|
import { PasteChartDialog } from "./PasteChartDialog";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { Section } from "./Section";
|
2021-01-17 17:46:23 +01:00
|
|
|
import { HelpDialog } from "./HelpDialog";
|
2021-01-05 20:06:14 +02:00
|
|
|
import Stack from "./Stack";
|
|
|
|
import { ToolButton } from "./ToolButton";
|
|
|
|
import { Tooltip } from "./Tooltip";
|
|
|
|
import { UserList } from "./UserList";
|
2021-04-21 23:38:24 +05:30
|
|
|
import Library from "../data/library";
|
2021-05-25 21:37:14 +02:00
|
|
|
import { JSONExportDialog } from "./JSONExportDialog";
|
2020-03-07 10:20:38 -05:00
|
|
|
|
|
|
|
interface LayerUIProps {
|
|
|
|
actionManager: ActionManager;
|
|
|
|
appState: AppState;
|
|
|
|
canvas: HTMLCanvasElement | null;
|
2020-10-16 11:53:40 +02:00
|
|
|
setAppState: React.Component<any, AppState>["setState"];
|
2020-04-08 09:49:52 -07:00
|
|
|
elements: readonly NonDeletedExcalidrawElement[];
|
2020-12-05 20:00:53 +05:30
|
|
|
onCollabButtonClick?: () => void;
|
2020-03-24 19:51:49 +09:00
|
|
|
onLockToggle: () => void;
|
2020-12-27 18:26:30 +02:00
|
|
|
onInsertElements: (elements: readonly NonDeletedExcalidrawElement[]) => void;
|
2020-04-25 18:43:02 +05:30
|
|
|
zenModeEnabled: boolean;
|
2021-02-06 21:22:28 +05:30
|
|
|
showExitZenModeBtn: boolean;
|
2021-03-15 11:33:46 -07:00
|
|
|
showThemeBtn: boolean;
|
2020-04-25 18:43:02 +05:30
|
|
|
toggleZenMode: () => void;
|
2021-01-04 02:21:52 +05:30
|
|
|
langCode: Language["code"];
|
2020-12-05 20:00:53 +05:30
|
|
|
isCollaborating: boolean;
|
2021-05-13 21:02:59 +05:30
|
|
|
renderTopRightUI?: (isMobile: boolean, appState: AppState) => JSX.Element;
|
2021-05-15 14:49:58 +05:30
|
|
|
renderCustomFooter?: (isMobile: boolean, appState: AppState) => JSX.Element;
|
2021-02-02 02:26:42 +05:30
|
|
|
viewModeEnabled: boolean;
|
2021-03-13 12:35:35 +01:00
|
|
|
libraryReturnUrl: ExcalidrawProps["libraryReturnUrl"];
|
2021-04-04 15:57:14 +05:30
|
|
|
UIOptions: AppProps["UIOptions"];
|
2021-04-13 01:29:25 +05:30
|
|
|
focusContainer: () => void;
|
2021-04-21 23:38:24 +05:30
|
|
|
library: Library;
|
|
|
|
id: string;
|
2020-03-07 10:20:38 -05:00
|
|
|
}
|
|
|
|
|
2020-11-06 22:06:39 +02:00
|
|
|
const useOnClickOutside = (
|
2020-07-10 02:20:23 -07:00
|
|
|
ref: RefObject<HTMLElement>,
|
|
|
|
cb: (event: MouseEvent) => void,
|
2020-11-06 22:06:39 +02:00
|
|
|
) => {
|
2020-07-10 02:20:23 -07:00
|
|
|
useEffect(() => {
|
|
|
|
const listener = (event: MouseEvent) => {
|
|
|
|
if (!ref.current) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
event.target instanceof Element &&
|
|
|
|
(ref.current.contains(event.target) ||
|
|
|
|
!document.body.contains(event.target))
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(event);
|
|
|
|
};
|
|
|
|
document.addEventListener("pointerdown", listener, false);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
document.removeEventListener("pointerdown", listener);
|
|
|
|
};
|
|
|
|
}, [ref, cb]);
|
2020-11-06 22:06:39 +02:00
|
|
|
};
|
2020-07-10 02:20:23 -07:00
|
|
|
|
|
|
|
const LibraryMenuItems = ({
|
2021-04-21 23:38:24 +05:30
|
|
|
libraryItems,
|
2020-07-10 02:20:23 -07:00
|
|
|
onRemoveFromLibrary,
|
|
|
|
onAddToLibrary,
|
|
|
|
onInsertShape,
|
|
|
|
pendingElements,
|
2020-07-27 15:29:19 +03:00
|
|
|
setAppState,
|
2021-02-09 15:49:04 +05:30
|
|
|
setLibraryItems,
|
2021-03-13 12:35:35 +01:00
|
|
|
libraryReturnUrl,
|
2021-04-13 01:29:25 +05:30
|
|
|
focusContainer,
|
2021-04-21 23:38:24 +05:30
|
|
|
library,
|
|
|
|
id,
|
2020-07-10 02:20:23 -07:00
|
|
|
}: {
|
2021-04-21 23:38:24 +05:30
|
|
|
libraryItems: LibraryItems;
|
2020-07-27 15:29:19 +03:00
|
|
|
pendingElements: LibraryItem;
|
2020-07-10 02:20:23 -07:00
|
|
|
onRemoveFromLibrary: (index: number) => void;
|
2020-07-27 15:29:19 +03:00
|
|
|
onInsertShape: (elements: LibraryItem) => void;
|
|
|
|
onAddToLibrary: (elements: LibraryItem) => void;
|
2020-10-16 11:53:40 +02:00
|
|
|
setAppState: React.Component<any, AppState>["setState"];
|
2021-02-09 15:49:04 +05:30
|
|
|
setLibraryItems: (library: LibraryItems) => void;
|
2021-03-13 12:35:35 +01:00
|
|
|
libraryReturnUrl: ExcalidrawProps["libraryReturnUrl"];
|
2021-04-13 01:29:25 +05:30
|
|
|
focusContainer: () => void;
|
2021-04-21 23:38:24 +05:30
|
|
|
library: Library;
|
|
|
|
id: string;
|
2020-07-10 02:20:23 -07:00
|
|
|
}) => {
|
2020-07-20 00:12:56 +03:00
|
|
|
const isMobile = useIsMobile();
|
2021-04-21 23:38:24 +05:30
|
|
|
const numCells = libraryItems.length + (pendingElements.length > 0 ? 1 : 0);
|
2020-07-20 00:12:56 +03:00
|
|
|
const CELLS_PER_ROW = isMobile ? 4 : 6;
|
2020-07-10 02:20:23 -07:00
|
|
|
const numRows = Math.max(1, Math.ceil(numCells / CELLS_PER_ROW));
|
|
|
|
const rows = [];
|
|
|
|
let addedPendingElements = false;
|
|
|
|
|
2021-03-25 18:27:40 +01:00
|
|
|
const referrer =
|
|
|
|
libraryReturnUrl || window.location.origin + window.location.pathname;
|
2021-03-13 12:35:35 +01:00
|
|
|
|
2020-07-27 15:29:19 +03:00
|
|
|
rows.push(
|
2021-03-23 22:25:54 +05:30
|
|
|
<div className="layer-ui__library-header" key="library-header">
|
2021-01-02 13:13:48 -03:00
|
|
|
<ToolButton
|
|
|
|
key="import"
|
|
|
|
type="button"
|
|
|
|
title={t("buttons.load")}
|
|
|
|
aria-label={t("buttons.load")}
|
|
|
|
icon={load}
|
|
|
|
onClick={() => {
|
2021-04-21 23:38:24 +05:30
|
|
|
importLibraryFromJSON(library)
|
2021-01-02 13:13:48 -03:00
|
|
|
.then(() => {
|
2021-04-04 19:06:10 +07:00
|
|
|
// Close and then open to get the libraries updated
|
2021-01-02 13:13:48 -03:00
|
|
|
setAppState({ isLibraryOpen: false });
|
2021-04-04 19:06:10 +07:00
|
|
|
setAppState({ isLibraryOpen: true });
|
2021-01-02 13:13:48 -03:00
|
|
|
})
|
|
|
|
.catch(muteFSAbortError)
|
|
|
|
.catch((error) => {
|
|
|
|
setAppState({ errorMessage: error.message });
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
2021-04-21 23:38:24 +05:30
|
|
|
{!!libraryItems.length && (
|
2021-03-20 21:58:37 +05:30
|
|
|
<>
|
|
|
|
<ToolButton
|
|
|
|
key="export"
|
|
|
|
type="button"
|
|
|
|
title={t("buttons.export")}
|
|
|
|
aria-label={t("buttons.export")}
|
|
|
|
icon={exportFile}
|
|
|
|
onClick={() => {
|
2021-04-21 23:38:24 +05:30
|
|
|
saveLibraryAsJSON(library)
|
2021-03-20 21:58:37 +05:30
|
|
|
.catch(muteFSAbortError)
|
|
|
|
.catch((error) => {
|
|
|
|
setAppState({ errorMessage: error.message });
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<ToolButton
|
|
|
|
key="reset"
|
|
|
|
type="button"
|
|
|
|
title={t("buttons.resetLibrary")}
|
|
|
|
aria-label={t("buttons.resetLibrary")}
|
|
|
|
icon={trash}
|
|
|
|
onClick={() => {
|
|
|
|
if (window.confirm(t("alerts.resetLibrary"))) {
|
2021-04-21 23:38:24 +05:30
|
|
|
library.resetLibrary();
|
2021-03-20 21:58:37 +05:30
|
|
|
setLibraryItems([]);
|
2021-04-13 01:29:25 +05:30
|
|
|
focusContainer();
|
2021-03-20 21:58:37 +05:30
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
2021-03-13 12:35:35 +01:00
|
|
|
<a
|
2021-03-21 18:13:52 +05:30
|
|
|
href={`https://libraries.excalidraw.com?target=${
|
|
|
|
window.name || "_blank"
|
2021-04-21 23:38:24 +05:30
|
|
|
}&referrer=${referrer}&useHash=true&token=${id}`}
|
2021-03-13 12:35:35 +01:00
|
|
|
target="_excalidraw_libraries"
|
|
|
|
>
|
2020-12-07 19:24:55 +02:00
|
|
|
{t("labels.libraries")}
|
|
|
|
</a>
|
2021-01-02 13:13:48 -03:00
|
|
|
</div>,
|
2020-07-27 15:29:19 +03:00
|
|
|
);
|
|
|
|
|
2020-07-10 02:20:23 -07:00
|
|
|
for (let row = 0; row < numRows; row++) {
|
2020-12-07 19:24:55 +02:00
|
|
|
const y = CELLS_PER_ROW * row;
|
2020-07-10 02:20:23 -07:00
|
|
|
const children = [];
|
2020-12-07 19:24:55 +02:00
|
|
|
for (let x = 0; x < CELLS_PER_ROW; x++) {
|
2020-07-10 02:20:23 -07:00
|
|
|
const shouldAddPendingElements: boolean =
|
|
|
|
pendingElements.length > 0 &&
|
|
|
|
!addedPendingElements &&
|
2021-04-21 23:38:24 +05:30
|
|
|
y + x >= libraryItems.length;
|
2020-07-10 02:20:23 -07:00
|
|
|
addedPendingElements = addedPendingElements || shouldAddPendingElements;
|
|
|
|
|
|
|
|
children.push(
|
2020-12-07 19:24:55 +02:00
|
|
|
<Stack.Col key={x}>
|
2020-07-10 02:20:23 -07:00
|
|
|
<LibraryUnit
|
2021-04-21 23:38:24 +05:30
|
|
|
elements={libraryItems[y + x]}
|
2020-07-10 02:20:23 -07:00
|
|
|
pendingElements={
|
|
|
|
shouldAddPendingElements ? pendingElements : undefined
|
|
|
|
}
|
2020-12-07 19:24:55 +02:00
|
|
|
onRemoveFromLibrary={onRemoveFromLibrary.bind(null, y + x)}
|
2020-07-10 02:20:23 -07:00
|
|
|
onClick={
|
|
|
|
shouldAddPendingElements
|
|
|
|
? onAddToLibrary.bind(null, pendingElements)
|
2021-04-21 23:38:24 +05:30
|
|
|
: onInsertShape.bind(null, libraryItems[y + x])
|
2020-07-10 02:20:23 -07:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
</Stack.Col>,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
rows.push(
|
|
|
|
<Stack.Row align="center" gap={1} key={row}>
|
|
|
|
{children}
|
|
|
|
</Stack.Row>,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-12-12 18:53:59 +05:30
|
|
|
<Stack.Col align="start" gap={1} className="layer-ui__library-items">
|
2020-07-10 02:20:23 -07:00
|
|
|
{rows}
|
|
|
|
</Stack.Col>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const LibraryMenu = ({
|
|
|
|
onClickOutside,
|
|
|
|
onInsertShape,
|
|
|
|
pendingElements,
|
|
|
|
onAddToLibrary,
|
2020-07-27 15:29:19 +03:00
|
|
|
setAppState,
|
2021-03-13 12:35:35 +01:00
|
|
|
libraryReturnUrl,
|
2021-04-13 01:29:25 +05:30
|
|
|
focusContainer,
|
2021-04-21 23:38:24 +05:30
|
|
|
library,
|
|
|
|
id,
|
2020-07-10 02:20:23 -07:00
|
|
|
}: {
|
2020-07-27 15:29:19 +03:00
|
|
|
pendingElements: LibraryItem;
|
2020-07-10 02:20:23 -07:00
|
|
|
onClickOutside: (event: MouseEvent) => void;
|
2020-07-27 15:29:19 +03:00
|
|
|
onInsertShape: (elements: LibraryItem) => void;
|
2020-07-10 02:20:23 -07:00
|
|
|
onAddToLibrary: () => void;
|
2020-10-16 11:53:40 +02:00
|
|
|
setAppState: React.Component<any, AppState>["setState"];
|
2021-03-13 12:35:35 +01:00
|
|
|
libraryReturnUrl: ExcalidrawProps["libraryReturnUrl"];
|
2021-04-13 01:29:25 +05:30
|
|
|
focusContainer: () => void;
|
2021-04-21 23:38:24 +05:30
|
|
|
library: Library;
|
|
|
|
id: string;
|
2020-07-10 02:20:23 -07:00
|
|
|
}) => {
|
|
|
|
const ref = useRef<HTMLDivElement | null>(null);
|
2020-11-27 03:02:40 +08:00
|
|
|
useOnClickOutside(ref, (event) => {
|
|
|
|
// If click on the library icon, do nothing.
|
|
|
|
if ((event.target as Element).closest(".ToolIcon_type_button__library")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
onClickOutside(event);
|
|
|
|
});
|
2020-07-10 02:20:23 -07:00
|
|
|
|
|
|
|
const [libraryItems, setLibraryItems] = useState<LibraryItems>([]);
|
|
|
|
|
|
|
|
const [loadingState, setIsLoading] = useState<
|
|
|
|
"preloading" | "loading" | "ready"
|
|
|
|
>("preloading");
|
|
|
|
|
|
|
|
const loadingTimerRef = useRef<NodeJS.Timeout | null>(null);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
Promise.race([
|
|
|
|
new Promise((resolve) => {
|
|
|
|
loadingTimerRef.current = setTimeout(() => {
|
|
|
|
resolve("loading");
|
|
|
|
}, 100);
|
|
|
|
}),
|
2021-04-21 23:38:24 +05:30
|
|
|
library.loadLibrary().then((items) => {
|
2020-07-10 02:20:23 -07:00
|
|
|
setLibraryItems(items);
|
|
|
|
setIsLoading("ready");
|
|
|
|
}),
|
|
|
|
]).then((data) => {
|
|
|
|
if (data === "loading") {
|
|
|
|
setIsLoading("loading");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return () => {
|
|
|
|
clearTimeout(loadingTimerRef.current!);
|
|
|
|
};
|
2021-04-21 23:38:24 +05:30
|
|
|
}, [library]);
|
2020-07-10 02:20:23 -07:00
|
|
|
|
2021-04-21 23:38:24 +05:30
|
|
|
const removeFromLibrary = useCallback(
|
|
|
|
async (indexToRemove) => {
|
|
|
|
const items = await library.loadLibrary();
|
|
|
|
const nextItems = items.filter((_, index) => index !== indexToRemove);
|
|
|
|
library.saveLibrary(nextItems).catch((error) => {
|
|
|
|
setLibraryItems(items);
|
|
|
|
setAppState({ errorMessage: t("alerts.errorRemovingFromLibrary") });
|
|
|
|
});
|
|
|
|
setLibraryItems(nextItems);
|
|
|
|
},
|
|
|
|
[library, setAppState],
|
|
|
|
);
|
2020-07-10 02:20:23 -07:00
|
|
|
|
|
|
|
const addToLibrary = useCallback(
|
2020-07-27 15:29:19 +03:00
|
|
|
async (elements: LibraryItem) => {
|
2021-04-21 23:38:24 +05:30
|
|
|
const items = await library.loadLibrary();
|
2020-07-10 02:20:23 -07:00
|
|
|
const nextItems = [...items, elements];
|
|
|
|
onAddToLibrary();
|
2021-04-21 23:38:24 +05:30
|
|
|
library.saveLibrary(nextItems).catch((error) => {
|
|
|
|
setLibraryItems(items);
|
|
|
|
setAppState({ errorMessage: t("alerts.errorAddingToLibrary") });
|
|
|
|
});
|
2020-07-10 02:20:23 -07:00
|
|
|
setLibraryItems(nextItems);
|
|
|
|
},
|
2021-04-21 23:38:24 +05:30
|
|
|
[onAddToLibrary, library, setAppState],
|
2020-07-10 02:20:23 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
return loadingState === "preloading" ? null : (
|
|
|
|
<Island padding={1} ref={ref} className="layer-ui__library">
|
|
|
|
{loadingState === "loading" ? (
|
|
|
|
<div className="layer-ui__library-message">
|
|
|
|
{t("labels.libraryLoadingMessage")}
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<LibraryMenuItems
|
2021-04-21 23:38:24 +05:30
|
|
|
libraryItems={libraryItems}
|
2020-07-10 02:20:23 -07:00
|
|
|
onRemoveFromLibrary={removeFromLibrary}
|
|
|
|
onAddToLibrary={addToLibrary}
|
|
|
|
onInsertShape={onInsertShape}
|
|
|
|
pendingElements={pendingElements}
|
2020-07-27 15:29:19 +03:00
|
|
|
setAppState={setAppState}
|
2021-02-09 15:49:04 +05:30
|
|
|
setLibraryItems={setLibraryItems}
|
2021-03-13 12:35:35 +01:00
|
|
|
libraryReturnUrl={libraryReturnUrl}
|
2021-04-13 01:29:25 +05:30
|
|
|
focusContainer={focusContainer}
|
2021-04-21 23:38:24 +05:30
|
|
|
library={library}
|
|
|
|
id={id}
|
2020-07-10 02:20:23 -07:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Island>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-04-18 01:54:19 +05:30
|
|
|
const LayerUI = ({
|
|
|
|
actionManager,
|
|
|
|
appState,
|
|
|
|
setAppState,
|
|
|
|
canvas,
|
|
|
|
elements,
|
2020-12-05 20:00:53 +05:30
|
|
|
onCollabButtonClick,
|
2020-04-18 01:54:19 +05:30
|
|
|
onLockToggle,
|
2020-12-27 18:26:30 +02:00
|
|
|
onInsertElements,
|
2020-04-25 18:43:02 +05:30
|
|
|
zenModeEnabled,
|
2021-02-06 21:22:28 +05:30
|
|
|
showExitZenModeBtn,
|
2021-03-15 11:33:46 -07:00
|
|
|
showThemeBtn,
|
2020-04-25 18:43:02 +05:30
|
|
|
toggleZenMode,
|
2020-12-05 20:00:53 +05:30
|
|
|
isCollaborating,
|
2021-05-13 21:02:59 +05:30
|
|
|
renderTopRightUI,
|
2021-01-04 02:21:52 +05:30
|
|
|
renderCustomFooter,
|
2021-02-02 02:26:42 +05:30
|
|
|
viewModeEnabled,
|
2021-03-13 12:35:35 +01:00
|
|
|
libraryReturnUrl,
|
2021-04-04 15:57:14 +05:30
|
|
|
UIOptions,
|
2021-04-13 01:29:25 +05:30
|
|
|
focusContainer,
|
2021-04-21 23:38:24 +05:30
|
|
|
library,
|
|
|
|
id,
|
2020-04-18 01:54:19 +05:30
|
|
|
}: LayerUIProps) => {
|
|
|
|
const isMobile = useIsMobile();
|
2020-03-07 10:20:38 -05:00
|
|
|
|
2021-05-25 21:37:14 +02:00
|
|
|
const renderJSONExportDialog = () => {
|
|
|
|
if (!UIOptions.canvasActions.export) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<JSONExportDialog
|
|
|
|
elements={elements}
|
|
|
|
appState={appState}
|
|
|
|
actionManager={actionManager}
|
2021-05-29 02:56:25 +05:30
|
|
|
onExportToBackend={(elements) => {
|
|
|
|
UIOptions.canvasActions.export.onExportToBackend &&
|
|
|
|
UIOptions.canvasActions.export.onExportToBackend(
|
|
|
|
elements,
|
|
|
|
appState,
|
|
|
|
canvas,
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
exportOpts={UIOptions.canvasActions.export}
|
2021-05-25 21:37:14 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderImageExportDialog = () => {
|
2021-04-04 15:57:14 +05:30
|
|
|
if (!UIOptions.canvasActions.export) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:53:40 +02:00
|
|
|
const createExporter = (type: ExportType): ExportCB => async (
|
2020-04-18 01:54:19 +05:30
|
|
|
exportedElements,
|
|
|
|
scale,
|
|
|
|
) => {
|
2021-05-13 18:21:15 +01:00
|
|
|
await exportCanvas(type, exportedElements, appState, {
|
|
|
|
exportBackground: appState.exportBackground,
|
|
|
|
name: appState.name,
|
|
|
|
viewBackgroundColor: appState.viewBackgroundColor,
|
|
|
|
scale,
|
|
|
|
})
|
|
|
|
.catch(muteFSAbortError)
|
|
|
|
.catch((error) => {
|
|
|
|
console.error(error);
|
|
|
|
setAppState({ errorMessage: error.message });
|
|
|
|
});
|
2020-04-18 01:54:19 +05:30
|
|
|
};
|
2020-12-20 19:44:04 +05:30
|
|
|
|
2020-04-18 01:54:19 +05:30
|
|
|
return (
|
2021-05-25 21:37:14 +02:00
|
|
|
<ImageExportDialog
|
2020-03-07 10:20:38 -05:00
|
|
|
elements={elements}
|
2020-04-18 01:54:19 +05:30
|
|
|
appState={appState}
|
2020-03-07 10:20:38 -05:00
|
|
|
actionManager={actionManager}
|
2020-04-18 01:54:19 +05:30
|
|
|
onExportToPng={createExporter("png")}
|
|
|
|
onExportToSvg={createExporter("svg")}
|
|
|
|
onExportToClipboard={createExporter("clipboard")}
|
2020-03-07 10:20:38 -05:00
|
|
|
/>
|
2020-04-18 01:54:19 +05:30
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-05-25 21:37:14 +02:00
|
|
|
const Separator = () => {
|
|
|
|
return <div style={{ width: ".625em" }} />;
|
|
|
|
};
|
|
|
|
|
2021-02-02 02:26:42 +05:30
|
|
|
const renderViewModeCanvasActions = () => {
|
|
|
|
return (
|
|
|
|
<Section
|
|
|
|
heading="canvasActions"
|
|
|
|
className={clsx("zen-mode-transition", {
|
|
|
|
"transition-left": zenModeEnabled,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{/* the zIndex ensures this menu has higher stacking order,
|
|
|
|
see https://github.com/excalidraw/excalidraw/pull/1445 */}
|
|
|
|
<Island padding={2} style={{ zIndex: 1 }}>
|
|
|
|
<Stack.Col gap={4}>
|
|
|
|
<Stack.Row gap={1} justifyContent="space-between">
|
2021-05-25 21:37:14 +02:00
|
|
|
{renderJSONExportDialog()}
|
|
|
|
{renderImageExportDialog()}
|
2021-02-02 02:26:42 +05:30
|
|
|
</Stack.Row>
|
|
|
|
</Stack.Col>
|
|
|
|
</Island>
|
|
|
|
</Section>
|
|
|
|
);
|
|
|
|
};
|
2020-04-18 01:54:19 +05:30
|
|
|
const renderCanvasActions = () => (
|
2020-04-25 18:43:02 +05:30
|
|
|
<Section
|
|
|
|
heading="canvasActions"
|
2020-10-19 17:14:28 +03:00
|
|
|
className={clsx("zen-mode-transition", {
|
|
|
|
"transition-left": zenModeEnabled,
|
|
|
|
})}
|
2020-04-25 18:43:02 +05:30
|
|
|
>
|
2020-04-18 01:54:19 +05:30
|
|
|
{/* the zIndex ensures this menu has higher stacking order,
|
|
|
|
see https://github.com/excalidraw/excalidraw/pull/1445 */}
|
2020-08-21 01:24:46 +03:00
|
|
|
<Island padding={2} style={{ zIndex: 1 }}>
|
2020-04-18 01:54:19 +05:30
|
|
|
<Stack.Col gap={4}>
|
|
|
|
<Stack.Row gap={1} justifyContent="space-between">
|
|
|
|
{actionManager.renderAction("clearCanvas")}
|
2021-05-25 21:37:14 +02:00
|
|
|
<Separator />
|
|
|
|
{actionManager.renderAction("loadScene")}
|
|
|
|
{renderJSONExportDialog()}
|
|
|
|
{renderImageExportDialog()}
|
|
|
|
<Separator />
|
2020-12-05 20:00:53 +05:30
|
|
|
{onCollabButtonClick && (
|
|
|
|
<CollabButton
|
|
|
|
isCollaborating={isCollaborating}
|
|
|
|
collaboratorCount={appState.collaborators.size}
|
|
|
|
onClick={onCollabButtonClick}
|
|
|
|
/>
|
|
|
|
)}
|
2020-04-18 01:54:19 +05:30
|
|
|
</Stack.Row>
|
2020-08-13 04:35:31 -07:00
|
|
|
<BackgroundPickerAndDarkModeToggle
|
|
|
|
actionManager={actionManager}
|
|
|
|
appState={appState}
|
|
|
|
setAppState={setAppState}
|
2021-03-15 11:33:46 -07:00
|
|
|
showThemeBtn={showThemeBtn}
|
2020-08-13 04:35:31 -07:00
|
|
|
/>
|
2020-04-18 01:54:19 +05:30
|
|
|
</Stack.Col>
|
|
|
|
</Island>
|
|
|
|
</Section>
|
|
|
|
);
|
|
|
|
|
|
|
|
const renderSelectedShapeActions = () => (
|
2020-04-25 18:43:02 +05:30
|
|
|
<Section
|
|
|
|
heading="selectedShapeActions"
|
2020-10-19 17:14:28 +03:00
|
|
|
className={clsx("zen-mode-transition", {
|
|
|
|
"transition-left": zenModeEnabled,
|
|
|
|
})}
|
2020-04-25 18:43:02 +05:30
|
|
|
>
|
2021-02-14 18:18:34 +05:30
|
|
|
<Island
|
|
|
|
className={CLASSES.SHAPE_ACTIONS_MENU}
|
|
|
|
padding={2}
|
|
|
|
style={{
|
|
|
|
// we want to make sure this doesn't overflow so substracting 200
|
|
|
|
// which is approximately height of zoom footer and top left menu items with some buffer
|
|
|
|
maxHeight: `${appState.height - 200}px`,
|
|
|
|
}}
|
|
|
|
>
|
2020-04-18 01:54:19 +05:30
|
|
|
<SelectedShapeActions
|
|
|
|
appState={appState}
|
|
|
|
elements={elements}
|
|
|
|
renderAction={actionManager.renderAction}
|
|
|
|
elementType={appState.elementType}
|
|
|
|
/>
|
|
|
|
</Island>
|
|
|
|
</Section>
|
|
|
|
);
|
|
|
|
|
2020-07-10 02:20:23 -07:00
|
|
|
const closeLibrary = useCallback(
|
|
|
|
(event) => {
|
|
|
|
setAppState({ isLibraryOpen: false });
|
|
|
|
},
|
|
|
|
[setAppState],
|
|
|
|
);
|
|
|
|
|
|
|
|
const deselectItems = useCallback(() => {
|
|
|
|
setAppState({
|
|
|
|
selectedElementIds: {},
|
|
|
|
selectedGroupIds: {},
|
|
|
|
});
|
|
|
|
}, [setAppState]);
|
|
|
|
|
2020-07-20 00:12:56 +03:00
|
|
|
const libraryMenu = appState.isLibraryOpen ? (
|
|
|
|
<LibraryMenu
|
|
|
|
pendingElements={getSelectedElements(elements, appState)}
|
|
|
|
onClickOutside={closeLibrary}
|
2020-12-27 18:26:30 +02:00
|
|
|
onInsertShape={onInsertElements}
|
2020-07-20 00:12:56 +03:00
|
|
|
onAddToLibrary={deselectItems}
|
2020-07-27 15:29:19 +03:00
|
|
|
setAppState={setAppState}
|
2021-03-13 12:35:35 +01:00
|
|
|
libraryReturnUrl={libraryReturnUrl}
|
2021-04-13 01:29:25 +05:30
|
|
|
focusContainer={focusContainer}
|
2021-04-21 23:38:24 +05:30
|
|
|
library={library}
|
|
|
|
id={id}
|
2020-07-20 00:12:56 +03:00
|
|
|
/>
|
|
|
|
) : null;
|
|
|
|
|
2020-04-18 01:54:19 +05:30
|
|
|
const renderFixedSideContainer = () => {
|
|
|
|
const shouldRenderSelectedShapeActions = showSelectedShapeActions(
|
|
|
|
appState,
|
|
|
|
elements,
|
|
|
|
);
|
2020-07-20 00:12:56 +03:00
|
|
|
|
2020-04-18 01:54:19 +05:30
|
|
|
return (
|
|
|
|
<FixedSideContainer side="top">
|
|
|
|
<div className="App-menu App-menu_top">
|
2020-04-29 22:49:36 +02:00
|
|
|
<Stack.Col
|
|
|
|
gap={4}
|
2020-10-19 17:14:28 +03:00
|
|
|
className={clsx({ "disable-pointerEvents": zenModeEnabled })}
|
2020-04-29 22:49:36 +02:00
|
|
|
>
|
2021-02-02 02:26:42 +05:30
|
|
|
{viewModeEnabled
|
|
|
|
? renderViewModeCanvasActions()
|
|
|
|
: renderCanvasActions()}
|
2020-04-18 01:54:19 +05:30
|
|
|
{shouldRenderSelectedShapeActions && renderSelectedShapeActions()}
|
|
|
|
</Stack.Col>
|
2021-02-02 02:26:42 +05:30
|
|
|
{!viewModeEnabled && (
|
|
|
|
<Section heading="shapes">
|
|
|
|
{(heading) => (
|
|
|
|
<Stack.Col gap={4} align="start">
|
|
|
|
<Stack.Row gap={1}>
|
|
|
|
<Island
|
|
|
|
padding={1}
|
|
|
|
className={clsx({ "zen-mode": zenModeEnabled })}
|
|
|
|
>
|
|
|
|
<HintViewer appState={appState} elements={elements} />
|
|
|
|
{heading}
|
|
|
|
<Stack.Row gap={1}>
|
|
|
|
<ShapesSwitcher
|
2021-03-03 14:04:02 +01:00
|
|
|
canvas={canvas}
|
2021-02-02 02:26:42 +05:30
|
|
|
elementType={appState.elementType}
|
|
|
|
setAppState={setAppState}
|
|
|
|
isLibraryOpen={appState.isLibraryOpen}
|
|
|
|
/>
|
|
|
|
</Stack.Row>
|
|
|
|
</Island>
|
|
|
|
<LockIcon
|
|
|
|
zenModeEnabled={zenModeEnabled}
|
|
|
|
checked={appState.elementLocked}
|
|
|
|
onChange={onLockToggle}
|
|
|
|
title={t("toolBar.lock")}
|
|
|
|
/>
|
|
|
|
</Stack.Row>
|
|
|
|
{libraryMenu}
|
|
|
|
</Stack.Col>
|
|
|
|
)}
|
|
|
|
</Section>
|
|
|
|
)}
|
2021-05-06 21:00:17 +02:00
|
|
|
<div
|
|
|
|
className={clsx(
|
|
|
|
"layer-ui__wrapper__top-right zen-mode-transition",
|
|
|
|
{
|
|
|
|
"transition-right": zenModeEnabled,
|
|
|
|
},
|
|
|
|
)}
|
2020-06-19 11:36:49 +01:00
|
|
|
>
|
2021-05-06 21:00:17 +02:00
|
|
|
<UserList>
|
|
|
|
{appState.collaborators.size > 0 &&
|
|
|
|
Array.from(appState.collaborators)
|
|
|
|
// Collaborator is either not initialized or is actually the current user.
|
|
|
|
.filter(([_, client]) => Object.keys(client).length !== 0)
|
|
|
|
.map(([clientId, client]) => (
|
|
|
|
<Tooltip
|
|
|
|
label={client.username || "Unknown user"}
|
|
|
|
key={clientId}
|
|
|
|
>
|
|
|
|
{actionManager.renderAction("goToCollaborator", clientId)}
|
|
|
|
</Tooltip>
|
|
|
|
))}
|
|
|
|
</UserList>
|
2021-05-13 21:02:59 +05:30
|
|
|
{renderTopRightUI?.(isMobile, appState)}
|
2021-05-06 21:00:17 +02:00
|
|
|
</div>
|
2020-04-18 01:54:19 +05:30
|
|
|
</div>
|
|
|
|
</FixedSideContainer>
|
2020-03-07 10:20:38 -05:00
|
|
|
);
|
2020-04-18 01:54:19 +05:30
|
|
|
};
|
2020-03-07 10:20:38 -05:00
|
|
|
|
2020-07-02 18:57:47 +05:30
|
|
|
const renderBottomAppMenu = () => {
|
|
|
|
return (
|
2021-05-15 14:49:58 +05:30
|
|
|
<footer
|
|
|
|
role="contentinfo"
|
|
|
|
className="layer-ui__wrapper__footer App-menu App-menu_bottom"
|
2020-07-02 18:57:47 +05:30
|
|
|
>
|
2021-05-15 14:49:58 +05:30
|
|
|
<div
|
|
|
|
className={clsx(
|
|
|
|
"layer-ui__wrapper__footer-left zen-mode-transition",
|
|
|
|
{
|
|
|
|
"layer-ui__wrapper__footer-left--transition-left": zenModeEnabled,
|
|
|
|
},
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<Stack.Col gap={2}>
|
|
|
|
<Section heading="canvasActions">
|
|
|
|
<Island padding={1}>
|
|
|
|
<ZoomActions
|
|
|
|
renderAction={actionManager.renderAction}
|
|
|
|
zoom={appState.zoom}
|
|
|
|
/>
|
|
|
|
</Island>
|
|
|
|
</Section>
|
|
|
|
</Stack.Col>
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className={clsx(
|
|
|
|
"layer-ui__wrapper__footer-center zen-mode-transition",
|
|
|
|
{
|
|
|
|
"layer-ui__wrapper__footer-left--transition-bottom": zenModeEnabled,
|
|
|
|
},
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{renderCustomFooter?.(false, appState)}
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className={clsx(
|
|
|
|
"layer-ui__wrapper__footer-right zen-mode-transition",
|
|
|
|
{
|
|
|
|
"transition-right disable-pointerEvents": zenModeEnabled,
|
|
|
|
},
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{actionManager.renderAction("toggleShortcuts")}
|
|
|
|
</div>
|
|
|
|
<button
|
|
|
|
className={clsx("disable-zen-mode", {
|
|
|
|
"disable-zen-mode--visible": showExitZenModeBtn,
|
|
|
|
})}
|
|
|
|
onClick={toggleZenMode}
|
|
|
|
>
|
|
|
|
{t("buttons.exitZenMode")}
|
|
|
|
</button>
|
|
|
|
</footer>
|
2020-07-02 18:57:47 +05:30
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-12-27 18:26:30 +02:00
|
|
|
const dialogs = (
|
|
|
|
<>
|
2020-04-18 01:54:19 +05:30
|
|
|
{appState.isLoading && <LoadingMessage />}
|
|
|
|
{appState.errorMessage && (
|
|
|
|
<ErrorDialog
|
|
|
|
message={appState.errorMessage}
|
|
|
|
onClose={() => setAppState({ errorMessage: null })}
|
|
|
|
/>
|
|
|
|
)}
|
2021-01-17 17:46:23 +01:00
|
|
|
{appState.showHelpDialog && (
|
2021-04-13 01:29:25 +05:30
|
|
|
<HelpDialog
|
|
|
|
onClose={() => {
|
|
|
|
setAppState({ showHelpDialog: false });
|
|
|
|
}}
|
|
|
|
/>
|
2020-04-18 01:54:19 +05:30
|
|
|
)}
|
2020-12-27 18:26:30 +02:00
|
|
|
{appState.pasteDialog.shown && (
|
|
|
|
<PasteChartDialog
|
|
|
|
setAppState={setAppState}
|
|
|
|
appState={appState}
|
|
|
|
onInsertChart={onInsertElements}
|
|
|
|
onClose={() =>
|
|
|
|
setAppState({
|
|
|
|
pasteDialog: { shown: false, data: null },
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
|
|
|
return isMobile ? (
|
|
|
|
<>
|
|
|
|
{dialogs}
|
|
|
|
<MobileMenu
|
|
|
|
appState={appState}
|
|
|
|
elements={elements}
|
|
|
|
actionManager={actionManager}
|
|
|
|
libraryMenu={libraryMenu}
|
2021-05-25 21:37:14 +02:00
|
|
|
renderJSONExportDialog={renderJSONExportDialog}
|
|
|
|
renderImageExportDialog={renderImageExportDialog}
|
2020-12-27 18:26:30 +02:00
|
|
|
setAppState={setAppState}
|
|
|
|
onCollabButtonClick={onCollabButtonClick}
|
|
|
|
onLockToggle={onLockToggle}
|
|
|
|
canvas={canvas}
|
|
|
|
isCollaborating={isCollaborating}
|
2021-01-04 02:21:52 +05:30
|
|
|
renderCustomFooter={renderCustomFooter}
|
2021-02-02 02:26:42 +05:30
|
|
|
viewModeEnabled={viewModeEnabled}
|
2021-03-15 11:33:46 -07:00
|
|
|
showThemeBtn={showThemeBtn}
|
2020-12-27 18:26:30 +02:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
) : (
|
2021-02-01 13:55:38 +01:00
|
|
|
<div
|
|
|
|
className={clsx("layer-ui__wrapper", {
|
2021-02-03 20:43:16 +01:00
|
|
|
"disable-pointerEvents":
|
|
|
|
appState.draggingElement ||
|
|
|
|
appState.resizingElement ||
|
|
|
|
(appState.editingElement && !isTextElement(appState.editingElement)),
|
2021-02-01 13:55:38 +01:00
|
|
|
})}
|
|
|
|
>
|
2020-12-27 18:26:30 +02:00
|
|
|
{dialogs}
|
2020-04-18 01:54:19 +05:30
|
|
|
{renderFixedSideContainer()}
|
2020-07-02 18:57:47 +05:30
|
|
|
{renderBottomAppMenu()}
|
2021-02-14 18:18:34 +05:30
|
|
|
{appState.scrolledOutside && (
|
|
|
|
<button
|
|
|
|
className="scroll-back-to-content"
|
|
|
|
onClick={() => {
|
|
|
|
setAppState({
|
|
|
|
...calculateScrollCenter(elements, appState, canvas),
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("buttons.scrollBackToContent")}
|
|
|
|
</button>
|
|
|
|
)}
|
2020-04-18 02:09:15 +05:30
|
|
|
</div>
|
2020-04-18 01:54:19 +05:30
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const areEqual = (prev: LayerUIProps, next: LayerUIProps) => {
|
|
|
|
const getNecessaryObj = (appState: AppState): Partial<AppState> => {
|
2020-08-08 21:04:15 -07:00
|
|
|
const {
|
|
|
|
suggestedBindings,
|
|
|
|
startBoundElement: boundElement,
|
|
|
|
...ret
|
|
|
|
} = appState;
|
2020-04-18 01:54:19 +05:30
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
const prevAppState = getNecessaryObj(prev.appState);
|
|
|
|
const nextAppState = getNecessaryObj(next.appState);
|
|
|
|
|
|
|
|
const keys = Object.keys(prevAppState) as (keyof Partial<AppState>)[];
|
|
|
|
return (
|
2021-03-06 14:23:25 +01:00
|
|
|
prev.renderCustomFooter === next.renderCustomFooter &&
|
2021-01-04 02:21:52 +05:30
|
|
|
prev.langCode === next.langCode &&
|
2020-04-18 01:54:19 +05:30
|
|
|
prev.elements === next.elements &&
|
|
|
|
keys.every((key) => prevAppState[key] === nextAppState[key])
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default React.memo(LayerUI, areEqual);
|