feat: constrain export dialog preview size (#6475)
This commit is contained in:
parent
801412bf6b
commit
4d0d844e39
@ -4,7 +4,6 @@ import { canvasToBlob } from "../data/blob";
|
|||||||
import { NonDeletedExcalidrawElement } from "../element/types";
|
import { NonDeletedExcalidrawElement } from "../element/types";
|
||||||
import { t } from "../i18n";
|
import { t } from "../i18n";
|
||||||
import { getSelectedElements, isSomeElementSelected } from "../scene";
|
import { getSelectedElements, isSomeElementSelected } from "../scene";
|
||||||
import { exportToCanvas } from "../scene/export";
|
|
||||||
import { AppState, BinaryFiles } from "../types";
|
import { AppState, BinaryFiles } from "../types";
|
||||||
import { Dialog } from "./Dialog";
|
import { Dialog } from "./Dialog";
|
||||||
import { clipboard } from "./icons";
|
import { clipboard } from "./icons";
|
||||||
@ -15,6 +14,7 @@ import { CheckboxItem } from "./CheckboxItem";
|
|||||||
import { DEFAULT_EXPORT_PADDING, isFirefox } from "../constants";
|
import { DEFAULT_EXPORT_PADDING, isFirefox } from "../constants";
|
||||||
import { nativeFileSystemSupported } from "../data/filesystem";
|
import { nativeFileSystemSupported } from "../data/filesystem";
|
||||||
import { ActionManager } from "../actions/manager";
|
import { ActionManager } from "../actions/manager";
|
||||||
|
import { exportToCanvas } from "../packages/utils";
|
||||||
|
|
||||||
const supportsContextFilters =
|
const supportsContextFilters =
|
||||||
"filter" in document.createElement("canvas").getContext("2d")!;
|
"filter" in document.createElement("canvas").getContext("2d")!;
|
||||||
@ -83,7 +83,6 @@ const ImageExportModal = ({
|
|||||||
const someElementIsSelected = isSomeElementSelected(elements, appState);
|
const someElementIsSelected = isSomeElementSelected(elements, appState);
|
||||||
const [exportSelected, setExportSelected] = useState(someElementIsSelected);
|
const [exportSelected, setExportSelected] = useState(someElementIsSelected);
|
||||||
const previewRef = useRef<HTMLDivElement>(null);
|
const previewRef = useRef<HTMLDivElement>(null);
|
||||||
const { exportBackground, viewBackgroundColor } = appState;
|
|
||||||
const [renderError, setRenderError] = useState<Error | null>(null);
|
const [renderError, setRenderError] = useState<Error | null>(null);
|
||||||
|
|
||||||
const exportedElements = exportSelected
|
const exportedElements = exportSelected
|
||||||
@ -99,10 +98,16 @@ const ImageExportModal = ({
|
|||||||
if (!previewNode) {
|
if (!previewNode) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
exportToCanvas(exportedElements, appState, files, {
|
const maxWidth = previewNode.offsetWidth;
|
||||||
exportBackground,
|
if (!maxWidth) {
|
||||||
viewBackgroundColor,
|
return;
|
||||||
|
}
|
||||||
|
exportToCanvas({
|
||||||
|
elements: exportedElements,
|
||||||
|
appState,
|
||||||
|
files,
|
||||||
exportPadding,
|
exportPadding,
|
||||||
|
maxWidthOrHeight: maxWidth,
|
||||||
})
|
})
|
||||||
.then((canvas) => {
|
.then((canvas) => {
|
||||||
setRenderError(null);
|
setRenderError(null);
|
||||||
@ -116,14 +121,7 @@ const ImageExportModal = ({
|
|||||||
console.error(error);
|
console.error(error);
|
||||||
setRenderError(error);
|
setRenderError(error);
|
||||||
});
|
});
|
||||||
}, [
|
}, [appState, files, exportedElements, exportPadding]);
|
||||||
appState,
|
|
||||||
files,
|
|
||||||
exportedElements,
|
|
||||||
exportBackground,
|
|
||||||
exportPadding,
|
|
||||||
viewBackgroundColor,
|
|
||||||
]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="ExportDialog">
|
<div className="ExportDialog">
|
||||||
|
@ -79,7 +79,11 @@ export const exportToCanvas = ({
|
|||||||
|
|
||||||
const max = Math.max(width, height);
|
const max = Math.max(width, height);
|
||||||
|
|
||||||
const scale = maxWidthOrHeight / max;
|
// if content is less then maxWidthOrHeight, fallback on supplied scale
|
||||||
|
const scale =
|
||||||
|
maxWidthOrHeight < max
|
||||||
|
? maxWidthOrHeight / max
|
||||||
|
: appState?.exportScale ?? 1;
|
||||||
|
|
||||||
canvas.width = width * scale;
|
canvas.width = width * scale;
|
||||||
canvas.height = height * scale;
|
canvas.height = height * scale;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user