fix: exportToSvg to honor frameRendering also for name not only for frame itself (#7270)

Co-authored-by: dwelle <5153846+dwelle@users.noreply.github.com>
This commit is contained in:
zsviczian 2023-11-12 23:34:05 +01:00 committed by GitHub
parent ae5b9a4ffd
commit ceb255e8ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -141,6 +141,36 @@ const getFrameRenderingConfig = (
}; };
}; };
const prepareElementsForRender = ({
elements,
exportingFrame,
frameRendering,
exportWithDarkMode,
}: {
elements: readonly ExcalidrawElement[];
exportingFrame: ExcalidrawFrameElement | null | undefined;
frameRendering: AppState["frameRendering"];
exportWithDarkMode: AppState["exportWithDarkMode"];
}) => {
let nextElements: readonly ExcalidrawElement[];
if (exportingFrame) {
nextElements = elementsOverlappingBBox({
elements,
bounds: exportingFrame,
type: "overlap",
});
} else if (frameRendering.enabled && frameRendering.name) {
nextElements = addFrameLabelsAsTextElements(elements, {
exportWithDarkMode,
});
} else {
nextElements = elements;
}
return nextElements;
};
export const exportToCanvas = async ( export const exportToCanvas = async (
elements: readonly NonDeletedExcalidrawElement[], elements: readonly NonDeletedExcalidrawElement[],
appState: AppState, appState: AppState,
@ -169,21 +199,24 @@ export const exportToCanvas = async (
const tempScene = __createSceneForElementsHack__(elements); const tempScene = __createSceneForElementsHack__(elements);
elements = tempScene.getNonDeletedElements(); elements = tempScene.getNonDeletedElements();
let nextElements: ExcalidrawElement[]; const frameRendering = getFrameRenderingConfig(
exportingFrame ?? null,
appState.frameRendering ?? null,
);
const elementsForRender = prepareElementsForRender({
elements,
exportingFrame,
exportWithDarkMode: appState.exportWithDarkMode,
frameRendering,
});
if (exportingFrame) { if (exportingFrame) {
exportPadding = 0; exportPadding = 0;
nextElements = elementsOverlappingBBox({
elements,
bounds: exportingFrame,
type: "overlap",
});
} else {
nextElements = addFrameLabelsAsTextElements(elements, appState);
} }
const [minX, minY, width, height] = getCanvasSize( const [minX, minY, width, height] = getCanvasSize(
exportingFrame ? [exportingFrame] : getRootElements(nextElements), exportingFrame ? [exportingFrame] : getRootElements(elementsForRender),
exportPadding, exportPadding,
); );
@ -193,7 +226,7 @@ export const exportToCanvas = async (
const { imageCache } = await updateImageCache({ const { imageCache } = await updateImageCache({
imageCache: new Map(), imageCache: new Map(),
fileIds: getInitializedImageElements(nextElements).map( fileIds: getInitializedImageElements(elementsForRender).map(
(element) => element.fileId, (element) => element.fileId,
), ),
files, files,
@ -202,15 +235,12 @@ export const exportToCanvas = async (
renderStaticScene({ renderStaticScene({
canvas, canvas,
rc: rough.canvas(canvas), rc: rough.canvas(canvas),
elements: nextElements, elements: elementsForRender,
visibleElements: nextElements, visibleElements: elementsForRender,
scale, scale,
appState: { appState: {
...appState, ...appState,
frameRendering: getFrameRenderingConfig( frameRendering,
exportingFrame ?? null,
appState.frameRendering ?? null,
),
viewBackgroundColor: exportBackground ? viewBackgroundColor : null, viewBackgroundColor: exportBackground ? viewBackgroundColor : null,
scrollX: -minX + exportPadding, scrollX: -minX + exportPadding,
scrollY: -minY + exportPadding, scrollY: -minY + exportPadding,
@ -250,8 +280,14 @@ export const exportToSvg = async (
const tempScene = __createSceneForElementsHack__(elements); const tempScene = __createSceneForElementsHack__(elements);
elements = tempScene.getNonDeletedElements(); elements = tempScene.getNonDeletedElements();
const frameRendering = getFrameRenderingConfig(
opts?.exportingFrame ?? null,
appState.frameRendering ?? null,
);
let { let {
exportPadding = DEFAULT_EXPORT_PADDING, exportPadding = DEFAULT_EXPORT_PADDING,
exportWithDarkMode = false,
viewBackgroundColor, viewBackgroundColor,
exportScale = 1, exportScale = 1,
exportEmbedScene, exportEmbedScene,
@ -259,19 +295,15 @@ export const exportToSvg = async (
const { exportingFrame = null } = opts || {}; const { exportingFrame = null } = opts || {};
let nextElements: ExcalidrawElement[] = []; const elementsForRender = prepareElementsForRender({
elements,
exportingFrame,
exportWithDarkMode,
frameRendering,
});
if (exportingFrame) { if (exportingFrame) {
exportPadding = 0; exportPadding = 0;
nextElements = elementsOverlappingBBox({
elements,
bounds: exportingFrame,
type: "overlap",
});
} else {
nextElements = addFrameLabelsAsTextElements(elements, {
exportWithDarkMode: appState.exportWithDarkMode ?? false,
});
} }
let metadata = ""; let metadata = "";
@ -295,7 +327,7 @@ export const exportToSvg = async (
} }
const [minX, minY, width, height] = getCanvasSize( const [minX, minY, width, height] = getCanvasSize(
exportingFrame ? [exportingFrame] : getRootElements(nextElements), exportingFrame ? [exportingFrame] : getRootElements(elementsForRender),
exportPadding, exportPadding,
); );
@ -306,7 +338,7 @@ export const exportToSvg = async (
svgRoot.setAttribute("viewBox", `0 0 ${width} ${height}`); svgRoot.setAttribute("viewBox", `0 0 ${width} ${height}`);
svgRoot.setAttribute("width", `${width * exportScale}`); svgRoot.setAttribute("width", `${width * exportScale}`);
svgRoot.setAttribute("height", `${height * exportScale}`); svgRoot.setAttribute("height", `${height * exportScale}`);
if (appState.exportWithDarkMode) { if (exportWithDarkMode) {
svgRoot.setAttribute("filter", THEME_FILTER); svgRoot.setAttribute("filter", THEME_FILTER);
} }
@ -381,15 +413,12 @@ export const exportToSvg = async (
} }
const rsvg = rough.svg(svgRoot); const rsvg = rough.svg(svgRoot);
renderSceneToSvg(nextElements, rsvg, svgRoot, files || {}, { renderSceneToSvg(elementsForRender, rsvg, svgRoot, files || {}, {
offsetX, offsetX,
offsetY, offsetY,
exportWithDarkMode: appState.exportWithDarkMode ?? false, exportWithDarkMode,
renderEmbeddables: opts?.renderEmbeddables ?? false, renderEmbeddables: opts?.renderEmbeddables ?? false,
frameRendering: getFrameRenderingConfig( frameRendering,
exportingFrame ?? null,
appState.frameRendering ?? null,
),
}); });
tempScene.destroy(); tempScene.destroy();