@dotcms/uve 0.0.1-beta.0 → 0.0.1-beta.11
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/index.cjs.d.ts +1 -1
- package/index.cjs.js +228 -20
- package/index.esm.d.ts +1 -1
- package/index.esm.js +221 -21
- package/internal.cjs.d.ts +1 -0
- package/internal.cjs.default.js +1 -0
- package/internal.cjs.js +76 -0
- package/internal.cjs.mjs +2 -0
- package/internal.esm.d.ts +1 -0
- package/internal.esm.js +73 -0
- package/package.json +27 -8
- package/src/index.d.ts +2 -0
- package/src/internal/constants.d.ts +18 -0
- package/src/internal/index.d.ts +1 -0
- package/src/internal.d.ts +3 -0
- package/src/lib/{utils.d.ts → core/core.utils.d.ts} +16 -1
- package/src/lib/dom/dom.utils.d.ts +111 -0
- package/src/lib/types/editor/internal.d.ts +80 -0
- package/src/lib/types/editor/public.d.ts +138 -0
- package/src/lib/types/events/internal.d.ts +32 -0
- package/src/lib/types/events/public.d.ts +18 -0
- package/src/types.d.ts +2 -0
- package/types.cjs.d.ts +1 -1
- package/types.cjs.js +70 -4
- package/types.esm.d.ts +1 -1
- package/types.esm.js +71 -5
- package/src/lib/types.d.ts +0 -33
- package/src/public/index.d.ts +0 -2
- package/src/public/types.d.ts +0 -2
package/index.cjs.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "./src/
|
|
1
|
+
export * from "./src/index";
|
package/index.cjs.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var internal = require('./internal.cjs.js');
|
|
3
4
|
var types = require('./types.cjs.js');
|
|
4
5
|
|
|
5
6
|
/**
|
|
@@ -31,28 +32,235 @@ var types = require('./types.cjs.js');
|
|
|
31
32
|
* ```
|
|
32
33
|
*/
|
|
33
34
|
function getUVEState() {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
35
|
+
if (typeof window === 'undefined' || window.parent === window || !window.location) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const url = new URL(window.location.href);
|
|
39
|
+
const possibleModes = Object.values(types.UVE_MODE);
|
|
40
|
+
let mode = url.searchParams.get('mode') ?? types.UVE_MODE.EDIT;
|
|
41
|
+
const languageId = url.searchParams.get('language_id');
|
|
42
|
+
const persona = url.searchParams.get('personaId');
|
|
43
|
+
const variantName = url.searchParams.get('variantName');
|
|
44
|
+
const experimentId = url.searchParams.get('experimentId');
|
|
45
|
+
const publishDate = url.searchParams.get('publishDate');
|
|
46
|
+
if (!possibleModes.includes(mode)) {
|
|
47
|
+
mode = types.UVE_MODE.EDIT;
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
mode,
|
|
51
|
+
languageId,
|
|
52
|
+
persona,
|
|
53
|
+
variantName,
|
|
54
|
+
experimentId,
|
|
55
|
+
publishDate
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Creates a subscription to a UVE event.
|
|
60
|
+
*
|
|
61
|
+
* @param {string} event - The event to subscribe to.
|
|
62
|
+
* @param {UVECallback} callback - The callback to call when the event is triggered.
|
|
63
|
+
* @return {UnsubscribeUVE | undefined} The unsubscribe function if the event is valid, undefined otherwise.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* const unsubscribeChanges = createUVESubscription('changes', (payload) => {
|
|
68
|
+
* console.log(payload);
|
|
69
|
+
* });
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
function createUVESubscription(event, callback) {
|
|
73
|
+
if (!getUVEState()) {
|
|
74
|
+
console.warn('UVE Subscription: Not running inside UVE');
|
|
75
|
+
return internal.__UVE_EVENT_ERROR_FALLBACK__(event);
|
|
76
|
+
}
|
|
77
|
+
const eventCallback = internal.__UVE_EVENTS__[event];
|
|
78
|
+
if (!eventCallback) {
|
|
79
|
+
console.error(`UVE Subscription: Event ${event} not found`);
|
|
80
|
+
return internal.__UVE_EVENT_ERROR_FALLBACK__(event);
|
|
81
|
+
}
|
|
82
|
+
return eventCallback(callback);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Calculates the bounding information for each page element within the given containers.
|
|
87
|
+
*
|
|
88
|
+
* @export
|
|
89
|
+
* @param {HTMLDivElement[]} containers - An array of HTMLDivElement representing the containers.
|
|
90
|
+
* @return {DotCMSContainerBound[]} An array of objects containing the bounding information for each page element.
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* const containers = document.querySelectorAll('.container');
|
|
94
|
+
* const bounds = getDotCMSPageBounds(containers);
|
|
95
|
+
* console.log(bounds);
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
function getDotCMSPageBounds(containers) {
|
|
99
|
+
return containers.map(container => {
|
|
100
|
+
const containerRect = container.getBoundingClientRect();
|
|
101
|
+
const contentlets = Array.from(container.querySelectorAll('[data-dot-object="contentlet"]'));
|
|
102
|
+
return {
|
|
103
|
+
x: containerRect.x,
|
|
104
|
+
y: containerRect.y,
|
|
105
|
+
width: containerRect.width,
|
|
106
|
+
height: containerRect.height,
|
|
107
|
+
payload: JSON.stringify({
|
|
108
|
+
container: getDotCMSContainerData(container)
|
|
109
|
+
}),
|
|
110
|
+
contentlets: getDotCMSContentletsBound(containerRect, contentlets)
|
|
111
|
+
};
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Calculates the bounding information for each contentlet inside a container.
|
|
116
|
+
*
|
|
117
|
+
* @export
|
|
118
|
+
* @param {DOMRect} containerRect - The bounding rectangle of the container.
|
|
119
|
+
* @param {HTMLDivElement[]} contentlets - An array of HTMLDivElement representing the contentlets.
|
|
120
|
+
* @return {DotCMSContentletBound[]} An array of objects containing the bounding information for each contentlet.
|
|
121
|
+
* @example
|
|
122
|
+
* ```ts
|
|
123
|
+
* const containerRect = container.getBoundingClientRect();
|
|
124
|
+
* const contentlets = container.querySelectorAll('.contentlet');
|
|
125
|
+
* const bounds = getDotCMSContentletsBound(containerRect, contentlets);
|
|
126
|
+
* console.log(bounds); // Element bounds within the container
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
function getDotCMSContentletsBound(containerRect, contentlets) {
|
|
130
|
+
return contentlets.map(contentlet => {
|
|
131
|
+
const contentletRect = contentlet.getBoundingClientRect();
|
|
132
|
+
return {
|
|
133
|
+
x: 0,
|
|
134
|
+
y: contentletRect.y - containerRect.y,
|
|
135
|
+
width: contentletRect.width,
|
|
136
|
+
height: contentletRect.height,
|
|
137
|
+
payload: JSON.stringify({
|
|
138
|
+
container: contentlet.dataset?.['dotContainer'] ? JSON.parse(contentlet.dataset?.['dotContainer']) : getClosestDotCMSContainerData(contentlet),
|
|
139
|
+
contentlet: {
|
|
140
|
+
identifier: contentlet.dataset?.['dotIdentifier'],
|
|
141
|
+
title: contentlet.dataset?.['dotTitle'],
|
|
142
|
+
inode: contentlet.dataset?.['dotInode'],
|
|
143
|
+
contentType: contentlet.dataset?.['dotType']
|
|
144
|
+
}
|
|
145
|
+
})
|
|
146
|
+
};
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Get container data from VTLS.
|
|
151
|
+
*
|
|
152
|
+
* @export
|
|
153
|
+
* @param {HTMLElement} container - The container element.
|
|
154
|
+
* @return {object} An object containing the container data.
|
|
155
|
+
* @example
|
|
156
|
+
* ```ts
|
|
157
|
+
* const container = document.querySelector('.container');
|
|
158
|
+
* const data = getContainerData(container);
|
|
159
|
+
* console.log(data);
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
162
|
+
function getDotCMSContainerData(container) {
|
|
163
|
+
return {
|
|
164
|
+
acceptTypes: container.dataset?.['dotAcceptTypes'] || '',
|
|
165
|
+
identifier: container.dataset?.['dotIdentifier'] || '',
|
|
166
|
+
maxContentlets: container.dataset?.['maxContentlets'] || '',
|
|
167
|
+
uuid: container.dataset?.['dotUuid'] || ''
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Get the closest container data from the contentlet.
|
|
172
|
+
*
|
|
173
|
+
* @export
|
|
174
|
+
* @param {Element} element - The contentlet element.
|
|
175
|
+
* @return {object | null} An object containing the closest container data or null if no container is found.
|
|
176
|
+
* @example
|
|
177
|
+
* ```ts
|
|
178
|
+
* const contentlet = document.querySelector('.contentlet');
|
|
179
|
+
* const data = getClosestDotCMSContainerData(contentlet);
|
|
180
|
+
* console.log(data);
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
function getClosestDotCMSContainerData(element) {
|
|
184
|
+
// Find the closest ancestor element with data-dot-object="container" attribute
|
|
185
|
+
const container = element.closest('[data-dot-object="container"]');
|
|
186
|
+
// If a container element is found
|
|
187
|
+
if (container) {
|
|
188
|
+
// Return the dataset of the container element
|
|
189
|
+
return getDotCMSContainerData(container);
|
|
190
|
+
} else {
|
|
191
|
+
// If no container element is found, return null
|
|
192
|
+
console.warn('No container found for the contentlet');
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Find the closest contentlet element based on HTMLElement.
|
|
198
|
+
*
|
|
199
|
+
* @export
|
|
200
|
+
* @param {HTMLElement | null} element - The starting element.
|
|
201
|
+
* @return {HTMLElement | null} The closest contentlet element or null if not found.
|
|
202
|
+
* @example
|
|
203
|
+
* const element = document.querySelector('.some-element');
|
|
204
|
+
* const contentlet = findDotCMSElement(element);
|
|
205
|
+
* console.log(contentlet);
|
|
206
|
+
*/
|
|
207
|
+
function findDotCMSElement(element) {
|
|
208
|
+
if (!element) return null;
|
|
209
|
+
if (element?.dataset?.['dotObject'] === 'contentlet' || element?.dataset?.['dotObject'] === 'container' && element.children.length === 0) {
|
|
210
|
+
return element;
|
|
211
|
+
}
|
|
212
|
+
return findDotCMSElement(element?.['parentElement']);
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Find VTL data within a target element.
|
|
216
|
+
*
|
|
217
|
+
* @export
|
|
218
|
+
* @param {HTMLElement} target - The target element to search within.
|
|
219
|
+
* @return {Array<{ inode: string, name: string }> | null} An array of objects containing VTL data or null if none found.
|
|
220
|
+
* @example
|
|
221
|
+
* ```ts
|
|
222
|
+
* const target = document.querySelector('.target-element');
|
|
223
|
+
* const vtlData = findDotCMSVTLData(target);
|
|
224
|
+
* console.log(vtlData);
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
227
|
+
function findDotCMSVTLData(target) {
|
|
228
|
+
const vltElements = target.querySelectorAll('[data-dot-object="vtl-file"]');
|
|
229
|
+
if (!vltElements.length) {
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
return Array.from(vltElements).map(vltElement => {
|
|
48
233
|
return {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
persona,
|
|
52
|
-
variantName,
|
|
53
|
-
experimentId,
|
|
54
|
-
publishDate
|
|
234
|
+
inode: vltElement.dataset?.['dotInode'],
|
|
235
|
+
name: vltElement.dataset?.['dotUrl']
|
|
55
236
|
};
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Check if the scroll position is at the bottom of the page.
|
|
241
|
+
*
|
|
242
|
+
* @export
|
|
243
|
+
* @return {boolean} True if the scroll position is at the bottom, otherwise false.
|
|
244
|
+
* @example
|
|
245
|
+
* ```ts
|
|
246
|
+
* if (dotCMSScrollIsInBottom()) {
|
|
247
|
+
* console.log('Scrolled to the bottom');
|
|
248
|
+
* }
|
|
249
|
+
* ```
|
|
250
|
+
*/
|
|
251
|
+
function computeScrollIsInBottom() {
|
|
252
|
+
const documentHeight = document.documentElement.scrollHeight;
|
|
253
|
+
const viewportHeight = window.innerHeight;
|
|
254
|
+
const scrollY = window.scrollY;
|
|
255
|
+
return scrollY + viewportHeight >= documentHeight;
|
|
56
256
|
}
|
|
57
257
|
|
|
258
|
+
exports.computeScrollIsInBottom = computeScrollIsInBottom;
|
|
259
|
+
exports.createUVESubscription = createUVESubscription;
|
|
260
|
+
exports.findDotCMSElement = findDotCMSElement;
|
|
261
|
+
exports.findDotCMSVTLData = findDotCMSVTLData;
|
|
262
|
+
exports.getClosestDotCMSContainerData = getClosestDotCMSContainerData;
|
|
263
|
+
exports.getDotCMSContainerData = getDotCMSContainerData;
|
|
264
|
+
exports.getDotCMSContentletsBound = getDotCMSContentletsBound;
|
|
265
|
+
exports.getDotCMSPageBounds = getDotCMSPageBounds;
|
|
58
266
|
exports.getUVEState = getUVEState;
|
package/index.esm.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "./src/
|
|
1
|
+
export * from "./src/index";
|
package/index.esm.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { __UVE_EVENT_ERROR_FALLBACK__, __UVE_EVENTS__ } from './internal.esm.js';
|
|
1
2
|
import { UVE_MODE } from './types.esm.js';
|
|
2
3
|
|
|
3
4
|
/**
|
|
@@ -29,28 +30,227 @@ import { UVE_MODE } from './types.esm.js';
|
|
|
29
30
|
* ```
|
|
30
31
|
*/
|
|
31
32
|
function getUVEState() {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
33
|
+
if (typeof window === 'undefined' || window.parent === window || !window.location) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const url = new URL(window.location.href);
|
|
37
|
+
const possibleModes = Object.values(UVE_MODE);
|
|
38
|
+
let mode = url.searchParams.get('mode') ?? UVE_MODE.EDIT;
|
|
39
|
+
const languageId = url.searchParams.get('language_id');
|
|
40
|
+
const persona = url.searchParams.get('personaId');
|
|
41
|
+
const variantName = url.searchParams.get('variantName');
|
|
42
|
+
const experimentId = url.searchParams.get('experimentId');
|
|
43
|
+
const publishDate = url.searchParams.get('publishDate');
|
|
44
|
+
if (!possibleModes.includes(mode)) {
|
|
45
|
+
mode = UVE_MODE.EDIT;
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
mode,
|
|
49
|
+
languageId,
|
|
50
|
+
persona,
|
|
51
|
+
variantName,
|
|
52
|
+
experimentId,
|
|
53
|
+
publishDate
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Creates a subscription to a UVE event.
|
|
58
|
+
*
|
|
59
|
+
* @param {string} event - The event to subscribe to.
|
|
60
|
+
* @param {UVECallback} callback - The callback to call when the event is triggered.
|
|
61
|
+
* @return {UnsubscribeUVE | undefined} The unsubscribe function if the event is valid, undefined otherwise.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* const unsubscribeChanges = createUVESubscription('changes', (payload) => {
|
|
66
|
+
* console.log(payload);
|
|
67
|
+
* });
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
function createUVESubscription(event, callback) {
|
|
71
|
+
if (!getUVEState()) {
|
|
72
|
+
console.warn('UVE Subscription: Not running inside UVE');
|
|
73
|
+
return __UVE_EVENT_ERROR_FALLBACK__(event);
|
|
74
|
+
}
|
|
75
|
+
const eventCallback = __UVE_EVENTS__[event];
|
|
76
|
+
if (!eventCallback) {
|
|
77
|
+
console.error(`UVE Subscription: Event ${event} not found`);
|
|
78
|
+
return __UVE_EVENT_ERROR_FALLBACK__(event);
|
|
79
|
+
}
|
|
80
|
+
return eventCallback(callback);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Calculates the bounding information for each page element within the given containers.
|
|
85
|
+
*
|
|
86
|
+
* @export
|
|
87
|
+
* @param {HTMLDivElement[]} containers - An array of HTMLDivElement representing the containers.
|
|
88
|
+
* @return {DotCMSContainerBound[]} An array of objects containing the bounding information for each page element.
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* const containers = document.querySelectorAll('.container');
|
|
92
|
+
* const bounds = getDotCMSPageBounds(containers);
|
|
93
|
+
* console.log(bounds);
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
function getDotCMSPageBounds(containers) {
|
|
97
|
+
return containers.map(container => {
|
|
98
|
+
const containerRect = container.getBoundingClientRect();
|
|
99
|
+
const contentlets = Array.from(container.querySelectorAll('[data-dot-object="contentlet"]'));
|
|
100
|
+
return {
|
|
101
|
+
x: containerRect.x,
|
|
102
|
+
y: containerRect.y,
|
|
103
|
+
width: containerRect.width,
|
|
104
|
+
height: containerRect.height,
|
|
105
|
+
payload: JSON.stringify({
|
|
106
|
+
container: getDotCMSContainerData(container)
|
|
107
|
+
}),
|
|
108
|
+
contentlets: getDotCMSContentletsBound(containerRect, contentlets)
|
|
109
|
+
};
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Calculates the bounding information for each contentlet inside a container.
|
|
114
|
+
*
|
|
115
|
+
* @export
|
|
116
|
+
* @param {DOMRect} containerRect - The bounding rectangle of the container.
|
|
117
|
+
* @param {HTMLDivElement[]} contentlets - An array of HTMLDivElement representing the contentlets.
|
|
118
|
+
* @return {DotCMSContentletBound[]} An array of objects containing the bounding information for each contentlet.
|
|
119
|
+
* @example
|
|
120
|
+
* ```ts
|
|
121
|
+
* const containerRect = container.getBoundingClientRect();
|
|
122
|
+
* const contentlets = container.querySelectorAll('.contentlet');
|
|
123
|
+
* const bounds = getDotCMSContentletsBound(containerRect, contentlets);
|
|
124
|
+
* console.log(bounds); // Element bounds within the container
|
|
125
|
+
* ```
|
|
126
|
+
*/
|
|
127
|
+
function getDotCMSContentletsBound(containerRect, contentlets) {
|
|
128
|
+
return contentlets.map(contentlet => {
|
|
129
|
+
const contentletRect = contentlet.getBoundingClientRect();
|
|
130
|
+
return {
|
|
131
|
+
x: 0,
|
|
132
|
+
y: contentletRect.y - containerRect.y,
|
|
133
|
+
width: contentletRect.width,
|
|
134
|
+
height: contentletRect.height,
|
|
135
|
+
payload: JSON.stringify({
|
|
136
|
+
container: contentlet.dataset?.['dotContainer'] ? JSON.parse(contentlet.dataset?.['dotContainer']) : getClosestDotCMSContainerData(contentlet),
|
|
137
|
+
contentlet: {
|
|
138
|
+
identifier: contentlet.dataset?.['dotIdentifier'],
|
|
139
|
+
title: contentlet.dataset?.['dotTitle'],
|
|
140
|
+
inode: contentlet.dataset?.['dotInode'],
|
|
141
|
+
contentType: contentlet.dataset?.['dotType']
|
|
142
|
+
}
|
|
143
|
+
})
|
|
144
|
+
};
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Get container data from VTLS.
|
|
149
|
+
*
|
|
150
|
+
* @export
|
|
151
|
+
* @param {HTMLElement} container - The container element.
|
|
152
|
+
* @return {object} An object containing the container data.
|
|
153
|
+
* @example
|
|
154
|
+
* ```ts
|
|
155
|
+
* const container = document.querySelector('.container');
|
|
156
|
+
* const data = getContainerData(container);
|
|
157
|
+
* console.log(data);
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
function getDotCMSContainerData(container) {
|
|
161
|
+
return {
|
|
162
|
+
acceptTypes: container.dataset?.['dotAcceptTypes'] || '',
|
|
163
|
+
identifier: container.dataset?.['dotIdentifier'] || '',
|
|
164
|
+
maxContentlets: container.dataset?.['maxContentlets'] || '',
|
|
165
|
+
uuid: container.dataset?.['dotUuid'] || ''
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Get the closest container data from the contentlet.
|
|
170
|
+
*
|
|
171
|
+
* @export
|
|
172
|
+
* @param {Element} element - The contentlet element.
|
|
173
|
+
* @return {object | null} An object containing the closest container data or null if no container is found.
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* const contentlet = document.querySelector('.contentlet');
|
|
177
|
+
* const data = getClosestDotCMSContainerData(contentlet);
|
|
178
|
+
* console.log(data);
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
function getClosestDotCMSContainerData(element) {
|
|
182
|
+
// Find the closest ancestor element with data-dot-object="container" attribute
|
|
183
|
+
const container = element.closest('[data-dot-object="container"]');
|
|
184
|
+
// If a container element is found
|
|
185
|
+
if (container) {
|
|
186
|
+
// Return the dataset of the container element
|
|
187
|
+
return getDotCMSContainerData(container);
|
|
188
|
+
} else {
|
|
189
|
+
// If no container element is found, return null
|
|
190
|
+
console.warn('No container found for the contentlet');
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Find the closest contentlet element based on HTMLElement.
|
|
196
|
+
*
|
|
197
|
+
* @export
|
|
198
|
+
* @param {HTMLElement | null} element - The starting element.
|
|
199
|
+
* @return {HTMLElement | null} The closest contentlet element or null if not found.
|
|
200
|
+
* @example
|
|
201
|
+
* const element = document.querySelector('.some-element');
|
|
202
|
+
* const contentlet = findDotCMSElement(element);
|
|
203
|
+
* console.log(contentlet);
|
|
204
|
+
*/
|
|
205
|
+
function findDotCMSElement(element) {
|
|
206
|
+
if (!element) return null;
|
|
207
|
+
if (element?.dataset?.['dotObject'] === 'contentlet' || element?.dataset?.['dotObject'] === 'container' && element.children.length === 0) {
|
|
208
|
+
return element;
|
|
209
|
+
}
|
|
210
|
+
return findDotCMSElement(element?.['parentElement']);
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Find VTL data within a target element.
|
|
214
|
+
*
|
|
215
|
+
* @export
|
|
216
|
+
* @param {HTMLElement} target - The target element to search within.
|
|
217
|
+
* @return {Array<{ inode: string, name: string }> | null} An array of objects containing VTL data or null if none found.
|
|
218
|
+
* @example
|
|
219
|
+
* ```ts
|
|
220
|
+
* const target = document.querySelector('.target-element');
|
|
221
|
+
* const vtlData = findDotCMSVTLData(target);
|
|
222
|
+
* console.log(vtlData);
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
function findDotCMSVTLData(target) {
|
|
226
|
+
const vltElements = target.querySelectorAll('[data-dot-object="vtl-file"]');
|
|
227
|
+
if (!vltElements.length) {
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
return Array.from(vltElements).map(vltElement => {
|
|
46
231
|
return {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
persona,
|
|
50
|
-
variantName,
|
|
51
|
-
experimentId,
|
|
52
|
-
publishDate
|
|
232
|
+
inode: vltElement.dataset?.['dotInode'],
|
|
233
|
+
name: vltElement.dataset?.['dotUrl']
|
|
53
234
|
};
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Check if the scroll position is at the bottom of the page.
|
|
239
|
+
*
|
|
240
|
+
* @export
|
|
241
|
+
* @return {boolean} True if the scroll position is at the bottom, otherwise false.
|
|
242
|
+
* @example
|
|
243
|
+
* ```ts
|
|
244
|
+
* if (dotCMSScrollIsInBottom()) {
|
|
245
|
+
* console.log('Scrolled to the bottom');
|
|
246
|
+
* }
|
|
247
|
+
* ```
|
|
248
|
+
*/
|
|
249
|
+
function computeScrollIsInBottom() {
|
|
250
|
+
const documentHeight = document.documentElement.scrollHeight;
|
|
251
|
+
const viewportHeight = window.innerHeight;
|
|
252
|
+
const scrollY = window.scrollY;
|
|
253
|
+
return scrollY + viewportHeight >= documentHeight;
|
|
54
254
|
}
|
|
55
255
|
|
|
56
|
-
export { getUVEState };
|
|
256
|
+
export { computeScrollIsInBottom, createUVESubscription, findDotCMSElement, findDotCMSVTLData, getClosestDotCMSContainerData, getDotCMSContainerData, getDotCMSContentletsBound, getDotCMSPageBounds, getUVEState };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/internal";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./internal.cjs.js').default;
|
package/internal.cjs.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Actions received from the dotcms editor
|
|
5
|
+
*
|
|
6
|
+
* @export
|
|
7
|
+
* @enum {number}
|
|
8
|
+
*/
|
|
9
|
+
exports.__DOTCMS_UVE_EVENT__ = void 0;
|
|
10
|
+
(function (__DOTCMS_UVE_EVENT__) {
|
|
11
|
+
/**
|
|
12
|
+
* Request to page to reload
|
|
13
|
+
*/
|
|
14
|
+
__DOTCMS_UVE_EVENT__["UVE_RELOAD_PAGE"] = "uve-reload-page";
|
|
15
|
+
/**
|
|
16
|
+
* Request the bounds for the elements
|
|
17
|
+
*/
|
|
18
|
+
__DOTCMS_UVE_EVENT__["UVE_REQUEST_BOUNDS"] = "uve-request-bounds";
|
|
19
|
+
/**
|
|
20
|
+
* Received pong from the editor
|
|
21
|
+
*/
|
|
22
|
+
__DOTCMS_UVE_EVENT__["UVE_EDITOR_PONG"] = "uve-editor-pong";
|
|
23
|
+
/**
|
|
24
|
+
* Received scroll event trigger from the editor
|
|
25
|
+
*/
|
|
26
|
+
__DOTCMS_UVE_EVENT__["UVE_SCROLL_INSIDE_IFRAME"] = "uve-scroll-inside-iframe";
|
|
27
|
+
/**
|
|
28
|
+
* Set the page data
|
|
29
|
+
*/
|
|
30
|
+
__DOTCMS_UVE_EVENT__["UVE_SET_PAGE_DATA"] = "uve-set-page-data";
|
|
31
|
+
/**
|
|
32
|
+
* Copy contentlet inline editing success
|
|
33
|
+
*/
|
|
34
|
+
__DOTCMS_UVE_EVENT__["UVE_COPY_CONTENTLET_INLINE_EDITING_SUCCESS"] = "uve-copy-contentlet-inline-editing-success";
|
|
35
|
+
})(exports.__DOTCMS_UVE_EVENT__ || (exports.__DOTCMS_UVE_EVENT__ = {}));
|
|
36
|
+
|
|
37
|
+
// TODO: WE NEED TO LOOK FOR ALL THE NOTIFY_CLIENT EVENTS AND ADD THEM TO THE UVE_EVENTS CONSTANT WHEN WE MIGRATE THE EDITOR TO THE NEW UVE LIBRARY
|
|
38
|
+
/**
|
|
39
|
+
* Events that can be subscribed to in the UVE
|
|
40
|
+
*
|
|
41
|
+
* @internal
|
|
42
|
+
* @type {Record<string, UVEEvent>}
|
|
43
|
+
*/
|
|
44
|
+
const __UVE_EVENTS__ = {
|
|
45
|
+
changes: callback => {
|
|
46
|
+
const messageCallback = event => {
|
|
47
|
+
if (event.data.name === exports.__DOTCMS_UVE_EVENT__.UVE_SET_PAGE_DATA) {
|
|
48
|
+
callback(event.data.payload);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
window.addEventListener('message', messageCallback);
|
|
52
|
+
return {
|
|
53
|
+
unsubscribe: () => {
|
|
54
|
+
window.removeEventListener('message', messageCallback);
|
|
55
|
+
},
|
|
56
|
+
event: 'changes'
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Default UVE event
|
|
62
|
+
*
|
|
63
|
+
* @param {string} event - The event to subscribe to.
|
|
64
|
+
* @internal
|
|
65
|
+
*/
|
|
66
|
+
const __UVE_EVENT_ERROR_FALLBACK__ = event => {
|
|
67
|
+
return {
|
|
68
|
+
unsubscribe: () => {
|
|
69
|
+
/* do nothing */
|
|
70
|
+
},
|
|
71
|
+
event
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
exports.__UVE_EVENTS__ = __UVE_EVENTS__;
|
|
76
|
+
exports.__UVE_EVENT_ERROR_FALLBACK__ = __UVE_EVENT_ERROR_FALLBACK__;
|
package/internal.cjs.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/internal";
|
package/internal.esm.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Actions received from the dotcms editor
|
|
3
|
+
*
|
|
4
|
+
* @export
|
|
5
|
+
* @enum {number}
|
|
6
|
+
*/
|
|
7
|
+
var __DOTCMS_UVE_EVENT__;
|
|
8
|
+
(function (__DOTCMS_UVE_EVENT__) {
|
|
9
|
+
/**
|
|
10
|
+
* Request to page to reload
|
|
11
|
+
*/
|
|
12
|
+
__DOTCMS_UVE_EVENT__["UVE_RELOAD_PAGE"] = "uve-reload-page";
|
|
13
|
+
/**
|
|
14
|
+
* Request the bounds for the elements
|
|
15
|
+
*/
|
|
16
|
+
__DOTCMS_UVE_EVENT__["UVE_REQUEST_BOUNDS"] = "uve-request-bounds";
|
|
17
|
+
/**
|
|
18
|
+
* Received pong from the editor
|
|
19
|
+
*/
|
|
20
|
+
__DOTCMS_UVE_EVENT__["UVE_EDITOR_PONG"] = "uve-editor-pong";
|
|
21
|
+
/**
|
|
22
|
+
* Received scroll event trigger from the editor
|
|
23
|
+
*/
|
|
24
|
+
__DOTCMS_UVE_EVENT__["UVE_SCROLL_INSIDE_IFRAME"] = "uve-scroll-inside-iframe";
|
|
25
|
+
/**
|
|
26
|
+
* Set the page data
|
|
27
|
+
*/
|
|
28
|
+
__DOTCMS_UVE_EVENT__["UVE_SET_PAGE_DATA"] = "uve-set-page-data";
|
|
29
|
+
/**
|
|
30
|
+
* Copy contentlet inline editing success
|
|
31
|
+
*/
|
|
32
|
+
__DOTCMS_UVE_EVENT__["UVE_COPY_CONTENTLET_INLINE_EDITING_SUCCESS"] = "uve-copy-contentlet-inline-editing-success";
|
|
33
|
+
})(__DOTCMS_UVE_EVENT__ || (__DOTCMS_UVE_EVENT__ = {}));
|
|
34
|
+
|
|
35
|
+
// TODO: WE NEED TO LOOK FOR ALL THE NOTIFY_CLIENT EVENTS AND ADD THEM TO THE UVE_EVENTS CONSTANT WHEN WE MIGRATE THE EDITOR TO THE NEW UVE LIBRARY
|
|
36
|
+
/**
|
|
37
|
+
* Events that can be subscribed to in the UVE
|
|
38
|
+
*
|
|
39
|
+
* @internal
|
|
40
|
+
* @type {Record<string, UVEEvent>}
|
|
41
|
+
*/
|
|
42
|
+
const __UVE_EVENTS__ = {
|
|
43
|
+
changes: callback => {
|
|
44
|
+
const messageCallback = event => {
|
|
45
|
+
if (event.data.name === __DOTCMS_UVE_EVENT__.UVE_SET_PAGE_DATA) {
|
|
46
|
+
callback(event.data.payload);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
window.addEventListener('message', messageCallback);
|
|
50
|
+
return {
|
|
51
|
+
unsubscribe: () => {
|
|
52
|
+
window.removeEventListener('message', messageCallback);
|
|
53
|
+
},
|
|
54
|
+
event: 'changes'
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Default UVE event
|
|
60
|
+
*
|
|
61
|
+
* @param {string} event - The event to subscribe to.
|
|
62
|
+
* @internal
|
|
63
|
+
*/
|
|
64
|
+
const __UVE_EVENT_ERROR_FALLBACK__ = event => {
|
|
65
|
+
return {
|
|
66
|
+
unsubscribe: () => {
|
|
67
|
+
/* do nothing */
|
|
68
|
+
},
|
|
69
|
+
event
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
export { __DOTCMS_UVE_EVENT__, __UVE_EVENTS__, __UVE_EVENT_ERROR_FALLBACK__ };
|