2021-01-05 20:06:14 +02:00
|
|
|
import LanguageDetector from "i18next-browser-languagedetector";
|
2021-01-04 02:21:52 +05:30
|
|
|
import React, {
|
2021-01-05 20:06:14 +02:00
|
|
|
useCallback,
|
2021-01-25 10:47:35 +01:00
|
|
|
useContext,
|
2021-01-04 02:21:52 +05:30
|
|
|
useEffect,
|
2021-01-05 20:06:14 +02:00
|
|
|
useLayoutEffect,
|
2021-01-04 02:21:52 +05:30
|
|
|
useRef,
|
2021-01-05 20:06:14 +02:00
|
|
|
useState,
|
2021-01-04 02:21:52 +05:30
|
|
|
} from "react";
|
2021-01-10 20:48:12 +02:00
|
|
|
import { trackEvent } from "../analytics";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { getDefaultAppState } from "../appState";
|
|
|
|
import { ExcalidrawImperativeAPI } from "../components/App";
|
|
|
|
import { ErrorDialog } from "../components/ErrorDialog";
|
|
|
|
import { TopErrorBoundary } from "../components/TopErrorBoundary";
|
2021-01-16 19:59:26 +02:00
|
|
|
import { APP_NAME, EVENT, TITLE_TIMEOUT, VERSION_TIMEOUT } from "../constants";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { ImportedDataState } from "../data/types";
|
|
|
|
import {
|
|
|
|
ExcalidrawElement,
|
|
|
|
NonDeletedExcalidrawElement,
|
|
|
|
} from "../element/types";
|
2021-01-25 10:47:35 +01:00
|
|
|
import { useCallbackRefState } from "../hooks/useCallbackRefState";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { Language, t } from "../i18n";
|
2021-01-04 02:21:52 +05:30
|
|
|
import Excalidraw, {
|
|
|
|
defaultLang,
|
2021-01-05 20:06:14 +02:00
|
|
|
languages,
|
2021-01-04 02:21:52 +05:30
|
|
|
} from "../packages/excalidraw/index";
|
2021-01-25 10:47:35 +01:00
|
|
|
import { AppState } from "../types";
|
2021-01-10 20:48:12 +02:00
|
|
|
import {
|
|
|
|
debounce,
|
|
|
|
getVersion,
|
|
|
|
ResolvablePromise,
|
|
|
|
resolvablePromise,
|
|
|
|
} from "../utils";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { SAVE_TO_LOCAL_STORAGE_TIMEOUT } from "./app_constants";
|
2021-01-25 10:47:35 +01:00
|
|
|
import CollabWrapper, {
|
|
|
|
CollabAPI,
|
|
|
|
CollabContext,
|
|
|
|
CollabContextConsumer,
|
|
|
|
} from "./collab/CollabWrapper";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { LanguageList } from "./components/LanguageList";
|
|
|
|
import { exportToBackend, getCollaborationLinkData, loadScene } from "./data";
|
2020-10-25 19:39:57 +05:30
|
|
|
import {
|
|
|
|
importFromLocalStorage,
|
|
|
|
saveToLocalStorage,
|
2020-12-05 20:00:53 +05:30
|
|
|
} from "./data/localStorage";
|
|
|
|
|
2021-01-04 02:21:52 +05:30
|
|
|
const languageDetector = new LanguageDetector();
|
|
|
|
languageDetector.init({
|
|
|
|
languageUtils: {
|
|
|
|
formatLanguageCode: (langCode: Language["code"]) => langCode,
|
|
|
|
isWhitelisted: () => true,
|
|
|
|
},
|
|
|
|
checkWhitelist: false,
|
|
|
|
});
|
2020-12-05 20:00:53 +05:30
|
|
|
|
2020-10-25 19:39:57 +05:30
|
|
|
const saveDebounced = debounce(
|
|
|
|
(elements: readonly ExcalidrawElement[], state: AppState) => {
|
|
|
|
saveToLocalStorage(elements, state);
|
|
|
|
},
|
|
|
|
SAVE_TO_LOCAL_STORAGE_TIMEOUT,
|
|
|
|
);
|
|
|
|
|
|
|
|
const onBlur = () => {
|
|
|
|
saveDebounced.flush();
|
|
|
|
};
|
|
|
|
|
2020-12-05 20:00:53 +05:30
|
|
|
const initializeScene = async (opts: {
|
2021-02-03 19:14:26 +01:00
|
|
|
collabAPI: CollabAPI;
|
|
|
|
}): Promise<ImportedDataState | null> => {
|
2020-12-05 20:00:53 +05:30
|
|
|
const searchParams = new URLSearchParams(window.location.search);
|
|
|
|
const id = searchParams.get("id");
|
|
|
|
const jsonMatch = window.location.hash.match(
|
|
|
|
/^#json=([0-9]+),([a-zA-Z0-9_-]+)$/,
|
|
|
|
);
|
|
|
|
|
|
|
|
const initialData = importFromLocalStorage();
|
|
|
|
|
|
|
|
let scene = await loadScene(null, null, initialData);
|
|
|
|
|
2021-02-03 19:14:26 +01:00
|
|
|
let roomLinkData = getCollaborationLinkData(window.location.href);
|
|
|
|
const isExternalScene = !!(id || jsonMatch || roomLinkData);
|
2020-12-05 20:00:53 +05:30
|
|
|
if (isExternalScene) {
|
2021-02-05 12:04:33 +01:00
|
|
|
if (
|
|
|
|
// don't prompt if scene is empty
|
|
|
|
!scene.elements.length ||
|
|
|
|
// don't prompt for collab scenes because we don't override local storage
|
|
|
|
roomLinkData ||
|
|
|
|
// otherwise, prompt whether user wants to override current scene
|
|
|
|
window.confirm(t("alerts.loadSceneOverridePrompt"))
|
|
|
|
) {
|
2020-12-05 20:00:53 +05:30
|
|
|
// Backwards compatibility with legacy url format
|
|
|
|
if (id) {
|
|
|
|
scene = await loadScene(id, null, initialData);
|
|
|
|
} else if (jsonMatch) {
|
|
|
|
scene = await loadScene(jsonMatch[1], jsonMatch[2], initialData);
|
|
|
|
}
|
2021-02-03 19:14:26 +01:00
|
|
|
if (!roomLinkData) {
|
2020-12-22 11:34:06 +02:00
|
|
|
window.history.replaceState({}, APP_NAME, window.location.origin);
|
2020-12-05 20:00:53 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// https://github.com/excalidraw/excalidraw/issues/1919
|
|
|
|
if (document.hidden) {
|
2020-12-29 21:03:34 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
window.addEventListener(
|
|
|
|
"focus",
|
|
|
|
() => initializeScene(opts).then(resolve).catch(reject),
|
|
|
|
{
|
|
|
|
once: true,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
2020-12-05 20:00:53 +05:30
|
|
|
}
|
|
|
|
|
2021-02-03 19:14:26 +01:00
|
|
|
roomLinkData = null;
|
2020-12-22 11:34:06 +02:00
|
|
|
window.history.replaceState({}, APP_NAME, window.location.origin);
|
2020-12-05 20:00:53 +05:30
|
|
|
}
|
|
|
|
}
|
2021-02-03 19:14:26 +01:00
|
|
|
if (roomLinkData) {
|
|
|
|
return opts.collabAPI.initializeSocketClient(roomLinkData);
|
2020-12-05 20:00:53 +05:30
|
|
|
} else if (scene) {
|
|
|
|
return scene;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2021-01-25 10:47:35 +01:00
|
|
|
function ExcalidrawWrapper() {
|
2020-12-05 20:00:53 +05:30
|
|
|
// dimensions
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2020-10-25 19:39:57 +05:30
|
|
|
const [dimensions, setDimensions] = useState({
|
|
|
|
width: window.innerWidth,
|
|
|
|
height: window.innerHeight,
|
|
|
|
});
|
2020-12-20 19:44:04 +05:30
|
|
|
const [errorMessage, setErrorMessage] = useState("");
|
2021-01-04 02:21:52 +05:30
|
|
|
const currentLangCode = languageDetector.detect() || defaultLang.code;
|
|
|
|
const [langCode, setLangCode] = useState(currentLangCode);
|
2020-12-05 20:00:53 +05:30
|
|
|
|
2020-10-25 19:39:57 +05:30
|
|
|
useLayoutEffect(() => {
|
|
|
|
const onResize = () => {
|
|
|
|
setDimensions({
|
|
|
|
width: window.innerWidth,
|
|
|
|
height: window.innerHeight,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener("resize", onResize);
|
|
|
|
|
|
|
|
return () => window.removeEventListener("resize", onResize);
|
|
|
|
}, []);
|
|
|
|
|
2020-12-05 20:00:53 +05:30
|
|
|
// initial state
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
const initialStatePromiseRef = useRef<{
|
|
|
|
promise: ResolvablePromise<ImportedDataState | null>;
|
|
|
|
}>({ promise: null! });
|
|
|
|
if (!initialStatePromiseRef.current.promise) {
|
|
|
|
initialStatePromiseRef.current.promise = resolvablePromise<ImportedDataState | null>();
|
|
|
|
}
|
|
|
|
|
2020-10-25 19:39:57 +05:30
|
|
|
useEffect(() => {
|
2021-01-16 19:59:26 +02:00
|
|
|
// Delayed so that the app has a time to load the latest SW
|
2021-01-12 17:47:31 +01:00
|
|
|
setTimeout(() => {
|
2021-01-16 19:59:26 +02:00
|
|
|
trackEvent("load", "version", getVersion());
|
|
|
|
}, VERSION_TIMEOUT);
|
2021-01-25 10:47:35 +01:00
|
|
|
}, []);
|
2021-01-12 17:47:31 +01:00
|
|
|
|
2021-01-25 10:47:35 +01:00
|
|
|
const [
|
|
|
|
excalidrawAPI,
|
|
|
|
excalidrawRefCallback,
|
|
|
|
] = useCallbackRefState<ExcalidrawImperativeAPI>();
|
|
|
|
|
|
|
|
const collabAPI = useContext(CollabContext)?.api;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!collabAPI || !excalidrawAPI) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-03 19:14:26 +01:00
|
|
|
initializeScene({ collabAPI }).then((scene) => {
|
2021-01-25 10:47:35 +01:00
|
|
|
initialStatePromiseRef.current.promise.resolve(scene);
|
2020-10-25 19:39:57 +05:30
|
|
|
});
|
|
|
|
|
2020-12-05 20:00:53 +05:30
|
|
|
const onHashChange = (_: HashChangeEvent) => {
|
2021-02-03 19:14:26 +01:00
|
|
|
initializeScene({ collabAPI }).then((scene) => {
|
|
|
|
if (scene) {
|
|
|
|
excalidrawAPI.updateScene(scene);
|
|
|
|
}
|
|
|
|
});
|
2020-12-05 20:00:53 +05:30
|
|
|
};
|
|
|
|
|
2020-12-22 11:34:06 +02:00
|
|
|
const titleTimeout = setTimeout(
|
|
|
|
() => (document.title = APP_NAME),
|
|
|
|
TITLE_TIMEOUT,
|
|
|
|
);
|
2020-12-05 20:00:53 +05:30
|
|
|
window.addEventListener(EVENT.HASHCHANGE, onHashChange, false);
|
2020-10-25 19:39:57 +05:30
|
|
|
window.addEventListener(EVENT.UNLOAD, onBlur, false);
|
|
|
|
window.addEventListener(EVENT.BLUR, onBlur, false);
|
|
|
|
return () => {
|
2020-12-05 20:00:53 +05:30
|
|
|
window.removeEventListener(EVENT.HASHCHANGE, onHashChange, false);
|
2020-10-25 19:39:57 +05:30
|
|
|
window.removeEventListener(EVENT.UNLOAD, onBlur, false);
|
|
|
|
window.removeEventListener(EVENT.BLUR, onBlur, false);
|
2020-12-22 11:34:06 +02:00
|
|
|
clearTimeout(titleTimeout);
|
2020-10-25 19:39:57 +05:30
|
|
|
};
|
2021-01-25 10:47:35 +01:00
|
|
|
}, [collabAPI, excalidrawAPI]);
|
2020-10-25 19:39:57 +05:30
|
|
|
|
2021-01-04 02:21:52 +05:30
|
|
|
useEffect(() => {
|
|
|
|
languageDetector.cacheUserLanguage(langCode);
|
|
|
|
}, [langCode]);
|
|
|
|
|
2020-12-05 20:00:53 +05:30
|
|
|
const onChange = (
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: AppState,
|
|
|
|
) => {
|
2021-02-03 19:14:26 +01:00
|
|
|
if (collabAPI?.isCollaborating()) {
|
2021-01-25 10:47:35 +01:00
|
|
|
collabAPI.broadcastElements(elements);
|
2021-02-03 19:14:26 +01:00
|
|
|
} else {
|
|
|
|
// collab scenes are persisted to the server, so we don't have to persist
|
|
|
|
// them locally, which has the added benefit of not overwriting whatever
|
|
|
|
// the user was working on before joining
|
|
|
|
saveDebounced(elements, appState);
|
2020-12-05 20:00:53 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-20 19:44:04 +05:30
|
|
|
const onExportToBackend = async (
|
|
|
|
exportedElements: readonly NonDeletedExcalidrawElement[],
|
|
|
|
appState: AppState,
|
|
|
|
canvas: HTMLCanvasElement | null,
|
|
|
|
) => {
|
|
|
|
if (exportedElements.length === 0) {
|
|
|
|
return window.alert(t("alerts.cannotExportEmptyCanvas"));
|
|
|
|
}
|
|
|
|
if (canvas) {
|
|
|
|
try {
|
|
|
|
await exportToBackend(exportedElements, {
|
|
|
|
...appState,
|
|
|
|
viewBackgroundColor: appState.exportBackground
|
|
|
|
? appState.viewBackgroundColor
|
|
|
|
: getDefaultAppState().viewBackgroundColor,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
if (error.name !== "AbortError") {
|
|
|
|
const { width, height } = canvas;
|
|
|
|
console.error(error, { width, height });
|
|
|
|
setErrorMessage(error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-01-04 02:21:52 +05:30
|
|
|
|
|
|
|
const renderFooter = useCallback(
|
|
|
|
(isMobile: boolean) => {
|
|
|
|
const renderLanguageList = () => (
|
|
|
|
<LanguageList
|
|
|
|
onChange={(langCode) => {
|
|
|
|
setLangCode(langCode);
|
|
|
|
}}
|
|
|
|
languages={languages}
|
|
|
|
floating={!isMobile}
|
|
|
|
currentLangCode={langCode}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
if (isMobile) {
|
|
|
|
return (
|
|
|
|
<fieldset>
|
|
|
|
<legend>{t("labels.language")}</legend>
|
|
|
|
{renderLanguageList()}
|
|
|
|
</fieldset>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return renderLanguageList();
|
|
|
|
},
|
|
|
|
[langCode],
|
|
|
|
);
|
|
|
|
|
2020-12-05 20:00:53 +05:30
|
|
|
return (
|
2020-12-20 19:44:04 +05:30
|
|
|
<>
|
|
|
|
<Excalidraw
|
2021-01-25 10:47:35 +01:00
|
|
|
ref={excalidrawRefCallback}
|
2020-12-20 19:44:04 +05:30
|
|
|
onChange={onChange}
|
|
|
|
width={dimensions.width}
|
|
|
|
height={dimensions.height}
|
|
|
|
initialData={initialStatePromiseRef.current.promise}
|
2021-01-25 10:47:35 +01:00
|
|
|
onCollabButtonClick={collabAPI?.onCollabButtonClick}
|
2021-02-03 19:14:26 +01:00
|
|
|
isCollaborating={collabAPI?.isCollaborating()}
|
2021-01-25 10:47:35 +01:00
|
|
|
onPointerUpdate={collabAPI?.onPointerUpdate}
|
2020-12-20 19:44:04 +05:30
|
|
|
onExportToBackend={onExportToBackend}
|
2021-01-04 02:21:52 +05:30
|
|
|
renderFooter={renderFooter}
|
|
|
|
langCode={langCode}
|
2020-12-20 19:44:04 +05:30
|
|
|
/>
|
2021-01-25 10:47:35 +01:00
|
|
|
{excalidrawAPI && <CollabWrapper excalidrawAPI={excalidrawAPI} />}
|
2020-12-20 19:44:04 +05:30
|
|
|
{errorMessage && (
|
|
|
|
<ErrorDialog
|
|
|
|
message={errorMessage}
|
|
|
|
onClose={() => setErrorMessage("")}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</>
|
2020-12-05 20:00:53 +05:30
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function ExcalidrawApp() {
|
|
|
|
return (
|
2020-10-25 19:39:57 +05:30
|
|
|
<TopErrorBoundary>
|
2021-01-25 10:47:35 +01:00
|
|
|
<CollabContextConsumer>
|
|
|
|
<ExcalidrawWrapper />
|
|
|
|
</CollabContextConsumer>
|
2020-10-25 19:39:57 +05:30
|
|
|
</TopErrorBoundary>
|
|
|
|
);
|
|
|
|
}
|