Skip to content

Commit 194b1d2

Browse files
authored
fix: fonts config file (#2960)
1 parent 0b234ca commit 194b1d2

File tree

5 files changed

+52
-14
lines changed

5 files changed

+52
-14
lines changed

apps/web/client/src/app/project/[id]/_components/editor-bar/hooks/use-text-control.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ export const useTextControl = () => {
7272

7373
const handleFontFamilyChange = (fontFamily: Font) => {
7474
editorEngine.style.updateFontFamily('fontFamily', fontFamily);
75-
editorEngine.frames.reloadAllViews();
75+
// Reload all views after a delay to ensure the font is applied
76+
setTimeout(async () => {
77+
await editorEngine.frames.reloadAllViews();
78+
}, 500);
7679
};
7780

7881
const handleFontSizeChange = (fontSize: number) => {

apps/web/client/src/app/project/[id]/_components/left-panel/brand-tab/font-panel/font-family.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import {
77
DropdownMenuTrigger,
88
} from '@onlook/ui/dropdown-menu';
99
import { Icons } from '@onlook/ui/icons';
10-
import { Tooltip, TooltipContent, TooltipPortal, TooltipTrigger } from '@onlook/ui/tooltip';
1110
import { cn } from '@onlook/ui/utils';
12-
import { TooltipArrow } from '@radix-ui/react-tooltip';
1311
import { camelCase } from 'lodash';
1412
import { useState } from 'react';
1513

apps/web/client/src/components/store/editor/ast/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ We can do that when parsing through the AST using this algorithm:
4848
Applying the algorithm to our case we will get this:
4949
1. Parse through the files, getting this map:
5050

51-
```
51+
```json
5252
{
5353
"parent": ["parent.jsx", "start and end location", "Parent"],
5454
"instance": ["parent.jsx", "start and end location", "Child"],

apps/web/client/src/components/store/editor/font/font-config-manager.ts

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,9 @@ export const readFontConfigFile = async (fontConfigPath: string, editorEngine: E
188188
| undefined
189189
> => {
190190
const codeEditor = editorEngine.fileSystem;
191-
const fileExists = await codeEditor.fileExists(fontConfigPath);
192-
if (!fileExists) {
193-
console.warn('Font config file does not exist', fontConfigPath);
194-
return;
195-
}
191+
192+
// Ensure the font config file exists, create it if it doesn't
193+
await ensureFontConfigFileExists(fontConfigPath, editorEngine);
196194

197195
const file = await codeEditor.readFile(fontConfigPath);
198196
if (!file || typeof file !== 'string') {
@@ -213,14 +211,49 @@ export const readFontConfigFile = async (fontConfigPath: string, editorEngine: E
213211
};
214212
}
215213

214+
/**
215+
* Creates a default font configuration file template
216+
*/
217+
const createDefaultFontConfigTemplate = (): string => {
218+
return `// This file contains font configurations for your application.
219+
// Fonts added through Onlook will be automatically exported from this file.
220+
//
221+
// Example Google Font:
222+
// import { Inter } from 'next/font/google';
223+
//
224+
// export const inter = Inter({
225+
// subsets: ['latin'],
226+
// weight: ['400', '700'],
227+
// style: ['normal'],
228+
// variable: '--font-inter',
229+
// display: 'swap'
230+
// });
231+
//
232+
// Example Local Font:
233+
// import localFont from 'next/font/local';
234+
//
235+
// export const customFont = localFont({
236+
// src: [
237+
// { path: './fonts/custom-regular.woff2', weight: '400', style: 'normal' },
238+
// { path: './fonts/custom-bold.woff2', weight: '700', style: 'normal' }
239+
// ],
240+
// variable: '--font-custom',
241+
// display: 'swap',
242+
// fallback: ['system-ui', 'sans-serif'],
243+
// preload: true
244+
// });
245+
`;
246+
}
247+
216248
/**
217249
* Ensures the font configuration file exists
218250
*/
219251
export const ensureFontConfigFileExists = async (fontConfigPath: string, editorEngine: EditorEngine): Promise<void> => {
220252
const codeEditor = editorEngine.fileSystem;
221253
const fontConfigExists = await codeEditor.fileExists(fontConfigPath);
222254
if (!fontConfigExists) {
223-
await codeEditor.writeFile(fontConfigPath, '');
255+
const template = createDefaultFontConfigTemplate();
256+
await codeEditor.writeFile(fontConfigPath, template);
224257
}
225258
}
226259

apps/web/client/src/components/store/editor/font/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,15 @@ export class FontManager {
161161
this._defaultFont = font.id;
162162
const codeDiff = await updateDefaultFontInRootLayout(font, this.editorEngine);
163163

164-
if (codeDiff) {
165-
await this.editorEngine.fileSystem.writeFile(codeDiff.path, codeDiff.generated);
166-
return true;
164+
if (!codeDiff) {
165+
return false;
167166
}
168-
return false;
167+
await this.editorEngine.fileSystem.writeFile(codeDiff.path, codeDiff.generated);
168+
// Reload all views after a delay to ensure the font is applied
169+
setTimeout(async () => {
170+
await this.editorEngine.frames.reloadAllViews();
171+
}, 500);
172+
return true;
169173
} catch (error) {
170174
console.error('Error setting default font:', error);
171175
return false;

0 commit comments

Comments
 (0)