2020-01-05 15:10:42 +01:00
|
|
|
import React from "react";
|
|
|
|
import ReactDOM from "react-dom";
|
2020-01-09 02:00:59 +04:00
|
|
|
|
2020-01-13 11:04:28 -08:00
|
|
|
import rough from "roughjs/bin/rough";
|
2020-01-09 02:00:59 +04:00
|
|
|
import { RoughCanvas } from "roughjs/bin/canvas";
|
2020-01-05 15:10:42 +01:00
|
|
|
|
2020-01-08 19:54:42 +01:00
|
|
|
import {
|
|
|
|
newElement,
|
2020-01-21 00:16:22 +01:00
|
|
|
newTextElement,
|
2020-01-08 19:54:42 +01:00
|
|
|
duplicateElement,
|
|
|
|
resizeTest,
|
2020-01-24 20:45:52 +01:00
|
|
|
normalizeResizeHandle,
|
2020-01-16 22:16:11 +00:00
|
|
|
isInvisiblySmallElement,
|
2020-01-08 19:54:42 +01:00
|
|
|
isTextElement,
|
2020-01-09 12:34:46 +01:00
|
|
|
textWysiwyg,
|
2020-01-26 20:15:08 +01:00
|
|
|
getCommonBounds,
|
2020-01-23 09:21:04 +00:00
|
|
|
getCursorForResizingElement,
|
|
|
|
getPerfectElementSize,
|
2020-01-24 20:45:52 +01:00
|
|
|
normalizeDimensions,
|
2020-01-08 19:54:42 +01:00
|
|
|
} from "./element";
|
2020-01-06 19:34:22 +04:00
|
|
|
import {
|
2020-01-06 20:24:54 +04:00
|
|
|
clearSelection,
|
|
|
|
deleteSelectedElements,
|
2020-01-13 04:32:25 +05:00
|
|
|
getElementsWithinSelection,
|
2020-01-06 20:24:54 +04:00
|
|
|
isOverScrollBars,
|
|
|
|
saveToLocalStorage,
|
|
|
|
getElementAtPosition,
|
2020-01-09 01:09:09 +05:00
|
|
|
createScene,
|
2020-01-15 20:42:02 +05:00
|
|
|
getElementContainingPosition,
|
|
|
|
hasBackground,
|
|
|
|
hasStroke,
|
|
|
|
hasText,
|
2020-01-20 00:56:19 -05:00
|
|
|
exportCanvas,
|
2020-02-07 23:46:19 +01:00
|
|
|
loadScene,
|
2020-02-01 16:52:10 +00:00
|
|
|
calculateScrollCenter,
|
2020-02-02 22:02:13 +01:00
|
|
|
loadFromBlob,
|
2020-01-06 20:24:54 +04:00
|
|
|
} from "./scene";
|
2020-01-07 19:04:52 +04:00
|
|
|
|
|
|
|
import { renderScene } from "./renderer";
|
2020-01-06 20:24:54 +04:00
|
|
|
import { AppState } from "./types";
|
2020-01-21 00:16:22 +01:00
|
|
|
import { ExcalidrawElement } from "./element/types";
|
2020-01-06 13:33:22 +04:00
|
|
|
|
2020-02-01 02:58:16 +00:00
|
|
|
import {
|
2020-02-04 11:50:18 +01:00
|
|
|
isWritableElement,
|
2020-02-05 22:47:10 +04:00
|
|
|
isInputLike,
|
2020-02-07 18:42:24 +01:00
|
|
|
isToolIcon,
|
2020-02-01 02:58:16 +00:00
|
|
|
debounce,
|
|
|
|
capitalizeString,
|
|
|
|
distance,
|
2020-02-01 15:49:18 +04:00
|
|
|
distance2d,
|
2020-02-10 15:09:50 +01:00
|
|
|
resetCursor,
|
2020-02-01 02:58:16 +00:00
|
|
|
} from "./utils";
|
2020-01-19 13:21:33 -08:00
|
|
|
import { KEYS, isArrowKey } from "./keys";
|
2020-01-06 20:24:54 +04:00
|
|
|
|
2020-01-15 20:42:02 +05:00
|
|
|
import { findShapeByKey, shapesShortcutKeys, SHAPES } from "./shapes";
|
2020-01-06 21:58:48 +04:00
|
|
|
import { createHistory } from "./history";
|
2020-01-05 15:10:42 +01:00
|
|
|
|
2020-01-07 07:50:59 +05:00
|
|
|
import ContextMenu from "./components/ContextMenu";
|
2020-01-05 15:10:42 +01:00
|
|
|
|
2020-01-09 00:06:25 +05:00
|
|
|
import "./styles.scss";
|
2020-01-08 23:56:35 -03:00
|
|
|
import { getElementWithResizeHandler } from "./element/resizeTest";
|
2020-01-12 02:22:03 +04:00
|
|
|
import {
|
|
|
|
ActionManager,
|
|
|
|
actionDeleteSelected,
|
|
|
|
actionSendBackward,
|
|
|
|
actionBringForward,
|
|
|
|
actionSendToBack,
|
|
|
|
actionBringToFront,
|
|
|
|
actionSelectAll,
|
|
|
|
actionChangeStrokeColor,
|
|
|
|
actionChangeBackgroundColor,
|
|
|
|
actionChangeOpacity,
|
|
|
|
actionChangeStrokeWidth,
|
|
|
|
actionChangeFillStyle,
|
|
|
|
actionChangeSloppiness,
|
|
|
|
actionChangeFontSize,
|
|
|
|
actionChangeFontFamily,
|
|
|
|
actionChangeViewBackgroundColor,
|
|
|
|
actionClearCanvas,
|
|
|
|
actionChangeProjectName,
|
|
|
|
actionChangeExportBackground,
|
|
|
|
actionLoadScene,
|
|
|
|
actionSaveScene,
|
|
|
|
actionCopyStyles,
|
2020-01-24 12:04:54 +02:00
|
|
|
actionPasteStyles,
|
2020-02-01 15:49:18 +04:00
|
|
|
actionFinalize,
|
2020-01-12 02:22:03 +04:00
|
|
|
} from "./actions";
|
2020-01-12 07:10:15 -08:00
|
|
|
import { Action, ActionResult } from "./actions/types";
|
2020-01-11 20:34:21 -08:00
|
|
|
import { getDefaultAppState } from "./appState";
|
2020-01-15 20:42:02 +05:00
|
|
|
import { Island } from "./components/Island";
|
|
|
|
import Stack from "./components/Stack";
|
|
|
|
import { FixedSideContainer } from "./components/FixedSideContainer";
|
2020-01-25 14:52:03 -03:00
|
|
|
import { ToolButton } from "./components/ToolButton";
|
2020-01-20 15:52:19 -08:00
|
|
|
import { LockIcon } from "./components/LockIcon";
|
2020-01-15 20:42:02 +05:00
|
|
|
import { ExportDialog } from "./components/ExportDialog";
|
2020-01-22 16:25:04 +02:00
|
|
|
import { LanguageList } from "./components/LanguageList";
|
2020-02-01 15:49:18 +04:00
|
|
|
import { Point } from "roughjs/bin/geometry";
|
2020-01-31 21:06:06 +00:00
|
|
|
import { t, languages, setLanguage, getLanguage } from "./i18n";
|
2020-02-03 21:52:21 +04:00
|
|
|
import { HintViewer } from "./components/HintViewer";
|
2020-01-09 00:06:25 +05:00
|
|
|
|
2020-02-07 18:42:24 +01:00
|
|
|
import { copyToAppClipboard, getClipboardContent } from "./clipboard";
|
2020-02-04 11:50:18 +01:00
|
|
|
|
2020-01-09 19:22:04 +04:00
|
|
|
let { elements } = createScene();
|
2020-01-06 21:58:48 +04:00
|
|
|
const { history } = createHistory();
|
2020-01-05 22:26:00 +00:00
|
|
|
|
2020-01-30 17:08:59 -03:00
|
|
|
function setCursorForShape(shape: string) {
|
|
|
|
if (shape === "selection") {
|
|
|
|
resetCursor();
|
|
|
|
} else {
|
|
|
|
document.documentElement.style.cursor =
|
|
|
|
shape === "text" ? CURSOR_TYPE.TEXT : CURSOR_TYPE.CROSSHAIR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 15:49:18 +04:00
|
|
|
const DRAGGING_THRESHOLD = 10; // 10px
|
2020-01-05 15:10:42 +01:00
|
|
|
const ELEMENT_SHIFT_TRANSLATE_AMOUNT = 5;
|
|
|
|
const ELEMENT_TRANSLATE_AMOUNT = 1;
|
2020-01-09 01:09:09 +05:00
|
|
|
const TEXT_TO_CENTER_SNAP_THRESHOLD = 30;
|
2020-01-22 19:36:08 +06:00
|
|
|
const CURSOR_TYPE = {
|
|
|
|
TEXT: "text",
|
2020-01-24 12:04:54 +02:00
|
|
|
CROSSHAIR: "crosshair",
|
2020-01-30 17:08:59 -03:00
|
|
|
GRABBING: "grabbing",
|
|
|
|
};
|
|
|
|
const MOUSE_BUTTON = {
|
|
|
|
MAIN: 0,
|
|
|
|
WHEEL: 1,
|
|
|
|
SECONDARY: 2,
|
2020-01-22 19:36:08 +06:00
|
|
|
};
|
2020-01-05 15:10:42 +01:00
|
|
|
|
|
|
|
let lastCanvasWidth = -1;
|
|
|
|
let lastCanvasHeight = -1;
|
|
|
|
|
|
|
|
let lastMouseUp: ((e: any) => void) | null = null;
|
|
|
|
|
2020-01-08 23:56:35 -03:00
|
|
|
export function viewportCoordsToSceneCoords(
|
|
|
|
{ clientX, clientY }: { clientX: number; clientY: number },
|
2020-01-24 12:04:54 +02:00
|
|
|
{ scrollX, scrollY }: { scrollX: number; scrollY: number },
|
2020-01-08 23:56:35 -03:00
|
|
|
) {
|
2020-02-08 16:20:14 -08:00
|
|
|
const x = clientX - scrollX;
|
|
|
|
const y = clientY - scrollY;
|
2020-01-08 23:56:35 -03:00
|
|
|
return { x, y };
|
|
|
|
}
|
|
|
|
|
2020-01-25 11:38:08 -08:00
|
|
|
let cursorX = 0;
|
|
|
|
let cursorY = 0;
|
2020-01-30 17:08:59 -03:00
|
|
|
let isHoldingSpace: boolean = false;
|
|
|
|
let isPanning: boolean = false;
|
|
|
|
let isHoldingMouseButton: boolean = false;
|
2020-01-25 11:38:08 -08:00
|
|
|
|
2020-02-05 22:47:10 +04:00
|
|
|
interface LayerUIProps {
|
|
|
|
actionManager: ActionManager;
|
|
|
|
appState: AppState;
|
|
|
|
canvas: HTMLCanvasElement | null;
|
|
|
|
setAppState: any;
|
|
|
|
elements: readonly ExcalidrawElement[];
|
2020-02-07 23:46:19 +01:00
|
|
|
language: string;
|
2020-02-05 22:47:10 +04:00
|
|
|
setElements: (elements: readonly ExcalidrawElement[]) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
const LayerUI = React.memo(
|
|
|
|
({
|
|
|
|
actionManager,
|
|
|
|
appState,
|
|
|
|
setAppState,
|
|
|
|
canvas,
|
|
|
|
elements,
|
2020-02-07 23:46:19 +01:00
|
|
|
language,
|
2020-02-05 22:47:10 +04:00
|
|
|
setElements,
|
|
|
|
}: LayerUIProps) => {
|
|
|
|
function renderCanvasActions() {
|
|
|
|
return (
|
|
|
|
<Stack.Col gap={4}>
|
|
|
|
<Stack.Row justifyContent={"space-between"}>
|
|
|
|
{actionManager.renderAction("loadScene")}
|
|
|
|
{actionManager.renderAction("saveScene")}
|
|
|
|
<ExportDialog
|
|
|
|
elements={elements}
|
|
|
|
appState={appState}
|
|
|
|
actionManager={actionManager}
|
|
|
|
onExportToPng={(exportedElements, scale) => {
|
|
|
|
if (canvas) {
|
|
|
|
exportCanvas("png", exportedElements, canvas, {
|
|
|
|
exportBackground: appState.exportBackground,
|
|
|
|
name: appState.name,
|
|
|
|
viewBackgroundColor: appState.viewBackgroundColor,
|
|
|
|
scale,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onExportToSvg={(exportedElements, scale) => {
|
|
|
|
if (canvas) {
|
|
|
|
exportCanvas("svg", exportedElements, canvas, {
|
|
|
|
exportBackground: appState.exportBackground,
|
|
|
|
name: appState.name,
|
|
|
|
viewBackgroundColor: appState.viewBackgroundColor,
|
|
|
|
scale,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onExportToClipboard={(exportedElements, scale) => {
|
|
|
|
if (canvas) {
|
|
|
|
exportCanvas("clipboard", exportedElements, canvas, {
|
|
|
|
exportBackground: appState.exportBackground,
|
|
|
|
name: appState.name,
|
|
|
|
viewBackgroundColor: appState.viewBackgroundColor,
|
|
|
|
scale,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onExportToBackend={exportedElements => {
|
|
|
|
if (canvas) {
|
|
|
|
exportCanvas(
|
|
|
|
"backend",
|
|
|
|
exportedElements.map(element => ({
|
|
|
|
...element,
|
|
|
|
isSelected: false,
|
|
|
|
})),
|
|
|
|
canvas,
|
|
|
|
appState,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
{actionManager.renderAction("clearCanvas")}
|
|
|
|
</Stack.Row>
|
|
|
|
{actionManager.renderAction("changeViewBackgroundColor")}
|
|
|
|
</Stack.Col>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderSelectedShapeActions(
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
) {
|
|
|
|
const { elementType, editingElement } = appState;
|
|
|
|
const targetElements = editingElement
|
|
|
|
? [editingElement]
|
|
|
|
: elements.filter(el => el.isSelected);
|
|
|
|
if (!targetElements.length && elementType === "selection") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Island padding={4}>
|
|
|
|
<div className="panelColumn">
|
|
|
|
{actionManager.renderAction("changeStrokeColor")}
|
|
|
|
{(hasBackground(elementType) ||
|
|
|
|
targetElements.some(element => hasBackground(element.type))) && (
|
|
|
|
<>
|
|
|
|
{actionManager.renderAction("changeBackgroundColor")}
|
|
|
|
|
|
|
|
{actionManager.renderAction("changeFillStyle")}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{(hasStroke(elementType) ||
|
|
|
|
targetElements.some(element => hasStroke(element.type))) && (
|
|
|
|
<>
|
|
|
|
{actionManager.renderAction("changeStrokeWidth")}
|
|
|
|
|
|
|
|
{actionManager.renderAction("changeSloppiness")}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{(hasText(elementType) ||
|
|
|
|
targetElements.some(element => hasText(element.type))) && (
|
|
|
|
<>
|
|
|
|
{actionManager.renderAction("changeFontSize")}
|
|
|
|
|
|
|
|
{actionManager.renderAction("changeFontFamily")}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{actionManager.renderAction("changeOpacity")}
|
|
|
|
|
2020-02-09 09:07:34 -05:00
|
|
|
<fieldset>
|
|
|
|
<legend>{t("labels.layers")}</legend>
|
|
|
|
<div className="buttonList">
|
|
|
|
{actionManager.renderAction("sendToBack")}
|
|
|
|
{actionManager.renderAction("sendBackward")}
|
|
|
|
{actionManager.renderAction("bringToFront")}
|
|
|
|
{actionManager.renderAction("bringForward")}
|
|
|
|
</div>
|
|
|
|
</fieldset>
|
|
|
|
|
2020-02-05 22:47:10 +04:00
|
|
|
{actionManager.renderAction("deleteSelectedElements")}
|
|
|
|
</div>
|
|
|
|
</Island>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderShapesSwitcher() {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{SHAPES.map(({ value, icon }, index) => {
|
|
|
|
const label = t(`toolBar.${value}`);
|
|
|
|
return (
|
|
|
|
<ToolButton
|
|
|
|
key={value}
|
|
|
|
type="radio"
|
|
|
|
icon={icon}
|
|
|
|
checked={appState.elementType === value}
|
|
|
|
name="editor-current-shape"
|
|
|
|
title={`${capitalizeString(label)} — ${
|
|
|
|
capitalizeString(value)[0]
|
|
|
|
}, ${index + 1}`}
|
|
|
|
keyBindingLabel={`${index + 1}`}
|
|
|
|
aria-label={capitalizeString(label)}
|
|
|
|
aria-keyshortcuts={`${label[0]} ${index + 1}`}
|
|
|
|
onChange={() => {
|
|
|
|
setAppState({ elementType: value, multiElement: null });
|
|
|
|
setElements(clearSelection(elements));
|
|
|
|
document.documentElement.style.cursor =
|
|
|
|
value === "text" ? CURSOR_TYPE.TEXT : CURSOR_TYPE.CROSSHAIR;
|
|
|
|
setAppState({});
|
|
|
|
}}
|
|
|
|
></ToolButton>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-02-07 23:46:19 +01:00
|
|
|
<>
|
|
|
|
<FixedSideContainer side="top">
|
|
|
|
<div className="App-menu App-menu_top">
|
|
|
|
<Stack.Col gap={4} align="end">
|
|
|
|
<section
|
|
|
|
className="App-right-menu"
|
|
|
|
aria-labelledby="canvas-actions-title"
|
|
|
|
>
|
|
|
|
<h2 className="visually-hidden" id="canvas-actions-title">
|
|
|
|
{t("headings.canvasActions")}
|
|
|
|
</h2>
|
|
|
|
<Island padding={4}>{renderCanvasActions()}</Island>
|
|
|
|
</section>
|
|
|
|
<section
|
|
|
|
className="App-right-menu"
|
|
|
|
aria-labelledby="selected-shape-title"
|
|
|
|
>
|
|
|
|
<h2 className="visually-hidden" id="selected-shape-title">
|
|
|
|
{t("headings.selectedShapeActions")}
|
|
|
|
</h2>
|
|
|
|
{renderSelectedShapeActions(elements)}
|
|
|
|
</section>
|
|
|
|
</Stack.Col>
|
|
|
|
<section aria-labelledby="shapes-title">
|
|
|
|
<Stack.Col gap={4} align="start">
|
|
|
|
<Stack.Row gap={1}>
|
|
|
|
<Island padding={1}>
|
|
|
|
<h2 className="visually-hidden" id="shapes-title">
|
|
|
|
{t("headings.shapes")}
|
|
|
|
</h2>
|
|
|
|
<Stack.Row gap={1}>{renderShapesSwitcher()}</Stack.Row>
|
|
|
|
</Island>
|
|
|
|
<LockIcon
|
|
|
|
checked={appState.elementLocked}
|
|
|
|
onChange={() => {
|
|
|
|
setAppState({
|
|
|
|
elementLocked: !appState.elementLocked,
|
|
|
|
elementType: appState.elementLocked
|
|
|
|
? "selection"
|
|
|
|
: appState.elementType,
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
title={t("toolBar.lock")}
|
|
|
|
/>
|
|
|
|
</Stack.Row>
|
|
|
|
</Stack.Col>
|
2020-02-05 22:47:10 +04:00
|
|
|
</section>
|
2020-02-07 23:46:19 +01:00
|
|
|
<div />
|
|
|
|
</div>
|
|
|
|
</FixedSideContainer>
|
|
|
|
<footer role="contentinfo">
|
|
|
|
<HintViewer
|
|
|
|
elementType={appState.elementType}
|
|
|
|
multiMode={appState.multiElement !== null}
|
|
|
|
isResizing={appState.isResizing}
|
|
|
|
elements={elements}
|
|
|
|
/>
|
|
|
|
<LanguageList
|
|
|
|
onChange={lng => {
|
|
|
|
setLanguage(lng);
|
|
|
|
setAppState({});
|
|
|
|
}}
|
|
|
|
languages={languages}
|
|
|
|
currentLanguage={language}
|
|
|
|
/>
|
|
|
|
{appState.scrolledOutside && (
|
|
|
|
<button
|
|
|
|
className="scroll-back-to-content"
|
|
|
|
onClick={() => {
|
|
|
|
setAppState({ ...calculateScrollCenter(elements) });
|
|
|
|
}}
|
2020-02-05 22:47:10 +04:00
|
|
|
>
|
2020-02-07 23:46:19 +01:00
|
|
|
{t("buttons.scrollBackToContent")}
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</footer>
|
|
|
|
</>
|
2020-02-05 22:47:10 +04:00
|
|
|
);
|
|
|
|
},
|
|
|
|
(prev, next) => {
|
|
|
|
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 (
|
2020-02-07 23:46:19 +01:00
|
|
|
prev.language === next.language &&
|
2020-02-05 22:47:10 +04:00
|
|
|
prev.elements === next.elements &&
|
|
|
|
keys.every(k => prevAppState[k] === nextAppState[k])
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-01-21 01:14:10 +02:00
|
|
|
export class App extends React.Component<any, AppState> {
|
2020-01-09 02:00:59 +04:00
|
|
|
canvas: HTMLCanvasElement | null = null;
|
|
|
|
rc: RoughCanvas | null = null;
|
|
|
|
|
2020-02-05 22:47:10 +04:00
|
|
|
actionManager: ActionManager;
|
2020-01-12 07:10:15 -08:00
|
|
|
canvasOnlyActions: Array<Action>;
|
2020-01-12 02:22:03 +04:00
|
|
|
constructor(props: any) {
|
|
|
|
super(props);
|
2020-02-05 22:47:10 +04:00
|
|
|
this.actionManager = new ActionManager(
|
|
|
|
this.syncActionResult,
|
|
|
|
() => this.state,
|
|
|
|
() => elements,
|
|
|
|
);
|
2020-02-01 15:49:18 +04:00
|
|
|
this.actionManager.registerAction(actionFinalize);
|
2020-01-12 02:22:03 +04:00
|
|
|
this.actionManager.registerAction(actionDeleteSelected);
|
|
|
|
this.actionManager.registerAction(actionSendToBack);
|
|
|
|
this.actionManager.registerAction(actionBringToFront);
|
|
|
|
this.actionManager.registerAction(actionSendBackward);
|
|
|
|
this.actionManager.registerAction(actionBringForward);
|
|
|
|
this.actionManager.registerAction(actionSelectAll);
|
|
|
|
|
|
|
|
this.actionManager.registerAction(actionChangeStrokeColor);
|
|
|
|
this.actionManager.registerAction(actionChangeBackgroundColor);
|
|
|
|
this.actionManager.registerAction(actionChangeFillStyle);
|
|
|
|
this.actionManager.registerAction(actionChangeStrokeWidth);
|
|
|
|
this.actionManager.registerAction(actionChangeOpacity);
|
|
|
|
this.actionManager.registerAction(actionChangeSloppiness);
|
|
|
|
this.actionManager.registerAction(actionChangeFontSize);
|
|
|
|
this.actionManager.registerAction(actionChangeFontFamily);
|
|
|
|
|
|
|
|
this.actionManager.registerAction(actionChangeViewBackgroundColor);
|
|
|
|
this.actionManager.registerAction(actionClearCanvas);
|
|
|
|
|
|
|
|
this.actionManager.registerAction(actionChangeProjectName);
|
|
|
|
this.actionManager.registerAction(actionChangeExportBackground);
|
|
|
|
this.actionManager.registerAction(actionSaveScene);
|
|
|
|
this.actionManager.registerAction(actionLoadScene);
|
|
|
|
|
|
|
|
this.actionManager.registerAction(actionCopyStyles);
|
|
|
|
this.actionManager.registerAction(actionPasteStyles);
|
2020-01-12 07:10:15 -08:00
|
|
|
|
|
|
|
this.canvasOnlyActions = [actionSelectAll];
|
2020-01-12 02:22:03 +04:00
|
|
|
}
|
|
|
|
|
2020-02-07 23:46:19 +01:00
|
|
|
private syncActionResult = (
|
|
|
|
res: ActionResult,
|
|
|
|
commitToHistory: boolean = true,
|
|
|
|
) => {
|
2020-02-12 02:19:43 +04:00
|
|
|
if (this.unmounted) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-07 23:46:19 +01:00
|
|
|
if (res.elements) {
|
2020-01-12 02:22:03 +04:00
|
|
|
elements = res.elements;
|
2020-02-07 23:46:19 +01:00
|
|
|
if (commitToHistory) {
|
|
|
|
history.resumeRecording();
|
|
|
|
}
|
2020-01-26 19:08:46 +00:00
|
|
|
this.setState({});
|
2020-01-12 02:22:03 +04:00
|
|
|
}
|
|
|
|
|
2020-02-07 23:46:19 +01:00
|
|
|
if (res.appState) {
|
|
|
|
if (commitToHistory) {
|
|
|
|
history.resumeRecording();
|
|
|
|
}
|
2020-01-12 02:22:03 +04:00
|
|
|
this.setState({ ...res.appState });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-11 21:39:16 -08:00
|
|
|
private onCut = (e: ClipboardEvent) => {
|
2020-02-04 11:50:18 +01:00
|
|
|
if (isWritableElement(e.target)) {
|
2020-02-02 20:04:35 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-02-04 11:50:18 +01:00
|
|
|
copyToAppClipboard(elements);
|
2020-01-11 21:39:16 -08:00
|
|
|
elements = deleteSelectedElements(elements);
|
2020-02-05 22:47:10 +04:00
|
|
|
history.resumeRecording();
|
2020-01-26 19:08:46 +00:00
|
|
|
this.setState({});
|
2020-01-11 21:39:16 -08:00
|
|
|
e.preventDefault();
|
|
|
|
};
|
|
|
|
private onCopy = (e: ClipboardEvent) => {
|
2020-02-04 11:50:18 +01:00
|
|
|
if (isWritableElement(e.target)) {
|
2020-02-02 20:04:35 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-02-04 11:50:18 +01:00
|
|
|
copyToAppClipboard(elements);
|
2020-01-11 21:39:16 -08:00
|
|
|
e.preventDefault();
|
|
|
|
};
|
|
|
|
|
2020-01-20 18:37:42 +01:00
|
|
|
private onUnload = () => {
|
2020-01-30 17:08:59 -03:00
|
|
|
isHoldingSpace = false;
|
2020-01-20 18:37:42 +01:00
|
|
|
this.saveDebounced();
|
|
|
|
this.saveDebounced.flush();
|
|
|
|
};
|
|
|
|
|
2020-02-12 02:19:43 +04:00
|
|
|
private disableEvent: EventHandlerNonNull = e => {
|
|
|
|
e.preventDefault();
|
|
|
|
};
|
|
|
|
|
|
|
|
private unmounted = false;
|
2020-01-30 16:39:37 -03:00
|
|
|
public async componentDidMount() {
|
|
|
|
document.addEventListener("copy", this.onCopy);
|
2020-02-07 18:42:24 +01:00
|
|
|
document.addEventListener("paste", this.pasteFromClipboard);
|
2020-01-30 16:39:37 -03:00
|
|
|
document.addEventListener("cut", this.onCut);
|
|
|
|
|
|
|
|
document.addEventListener("keydown", this.onKeyDown, false);
|
2020-01-30 17:08:59 -03:00
|
|
|
document.addEventListener("keyup", this.onKeyUp, { passive: true });
|
2020-01-30 16:39:37 -03:00
|
|
|
document.addEventListener("mousemove", this.updateCurrentCursorPosition);
|
|
|
|
window.addEventListener("resize", this.onResize, false);
|
|
|
|
window.addEventListener("unload", this.onUnload, false);
|
2020-01-30 17:08:59 -03:00
|
|
|
window.addEventListener("blur", this.onUnload, false);
|
2020-02-12 02:19:43 +04:00
|
|
|
window.addEventListener("dragover", this.disableEvent, false);
|
|
|
|
window.addEventListener("drop", this.disableEvent, false);
|
2020-01-30 16:39:37 -03:00
|
|
|
|
|
|
|
const searchParams = new URLSearchParams(window.location.search);
|
|
|
|
const id = searchParams.get("id");
|
|
|
|
|
2020-02-05 07:35:51 -08:00
|
|
|
if (id) {
|
|
|
|
// Backwards compatibility with legacy url format
|
2020-02-12 02:19:43 +04:00
|
|
|
const scene = await loadScene(id);
|
|
|
|
this.syncActionResult(scene);
|
2020-02-05 07:35:51 -08:00
|
|
|
} else {
|
|
|
|
const match = window.location.hash.match(
|
|
|
|
/^#json=([0-9]+),([a-zA-Z0-9_-]+)$/,
|
|
|
|
);
|
|
|
|
if (match) {
|
2020-02-12 02:19:43 +04:00
|
|
|
const scene = await loadScene(match[1], match[2]);
|
|
|
|
this.syncActionResult(scene);
|
2020-02-05 17:57:08 +01:00
|
|
|
} else {
|
2020-02-12 02:19:43 +04:00
|
|
|
const scene = await loadScene(null);
|
|
|
|
this.syncActionResult(scene);
|
2020-02-05 07:35:51 -08:00
|
|
|
}
|
|
|
|
}
|
2020-01-30 16:39:37 -03:00
|
|
|
}
|
|
|
|
|
2020-01-05 15:10:42 +01:00
|
|
|
public componentWillUnmount() {
|
2020-02-12 02:19:43 +04:00
|
|
|
this.unmounted = true;
|
2020-01-11 21:39:16 -08:00
|
|
|
document.removeEventListener("copy", this.onCopy);
|
2020-02-07 18:42:24 +01:00
|
|
|
document.removeEventListener("paste", this.pasteFromClipboard);
|
2020-01-11 21:39:16 -08:00
|
|
|
document.removeEventListener("cut", this.onCut);
|
|
|
|
|
2020-01-05 15:10:42 +01:00
|
|
|
document.removeEventListener("keydown", this.onKeyDown, false);
|
2020-01-09 12:34:46 +01:00
|
|
|
document.removeEventListener(
|
|
|
|
"mousemove",
|
2020-01-25 11:38:08 -08:00
|
|
|
this.updateCurrentCursorPosition,
|
2020-01-24 12:04:54 +02:00
|
|
|
false,
|
2020-01-09 12:34:46 +01:00
|
|
|
);
|
2020-02-12 02:19:43 +04:00
|
|
|
document.removeEventListener("keyup", this.onKeyUp);
|
2020-01-05 15:10:42 +01:00
|
|
|
window.removeEventListener("resize", this.onResize, false);
|
2020-01-20 18:37:42 +01:00
|
|
|
window.removeEventListener("unload", this.onUnload, false);
|
2020-01-30 17:08:59 -03:00
|
|
|
window.removeEventListener("blur", this.onUnload, false);
|
2020-02-12 02:19:43 +04:00
|
|
|
window.removeEventListener("dragover", this.disableEvent, false);
|
|
|
|
window.removeEventListener("drop", this.disableEvent, false);
|
2020-01-05 15:10:42 +01:00
|
|
|
}
|
|
|
|
|
2020-01-11 20:34:21 -08:00
|
|
|
public state: AppState = getDefaultAppState();
|
2020-01-05 15:10:42 +01:00
|
|
|
|
|
|
|
private onResize = () => {
|
2020-01-26 19:08:46 +00:00
|
|
|
this.setState({});
|
2020-01-05 15:10:42 +01:00
|
|
|
};
|
|
|
|
|
2020-01-25 11:38:08 -08:00
|
|
|
private updateCurrentCursorPosition = (e: MouseEvent) => {
|
|
|
|
cursorX = e.x;
|
|
|
|
cursorY = e.y;
|
2020-01-09 12:34:46 +01:00
|
|
|
};
|
|
|
|
|
2020-01-05 15:10:42 +01:00
|
|
|
private onKeyDown = (event: KeyboardEvent) => {
|
2020-02-05 19:07:53 +01:00
|
|
|
if (
|
|
|
|
(isWritableElement(event.target) && event.key !== KEYS.ESCAPE) ||
|
|
|
|
// case: using arrows to move between buttons
|
|
|
|
(isArrowKey(event.key) && isInputLike(event.target))
|
|
|
|
) {
|
2020-02-02 20:04:35 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-01-09 16:30:18 +01:00
|
|
|
|
2020-02-07 23:46:19 +01:00
|
|
|
if (this.actionManager.handleKeyDown(event)) {
|
|
|
|
return;
|
2020-01-12 02:22:03 +04:00
|
|
|
}
|
|
|
|
|
2020-01-22 19:36:08 +06:00
|
|
|
const shape = findShapeByKey(event.key);
|
|
|
|
|
2020-01-12 02:22:03 +04:00
|
|
|
if (isArrowKey(event.key)) {
|
2020-01-05 15:10:42 +01:00
|
|
|
const step = event.shiftKey
|
|
|
|
? ELEMENT_SHIFT_TRANSLATE_AMOUNT
|
|
|
|
: ELEMENT_TRANSLATE_AMOUNT;
|
2020-01-09 19:22:04 +04:00
|
|
|
elements = elements.map(el => {
|
|
|
|
if (el.isSelected) {
|
|
|
|
const element = { ...el };
|
2020-02-02 20:04:35 +02:00
|
|
|
if (event.key === KEYS.ARROW_LEFT) {
|
|
|
|
element.x -= step;
|
|
|
|
} else if (event.key === KEYS.ARROW_RIGHT) {
|
|
|
|
element.x += step;
|
|
|
|
} else if (event.key === KEYS.ARROW_UP) {
|
|
|
|
element.y -= step;
|
|
|
|
} else if (event.key === KEYS.ARROW_DOWN) {
|
|
|
|
element.y += step;
|
|
|
|
}
|
2020-01-09 19:22:04 +04:00
|
|
|
return element;
|
2020-01-05 15:10:42 +01:00
|
|
|
}
|
2020-01-09 19:22:04 +04:00
|
|
|
return el;
|
2020-01-05 15:10:42 +01:00
|
|
|
});
|
2020-01-26 19:08:46 +00:00
|
|
|
this.setState({});
|
2020-01-05 15:10:42 +01:00
|
|
|
event.preventDefault();
|
2020-01-11 19:42:34 -08:00
|
|
|
} else if (
|
|
|
|
shapesShortcutKeys.includes(event.key.toLowerCase()) &&
|
|
|
|
!event.ctrlKey &&
|
|
|
|
!event.altKey &&
|
2020-01-19 07:12:49 +09:00
|
|
|
!event.metaKey &&
|
2020-01-20 00:27:00 +01:00
|
|
|
this.state.draggingElement === null
|
2020-01-11 19:42:34 -08:00
|
|
|
) {
|
2020-02-07 18:42:24 +01:00
|
|
|
this.selectShapeTool(shape);
|
2020-02-01 17:37:22 +01:00
|
|
|
// Undo action
|
|
|
|
} else if (event[KEYS.META] && /z/i.test(event.key)) {
|
2020-01-22 21:32:43 +01:00
|
|
|
event.preventDefault();
|
|
|
|
|
2020-02-01 15:49:18 +04:00
|
|
|
if (
|
|
|
|
this.state.multiElement ||
|
2020-02-05 22:47:10 +04:00
|
|
|
this.state.resizingElement ||
|
|
|
|
this.state.editingElement ||
|
|
|
|
this.state.draggingElement
|
2020-02-01 15:49:18 +04:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-06 00:58:54 -03:00
|
|
|
if (event.shiftKey) {
|
|
|
|
// Redo action
|
2020-01-12 12:19:24 +01:00
|
|
|
const data = history.redoOnce();
|
2020-01-09 19:22:04 +04:00
|
|
|
if (data !== null) {
|
2020-01-22 21:32:43 +01:00
|
|
|
elements = data.elements;
|
2020-02-01 15:49:18 +04:00
|
|
|
this.setState({ ...data.appState });
|
2020-01-09 19:22:04 +04:00
|
|
|
}
|
2020-01-06 00:58:54 -03:00
|
|
|
} else {
|
|
|
|
// undo action
|
2020-01-11 20:45:56 -08:00
|
|
|
const data = history.undoOnce();
|
2020-01-09 19:22:04 +04:00
|
|
|
if (data !== null) {
|
2020-01-22 21:32:43 +01:00
|
|
|
elements = data.elements;
|
2020-02-01 15:49:18 +04:00
|
|
|
this.setState({ ...data.appState });
|
2020-01-09 19:22:04 +04:00
|
|
|
}
|
2020-01-05 15:10:42 +01:00
|
|
|
}
|
2020-01-30 17:08:59 -03:00
|
|
|
} else if (event.key === KEYS.SPACE && !isHoldingMouseButton) {
|
|
|
|
isHoldingSpace = true;
|
|
|
|
document.documentElement.style.cursor = CURSOR_TYPE.GRABBING;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private onKeyUp = (event: KeyboardEvent) => {
|
|
|
|
if (event.key === KEYS.SPACE) {
|
|
|
|
if (this.state.elementType === "selection") {
|
|
|
|
resetCursor();
|
|
|
|
} else {
|
|
|
|
elements = clearSelection(elements);
|
|
|
|
document.documentElement.style.cursor =
|
|
|
|
this.state.elementType === "text"
|
|
|
|
? CURSOR_TYPE.TEXT
|
|
|
|
: CURSOR_TYPE.CROSSHAIR;
|
|
|
|
this.setState({});
|
|
|
|
}
|
|
|
|
isHoldingSpace = false;
|
2020-01-05 15:10:42 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private removeWheelEventListener: (() => void) | undefined;
|
|
|
|
|
2020-02-04 11:50:18 +01:00
|
|
|
private copyToAppClipboard = () => {
|
|
|
|
copyToAppClipboard(elements);
|
2020-01-07 07:50:59 +05:00
|
|
|
};
|
|
|
|
|
2020-02-07 18:42:24 +01:00
|
|
|
private pasteFromClipboard = async (e: ClipboardEvent | null) => {
|
|
|
|
// #686
|
|
|
|
const target = document.activeElement;
|
|
|
|
const elementUnderCursor = document.elementFromPoint(cursorX, cursorY);
|
|
|
|
if (
|
|
|
|
// if no ClipboardEvent supplied, assume we're pasting via contextMenu
|
|
|
|
// thus these checks don't make sense
|
|
|
|
!e ||
|
|
|
|
(elementUnderCursor instanceof HTMLCanvasElement &&
|
|
|
|
!isWritableElement(target))
|
|
|
|
) {
|
|
|
|
const data = await getClipboardContent(e);
|
|
|
|
if (data.elements) {
|
|
|
|
this.addElementsFromPaste(data.elements);
|
|
|
|
} else if (data.text) {
|
|
|
|
const { x, y } = viewportCoordsToSceneCoords(
|
|
|
|
{ clientX: cursorX, clientY: cursorY },
|
|
|
|
this.state,
|
|
|
|
);
|
|
|
|
|
|
|
|
const element = newTextElement(
|
|
|
|
newElement(
|
|
|
|
"text",
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
this.state.currentItemStrokeColor,
|
|
|
|
this.state.currentItemBackgroundColor,
|
|
|
|
this.state.currentItemFillStyle,
|
|
|
|
this.state.currentItemStrokeWidth,
|
|
|
|
this.state.currentItemRoughness,
|
|
|
|
this.state.currentItemOpacity,
|
|
|
|
),
|
|
|
|
data.text,
|
|
|
|
this.state.currentItemFont,
|
|
|
|
);
|
|
|
|
|
|
|
|
element.isSelected = true;
|
|
|
|
|
|
|
|
elements = [...clearSelection(elements), element];
|
|
|
|
history.resumeRecording();
|
|
|
|
}
|
|
|
|
this.selectShapeTool("selection");
|
|
|
|
e?.preventDefault();
|
2020-01-07 07:50:59 +05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-07 18:42:24 +01:00
|
|
|
private selectShapeTool(elementType: AppState["elementType"]) {
|
|
|
|
if (!isHoldingSpace) {
|
|
|
|
setCursorForShape(elementType);
|
|
|
|
}
|
|
|
|
if (isToolIcon(document.activeElement)) {
|
|
|
|
document.activeElement.blur();
|
|
|
|
}
|
|
|
|
if (elementType !== "selection") {
|
|
|
|
elements = clearSelection(elements);
|
|
|
|
}
|
|
|
|
this.setState({ elementType });
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:47:10 +04:00
|
|
|
setAppState = (obj: any) => {
|
|
|
|
this.setState(obj);
|
|
|
|
};
|
2020-01-15 20:42:02 +05:00
|
|
|
|
2020-02-05 22:47:10 +04:00
|
|
|
setElements = (elements_: readonly ExcalidrawElement[]) => {
|
|
|
|
elements = elements_;
|
|
|
|
this.setState({});
|
|
|
|
};
|
2020-01-15 20:42:02 +05:00
|
|
|
|
2020-01-05 15:10:42 +01:00
|
|
|
public render() {
|
2020-02-08 16:20:14 -08:00
|
|
|
const canvasWidth = window.innerWidth;
|
|
|
|
const canvasHeight = window.innerHeight;
|
2020-01-05 15:10:42 +01:00
|
|
|
|
|
|
|
return (
|
2020-01-11 21:39:16 -08:00
|
|
|
<div className="container">
|
2020-02-05 22:47:10 +04:00
|
|
|
<LayerUI
|
|
|
|
canvas={this.canvas}
|
|
|
|
appState={this.state}
|
|
|
|
setAppState={this.setAppState}
|
|
|
|
actionManager={this.actionManager}
|
|
|
|
elements={elements}
|
|
|
|
setElements={this.setElements}
|
2020-02-07 23:46:19 +01:00
|
|
|
language={getLanguage()}
|
2020-02-05 22:47:10 +04:00
|
|
|
/>
|
2020-01-26 17:14:31 -03:00
|
|
|
<main>
|
|
|
|
<canvas
|
|
|
|
id="canvas"
|
|
|
|
style={{
|
|
|
|
width: canvasWidth,
|
|
|
|
height: canvasHeight,
|
|
|
|
}}
|
|
|
|
width={canvasWidth * window.devicePixelRatio}
|
|
|
|
height={canvasHeight * window.devicePixelRatio}
|
|
|
|
ref={canvas => {
|
|
|
|
if (this.canvas === null) {
|
|
|
|
this.canvas = canvas;
|
|
|
|
this.rc = rough.canvas(this.canvas!);
|
|
|
|
}
|
|
|
|
if (this.removeWheelEventListener) {
|
|
|
|
this.removeWheelEventListener();
|
|
|
|
this.removeWheelEventListener = undefined;
|
|
|
|
}
|
|
|
|
if (canvas) {
|
|
|
|
canvas.addEventListener("wheel", this.handleWheel, {
|
|
|
|
passive: false,
|
|
|
|
});
|
|
|
|
this.removeWheelEventListener = () =>
|
|
|
|
canvas.removeEventListener("wheel", this.handleWheel);
|
|
|
|
// Whenever React sets the width/height of the canvas element,
|
|
|
|
// the context loses the scale transform. We need to re-apply it
|
|
|
|
if (
|
|
|
|
canvasWidth !== lastCanvasWidth ||
|
|
|
|
canvasHeight !== lastCanvasHeight
|
|
|
|
) {
|
|
|
|
lastCanvasWidth = canvasWidth;
|
|
|
|
lastCanvasHeight = canvasHeight;
|
|
|
|
canvas
|
|
|
|
.getContext("2d")!
|
|
|
|
.scale(window.devicePixelRatio, window.devicePixelRatio);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onContextMenu={e => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const { x, y } = viewportCoordsToSceneCoords(e, this.state);
|
|
|
|
|
|
|
|
const element = getElementAtPosition(elements, x, y);
|
|
|
|
if (!element) {
|
|
|
|
ContextMenu.push({
|
|
|
|
options: [
|
|
|
|
navigator.clipboard && {
|
|
|
|
label: t("labels.paste"),
|
2020-02-07 18:42:24 +01:00
|
|
|
action: () => this.pasteFromClipboard(null),
|
2020-01-26 17:14:31 -03:00
|
|
|
},
|
2020-02-05 22:47:10 +04:00
|
|
|
...this.actionManager.getContextMenuItems(action =>
|
|
|
|
this.canvasOnlyActions.includes(action),
|
2020-01-26 17:14:31 -03:00
|
|
|
),
|
|
|
|
],
|
|
|
|
top: e.clientY,
|
|
|
|
left: e.clientX,
|
|
|
|
});
|
|
|
|
return;
|
2020-01-05 15:10:42 +01:00
|
|
|
}
|
2020-01-07 07:50:59 +05:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
if (!element.isSelected) {
|
|
|
|
elements = clearSelection(elements);
|
|
|
|
element.isSelected = true;
|
|
|
|
this.setState({});
|
|
|
|
}
|
2020-01-07 07:50:59 +05:00
|
|
|
|
|
|
|
ContextMenu.push({
|
|
|
|
options: [
|
2020-01-26 17:14:31 -03:00
|
|
|
navigator.clipboard && {
|
|
|
|
label: t("labels.copy"),
|
2020-02-04 11:50:18 +01:00
|
|
|
action: this.copyToAppClipboard,
|
2020-01-26 17:14:31 -03:00
|
|
|
},
|
2020-01-07 07:50:59 +05:00
|
|
|
navigator.clipboard && {
|
2020-01-21 01:14:10 +02:00
|
|
|
label: t("labels.paste"),
|
2020-02-07 18:42:24 +01:00
|
|
|
action: () => this.pasteFromClipboard(null),
|
2020-01-12 07:10:15 -08:00
|
|
|
},
|
|
|
|
...this.actionManager.getContextMenuItems(
|
2020-01-26 17:14:31 -03:00
|
|
|
action => !this.canvasOnlyActions.includes(action),
|
2020-01-24 12:04:54 +02:00
|
|
|
),
|
2020-01-07 07:50:59 +05:00
|
|
|
],
|
|
|
|
top: e.clientY,
|
2020-01-24 12:04:54 +02:00
|
|
|
left: e.clientX,
|
2020-01-07 07:50:59 +05:00
|
|
|
});
|
2020-01-26 17:14:31 -03:00
|
|
|
}}
|
|
|
|
onMouseDown={e => {
|
|
|
|
if (lastMouseUp !== null) {
|
|
|
|
// Unfortunately, sometimes we don't get a mouseup after a mousedown,
|
|
|
|
// this can happen when a contextual menu or alert is triggered. In order to avoid
|
|
|
|
// being in a weird state, we clean up on the next mousedown
|
|
|
|
lastMouseUp(e);
|
|
|
|
}
|
2020-01-07 07:50:59 +05:00
|
|
|
|
2020-02-02 20:04:35 +02:00
|
|
|
if (isPanning) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-30 17:08:59 -03:00
|
|
|
|
|
|
|
// pan canvas on wheel button drag or space+drag
|
|
|
|
if (
|
|
|
|
!isHoldingMouseButton &&
|
|
|
|
(e.button === MOUSE_BUTTON.WHEEL ||
|
|
|
|
(e.button === MOUSE_BUTTON.MAIN && isHoldingSpace))
|
|
|
|
) {
|
|
|
|
isHoldingMouseButton = true;
|
|
|
|
isPanning = true;
|
|
|
|
document.documentElement.style.cursor = CURSOR_TYPE.GRABBING;
|
2020-01-26 17:14:31 -03:00
|
|
|
let { clientX: lastX, clientY: lastY } = e;
|
|
|
|
const onMouseMove = (e: MouseEvent) => {
|
2020-02-02 20:04:35 +02:00
|
|
|
const deltaX = lastX - e.clientX;
|
|
|
|
const deltaY = lastY - e.clientY;
|
2020-01-26 17:14:31 -03:00
|
|
|
lastX = e.clientX;
|
|
|
|
lastY = e.clientY;
|
2020-02-05 22:47:10 +04:00
|
|
|
|
2020-01-31 20:09:42 +00:00
|
|
|
this.setState({
|
|
|
|
scrollX: this.state.scrollX - deltaX,
|
|
|
|
scrollY: this.state.scrollY - deltaY,
|
|
|
|
});
|
2020-01-26 17:14:31 -03:00
|
|
|
};
|
2020-01-30 17:08:59 -03:00
|
|
|
const teardown = (lastMouseUp = () => {
|
2020-01-26 17:14:31 -03:00
|
|
|
lastMouseUp = null;
|
2020-01-30 17:08:59 -03:00
|
|
|
isPanning = false;
|
|
|
|
isHoldingMouseButton = false;
|
|
|
|
if (!isHoldingSpace) {
|
|
|
|
setCursorForShape(this.state.elementType);
|
|
|
|
}
|
2020-01-26 17:14:31 -03:00
|
|
|
window.removeEventListener("mousemove", onMouseMove);
|
2020-01-30 17:08:59 -03:00
|
|
|
window.removeEventListener("mouseup", teardown);
|
|
|
|
window.removeEventListener("blur", teardown);
|
2020-01-26 17:14:31 -03:00
|
|
|
});
|
2020-01-30 17:08:59 -03:00
|
|
|
window.addEventListener("blur", teardown);
|
2020-01-26 17:14:31 -03:00
|
|
|
window.addEventListener("mousemove", onMouseMove, {
|
|
|
|
passive: true,
|
|
|
|
});
|
2020-01-30 17:08:59 -03:00
|
|
|
window.addEventListener("mouseup", teardown);
|
2020-01-26 17:14:31 -03:00
|
|
|
return;
|
|
|
|
}
|
2020-01-07 07:50:59 +05:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
// only handle left mouse button
|
2020-02-02 20:04:35 +02:00
|
|
|
if (e.button !== MOUSE_BUTTON.MAIN) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-26 17:14:31 -03:00
|
|
|
// fixes mousemove causing selection of UI texts #32
|
|
|
|
e.preventDefault();
|
|
|
|
// Preventing the event above disables default behavior
|
2020-01-27 22:14:35 +01:00
|
|
|
// of defocusing potentially focused element, which is what we
|
|
|
|
// want when clicking inside the canvas.
|
|
|
|
if (document.activeElement instanceof HTMLElement) {
|
2020-01-26 17:14:31 -03:00
|
|
|
document.activeElement.blur();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle scrollbars dragging
|
|
|
|
const {
|
|
|
|
isOverHorizontalScrollBar,
|
|
|
|
isOverVerticalScrollBar,
|
|
|
|
} = isOverScrollBars(
|
2020-01-08 23:56:35 -03:00
|
|
|
elements,
|
2020-02-08 16:20:14 -08:00
|
|
|
e.clientX / window.devicePixelRatio,
|
|
|
|
e.clientY / window.devicePixelRatio,
|
|
|
|
canvasWidth / window.devicePixelRatio,
|
|
|
|
canvasHeight / window.devicePixelRatio,
|
2020-01-26 17:14:31 -03:00
|
|
|
this.state.scrollX,
|
|
|
|
this.state.scrollY,
|
2020-01-08 23:56:35 -03:00
|
|
|
);
|
2020-01-05 15:10:42 +01:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
const { x, y } = viewportCoordsToSceneCoords(e, this.state);
|
|
|
|
|
|
|
|
const originX = x;
|
|
|
|
const originY = y;
|
|
|
|
|
|
|
|
let element = newElement(
|
|
|
|
this.state.elementType,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
this.state.currentItemStrokeColor,
|
|
|
|
this.state.currentItemBackgroundColor,
|
|
|
|
this.state.currentItemFillStyle,
|
|
|
|
this.state.currentItemStrokeWidth,
|
|
|
|
this.state.currentItemRoughness,
|
|
|
|
this.state.currentItemOpacity,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (isTextElement(element)) {
|
|
|
|
element = newTextElement(
|
|
|
|
element,
|
|
|
|
"",
|
|
|
|
this.state.currentItemFont,
|
2020-01-19 21:11:46 +00:00
|
|
|
);
|
2020-01-26 17:14:31 -03:00
|
|
|
}
|
2020-01-05 15:10:42 +01:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
type ResizeTestType = ReturnType<typeof resizeTest>;
|
|
|
|
let resizeHandle: ResizeTestType = false;
|
|
|
|
let isResizingElements = false;
|
|
|
|
let draggingOccurred = false;
|
|
|
|
let hitElement: ExcalidrawElement | null = null;
|
|
|
|
let elementIsAddedToSelection = false;
|
|
|
|
if (this.state.elementType === "selection") {
|
|
|
|
const resizeElement = getElementWithResizeHandler(
|
|
|
|
elements,
|
|
|
|
{ x, y },
|
|
|
|
this.state,
|
|
|
|
);
|
|
|
|
this.setState({
|
|
|
|
resizingElement: resizeElement ? resizeElement.element : null,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (resizeElement) {
|
|
|
|
resizeHandle = resizeElement.resizeHandle;
|
|
|
|
document.documentElement.style.cursor = getCursorForResizingElement(
|
|
|
|
resizeElement,
|
|
|
|
);
|
|
|
|
isResizingElements = true;
|
|
|
|
} else {
|
|
|
|
hitElement = getElementAtPosition(elements, x, y);
|
|
|
|
// clear selection if shift is not clicked
|
|
|
|
if (!hitElement?.isSelected && !e.shiftKey) {
|
|
|
|
elements = clearSelection(elements);
|
2020-01-11 02:45:58 +05:00
|
|
|
}
|
2020-01-09 19:22:04 +04:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
// If we click on something
|
|
|
|
if (hitElement) {
|
|
|
|
// deselect if item is selected
|
|
|
|
// if shift is not clicked, this will always return true
|
|
|
|
// otherwise, it will trigger selection based on current
|
|
|
|
// state of the box
|
|
|
|
if (!hitElement.isSelected) {
|
|
|
|
hitElement.isSelected = true;
|
2020-02-05 22:47:10 +04:00
|
|
|
elements = elements.slice();
|
2020-01-26 17:14:31 -03:00
|
|
|
elementIsAddedToSelection = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We duplicate the selected element if alt is pressed on Mouse down
|
|
|
|
if (e.altKey) {
|
|
|
|
elements = [
|
|
|
|
...elements.map(element => ({
|
|
|
|
...element,
|
|
|
|
isSelected: false,
|
|
|
|
})),
|
|
|
|
...elements
|
|
|
|
.filter(element => element.isSelected)
|
|
|
|
.map(element => {
|
|
|
|
const newElement = duplicateElement(element);
|
|
|
|
newElement.isSelected = true;
|
|
|
|
return newElement;
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
}
|
2020-01-05 15:10:42 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-26 17:14:31 -03:00
|
|
|
} else {
|
|
|
|
elements = clearSelection(elements);
|
2020-01-05 15:10:42 +01:00
|
|
|
}
|
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
if (isTextElement(element)) {
|
2020-02-10 15:09:50 +01:00
|
|
|
// if we're currently still editing text, clicking outside
|
|
|
|
// should only finalize it, not create another (irrespective
|
|
|
|
// of state.elementLocked)
|
|
|
|
if (this.state.editingElement?.type === "text") {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-26 17:14:31 -03:00
|
|
|
let textX = e.clientX;
|
|
|
|
let textY = e.clientY;
|
|
|
|
if (!e.altKey) {
|
|
|
|
const snappedToCenterPosition = this.getTextWysiwygSnappedToCenterPosition(
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
);
|
|
|
|
if (snappedToCenterPosition) {
|
|
|
|
element.x = snappedToCenterPosition.elementCenterX;
|
|
|
|
element.y = snappedToCenterPosition.elementCenterY;
|
|
|
|
textX = snappedToCenterPosition.wysiwygX;
|
|
|
|
textY = snappedToCenterPosition.wysiwygY;
|
|
|
|
}
|
2020-01-09 01:09:09 +05:00
|
|
|
}
|
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
const resetSelection = () => {
|
|
|
|
this.setState({
|
|
|
|
draggingElement: null,
|
|
|
|
editingElement: null,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
textWysiwyg({
|
|
|
|
initText: "",
|
|
|
|
x: textX,
|
|
|
|
y: textY,
|
|
|
|
strokeColor: this.state.currentItemStrokeColor,
|
|
|
|
opacity: this.state.currentItemOpacity,
|
|
|
|
font: this.state.currentItemFont,
|
|
|
|
onSubmit: text => {
|
|
|
|
if (text) {
|
|
|
|
elements = [
|
|
|
|
...elements,
|
|
|
|
{
|
|
|
|
...newTextElement(
|
|
|
|
element,
|
|
|
|
text,
|
|
|
|
this.state.currentItemFont,
|
|
|
|
),
|
|
|
|
isSelected: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
2020-02-10 15:09:50 +01:00
|
|
|
if (this.state.elementLocked) {
|
|
|
|
setCursorForShape(this.state.elementType);
|
|
|
|
}
|
2020-02-05 22:47:10 +04:00
|
|
|
history.resumeRecording();
|
2020-01-26 17:14:31 -03:00
|
|
|
resetSelection();
|
|
|
|
},
|
|
|
|
onCancel: () => {
|
|
|
|
resetSelection();
|
|
|
|
},
|
|
|
|
});
|
2020-02-10 15:09:50 +01:00
|
|
|
resetCursor();
|
|
|
|
if (!this.state.elementLocked) {
|
|
|
|
this.setState({
|
|
|
|
editingElement: element,
|
|
|
|
elementType: "selection",
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
editingElement: element,
|
|
|
|
});
|
|
|
|
}
|
2020-01-26 17:14:31 -03:00
|
|
|
return;
|
2020-02-04 13:45:22 +04:00
|
|
|
} else if (
|
|
|
|
this.state.elementType === "arrow" ||
|
|
|
|
this.state.elementType === "line"
|
|
|
|
) {
|
2020-02-01 15:49:18 +04:00
|
|
|
if (this.state.multiElement) {
|
|
|
|
const { multiElement } = this.state;
|
|
|
|
const { x: rx, y: ry } = multiElement;
|
|
|
|
multiElement.isSelected = true;
|
|
|
|
multiElement.points.push([x - rx, y - ry]);
|
|
|
|
multiElement.shape = null;
|
|
|
|
} else {
|
|
|
|
element.isSelected = false;
|
|
|
|
element.points.push([0, 0]);
|
|
|
|
element.shape = null;
|
|
|
|
elements = [...elements, element];
|
|
|
|
this.setState({
|
|
|
|
draggingElement: element,
|
|
|
|
});
|
|
|
|
}
|
2020-02-05 22:47:10 +04:00
|
|
|
} else if (element.type === "selection") {
|
|
|
|
this.setState({
|
|
|
|
selectionElement: element,
|
|
|
|
draggingElement: element,
|
|
|
|
});
|
2020-02-01 15:49:18 +04:00
|
|
|
} else {
|
|
|
|
elements = [...elements, element];
|
|
|
|
this.setState({ multiElement: null, draggingElement: element });
|
2020-01-26 17:14:31 -03:00
|
|
|
}
|
2020-01-05 15:10:42 +01:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
let lastX = x;
|
|
|
|
let lastY = y;
|
2020-01-05 23:06:21 +05:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
if (isOverHorizontalScrollBar || isOverVerticalScrollBar) {
|
2020-02-08 16:20:14 -08:00
|
|
|
lastX = e.clientX;
|
|
|
|
lastY = e.clientY;
|
2020-01-05 15:10:42 +01:00
|
|
|
}
|
|
|
|
|
2020-02-01 15:49:18 +04:00
|
|
|
let resizeArrowFn:
|
|
|
|
| ((
|
|
|
|
element: ExcalidrawElement,
|
|
|
|
p1: Point,
|
|
|
|
deltaX: number,
|
|
|
|
deltaY: number,
|
|
|
|
mouseX: number,
|
|
|
|
mouseY: number,
|
|
|
|
perfect: boolean,
|
|
|
|
) => void)
|
|
|
|
| null = null;
|
|
|
|
|
|
|
|
const arrowResizeOrigin = (
|
|
|
|
element: ExcalidrawElement,
|
|
|
|
p1: Point,
|
|
|
|
deltaX: number,
|
|
|
|
deltaY: number,
|
|
|
|
mouseX: number,
|
|
|
|
mouseY: number,
|
|
|
|
perfect: boolean,
|
|
|
|
) => {
|
|
|
|
if (perfect) {
|
|
|
|
const absPx = p1[0] + element.x;
|
|
|
|
const absPy = p1[1] + element.y;
|
|
|
|
|
2020-02-02 20:04:35 +02:00
|
|
|
const { width, height } = getPerfectElementSize(
|
2020-02-04 13:45:22 +04:00
|
|
|
element.type,
|
2020-02-01 15:49:18 +04:00
|
|
|
mouseX - element.x - p1[0],
|
|
|
|
mouseY - element.y - p1[1],
|
|
|
|
);
|
|
|
|
|
|
|
|
const dx = element.x + width + p1[0];
|
|
|
|
const dy = element.y + height + p1[1];
|
|
|
|
element.x = dx;
|
|
|
|
element.y = dy;
|
|
|
|
p1[0] = absPx - element.x;
|
|
|
|
p1[1] = absPy - element.y;
|
|
|
|
} else {
|
|
|
|
element.x += deltaX;
|
|
|
|
element.y += deltaY;
|
|
|
|
p1[0] -= deltaX;
|
|
|
|
p1[1] -= deltaY;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const arrowResizeEnd = (
|
|
|
|
element: ExcalidrawElement,
|
|
|
|
p1: Point,
|
|
|
|
deltaX: number,
|
|
|
|
deltaY: number,
|
|
|
|
mouseX: number,
|
|
|
|
mouseY: number,
|
|
|
|
perfect: boolean,
|
|
|
|
) => {
|
|
|
|
if (perfect) {
|
|
|
|
const { width, height } = getPerfectElementSize(
|
2020-02-04 13:45:22 +04:00
|
|
|
element.type,
|
2020-02-01 15:49:18 +04:00
|
|
|
mouseX - element.x,
|
|
|
|
mouseY - element.y,
|
|
|
|
);
|
|
|
|
p1[0] = width;
|
|
|
|
p1[1] = height;
|
|
|
|
} else {
|
|
|
|
p1[0] += deltaX;
|
|
|
|
p1[1] += deltaY;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
const onMouseMove = (e: MouseEvent) => {
|
|
|
|
const target = e.target;
|
|
|
|
if (!(target instanceof HTMLElement)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-05 23:06:21 +05:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
if (isOverHorizontalScrollBar) {
|
2020-02-08 16:20:14 -08:00
|
|
|
const x = e.clientX;
|
2020-01-26 17:14:31 -03:00
|
|
|
const dx = x - lastX;
|
2020-01-31 20:09:42 +00:00
|
|
|
this.setState({ scrollX: this.state.scrollX - dx });
|
2020-01-26 17:14:31 -03:00
|
|
|
lastX = x;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isOverVerticalScrollBar) {
|
2020-02-08 16:20:14 -08:00
|
|
|
const y = e.clientY;
|
2020-01-26 17:14:31 -03:00
|
|
|
const dy = y - lastY;
|
2020-01-31 20:09:42 +00:00
|
|
|
this.setState({ scrollY: this.state.scrollY - dy });
|
2020-01-26 17:14:31 -03:00
|
|
|
lastY = y;
|
|
|
|
return;
|
|
|
|
}
|
2020-01-05 23:06:21 +05:00
|
|
|
|
2020-02-01 15:49:18 +04:00
|
|
|
// for arrows, don't start dragging until a given threshold
|
|
|
|
// to ensure we don't create a 2-point arrow by mistake when
|
|
|
|
// user clicks mouse in a way that it moves a tiny bit (thus
|
|
|
|
// triggering mousemove)
|
2020-02-04 13:45:22 +04:00
|
|
|
if (
|
|
|
|
!draggingOccurred &&
|
|
|
|
(this.state.elementType === "arrow" ||
|
|
|
|
this.state.elementType === "line")
|
|
|
|
) {
|
2020-02-01 15:49:18 +04:00
|
|
|
const { x, y } = viewportCoordsToSceneCoords(e, this.state);
|
2020-02-02 20:04:35 +02:00
|
|
|
if (distance2d(x, y, originX, originY) < DRAGGING_THRESHOLD) {
|
2020-02-01 15:49:18 +04:00
|
|
|
return;
|
2020-02-02 20:04:35 +02:00
|
|
|
}
|
2020-02-01 15:49:18 +04:00
|
|
|
}
|
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
if (isResizingElements && this.state.resizingElement) {
|
2020-02-03 21:52:21 +04:00
|
|
|
this.setState({ isResizing: true });
|
2020-01-26 17:14:31 -03:00
|
|
|
const el = this.state.resizingElement;
|
|
|
|
const selectedElements = elements.filter(el => el.isSelected);
|
|
|
|
if (selectedElements.length === 1) {
|
|
|
|
const { x, y } = viewportCoordsToSceneCoords(e, this.state);
|
|
|
|
const deltaX = x - lastX;
|
|
|
|
const deltaY = y - lastY;
|
|
|
|
const element = selectedElements[0];
|
|
|
|
const isLinear =
|
|
|
|
element.type === "line" || element.type === "arrow";
|
|
|
|
switch (resizeHandle) {
|
|
|
|
case "nw":
|
2020-02-04 13:45:22 +04:00
|
|
|
if (isLinear && element.points.length === 2) {
|
2020-02-01 15:49:18 +04:00
|
|
|
const [, p1] = element.points;
|
|
|
|
|
|
|
|
if (!resizeArrowFn) {
|
|
|
|
if (p1[0] < 0 || p1[1] < 0) {
|
|
|
|
resizeArrowFn = arrowResizeEnd;
|
|
|
|
} else {
|
|
|
|
resizeArrowFn = arrowResizeOrigin;
|
|
|
|
}
|
2020-01-31 21:16:33 +04:00
|
|
|
}
|
2020-02-01 15:49:18 +04:00
|
|
|
resizeArrowFn(
|
|
|
|
element,
|
|
|
|
p1,
|
|
|
|
deltaX,
|
|
|
|
deltaY,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
e.shiftKey,
|
|
|
|
);
|
2020-01-31 18:56:55 +01:00
|
|
|
} else {
|
2020-02-01 15:49:18 +04:00
|
|
|
element.width -= deltaX;
|
|
|
|
element.x += deltaX;
|
|
|
|
|
|
|
|
if (e.shiftKey) {
|
2020-02-04 13:45:22 +04:00
|
|
|
element.y += element.height - element.width;
|
|
|
|
element.height = element.width;
|
2020-02-01 15:49:18 +04:00
|
|
|
} else {
|
|
|
|
element.height -= deltaY;
|
|
|
|
element.y += deltaY;
|
|
|
|
}
|
2020-01-26 17:14:31 -03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "ne":
|
2020-02-04 13:45:22 +04:00
|
|
|
if (isLinear && element.points.length === 2) {
|
2020-02-01 15:49:18 +04:00
|
|
|
const [, p1] = element.points;
|
|
|
|
if (!resizeArrowFn) {
|
|
|
|
if (p1[0] >= 0) {
|
|
|
|
resizeArrowFn = arrowResizeEnd;
|
|
|
|
} else {
|
|
|
|
resizeArrowFn = arrowResizeOrigin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resizeArrowFn(
|
|
|
|
element,
|
|
|
|
p1,
|
|
|
|
deltaX,
|
|
|
|
deltaY,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
e.shiftKey,
|
|
|
|
);
|
2020-01-26 17:14:31 -03:00
|
|
|
} else {
|
2020-02-01 15:49:18 +04:00
|
|
|
element.width += deltaX;
|
|
|
|
if (e.shiftKey) {
|
|
|
|
element.y += element.height - element.width;
|
|
|
|
element.height = element.width;
|
|
|
|
} else {
|
|
|
|
element.height -= deltaY;
|
|
|
|
element.y += deltaY;
|
|
|
|
}
|
2020-01-23 09:21:04 +00:00
|
|
|
}
|
2020-01-26 17:14:31 -03:00
|
|
|
break;
|
|
|
|
case "sw":
|
2020-02-04 13:45:22 +04:00
|
|
|
if (isLinear && element.points.length === 2) {
|
2020-02-01 15:49:18 +04:00
|
|
|
const [, p1] = element.points;
|
|
|
|
if (!resizeArrowFn) {
|
|
|
|
if (p1[0] <= 0) {
|
|
|
|
resizeArrowFn = arrowResizeEnd;
|
|
|
|
} else {
|
|
|
|
resizeArrowFn = arrowResizeOrigin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resizeArrowFn(
|
|
|
|
element,
|
|
|
|
p1,
|
|
|
|
deltaX,
|
|
|
|
deltaY,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
e.shiftKey,
|
|
|
|
);
|
2020-01-26 17:14:31 -03:00
|
|
|
} else {
|
2020-02-01 15:49:18 +04:00
|
|
|
element.width -= deltaX;
|
|
|
|
element.x += deltaX;
|
|
|
|
if (e.shiftKey) {
|
|
|
|
element.height = element.width;
|
|
|
|
} else {
|
|
|
|
element.height += deltaY;
|
|
|
|
}
|
2020-01-26 17:14:31 -03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "se":
|
2020-02-04 13:45:22 +04:00
|
|
|
if (isLinear && element.points.length === 2) {
|
2020-02-01 15:49:18 +04:00
|
|
|
const [, p1] = element.points;
|
|
|
|
if (!resizeArrowFn) {
|
|
|
|
if (p1[0] > 0 || p1[1] > 0) {
|
|
|
|
resizeArrowFn = arrowResizeEnd;
|
|
|
|
} else {
|
|
|
|
resizeArrowFn = arrowResizeOrigin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resizeArrowFn(
|
|
|
|
element,
|
|
|
|
p1,
|
|
|
|
deltaX,
|
|
|
|
deltaY,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
e.shiftKey,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
if (e.shiftKey) {
|
2020-02-04 13:45:22 +04:00
|
|
|
element.width += deltaX;
|
|
|
|
element.height = element.width;
|
2020-01-26 17:14:31 -03:00
|
|
|
} else {
|
|
|
|
element.width += deltaX;
|
2020-02-01 15:49:18 +04:00
|
|
|
element.height += deltaY;
|
2020-01-26 17:14:31 -03:00
|
|
|
}
|
2020-01-23 09:21:04 +00:00
|
|
|
}
|
2020-01-26 17:14:31 -03:00
|
|
|
break;
|
2020-02-01 15:49:18 +04:00
|
|
|
case "n": {
|
2020-01-26 17:14:31 -03:00
|
|
|
element.height -= deltaY;
|
|
|
|
element.y += deltaY;
|
2020-02-01 15:49:18 +04:00
|
|
|
|
|
|
|
if (element.points.length > 0) {
|
|
|
|
const len = element.points.length;
|
|
|
|
|
|
|
|
const points = [...element.points].sort(
|
|
|
|
(a, b) => a[1] - b[1],
|
|
|
|
);
|
|
|
|
|
|
|
|
for (let i = 1; i < points.length; ++i) {
|
|
|
|
const pnt = points[i];
|
|
|
|
pnt[1] -= deltaY / (len - i);
|
|
|
|
}
|
|
|
|
}
|
2020-01-26 17:14:31 -03:00
|
|
|
break;
|
2020-02-01 15:49:18 +04:00
|
|
|
}
|
|
|
|
case "w": {
|
2020-01-26 17:14:31 -03:00
|
|
|
element.width -= deltaX;
|
|
|
|
element.x += deltaX;
|
2020-02-01 15:49:18 +04:00
|
|
|
|
|
|
|
if (element.points.length > 0) {
|
|
|
|
const len = element.points.length;
|
|
|
|
const points = [...element.points].sort(
|
|
|
|
(a, b) => a[0] - b[0],
|
|
|
|
);
|
|
|
|
|
|
|
|
for (let i = 0; i < points.length; ++i) {
|
|
|
|
const pnt = points[i];
|
|
|
|
pnt[0] -= deltaX / (len - i);
|
|
|
|
}
|
|
|
|
}
|
2020-01-26 17:14:31 -03:00
|
|
|
break;
|
2020-02-01 15:49:18 +04:00
|
|
|
}
|
|
|
|
case "s": {
|
2020-01-23 09:21:04 +00:00
|
|
|
element.height += deltaY;
|
2020-02-01 15:49:18 +04:00
|
|
|
if (element.points.length > 0) {
|
|
|
|
const len = element.points.length;
|
|
|
|
const points = [...element.points].sort(
|
|
|
|
(a, b) => a[1] - b[1],
|
|
|
|
);
|
|
|
|
|
|
|
|
for (let i = 1; i < points.length; ++i) {
|
|
|
|
const pnt = points[i];
|
|
|
|
pnt[1] += deltaY / (len - i);
|
|
|
|
}
|
|
|
|
}
|
2020-01-26 17:14:31 -03:00
|
|
|
break;
|
2020-02-01 15:49:18 +04:00
|
|
|
}
|
|
|
|
case "e": {
|
2020-01-26 17:14:31 -03:00
|
|
|
element.width += deltaX;
|
2020-02-01 15:49:18 +04:00
|
|
|
if (element.points.length > 0) {
|
|
|
|
const len = element.points.length;
|
|
|
|
const points = [...element.points].sort(
|
|
|
|
(a, b) => a[0] - b[0],
|
|
|
|
);
|
|
|
|
|
|
|
|
for (let i = 1; i < points.length; ++i) {
|
|
|
|
const pnt = points[i];
|
|
|
|
pnt[0] += deltaX / (len - i);
|
|
|
|
}
|
|
|
|
}
|
2020-01-26 17:14:31 -03:00
|
|
|
break;
|
2020-02-01 15:49:18 +04:00
|
|
|
}
|
2020-01-26 17:14:31 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (resizeHandle) {
|
|
|
|
resizeHandle = normalizeResizeHandle(
|
|
|
|
element,
|
|
|
|
resizeHandle,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
normalizeDimensions(element);
|
|
|
|
|
|
|
|
document.documentElement.style.cursor = getCursorForResizingElement(
|
|
|
|
{ element, resizeHandle },
|
|
|
|
);
|
|
|
|
el.x = element.x;
|
|
|
|
el.y = element.y;
|
|
|
|
el.shape = null;
|
|
|
|
|
|
|
|
lastX = x;
|
|
|
|
lastY = y;
|
|
|
|
this.setState({});
|
|
|
|
return;
|
2020-01-24 20:45:52 +01:00
|
|
|
}
|
2020-01-05 15:10:42 +01:00
|
|
|
}
|
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
if (hitElement?.isSelected) {
|
|
|
|
// Marking that click was used for dragging to check
|
|
|
|
// if elements should be deselected on mouseup
|
|
|
|
draggingOccurred = true;
|
|
|
|
const selectedElements = elements.filter(el => el.isSelected);
|
|
|
|
if (selectedElements.length) {
|
|
|
|
const { x, y } = viewportCoordsToSceneCoords(e, this.state);
|
|
|
|
|
|
|
|
selectedElements.forEach(element => {
|
|
|
|
element.x += x - lastX;
|
|
|
|
element.y += y - lastY;
|
|
|
|
});
|
|
|
|
lastX = x;
|
|
|
|
lastY = y;
|
|
|
|
this.setState({});
|
|
|
|
return;
|
|
|
|
}
|
2020-01-05 15:10:42 +01:00
|
|
|
}
|
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
// It is very important to read this.state within each move event,
|
|
|
|
// otherwise we would read a stale one!
|
|
|
|
const draggingElement = this.state.draggingElement;
|
2020-02-02 20:04:35 +02:00
|
|
|
if (!draggingElement) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-12 04:00:00 +04:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
const { x, y } = viewportCoordsToSceneCoords(e, this.state);
|
2020-01-24 20:45:52 +01:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
let width = distance(originX, x);
|
|
|
|
let height = distance(originY, y);
|
2020-01-24 20:45:52 +01:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
const isLinear =
|
|
|
|
this.state.elementType === "line" ||
|
|
|
|
this.state.elementType === "arrow";
|
2020-01-24 20:45:52 +01:00
|
|
|
|
2020-02-04 13:45:22 +04:00
|
|
|
if (isLinear) {
|
2020-02-01 15:49:18 +04:00
|
|
|
draggingOccurred = true;
|
|
|
|
const points = draggingElement.points;
|
|
|
|
let dx = x - draggingElement.x;
|
|
|
|
let dy = y - draggingElement.y;
|
|
|
|
|
|
|
|
if (e.shiftKey && points.length === 2) {
|
|
|
|
({ width: dx, height: dy } = getPerfectElementSize(
|
|
|
|
this.state.elementType,
|
|
|
|
dx,
|
|
|
|
dy,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (points.length === 1) {
|
|
|
|
points.push([dx, dy]);
|
|
|
|
} else if (points.length > 1) {
|
|
|
|
const pnt = points[points.length - 1];
|
|
|
|
pnt[0] = dx;
|
|
|
|
pnt[1] = dy;
|
|
|
|
}
|
2020-02-04 13:45:22 +04:00
|
|
|
} else {
|
|
|
|
if (e.shiftKey) {
|
|
|
|
({ width, height } = getPerfectElementSize(
|
|
|
|
this.state.elementType,
|
|
|
|
width,
|
|
|
|
y < originY ? -height : height,
|
|
|
|
));
|
|
|
|
|
|
|
|
if (height < 0) {
|
|
|
|
height = -height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
draggingElement.x = x < originX ? originX - width : originX;
|
|
|
|
draggingElement.y = y < originY ? originY - height : originY;
|
|
|
|
|
|
|
|
draggingElement.width = width;
|
|
|
|
draggingElement.height = height;
|
2020-02-01 15:49:18 +04:00
|
|
|
}
|
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
draggingElement.shape = null;
|
2020-01-05 15:10:42 +01:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
if (this.state.elementType === "selection") {
|
2020-02-05 22:47:10 +04:00
|
|
|
if (!e.shiftKey && elements.some(el => el.isSelected)) {
|
2020-01-26 17:14:31 -03:00
|
|
|
elements = clearSelection(elements);
|
|
|
|
}
|
|
|
|
const elementsWithinSelection = getElementsWithinSelection(
|
|
|
|
elements,
|
|
|
|
draggingElement,
|
|
|
|
);
|
|
|
|
elementsWithinSelection.forEach(element => {
|
|
|
|
element.isSelected = true;
|
|
|
|
});
|
2020-01-13 04:32:25 +05:00
|
|
|
}
|
2020-01-26 17:14:31 -03:00
|
|
|
this.setState({});
|
|
|
|
};
|
|
|
|
|
|
|
|
const onMouseUp = (e: MouseEvent) => {
|
|
|
|
const {
|
2020-01-24 12:04:54 +02:00
|
|
|
draggingElement,
|
2020-01-26 17:14:31 -03:00
|
|
|
resizingElement,
|
2020-02-01 15:49:18 +04:00
|
|
|
multiElement,
|
2020-01-26 17:14:31 -03:00
|
|
|
elementType,
|
|
|
|
elementLocked,
|
|
|
|
} = this.state;
|
2020-01-05 15:10:42 +01:00
|
|
|
|
2020-02-05 22:47:10 +04:00
|
|
|
this.setState({
|
|
|
|
isResizing: false,
|
|
|
|
resizingElement: null,
|
|
|
|
selectionElement: null,
|
|
|
|
});
|
|
|
|
|
2020-02-01 15:49:18 +04:00
|
|
|
resizeArrowFn = null;
|
2020-01-26 17:14:31 -03:00
|
|
|
lastMouseUp = null;
|
2020-01-30 17:08:59 -03:00
|
|
|
isHoldingMouseButton = false;
|
2020-01-26 17:14:31 -03:00
|
|
|
window.removeEventListener("mousemove", onMouseMove);
|
|
|
|
window.removeEventListener("mouseup", onMouseUp);
|
2020-01-05 15:10:42 +01:00
|
|
|
|
2020-02-04 13:45:22 +04:00
|
|
|
if (elementType === "arrow" || elementType === "line") {
|
2020-02-01 15:49:18 +04:00
|
|
|
if (draggingElement!.points.length > 1) {
|
|
|
|
history.resumeRecording();
|
2020-02-05 22:47:10 +04:00
|
|
|
this.setState({});
|
2020-02-01 15:49:18 +04:00
|
|
|
}
|
2020-02-04 17:39:08 +04:00
|
|
|
if (!draggingOccurred && draggingElement && !multiElement) {
|
|
|
|
const { x, y } = viewportCoordsToSceneCoords(e, this.state);
|
|
|
|
draggingElement.points.push([
|
|
|
|
x - draggingElement.x,
|
|
|
|
y - draggingElement.y,
|
|
|
|
]);
|
|
|
|
draggingElement.shape = null;
|
2020-02-01 15:49:18 +04:00
|
|
|
this.setState({ multiElement: this.state.draggingElement });
|
|
|
|
} else if (draggingOccurred && !multiElement) {
|
|
|
|
this.state.draggingElement!.isSelected = true;
|
2020-02-10 15:09:50 +01:00
|
|
|
if (!elementLocked) {
|
|
|
|
resetCursor();
|
|
|
|
this.setState({
|
|
|
|
draggingElement: null,
|
|
|
|
elementType: "selection",
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
draggingElement: null,
|
|
|
|
});
|
|
|
|
}
|
2020-02-01 15:49:18 +04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
if (
|
|
|
|
elementType !== "selection" &&
|
|
|
|
draggingElement &&
|
|
|
|
isInvisiblySmallElement(draggingElement)
|
|
|
|
) {
|
|
|
|
// remove invisible element which was added in onMouseDown
|
|
|
|
elements = elements.slice(0, -1);
|
|
|
|
this.setState({
|
|
|
|
draggingElement: null,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2020-01-05 15:10:42 +01:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
if (normalizeDimensions(draggingElement)) {
|
|
|
|
this.setState({});
|
|
|
|
}
|
2020-01-16 16:27:18 +00:00
|
|
|
|
2020-02-05 22:47:10 +04:00
|
|
|
if (resizingElement) {
|
|
|
|
history.resumeRecording();
|
|
|
|
this.setState({});
|
|
|
|
}
|
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
if (
|
|
|
|
resizingElement &&
|
|
|
|
isInvisiblySmallElement(resizingElement)
|
|
|
|
) {
|
|
|
|
elements = elements.filter(
|
|
|
|
el => el.id !== resizingElement.id,
|
|
|
|
);
|
|
|
|
}
|
2020-01-24 20:45:52 +01:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
// If click occurred on already selected element
|
|
|
|
// it is needed to remove selection from other elements
|
|
|
|
// or if SHIFT or META key pressed remove selection
|
|
|
|
// from hitted element
|
|
|
|
//
|
|
|
|
// If click occurred and elements were dragged or some element
|
|
|
|
// was added to selection (on mousedown phase) we need to keep
|
|
|
|
// selection unchanged
|
|
|
|
if (
|
|
|
|
hitElement &&
|
|
|
|
!draggingOccurred &&
|
|
|
|
!elementIsAddedToSelection
|
|
|
|
) {
|
|
|
|
if (e.shiftKey) {
|
|
|
|
hitElement.isSelected = false;
|
|
|
|
} else {
|
|
|
|
elements = clearSelection(elements);
|
|
|
|
hitElement.isSelected = true;
|
|
|
|
}
|
|
|
|
}
|
2020-01-16 22:16:11 +00:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
if (draggingElement === null) {
|
|
|
|
// if no element is clicked, clear the selection and redraw
|
2020-01-11 02:45:58 +05:00
|
|
|
elements = clearSelection(elements);
|
2020-01-26 17:14:31 -03:00
|
|
|
this.setState({});
|
|
|
|
return;
|
2020-01-11 02:45:58 +05:00
|
|
|
}
|
|
|
|
|
2020-02-05 22:47:10 +04:00
|
|
|
if (!elementLocked) {
|
2020-01-26 17:14:31 -03:00
|
|
|
draggingElement.isSelected = true;
|
|
|
|
}
|
2020-01-05 15:10:42 +01:00
|
|
|
|
2020-02-05 22:47:10 +04:00
|
|
|
if (
|
|
|
|
elementType !== "selection" ||
|
|
|
|
elements.some(el => el.isSelected)
|
|
|
|
) {
|
|
|
|
history.resumeRecording();
|
|
|
|
}
|
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
if (!elementLocked) {
|
|
|
|
resetCursor();
|
|
|
|
this.setState({
|
|
|
|
draggingElement: null,
|
|
|
|
elementType: "selection",
|
|
|
|
});
|
2020-01-30 22:14:40 +01:00
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
draggingElement: null,
|
|
|
|
});
|
2020-01-26 17:14:31 -03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
lastMouseUp = onMouseUp;
|
2020-01-10 18:01:00 +03:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
window.addEventListener("mousemove", onMouseMove);
|
|
|
|
window.addEventListener("mouseup", onMouseUp);
|
|
|
|
}}
|
|
|
|
onDoubleClick={e => {
|
2020-02-10 15:09:50 +01:00
|
|
|
resetCursor();
|
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
const { x, y } = viewportCoordsToSceneCoords(e, this.state);
|
2020-01-07 22:21:05 +05:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
const elementAtPosition = getElementAtPosition(elements, x, y);
|
|
|
|
|
|
|
|
const element =
|
|
|
|
elementAtPosition && isTextElement(elementAtPosition)
|
|
|
|
? elementAtPosition
|
|
|
|
: newTextElement(
|
|
|
|
newElement(
|
|
|
|
"text",
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
this.state.currentItemStrokeColor,
|
|
|
|
this.state.currentItemBackgroundColor,
|
|
|
|
this.state.currentItemFillStyle,
|
|
|
|
this.state.currentItemStrokeWidth,
|
|
|
|
this.state.currentItemRoughness,
|
|
|
|
this.state.currentItemOpacity,
|
|
|
|
),
|
|
|
|
"", // default text
|
|
|
|
this.state.currentItemFont, // default font
|
|
|
|
);
|
|
|
|
|
|
|
|
this.setState({ editingElement: element });
|
2020-01-19 23:32:24 +01:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
let textX = e.clientX;
|
|
|
|
let textY = e.clientY;
|
2020-01-09 01:09:09 +05:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
if (elementAtPosition && isTextElement(elementAtPosition)) {
|
|
|
|
elements = elements.filter(
|
|
|
|
element => element.id !== elementAtPosition.id,
|
|
|
|
);
|
|
|
|
this.setState({});
|
2020-01-09 01:09:09 +05:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
textX =
|
|
|
|
this.state.scrollX +
|
|
|
|
elementAtPosition.x +
|
|
|
|
elementAtPosition.width / 2;
|
|
|
|
textY =
|
|
|
|
this.state.scrollY +
|
|
|
|
elementAtPosition.y +
|
|
|
|
elementAtPosition.height / 2;
|
|
|
|
|
|
|
|
// x and y will change after calling newTextElement function
|
|
|
|
element.x = elementAtPosition.x + elementAtPosition.width / 2;
|
|
|
|
element.y = elementAtPosition.y + elementAtPosition.height / 2;
|
|
|
|
} else if (!e.altKey) {
|
|
|
|
const snappedToCenterPosition = this.getTextWysiwygSnappedToCenterPosition(
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
);
|
2020-01-09 01:09:09 +05:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
if (snappedToCenterPosition) {
|
|
|
|
element.x = snappedToCenterPosition.elementCenterX;
|
|
|
|
element.y = snappedToCenterPosition.elementCenterY;
|
|
|
|
textX = snappedToCenterPosition.wysiwygX;
|
|
|
|
textY = snappedToCenterPosition.wysiwygY;
|
|
|
|
}
|
2020-01-09 01:09:09 +05:00
|
|
|
}
|
2020-01-05 22:38:19 -03:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
const resetSelection = () => {
|
|
|
|
this.setState({
|
|
|
|
draggingElement: null,
|
|
|
|
editingElement: null,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
textWysiwyg({
|
|
|
|
initText: element.text,
|
|
|
|
x: textX,
|
|
|
|
y: textY,
|
|
|
|
strokeColor: element.strokeColor,
|
|
|
|
font: element.font,
|
|
|
|
opacity: this.state.currentItemOpacity,
|
|
|
|
onSubmit: text => {
|
|
|
|
if (text) {
|
|
|
|
elements = [
|
|
|
|
...elements,
|
|
|
|
{
|
|
|
|
// we need to recreate the element to update dimensions &
|
|
|
|
// position
|
|
|
|
...newTextElement(element, text, element.font),
|
|
|
|
isSelected: true,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
2020-02-05 22:47:10 +04:00
|
|
|
history.resumeRecording();
|
2020-01-26 17:14:31 -03:00
|
|
|
resetSelection();
|
|
|
|
},
|
|
|
|
onCancel: () => {
|
|
|
|
resetSelection();
|
|
|
|
},
|
2020-01-25 18:45:23 +01:00
|
|
|
});
|
2020-01-26 17:14:31 -03:00
|
|
|
}}
|
|
|
|
onMouseMove={e => {
|
2020-02-02 20:04:35 +02:00
|
|
|
if (isHoldingSpace || isPanning) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-26 17:14:31 -03:00
|
|
|
const hasDeselectedButton = Boolean(e.buttons);
|
2020-02-04 17:39:08 +04:00
|
|
|
|
|
|
|
const { x, y } = viewportCoordsToSceneCoords(e, this.state);
|
|
|
|
if (this.state.multiElement) {
|
|
|
|
const { multiElement } = this.state;
|
|
|
|
const originX = multiElement.x;
|
|
|
|
const originY = multiElement.y;
|
|
|
|
const points = multiElement.points;
|
|
|
|
const pnt = points[points.length - 1];
|
|
|
|
pnt[0] = x - originX;
|
|
|
|
pnt[1] = y - originY;
|
|
|
|
multiElement.shape = null;
|
|
|
|
this.setState({});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
if (
|
|
|
|
hasDeselectedButton ||
|
|
|
|
this.state.elementType !== "selection"
|
|
|
|
) {
|
2020-01-12 17:24:11 -03:00
|
|
|
return;
|
|
|
|
}
|
2020-02-04 17:39:08 +04:00
|
|
|
|
2020-01-26 17:14:31 -03:00
|
|
|
const selectedElements = elements.filter(e => e.isSelected)
|
|
|
|
.length;
|
|
|
|
if (selectedElements === 1) {
|
|
|
|
const resizeElement = getElementWithResizeHandler(
|
|
|
|
elements,
|
|
|
|
{ x, y },
|
|
|
|
this.state,
|
|
|
|
);
|
|
|
|
if (resizeElement && resizeElement.resizeHandle) {
|
|
|
|
document.documentElement.style.cursor = getCursorForResizingElement(
|
|
|
|
resizeElement,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const hitElement = getElementAtPosition(elements, x, y);
|
|
|
|
document.documentElement.style.cursor = hitElement ? "move" : "";
|
|
|
|
}}
|
2020-02-02 22:02:13 +01:00
|
|
|
onDrop={e => {
|
|
|
|
const file = e.dataTransfer.files[0];
|
|
|
|
if (file?.type === "application/json") {
|
|
|
|
loadFromBlob(file)
|
|
|
|
.then(({ elements, appState }) =>
|
2020-02-07 23:46:19 +01:00
|
|
|
this.syncActionResult({ elements, appState }),
|
2020-02-02 22:02:13 +01:00
|
|
|
)
|
|
|
|
.catch(err => console.error(err));
|
|
|
|
}
|
|
|
|
}}
|
2020-01-26 17:14:31 -03:00
|
|
|
>
|
|
|
|
{t("labels.drawingCanvas")}
|
|
|
|
</canvas>
|
|
|
|
</main>
|
2020-01-05 15:10:42 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private handleWheel = (e: WheelEvent) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const { deltaX, deltaY } = e;
|
2020-02-05 22:47:10 +04:00
|
|
|
|
2020-01-31 20:09:42 +00:00
|
|
|
this.setState({
|
|
|
|
scrollX: this.state.scrollX - deltaX,
|
|
|
|
scrollY: this.state.scrollY - deltaY,
|
|
|
|
});
|
2020-01-05 15:10:42 +01:00
|
|
|
};
|
|
|
|
|
2020-02-04 11:50:18 +01:00
|
|
|
private addElementsFromPaste = (
|
|
|
|
clipboardElements: readonly ExcalidrawElement[],
|
|
|
|
) => {
|
|
|
|
elements = clearSelection(elements);
|
|
|
|
|
|
|
|
const [minX, minY, maxX, maxY] = getCommonBounds(clipboardElements);
|
|
|
|
|
|
|
|
const elementsCenterX = distance(minX, maxX) / 2;
|
|
|
|
const elementsCenterY = distance(minY, maxY) / 2;
|
|
|
|
|
2020-02-08 16:20:14 -08:00
|
|
|
const dx = cursorX - this.state.scrollX - elementsCenterX;
|
|
|
|
const dy = cursorY - this.state.scrollY - elementsCenterY;
|
2020-02-04 11:50:18 +01:00
|
|
|
|
|
|
|
elements = [
|
|
|
|
...elements,
|
|
|
|
...clipboardElements.map(clipboardElements => {
|
|
|
|
const duplicate = duplicateElement(clipboardElements);
|
|
|
|
duplicate.x += dx - minX;
|
|
|
|
duplicate.y += dy - minY;
|
|
|
|
return duplicate;
|
|
|
|
}),
|
|
|
|
];
|
2020-02-05 22:47:10 +04:00
|
|
|
history.resumeRecording();
|
2020-02-04 11:50:18 +01:00
|
|
|
this.setState({});
|
2020-01-07 07:50:59 +05:00
|
|
|
};
|
|
|
|
|
2020-01-09 01:09:09 +05:00
|
|
|
private getTextWysiwygSnappedToCenterPosition(x: number, y: number) {
|
|
|
|
const elementClickedInside = getElementContainingPosition(elements, x, y);
|
|
|
|
if (elementClickedInside) {
|
|
|
|
const elementCenterX =
|
|
|
|
elementClickedInside.x + elementClickedInside.width / 2;
|
|
|
|
const elementCenterY =
|
|
|
|
elementClickedInside.y + elementClickedInside.height / 2;
|
|
|
|
const distanceToCenter = Math.hypot(
|
|
|
|
x - elementCenterX,
|
2020-01-24 12:04:54 +02:00
|
|
|
y - elementCenterY,
|
2020-01-09 01:09:09 +05:00
|
|
|
);
|
|
|
|
const isSnappedToCenter =
|
|
|
|
distanceToCenter < TEXT_TO_CENTER_SNAP_THRESHOLD;
|
|
|
|
if (isSnappedToCenter) {
|
|
|
|
const wysiwygX =
|
|
|
|
this.state.scrollX +
|
|
|
|
elementClickedInside.x +
|
|
|
|
elementClickedInside.width / 2;
|
|
|
|
const wysiwygY =
|
|
|
|
this.state.scrollY +
|
|
|
|
elementClickedInside.y +
|
|
|
|
elementClickedInside.height / 2;
|
|
|
|
return { wysiwygX, wysiwygY, elementCenterX, elementCenterY };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-11 20:15:41 -08:00
|
|
|
private saveDebounced = debounce(() => {
|
2020-01-28 16:44:34 +01:00
|
|
|
saveToLocalStorage(
|
|
|
|
elements.filter(x => x.type !== "selection"),
|
|
|
|
this.state,
|
|
|
|
);
|
2020-01-11 20:15:41 -08:00
|
|
|
}, 300);
|
|
|
|
|
2020-01-05 15:10:42 +01:00
|
|
|
componentDidUpdate() {
|
2020-02-01 16:52:10 +00:00
|
|
|
const atLeastOneVisibleElement = renderScene(
|
|
|
|
elements,
|
2020-02-05 22:47:10 +04:00
|
|
|
this.state.selectionElement,
|
2020-02-01 16:52:10 +00:00
|
|
|
this.rc!,
|
|
|
|
this.canvas!,
|
|
|
|
{
|
|
|
|
scrollX: this.state.scrollX,
|
|
|
|
scrollY: this.state.scrollY,
|
|
|
|
viewBackgroundColor: this.state.viewBackgroundColor,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
const scrolledOutside = !atLeastOneVisibleElement && elements.length > 0;
|
|
|
|
if (this.state.scrolledOutside !== scrolledOutside) {
|
|
|
|
this.setState({ scrolledOutside: scrolledOutside });
|
|
|
|
}
|
2020-01-11 20:15:41 -08:00
|
|
|
this.saveDebounced();
|
2020-01-06 21:58:48 +04:00
|
|
|
if (history.isRecording()) {
|
2020-02-05 22:47:10 +04:00
|
|
|
history.pushEntry(this.state, elements);
|
|
|
|
history.skipRecording();
|
2020-01-05 15:10:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const rootElement = document.getElementById("root");
|
2020-01-21 15:50:25 +01:00
|
|
|
|
|
|
|
class TopErrorBoundary extends React.Component {
|
|
|
|
state = { hasError: false, stack: "", localStorage: "" };
|
|
|
|
|
|
|
|
static getDerivedStateFromError(error: any) {
|
|
|
|
console.error(error);
|
|
|
|
return {
|
|
|
|
hasError: true,
|
|
|
|
localStorage: JSON.stringify({ ...localStorage }),
|
2020-01-24 12:04:54 +02:00
|
|
|
stack: error.stack,
|
2020-01-21 15:50:25 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
private selectTextArea(event: React.MouseEvent<HTMLTextAreaElement>) {
|
|
|
|
(event.target as HTMLTextAreaElement).select();
|
|
|
|
}
|
|
|
|
|
|
|
|
private async createGithubIssue() {
|
|
|
|
let body = "";
|
|
|
|
try {
|
|
|
|
const templateStr = (await import("./bug-issue-template")).default;
|
|
|
|
if (typeof templateStr === "string") {
|
|
|
|
body = encodeURIComponent(templateStr);
|
|
|
|
}
|
|
|
|
} catch {}
|
|
|
|
|
|
|
|
window.open(
|
2020-01-24 12:04:54 +02:00
|
|
|
`https://github.com/excalidraw/excalidraw/issues/new?body=${body}`,
|
2020-01-21 15:50:25 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (this.state.hasError) {
|
|
|
|
return (
|
|
|
|
<div className="ErrorSplash">
|
|
|
|
<div className="ErrorSplash-messageContainer">
|
|
|
|
<div className="ErrorSplash-paragraph bigger">
|
|
|
|
Encountered an error. Please{" "}
|
|
|
|
<button onClick={() => window.location.reload()}>
|
|
|
|
reload the page
|
|
|
|
</button>
|
|
|
|
.
|
|
|
|
</div>
|
|
|
|
<div className="ErrorSplash-paragraph">
|
|
|
|
If reloading doesn't work. Try{" "}
|
|
|
|
<button
|
|
|
|
onClick={() => {
|
|
|
|
localStorage.clear();
|
|
|
|
window.location.reload();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
clearing the canvas
|
|
|
|
</button>
|
|
|
|
.<br />
|
|
|
|
<div className="smaller">
|
|
|
|
(This will unfortunately result in loss of work.)
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<div className="ErrorSplash-paragraph">
|
|
|
|
Before doing so, we'd appreciate if you opened an issue on our{" "}
|
|
|
|
<button onClick={this.createGithubIssue}>bug tracker</button>.
|
|
|
|
Please include the following error stack trace & localStorage
|
|
|
|
content (provided it's not private):
|
|
|
|
</div>
|
|
|
|
<div className="ErrorSplash-paragraph">
|
|
|
|
<div className="ErrorSplash-details">
|
|
|
|
<label>Error stack trace:</label>
|
|
|
|
<textarea
|
|
|
|
rows={10}
|
|
|
|
onClick={this.selectTextArea}
|
|
|
|
defaultValue={this.state.stack}
|
|
|
|
/>
|
|
|
|
<label>LocalStorage content:</label>
|
|
|
|
<textarea
|
|
|
|
rows={5}
|
|
|
|
onClick={this.selectTextArea}
|
|
|
|
defaultValue={this.state.localStorage}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.props.children;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReactDOM.render(
|
|
|
|
<TopErrorBoundary>
|
2020-01-31 21:06:06 +00:00
|
|
|
<App />
|
2020-01-21 15:50:25 +01:00
|
|
|
</TopErrorBoundary>,
|
2020-01-24 12:04:54 +02:00
|
|
|
rootElement,
|
2020-01-21 15:50:25 +01:00
|
|
|
);
|