2020-03-17 20:55:40 +01:00
|
|
|
import {
|
|
|
|
PointerType,
|
|
|
|
ExcalidrawLinearElement,
|
2020-04-08 09:49:52 -07:00
|
|
|
NonDeletedExcalidrawElement,
|
|
|
|
NonDeleted,
|
2020-04-08 21:00:27 +01:00
|
|
|
TextAlign,
|
2020-05-14 17:04:33 +02:00
|
|
|
ExcalidrawElement,
|
2020-05-26 13:07:46 -07:00
|
|
|
GroupId,
|
2020-08-08 21:04:15 -07:00
|
|
|
ExcalidrawBindableElement,
|
2020-12-08 15:02:55 +00:00
|
|
|
Arrowhead,
|
2020-12-27 18:26:30 +02:00
|
|
|
ChartType,
|
2021-06-13 21:26:55 +05:30
|
|
|
FontFamilyValues,
|
2021-10-14 14:15:57 +05:30
|
|
|
Theme,
|
2020-03-17 20:55:40 +01:00
|
|
|
} from "./element/types";
|
2020-02-07 18:42:24 +01:00
|
|
|
import { SHAPES } from "./shapes";
|
2020-03-14 21:48:51 -07:00
|
|
|
import { Point as RoughPoint } from "roughjs/bin/geometry";
|
2020-06-01 11:35:44 +02:00
|
|
|
import { LinearElementEditor } from "./element/linearElementEditor";
|
2020-08-08 21:04:15 -07:00
|
|
|
import { SuggestedBinding } from "./element/binding";
|
2020-09-22 21:51:49 +02:00
|
|
|
import { ImportedDataState } from "./data/types";
|
2021-06-01 23:52:13 +05:30
|
|
|
import type App from "./components/App";
|
2020-12-05 20:00:53 +05:30
|
|
|
import type { ResolvablePromise } from "./utils";
|
2020-12-27 18:26:30 +02:00
|
|
|
import { Spreadsheet } from "./charts";
|
2021-01-04 02:21:52 +05:30
|
|
|
import { Language } from "./i18n";
|
2021-04-09 16:49:58 +02:00
|
|
|
import { ClipboardData } from "./clipboard";
|
2021-06-01 23:52:13 +05:30
|
|
|
import { isOverScrollBars } from "./scene";
|
|
|
|
import { MaybeTransformHandleType } from "./element/transformHandles";
|
2021-10-07 13:19:40 +02:00
|
|
|
import { FileSystemHandle } from "./data/filesystem";
|
2020-01-06 20:24:54 +04:00
|
|
|
|
2020-03-14 21:48:51 -07:00
|
|
|
export type Point = Readonly<RoughPoint>;
|
2020-02-19 08:25:01 -08:00
|
|
|
|
2020-06-19 11:36:49 +01:00
|
|
|
export type Collaborator = {
|
|
|
|
pointer?: {
|
|
|
|
x: number;
|
|
|
|
y: number;
|
|
|
|
};
|
|
|
|
button?: "up" | "down";
|
|
|
|
selectedElementIds?: AppState["selectedElementIds"];
|
|
|
|
username?: string | null;
|
2021-02-04 11:55:43 +01:00
|
|
|
userState?: UserIdleState;
|
2021-02-06 23:33:52 +05:30
|
|
|
color?: {
|
|
|
|
background: string;
|
|
|
|
stroke: string;
|
|
|
|
};
|
2020-06-19 11:36:49 +01:00
|
|
|
};
|
|
|
|
|
2020-01-06 20:24:54 +04:00
|
|
|
export type AppState = {
|
2020-03-26 18:28:26 +01:00
|
|
|
isLoading: boolean;
|
2020-04-03 12:50:51 +01:00
|
|
|
errorMessage: string | null;
|
2020-04-08 09:49:52 -07:00
|
|
|
draggingElement: NonDeletedExcalidrawElement | null;
|
|
|
|
resizingElement: NonDeletedExcalidrawElement | null;
|
|
|
|
multiElement: NonDeleted<ExcalidrawLinearElement> | null;
|
|
|
|
selectionElement: NonDeletedExcalidrawElement | null;
|
2020-08-08 21:04:15 -07:00
|
|
|
isBindingEnabled: boolean;
|
|
|
|
startBoundElement: NonDeleted<ExcalidrawBindableElement> | null;
|
|
|
|
suggestedBindings: SuggestedBinding[];
|
2020-01-19 23:32:24 +01:00
|
|
|
// element being edited, but not necessarily added to elements array yet
|
2020-11-05 19:06:18 +02:00
|
|
|
// (e.g. text element when typing into the input)
|
2020-04-08 09:49:52 -07:00
|
|
|
editingElement: NonDeletedExcalidrawElement | null;
|
2020-06-01 11:35:44 +02:00
|
|
|
editingLinearElement: LinearElementEditor | null;
|
2020-02-07 18:42:24 +01:00
|
|
|
elementType: typeof SHAPES[number]["value"];
|
2020-01-20 15:52:19 -08:00
|
|
|
elementLocked: boolean;
|
2020-01-06 20:24:54 +04:00
|
|
|
exportBackground: boolean;
|
2020-10-13 14:47:07 +02:00
|
|
|
exportEmbedScene: boolean;
|
2021-02-24 19:52:17 +05:30
|
|
|
exportWithDarkMode: boolean;
|
2021-05-30 15:31:12 +01:00
|
|
|
exportScale: number;
|
2020-01-06 20:24:54 +04:00
|
|
|
currentItemStrokeColor: string;
|
|
|
|
currentItemBackgroundColor: string;
|
2020-12-11 13:13:23 +02:00
|
|
|
currentItemFillStyle: ExcalidrawElement["fillStyle"];
|
2020-01-25 18:58:57 +01:00
|
|
|
currentItemStrokeWidth: number;
|
2020-05-14 17:04:33 +02:00
|
|
|
currentItemStrokeStyle: ExcalidrawElement["strokeStyle"];
|
2020-01-25 18:58:57 +01:00
|
|
|
currentItemRoughness: number;
|
|
|
|
currentItemOpacity: number;
|
2021-06-13 21:26:55 +05:30
|
|
|
currentItemFontFamily: FontFamilyValues;
|
2020-05-27 15:14:50 +02:00
|
|
|
currentItemFontSize: number;
|
2020-04-08 21:00:27 +01:00
|
|
|
currentItemTextAlign: TextAlign;
|
2020-08-15 00:59:43 +09:00
|
|
|
currentItemStrokeSharpness: ExcalidrawElement["strokeSharpness"];
|
2020-12-12 16:42:30 +00:00
|
|
|
currentItemStartArrowhead: Arrowhead | null;
|
|
|
|
currentItemEndArrowhead: Arrowhead | null;
|
2020-08-15 00:59:43 +09:00
|
|
|
currentItemLinearStrokeSharpness: ExcalidrawElement["strokeSharpness"];
|
2020-01-06 20:24:54 +04:00
|
|
|
viewBackgroundColor: string;
|
2021-01-31 10:47:43 +01:00
|
|
|
scrollX: number;
|
|
|
|
scrollY: number;
|
2020-04-04 16:12:19 +01:00
|
|
|
cursorButton: "up" | "down";
|
2020-02-01 16:52:10 +00:00
|
|
|
scrolledOutside: boolean;
|
2020-12-01 14:00:13 +01:00
|
|
|
name: string;
|
2020-02-03 21:52:21 +04:00
|
|
|
isResizing: boolean;
|
2020-04-02 17:40:26 +09:00
|
|
|
isRotating: boolean;
|
2020-11-04 17:49:15 +00:00
|
|
|
zoom: Zoom;
|
2021-05-30 12:05:07 +02:00
|
|
|
openMenu: "canvas" | "shape" | null;
|
|
|
|
openPopup:
|
2021-05-28 17:22:42 +05:30
|
|
|
| "canvasColorPicker"
|
|
|
|
| "backgroundColorPicker"
|
|
|
|
| "strokeColorPicker"
|
|
|
|
| null;
|
2020-02-21 14:34:18 -05:00
|
|
|
lastPointerDownWith: PointerType;
|
2020-03-08 10:20:55 -07:00
|
|
|
selectedElementIds: { [id: string]: boolean };
|
2020-06-02 18:41:40 +02:00
|
|
|
previousSelectedElementIds: { [id: string]: boolean };
|
2020-03-28 16:59:36 -07:00
|
|
|
shouldCacheIgnoreZoom: boolean;
|
2021-01-17 17:46:23 +01:00
|
|
|
showHelpDialog: boolean;
|
2021-01-15 20:32:46 +05:30
|
|
|
toastMessage: string | null;
|
2020-04-25 18:43:02 +05:30
|
|
|
zenModeEnabled: boolean;
|
2021-10-14 14:15:57 +05:30
|
|
|
theme: Theme;
|
2020-06-24 00:24:52 +09:00
|
|
|
gridSize: number | null;
|
2021-02-02 02:26:42 +05:30
|
|
|
viewModeEnabled: boolean;
|
2020-05-26 13:07:46 -07:00
|
|
|
|
2020-05-30 22:48:57 +02:00
|
|
|
/** top-most selected groups (i.e. does not include nested groups) */
|
2020-05-26 13:07:46 -07:00
|
|
|
selectedGroupIds: { [groupId: string]: boolean };
|
2020-05-30 22:48:57 +02:00
|
|
|
/** group being edited when you drill down to its constituent element
|
|
|
|
(e.g. when you double-click on a group's element) */
|
2020-05-26 13:07:46 -07:00
|
|
|
editingGroupId: GroupId | null;
|
2020-07-07 20:40:39 +05:30
|
|
|
width: number;
|
|
|
|
height: number;
|
2020-07-27 17:18:49 +05:30
|
|
|
offsetTop: number;
|
|
|
|
offsetLeft: number;
|
2020-07-10 02:20:23 -07:00
|
|
|
|
|
|
|
isLibraryOpen: boolean;
|
2021-10-07 13:19:40 +02:00
|
|
|
fileHandle: FileSystemHandle | null;
|
2020-12-05 20:00:53 +05:30
|
|
|
collaborators: Map<string, Collaborator>;
|
2020-12-07 18:35:16 +02:00
|
|
|
showStats: boolean;
|
2020-12-27 18:26:30 +02:00
|
|
|
currentChartType: ChartType;
|
|
|
|
pasteDialog:
|
|
|
|
| {
|
|
|
|
shown: false;
|
|
|
|
data: null;
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
shown: true;
|
|
|
|
data: Spreadsheet;
|
|
|
|
};
|
2020-01-06 20:24:54 +04:00
|
|
|
};
|
2020-02-21 08:17:20 -05:00
|
|
|
|
2020-11-04 17:49:15 +00:00
|
|
|
export type NormalizedZoomValue = number & { _brand: "normalizedZoom" };
|
|
|
|
|
|
|
|
export type Zoom = Readonly<{
|
|
|
|
value: NormalizedZoomValue;
|
|
|
|
translation: Readonly<{
|
|
|
|
x: number;
|
|
|
|
y: number;
|
|
|
|
}>;
|
|
|
|
}>;
|
|
|
|
|
2020-03-08 19:25:16 -07:00
|
|
|
export type PointerCoords = Readonly<{
|
2020-02-21 08:17:20 -05:00
|
|
|
x: number;
|
|
|
|
y: number;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
export type Gesture = {
|
2020-03-08 19:25:16 -07:00
|
|
|
pointers: Map<number, PointerCoords>;
|
2020-02-21 08:17:20 -05:00
|
|
|
lastCenter: { x: number; y: number } | null;
|
|
|
|
initialDistance: number | null;
|
|
|
|
initialScale: number | null;
|
|
|
|
};
|
2020-03-01 14:39:03 -05:00
|
|
|
|
|
|
|
export declare class GestureEvent extends UIEvent {
|
|
|
|
readonly rotation: number;
|
|
|
|
readonly scale: number;
|
|
|
|
}
|
2020-04-12 16:24:52 +05:30
|
|
|
|
2020-11-08 17:08:22 +01:00
|
|
|
export type LibraryItem = readonly NonDeleted<ExcalidrawElement>[];
|
2020-07-27 15:29:19 +03:00
|
|
|
export type LibraryItems = readonly LibraryItem[];
|
2020-07-11 18:43:20 +05:30
|
|
|
|
2020-12-09 01:35:08 +05:30
|
|
|
// NOTE ready/readyPromise props are optional for host apps' sake (our own
|
|
|
|
// implem guarantees existence)
|
2020-12-05 20:00:53 +05:30
|
|
|
export type ExcalidrawAPIRefValue =
|
2021-01-25 10:47:35 +01:00
|
|
|
| ExcalidrawImperativeAPI
|
2020-12-05 20:00:53 +05:30
|
|
|
| {
|
2020-12-09 01:35:08 +05:30
|
|
|
readyPromise?: ResolvablePromise<ExcalidrawImperativeAPI>;
|
|
|
|
ready?: false;
|
2020-12-05 20:00:53 +05:30
|
|
|
};
|
|
|
|
|
2020-07-11 18:43:20 +05:30
|
|
|
export interface ExcalidrawProps {
|
2020-08-20 16:45:20 +02:00
|
|
|
onChange?: (
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: AppState,
|
|
|
|
) => void;
|
2020-12-05 20:00:53 +05:30
|
|
|
initialData?: ImportedDataState | null | Promise<ImportedDataState | null>;
|
|
|
|
excalidrawRef?: ForwardRef<ExcalidrawAPIRefValue>;
|
|
|
|
onCollabButtonClick?: () => void;
|
|
|
|
isCollaborating?: boolean;
|
|
|
|
onPointerUpdate?: (payload: {
|
|
|
|
pointer: { x: number; y: number };
|
|
|
|
button: "down" | "up";
|
|
|
|
pointersMap: Gesture["pointers"];
|
|
|
|
}) => void;
|
2021-04-09 16:49:58 +02:00
|
|
|
onPaste?: (
|
|
|
|
data: ClipboardData,
|
|
|
|
event: ClipboardEvent | null,
|
|
|
|
) => Promise<boolean> | boolean;
|
2021-05-13 21:02:59 +05:30
|
|
|
renderTopRightUI?: (isMobile: boolean, appState: AppState) => JSX.Element;
|
2021-05-15 14:49:58 +05:30
|
|
|
renderFooter?: (isMobile: boolean, appState: AppState) => JSX.Element;
|
2021-01-04 02:21:52 +05:30
|
|
|
langCode?: Language["code"];
|
2021-02-02 02:26:42 +05:30
|
|
|
viewModeEnabled?: boolean;
|
2021-02-06 21:22:28 +05:30
|
|
|
zenModeEnabled?: boolean;
|
|
|
|
gridModeEnabled?: boolean;
|
2021-03-13 12:35:35 +01:00
|
|
|
libraryReturnUrl?: string;
|
2021-10-14 14:15:57 +05:30
|
|
|
theme?: Theme;
|
2021-03-20 16:08:03 +05:30
|
|
|
name?: string;
|
2021-03-29 20:06:34 +05:30
|
|
|
renderCustomStats?: (
|
|
|
|
elements: readonly NonDeletedExcalidrawElement[],
|
|
|
|
appState: AppState,
|
|
|
|
) => JSX.Element;
|
2021-04-04 15:57:14 +05:30
|
|
|
UIOptions?: UIOptions;
|
2021-04-09 20:44:54 +05:30
|
|
|
detectScroll?: boolean;
|
2021-04-13 01:29:25 +05:30
|
|
|
handleKeyboardGlobally?: boolean;
|
2021-04-21 23:38:24 +05:30
|
|
|
onLibraryChange?: (libraryItems: LibraryItems) => void | Promise<any>;
|
2021-06-02 19:54:21 +05:30
|
|
|
autoFocus?: boolean;
|
2020-07-11 18:43:20 +05:30
|
|
|
}
|
2020-12-05 20:00:53 +05:30
|
|
|
|
|
|
|
export type SceneData = {
|
|
|
|
elements?: ImportedDataState["elements"];
|
|
|
|
appState?: ImportedDataState["appState"];
|
|
|
|
collaborators?: Map<string, Collaborator>;
|
|
|
|
commitToHistory?: boolean;
|
|
|
|
};
|
2021-03-28 19:26:03 +05:30
|
|
|
|
|
|
|
export enum UserIdleState {
|
|
|
|
ACTIVE = "active",
|
|
|
|
AWAY = "away",
|
|
|
|
IDLE = "idle",
|
|
|
|
}
|
2021-04-04 15:57:14 +05:30
|
|
|
|
2021-05-29 02:56:25 +05:30
|
|
|
export type ExportOpts = {
|
|
|
|
saveFileToDisk?: boolean;
|
|
|
|
onExportToBackend?: (
|
|
|
|
exportedElements: readonly NonDeletedExcalidrawElement[],
|
|
|
|
appState: AppState,
|
|
|
|
canvas: HTMLCanvasElement | null,
|
|
|
|
) => void;
|
2021-05-30 00:37:38 +05:30
|
|
|
renderCustomUI?: (
|
|
|
|
exportedElements: readonly NonDeletedExcalidrawElement[],
|
|
|
|
appState: AppState,
|
|
|
|
canvas: HTMLCanvasElement | null,
|
|
|
|
) => JSX.Element;
|
2021-05-29 02:56:25 +05:30
|
|
|
};
|
|
|
|
|
2021-04-04 15:57:14 +05:30
|
|
|
type CanvasActions = {
|
|
|
|
changeViewBackgroundColor?: boolean;
|
|
|
|
clearCanvas?: boolean;
|
2021-05-29 02:56:25 +05:30
|
|
|
export?: false | ExportOpts;
|
2021-04-04 15:57:14 +05:30
|
|
|
loadScene?: boolean;
|
2021-05-28 02:10:33 +05:30
|
|
|
saveToActiveFile?: boolean;
|
2021-04-04 15:57:14 +05:30
|
|
|
theme?: boolean;
|
2021-05-29 19:41:50 +05:30
|
|
|
saveAsImage?: boolean;
|
2021-04-04 15:57:14 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
export type UIOptions = {
|
|
|
|
canvasActions?: CanvasActions;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type AppProps = ExcalidrawProps & {
|
|
|
|
UIOptions: {
|
2021-05-29 02:56:25 +05:30
|
|
|
canvasActions: Required<CanvasActions> & { export: ExportOpts };
|
2021-04-04 15:57:14 +05:30
|
|
|
};
|
2021-04-09 20:44:54 +05:30
|
|
|
detectScroll: boolean;
|
2021-04-13 01:29:25 +05:30
|
|
|
handleKeyboardGlobally: boolean;
|
2021-04-04 15:57:14 +05:30
|
|
|
};
|
2021-06-01 23:52:13 +05:30
|
|
|
|
|
|
|
export type PointerDownState = Readonly<{
|
|
|
|
// The first position at which pointerDown happened
|
|
|
|
origin: Readonly<{ x: number; y: number }>;
|
|
|
|
// Same as "origin" but snapped to the grid, if grid is on
|
|
|
|
originInGrid: Readonly<{ x: number; y: number }>;
|
|
|
|
// Scrollbar checks
|
|
|
|
scrollbars: ReturnType<typeof isOverScrollBars>;
|
|
|
|
// The previous pointer position
|
|
|
|
lastCoords: { x: number; y: number };
|
|
|
|
// map of original elements data
|
|
|
|
originalElements: Map<string, NonDeleted<ExcalidrawElement>>;
|
|
|
|
resize: {
|
|
|
|
// Handle when resizing, might change during the pointer interaction
|
|
|
|
handleType: MaybeTransformHandleType;
|
|
|
|
// This is determined on the initial pointer down event
|
|
|
|
isResizing: boolean;
|
|
|
|
// This is determined on the initial pointer down event
|
|
|
|
offset: { x: number; y: number };
|
|
|
|
// This is determined on the initial pointer down event
|
|
|
|
arrowDirection: "origin" | "end";
|
|
|
|
// This is a center point of selected elements determined on the initial pointer down event (for rotation only)
|
|
|
|
center: { x: number; y: number };
|
|
|
|
};
|
|
|
|
hit: {
|
|
|
|
// The element the pointer is "hitting", is determined on the initial
|
|
|
|
// pointer down event
|
|
|
|
element: NonDeleted<ExcalidrawElement> | null;
|
|
|
|
// The elements the pointer is "hitting", is determined on the initial
|
|
|
|
// pointer down event
|
|
|
|
allHitElements: NonDeleted<ExcalidrawElement>[];
|
|
|
|
// This is determined on the initial pointer down event
|
|
|
|
wasAddedToSelection: boolean;
|
|
|
|
// Whether selected element(s) were duplicated, might change during the
|
|
|
|
// pointer interaction
|
|
|
|
hasBeenDuplicated: boolean;
|
|
|
|
hasHitCommonBoundingBoxOfSelectedElements: boolean;
|
|
|
|
};
|
|
|
|
withCmdOrCtrl: boolean;
|
|
|
|
drag: {
|
|
|
|
// Might change during the pointer interation
|
|
|
|
hasOccurred: boolean;
|
|
|
|
// Might change during the pointer interation
|
|
|
|
offset: { x: number; y: number } | null;
|
|
|
|
};
|
|
|
|
// We need to have these in the state so that we can unsubscribe them
|
|
|
|
eventListeners: {
|
|
|
|
// It's defined on the initial pointer down event
|
|
|
|
onMove: null | ((event: PointerEvent) => void);
|
|
|
|
// It's defined on the initial pointer down event
|
|
|
|
onUp: null | ((event: PointerEvent) => void);
|
|
|
|
// It's defined on the initial pointer down event
|
|
|
|
onKeyDown: null | ((event: KeyboardEvent) => void);
|
|
|
|
// It's defined on the initial pointer down event
|
|
|
|
onKeyUp: null | ((event: KeyboardEvent) => void);
|
|
|
|
};
|
|
|
|
}>;
|
|
|
|
|
|
|
|
export type ExcalidrawImperativeAPI = {
|
|
|
|
updateScene: InstanceType<typeof App>["updateScene"];
|
|
|
|
resetScene: InstanceType<typeof App>["resetScene"];
|
|
|
|
getSceneElementsIncludingDeleted: InstanceType<
|
|
|
|
typeof App
|
|
|
|
>["getSceneElementsIncludingDeleted"];
|
|
|
|
history: {
|
|
|
|
clear: InstanceType<typeof App>["resetHistory"];
|
|
|
|
};
|
|
|
|
scrollToContent: InstanceType<typeof App>["scrollToContent"];
|
|
|
|
getSceneElements: InstanceType<typeof App>["getSceneElements"];
|
|
|
|
getAppState: () => InstanceType<typeof App>["state"];
|
|
|
|
refresh: InstanceType<typeof App>["refresh"];
|
|
|
|
importLibrary: InstanceType<typeof App>["importLibraryFromUrl"];
|
|
|
|
setToastMessage: InstanceType<typeof App>["setToastMessage"];
|
|
|
|
readyPromise: ResolvablePromise<ExcalidrawImperativeAPI>;
|
|
|
|
ready: true;
|
|
|
|
id: string;
|
|
|
|
};
|