mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-04-14 16:40:58 -04:00
fix: remove dependency of t in blob.ts (#7717)
* remove dependency of t in blob.ts * fix
This commit is contained in:
parent
f5ab3e4e12
commit
f639d44a95
5 changed files with 86 additions and 44 deletions
|
@ -3214,7 +3214,13 @@ class App extends React.Component<AppProps, AppState> {
|
||||||
try {
|
try {
|
||||||
return { file: await ImageURLToFile(url) };
|
return { file: await ImageURLToFile(url) };
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return { errorMessage: error.message as string };
|
let errorMessage = error.message;
|
||||||
|
if (error.cause === "FETCH_ERROR") {
|
||||||
|
errorMessage = t("errors.failedToFetchImage");
|
||||||
|
} else if (error.cause === "UNSUPPORTED") {
|
||||||
|
errorMessage = t("errors.unsupportedFileType");
|
||||||
|
}
|
||||||
|
return { errorMessage };
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
@ -8478,10 +8484,18 @@ class App extends React.Component<AppProps, AppState> {
|
||||||
// mustn't be larger than 128 px
|
// mustn't be larger than 128 px
|
||||||
// https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Basic_User_Interface/Using_URL_values_for_the_cursor_property
|
// https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Basic_User_Interface/Using_URL_values_for_the_cursor_property
|
||||||
const cursorImageSizePx = 96;
|
const cursorImageSizePx = 96;
|
||||||
|
let imagePreview;
|
||||||
|
|
||||||
const imagePreview = await resizeImageFile(imageFile, {
|
try {
|
||||||
|
imagePreview = await resizeImageFile(imageFile, {
|
||||||
maxWidthOrHeight: cursorImageSizePx,
|
maxWidthOrHeight: cursorImageSizePx,
|
||||||
});
|
});
|
||||||
|
} catch (e: any) {
|
||||||
|
if (e.cause === "UNSUPPORTED") {
|
||||||
|
throw new Error(t("errors.unsupportedFileType"));
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
let previewDataURL = await getDataURL(imagePreview);
|
let previewDataURL = await getDataURL(imagePreview);
|
||||||
|
|
||||||
|
@ -8870,8 +8884,9 @@ class App extends React.Component<AppProps, AppState> {
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
// Don't throw for image scene daa
|
||||||
if (error.name !== "EncodingError") {
|
if (error.name !== "EncodingError") {
|
||||||
throw error;
|
throw new Error(t("alerts.couldNotLoadInvalidFile"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8945,12 +8960,39 @@ class App extends React.Component<AppProps, AppState> {
|
||||||
) => {
|
) => {
|
||||||
file = await normalizeFile(file);
|
file = await normalizeFile(file);
|
||||||
try {
|
try {
|
||||||
const ret = await loadSceneOrLibraryFromBlob(
|
let ret;
|
||||||
|
try {
|
||||||
|
ret = await loadSceneOrLibraryFromBlob(
|
||||||
file,
|
file,
|
||||||
this.state,
|
this.state,
|
||||||
this.scene.getElementsIncludingDeleted(),
|
this.scene.getElementsIncludingDeleted(),
|
||||||
fileHandle,
|
fileHandle,
|
||||||
);
|
);
|
||||||
|
} catch (error: any) {
|
||||||
|
const imageSceneDataError = error instanceof ImageSceneDataError;
|
||||||
|
if (
|
||||||
|
imageSceneDataError &&
|
||||||
|
error.code === "IMAGE_NOT_CONTAINS_SCENE_DATA" &&
|
||||||
|
!this.isToolSupported("image")
|
||||||
|
) {
|
||||||
|
this.setState({
|
||||||
|
isLoading: false,
|
||||||
|
errorMessage: t("errors.imageToolNotSupported"),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const errorMessage = imageSceneDataError
|
||||||
|
? t("alerts.cannotRestoreFromImage")
|
||||||
|
: t("alerts.couldNotLoadInvalidFile");
|
||||||
|
this.setState({
|
||||||
|
isLoading: false,
|
||||||
|
errorMessage,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (!ret) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (ret.type === MIME_TYPES.excalidraw) {
|
if (ret.type === MIME_TYPES.excalidraw) {
|
||||||
this.setState({ isLoading: true });
|
this.setState({ isLoading: true });
|
||||||
this.syncActionResult({
|
this.syncActionResult({
|
||||||
|
@ -8975,17 +9017,6 @@ class App extends React.Component<AppProps, AppState> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (
|
|
||||||
error instanceof ImageSceneDataError &&
|
|
||||||
error.code === "IMAGE_NOT_CONTAINS_SCENE_DATA" &&
|
|
||||||
!this.isToolSupported("image")
|
|
||||||
) {
|
|
||||||
this.setState({
|
|
||||||
isLoading: false,
|
|
||||||
errorMessage: t("errors.imageToolNotSupported"),
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.setState({ isLoading: false, errorMessage: error.message });
|
this.setState({ isLoading: false, errorMessage: error.message });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -124,8 +124,15 @@ const ImageExportModal = ({
|
||||||
setRenderError(null);
|
setRenderError(null);
|
||||||
// if converting to blob fails, there's some problem that will
|
// if converting to blob fails, there's some problem that will
|
||||||
// likely prevent preview and export (e.g. canvas too big)
|
// likely prevent preview and export (e.g. canvas too big)
|
||||||
return canvasToBlob(canvas).then(() => {
|
return canvasToBlob(canvas)
|
||||||
|
.then(() => {
|
||||||
previewNode.replaceChildren(canvas);
|
previewNode.replaceChildren(canvas);
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
if (e.name === "CANVAS_POSSIBLY_TOO_BIG") {
|
||||||
|
throw new Error(t("canvasError.canvasTooBig"));
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
|
|
|
@ -10,6 +10,7 @@ import { NonDeletedExcalidrawElement } from "../../element/types";
|
||||||
import { AppClassProperties, BinaryFiles } from "../../types";
|
import { AppClassProperties, BinaryFiles } from "../../types";
|
||||||
import { canvasToBlob } from "../../data/blob";
|
import { canvasToBlob } from "../../data/blob";
|
||||||
import { EditorLocalStorage } from "../../data/EditorLocalStorage";
|
import { EditorLocalStorage } from "../../data/EditorLocalStorage";
|
||||||
|
import { t } from "../../i18n";
|
||||||
|
|
||||||
const resetPreview = ({
|
const resetPreview = ({
|
||||||
canvasRef,
|
canvasRef,
|
||||||
|
@ -108,7 +109,14 @@ export const convertMermaidToExcalidraw = async ({
|
||||||
});
|
});
|
||||||
// if converting to blob fails, there's some problem that will
|
// if converting to blob fails, there's some problem that will
|
||||||
// likely prevent preview and export (e.g. canvas too big)
|
// likely prevent preview and export (e.g. canvas too big)
|
||||||
|
try {
|
||||||
await canvasToBlob(canvas);
|
await canvasToBlob(canvas);
|
||||||
|
} catch (e: any) {
|
||||||
|
if (e.name === "CANVAS_POSSIBLY_TOO_BIG") {
|
||||||
|
throw new Error(t("canvasError.canvasTooBig"));
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
parent.style.background = "var(--default-bg-color)";
|
parent.style.background = "var(--default-bg-color)";
|
||||||
canvasNode.replaceChildren(canvas);
|
canvasNode.replaceChildren(canvas);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
|
|
|
@ -4,7 +4,6 @@ import { IMAGE_MIME_TYPES, MIME_TYPES } from "../constants";
|
||||||
import { clearElementsForExport } from "../element";
|
import { clearElementsForExport } from "../element";
|
||||||
import { ExcalidrawElement, FileId } from "../element/types";
|
import { ExcalidrawElement, FileId } from "../element/types";
|
||||||
import { CanvasError, ImageSceneDataError } from "../errors";
|
import { CanvasError, ImageSceneDataError } from "../errors";
|
||||||
import { t } from "../i18n";
|
|
||||||
import { calculateScrollCenter } from "../scene";
|
import { calculateScrollCenter } from "../scene";
|
||||||
import { AppState, DataURL, LibraryItem } from "../types";
|
import { AppState, DataURL, LibraryItem } from "../types";
|
||||||
import { ValueOf } from "../utility-types";
|
import { ValueOf } from "../utility-types";
|
||||||
|
@ -23,11 +22,11 @@ const parseFileContents = async (blob: Blob | File) => {
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (error.message === "INVALID") {
|
if (error.message === "INVALID") {
|
||||||
throw new ImageSceneDataError(
|
throw new ImageSceneDataError(
|
||||||
t("alerts.imageDoesNotContainScene"),
|
"Image doesn't contain scene",
|
||||||
"IMAGE_NOT_CONTAINS_SCENE_DATA",
|
"IMAGE_NOT_CONTAINS_SCENE_DATA",
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
throw new ImageSceneDataError(t("alerts.cannotRestoreFromImage"));
|
throw new ImageSceneDataError("Error: cannot restore image");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -54,11 +53,11 @@ const parseFileContents = async (blob: Blob | File) => {
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (error.message === "INVALID") {
|
if (error.message === "INVALID") {
|
||||||
throw new ImageSceneDataError(
|
throw new ImageSceneDataError(
|
||||||
t("alerts.imageDoesNotContainScene"),
|
"Image doesn't contain scene",
|
||||||
"IMAGE_NOT_CONTAINS_SCENE_DATA",
|
"IMAGE_NOT_CONTAINS_SCENE_DATA",
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
throw new ImageSceneDataError(t("alerts.cannotRestoreFromImage"));
|
throw new ImageSceneDataError("Error: cannot restore image");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -130,7 +129,7 @@ export const loadSceneOrLibraryFromBlob = async (
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (isSupportedImageFile(blob)) {
|
if (isSupportedImageFile(blob)) {
|
||||||
throw new ImageSceneDataError(
|
throw new ImageSceneDataError(
|
||||||
t("alerts.imageDoesNotContainScene"),
|
"Image doesn't contain scene",
|
||||||
"IMAGE_NOT_CONTAINS_SCENE_DATA",
|
"IMAGE_NOT_CONTAINS_SCENE_DATA",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -163,12 +162,12 @@ export const loadSceneOrLibraryFromBlob = async (
|
||||||
data,
|
data,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
throw new Error(t("alerts.couldNotLoadInvalidFile"));
|
throw new Error("Error: invalid file");
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
if (error instanceof ImageSceneDataError) {
|
if (error instanceof ImageSceneDataError) {
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
throw new Error(t("alerts.couldNotLoadInvalidFile"));
|
throw new Error("Error: invalid file");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -187,7 +186,7 @@ export const loadFromBlob = async (
|
||||||
fileHandle,
|
fileHandle,
|
||||||
);
|
);
|
||||||
if (ret.type !== MIME_TYPES.excalidraw) {
|
if (ret.type !== MIME_TYPES.excalidraw) {
|
||||||
throw new Error(t("alerts.couldNotLoadInvalidFile"));
|
throw new Error("Error: invalid file");
|
||||||
}
|
}
|
||||||
return ret.data;
|
return ret.data;
|
||||||
};
|
};
|
||||||
|
@ -222,10 +221,7 @@ export const canvasToBlob = async (
|
||||||
canvas.toBlob((blob) => {
|
canvas.toBlob((blob) => {
|
||||||
if (!blob) {
|
if (!blob) {
|
||||||
return reject(
|
return reject(
|
||||||
new CanvasError(
|
new CanvasError("Error: Canvas too big", "CANVAS_POSSIBLY_TOO_BIG"),
|
||||||
t("canvasError.canvasTooBig"),
|
|
||||||
"CANVAS_POSSIBLY_TOO_BIG",
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
resolve(blob);
|
resolve(blob);
|
||||||
|
@ -314,7 +310,7 @@ export const resizeImageFile = async (
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isSupportedImageFile(file)) {
|
if (!isSupportedImageFile(file)) {
|
||||||
throw new Error(t("errors.unsupportedFileType"));
|
throw new Error("Error: unsupported file type", { cause: "UNSUPPORTED" });
|
||||||
}
|
}
|
||||||
|
|
||||||
return new File(
|
return new File(
|
||||||
|
@ -340,11 +336,11 @@ export const ImageURLToFile = async (
|
||||||
try {
|
try {
|
||||||
response = await fetch(imageUrl);
|
response = await fetch(imageUrl);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
throw new Error(t("errors.failedToFetchImage"));
|
throw new Error("Error: failed to fetch image", { cause: "FETCH_ERROR" });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(t("errors.failedToFetchImage"));
|
throw new Error("Error: failed to fetch image", { cause: "FETCH_ERROR" });
|
||||||
}
|
}
|
||||||
|
|
||||||
const blob = await response.blob();
|
const blob = await response.blob();
|
||||||
|
@ -354,7 +350,7 @@ export const ImageURLToFile = async (
|
||||||
return new File([blob], name, { type: blob.type });
|
return new File([blob], name, { type: blob.type });
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(t("errors.unsupportedFileType"));
|
throw new Error("Error: unsupported file type", { cause: "UNSUPPORTED" });
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getFileFromEvent = async (
|
export const getFileFromEvent = async (
|
||||||
|
|
|
@ -179,7 +179,7 @@ export const exportCanvas = async (
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.warn(error);
|
console.warn(error);
|
||||||
if (error.name === "CANVAS_POSSIBLY_TOO_BIG") {
|
if (error.name === "CANVAS_POSSIBLY_TOO_BIG") {
|
||||||
throw error;
|
throw new Error(t("canvasError.canvasTooBig"));
|
||||||
}
|
}
|
||||||
// TypeError *probably* suggests ClipboardItem not defined, which
|
// TypeError *probably* suggests ClipboardItem not defined, which
|
||||||
// people on Firefox can enable through a flag, so let's tell them.
|
// people on Firefox can enable through a flag, so let's tell them.
|
||||||
|
|
Loading…
Add table
Reference in a new issue