feat: introduce font picker (#8012)

Co-authored-by: dwelle <5153846+dwelle@users.noreply.github.com>
This commit is contained in:
Marcel Mraz 2024-07-25 18:55:55 +02:00 committed by GitHub
parent 4c5408263c
commit 62228e0bbb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
120 changed files with 3390 additions and 1106 deletions

View file

@ -1,6 +1,7 @@
// vitest.setup.ts
import "vitest-canvas-mock";
import "@testing-library/jest-dom";
import fs from "fs";
import { vi } from "vitest";
import polyfill from "./packages/excalidraw/polyfill";
import { testPolyfills } from "./packages/excalidraw/tests/helpers/polyfills";
@ -25,6 +26,65 @@ Object.defineProperty(window, "matchMedia", {
})),
});
Object.defineProperty(window, "FontFace", {
enumerable: true,
value: class {
private family: string;
private source: string;
private descriptors: any;
private status: string;
constructor(family, source, descriptors) {
this.family = family;
this.source = source;
this.descriptors = descriptors;
this.status = "unloaded";
}
load() {
this.status = "loaded";
}
},
});
Object.defineProperty(document, "fonts", {
value: {
load: vi.fn().mockResolvedValue([]),
check: vi.fn().mockResolvedValue(true),
has: vi.fn().mockResolvedValue(true),
add: vi.fn(),
},
});
Object.defineProperty(window, "EXCALIDRAW_ASSET_PATH", {
value: `file://${__dirname}/`,
});
vi.mock(
"./packages/excalidraw/fonts/ExcalidrawFont",
async (importOriginal) => {
const mod = await importOriginal<
typeof import("./packages/excalidraw/fonts/ExcalidrawFont")
>();
const ExcalidrawFontImpl = mod.ExcalidrawFont;
return {
...mod,
ExcalidrawFont: class extends ExcalidrawFontImpl {
public async getContent(): Promise<string> {
if (this.url.protocol !== "file:") {
return super.getContent();
}
// read local assets directly, without running a server
const content = await fs.promises.readFile(this.url);
return `data:font/woff2;base64,${content.toString("base64")}`;
}
},
};
},
);
vi.mock("nanoid", () => {
return {
nanoid: vi.fn(() => "test-id"),