Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

LayerUI.tsx 25KB

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