Browse Source

fix: use excalidraw asset path in fonts when exporting to svg (#5065)

* fix: use excalidraw asset path in fonts when exporting

* fix

* fix

* introduce env variables and determine asset path correctly

* fix snaps

* use env vars to determine pkg name and version

* update docs

* quotes
Aakansha Doshi 3 years ago
parent
commit
da94eb1284

+ 1 - 0
src/packages/excalidraw/CHANGELOG.md

@@ -23,6 +23,7 @@ Please add the latest change on the top under the correct section.
 
 
 #### Fixes
 #### Fixes
 
 
+- Use `window.EXCALIDRAW_ASSET_PATH` for fonts when exporting to svg [#5065](https://github.com/excalidraw/excalidraw/pull/5065).
 - Library menu now properly rerenders if open when library is updated using `updateScene({ libraryItems })` [#4995](https://github.com/excalidraw/excalidraw/pull/4995).
 - Library menu now properly rerenders if open when library is updated using `updateScene({ libraryItems })` [#4995](https://github.com/excalidraw/excalidraw/pull/4995).
 
 
 #### Refactor
 #### Refactor

+ 6 - 2
src/packages/excalidraw/env.js

@@ -1,14 +1,18 @@
 const dotenv = require("dotenv");
 const dotenv = require("dotenv");
 const { readFileSync } = require("fs");
 const { readFileSync } = require("fs");
-
+const pkg = require("./package.json");
 const parseEnvVariables = (filepath) => {
 const parseEnvVariables = (filepath) => {
-  return Object.entries(dotenv.parse(readFileSync(filepath))).reduce(
+  const envVars = Object.entries(dotenv.parse(readFileSync(filepath))).reduce(
     (env, [key, value]) => {
     (env, [key, value]) => {
       env[key] = JSON.stringify(value);
       env[key] = JSON.stringify(value);
       return env;
       return env;
     },
     },
     {},
     {},
   );
   );
+  envVars.PKG_NAME = JSON.stringify(pkg.name);
+  envVars.PKG_VERSION = JSON.stringify(pkg.version);
+  envVars.IS_EXCALIDRAW_NPM_PACKAGE = JSON.stringify(true);
+  return envVars;
 };
 };
 
 
 module.exports = { parseEnvVariables };
 module.exports = { parseEnvVariables };

+ 1 - 2
src/packages/excalidraw/publicPath.js

@@ -1,9 +1,8 @@
 import { ENV } from "../../constants";
 import { ENV } from "../../constants";
-import pkg from "./package.json";
 if (process.env.NODE_ENV !== ENV.TEST) {
 if (process.env.NODE_ENV !== ENV.TEST) {
   /* eslint-disable */
   /* eslint-disable */
   /* global __webpack_public_path__:writable */
   /* global __webpack_public_path__:writable */
   __webpack_public_path__ =
   __webpack_public_path__ =
     window.EXCALIDRAW_ASSET_PATH ||
     window.EXCALIDRAW_ASSET_PATH ||
-    `https://unpkg.com/${pkg.name}@${pkg.version}/dist/`;
+    `https://unpkg.com/${process.env.PKG_NAME}@${process.env.PKG_VERSION}/dist/`;
 }
 }

+ 15 - 3
src/scene/export.ts

@@ -115,6 +115,19 @@ export const exportToSvg = async (
     svgRoot.setAttribute("filter", THEME_FILTER);
     svgRoot.setAttribute("filter", THEME_FILTER);
   }
   }
 
 
+  let assetPath = "https://excalidraw.com/";
+
+  // Asset path needs to be determined only when using package
+  if (process.env.IS_EXCALIDRAW_NPM_PACKAGE) {
+    assetPath =
+      window.EXCALIDRAW_ASSET_PATH ||
+      `https://unpkg.com/${process.env.PKG_NAME}@${process.env.PKG_VERSION}`;
+
+    if (assetPath?.startsWith("/")) {
+      assetPath = assetPath.replace("/", `${window.location.origin}/`);
+    }
+    assetPath = `${assetPath}/dist/excalidraw-assets/`;
+  }
   svgRoot.innerHTML = `
   svgRoot.innerHTML = `
   ${SVG_EXPORT_TAG}
   ${SVG_EXPORT_TAG}
   ${metadata}
   ${metadata}
@@ -122,16 +135,15 @@ export const exportToSvg = async (
     <style>
     <style>
       @font-face {
       @font-face {
         font-family: "Virgil";
         font-family: "Virgil";
-        src: url("https://excalidraw.com/Virgil.woff2");
+        src: url("${assetPath}Virgil.woff2");
       }
       }
       @font-face {
       @font-face {
         font-family: "Cascadia";
         font-family: "Cascadia";
-        src: url("https://excalidraw.com/Cascadia.woff2");
+        src: url("${assetPath}Cascadia.woff2");
       }
       }
     </style>
     </style>
   </defs>
   </defs>
   `;
   `;
-
   // render background rect
   // render background rect
   if (appState.exportBackground && viewBackgroundColor) {
   if (appState.exportBackground && viewBackgroundColor) {
     const rect = svgRoot.ownerDocument!.createElementNS(SVG_NS, "rect");
     const rect = svgRoot.ownerDocument!.createElementNS(SVG_NS, "rect");

+ 1 - 0
src/tests/scene/export.test.ts

@@ -7,6 +7,7 @@ import {
 } from "../fixtures/elementFixture";
 } from "../fixtures/elementFixture";
 
 
 describe("exportToSvg", () => {
 describe("exportToSvg", () => {
+  window.EXCALIDRAW_ASSET_PATH = "/";
   const ELEMENT_HEIGHT = 100;
   const ELEMENT_HEIGHT = 100;
   const ELEMENT_WIDTH = 100;
   const ELEMENT_WIDTH = 100;
   const ELEMENTS = [
   const ELEMENTS = [

+ 3 - 0
src/utils.ts

@@ -575,6 +575,9 @@ export const arrayToMap = <T extends { id: string } | string>(
 export const isTestEnv = () =>
 export const isTestEnv = () =>
   typeof process !== "undefined" && process.env?.NODE_ENV === "test";
   typeof process !== "undefined" && process.env?.NODE_ENV === "test";
 
 
+export const isProdEnv = () =>
+  typeof process !== "undefined" && process.env?.NODE_ENV === "production";
+
 export const wrapEvent = <T extends Event>(name: EVENT, nativeEvent: T) => {
 export const wrapEvent = <T extends Event>(name: EVENT, nativeEvent: T) => {
   return new CustomEvent(name, {
   return new CustomEvent(name, {
     detail: {
     detail: {