@servicetitan/dte-unlayer 0.30.0 → 0.31.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.
@@ -6,25 +6,32 @@ export const constGenericsEditor = {
6
6
  userConfigProperty: 'config',
7
7
  };
8
8
 
9
- export interface IUnlayerEditorUnit {
9
+ export interface UnlayerEditorUnit {
10
10
  id: string;
11
11
  generic: string;
12
12
  title: string;
13
13
  values: any;
14
14
  }
15
15
 
16
+ export interface UnlayerEditorTwin {
17
+ tool: string;
18
+ title: string;
19
+ values: any;
20
+ }
21
+
16
22
  export interface UnlayerEventConfig {
17
23
  dummyData?: any;
18
24
  schema?: SchemaObject;
19
25
  generics?: true | string[];
20
26
  genericConfigMode?: boolean;
21
- units?: IUnlayerEditorUnit[];
27
+ units?: UnlayerEditorUnit[];
22
28
  }
23
29
 
24
30
  export interface UnlayerEventRegister {
25
- units?: IUnlayerEditorUnit[];
31
+ units?: UnlayerEditorUnit[];
26
32
  customTools?: {
27
33
  key: string;
28
34
  title?: string;
29
35
  }[];
36
+ toolTwins?: UnlayerEditorTwin[];
30
37
  }
@@ -1,64 +1,8 @@
1
1
  export const unlayerSupportedFonts = [
2
- {
3
- label: 'Andale Mono',
4
- value: 'andale mono,times',
5
- },
6
2
  {
7
3
  label: 'Arial',
8
4
  value: 'arial,helvetica,sans-serif',
9
5
  },
10
- {
11
- label: 'Arial Black',
12
- value: 'arial black,avant garde,arial',
13
- },
14
- {
15
- label: 'Book Antiqua',
16
- value: 'book antiqua,palatino',
17
- },
18
- {
19
- label: 'Comic Sans MS',
20
- value: 'comic sans ms,sans-serif',
21
- },
22
- {
23
- label: 'Courier New',
24
- value: 'courier new,courier',
25
- },
26
- {
27
- label: 'Georgia',
28
- value: 'georgia,palatino',
29
- },
30
- {
31
- label: 'Helvetica',
32
- value: 'helvetica,sans-serif',
33
- },
34
- {
35
- label: 'Impact',
36
- value: 'impact,chicago',
37
- },
38
- {
39
- label: 'Symbol',
40
- value: 'symbol',
41
- },
42
- {
43
- label: 'Tahoma',
44
- value: 'tahoma,arial,helvetica,sans-serif',
45
- },
46
- {
47
- label: 'Terminal',
48
- value: 'terminal,monaco',
49
- },
50
- {
51
- label: 'Times New Roman',
52
- value: 'times new roman,times',
53
- },
54
- {
55
- label: 'Trebuchet MS',
56
- value: 'trebuchet ms,geneva',
57
- },
58
- {
59
- label: 'Verdana',
60
- value: 'verdana,geneva',
61
- },
62
6
  {
63
7
  label: 'Lobster Two',
64
8
  value: "'Lobster Two',cursive",
@@ -1,7 +1,7 @@
1
- export const unlayerUnitsGenerateToolKey = (unitId: string, unitGeneric: string) =>
1
+ export const unlayerToolsGenerateUnitKey = (unitId: string, unitGeneric: string) =>
2
2
  `${unitGeneric}:unit:${unitId}`;
3
3
 
4
- export const unlayerUnitsParseToolKey = (
4
+ export const unlayerToolsParseUnitKey = (
5
5
  toolKey: string
6
6
  ): { id: string; generic: string } | undefined => {
7
7
  const [generic, type, id] = (toolKey ?? '').split(':');
@@ -11,6 +11,19 @@ export const unlayerUnitsParseToolKey = (
11
11
  }
12
12
  };
13
13
 
14
+ export const unlayerToolsGenerateTwinKey = (toolType: string, toolSlug?: string) =>
15
+ `:twin:${toolType}:${toolSlug ?? ''}:${Date.now()}${Math.round(Math.random() * 10000)}`;
16
+
17
+ export const unlayerToolsParseTwinKey = (
18
+ toolKey: string
19
+ ): { type: string; slug?: string } | undefined => {
20
+ const [, twin, type, slug] = (toolKey ?? '').split(':');
21
+
22
+ if (twin === 'twin' && type) {
23
+ return { type, slug: slug || undefined };
24
+ }
25
+ };
26
+
14
27
  export interface UnlayerStore {
15
28
  renderHtml(html: string, data: any): string;
16
29
  sendData(type: string, data?: any): void;
package/src/store.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { loadScript } from './loadScript';
2
2
  import { UnlayerEventConfig, UnlayerEventRegister } from './shared/const';
3
+ import { unlayerToolsParseTwinKey } from './shared/tools';
4
+ import { unlayerToolsIterate } from './tools';
3
5
  import {
4
6
  createUnlayerEditor,
5
7
  DesignUpdatedEventType,
@@ -12,9 +14,9 @@ const defaultScriptUrl = 'https://editor.unlayer.com/embed.js?2';
12
14
 
13
15
  export interface UnlayerDesignChangeInfo {
14
16
  isToolsListChanged: boolean;
15
- event: any;
16
17
  }
17
18
 
19
+ // We are using exclusive lists here, because we can't be sure that we know all unlayer event types
18
20
  const eventsNotChangingToolsList: DesignUpdatedEventType[] = [
19
21
  DesignUpdatedEventType.ContentMoved,
20
22
  DesignUpdatedEventType.ContentModified,
@@ -24,6 +26,13 @@ const eventsNotChangingToolsList: DesignUpdatedEventType[] = [
24
26
  DesignUpdatedEventType.BodyModified,
25
27
  ];
26
28
 
29
+ const eventsNotAddingTools: DesignUpdatedEventType[] = [
30
+ ...eventsNotChangingToolsList,
31
+ DesignUpdatedEventType.ContentRemoved,
32
+ DesignUpdatedEventType.ColumnRemoved,
33
+ DesignUpdatedEventType.RowRemoved,
34
+ ];
35
+
27
36
  export class UnlayerStore {
28
37
  readonly unlayerRef: UnlayerRef;
29
38
 
@@ -123,10 +132,40 @@ export class UnlayerStore {
123
132
  };
124
133
 
125
134
  private onDesignUpdated = (event: EventDesignUpdated) => {
126
- this.onChangeCB?.({
127
- isToolsListChanged: !eventsNotChangingToolsList.includes(event.type),
128
- event,
129
- });
135
+ const isToolAdded = !eventsNotAddingTools.includes(event.type);
136
+ const isToolsListChanged = !eventsNotChangingToolsList.includes(event.type);
137
+
138
+ if (isToolAdded) {
139
+ this.editor?.exportHtml(data => {
140
+ let hasChanges = false;
141
+
142
+ unlayerToolsIterate(data.design, tool => {
143
+ const realTool = tool.slug
144
+ ? unlayerToolsParseTwinKey(tool.slug ?? '')
145
+ : undefined;
146
+
147
+ if (realTool) {
148
+ hasChanges = true;
149
+
150
+ if (tool.slug) {
151
+ delete data.design.counters[`u_content_custom_${tool.slug}`];
152
+ delete tool.values._meta;
153
+ }
154
+
155
+ tool.type = realTool.type;
156
+ tool.slug = realTool.slug;
157
+ }
158
+ });
159
+
160
+ if (hasChanges) {
161
+ this.setDesign(data.design);
162
+ }
163
+
164
+ this.onChangeCB?.({ isToolsListChanged });
165
+ });
166
+ } else {
167
+ this.onChangeCB?.({ isToolsListChanged });
168
+ }
130
169
  };
131
170
 
132
171
  private onPostMessage = (e: MessageEvent) => {
@@ -155,6 +194,7 @@ export class UnlayerStore {
155
194
  } else if (type === '--data-loaded') {
156
195
  const data: UnlayerEventRegister = {
157
196
  customTools: this.props.tools.map(tool => ({ key: tool.key })),
197
+ toolTwins: this.props.toolTwins,
158
198
  units: this.props.units?.map(unit => ({
159
199
  ...unit,
160
200
  values: {
package/src/tools.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { constGenericsEditor } from './shared/const';
2
- import { unlayerUnitsParseToolKey } from './shared/tools';
2
+ import { unlayerToolsParseUnitKey } from './shared/tools';
3
3
  import { versions } from './unlayer';
4
4
  import {
5
+ UnlayerDesignCustomTool,
5
6
  UnlayerDesignFormat,
6
7
  UnlayerDesignTool,
7
8
  UnlayerEditorCustomTool,
@@ -109,11 +110,11 @@ export const unlayerToolsIterate = (
109
110
 
110
111
  export const unlayerToolsIterateCustom = (
111
112
  design: UnlayerDesignFormat,
112
- cb: (tool: UnlayerDesignTool) => boolean | undefined | void
113
+ cb: (tool: UnlayerDesignCustomTool) => boolean | undefined | void
113
114
  ) => {
114
115
  unlayerToolsIterate(design, tool => {
115
- if (tool.type === 'custom') {
116
- return cb(tool);
116
+ if (tool.type === 'custom' && tool.slug) {
117
+ return cb(tool as any);
117
118
  }
118
119
  });
119
120
  };
@@ -160,7 +161,7 @@ export const unlayerToolsCustomStat = (
160
161
 
161
162
  counters[tool.slug]++;
162
163
 
163
- const isUnit = unlayerUnitsParseToolKey(tool.slug);
164
+ const isUnit = unlayerToolsParseUnitKey(tool.slug);
164
165
 
165
166
  if (isUnit) {
166
167
  unitIDs.add(isUnit.id);
@@ -189,8 +190,8 @@ export const unlayerToolsCustomStat = (
189
190
  };
190
191
  };
191
192
 
192
- export const unlayerToolsListCustom = (design: UnlayerDesignFormat): UnlayerDesignTool[] => {
193
- const tools: UnlayerDesignTool[] = [];
193
+ export const unlayerToolsListCustom = (design: UnlayerDesignFormat): UnlayerDesignCustomTool[] => {
194
+ const tools: UnlayerDesignCustomTool[] = [];
194
195
 
195
196
  unlayerToolsIterateCustom(design, tool => {
196
197
  tools.push(tool);
@@ -1,10 +1,14 @@
1
+ import { UnlayerEditorTwin, UnlayerEditorUnit } from './shared/const';
1
2
  import { SchemaObject } from './shared/schema';
2
3
 
3
4
  export interface UnlayerDesignTool {
4
5
  type: string;
5
- slug: string;
6
+ slug?: string;
6
7
  values: any;
7
8
  }
9
+ export interface UnlayerDesignCustomTool extends UnlayerDesignTool {
10
+ slug: string;
11
+ }
8
12
 
9
13
  export interface UnlayerDesignFormat {
10
14
  body: {
@@ -40,13 +44,6 @@ export interface UnlayerEditorMergeTagInfo {
40
44
  propertyPath: string;
41
45
  }
42
46
 
43
- export interface UnlayerEditorUnit {
44
- id: string;
45
- generic: string;
46
- title: string;
47
- values: any;
48
- }
49
-
50
47
  export interface UnlayerEditorCustomTool {
51
48
  key: string;
52
49
  title: string;
@@ -56,6 +53,7 @@ export interface UnlayerEditorCustomTool {
56
53
 
57
54
  export interface CreateUnlayerEditorProps {
58
55
  tools: UnlayerEditorCustomTool[];
56
+ toolTwins?: UnlayerEditorTwin[];
59
57
  dummyData?: any;
60
58
  schema?: SchemaObject;
61
59
  customCSS?: string | string[] | undefined;