@servicetitan/dte-unlayer 0.22.0 → 0.25.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.
@@ -0,0 +1,94 @@
1
+ import { loadScript } from './loadScript';
2
+ import { UnlayerStore } from './shared/tools';
3
+ import { UnlayerDesignTool } from './unlayer-interface';
4
+
5
+ const dummyStore: UnlayerStore = {
6
+ renderHtml() {
7
+ return '';
8
+ },
9
+ sendData() {},
10
+ getTool() {
11
+ return undefined;
12
+ },
13
+ };
14
+
15
+ export class UnlayerCoreApi {
16
+ customCss = '';
17
+ private loaded = new Set<string>();
18
+
19
+ private core = ['core.bundle.js'];
20
+ private coreTools = ['tools.bundle.js'];
21
+
22
+ constructor(private baseUrl: string) {}
23
+
24
+ private get dteStore(): UnlayerStore {
25
+ return (window as any).dteStore ?? dummyStore;
26
+ }
27
+
28
+ get pathsCore() {
29
+ return this.core.map(c => `${this.baseUrl}/${c}`);
30
+ }
31
+
32
+ get pathsCoreTools() {
33
+ return this.coreTools.map(c => `${this.baseUrl}/${c}`);
34
+ }
35
+
36
+ async init() {
37
+ return Promise.all([
38
+ fetch(`${this.baseUrl}/custom.css`).then(response => response.text()),
39
+ ]).then(([customCSS]) => {
40
+ this.customCss = customCSS;
41
+ });
42
+ }
43
+
44
+ clean() {
45
+ this.customCss = '';
46
+ this.loaded = new Set();
47
+ }
48
+
49
+ async preloadCore() {
50
+ if (this.loaded.has('_core')) {
51
+ return;
52
+ }
53
+ this.loaded.add('_core');
54
+
55
+ await Promise.all(this.core.map(c => loadScript(`${this.baseUrl}/${c}`)));
56
+ }
57
+
58
+ async preloadCoreTools() {
59
+ if (this.loaded.has('_coreTools')) {
60
+ return;
61
+ }
62
+ this.loaded.add('_coreTools');
63
+
64
+ await Promise.all(this.coreTools.map(c => loadScript(`${this.baseUrl}/${c}`)));
65
+ }
66
+
67
+ getCustomToolModel(key: string): object {
68
+ const dteTool = this.dteStore.getTool(key);
69
+
70
+ if (!dteTool) {
71
+ return {};
72
+ }
73
+
74
+ try {
75
+ return dteTool.getModel() ?? {};
76
+ } catch {
77
+ return {};
78
+ }
79
+ }
80
+
81
+ getCustomToolDataSources(tool: UnlayerDesignTool): string[] {
82
+ const dteTool = this.dteStore.getTool(tool.slug);
83
+
84
+ if (!dteTool) {
85
+ return [];
86
+ }
87
+
88
+ try {
89
+ return dteTool.dataSources(tool.values) ?? [];
90
+ } catch {
91
+ return [];
92
+ }
93
+ }
94
+ }
@@ -0,0 +1,79 @@
1
+ import { loadScript } from './loadScript';
2
+ import { UnlayerEditorCustomTool } from './unlayer-interface';
3
+
4
+ export interface UnlayerCustomToolsPackageData {
5
+ packageUrl: string;
6
+ customTools: UnlayerEditorCustomTool[];
7
+ }
8
+
9
+ export class UnlayerCustomToolsApi {
10
+ private dataInternal?: UnlayerCustomToolsPackageData;
11
+ private loaded = new Set<string>();
12
+ private schemas?: Record<string, any>;
13
+
14
+ constructor(private packageUrl: string) {}
15
+
16
+ get data() {
17
+ return this.dataInternal!;
18
+ }
19
+
20
+ async init() {
21
+ await fetch(`${this.packageUrl}/data.json`)
22
+ .then(response =>
23
+ response
24
+ .json()
25
+ .then(data => ({ ...data, packageUrl: response.url.replace('/data.json', '') }))
26
+ )
27
+ .then(({ packageUrl, tools }) => ({
28
+ customTools: tools.map((tool: any) => ({
29
+ key: tool.key,
30
+ title: tool.title,
31
+ path: `${packageUrl}/${tool.path}`,
32
+ })),
33
+ packageUrl,
34
+ }))
35
+ .then(data => {
36
+ this.dataInternal = data;
37
+ });
38
+ }
39
+
40
+ clean() {
41
+ this.dataInternal = undefined;
42
+ this.loaded = new Set();
43
+ this.schemas = undefined;
44
+ }
45
+
46
+ async loadSchemas(): Promise<Record<string, any>> {
47
+ if (this.schemas) {
48
+ return this.schemas;
49
+ }
50
+
51
+ return fetch(`${this.data.packageUrl}/schemas.json`)
52
+ .then(response => response.json())
53
+ .then(schemas => {
54
+ this.schemas = schemas;
55
+
56
+ return schemas;
57
+ });
58
+ }
59
+
60
+ async preloadCustomTools(tools: string[]) {
61
+ if (!this.dataInternal) {
62
+ return;
63
+ }
64
+ const toolsMap = this.data.customTools.reduce((out, t) => {
65
+ out[t.key] = t.path;
66
+
67
+ return out;
68
+ }, {} as Record<string, string>);
69
+
70
+ await Promise.all(
71
+ tools
72
+ .filter(t => !!toolsMap[t] && !this.loaded.has(t))
73
+ .map(t => {
74
+ this.loaded.add(t);
75
+ return loadScript(toolsMap[t]).catch(() => null);
76
+ })
77
+ );
78
+ }
79
+ }
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './editor';
2
2
  export * from './tools';
3
- export * from './tools-api';
3
+ export * from './api-core';
4
+ export * from './api-custom-tools';
4
5
  export * from './unlayer-interface';
5
6
  export * from './loadScript';
6
7
  export * from './shared/const';
@@ -1,5 +1,30 @@
1
+ import { SchemaObject } from './schema';
2
+
1
3
  export const constGenericsEditor = {
2
4
  infoDataProperty: '_info',
3
5
  adminConfigProperty: 'adminConfig',
4
6
  userConfigProperty: 'config',
5
7
  };
8
+
9
+ export interface IUnlayerEditorUnit {
10
+ id: string;
11
+ generic: string;
12
+ title: string;
13
+ values: any;
14
+ }
15
+
16
+ export interface UnlayerEventConfig {
17
+ dummyData?: any;
18
+ schema?: SchemaObject;
19
+ generics?: true | string[];
20
+ genericConfigMode?: boolean;
21
+ units?: IUnlayerEditorUnit[];
22
+ }
23
+
24
+ export interface UnlayerEventRegister {
25
+ units?: IUnlayerEditorUnit[];
26
+ customTools?: {
27
+ key: string;
28
+ title?: string;
29
+ }[];
30
+ }
package/src/store.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { loadScript } from './loadScript';
2
+ import { UnlayerEventConfig, UnlayerEventRegister } from './shared/const';
2
3
  import {
3
4
  createUnlayerEditor,
4
5
  DesignUpdatedEventType,
@@ -143,35 +144,27 @@ export class UnlayerStore {
143
144
  if (type === '--ready' || type === '--registered') {
144
145
  this.onReadyCB?.();
145
146
  } else if (type === '--core-loaded') {
146
- this.sendMessage(
147
- '--config',
148
- JSON.parse(
149
- JSON.stringify({
150
- dummyData: this.props.dummyData,
151
- schema: this.props.schema,
152
- generics: this.props.generics,
153
- genericConfigMode: this.props.genericConfigMode,
154
- })
155
- )
156
- );
147
+ const data: UnlayerEventConfig = {
148
+ dummyData: this.props.dummyData,
149
+ schema: this.props.schema,
150
+ generics: this.props.generics,
151
+ genericConfigMode: this.props.genericConfigMode,
152
+ };
153
+
154
+ this.sendMessage('--config', JSON.parse(JSON.stringify(data)));
157
155
  } else if (type === '--data-loaded') {
158
- this.sendMessage(
159
- '--register',
160
- JSON.parse(
161
- JSON.stringify({
162
- customTools: this.props.data.customTools.filter(t =>
163
- this.props.tools.includes(t.key)
164
- ),
165
- units: this.props.units?.map(unit => ({
166
- ...unit,
167
- values: {
168
- ...unit.values,
169
- adminConfig: undefined,
170
- },
171
- })),
172
- })
173
- )
174
- );
156
+ const data: UnlayerEventRegister = {
157
+ customTools: this.props.tools.map(tool => ({ key: tool.key })),
158
+ units: this.props.units?.map(unit => ({
159
+ ...unit,
160
+ values: {
161
+ ...unit.values,
162
+ adminConfig: undefined,
163
+ },
164
+ })),
165
+ };
166
+
167
+ this.sendMessage('--register', JSON.parse(JSON.stringify(data)));
175
168
  }
176
169
 
177
170
  this.onMessageCB?.(type, data);
@@ -1,3 +1,4 @@
1
+ import { UnlayerCoreApi } from './api-core';
1
2
  import { SchemaObject } from './shared/schema';
2
3
 
3
4
  export interface UnlayerDesignTool {
@@ -53,18 +54,8 @@ export interface UnlayerEditorCustomTool {
53
54
  path: string;
54
55
  }
55
56
 
56
- export interface UnlayerPackageData {
57
- customTools: UnlayerEditorCustomTool[];
58
- packageUrl: string;
59
- customCSS: string;
60
- core: string[];
61
- coreTools: string[];
62
- tag: string;
63
- }
64
-
65
57
  export interface CreateUnlayerEditorProps {
66
- data: UnlayerPackageData;
67
- tools: string[];
58
+ tools: UnlayerEditorCustomTool[];
68
59
  dummyData?: any;
69
60
  schema?: SchemaObject;
70
61
  customCSS?: string | string[] | undefined;
@@ -76,4 +67,5 @@ export interface CreateUnlayerEditorProps {
76
67
  noCoreTools?: boolean;
77
68
  latest?: boolean;
78
69
  blocks?: boolean;
70
+ coreApi: UnlayerCoreApi;
79
71
  }
package/src/unlayer.tsx CHANGED
@@ -62,9 +62,9 @@ export const versions = {
62
62
  export const createUnlayerEditor = (
63
63
  container: HTMLDivElement,
64
64
  {
65
+ coreApi,
65
66
  customCSS,
66
67
  customJS,
67
- data,
68
68
  latest,
69
69
  mergeTags,
70
70
  noCoreTools,
@@ -72,26 +72,18 @@ export const createUnlayerEditor = (
72
72
  units,
73
73
  }: CreateUnlayerEditorProps
74
74
  ): Unlayer => {
75
- const enabledTools = data.customTools.filter(t => tools.includes(t.key));
76
75
  const customScripts = [
77
- ...data.core,
78
- ...data.coreTools,
76
+ ...coreApi.pathsCore,
77
+ ...coreApi.pathsCoreTools,
79
78
  'window.dteStore.sendData("--core-loaded")',
80
- ...enabledTools.map(tool => `/${tool.path}`),
79
+ ...tools.map(tool => tool.path),
81
80
  ...(customJS ? (Array.isArray(customJS) ? customJS : [customJS]) : []),
82
- '/anvilfonts.bundle.js',
83
81
  'window.dteStore.sendData("--data-loaded")',
84
- ]
85
- .map(url => (url.startsWith('/') ? `${data.packageUrl}${url}` : url))
86
- .filter(js => !!js?.trim());
82
+ ].filter(js => !!js?.trim());
87
83
  const customStyles = [
88
- ...(data.customCSS ? [data.customCSS] : []),
84
+ coreApi.customCss,
89
85
  ...(customCSS ? (Array.isArray(customCSS) ? customCSS : [customCSS]) : []),
90
- ]
91
- .map(url =>
92
- url.startsWith('/') && !url.startsWith('/*') ? `${data.packageUrl}${url}` : url
93
- )
94
- .filter(css => !!css?.trim());
86
+ ].filter(css => !!css?.trim());
95
87
 
96
88
  /**
97
89
  * Unlayer supports only passing id of container, but when we use shadowDOM (MFE),
@@ -1,20 +0,0 @@
1
- import { UnlayerDesignTool, UnlayerPackageData } from './unlayer-interface';
2
- export declare class UnlayerToolsApi {
3
- private dataInternal?;
4
- private loaded;
5
- private schemas?;
6
- private get dteStore();
7
- get data(): UnlayerPackageData;
8
- init(opts?: {
9
- url?: string;
10
- tag?: string;
11
- }): Promise<void>;
12
- clean(): void;
13
- loadSchemas(): Promise<Record<string, any>>;
14
- preloadCore(): Promise<void>;
15
- preloadCoreTools(): Promise<void>;
16
- preloadCustomTools(tools: string[]): Promise<void>;
17
- getCustomToolModel(key: string): object;
18
- getCustomToolDataSources(tool: UnlayerDesignTool): string[];
19
- }
20
- //# sourceMappingURL=tools-api.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"tools-api.d.ts","sourceRoot":"","sources":["../src/tools-api.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAe5E,qBAAa,eAAe;IACxB,OAAO,CAAC,YAAY,CAAC,CAAqB;IAC1C,OAAO,CAAC,MAAM,CAAqB;IACnC,OAAO,CAAC,OAAO,CAAC,CAAsB;IAEtC,OAAO,KAAK,QAAQ,GAEnB;IAED,IAAI,IAAI,uBAEP;IAEK,IAAI,CAAC,IAAI,CAAC,EAAE;QAAE,GAAG,CAAC,EAAE,MAAM,CAAC;QAAC,GAAG,CAAC,EAAE,MAAM,CAAA;KAAE;IA2BhD,KAAK;IAMC,WAAW,IAAI,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAc3C,WAAW;IASX,gBAAgB;IAShB,kBAAkB,CAAC,KAAK,EAAE,MAAM,EAAE;IAsBxC,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM;IAcvC,wBAAwB,CAAC,IAAI,EAAE,iBAAiB,GAAG,MAAM,EAAE;CAa9D"}
package/dist/tools-api.js DELETED
@@ -1,156 +0,0 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- import { loadScript } from './loadScript';
11
- const dummyStore = {
12
- renderHtml() {
13
- return '';
14
- },
15
- sendData() { },
16
- getTool() {
17
- return undefined;
18
- },
19
- };
20
- const baseName = 'https://unpkg.servicetitan.com/@servicetitan/dte-tools';
21
- const baseTag = 'prod2';
22
- export class UnlayerToolsApi {
23
- constructor() {
24
- Object.defineProperty(this, "dataInternal", {
25
- enumerable: true,
26
- configurable: true,
27
- writable: true,
28
- value: void 0
29
- });
30
- Object.defineProperty(this, "loaded", {
31
- enumerable: true,
32
- configurable: true,
33
- writable: true,
34
- value: new Set()
35
- });
36
- Object.defineProperty(this, "schemas", {
37
- enumerable: true,
38
- configurable: true,
39
- writable: true,
40
- value: void 0
41
- });
42
- }
43
- get dteStore() {
44
- var _a;
45
- return (_a = window.dteStore) !== null && _a !== void 0 ? _a : dummyStore;
46
- }
47
- get data() {
48
- return this.dataInternal;
49
- }
50
- init(opts) {
51
- var _a, _b;
52
- return __awaiter(this, void 0, void 0, function* () {
53
- const url = (_a = opts === null || opts === void 0 ? void 0 : opts.url) !== null && _a !== void 0 ? _a : baseName;
54
- const tag = (_b = opts === null || opts === void 0 ? void 0 : opts.tag) !== null && _b !== void 0 ? _b : baseTag;
55
- const initialUrl = `${url}${tag ? '@' + tag : ''}`;
56
- return Promise.all([
57
- fetch(`${initialUrl}/data.json`).then(response => response
58
- .json()
59
- .then(data => (Object.assign(Object.assign({}, data), { packageUrl: response.url.replace('/data.json', '') })))),
60
- fetch(`${initialUrl}/custom.css`).then(response => response.text()),
61
- ])
62
- .then(([{ core, coreTools, packageUrl, tools }, customCSS]) => ({
63
- customTools: tools,
64
- packageUrl,
65
- customCSS,
66
- core: core !== null && core !== void 0 ? core : ['/core.bundle.js'],
67
- coreTools: coreTools !== null && coreTools !== void 0 ? coreTools : [],
68
- tag,
69
- }))
70
- .then(data => {
71
- this.dataInternal = data;
72
- });
73
- });
74
- }
75
- clean() {
76
- this.dataInternal = undefined;
77
- this.loaded = new Set();
78
- this.schemas = undefined;
79
- }
80
- loadSchemas() {
81
- return __awaiter(this, void 0, void 0, function* () {
82
- if (this.schemas) {
83
- return this.schemas;
84
- }
85
- return fetch(`${this.data.packageUrl}/schemas.json`)
86
- .then(response => response.json())
87
- .then(schemas => {
88
- this.schemas = schemas;
89
- return schemas;
90
- });
91
- });
92
- }
93
- preloadCore() {
94
- return __awaiter(this, void 0, void 0, function* () {
95
- if (this.loaded.has('_core') || !this.dataInternal) {
96
- return;
97
- }
98
- this.loaded.add('_core');
99
- yield Promise.all(this.data.core.map(c => loadScript(`${this.data.packageUrl}${c}`)));
100
- });
101
- }
102
- preloadCoreTools() {
103
- return __awaiter(this, void 0, void 0, function* () {
104
- if (this.loaded.has('_coreTools') || !this.dataInternal) {
105
- return;
106
- }
107
- this.loaded.add('_coreTools');
108
- yield Promise.all(this.data.coreTools.map(c => loadScript(`${this.data.packageUrl}${c}`)));
109
- });
110
- }
111
- preloadCustomTools(tools) {
112
- return __awaiter(this, void 0, void 0, function* () {
113
- if (!this.dataInternal) {
114
- return;
115
- }
116
- const toolsMap = this.data.customTools.reduce((out, t) => {
117
- out[t.key] = t.path;
118
- return out;
119
- }, {});
120
- yield this.preloadCore();
121
- yield Promise.all(tools
122
- .filter(t => !!toolsMap[t] && !this.loaded.has(t))
123
- .map(t => {
124
- this.loaded.add(t);
125
- return loadScript(this.data.packageUrl + '/' + toolsMap[t]).catch(() => null);
126
- }));
127
- });
128
- }
129
- getCustomToolModel(key) {
130
- var _a;
131
- const dteTool = this.dteStore.getTool(key);
132
- if (!dteTool) {
133
- return {};
134
- }
135
- try {
136
- return (_a = dteTool.getModel()) !== null && _a !== void 0 ? _a : {};
137
- }
138
- catch (_b) {
139
- return {};
140
- }
141
- }
142
- getCustomToolDataSources(tool) {
143
- var _a;
144
- const dteTool = this.dteStore.getTool(tool.slug);
145
- if (!dteTool) {
146
- return [];
147
- }
148
- try {
149
- return (_a = dteTool.dataSources(tool.values)) !== null && _a !== void 0 ? _a : [];
150
- }
151
- catch (_b) {
152
- return [];
153
- }
154
- }
155
- }
156
- //# sourceMappingURL=tools-api.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"tools-api.js","sourceRoot":"","sources":["../src/tools-api.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAI1C,MAAM,UAAU,GAAiB;IAC7B,UAAU;QACN,OAAO,EAAE,CAAC;IACd,CAAC;IACD,QAAQ,KAAI,CAAC;IACb,OAAO;QACH,OAAO,SAAS,CAAC;IACrB,CAAC;CACJ,CAAC;AAEF,MAAM,QAAQ,GAAG,wDAAwD,CAAC;AAC1E,MAAM,OAAO,GAAG,OAAO,CAAC;AAExB,MAAM,OAAO,eAAe;IAA5B;QACI;;;;;WAA0C;QAC1C;;;;mBAAiB,IAAI,GAAG,EAAU;WAAC;QACnC;;;;;WAAsC;IA4H1C,CAAC;IA1HG,IAAY,QAAQ;;QAChB,OAAO,MAAC,MAAc,CAAC,QAAQ,mCAAI,UAAU,CAAC;IAClD,CAAC;IAED,IAAI,IAAI;QACJ,OAAO,IAAI,CAAC,YAAa,CAAC;IAC9B,CAAC;IAEK,IAAI,CAAC,IAAqC;;;YAC5C,MAAM,GAAG,GAAG,MAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,GAAG,mCAAI,QAAQ,CAAC;YAClC,MAAM,GAAG,GAAG,MAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,GAAG,mCAAI,OAAO,CAAC;YAEjC,MAAM,UAAU,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC,CA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W,CAAC,IAAI,CAAC,MAAM,CAAC,mCAAI,EAAE,CAAC;SACjD;QAAC,WAAM;YACJ,OAAO,EAAE,CAAC;SACb;IACL,CAAC;CACJ"}
package/src/tools-api.ts DELETED
@@ -1,145 +0,0 @@
1
- import { loadScript } from './loadScript';
2
- import { UnlayerStore } from './shared/tools';
3
- import { UnlayerDesignTool, UnlayerPackageData } from './unlayer-interface';
4
-
5
- const dummyStore: UnlayerStore = {
6
- renderHtml() {
7
- return '';
8
- },
9
- sendData() {},
10
- getTool() {
11
- return undefined;
12
- },
13
- };
14
-
15
- const baseName = 'https://unpkg.servicetitan.com/@servicetitan/dte-tools';
16
- const baseTag = 'prod2';
17
-
18
- export class UnlayerToolsApi {
19
- private dataInternal?: UnlayerPackageData;
20
- private loaded = new Set<string>();
21
- private schemas?: Record<string, any>;
22
-
23
- private get dteStore(): UnlayerStore {
24
- return (window as any).dteStore ?? dummyStore;
25
- }
26
-
27
- get data() {
28
- return this.dataInternal!;
29
- }
30
-
31
- async init(opts?: { url?: string; tag?: string }) {
32
- const url = opts?.url ?? baseName;
33
- const tag = opts?.tag ?? baseTag;
34
-
35
- const initialUrl = `${url}${tag ? '@' + tag : ''}`;
36
-
37
- return Promise.all([
38
- fetch(`${initialUrl}/data.json`).then(response =>
39
- response
40
- .json()
41
- .then(data => ({ ...data, packageUrl: response.url.replace('/data.json', '') }))
42
- ),
43
- fetch(`${initialUrl}/custom.css`).then(response => response.text()),
44
- ])
45
- .then(([{ core, coreTools, packageUrl, tools }, customCSS]) => ({
46
- customTools: tools,
47
- packageUrl,
48
- customCSS,
49
- core: core ?? ['/core.bundle.js'],
50
- coreTools: coreTools ?? [],
51
- tag,
52
- }))
53
- .then(data => {
54
- this.dataInternal = data;
55
- });
56
- }
57
-
58
- clean() {
59
- this.dataInternal = undefined;
60
- this.loaded = new Set();
61
- this.schemas = undefined;
62
- }
63
-
64
- async loadSchemas(): Promise<Record<string, any>> {
65
- if (this.schemas) {
66
- return this.schemas;
67
- }
68
-
69
- return fetch(`${this.data.packageUrl}/schemas.json`)
70
- .then(response => response.json())
71
- .then(schemas => {
72
- this.schemas = schemas;
73
-
74
- return schemas;
75
- });
76
- }
77
-
78
- async preloadCore() {
79
- if (this.loaded.has('_core') || !this.dataInternal) {
80
- return;
81
- }
82
- this.loaded.add('_core');
83
-
84
- await Promise.all(this.data.core.map(c => loadScript(`${this.data.packageUrl}${c}`)));
85
- }
86
-
87
- async preloadCoreTools() {
88
- if (this.loaded.has('_coreTools') || !this.dataInternal) {
89
- return;
90
- }
91
- this.loaded.add('_coreTools');
92
-
93
- await Promise.all(this.data.coreTools.map(c => loadScript(`${this.data.packageUrl}${c}`)));
94
- }
95
-
96
- async preloadCustomTools(tools: string[]) {
97
- if (!this.dataInternal) {
98
- return;
99
- }
100
- const toolsMap = this.data.customTools.reduce((out, t) => {
101
- out[t.key] = t.path;
102
-
103
- return out;
104
- }, {} as Record<string, string>);
105
-
106
- await this.preloadCore();
107
-
108
- await Promise.all(
109
- tools
110
- .filter(t => !!toolsMap[t] && !this.loaded.has(t))
111
- .map(t => {
112
- this.loaded.add(t);
113
- return loadScript(this.data.packageUrl + '/' + toolsMap[t]).catch(() => null);
114
- })
115
- );
116
- }
117
-
118
- getCustomToolModel(key: string): object {
119
- const dteTool = this.dteStore.getTool(key);
120
-
121
- if (!dteTool) {
122
- return {};
123
- }
124
-
125
- try {
126
- return dteTool.getModel() ?? {};
127
- } catch {
128
- return {};
129
- }
130
- }
131
-
132
- getCustomToolDataSources(tool: UnlayerDesignTool): string[] {
133
- const dteTool = this.dteStore.getTool(tool.slug);
134
-
135
- if (!dteTool) {
136
- return [];
137
- }
138
-
139
- try {
140
- return dteTool.dataSources(tool.values) ?? [];
141
- } catch {
142
- return [];
143
- }
144
- }
145
- }