@dotcms/uve 0.0.1-beta.10 → 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.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var types = require('./types.cjs.js');
4
3
  var internal = require('./internal.cjs.js');
4
+ var types = require('./types.cjs.js');
5
5
 
6
6
  /**
7
7
  * Gets the current state of the Universal Visual Editor (UVE).
@@ -82,5 +82,185 @@ function createUVESubscription(event, callback) {
82
82
  return eventCallback(callback);
83
83
  }
84
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 => {
233
+ return {
234
+ inode: vltElement.dataset?.['dotInode'],
235
+ name: vltElement.dataset?.['dotUrl']
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;
256
+ }
257
+
258
+ exports.computeScrollIsInBottom = computeScrollIsInBottom;
85
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;
86
266
  exports.getUVEState = getUVEState;
package/index.esm.js CHANGED
@@ -1,5 +1,5 @@
1
- import { UVE_MODE } from './types.esm.js';
2
1
  import { __UVE_EVENT_ERROR_FALLBACK__, __UVE_EVENTS__ } from './internal.esm.js';
2
+ import { UVE_MODE } from './types.esm.js';
3
3
 
4
4
  /**
5
5
  * Gets the current state of the Universal Visual Editor (UVE).
@@ -80,4 +80,177 @@ function createUVESubscription(event, callback) {
80
80
  return eventCallback(callback);
81
81
  }
82
82
 
83
- export { createUVESubscription, getUVEState };
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 => {
231
+ return {
232
+ inode: vltElement.dataset?.['dotInode'],
233
+ name: vltElement.dataset?.['dotUrl']
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;
254
+ }
255
+
256
+ export { computeScrollIsInBottom, createUVESubscription, findDotCMSElement, findDotCMSVTLData, getClosestDotCMSContainerData, getDotCMSContainerData, getDotCMSContentletsBound, getDotCMSPageBounds, getUVEState };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotcms/uve",
3
- "version": "0.0.1-beta.10",
3
+ "version": "0.0.1-beta.11",
4
4
  "description": "Official JavaScript library for interacting with Universal Visual Editor (UVE)",
5
5
  "repository": {
6
6
  "type": "git",
package/src/index.d.ts CHANGED
@@ -1 +1,2 @@
1
- export * from './lib/utils';
1
+ export * from './lib/core/core.utils';
2
+ export * from './lib/dom/dom.utils';
@@ -1,4 +1,4 @@
1
- import { UVECallback, UVEState, UVESubscription } from './types/editor/public';
1
+ import { UVECallback, UVEState, UVESubscription } from '../types/editor/public';
2
2
  /**
3
3
  * Gets the current state of the Universal Visual Editor (UVE).
4
4
  *
@@ -0,0 +1,111 @@
1
+ import { DotCMSContainerBound, DotCMSContentletBound } from '../types/editor/internal';
2
+ /**
3
+ * Calculates the bounding information for each page element within the given containers.
4
+ *
5
+ * @export
6
+ * @param {HTMLDivElement[]} containers - An array of HTMLDivElement representing the containers.
7
+ * @return {DotCMSContainerBound[]} An array of objects containing the bounding information for each page element.
8
+ * @example
9
+ * ```ts
10
+ * const containers = document.querySelectorAll('.container');
11
+ * const bounds = getDotCMSPageBounds(containers);
12
+ * console.log(bounds);
13
+ * ```
14
+ */
15
+ export declare function getDotCMSPageBounds(containers: HTMLDivElement[]): DotCMSContainerBound[];
16
+ /**
17
+ * Calculates the bounding information for each contentlet inside a container.
18
+ *
19
+ * @export
20
+ * @param {DOMRect} containerRect - The bounding rectangle of the container.
21
+ * @param {HTMLDivElement[]} contentlets - An array of HTMLDivElement representing the contentlets.
22
+ * @return {DotCMSContentletBound[]} An array of objects containing the bounding information for each contentlet.
23
+ * @example
24
+ * ```ts
25
+ * const containerRect = container.getBoundingClientRect();
26
+ * const contentlets = container.querySelectorAll('.contentlet');
27
+ * const bounds = getDotCMSContentletsBound(containerRect, contentlets);
28
+ * console.log(bounds); // Element bounds within the container
29
+ * ```
30
+ */
31
+ export declare function getDotCMSContentletsBound(containerRect: DOMRect, contentlets: HTMLDivElement[]): DotCMSContentletBound[];
32
+ /**
33
+ * Get container data from VTLS.
34
+ *
35
+ * @export
36
+ * @param {HTMLElement} container - The container element.
37
+ * @return {object} An object containing the container data.
38
+ * @example
39
+ * ```ts
40
+ * const container = document.querySelector('.container');
41
+ * const data = getContainerData(container);
42
+ * console.log(data);
43
+ * ```
44
+ */
45
+ export declare function getDotCMSContainerData(container: HTMLElement): {
46
+ acceptTypes: string;
47
+ identifier: string;
48
+ maxContentlets: string;
49
+ uuid: string;
50
+ };
51
+ /**
52
+ * Get the closest container data from the contentlet.
53
+ *
54
+ * @export
55
+ * @param {Element} element - The contentlet element.
56
+ * @return {object | null} An object containing the closest container data or null if no container is found.
57
+ * @example
58
+ * ```ts
59
+ * const contentlet = document.querySelector('.contentlet');
60
+ * const data = getClosestDotCMSContainerData(contentlet);
61
+ * console.log(data);
62
+ * ```
63
+ */
64
+ export declare function getClosestDotCMSContainerData(element: Element): {
65
+ acceptTypes: string;
66
+ identifier: string;
67
+ maxContentlets: string;
68
+ uuid: string;
69
+ } | null;
70
+ /**
71
+ * Find the closest contentlet element based on HTMLElement.
72
+ *
73
+ * @export
74
+ * @param {HTMLElement | null} element - The starting element.
75
+ * @return {HTMLElement | null} The closest contentlet element or null if not found.
76
+ * @example
77
+ * const element = document.querySelector('.some-element');
78
+ * const contentlet = findDotCMSElement(element);
79
+ * console.log(contentlet);
80
+ */
81
+ export declare function findDotCMSElement(element: HTMLElement | null): HTMLElement | null;
82
+ /**
83
+ * Find VTL data within a target element.
84
+ *
85
+ * @export
86
+ * @param {HTMLElement} target - The target element to search within.
87
+ * @return {Array<{ inode: string, name: string }> | null} An array of objects containing VTL data or null if none found.
88
+ * @example
89
+ * ```ts
90
+ * const target = document.querySelector('.target-element');
91
+ * const vtlData = findDotCMSVTLData(target);
92
+ * console.log(vtlData);
93
+ * ```
94
+ */
95
+ export declare function findDotCMSVTLData(target: HTMLElement): {
96
+ inode: string | undefined;
97
+ name: string | undefined;
98
+ }[] | null;
99
+ /**
100
+ * Check if the scroll position is at the bottom of the page.
101
+ *
102
+ * @export
103
+ * @return {boolean} True if the scroll position is at the bottom, otherwise false.
104
+ * @example
105
+ * ```ts
106
+ * if (dotCMSScrollIsInBottom()) {
107
+ * console.log('Scrolled to the bottom');
108
+ * }
109
+ * ```
110
+ */
111
+ export declare function computeScrollIsInBottom(): boolean;
@@ -41,4 +41,40 @@ export interface DotCMSUVE {
41
41
  reorderMenu: DotCMSUVEFunction;
42
42
  lastScrollYPosition: number;
43
43
  }
44
+ /**
45
+ * Bound information for a contentlet.
46
+ *
47
+ * @interface DotCMSContentletBound
48
+ * @property {number} x - The x-coordinate of the contentlet.
49
+ * @property {number} y - The y-coordinate of the contentlet.
50
+ * @property {number} width - The width of the contentlet.
51
+ * @property {number} height - The height of the contentlet.
52
+ * @property {string} payload - The payload data of the contentlet in JSON format.
53
+ */
54
+ export interface DotCMSContentletBound {
55
+ x: number;
56
+ y: number;
57
+ width: number;
58
+ height: number;
59
+ payload: string;
60
+ }
61
+ /**
62
+ * Bound information for a container.
63
+ *
64
+ * @interface DotCMSContainerBound
65
+ * @property {number} x - The x-coordinate of the container.
66
+ * @property {number} y - The y-coordinate of the container.
67
+ * @property {number} width - The width of the container.
68
+ * @property {number} height - The height of the container.
69
+ * @property {string} payload - The payload data of the container in JSON format.
70
+ * @property {DotCMSContentletBound[]} contentlets - An array of contentlets within the container.
71
+ */
72
+ export interface DotCMSContainerBound {
73
+ x: number;
74
+ y: number;
75
+ width: number;
76
+ height: number;
77
+ payload: string;
78
+ contentlets: DotCMSContentletBound[];
79
+ }
44
80
  export {};