@dotcms/client 0.0.1-alpha.4 → 0.0.1-alpha.40

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.
Files changed (51) hide show
  1. package/.eslintrc.json +18 -0
  2. package/README.md +17 -6
  3. package/jest.config.ts +15 -0
  4. package/package.json +5 -5
  5. package/project.json +72 -0
  6. package/src/index.ts +30 -0
  7. package/src/lib/client/content/builders/collection/collection.spec.ts +515 -0
  8. package/src/lib/client/content/builders/collection/collection.ts +416 -0
  9. package/src/lib/client/content/content-api.ts +139 -0
  10. package/src/lib/client/content/shared/const.ts +15 -0
  11. package/src/lib/client/content/shared/types.ts +155 -0
  12. package/src/lib/client/content/shared/utils.ts +28 -0
  13. package/src/lib/client/models/index.ts +19 -0
  14. package/src/lib/client/models/types.ts +14 -0
  15. package/src/lib/client/sdk-js-client.spec.ts +483 -0
  16. package/src/lib/client/sdk-js-client.ts +442 -0
  17. package/src/lib/editor/listeners/listeners.spec.ts +119 -0
  18. package/src/lib/editor/listeners/listeners.ts +223 -0
  19. package/src/lib/editor/models/client.model.ts +84 -0
  20. package/src/lib/editor/models/editor.model.ts +53 -0
  21. package/src/lib/editor/models/listeners.model.ts +50 -0
  22. package/src/lib/editor/sdk-editor-vtl.ts +31 -0
  23. package/src/lib/editor/sdk-editor.spec.ts +116 -0
  24. package/src/lib/editor/sdk-editor.ts +105 -0
  25. package/src/lib/editor/utils/editor.utils.spec.ts +206 -0
  26. package/src/lib/editor/utils/editor.utils.ts +258 -0
  27. package/src/lib/query-builder/lucene-syntax/Equals.ts +148 -0
  28. package/src/lib/query-builder/lucene-syntax/Field.ts +40 -0
  29. package/src/lib/query-builder/lucene-syntax/NotOperand.ts +34 -0
  30. package/src/lib/query-builder/lucene-syntax/Operand.ts +58 -0
  31. package/src/lib/query-builder/lucene-syntax/index.ts +4 -0
  32. package/src/lib/query-builder/sdk-query-builder.spec.ts +159 -0
  33. package/src/lib/query-builder/sdk-query-builder.ts +87 -0
  34. package/src/lib/query-builder/utils/index.ts +179 -0
  35. package/src/lib/utils/graphql/transforms.spec.ts +150 -0
  36. package/src/lib/utils/graphql/transforms.ts +99 -0
  37. package/src/lib/utils/index.ts +2 -0
  38. package/src/lib/utils/page/common-utils.spec.ts +37 -0
  39. package/src/lib/utils/page/common-utils.ts +64 -0
  40. package/tsconfig.json +22 -0
  41. package/tsconfig.lib.json +13 -0
  42. package/tsconfig.spec.json +9 -0
  43. package/src/index.d.ts +0 -2
  44. package/src/index.js +0 -3
  45. package/src/index.js.map +0 -1
  46. package/src/lib/postMessageToEditor.d.ts +0 -50
  47. package/src/lib/postMessageToEditor.js +0 -42
  48. package/src/lib/postMessageToEditor.js.map +0 -1
  49. package/src/lib/sdk-js-client.d.ts +0 -183
  50. package/src/lib/sdk-js-client.js +0 -145
  51. package/src/lib/sdk-js-client.js.map +0 -1
@@ -0,0 +1,223 @@
1
+ import { CUSTOMER_ACTIONS, postMessageToEditor } from '../models/client.model';
2
+ import { DotCMSPageEditorSubscription, NOTIFY_CUSTOMER } from '../models/listeners.model';
3
+ import {
4
+ findVTLData,
5
+ findDotElement,
6
+ getClosestContainerData,
7
+ getPageElementBound,
8
+ scrollIsInBottom
9
+ } from '../utils/editor.utils';
10
+
11
+ declare global {
12
+ interface Window {
13
+ lastScrollYPosition: number;
14
+ }
15
+ }
16
+
17
+ /**
18
+ * Represents an array of DotCMSPageEditorSubscription objects.
19
+ * Used to store the subscriptions for the editor and unsubscribe later.
20
+ */
21
+ export const subscriptions: DotCMSPageEditorSubscription[] = [];
22
+
23
+ /**
24
+ * Sets the bounds of the containers in the editor.
25
+ * Retrieves the containers from the DOM and sends their position data to the editor.
26
+ * @private
27
+ * @memberof DotCMSPageEditor
28
+ */
29
+ function setBounds(): void {
30
+ const containers = Array.from(
31
+ document.querySelectorAll('[data-dot-object="container"]')
32
+ ) as HTMLDivElement[];
33
+ const positionData = getPageElementBound(containers);
34
+
35
+ postMessageToEditor({
36
+ action: CUSTOMER_ACTIONS.SET_BOUNDS,
37
+ payload: positionData
38
+ });
39
+ }
40
+
41
+ /**
42
+ * Listens for editor messages and performs corresponding actions based on the received message.
43
+ *
44
+ * @private
45
+ * @memberof DotCMSPageEditor
46
+ */
47
+ export function listenEditorMessages(): void {
48
+ const messageCallback = (event: MessageEvent) => {
49
+ switch (event.data) {
50
+ case NOTIFY_CUSTOMER.EMA_REQUEST_BOUNDS: {
51
+ setBounds();
52
+ break;
53
+ }
54
+ }
55
+
56
+ if (event.data.name === NOTIFY_CUSTOMER.EMA_SCROLL_INSIDE_IFRAME) {
57
+ const direction = event.data.direction;
58
+
59
+ if (
60
+ (window.scrollY === 0 && direction === 'up') ||
61
+ (scrollIsInBottom() && direction === 'down')
62
+ ) {
63
+ // If the iframe scroll is at the top or bottom, do not send anything.
64
+ // This avoids losing the scrollend event.
65
+ return;
66
+ }
67
+
68
+ const scrollY = direction === 'up' ? -120 : 120;
69
+ window.scrollBy({ left: 0, top: scrollY, behavior: 'smooth' });
70
+ }
71
+ };
72
+
73
+ window.addEventListener('message', messageCallback);
74
+
75
+ subscriptions.push({
76
+ type: 'listener',
77
+ event: 'message',
78
+ callback: messageCallback
79
+ });
80
+ }
81
+
82
+ /**
83
+ * Listens for pointer move events and extracts information about the hovered contentlet.
84
+ *
85
+ * @private
86
+ * @memberof DotCMSPageEditor
87
+ */
88
+ export function listenHoveredContentlet(): void {
89
+ const pointerMoveCallback = (event: PointerEvent) => {
90
+ const foundElement = findDotElement(event.target as HTMLElement);
91
+
92
+ if (!foundElement) return;
93
+
94
+ const { x, y, width, height } = foundElement.getBoundingClientRect();
95
+
96
+ const isContainer = foundElement.dataset?.['dotObject'] === 'container';
97
+
98
+ const contentletForEmptyContainer = {
99
+ identifier: 'TEMP_EMPTY_CONTENTLET',
100
+ title: 'TEMP_EMPTY_CONTENTLET',
101
+ contentType: 'TEMP_EMPTY_CONTENTLET_TYPE',
102
+ inode: 'TEMPY_EMPTY_CONTENTLET_INODE',
103
+ widgetTitle: 'TEMP_EMPTY_CONTENTLET',
104
+ baseType: 'TEMP_EMPTY_CONTENTLET',
105
+ onNumberOfPages: 1
106
+ };
107
+
108
+ const contentlet = {
109
+ identifier: foundElement.dataset?.['dotIdentifier'],
110
+ title: foundElement.dataset?.['dotTitle'],
111
+ inode: foundElement.dataset?.['dotInode'],
112
+ contentType: foundElement.dataset?.['dotType'],
113
+ baseType: foundElement.dataset?.['dotBasetype'],
114
+ widgetTitle: foundElement.dataset?.['dotWidgetTitle'],
115
+ onNumberOfPages: foundElement.dataset?.['dotOnNumberOfPages']
116
+ };
117
+
118
+ const vtlFiles = findVTLData(foundElement);
119
+ const contentletPayload = {
120
+ container:
121
+ // Here extract dot-container from contentlet if it is Headless
122
+ // or search in parent container if it is VTL
123
+ foundElement.dataset?.['dotContainer']
124
+ ? JSON.parse(foundElement.dataset?.['dotContainer'])
125
+ : getClosestContainerData(foundElement),
126
+ contentlet: isContainer ? contentletForEmptyContainer : contentlet,
127
+ vtlFiles
128
+ };
129
+
130
+ postMessageToEditor({
131
+ action: CUSTOMER_ACTIONS.SET_CONTENTLET,
132
+ payload: {
133
+ x,
134
+ y,
135
+ width,
136
+ height,
137
+ payload: contentletPayload
138
+ }
139
+ });
140
+ };
141
+
142
+ document.addEventListener('pointermove', pointerMoveCallback);
143
+
144
+ subscriptions.push({
145
+ type: 'listener',
146
+ event: 'pointermove',
147
+ callback: pointerMoveCallback
148
+ });
149
+ }
150
+
151
+ /**
152
+ * Attaches a scroll event listener to the window
153
+ * and sends a message to the editor when the window is scrolled.
154
+ *
155
+ * @private
156
+ * @memberof DotCMSPageEditor
157
+ */
158
+ export function scrollHandler(): void {
159
+ const scrollCallback = () => {
160
+ postMessageToEditor({
161
+ action: CUSTOMER_ACTIONS.IFRAME_SCROLL
162
+ });
163
+ window.lastScrollYPosition = window.scrollY;
164
+ };
165
+
166
+ const scrollEndCallback = () => {
167
+ postMessageToEditor({
168
+ action: CUSTOMER_ACTIONS.IFRAME_SCROLL_END
169
+ });
170
+ };
171
+
172
+ window.addEventListener('scroll', scrollCallback);
173
+ window.addEventListener('scrollend', scrollEndCallback);
174
+
175
+ subscriptions.push({
176
+ type: 'listener',
177
+ event: 'scroll',
178
+ callback: scrollEndCallback
179
+ });
180
+
181
+ subscriptions.push({
182
+ type: 'listener',
183
+ event: 'scroll',
184
+ callback: scrollCallback
185
+ });
186
+ }
187
+
188
+ /**
189
+ * Restores the scroll position of the window when an iframe is loaded.
190
+ * Only used in VTL Pages.
191
+ * @export
192
+ * @example
193
+ * ```ts
194
+ * preserveScrollOnIframe();
195
+ * ```
196
+ */
197
+ export function preserveScrollOnIframe(): void {
198
+ const preserveScrollCallback = () => {
199
+ window.scrollTo(0, window.lastScrollYPosition);
200
+ };
201
+
202
+ window.addEventListener('load', preserveScrollCallback);
203
+ subscriptions.push({
204
+ type: 'listener',
205
+ event: 'scroll',
206
+ callback: preserveScrollCallback
207
+ });
208
+ }
209
+
210
+ /**
211
+ * Sends a message to the editor to get the page data.
212
+ * @param {string} pathname - The pathname of the page.
213
+ * @private
214
+ * @memberof DotCMSPageEditor
215
+ */
216
+ export function fetchPageDataFromInsideUVE(pathname: string) {
217
+ postMessageToEditor({
218
+ action: CUSTOMER_ACTIONS.GET_PAGE_DATA,
219
+ payload: {
220
+ pathname
221
+ }
222
+ });
223
+ }
@@ -0,0 +1,84 @@
1
+ /**
2
+ * Actions send to the dotcms editor
3
+ *
4
+ * @export
5
+ * @enum {number}
6
+ */
7
+ export enum CUSTOMER_ACTIONS {
8
+ /**
9
+ * Tell the dotcms editor that page change
10
+ */
11
+ NAVIGATION_UPDATE = 'set-url',
12
+ /**
13
+ * Send the element position of the rows, columnsm containers and contentlets
14
+ */
15
+ SET_BOUNDS = 'set-bounds',
16
+ /**
17
+ * Send the information of the hovered contentlet
18
+ */
19
+ SET_CONTENTLET = 'set-contentlet',
20
+ /**
21
+ * Tell the editor that the page is being scrolled
22
+ */
23
+ IFRAME_SCROLL = 'scroll',
24
+ /**
25
+ * Tell the editor that the page has stopped scrolling
26
+ */
27
+ IFRAME_SCROLL_END = 'scroll-end',
28
+ /**
29
+ * Ping the editor to see if the page is inside the editor
30
+ */
31
+ PING_EDITOR = 'ping-editor',
32
+ /**
33
+ * Tell the editor to init the inline editing editor.
34
+ */
35
+ INIT_INLINE_EDITING = 'init-inline-editing',
36
+ /**
37
+ * Tell the editor to open the Copy-contentlet dialog
38
+ * To copy a content and then edit it inline.
39
+ */
40
+ COPY_CONTENTLET_INLINE_EDITING = 'copy-contentlet-inline-editing',
41
+ /**
42
+ * Tell the editor to save inline edited contentlet
43
+ */
44
+ UPDATE_CONTENTLET_INLINE_EDITING = 'update-contentlet-inline-editing',
45
+ /**
46
+ * Tell the editor to trigger a menu reorder
47
+ */
48
+ REORDER_MENU = 'reorder-menu',
49
+ /**
50
+ * Tell the editor to send the page info to iframe
51
+ */
52
+ GET_PAGE_DATA = 'get-page-data',
53
+ /**
54
+ * Tell the editor an user send a graphql query
55
+ */
56
+ CLIENT_READY = 'client-ready',
57
+ /**
58
+ * Tell the editor to do nothing
59
+ */
60
+ NOOP = 'noop'
61
+ }
62
+
63
+ /**
64
+ * Post message props
65
+ *
66
+ * @export
67
+ * @template T
68
+ * @interface PostMessageProps
69
+ */
70
+ type PostMessageProps<T> = {
71
+ action: CUSTOMER_ACTIONS;
72
+ payload?: T;
73
+ };
74
+
75
+ /**
76
+ * Post message to dotcms page editor
77
+ *
78
+ * @export
79
+ * @template T
80
+ * @param {PostMessageProps<T>} message
81
+ */
82
+ export function postMessageToEditor<T = unknown>(message: PostMessageProps<T>) {
83
+ window.parent.postMessage(message, '*');
84
+ }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * @description Custom client parameters for fetching data.
3
+ */
4
+ export type CustomClientParams = {
5
+ depth: string;
6
+ };
7
+
8
+ /**
9
+ * @description Union type for fetch configurations.
10
+ * @typedef {GraphQLFetchConfig | PageAPIFetchConfig} DotCMSFetchConfig
11
+ */
12
+ export type EditorConfig =
13
+ | {
14
+ params: CustomClientParams;
15
+ }
16
+ | {
17
+ query: string;
18
+ };
19
+
20
+ /**
21
+ * Represents the configuration options for the DotCMS page editor.
22
+ * @export
23
+ * @interface DotCMSPageEditorConfig
24
+ */
25
+ export interface DotCMSPageEditorConfig {
26
+ /**
27
+ * The pathname of the page being edited. Optional.
28
+ * @type {string}
29
+ */
30
+ pathname: string;
31
+ /**
32
+ *
33
+ * @type {DotCMSFetchConfig}
34
+ * @memberof DotCMSPageEditorConfig
35
+ * @description The configuration custom params for data fetching on Edit Mode.
36
+ * @example <caption>Example with Custom GraphQL query</caption>
37
+ * const config: DotCMSPageEditorConfig = {
38
+ * editor: { query: 'query { ... }' }
39
+ * };
40
+ *
41
+ * @example <caption>Example usage with Custom Page API parameters</caption>
42
+ * const config: DotCMSPageEditorConfig = {
43
+ * editor: { params: { depth: '2' } }
44
+ * };
45
+ */
46
+ editor?: EditorConfig;
47
+ /**
48
+ * The reload function to call when the page is reloaded.
49
+ * @deprecated In future implementation we will be listening for the changes from the editor to update the page state so reload will not be needed.
50
+ * @type {Function}
51
+ */
52
+ onReload?: () => void;
53
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Actions received from the dotcms editor
3
+ *
4
+ * @export
5
+ * @enum {number}
6
+ */
7
+ export enum NOTIFY_CUSTOMER {
8
+ /**
9
+ * Request to page to reload
10
+ */
11
+ EMA_RELOAD_PAGE = 'ema-reload-page',
12
+ /**
13
+ * Request the bounds for the elements
14
+ */
15
+ EMA_REQUEST_BOUNDS = 'ema-request-bounds',
16
+ /**
17
+ * Received pong from the editor
18
+ */
19
+ EMA_EDITOR_PONG = 'ema-editor-pong',
20
+ /**
21
+ * Received scroll event trigger from the editor
22
+ */
23
+ EMA_SCROLL_INSIDE_IFRAME = 'scroll-inside-iframe'
24
+ }
25
+
26
+ type ListenerCallbackMessage = (event: MessageEvent) => void;
27
+ type ListenerCallbackPointer = (event: PointerEvent) => void;
28
+
29
+ /**
30
+ * Listener for the dotcms editor
31
+ *
32
+ * @interface DotCMSPageEditorListener
33
+ */
34
+ interface DotCMSPageEditorListener {
35
+ type: 'listener';
36
+ event: string;
37
+ callback: ListenerCallbackMessage | ListenerCallbackPointer;
38
+ }
39
+
40
+ /**
41
+ * Observer for the dotcms editor
42
+ *
43
+ * @interface DotCMSPageEditorObserver
44
+ */
45
+ interface DotCMSPageEditorObserver {
46
+ type: 'observer';
47
+ observer: MutationObserver;
48
+ }
49
+
50
+ export type DotCMSPageEditorSubscription = DotCMSPageEditorListener | DotCMSPageEditorObserver;
@@ -0,0 +1,31 @@
1
+ import {
2
+ listenEditorMessages,
3
+ listenHoveredContentlet,
4
+ preserveScrollOnIframe,
5
+ scrollHandler
6
+ } from './listeners/listeners';
7
+ import { isInsideEditor, addClassToEmptyContentlets } from './sdk-editor';
8
+
9
+ declare global {
10
+ interface Window {
11
+ lastScrollYPosition: number;
12
+ }
13
+ }
14
+
15
+ /**
16
+ * This is the main entry point for the SDK VTL.
17
+ * This is added to VTL Script in the EditPage
18
+ *
19
+ * @remarks
20
+ * This module sets up the necessary listeners and functionality for the SDK VTL.
21
+ * It checks if the script is running inside the editor and then initializes the client by pinging the editor,
22
+ * listening for editor messages, hovered contentlet changes, and content changes.
23
+ *
24
+ */
25
+ if (isInsideEditor()) {
26
+ listenEditorMessages();
27
+ scrollHandler();
28
+ preserveScrollOnIframe();
29
+ listenHoveredContentlet();
30
+ addClassToEmptyContentlets();
31
+ }
@@ -0,0 +1,116 @@
1
+ import {
2
+ listenEditorMessages,
3
+ listenHoveredContentlet,
4
+ fetchPageDataFromInsideUVE,
5
+ scrollHandler
6
+ } from './listeners/listeners';
7
+ import { postMessageToEditor, CUSTOMER_ACTIONS } from './models/client.model';
8
+ import {
9
+ addClassToEmptyContentlets,
10
+ initEditor,
11
+ isInsideEditor,
12
+ updateNavigation
13
+ } from './sdk-editor';
14
+
15
+ jest.mock('./models/client.model', () => ({
16
+ postMessageToEditor: jest.fn(),
17
+ CUSTOMER_ACTIONS: {
18
+ NAVIGATION_UPDATE: 'set-url',
19
+ SET_BOUNDS: 'set-bounds',
20
+ SET_CONTENTLET: 'set-contentlet',
21
+ IFRAME_SCROLL: 'scroll',
22
+ PING_EDITOR: 'ping-editor',
23
+ CONTENT_CHANGE: 'content-change',
24
+ NOOP: 'noop'
25
+ }
26
+ }));
27
+
28
+ jest.mock('./listeners/listeners', () => ({
29
+ pingEditor: jest.fn(),
30
+ listenEditorMessages: jest.fn(),
31
+ listenHoveredContentlet: jest.fn(),
32
+ scrollHandler: jest.fn(),
33
+ listenContentChange: jest.fn(),
34
+ fetchPageDataFromInsideUVE: jest.fn()
35
+ }));
36
+
37
+ describe('DotCMSPageEditor', () => {
38
+ describe('is NOT inside editor', () => {
39
+ beforeEach(() => {
40
+ const mockWindow = {
41
+ ...window,
42
+ parent: window
43
+ };
44
+
45
+ const spy = jest.spyOn(global, 'window', 'get');
46
+ spy.mockReturnValueOnce(mockWindow as unknown as Window & typeof globalThis);
47
+ });
48
+
49
+ afterEach(() => {
50
+ jest.clearAllMocks();
51
+ });
52
+
53
+ it('should initialize without any listener', () => {
54
+ const addEventListenerSpy = jest.spyOn(window, 'addEventListener');
55
+
56
+ expect(isInsideEditor()).toBe(false);
57
+ expect(addEventListenerSpy).not.toHaveBeenCalled();
58
+ });
59
+ });
60
+
61
+ describe('is inside editor', () => {
62
+ beforeEach(() => {
63
+ const mockWindow = {
64
+ ...window,
65
+ parent: null
66
+ };
67
+
68
+ const spy = jest.spyOn(global, 'window', 'get');
69
+ spy.mockReturnValue(mockWindow as unknown as Window & typeof globalThis);
70
+ });
71
+
72
+ afterEach(() => {
73
+ jest.clearAllMocks();
74
+ });
75
+
76
+ it('should initialize properly', () => {
77
+ expect(isInsideEditor()).toBe(true);
78
+ });
79
+
80
+ it('should update navigation', () => {
81
+ updateNavigation('/');
82
+ expect(postMessageToEditor).toHaveBeenCalledWith({
83
+ action: CUSTOMER_ACTIONS.NAVIGATION_UPDATE,
84
+ payload: {
85
+ url: 'index'
86
+ }
87
+ });
88
+ });
89
+
90
+ it('should init editor calling listeners', () => {
91
+ initEditor({ pathname: 'some-url' });
92
+ expect(fetchPageDataFromInsideUVE).toHaveBeenCalledWith('some-url');
93
+ expect(listenEditorMessages).toHaveBeenCalled();
94
+ expect(listenHoveredContentlet).toHaveBeenCalled();
95
+ expect(scrollHandler).toHaveBeenCalled();
96
+ });
97
+ });
98
+
99
+ describe('Add Class to Empty Contentets', () => {
100
+ it('should add class to empty contentlets', () => {
101
+ const contentlet = document.createElement('div');
102
+ contentlet.setAttribute('data-dot-object', 'contentlet');
103
+ Object.defineProperty(contentlet, 'clientHeight', { value: 100 }); // Emulate a contentlet with height in the DOM
104
+ document.body.appendChild(contentlet);
105
+
106
+ const emptyContentlet = document.createElement('div');
107
+ emptyContentlet.setAttribute('data-dot-object', 'contentlet');
108
+ document.body.appendChild(emptyContentlet);
109
+
110
+ addClassToEmptyContentlets();
111
+
112
+ expect(emptyContentlet.classList.contains('empty-contentlet')).toBe(true);
113
+ expect(contentlet.classList.contains('empty-contentlet')).toBe(false);
114
+ });
115
+ });
116
+ });
@@ -0,0 +1,105 @@
1
+ import {
2
+ fetchPageDataFromInsideUVE,
3
+ listenEditorMessages,
4
+ listenHoveredContentlet,
5
+ scrollHandler,
6
+ subscriptions
7
+ } from './listeners/listeners';
8
+ import { CUSTOMER_ACTIONS, postMessageToEditor } from './models/client.model';
9
+ import { DotCMSPageEditorConfig } from './models/editor.model';
10
+
11
+ /**
12
+ * Updates the navigation in the editor.
13
+ *
14
+ * @param {string} pathname - The pathname to update the navigation with.
15
+ * @memberof DotCMSPageEditor
16
+ * @example
17
+ * updateNavigation('/home'); // Sends a message to the editor to update the navigation to '/home'
18
+ */
19
+ export function updateNavigation(pathname: string): void {
20
+ postMessageToEditor({
21
+ action: CUSTOMER_ACTIONS.NAVIGATION_UPDATE,
22
+ payload: {
23
+ url: pathname === '/' ? 'index' : pathname?.replace('/', '')
24
+ }
25
+ });
26
+ }
27
+
28
+ /**
29
+ * Checks if the code is running inside an editor.
30
+ *
31
+ * @returns {boolean} Returns true if the code is running inside an editor, otherwise false.
32
+ * @example
33
+ * ```ts
34
+ * if (isInsideEditor()) {
35
+ * console.log('Running inside the editor');
36
+ * } else {
37
+ * console.log('Running outside the editor');
38
+ * }
39
+ * ```
40
+ */
41
+ export function isInsideEditor(): boolean {
42
+ if (typeof window === 'undefined') {
43
+ return false;
44
+ }
45
+
46
+ return window.parent !== window;
47
+ }
48
+
49
+ /**
50
+ * Initializes the DotCMS page editor.
51
+ *
52
+ * @param {DotCMSPageEditorConfig} config - Optional configuration for the editor.
53
+ * @example
54
+ * ```ts
55
+ * const config = { pathname: '/home' };
56
+ * initEditor(config); // Initializes the editor with the provided configuration
57
+ * ```
58
+ */
59
+ export function initEditor(config: DotCMSPageEditorConfig): void {
60
+ fetchPageDataFromInsideUVE(config.pathname);
61
+ listenEditorMessages();
62
+ listenHoveredContentlet();
63
+ scrollHandler();
64
+ }
65
+
66
+ /**
67
+ * Destroys the editor by removing event listeners and disconnecting observers.
68
+ *
69
+ * @example
70
+ * ```ts
71
+ * destroyEditor(); // Cleans up the editor by removing all event listeners and disconnecting observers
72
+ * ```
73
+ */
74
+ export function destroyEditor(): void {
75
+ subscriptions.forEach((subscription) => {
76
+ if (subscription.type === 'listener') {
77
+ window.removeEventListener(subscription.event, subscription.callback as EventListener);
78
+ }
79
+
80
+ if (subscription.type === 'observer') {
81
+ subscription.observer.disconnect();
82
+ }
83
+ });
84
+ }
85
+
86
+ /**
87
+ * Adds a style class to empty contentlets.
88
+ *
89
+ * @export
90
+ * @example
91
+ * ```ts
92
+ * addClassToEmptyContentlets(); // Adds the 'empty-contentlet' class to all contentlets that have no height
93
+ * ```
94
+ */
95
+ export function addClassToEmptyContentlets(): void {
96
+ const contentlets = document.querySelectorAll('[data-dot-object="contentlet"]');
97
+
98
+ contentlets.forEach((contentlet) => {
99
+ if (contentlet.clientHeight) {
100
+ return;
101
+ }
102
+
103
+ contentlet.classList.add('empty-contentlet');
104
+ });
105
+ }