2020-03-07 10:20:38 -05:00
|
|
|
import React from "react";
|
|
|
|
import { showSelectedShapeActions } from "../element";
|
2020-04-02 16:52:24 +09:00
|
|
|
import { calculateScrollCenter } from "../scene";
|
2020-03-07 10:20:38 -05:00
|
|
|
import { exportCanvas } from "../data";
|
|
|
|
|
|
|
|
import { AppState } from "../types";
|
2020-04-08 09:49:52 -07:00
|
|
|
import { NonDeletedExcalidrawElement } from "../element/types";
|
2020-03-07 10:20:38 -05:00
|
|
|
|
|
|
|
import { ActionManager } from "../actions/manager";
|
|
|
|
import { Island } from "./Island";
|
|
|
|
import Stack from "./Stack";
|
|
|
|
import { FixedSideContainer } from "./FixedSideContainer";
|
|
|
|
import { LockIcon } from "./LockIcon";
|
|
|
|
import { ExportDialog, ExportCB } from "./ExportDialog";
|
|
|
|
import { LanguageList } from "./LanguageList";
|
|
|
|
import { t, languages, setLanguage } from "../i18n";
|
|
|
|
import { HintViewer } from "./HintViewer";
|
|
|
|
import useIsMobile from "../is-mobile";
|
|
|
|
|
|
|
|
import { ExportType } from "../scene/types";
|
|
|
|
import { MobileMenu } from "./MobileMenu";
|
|
|
|
import { ZoomActions, SelectedShapeActions, ShapesSwitcher } from "./Actions";
|
|
|
|
import { Section } from "./Section";
|
2020-03-11 19:42:18 +01:00
|
|
|
import { RoomDialog } from "./RoomDialog";
|
2020-04-03 12:50:51 +01:00
|
|
|
import { ErrorDialog } from "./ErrorDialog";
|
2020-04-05 15:58:00 +03:00
|
|
|
import { ShortcutsDialog } from "./ShortcutsDialog";
|
2020-03-26 18:28:26 +01:00
|
|
|
import { LoadingMessage } from "./LoadingMessage";
|
2020-04-12 15:57:57 +02:00
|
|
|
import { CLASSES } from "../constants";
|
2020-04-18 02:09:15 +05:30
|
|
|
import { shield } from "./icons";
|
|
|
|
import { GitHubCorner } from "./GitHubCorner";
|
|
|
|
|
|
|
|
import "./LayerUI.scss";
|
2020-03-07 10:20:38 -05:00
|
|
|
|
|
|
|
interface LayerUIProps {
|
|
|
|
actionManager: ActionManager;
|
|
|
|
appState: AppState;
|
|
|
|
canvas: HTMLCanvasElement | null;
|
|
|
|
setAppState: any;
|
2020-04-08 09:49:52 -07:00
|
|
|
elements: readonly NonDeletedExcalidrawElement[];
|
2020-03-11 19:42:18 +01:00
|
|
|
onRoomCreate: () => void;
|
2020-04-11 17:13:10 +01:00
|
|
|
onUsernameChange: (username: string) => void;
|
2020-03-11 19:42:18 +01:00
|
|
|
onRoomDestroy: () => void;
|
2020-03-24 19:51:49 +09:00
|
|
|
onLockToggle: () => void;
|
2020-03-07 10:20:38 -05:00
|
|
|
}
|
|
|
|
|
2020-04-18 01:54:19 +05:30
|
|
|
const LayerUI = ({
|
|
|
|
actionManager,
|
|
|
|
appState,
|
|
|
|
setAppState,
|
|
|
|
canvas,
|
|
|
|
elements,
|
|
|
|
onRoomCreate,
|
|
|
|
onUsernameChange,
|
|
|
|
onRoomDestroy,
|
|
|
|
onLockToggle,
|
|
|
|
}: LayerUIProps) => {
|
|
|
|
const isMobile = useIsMobile();
|
2020-03-07 10:20:38 -05:00
|
|
|
|
2020-04-18 02:09:15 +05:30
|
|
|
const renderEncryptedIcon = () => (
|
|
|
|
<a
|
|
|
|
className="encrypted-icon tooltip"
|
|
|
|
href="https://blog.excalidraw.com/end-to-end-encryption/"
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
>
|
2020-04-21 17:50:08 -04:00
|
|
|
<span className="tooltip-text" dir="auto">
|
|
|
|
{t("encrypted.tooltip")}
|
|
|
|
</span>
|
2020-04-18 02:09:15 +05:30
|
|
|
{shield}
|
|
|
|
</a>
|
|
|
|
);
|
|
|
|
|
2020-04-18 01:54:19 +05:30
|
|
|
const renderExportDialog = () => {
|
|
|
|
const createExporter = (type: ExportType): ExportCB => (
|
|
|
|
exportedElements,
|
|
|
|
scale,
|
|
|
|
) => {
|
|
|
|
if (canvas) {
|
|
|
|
exportCanvas(type, exportedElements, appState, canvas, {
|
|
|
|
exportBackground: appState.exportBackground,
|
|
|
|
name: appState.name,
|
|
|
|
viewBackgroundColor: appState.viewBackgroundColor,
|
|
|
|
scale,
|
2020-04-19 20:50:23 +01:00
|
|
|
shouldAddWatermark: appState.shouldAddWatermark,
|
2020-04-18 01:54:19 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<ExportDialog
|
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")}
|
|
|
|
onExportToBackend={(exportedElements) => {
|
|
|
|
if (canvas) {
|
|
|
|
exportCanvas(
|
|
|
|
"backend",
|
|
|
|
exportedElements,
|
|
|
|
{
|
|
|
|
...appState,
|
|
|
|
selectedElementIds: {},
|
|
|
|
},
|
|
|
|
canvas,
|
|
|
|
appState,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}}
|
2020-03-07 10:20:38 -05:00
|
|
|
/>
|
2020-04-18 01:54:19 +05:30
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderCanvasActions = () => (
|
|
|
|
<Section heading="canvasActions">
|
|
|
|
{/* the zIndex ensures this menu has higher stacking order,
|
|
|
|
see https://github.com/excalidraw/excalidraw/pull/1445 */}
|
|
|
|
<Island padding={4} style={{ zIndex: 1 }}>
|
|
|
|
<Stack.Col gap={4}>
|
|
|
|
<Stack.Row gap={1} justifyContent="space-between">
|
|
|
|
{actionManager.renderAction("loadScene")}
|
|
|
|
{actionManager.renderAction("saveScene")}
|
|
|
|
{renderExportDialog()}
|
|
|
|
{actionManager.renderAction("clearCanvas")}
|
|
|
|
<RoomDialog
|
|
|
|
isCollaborating={appState.isCollaborating}
|
|
|
|
collaboratorCount={appState.collaborators.size}
|
|
|
|
username={appState.username}
|
|
|
|
onUsernameChange={onUsernameChange}
|
|
|
|
onRoomCreate={onRoomCreate}
|
|
|
|
onRoomDestroy={onRoomDestroy}
|
|
|
|
/>
|
|
|
|
</Stack.Row>
|
|
|
|
{actionManager.renderAction("changeViewBackgroundColor")}
|
|
|
|
</Stack.Col>
|
|
|
|
</Island>
|
|
|
|
</Section>
|
|
|
|
);
|
|
|
|
|
|
|
|
const renderSelectedShapeActions = () => (
|
|
|
|
<Section heading="selectedShapeActions">
|
|
|
|
<Island className={CLASSES.SHAPE_ACTIONS_MENU} padding={4}>
|
|
|
|
<SelectedShapeActions
|
|
|
|
appState={appState}
|
|
|
|
elements={elements}
|
|
|
|
renderAction={actionManager.renderAction}
|
|
|
|
elementType={appState.elementType}
|
|
|
|
/>
|
|
|
|
</Island>
|
|
|
|
</Section>
|
|
|
|
);
|
|
|
|
|
|
|
|
const renderFixedSideContainer = () => {
|
|
|
|
const shouldRenderSelectedShapeActions = showSelectedShapeActions(
|
|
|
|
appState,
|
|
|
|
elements,
|
|
|
|
);
|
|
|
|
return (
|
|
|
|
<FixedSideContainer side="top">
|
|
|
|
<HintViewer appState={appState} elements={elements} />
|
|
|
|
<div className="App-menu App-menu_top">
|
|
|
|
<Stack.Col gap={4}>
|
|
|
|
{renderCanvasActions()}
|
|
|
|
{shouldRenderSelectedShapeActions && renderSelectedShapeActions()}
|
|
|
|
</Stack.Col>
|
|
|
|
<Section heading="shapes">
|
|
|
|
{(heading) => (
|
|
|
|
<Stack.Col gap={4} align="start">
|
|
|
|
<Stack.Row gap={1}>
|
|
|
|
<Island padding={1}>
|
|
|
|
{heading}
|
|
|
|
<Stack.Row gap={1}>
|
|
|
|
<ShapesSwitcher
|
|
|
|
elementType={appState.elementType}
|
|
|
|
setAppState={setAppState}
|
2020-03-11 19:42:18 +01:00
|
|
|
/>
|
2020-03-07 10:20:38 -05:00
|
|
|
</Stack.Row>
|
|
|
|
</Island>
|
2020-04-18 01:54:19 +05:30
|
|
|
<LockIcon
|
|
|
|
checked={appState.elementLocked}
|
|
|
|
onChange={onLockToggle}
|
|
|
|
title={t("toolBar.lock")}
|
2020-03-07 10:20:38 -05:00
|
|
|
/>
|
2020-04-18 01:54:19 +05:30
|
|
|
</Stack.Row>
|
|
|
|
</Stack.Col>
|
|
|
|
)}
|
|
|
|
</Section>
|
|
|
|
<div />
|
|
|
|
</div>
|
|
|
|
<div className="App-menu App-menu_bottom">
|
|
|
|
<Stack.Col gap={2}>
|
|
|
|
<Section heading="canvasActions">
|
|
|
|
<Island padding={1}>
|
|
|
|
<ZoomActions
|
|
|
|
renderAction={actionManager.renderAction}
|
|
|
|
zoom={appState.zoom}
|
|
|
|
/>
|
|
|
|
</Island>
|
2020-04-18 02:09:15 +05:30
|
|
|
{renderEncryptedIcon()}
|
2020-04-18 01:54:19 +05:30
|
|
|
</Section>
|
|
|
|
</Stack.Col>
|
|
|
|
</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-04-18 01:54:19 +05:30
|
|
|
const renderFooter = () => (
|
|
|
|
<footer role="contentinfo">
|
|
|
|
<LanguageList
|
|
|
|
onChange={(lng) => {
|
|
|
|
setLanguage(lng);
|
|
|
|
setAppState({});
|
|
|
|
}}
|
|
|
|
languages={languages}
|
|
|
|
floating
|
|
|
|
/>
|
|
|
|
{actionManager.renderAction("toggleShortcuts")}
|
|
|
|
{appState.scrolledOutside && (
|
|
|
|
<button
|
|
|
|
className="scroll-back-to-content"
|
|
|
|
onClick={() => {
|
|
|
|
setAppState({ ...calculateScrollCenter(elements) });
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{t("buttons.scrollBackToContent")}
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</footer>
|
|
|
|
);
|
2020-03-07 10:20:38 -05:00
|
|
|
|
2020-04-18 01:54:19 +05:30
|
|
|
return isMobile ? (
|
|
|
|
<MobileMenu
|
|
|
|
appState={appState}
|
|
|
|
elements={elements}
|
|
|
|
actionManager={actionManager}
|
|
|
|
exportButton={renderExportDialog()}
|
|
|
|
setAppState={setAppState}
|
|
|
|
onUsernameChange={onUsernameChange}
|
|
|
|
onRoomCreate={onRoomCreate}
|
|
|
|
onRoomDestroy={onRoomDestroy}
|
|
|
|
onLockToggle={onLockToggle}
|
|
|
|
/>
|
|
|
|
) : (
|
2020-04-18 02:09:15 +05:30
|
|
|
<div className="layer-ui__wrapper">
|
2020-04-18 01:54:19 +05:30
|
|
|
{appState.isLoading && <LoadingMessage />}
|
|
|
|
{appState.errorMessage && (
|
|
|
|
<ErrorDialog
|
|
|
|
message={appState.errorMessage}
|
|
|
|
onClose={() => setAppState({ errorMessage: null })}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{appState.showShortcutsDialog && (
|
|
|
|
<ShortcutsDialog
|
|
|
|
onClose={() => setAppState({ showShortcutsDialog: null })}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{renderFixedSideContainer()}
|
|
|
|
<aside>
|
|
|
|
<GitHubCorner />
|
|
|
|
</aside>
|
|
|
|
{renderFooter()}
|
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> => {
|
|
|
|
const {
|
|
|
|
draggingElement,
|
|
|
|
resizingElement,
|
|
|
|
multiElement,
|
|
|
|
editingElement,
|
|
|
|
isResizing,
|
|
|
|
cursorX,
|
|
|
|
cursorY,
|
|
|
|
...ret
|
|
|
|
} = appState;
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
const prevAppState = getNecessaryObj(prev.appState);
|
|
|
|
const nextAppState = getNecessaryObj(next.appState);
|
|
|
|
|
|
|
|
const keys = Object.keys(prevAppState) as (keyof Partial<AppState>)[];
|
|
|
|
|
|
|
|
return (
|
|
|
|
prev.elements === next.elements &&
|
|
|
|
keys.every((key) => prevAppState[key] === nextAppState[key])
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default React.memo(LayerUI, areEqual);
|