LayerUI.tsx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. import clsx from "clsx";
  2. import React, {
  3. RefObject,
  4. useCallback,
  5. useEffect,
  6. useRef,
  7. useState,
  8. } from "react";
  9. import { ActionManager } from "../actions/manager";
  10. import { CLASSES } from "../constants";
  11. import { exportCanvas } from "../data";
  12. import { importLibraryFromJSON, saveLibraryAsJSON } from "../data/json";
  13. import { isTextElement, showSelectedShapeActions } from "../element";
  14. import { NonDeletedExcalidrawElement } from "../element/types";
  15. import { Language, t } from "../i18n";
  16. import { useIsMobile } from "../components/App";
  17. import { calculateScrollCenter, getSelectedElements } from "../scene";
  18. import { ExportType } from "../scene/types";
  19. import {
  20. AppProps,
  21. AppState,
  22. ExcalidrawProps,
  23. LibraryItem,
  24. LibraryItems,
  25. } from "../types";
  26. import { muteFSAbortError } from "../utils";
  27. import { SelectedShapeActions, ShapesSwitcher, ZoomActions } from "./Actions";
  28. import { BackgroundPickerAndDarkModeToggle } from "./BackgroundPickerAndDarkModeToggle";
  29. import CollabButton from "./CollabButton";
  30. import { ErrorDialog } from "./ErrorDialog";
  31. import { ExportCB, ImageExportDialog } from "./ImageExportDialog";
  32. import { FixedSideContainer } from "./FixedSideContainer";
  33. import { HintViewer } from "./HintViewer";
  34. import { exportFile, load, trash } from "./icons";
  35. import { Island } from "./Island";
  36. import "./LayerUI.scss";
  37. import { LibraryUnit } from "./LibraryUnit";
  38. import { LoadingMessage } from "./LoadingMessage";
  39. import { LockButton } from "./LockButton";
  40. import { MobileMenu } from "./MobileMenu";
  41. import { PasteChartDialog } from "./PasteChartDialog";
  42. import { Section } from "./Section";
  43. import { HelpDialog } from "./HelpDialog";
  44. import Stack from "./Stack";
  45. import { ToolButton } from "./ToolButton";
  46. import { Tooltip } from "./Tooltip";
  47. import { UserList } from "./UserList";
  48. import Library from "../data/library";
  49. import { JSONExportDialog } from "./JSONExportDialog";
  50. import { LibraryButton } from "./LibraryButton";
  51. import { isImageFileHandle } from "../data/blob";
  52. interface LayerUIProps {
  53. actionManager: ActionManager;
  54. appState: AppState;
  55. canvas: HTMLCanvasElement | null;
  56. setAppState: React.Component<any, AppState>["setState"];
  57. elements: readonly NonDeletedExcalidrawElement[];
  58. onCollabButtonClick?: () => void;
  59. onLockToggle: () => void;
  60. onInsertElements: (elements: readonly NonDeletedExcalidrawElement[]) => void;
  61. zenModeEnabled: boolean;
  62. showExitZenModeBtn: boolean;
  63. showThemeBtn: boolean;
  64. toggleZenMode: () => void;
  65. langCode: Language["code"];
  66. isCollaborating: boolean;
  67. renderTopRightUI?: (
  68. isMobile: boolean,
  69. appState: AppState,
  70. ) => JSX.Element | null;
  71. renderCustomFooter?: (isMobile: boolean, appState: AppState) => JSX.Element;
  72. viewModeEnabled: boolean;
  73. libraryReturnUrl: ExcalidrawProps["libraryReturnUrl"];
  74. UIOptions: AppProps["UIOptions"];
  75. focusContainer: () => void;
  76. library: Library;
  77. id: string;
  78. }
  79. const useOnClickOutside = (
  80. ref: RefObject<HTMLElement>,
  81. cb: (event: MouseEvent) => void,
  82. ) => {
  83. useEffect(() => {
  84. const listener = (event: MouseEvent) => {
  85. if (!ref.current) {
  86. return;
  87. }
  88. if (
  89. event.target instanceof Element &&
  90. (ref.current.contains(event.target) ||
  91. !document.body.contains(event.target))
  92. ) {
  93. return;
  94. }
  95. cb(event);
  96. };
  97. document.addEventListener("pointerdown", listener, false);
  98. return () => {
  99. document.removeEventListener("pointerdown", listener);
  100. };
  101. }, [ref, cb]);
  102. };
  103. const LibraryMenuItems = ({
  104. libraryItems,
  105. onRemoveFromLibrary,
  106. onAddToLibrary,
  107. onInsertShape,
  108. pendingElements,
  109. theme,
  110. setAppState,
  111. setLibraryItems,
  112. libraryReturnUrl,
  113. focusContainer,
  114. library,
  115. id,
  116. }: {
  117. libraryItems: LibraryItems;
  118. pendingElements: LibraryItem;
  119. onRemoveFromLibrary: (index: number) => void;
  120. onInsertShape: (elements: LibraryItem) => void;
  121. onAddToLibrary: (elements: LibraryItem) => void;
  122. theme: AppState["theme"];
  123. setAppState: React.Component<any, AppState>["setState"];
  124. setLibraryItems: (library: LibraryItems) => void;
  125. libraryReturnUrl: ExcalidrawProps["libraryReturnUrl"];
  126. focusContainer: () => void;
  127. library: Library;
  128. id: string;
  129. }) => {
  130. const isMobile = useIsMobile();
  131. const numCells = libraryItems.length + (pendingElements.length > 0 ? 1 : 0);
  132. const CELLS_PER_ROW = isMobile ? 4 : 6;
  133. const numRows = Math.max(1, Math.ceil(numCells / CELLS_PER_ROW));
  134. const rows = [];
  135. let addedPendingElements = false;
  136. const referrer =
  137. libraryReturnUrl || window.location.origin + window.location.pathname;
  138. rows.push(
  139. <div className="layer-ui__library-header" key="library-header">
  140. <ToolButton
  141. key="import"
  142. type="button"
  143. title={t("buttons.load")}
  144. aria-label={t("buttons.load")}
  145. icon={load}
  146. onClick={() => {
  147. importLibraryFromJSON(library)
  148. .then(() => {
  149. // Close and then open to get the libraries updated
  150. setAppState({ isLibraryOpen: false });
  151. setAppState({ isLibraryOpen: true });
  152. })
  153. .catch(muteFSAbortError)
  154. .catch((error) => {
  155. setAppState({ errorMessage: error.message });
  156. });
  157. }}
  158. />
  159. {!!libraryItems.length && (
  160. <>
  161. <ToolButton
  162. key="export"
  163. type="button"
  164. title={t("buttons.export")}
  165. aria-label={t("buttons.export")}
  166. icon={exportFile}
  167. onClick={() => {
  168. saveLibraryAsJSON(library)
  169. .catch(muteFSAbortError)
  170. .catch((error) => {
  171. setAppState({ errorMessage: error.message });
  172. });
  173. }}
  174. />
  175. <ToolButton
  176. key="reset"
  177. type="button"
  178. title={t("buttons.resetLibrary")}
  179. aria-label={t("buttons.resetLibrary")}
  180. icon={trash}
  181. onClick={() => {
  182. if (window.confirm(t("alerts.resetLibrary"))) {
  183. library.resetLibrary();
  184. setLibraryItems([]);
  185. focusContainer();
  186. }
  187. }}
  188. />
  189. </>
  190. )}
  191. <a
  192. href={`https://libraries.excalidraw.com?target=${
  193. window.name || "_blank"
  194. }&referrer=${referrer}&useHash=true&token=${id}&theme=${theme}`}
  195. target="_excalidraw_libraries"
  196. >
  197. {t("labels.libraries")}
  198. </a>
  199. </div>,
  200. );
  201. for (let row = 0; row < numRows; row++) {
  202. const y = CELLS_PER_ROW * row;
  203. const children = [];
  204. for (let x = 0; x < CELLS_PER_ROW; x++) {
  205. const shouldAddPendingElements: boolean =
  206. pendingElements.length > 0 &&
  207. !addedPendingElements &&
  208. y + x >= libraryItems.length;
  209. addedPendingElements = addedPendingElements || shouldAddPendingElements;
  210. children.push(
  211. <Stack.Col key={x}>
  212. <LibraryUnit
  213. elements={libraryItems[y + x]}
  214. pendingElements={
  215. shouldAddPendingElements ? pendingElements : undefined
  216. }
  217. onRemoveFromLibrary={onRemoveFromLibrary.bind(null, y + x)}
  218. onClick={
  219. shouldAddPendingElements
  220. ? onAddToLibrary.bind(null, pendingElements)
  221. : onInsertShape.bind(null, libraryItems[y + x])
  222. }
  223. />
  224. </Stack.Col>,
  225. );
  226. }
  227. rows.push(
  228. <Stack.Row align="center" gap={1} key={row}>
  229. {children}
  230. </Stack.Row>,
  231. );
  232. }
  233. return (
  234. <Stack.Col align="start" gap={1} className="layer-ui__library-items">
  235. {rows}
  236. </Stack.Col>
  237. );
  238. };
  239. const LibraryMenu = ({
  240. onClickOutside,
  241. onInsertShape,
  242. pendingElements,
  243. onAddToLibrary,
  244. theme,
  245. setAppState,
  246. libraryReturnUrl,
  247. focusContainer,
  248. library,
  249. id,
  250. }: {
  251. pendingElements: LibraryItem;
  252. onClickOutside: (event: MouseEvent) => void;
  253. onInsertShape: (elements: LibraryItem) => void;
  254. onAddToLibrary: () => void;
  255. theme: AppState["theme"];
  256. setAppState: React.Component<any, AppState>["setState"];
  257. libraryReturnUrl: ExcalidrawProps["libraryReturnUrl"];
  258. focusContainer: () => void;
  259. library: Library;
  260. id: string;
  261. }) => {
  262. const ref = useRef<HTMLDivElement | null>(null);
  263. useOnClickOutside(ref, (event) => {
  264. // If click on the library icon, do nothing.
  265. if ((event.target as Element).closest(".ToolIcon_type_button__library")) {
  266. return;
  267. }
  268. onClickOutside(event);
  269. });
  270. const [libraryItems, setLibraryItems] = useState<LibraryItems>([]);
  271. const [loadingState, setIsLoading] = useState<
  272. "preloading" | "loading" | "ready"
  273. >("preloading");
  274. const loadingTimerRef = useRef<NodeJS.Timeout | null>(null);
  275. useEffect(() => {
  276. Promise.race([
  277. new Promise((resolve) => {
  278. loadingTimerRef.current = setTimeout(() => {
  279. resolve("loading");
  280. }, 100);
  281. }),
  282. library.loadLibrary().then((items) => {
  283. setLibraryItems(items);
  284. setIsLoading("ready");
  285. }),
  286. ]).then((data) => {
  287. if (data === "loading") {
  288. setIsLoading("loading");
  289. }
  290. });
  291. return () => {
  292. clearTimeout(loadingTimerRef.current!);
  293. };
  294. }, [library]);
  295. const removeFromLibrary = useCallback(
  296. async (indexToRemove) => {
  297. const items = await library.loadLibrary();
  298. const nextItems = items.filter((_, index) => index !== indexToRemove);
  299. library.saveLibrary(nextItems).catch((error) => {
  300. setLibraryItems(items);
  301. setAppState({ errorMessage: t("alerts.errorRemovingFromLibrary") });
  302. });
  303. setLibraryItems(nextItems);
  304. },
  305. [library, setAppState],
  306. );
  307. const addToLibrary = useCallback(
  308. async (elements: LibraryItem) => {
  309. const items = await library.loadLibrary();
  310. const nextItems = [...items, elements];
  311. onAddToLibrary();
  312. library.saveLibrary(nextItems).catch((error) => {
  313. setLibraryItems(items);
  314. setAppState({ errorMessage: t("alerts.errorAddingToLibrary") });
  315. });
  316. setLibraryItems(nextItems);
  317. },
  318. [onAddToLibrary, library, setAppState],
  319. );
  320. return loadingState === "preloading" ? null : (
  321. <Island padding={1} ref={ref} className="layer-ui__library">
  322. {loadingState === "loading" ? (
  323. <div className="layer-ui__library-message">
  324. {t("labels.libraryLoadingMessage")}
  325. </div>
  326. ) : (
  327. <LibraryMenuItems
  328. libraryItems={libraryItems}
  329. onRemoveFromLibrary={removeFromLibrary}
  330. onAddToLibrary={addToLibrary}
  331. onInsertShape={onInsertShape}
  332. pendingElements={pendingElements}
  333. setAppState={setAppState}
  334. setLibraryItems={setLibraryItems}
  335. libraryReturnUrl={libraryReturnUrl}
  336. focusContainer={focusContainer}
  337. library={library}
  338. theme={theme}
  339. id={id}
  340. />
  341. )}
  342. </Island>
  343. );
  344. };
  345. const LayerUI = ({
  346. actionManager,
  347. appState,
  348. setAppState,
  349. canvas,
  350. elements,
  351. onCollabButtonClick,
  352. onLockToggle,
  353. onInsertElements,
  354. zenModeEnabled,
  355. showExitZenModeBtn,
  356. showThemeBtn,
  357. toggleZenMode,
  358. isCollaborating,
  359. renderTopRightUI,
  360. renderCustomFooter,
  361. viewModeEnabled,
  362. libraryReturnUrl,
  363. UIOptions,
  364. focusContainer,
  365. library,
  366. id,
  367. }: LayerUIProps) => {
  368. const isMobile = useIsMobile();
  369. const renderJSONExportDialog = () => {
  370. if (!UIOptions.canvasActions.export) {
  371. return null;
  372. }
  373. return (
  374. <JSONExportDialog
  375. elements={elements}
  376. appState={appState}
  377. actionManager={actionManager}
  378. exportOpts={UIOptions.canvasActions.export}
  379. canvas={canvas}
  380. />
  381. );
  382. };
  383. const renderImageExportDialog = () => {
  384. if (!UIOptions.canvasActions.saveAsImage) {
  385. return null;
  386. }
  387. const createExporter = (type: ExportType): ExportCB => async (
  388. exportedElements,
  389. ) => {
  390. const fileHandle = await exportCanvas(type, exportedElements, appState, {
  391. exportBackground: appState.exportBackground,
  392. name: appState.name,
  393. viewBackgroundColor: appState.viewBackgroundColor,
  394. })
  395. .catch(muteFSAbortError)
  396. .catch((error) => {
  397. console.error(error);
  398. setAppState({ errorMessage: error.message });
  399. });
  400. if (
  401. appState.exportEmbedScene &&
  402. fileHandle &&
  403. isImageFileHandle(fileHandle)
  404. ) {
  405. setAppState({ fileHandle });
  406. }
  407. };
  408. return (
  409. <ImageExportDialog
  410. elements={elements}
  411. appState={appState}
  412. actionManager={actionManager}
  413. onExportToPng={createExporter("png")}
  414. onExportToSvg={createExporter("svg")}
  415. onExportToClipboard={createExporter("clipboard")}
  416. />
  417. );
  418. };
  419. const Separator = () => {
  420. return <div style={{ width: ".625em" }} />;
  421. };
  422. const renderViewModeCanvasActions = () => {
  423. return (
  424. <Section
  425. heading="canvasActions"
  426. className={clsx("zen-mode-transition", {
  427. "transition-left": zenModeEnabled,
  428. })}
  429. >
  430. {/* the zIndex ensures this menu has higher stacking order,
  431. see https://github.com/excalidraw/excalidraw/pull/1445 */}
  432. <Island padding={2} style={{ zIndex: 1 }}>
  433. <Stack.Col gap={4}>
  434. <Stack.Row gap={1} justifyContent="space-between">
  435. {renderJSONExportDialog()}
  436. {renderImageExportDialog()}
  437. </Stack.Row>
  438. </Stack.Col>
  439. </Island>
  440. </Section>
  441. );
  442. };
  443. const renderCanvasActions = () => (
  444. <Section
  445. heading="canvasActions"
  446. className={clsx("zen-mode-transition", {
  447. "transition-left": zenModeEnabled,
  448. })}
  449. >
  450. {/* the zIndex ensures this menu has higher stacking order,
  451. see https://github.com/excalidraw/excalidraw/pull/1445 */}
  452. <Island padding={2} style={{ zIndex: 1 }}>
  453. <Stack.Col gap={4}>
  454. <Stack.Row gap={1} justifyContent="space-between">
  455. {actionManager.renderAction("clearCanvas")}
  456. <Separator />
  457. {actionManager.renderAction("loadScene")}
  458. {renderJSONExportDialog()}
  459. {renderImageExportDialog()}
  460. <Separator />
  461. {onCollabButtonClick && (
  462. <CollabButton
  463. isCollaborating={isCollaborating}
  464. collaboratorCount={appState.collaborators.size}
  465. onClick={onCollabButtonClick}
  466. />
  467. )}
  468. </Stack.Row>
  469. <BackgroundPickerAndDarkModeToggle
  470. actionManager={actionManager}
  471. appState={appState}
  472. setAppState={setAppState}
  473. showThemeBtn={showThemeBtn}
  474. />
  475. {appState.fileHandle && (
  476. <>{actionManager.renderAction("saveToActiveFile")}</>
  477. )}
  478. </Stack.Col>
  479. </Island>
  480. </Section>
  481. );
  482. const renderSelectedShapeActions = () => (
  483. <Section
  484. heading="selectedShapeActions"
  485. className={clsx("zen-mode-transition", {
  486. "transition-left": zenModeEnabled,
  487. })}
  488. >
  489. <Island
  490. className={CLASSES.SHAPE_ACTIONS_MENU}
  491. padding={2}
  492. style={{
  493. // we want to make sure this doesn't overflow so substracting 200
  494. // which is approximately height of zoom footer and top left menu items with some buffer
  495. // if active file name is displayed, subtracting 248 to account for its height
  496. maxHeight: `${appState.height - (appState.fileHandle ? 248 : 200)}px`,
  497. }}
  498. >
  499. <SelectedShapeActions
  500. appState={appState}
  501. elements={elements}
  502. renderAction={actionManager.renderAction}
  503. elementType={appState.elementType}
  504. />
  505. </Island>
  506. </Section>
  507. );
  508. const closeLibrary = useCallback(
  509. (event) => {
  510. setAppState({ isLibraryOpen: false });
  511. },
  512. [setAppState],
  513. );
  514. const deselectItems = useCallback(() => {
  515. setAppState({
  516. selectedElementIds: {},
  517. selectedGroupIds: {},
  518. });
  519. }, [setAppState]);
  520. const libraryMenu = appState.isLibraryOpen ? (
  521. <LibraryMenu
  522. pendingElements={getSelectedElements(elements, appState)}
  523. onClickOutside={closeLibrary}
  524. onInsertShape={onInsertElements}
  525. onAddToLibrary={deselectItems}
  526. setAppState={setAppState}
  527. libraryReturnUrl={libraryReturnUrl}
  528. focusContainer={focusContainer}
  529. library={library}
  530. theme={appState.theme}
  531. id={id}
  532. />
  533. ) : null;
  534. const renderFixedSideContainer = () => {
  535. const shouldRenderSelectedShapeActions = showSelectedShapeActions(
  536. appState,
  537. elements,
  538. );
  539. return (
  540. <FixedSideContainer side="top">
  541. <div className="App-menu App-menu_top">
  542. <Stack.Col
  543. gap={4}
  544. className={clsx({ "disable-pointerEvents": zenModeEnabled })}
  545. >
  546. {viewModeEnabled
  547. ? renderViewModeCanvasActions()
  548. : renderCanvasActions()}
  549. {shouldRenderSelectedShapeActions && renderSelectedShapeActions()}
  550. </Stack.Col>
  551. {!viewModeEnabled && (
  552. <Section heading="shapes">
  553. {(heading) => (
  554. <Stack.Col gap={4} align="start">
  555. <Stack.Row gap={1}>
  556. <LockButton
  557. zenModeEnabled={zenModeEnabled}
  558. checked={appState.elementLocked}
  559. onChange={onLockToggle}
  560. title={t("toolBar.lock")}
  561. />
  562. <Island
  563. padding={1}
  564. className={clsx({ "zen-mode": zenModeEnabled })}
  565. >
  566. <HintViewer appState={appState} elements={elements} />
  567. {heading}
  568. <Stack.Row gap={1}>
  569. <ShapesSwitcher
  570. canvas={canvas}
  571. elementType={appState.elementType}
  572. setAppState={setAppState}
  573. />
  574. </Stack.Row>
  575. </Island>
  576. <LibraryButton
  577. appState={appState}
  578. setAppState={setAppState}
  579. />
  580. </Stack.Row>
  581. {libraryMenu}
  582. </Stack.Col>
  583. )}
  584. </Section>
  585. )}
  586. <div
  587. className={clsx(
  588. "layer-ui__wrapper__top-right zen-mode-transition",
  589. {
  590. "transition-right": zenModeEnabled,
  591. },
  592. )}
  593. >
  594. <UserList>
  595. {appState.collaborators.size > 0 &&
  596. Array.from(appState.collaborators)
  597. // Collaborator is either not initialized or is actually the current user.
  598. .filter(([_, client]) => Object.keys(client).length !== 0)
  599. .map(([clientId, client]) => (
  600. <Tooltip
  601. label={client.username || "Unknown user"}
  602. key={clientId}
  603. >
  604. {actionManager.renderAction("goToCollaborator", {
  605. id: clientId,
  606. })}
  607. </Tooltip>
  608. ))}
  609. </UserList>
  610. {renderTopRightUI?.(isMobile, appState)}
  611. </div>
  612. </div>
  613. </FixedSideContainer>
  614. );
  615. };
  616. const renderBottomAppMenu = () => {
  617. return (
  618. <footer
  619. role="contentinfo"
  620. className="layer-ui__wrapper__footer App-menu App-menu_bottom"
  621. >
  622. <div
  623. className={clsx(
  624. "layer-ui__wrapper__footer-left zen-mode-transition",
  625. {
  626. "layer-ui__wrapper__footer-left--transition-left": zenModeEnabled,
  627. },
  628. )}
  629. >
  630. <Stack.Col gap={2}>
  631. <Section heading="canvasActions">
  632. <Island padding={1}>
  633. <ZoomActions
  634. renderAction={actionManager.renderAction}
  635. zoom={appState.zoom}
  636. />
  637. </Island>
  638. {!viewModeEnabled && (
  639. <div
  640. className={clsx("undo-redo-buttons zen-mode-transition", {
  641. "layer-ui__wrapper__footer-left--transition-bottom": zenModeEnabled,
  642. })}
  643. >
  644. {actionManager.renderAction("undo", { size: "small" })}
  645. {actionManager.renderAction("redo", { size: "small" })}
  646. </div>
  647. )}
  648. </Section>
  649. </Stack.Col>
  650. </div>
  651. <div
  652. className={clsx(
  653. "layer-ui__wrapper__footer-center zen-mode-transition",
  654. {
  655. "layer-ui__wrapper__footer-left--transition-bottom": zenModeEnabled,
  656. },
  657. )}
  658. >
  659. {renderCustomFooter?.(false, appState)}
  660. </div>
  661. <div
  662. className={clsx(
  663. "layer-ui__wrapper__footer-right zen-mode-transition",
  664. {
  665. "transition-right disable-pointerEvents": zenModeEnabled,
  666. },
  667. )}
  668. >
  669. {actionManager.renderAction("toggleShortcuts")}
  670. </div>
  671. <button
  672. className={clsx("disable-zen-mode", {
  673. "disable-zen-mode--visible": showExitZenModeBtn,
  674. })}
  675. onClick={toggleZenMode}
  676. >
  677. {t("buttons.exitZenMode")}
  678. </button>
  679. </footer>
  680. );
  681. };
  682. const dialogs = (
  683. <>
  684. {appState.isLoading && <LoadingMessage />}
  685. {appState.errorMessage && (
  686. <ErrorDialog
  687. message={appState.errorMessage}
  688. onClose={() => setAppState({ errorMessage: null })}
  689. />
  690. )}
  691. {appState.showHelpDialog && (
  692. <HelpDialog
  693. onClose={() => {
  694. setAppState({ showHelpDialog: false });
  695. }}
  696. />
  697. )}
  698. {appState.pasteDialog.shown && (
  699. <PasteChartDialog
  700. setAppState={setAppState}
  701. appState={appState}
  702. onInsertChart={onInsertElements}
  703. onClose={() =>
  704. setAppState({
  705. pasteDialog: { shown: false, data: null },
  706. })
  707. }
  708. />
  709. )}
  710. </>
  711. );
  712. return isMobile ? (
  713. <>
  714. {dialogs}
  715. <MobileMenu
  716. appState={appState}
  717. elements={elements}
  718. actionManager={actionManager}
  719. libraryMenu={libraryMenu}
  720. renderJSONExportDialog={renderJSONExportDialog}
  721. renderImageExportDialog={renderImageExportDialog}
  722. setAppState={setAppState}
  723. onCollabButtonClick={onCollabButtonClick}
  724. onLockToggle={onLockToggle}
  725. canvas={canvas}
  726. isCollaborating={isCollaborating}
  727. renderCustomFooter={renderCustomFooter}
  728. viewModeEnabled={viewModeEnabled}
  729. showThemeBtn={showThemeBtn}
  730. renderTopRightUI={renderTopRightUI}
  731. />
  732. </>
  733. ) : (
  734. <div
  735. className={clsx("layer-ui__wrapper", {
  736. "disable-pointerEvents":
  737. appState.draggingElement ||
  738. appState.resizingElement ||
  739. (appState.editingElement && !isTextElement(appState.editingElement)),
  740. })}
  741. >
  742. {dialogs}
  743. {renderFixedSideContainer()}
  744. {renderBottomAppMenu()}
  745. {appState.scrolledOutside && (
  746. <button
  747. className="scroll-back-to-content"
  748. onClick={() => {
  749. setAppState({
  750. ...calculateScrollCenter(elements, appState, canvas),
  751. });
  752. }}
  753. >
  754. {t("buttons.scrollBackToContent")}
  755. </button>
  756. )}
  757. </div>
  758. );
  759. };
  760. const areEqual = (prev: LayerUIProps, next: LayerUIProps) => {
  761. const getNecessaryObj = (appState: AppState): Partial<AppState> => {
  762. const {
  763. suggestedBindings,
  764. startBoundElement: boundElement,
  765. ...ret
  766. } = appState;
  767. return ret;
  768. };
  769. const prevAppState = getNecessaryObj(prev.appState);
  770. const nextAppState = getNecessaryObj(next.appState);
  771. const keys = Object.keys(prevAppState) as (keyof Partial<AppState>)[];
  772. return (
  773. prev.renderCustomFooter === next.renderCustomFooter &&
  774. prev.langCode === next.langCode &&
  775. prev.elements === next.elements &&
  776. keys.every((key) => prevAppState[key] === nextAppState[key])
  777. );
  778. };
  779. export default React.memo(LayerUI, areEqual);