Skip to content

Commit

Permalink
export-wip-release
Browse files Browse the repository at this point in the history
  • Loading branch information
dwelle committed Jul 10, 2023
1 parent 0732d68 commit ab4bf2a
Show file tree
Hide file tree
Showing 18 changed files with 618 additions and 289 deletions.
6 changes: 3 additions & 3 deletions src/actions/actionClipboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "../clipboard";
import { actionDeleteSelected } from "./actionDeleteSelected";
import { getSelectedElements } from "../scene/selection";
import { exportCanvas } from "../data/index";
import { exportAsImage } from "../data/index";
import { getNonDeletedElements, isTextElement } from "../element";
import { t } from "../i18n";

Expand Down Expand Up @@ -84,7 +84,7 @@ export const actionCopyAsSvg = register({
},
);
try {
await exportCanvas(
await exportAsImage(
"clipboard-svg",
selectedElements.length
? selectedElements
Expand Down Expand Up @@ -131,7 +131,7 @@ export const actionCopyAsPng = register({
},
);
try {
await exportCanvas(
await exportAsImage(
"clipboard",
selectedElements.length
? selectedElements
Expand Down
5 changes: 3 additions & 2 deletions src/appState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import {
DEFAULT_FONT_FAMILY,
DEFAULT_FONT_SIZE,
DEFAULT_TEXT_ALIGN,
DEFAULT_ZOOM_VALUE,
EXPORT_SCALES,
THEME,
} from "./constants";
import { t } from "./i18n";
import { AppState, NormalizedZoomValue } from "./types";
import { AppState } from "./types";
import { getDateTime } from "./utils";

const defaultExportScale = EXPORT_SCALES.includes(devicePixelRatio)
Expand Down Expand Up @@ -92,7 +93,7 @@ export const getDefaultAppState = (): Omit<
viewBackgroundColor: COLOR_PALETTE.white,
zenModeEnabled: false,
zoom: {
value: 1 as NormalizedZoomValue,
value: DEFAULT_ZOOM_VALUE,
},
viewModeEnabled: false,
pendingImageElementId: null,
Expand Down
8 changes: 4 additions & 4 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ import {
VERTICAL_ALIGN,
ZOOM_STEP,
} from "../constants";
import { exportCanvas, loadFromBlob } from "../data";
import { exportAsImage, loadFromBlob } from "../data";
import Library, { distributeLibraryItemsOnSquareGrid } from "../data/library";
import { restore, restoreElements } from "../data/restore";
import {
Expand Down Expand Up @@ -966,7 +966,7 @@ class App extends React.Component<AppProps, AppState> {
elements: readonly NonDeletedExcalidrawElement[],
) => {
trackEvent("export", type, "ui");
const fileHandle = await exportCanvas(
const fileHandle = await exportAsImage(
type,
elements,
this.state,
Expand Down Expand Up @@ -1788,14 +1788,14 @@ class App extends React.Component<AppProps, AppState> {
{
elements: renderingElements,
appState: this.state,
scale: window.devicePixelRatio,
rc: this.rc!,
canvas: this.canvas!,
renderConfig: {
canvasScale: window.devicePixelRatio,
selectionColor,
scrollX: this.state.scrollX,
scrollY: this.state.scrollY,
viewBackgroundColor: this.state.viewBackgroundColor,
canvasBackgroundColor: this.state.viewBackgroundColor,
zoom: this.state.zoom,
remotePointerViewportCoords: pointerViewportCoords,
remotePointerButton: cursorButton,
Expand Down
19 changes: 14 additions & 5 deletions src/components/ImageExportDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,20 @@ const ImageExportModal = ({
return;
}
exportToCanvas({
elements: exportedElements,
appState,
files,
exportPadding: DEFAULT_EXPORT_PADDING,
maxWidthOrHeight: Math.max(maxWidth, maxHeight),
data: {
elements: exportedElements,
appState,
files,
},
config: {
canvasBackgroundColor: !appState.exportBackground
? false
: appState.viewBackgroundColor,
padding: DEFAULT_EXPORT_PADDING,
theme: appState.exportWithDarkMode ? "dark" : "light",
scale: appState.exportScale,
maxWidthOrHeight: Math.max(maxWidth, maxHeight),
},
})
.then((canvas) => {
setRenderError(null);
Expand Down
1 change: 1 addition & 0 deletions src/components/LayerUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import clsx from "clsx";
import React from "react";
import { ActionManager } from "../actions/manager";
import { CLASSES, DEFAULT_SIDEBAR, LIBRARY_SIDEBAR_WIDTH } from "../constants";
import { exportAsImage } from "../data";
import { isTextElement, showSelectedShapeActions } from "../element";
import { NonDeletedExcalidrawElement } from "../element/types";
import { Language, t } from "../i18n";
Expand Down
24 changes: 15 additions & 9 deletions src/components/PublishLibrary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ const generatePreviewImage = async (libraryItems: LibraryItems) => {
// ---------------------------------------------------------------------------
for (const [index, item] of libraryItems.entries()) {
const itemCanvas = await exportToCanvas({
elements: item.elements,
files: null,
maxWidthOrHeight: BOX_SIZE,
data: {
elements: item.elements,
files: null,
},
config: {
maxWidthOrHeight: BOX_SIZE,
},
});

const { width, height } = itemCanvas;
Expand Down Expand Up @@ -151,13 +155,15 @@ const SingleLibraryItem = ({
}
(async () => {
const svg = await exportToSvg({
elements: libItem.elements,
appState: {
...appState,
viewBackgroundColor: OpenColor.white,
exportBackground: true,
data: {
elements: libItem.elements,
appState: {
...appState,
viewBackgroundColor: OpenColor.white,
exportBackground: true,
},
files: null,
},
files: null,
});
node.innerHTML = svg.outerHTML;
})();
Expand Down
6 changes: 5 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cssVariables from "./css/variables.module.scss";
import { AppProps } from "./types";
import { AppProps, NormalizedZoomValue } from "./types";
import { ExcalidrawElement, FontFamilyValues } from "./element/types";
import { COLOR_PALETTE } from "./colors";

Expand Down Expand Up @@ -76,6 +76,7 @@ export enum EVENT {
export const ENV = {
TEST: "test",
DEVELOPMENT: "development",
PRODUCTION: "production",
};

export const CLASSES = {
Expand Down Expand Up @@ -112,6 +113,9 @@ export const DEFAULT_FONT_FAMILY: FontFamilyValues = FONT_FAMILY.Virgil;
export const DEFAULT_TEXT_ALIGN = "left";
export const DEFAULT_VERTICAL_ALIGN = "top";
export const DEFAULT_VERSION = "{version}";
export const DEFAULT_BACKGROUND_COLOR = "#ffffff";
export const DEFAULT_STROKE_COLOR = "#000000";
export const DEFAULT_ZOOM_VALUE = 1 as NormalizedZoomValue;

export const CANVAS_ONLY_ACTIONS = ["selectAll"];

Expand Down
18 changes: 13 additions & 5 deletions src/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { serializeAsJSON } from "./json";
export { loadFromBlob } from "./blob";
export { loadFromJSON, saveAsJSON } from "./json";

export const exportCanvas = async (
export const exportAsImage = async (
type: Omit<ExportType, "backend">,
elements: readonly NonDeletedExcalidrawElement[],
appState: AppState,
Expand Down Expand Up @@ -66,10 +66,18 @@ export const exportCanvas = async (
}
}

const tempCanvas = await exportToCanvas(elements, appState, files, {
exportBackground,
viewBackgroundColor,
exportPadding,
const tempCanvas = await exportToCanvas({
data: {
elements,
appState,
files,
},
config: {
canvasBackgroundColor: !exportBackground ? false : viewBackgroundColor,
padding: exportPadding,
theme: appState.exportWithDarkMode ? "dark" : "light",
scale: appState.exportScale,
},
});
tempCanvas.style.display = "none";
document.body.appendChild(tempCanvas);
Expand Down
4 changes: 2 additions & 2 deletions src/data/resave.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ExcalidrawElement } from "../element/types";
import { AppState, BinaryFiles } from "../types";
import { exportCanvas } from ".";
import { exportAsImage } from ".";
import { getNonDeletedElements } from "../element";
import { getFileHandleType, isImageFileHandleType } from "./blob";

Expand All @@ -23,7 +23,7 @@ export const resaveAsImageWithScene = async (
exportEmbedScene: true,
};

await exportCanvas(
await exportAsImage(
fileHandleType,
getNonDeletedElements(elements),
appState,
Expand Down
12 changes: 7 additions & 5 deletions src/hooks/useLibraryItemSvg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ export const libraryItemSvgsCache = atom<SvgCache>(new Map());

const exportLibraryItemToSvg = async (elements: LibraryItem["elements"]) => {
return await exportToSvg({
elements,
appState: {
exportBackground: false,
viewBackgroundColor: COLOR_PALETTE.white,
data: {
elements,
appState: {
exportBackground: false,
viewBackgroundColor: COLOR_PALETTE.white,
},
files: null,
},
files: null,
});
};

Expand Down
27 changes: 13 additions & 14 deletions src/index-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,21 @@ const elements = [
registerFont("./public/Virgil.woff2", { family: "Virgil" });
registerFont("./public/Cascadia.woff2", { family: "Cascadia" });

const canvas = exportToCanvas(
elements as any,
{
...getDefaultAppState(),
offsetTop: 0,
offsetLeft: 0,
width: 0,
height: 0,
const canvas = exportToCanvas({
data: {
elements: elements as any,
appState: {
...getDefaultAppState(),
width: 0,
height: 0,
},
files: {}, // files
},
{}, // files
{
exportBackground: true,
viewBackgroundColor: "#ffffff",
config: {
canvasBackgroundColor: "#ffffff",
createCanvas,
},
createCanvas,
);
});

const fs = require("fs");
const out = fs.createWriteStream("test.png");
Expand Down
72 changes: 48 additions & 24 deletions src/packages/excalidraw/example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,12 @@ export default function App({ appTitle, useCustom, customArgs }: AppProps) {
return false;
}
await exportToClipboard({
elements: excalidrawAPI.getSceneElements(),
appState: excalidrawAPI.getAppState(),
files: excalidrawAPI.getFiles(),
type,
data: {
elements: excalidrawAPI.getSceneElements(),
appState: excalidrawAPI.getAppState(),
files: excalidrawAPI.getFiles(),
},
type: "json",
});
window.alert(`Copied to clipboard as ${type} successfully`);
};
Expand Down Expand Up @@ -743,15 +745,17 @@ export default function App({ appTitle, useCustom, customArgs }: AppProps) {
return;
}
const svg = await exportToSvg({
elements: excalidrawAPI?.getSceneElements(),
appState: {
...initialData.appState,
exportWithDarkMode,
exportEmbedScene,
width: 300,
height: 100,
data: {
elements: excalidrawAPI?.getSceneElements(),
appState: {
...initialData.appState,
exportWithDarkMode,
exportEmbedScene,
width: 300,
height: 100,
},
files: excalidrawAPI?.getFiles(),
},
files: excalidrawAPI?.getFiles(),
});
appRef.current.querySelector(".export-svg").innerHTML =
svg.outerHTML;
Expand All @@ -767,14 +771,18 @@ export default function App({ appTitle, useCustom, customArgs }: AppProps) {
return;
}
const blob = await exportToBlob({
elements: excalidrawAPI?.getSceneElements(),
mimeType: "image/png",
appState: {
...initialData.appState,
exportEmbedScene,
exportWithDarkMode,
data: {
elements: excalidrawAPI?.getSceneElements(),
appState: {
...initialData.appState,
exportEmbedScene,
exportWithDarkMode,
},
files: excalidrawAPI?.getFiles(),
},
config: {
mimeType: "image/png",
},
files: excalidrawAPI?.getFiles(),
});
setBlobUrl(window.URL.createObjectURL(blob));
}}
Expand All @@ -789,6 +797,20 @@ export default function App({ appTitle, useCustom, customArgs }: AppProps) {
if (!excalidrawAPI) {
return;
}
const canvas = await exportToCanvas({
data: {
elements: excalidrawAPI.getSceneElements(),
appState: {
...initialData.appState,
exportWithDarkMode,
},
files: excalidrawAPI.getFiles(),
},
});
const ctx = canvas.getContext("2d")!;
ctx.font = "30px Virgil";
ctx.strokeText("My custom text", 50, 60);
setCanvasUrl(canvas.toDataURL());
}}
>
Export to Canvas
Expand All @@ -799,12 +821,14 @@ export default function App({ appTitle, useCustom, customArgs }: AppProps) {
return;
}
const canvas = await exportToCanvas({
elements: excalidrawAPI.getSceneElements(),
appState: {
...initialData.appState,
exportWithDarkMode,
data: {
elements: excalidrawAPI.getSceneElements(),
appState: {
...initialData.appState,
exportWithDarkMode,
},
files: excalidrawAPI.getFiles(),
},
files: excalidrawAPI.getFiles(),
});
const ctx = canvas.getContext("2d")!;
ctx.font = "30px Virgil";
Expand Down
Loading

0 comments on commit ab4bf2a

Please sign in to comment.