@servicetitan/dte-unlayer 0.33.0 → 0.35.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.
package/src/editor.tsx CHANGED
@@ -7,6 +7,7 @@ import {
7
7
  useRef,
8
8
  useState,
9
9
  } from 'react';
10
+ import { UnlayerEditorTwin } from './shared/const';
10
11
  import { UnlayerStore, UnlayerDesignChangeInfo } from './store';
11
12
  import { UnlayerRef, CreateUnlayerEditorProps } from './unlayer-interface';
12
13
 
@@ -16,9 +17,15 @@ export interface UnlayerEditorProps {
16
17
  opts: CreateUnlayerEditorProps;
17
18
  minHeight?: number;
18
19
  style?: CSSProperties;
20
+
19
21
  onReady?(): void;
22
+
20
23
  onChange?(info: UnlayerDesignChangeInfo): void;
24
+
25
+ onSnapshotSave?(tool: UnlayerEditorTwin): void;
26
+
21
27
  onImage?(file: File): Promise<{ url: string }>;
28
+
22
29
  onMessage?(type: string, data: any): void;
23
30
  }
24
31
 
@@ -58,6 +65,18 @@ export const UnlayerEditor = forwardRef<UnlayerRef, UnlayerEditorProps>((props,
58
65
  return () => store.setOnChange();
59
66
  }, [props.onChange, store]);
60
67
 
68
+ useEffect(() => {
69
+ store.setOnSnapshotSave(props.onSnapshotSave);
70
+
71
+ return () => store.setOnSnapshotSave();
72
+ }, [props.onSnapshotSave, store]);
73
+
74
+ useEffect(() => {
75
+ if (props.opts.newSnapshot) {
76
+ store.addTwin(props.opts.newSnapshot);
77
+ }
78
+ }, [props.opts.newSnapshot, store]);
79
+
61
80
  useEffect(() => {
62
81
  store.setOnReady(props.onReady);
63
82
 
@@ -12,12 +12,12 @@ export const unlayerToolsParseUnitKey = (
12
12
  };
13
13
 
14
14
  export const unlayerToolsGenerateTwinKey = (toolType: string, toolSlug?: string) =>
15
- `:twin:${toolType}:${toolSlug ?? ''}:${Date.now()}${Math.round(Math.random() * 10000)}`;
15
+ `|twin|${toolType}|${toolSlug ?? ''}|${Date.now()}${Math.round(Math.random() * 10000)}`;
16
16
 
17
17
  export const unlayerToolsParseTwinKey = (
18
18
  toolKey: string
19
19
  ): { type: string; slug?: string } | undefined => {
20
- const [, twin, type, slug] = (toolKey ?? '').split(':');
20
+ const [, twin, type, slug] = (toolKey ?? '').split('|');
21
21
 
22
22
  if (twin === 'twin' && type) {
23
23
  return { type, slug: slug || undefined };
package/src/store.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { loadScript } from './loadScript';
2
- import { UnlayerEventConfig, UnlayerEventRegister } from './shared/const';
2
+ import { UnlayerEditorTwin, UnlayerEventConfig, UnlayerEventRegister } from './shared/const';
3
3
  import { unlayerToolsParseTwinKey } from './shared/tools';
4
4
  import { unlayerToolsIterate } from './tools';
5
5
  import {
@@ -43,6 +43,7 @@ export class UnlayerStore {
43
43
 
44
44
  private onMessageCB?: (type: string, data: any) => void;
45
45
  private onChangeCB?: (info: UnlayerDesignChangeInfo) => void;
46
+ private onSnapshotSaveCB?: (snapshot: UnlayerEditorTwin) => void;
46
47
  private onReadyCB?: () => void;
47
48
  private onImageCB?: (file: File) => Promise<{ url: string }>;
48
49
 
@@ -93,6 +94,11 @@ export class UnlayerStore {
93
94
  }
94
95
  };
95
96
 
97
+ addTwin = (twin: UnlayerEditorTwin) => {
98
+ this.props.toolTwins?.push(twin);
99
+ this.sendMessage('--add-twin', JSON.parse(JSON.stringify(twin)));
100
+ };
101
+
96
102
  setDesign = (design?: UnlayerDesignFormat) => {
97
103
  if (!this.editor) {
98
104
  return;
@@ -109,6 +115,10 @@ export class UnlayerStore {
109
115
  this.onChangeCB = onChange;
110
116
  };
111
117
 
118
+ setOnSnapshotSave = (onSnapshotSave?: UnlayerStore['onSnapshotSaveCB']) => {
119
+ this.onSnapshotSaveCB = onSnapshotSave;
120
+ };
121
+
112
122
  setOnReady = (onReady?: UnlayerStore['onReadyCB']) => {
113
123
  this.onReadyCB = onReady;
114
124
  };
@@ -205,6 +215,23 @@ export class UnlayerStore {
205
215
  };
206
216
 
207
217
  this.sendMessage('--register', JSON.parse(JSON.stringify(data)));
218
+ } else if (type === '--save-snapshot') {
219
+ this.editor?.exportHtml(template => {
220
+ unlayerToolsIterate(template.design, tool => {
221
+ if (tool.values?.snapshot?.id === data.id) {
222
+ if (!tool.slug) {
223
+ return;
224
+ }
225
+
226
+ this.onSnapshotSaveCB?.({
227
+ title: '',
228
+ tool: tool.slug,
229
+ values: tool.values,
230
+ });
231
+ return false;
232
+ }
233
+ });
234
+ });
208
235
  }
209
236
 
210
237
  this.onMessageCB?.(type, data);
@@ -53,6 +53,7 @@ export interface UnlayerEditorCustomTool {
53
53
 
54
54
  export interface CreateUnlayerEditorProps {
55
55
  tools: UnlayerEditorCustomTool[];
56
+ newSnapshot?: UnlayerEditorTwin;
56
57
  toolTwins?: UnlayerEditorTwin[];
57
58
  dummyData?: any;
58
59
  schema?: SchemaObject;
package/src/unlayer.tsx CHANGED
@@ -94,6 +94,7 @@ export const createUnlayerEditor = (
94
94
  };
95
95
 
96
96
  const acv = constGenericsEditor.adminConfigProperty;
97
+
97
98
  const unitsConfig = (units ?? []).reduce((out, unit) => {
98
99
  out[`custom#${unit.generic}:unit:${unit.id}`] = {
99
100
  data: {