@servicetitan/dte-unlayer 0.125.0 → 0.127.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -67,7 +67,7 @@ export const unlayerSupportedFonts = [
67
67
  {
68
68
  label: 'Sofia Pro',
69
69
  value: 'Sofia Pro',
70
- url: 'https://fonts.cdnfonts.com/css/sofia-pro',
70
+ url: 'https://fonts.googleapis.com/css2?family=Sofia+Sans:ital,wght@0,1..1000;1,1..1000&display=swap',
71
71
  },
72
72
  {
73
73
  label: 'Nunito',
package/src/store.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { loadScript } from './loadScript';
2
2
  import { defaultImageValidation } from './shared/configs';
3
3
  import { UnlayerEditorTwin, UnlayerEventConfig, UnlayerEventRegister } from './shared/const';
4
+ import { unlayerSupportedFonts } from './shared/fonts';
4
5
  import { FormFieldInfo, FormInfo } from './shared/forms';
5
6
  import { unlayerToolsParseTwinKey } from './shared/tools';
6
7
  import { unlayerToolsIterate } from './tools';
@@ -14,6 +15,80 @@ import { CreateUnlayerEditorProps, UnlayerDesignFormat, UnlayerRef } from './unl
14
15
 
15
16
  const defaultScriptUrl = 'https://editor.unlayer.com/embed.js?2';
16
17
 
18
+ const normalizeFontToken = (font: string) =>
19
+ font
20
+ .replace(/["']/g, '')
21
+ .split(',')[0]
22
+ .trim()
23
+ .toLowerCase();
24
+
25
+ const getDesignFontTokens = (value: any, out: Set<string>) => {
26
+ if (!value || typeof value !== 'object') {
27
+ return;
28
+ }
29
+
30
+ if (Array.isArray(value)) {
31
+ for (const item of value) {
32
+ getDesignFontTokens(item, out);
33
+ }
34
+
35
+ return;
36
+ }
37
+
38
+ for (const [key, fieldValue] of Object.entries(value)) {
39
+ if (key === 'fontFamily') {
40
+ if (typeof fieldValue === 'string') {
41
+ out.add(normalizeFontToken(fieldValue));
42
+ } else if (fieldValue && typeof fieldValue === 'object') {
43
+ const fontFamilyValue = fieldValue as { label?: string; value?: string };
44
+
45
+ if (typeof fontFamilyValue.label === 'string') {
46
+ out.add(normalizeFontToken(fontFamilyValue.label));
47
+ }
48
+
49
+ if (typeof fontFamilyValue.value === 'string') {
50
+ out.add(normalizeFontToken(fontFamilyValue.value));
51
+ }
52
+ }
53
+ }
54
+
55
+ if (fieldValue && typeof fieldValue === 'object') {
56
+ getDesignFontTokens(fieldValue, out);
57
+ }
58
+ }
59
+ };
60
+
61
+ const ensureChunksFonts = (data: any) => {
62
+ if (!data || typeof data !== 'object') {
63
+ return data;
64
+ }
65
+
66
+ data.chunks ??= {};
67
+
68
+ const existingFonts: any[] = Array.isArray(data.chunks.fonts) ? data.chunks.fonts : [];
69
+ const existingLabels = new Set(existingFonts.map((f: any) => f.label));
70
+
71
+ const usedFontTokens = new Set<string>();
72
+ getDesignFontTokens(data.design, usedFontTokens);
73
+
74
+ const html = typeof data.html === 'string' ? data.html.toLowerCase() : '';
75
+
76
+ for (const font of unlayerSupportedFonts) {
77
+ const labelToken = normalizeFontToken(font.label);
78
+ const valueToken = normalizeFontToken(font.value);
79
+ const isUsedInDesign = usedFontTokens.has(labelToken) || usedFontTokens.has(valueToken);
80
+ const isUsedInHtml = !!html && (html.includes(labelToken) || html.includes(valueToken));
81
+
82
+ if ((isUsedInDesign || isUsedInHtml) && !existingLabels.has(font.label)) {
83
+ existingFonts.push(font);
84
+ existingLabels.add(font.label);
85
+ }
86
+ }
87
+
88
+ data.chunks.fonts = existingFonts;
89
+ return data;
90
+ };
91
+
17
92
  export interface UnlayerDesignChangeInfo {
18
93
  isToolsListChanged: boolean;
19
94
  }
@@ -65,7 +140,7 @@ export class UnlayerStore {
65
140
  },
66
141
  exportHtml: cb => {
67
142
  this.editor?.exportHtml(data => {
68
- cb(data);
143
+ cb(ensureChunksFonts(data));
69
144
  });
70
145
  },
71
146
  sendFormList: forms => {