-
Notifications
You must be signed in to change notification settings - Fork 10
/
ice.ts
227 lines (199 loc) · 7.04 KB
/
ice.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright (C) 2007-2021 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
import { ContentInstance, CrafterConfig } from '@craftercms/models';
import { crafterConf } from '@craftercms/classes';
declare namespace window {
const crafterRequire: any;
const craftercms: {
xb: {
initInContextEditing(props: { path: string; props: Record<string, any> }): { unmount(): void }
}
};
}
export interface BaseCrafterConfig extends Pick<CrafterConfig, 'site' | 'baseUrl'> {
}
export interface ICEConfig {
model: ContentInstance;
parentModelId?: string;
label?: string;
group?: string;
isAuthoring?: boolean;
}
export interface UseDropZoneConfig {
model: ContentInstance;
zoneName: string;
isAuthoring?: boolean;
}
export interface ICEAttributes {
'data-studio-ice': string;
'data-studio-ice-path': string;
'data-studio-ice-label': string;
'data-studio-component': string;
'data-studio-component-path': string;
'data-studio-embedded-item-id'?: string;
}
export interface DropZoneAttributes {
'data-studio-components-target': string,
'data-studio-components-objectid': string,
'data-studio-zone-content-type': string
}
const pathRegExp = /^\/(.*?)\.xml$/;
const printedErrorCache = {
nullParentId: {},
invalidParentId: {},
invalidPath: {}
};
export function addAuthoringSupport(config?: Partial<BaseCrafterConfig & { xb?: boolean }>): Promise<any> {
const isV4 = Boolean(config?.xb);
config = crafterConf.mix(config);
return new Promise((resolve) => {
const script = document.createElement('script');
script.src = isV4
? `${config.baseUrl}/studio/static-assets/scripts/craftercms-xb.umd.js`
: `${config.baseUrl}/studio/static-assets/libs/requirejs/require.js`;
script.addEventListener('load', () => {
if (isV4) {
resolve(window.craftercms?.xb);
} else {
window.crafterRequire?.([`${config.baseUrl}/studio/overlayhook?.js`], () => {
window.crafterRequire(['guest'], (guest) => {
resolve(guest);
});
});
}
});
document.head.appendChild(script);
});
}
export function getICEAttributes(config: ICEConfig);
export function getICEAttributes(
config: ICEConfig,
wrapperUtility: string = '[Error @ getICEAttributes]'
): ICEAttributes {
let {
model,
parentModelId = null,
label,
isAuthoring = true,
group = ''
} = config;
if (!isAuthoring) {
return ({} as ICEAttributes);
}
if (label === null || label === undefined) {
label = (model?.craftercms.label || '');
}
let error = false;
const isEmbedded = model?.craftercms.path == null;
const path = model?.craftercms.path ?? parentModelId;
const modelId = model?.craftercms.id;
if (isEmbedded && parentModelId == null) {
error = true;
(!modelId) || (printedErrorCache.nullParentId[modelId] == null) &&
console?.error?.(
wrapperUtility +
'The "parentModelId" argument is required for embedded components. ' +
'Note the value of "parentModelId" should be the *path* of it\'s top parent component. ' +
'The error occurred with the model attached to this error.',
model
);
modelId && (printedErrorCache.nullParentId[modelId] = true);
}
if (parentModelId != null && !pathRegExp.test(parentModelId)) {
error = true;
(!modelId) || (printedErrorCache.invalidParentId[modelId] == null) &&
console?.error?.(
wrapperUtility +
'The "parentModelId" argument should be the "path" of it\'s top parent component. ' +
`Provided value was "${parentModelId}" which doesn't comply with the expected format ` +
'(i.e. \'/a/**/b.xml\'). The error occurred with the model attached to this error. ' +
'Did you send the id (objectId) instead of the path?',
model
);
modelId && (printedErrorCache.invalidParentId[modelId] = true);
}
// Only run this if it's not embedded. When is embedded, the prior parentModelId
// validations would have thrown already.
if (!isEmbedded && !pathRegExp.test(path)) {
error = true;
(modelId) && (printedErrorCache.invalidPath[modelId] == null) &&
console?.error?.(
wrapperUtility +
'The model.craftercms.path property to be the "path" of page/component. ' +
`Provided value was "${path}" which doesn't comply with the expected format ` +
'(i.e. \'/a/**/b.xml\'). The error occurred with the model attached to this error. ' +
'Check that your query includes this value and you\'re using parseDescriptor to supply ' +
'the expected data structure for this utility.',
model
);
modelId && (printedErrorCache.invalidPath[modelId] = true);
}
if (error) {
return ({} as ICEAttributes);
}
return {
...isEmbedded ? { 'data-studio-embedded-item-id': modelId } : {},
'data-studio-ice': group,
'data-studio-ice-path': path,
'data-studio-ice-label': label,
'data-studio-component': path,
'data-studio-component-path': path
};
}
export function getDropZoneAttributes(config: UseDropZoneConfig): DropZoneAttributes {
const { model, zoneName, isAuthoring = true } = config;
if (!isAuthoring) {
return ({} as DropZoneAttributes);
}
const modelId = model?.craftercms.id;
const contentTypeId = model?.craftercms.contentTypeId;
return {
'data-studio-components-target': zoneName,
'data-studio-components-objectid': modelId,
'data-studio-zone-content-type': contentTypeId
};
}
export const reportNavigation: (url: string) => void = (function () {
let reportNavigation;
reportNavigation = (location: string, url: string) => {
window.crafterRequire?.(['guest'], (guest) => {
reportNavigation = guest.reportNavigation;
__report(url);
});
};
function __report(url: string) {
// @ts-ignore
reportNavigation(window.location.origin, url);
}
return __report;
}) ();
export const repaintPencils: (() => void) = (function () {
let repaintPencilsTimeout;
return () => {
clearTimeout(repaintPencilsTimeout);
repaintPencilsTimeout = setTimeout(() => {
window.crafterRequire?.defined('guest') && window.crafterRequire(['guest'], function ({ iceRepaint }) {
iceRepaint();
});
}, 150);
};
})();
export function fetchIsAuthoring(config?: Partial<BaseCrafterConfig>): Promise<boolean> {
let cfg = crafterConf.mix(config);
return fetch(`${cfg.baseUrl}/api/1/config/preview.json?crafterSite=${cfg.site}`, cfg.cors ? { mode: 'cors' } : {})
.then((response) => response.json())
.then((response) => response.preview);
}