Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

delete font-face from canvas when font was deleted

Oleg Isonen0a8c3d8f36be
diff --git a/apps/designer/app/canvas/canvas.tsx b/apps/designer/app/canvas/canvas.tsx
index 93e3dfc247..e87056b67f 100644
--- a/apps/designer/app/canvas/canvas.tsx
+++ b/apps/designer/app/canvas/canvas.tsx
@@ -45,6 +45,7 @@ import { useDragAndDrop } from "./shared/use-drag-drop";
import { setInstanceChildrenMutable } from "~/shared/tree-utils";
import { CanvasData } from "~/shared/db";
import { useSubscribeDesignerReady } from "./shared/use-designer-ready";
+import type { Asset } from "@webstudio-is/asset-uploader";
registerContainers();
@@ -80,6 +81,12 @@ const useSubscribePreviewMode = () => {
return isPreviewMode;
};
+const useAssets = (initialAssets: Array<Asset>) => {
+ const [assets, setAssets] = useState(initialAssets);
+ useSubscribe("updateAssets", setAssets);
+ return assets;
+};
+
type DesignModeProps = {
treeId: Tree["id"];
buildId: Build["id"];
@@ -118,7 +125,8 @@ export const Canvas = ({ data }: CanvasProps): JSX.Element | null => {
}
const isDesignerReady = useSubscribeDesignerReady();
useInitializeBreakpoints(data.breakpoints);
- useGlobalStyles({ assets: data.assets });
+ const assets = useAssets(data.assets);
+ useGlobalStyles({ assets });
useAllUserProps(data.props);
usePopulateRootInstance(data.tree);
// e.g. toggling preview is still needed in both modes
diff --git a/apps/designer/app/designer/designer.tsx b/apps/designer/app/designer/designer.tsx
index e69252688c..bc2dd92f88 100644
--- a/apps/designer/app/designer/designer.tsx
+++ b/apps/designer/app/designer/designer.tsx
@@ -11,7 +11,6 @@ import interStyles from "~/shared/font-faces/inter.css";
import { SidebarLeft } from "./features/sidebar-left";
import { Inspector } from "./features/inspector";
import {
- useAssets,
useHoveredInstanceData,
usePages,
useProject,
@@ -41,8 +40,8 @@ import {
import { useClientSettings } from "./shared/client-settings";
import { Navigator } from "./features/sidebar-left";
import { PANEL_WIDTH } from "./shared/constants";
-import { Asset } from "@webstudio-is/asset-uploader";
import env from "~/shared/env";
+import { useSetAssets } from "./shared/assets";
export const links = () => {
return [
@@ -71,15 +70,6 @@ const useSubscribeSyncStatus = () => {
useSubscribe("syncStatus", setValue);
};
-const useSetAssets = (assets?: Array<Asset>) => {
- const [, setAssets] = useAssets();
- useEffect(() => {
- if (assets) {
- setAssets(assets);
- }
- }, [assets, setAssets]);
-};
-
const useSetProject = (project: Project) => {
const [, setProject] = useProject();
useEffect(() => {
@@ -275,11 +265,11 @@ export const Designer = ({
useSubscribeSelectedInstanceData();
useSubscribeHoveredInstanceData();
useSubscribeBreakpoints();
- useSetAssets(project.assets);
useSetProject(project);
useSetPages(pages);
useSetCurrentPageId(pageId);
const [publish, publishRef] = usePublish();
+ useSetAssets({ assets: project.assets, publish });
const [isPreviewMode] = useIsPreviewMode();
usePublishShortcuts(publish);
const onRefReadCanvasWidth = useUpdateCanvasWidth();
diff --git a/apps/designer/app/designer/shared/assets/index.ts b/apps/designer/app/designer/shared/assets/index.ts
index ae2a760c85..79e7479e04 100644
--- a/apps/designer/app/designer/shared/assets/index.ts
+++ b/apps/designer/app/designer/shared/assets/index.ts
@@ -1,3 +1,4 @@
export * from "./asset-upload";
export * from "./use-assets";
export * from "./types";
+export * from "./use-set-assets";
diff --git a/apps/designer/app/designer/shared/assets/use-set-assets.ts b/apps/designer/app/designer/shared/assets/use-set-assets.ts
new file mode 100644
index 0000000000..23ae061c94
--- /dev/null
+++ b/apps/designer/app/designer/shared/assets/use-set-assets.ts
@@ -0,0 +1,25 @@
+import type { Asset } from "@webstudio-is/asset-uploader";
+import { useEffect } from "react";
+import { Publish } from "~/shared/pubsub";
+import { useAssets } from "../nano-states";
+
+declare module "~/shared/pubsub" {
+ export interface PubsubMap {
+ updateAssets: Array<Asset>;
+ }
+}
+
+type UseSetAssets = {
+ assets?: Array<Asset>;
+ publish: Publish;
+};
+
+export const useSetAssets = ({ assets, publish }: UseSetAssets) => {
+ const [, setAssets] = useAssets();
+ useEffect(() => {
+ if (assets) {
+ setAssets(assets);
+ publish({ type: "updateAssets", payload: assets });
+ }
+ }, [assets, setAssets, publish]);
+};
diff --git a/packages/design-system/src/components/list.stories.tsx b/packages/design-system/src/components/list.stories.tsx
index 4798d8e129..bcbac6e82b 100644
--- a/packages/design-system/src/components/list.stories.tsx
+++ b/packages/design-system/src/components/list.stories.tsx
@@ -46,6 +46,7 @@ export const WithHook: ComponentStory<typeof List> = () => {
return (
<ListItem
{...itemProps}
+ key={index}
prefix={itemProps.current ? <CheckIcon /> : undefined}
>
{item}
diff --git a/packages/fonts/src/get-font-faces.ts b/packages/fonts/src/get-font-faces.ts
index f1c3c81040..02be31f7ff 100644
--- a/packages/fonts/src/get-font-faces.ts
+++ b/packages/fonts/src/get-font-faces.ts
@@ -8,7 +8,17 @@ export type PartialFontAsset = {
path: string;
};
-export const getFontFaces = (assets: Array<PartialFontAsset>) => {
+export type FontFace = {
+ fontFamily: string;
+ fontStyle: FontMeta["style"];
+ fontWeight: number;
+ fontDisplay: "swap" | "auto" | "block" | "fallback" | "optional";
+ src: string;
+};
+
+export const getFontFaces = (
+ assets: Array<PartialFontAsset>
+): Array<FontFace> => {
const faces = new Map();
for (const asset of assets) {
const face = faces.get(asset.meta.family);
@@ -19,7 +29,7 @@ export const getFontFaces = (assets: Array<PartialFontAsset>) => {
fontFamily: asset.meta.family,
fontStyle: asset.meta.style,
fontWeight: asset.meta.weight,
- fontDisplay: "swap" as const,
+ fontDisplay: "swap",
src: `url('${asset.path}') format('${format}')`,
});
continue;
diff --git a/packages/react-sdk/src/stitches/stitches.ts b/packages/react-sdk/src/stitches/stitches.ts
index 67662b3eb3..11d16995fa 100644
--- a/packages/react-sdk/src/stitches/stitches.ts
+++ b/packages/react-sdk/src/stitches/stitches.ts
@@ -1,9 +1,10 @@
import {
+ type CSS,
createStitches,
globalCss as globalCssImport,
- type CSS,
css as createCss,
} from "@stitches/core";
+import type { FontFace } from "@webstudio-is/fonts";
import type { Breakpoint } from "../css";
let media = {};
@@ -12,6 +13,8 @@ let media = {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let stitches: any;
+let fontsCss = "";
+
export { type CSS };
const getCachedConfig = () => {
@@ -31,8 +34,9 @@ export const globalCss: typeof globalCssImport = (...args) => {
export const flushCss = () => {
const config = getCachedConfig();
- const css = config.getCssText();
+ const css = config.getCssText() + fontsCss;
config.reset();
+ fontsCss = "";
return css;
};
@@ -44,3 +48,35 @@ export const setBreakpoints = (breakpoints: Array<Breakpoint>) => {
media = nextMedia;
stitches = undefined;
};
+
+let styleElement: HTMLStyleElement | undefined;
+
+/**
+ * Stitches doesn't update fonts over globalCss
+ */
+export const renderFontFaces = (fontFaces: Array<FontFace>) => {
+ fontsCss = fontFaces
+ .map(
+ (fontFace) =>
+ `
+@font-face {
+ font-family: ${fontFace.fontFamily};
+ font-style: ${fontFace.fontStyle};
+ font-weight: ${fontFace.fontWeight};
+ font-display: ${fontFace.fontDisplay};
+ src: ${fontFace.src};
+}`
+ )
+ .join("");
+
+ if (
+ typeof document !== "undefined" &&
+ typeof document.createElement === "function"
+ ) {
+ if (styleElement === undefined) {
+ styleElement = document.createElement("style");
+ document.head.appendChild(styleElement);
+ }
+ styleElement.textContent = fontsCss;
+ }
+};
diff --git a/packages/react-sdk/src/tree/root.ts b/packages/react-sdk/src/tree/root.ts
index 74f82849d2..6106b4b404 100644
--- a/packages/react-sdk/src/tree/root.ts
+++ b/packages/react-sdk/src/tree/root.ts
@@ -1,7 +1,7 @@
import { useAllUserProps } from "../user-props/";
import type { Tree, InstanceProps } from "../db";
import type { Breakpoint } from "../css";
-import { globalCss, setBreakpoints } from "../stitches";
+import { globalCss, setBreakpoints, renderFontFaces } from "../stitches";
import { createElementsTree } from "./create-elements-tree";
import {
WrapperComponent,
@@ -33,8 +33,8 @@ export const useGlobalStyles = ({ assets }: { assets: Array<Asset> }) => {
FONT_FORMATS.has(asset.format as FontFormat)
) as Array<FontAsset>;
+ renderFontFaces(getFontFaces(fontAssets));
globalCss({
- "@font-face": getFontFaces(fontAssets),
html: {
height: "100%",
},