Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Apply temporary logic to apply missing fonts in the current excalidraw package #56

Merged
merged 7 commits into from
Sep 2, 2024
Prev Previous commit
Next Next commit
fix: Gracefully check textElement's fontFamily in case it's missing
kei95 committed Aug 13, 2024
commit 889a2bdf5ef125ef42c6091aa39e771a7161db72
26 changes: 13 additions & 13 deletions src/useLoadSvg.ts
Original file line number Diff line number Diff line change
@@ -17,10 +17,10 @@ import { loadScene } from "./vendor/loadScene";
import { animateSvg } from "./animate";

export const getNonDeletedElements = (
elements: readonly ExcalidrawElement[]
elements: readonly ExcalidrawElement[],
): NonDeletedExcalidrawElement[] =>
elements.filter(
(element): element is NonDeletedExcalidrawElement => !element.isDeleted
(element): element is NonDeletedExcalidrawElement => !element.isDeleted,
);

const importLibraryFromUrl = async (url: string) => {
@@ -29,7 +29,7 @@ const importLibraryFromUrl = async (url: string) => {
const blob = await request.blob();
const libraryItems = await loadLibraryFromBlob(blob);
return libraryItems.map((libraryItem) =>
getNonDeletedElements(restoreElements(libraryItem.elements, null))
getNonDeletedElements(restoreElements(libraryItem.elements, null)),
);
} catch (error) {
window.alert("Unable to load library");
@@ -53,7 +53,7 @@ export const useLoadSvg = () => {
appState: Parameters<typeof exportToSvg>[0]["appState"];
files: BinaryFiles;
}[],
inSequence?: boolean
inSequence?: boolean,
) => {
const hash = window.location.hash.slice(1);
const searchParams = new URLSearchParams(hash);
@@ -83,20 +83,20 @@ export const useLoadSvg = () => {
options.startMs = result.finishedMs;
}
return { svg, finishedMs: result.finishedMs };
})
}),
);
setLoadedSvgList(svgList);
return svgList;
},
[]
[],
);

useEffect(() => {
(async () => {
const hash = window.location.hash.slice(1);
const searchParams = new URLSearchParams(hash);
const matchIdKey = /([a-zA-Z0-9_-]+),?([a-zA-Z0-9_-]*)/.exec(
searchParams.get("json") || ""
searchParams.get("json") || "",
);
if (matchIdKey) {
const [, id, key] = matchIdKey;
@@ -107,14 +107,14 @@ export const useLoadSvg = () => {
}
}
const matchLibrary = /(.*\.excalidrawlib)/.exec(
searchParams.get("library") || ""
searchParams.get("library") || "",
);
if (matchLibrary) {
const [, url] = matchLibrary;
const dataList = await importLibraryFromUrl(url);
const svgList = await loadDataList(
dataList.map((elements) => ({ elements, appState: {}, files: {} })),
searchParams.has("sequence")
searchParams.has("sequence"),
);
if (searchParams.get("autoplay") === "no") {
svgList.forEach(({ svg, finishedMs }) => {
@@ -156,18 +156,18 @@ export const FONT_FAMILY = {
function applyNewFontsToSvg(svg: SVGSVGElement, elements: ExcalidrawElement[]) {
const textElements: ExcalidrawTextElement[] = elements.filter(
(element): element is ExcalidrawTextElement =>
element.type === "text" && !!element.fontFamily
element.type === "text" && !!element.fontFamily,
) as ExcalidrawTextElement[];

svg.querySelectorAll("text").forEach((textElement, index) => {
const fontFamily = textElements[index].fontFamily;
const fontFamily = textElements[index]?.fontFamily;
convertFontFamily(textElement, fontFamily);
});
}

function convertFontFamily(
textElement: SVGTextElement,
fontFamilyNumber: number
fontFamilyNumber: number | undefined,
) {
switch (fontFamilyNumber) {
case FONT_FAMILY.Virgil:
@@ -201,7 +201,7 @@ function convertFontFamily(
case FONT_FAMILY["Liberation Sans"]:
textElement.setAttribute(
"font-family",
`Liberation Sans, ${DEFAULT_FONT}`
`Liberation Sans, ${DEFAULT_FONT}`,
);
break;