From 904b1becd10ab9aba5db1c350b45e2574ea4b89b Mon Sep 17 00:00:00 2001 From: Cagatay Civici Date: Sat, 28 Dec 2024 16:35:57 +0300 Subject: [PATCH] Support for custom font size and family --- .../components/layout/AppDesigner.vue | 37 +++++++++++- .../designer/dashboard/DesignDashboard.vue | 2 + .../layout/designer/editor/DesignEditor.vue | 4 ++ .../editor/settings/DesignSettings.vue | 56 +++++++++++++++++++ 4 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 apps/showcase/components/layout/designer/editor/settings/DesignSettings.vue diff --git a/apps/showcase/components/layout/AppDesigner.vue b/apps/showcase/components/layout/AppDesigner.vue index e3e6cf3273..713089662f 100644 --- a/apps/showcase/components/layout/AppDesigner.vue +++ b/apps/showcase/components/layout/AppDesigner.vue @@ -48,7 +48,8 @@ export default { refreshACTokens: this.refreshACTokens, saveTheme: this.saveTheme, downloadTheme: this.downloadTheme, - applyTheme: this.applyTheme + applyTheme: this.applyTheme, + applyFont: this.applyFont } }; }, @@ -91,7 +92,7 @@ export default { } } }, - async updateTheme(theme) { + async saveTheme(theme) { const { error } = await $fetch(this.designerApiBase + '/theme/update', { method: 'POST', body: { @@ -108,7 +109,7 @@ export default { }, applyTheme(theme) { if (this.$appState.designer.licenseKey) { - this.updateTheme(theme); + this.saveTheme(theme); } updatePreset(theme.preset); @@ -145,6 +146,36 @@ export default { }, openDashboard() { this.$appState.designer.activeView = 'dashboard'; + }, + applyFont(fontFamily) { + if (fontFamily !== 'Inter var') { + this.loadFont(fontFamily, 400); + this.loadFont(fontFamily, 500); + this.loadFont(fontFamily, 600); + this.loadFont(fontFamily, 700); + } else { + document.body.style.fontFamily = `"Inter var", sans-serif`; + } + }, + async loadFont(fontFamily, weight) { + const fontFamilyPath = fontFamily.toLowerCase().replace(/\s+/g, '-'); + const fontUrl = `https://fonts.bunny.net/${fontFamilyPath}/files/${fontFamilyPath}-latin-${weight}-normal.woff2`; + + const font = new FontFace(fontFamily, `url(${fontUrl})`, { + weight: weight.toString(), + style: 'normal' + }); + + try { + const loadedFont = await font.load(); + + document.fonts.add(loadedFont); + document.body.style.fontFamily = `"${fontFamily}", sans-serif`; + + return loadedFont; + } catch (error) { + // silent fail as some fonts may have not all the font weights + } } }, computed: { diff --git a/apps/showcase/components/layout/designer/dashboard/DesignDashboard.vue b/apps/showcase/components/layout/designer/dashboard/DesignDashboard.vue index 55f0ebd1d9..b922708120 100644 --- a/apps/showcase/components/layout/designer/dashboard/DesignDashboard.vue +++ b/apps/showcase/components/layout/designer/dashboard/DesignDashboard.vue @@ -177,6 +177,8 @@ export default { }; usePreset(this.$appState.designer.theme.preset); + this.designerService.applyFont(this.$appState.designer.theme.config.fontFamily); + document.documentElement.style.fontSize = this.$appState.designer.theme.config.baseFontSize; this.designerService.refreshACTokens(); this.$appState.designer.activeView = 'editor'; } diff --git a/apps/showcase/components/layout/designer/editor/DesignEditor.vue b/apps/showcase/components/layout/designer/editor/DesignEditor.vue index f01341e1a1..c7a85503ce 100644 --- a/apps/showcase/components/layout/designer/editor/DesignEditor.vue +++ b/apps/showcase/components/layout/designer/editor/DesignEditor.vue @@ -5,6 +5,7 @@ Semantic Component Custom + Settings @@ -65,6 +66,9 @@ + + + diff --git a/apps/showcase/components/layout/designer/editor/settings/DesignSettings.vue b/apps/showcase/components/layout/designer/editor/settings/DesignSettings.vue new file mode 100644 index 0000000000..e8489f5876 --- /dev/null +++ b/apps/showcase/components/layout/designer/editor/settings/DesignSettings.vue @@ -0,0 +1,56 @@ + + +