@dotcms/uve 0.0.1-beta.4 → 0.0.1-beta.5

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,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var types = require('./types.cjs.js');
4
+ var internal = require('./internal.cjs.js');
4
5
 
5
6
  /**
6
7
  * Gets the current state of the Universal Visual Editor (UVE).
@@ -54,5 +55,30 @@ function getUVEState() {
54
55
  publishDate
55
56
  };
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
+ throw new Error('UVE Subscription: Not running inside UVE');
75
+ }
76
+ const eventCallback = internal.__UVE_EVENTS__[event];
77
+ if (!eventCallback) {
78
+ throw new Error(`UVE Subscription: Event ${event} not found`);
79
+ }
80
+ return eventCallback(callback);
81
+ }
57
82
 
83
+ exports.createUVESubscription = createUVESubscription;
58
84
  exports.getUVEState = getUVEState;
package/index.esm.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { UVE_MODE } from './types.esm.js';
2
+ import { __UVE_EVENTS__ } from './internal.esm.js';
2
3
 
3
4
  /**
4
5
  * Gets the current state of the Universal Visual Editor (UVE).
@@ -52,5 +53,29 @@ function getUVEState() {
52
53
  publishDate
53
54
  };
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
+ throw new Error('UVE Subscription: Not running inside UVE');
73
+ }
74
+ const eventCallback = __UVE_EVENTS__[event];
75
+ if (!eventCallback) {
76
+ throw new Error(`UVE Subscription: Event ${event} not found`);
77
+ }
78
+ return eventCallback(callback);
79
+ }
55
80
 
56
- export { getUVEState };
81
+ export { createUVESubscription, getUVEState };
@@ -0,0 +1 @@
1
+ export * from "./src/internal";
@@ -0,0 +1 @@
1
+ exports._default = require('./internal.cjs.js').default;
@@ -0,0 +1,61 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Actions received from the dotCMS UVE
5
+ *
6
+ * @internal
7
+ * @enum {string}
8
+ */
9
+ exports.__NOTIFY_CLIENT__ = void 0;
10
+ (function (__NOTIFY_CLIENT__) {
11
+ /**
12
+ * Request to page to reload
13
+ */
14
+ __NOTIFY_CLIENT__["UVE_RELOAD_PAGE"] = "uve-reload-page";
15
+ /**
16
+ * Request the bounds for the elements
17
+ */
18
+ __NOTIFY_CLIENT__["UVE_REQUEST_BOUNDS"] = "uve-request-bounds";
19
+ /**
20
+ * Received pong from the editor
21
+ */
22
+ __NOTIFY_CLIENT__["UVE_EDITOR_PONG"] = "uve-editor-pong";
23
+ /**
24
+ * Received scroll event trigger from the editor
25
+ */
26
+ __NOTIFY_CLIENT__["UVE_SCROLL_INSIDE_IFRAME"] = "uve-scroll-inside-iframe";
27
+ /**
28
+ * Set the page data
29
+ */
30
+ __NOTIFY_CLIENT__["UVE_SET_PAGE_DATA"] = "uve-set-page-data";
31
+ /**
32
+ * Copy contentlet inline editing success
33
+ */
34
+ __NOTIFY_CLIENT__["UVE_COPY_CONTENTLET_INLINE_EDITING_SUCCESS"] = "uve-copy-contentlet-inline-editing-success";
35
+ })(exports.__NOTIFY_CLIENT__ || (exports.__NOTIFY_CLIENT__ = {}));
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.__NOTIFY_CLIENT__.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
+ exports.__UVE_EVENTS__ = __UVE_EVENTS__;
@@ -0,0 +1,2 @@
1
+ export * from './internal.cjs.js';
2
+ export { _default as default } from './internal.cjs.default.js';
@@ -0,0 +1 @@
1
+ export * from "./src/internal";
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Actions received from the dotCMS UVE
3
+ *
4
+ * @internal
5
+ * @enum {string}
6
+ */
7
+ var __NOTIFY_CLIENT__;
8
+ (function (__NOTIFY_CLIENT__) {
9
+ /**
10
+ * Request to page to reload
11
+ */
12
+ __NOTIFY_CLIENT__["UVE_RELOAD_PAGE"] = "uve-reload-page";
13
+ /**
14
+ * Request the bounds for the elements
15
+ */
16
+ __NOTIFY_CLIENT__["UVE_REQUEST_BOUNDS"] = "uve-request-bounds";
17
+ /**
18
+ * Received pong from the editor
19
+ */
20
+ __NOTIFY_CLIENT__["UVE_EDITOR_PONG"] = "uve-editor-pong";
21
+ /**
22
+ * Received scroll event trigger from the editor
23
+ */
24
+ __NOTIFY_CLIENT__["UVE_SCROLL_INSIDE_IFRAME"] = "uve-scroll-inside-iframe";
25
+ /**
26
+ * Set the page data
27
+ */
28
+ __NOTIFY_CLIENT__["UVE_SET_PAGE_DATA"] = "uve-set-page-data";
29
+ /**
30
+ * Copy contentlet inline editing success
31
+ */
32
+ __NOTIFY_CLIENT__["UVE_COPY_CONTENTLET_INLINE_EDITING_SUCCESS"] = "uve-copy-contentlet-inline-editing-success";
33
+ })(__NOTIFY_CLIENT__ || (__NOTIFY_CLIENT__ = {}));
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 === __NOTIFY_CLIENT__.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
+ export { __NOTIFY_CLIENT__, __UVE_EVENTS__ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotcms/uve",
3
- "version": "0.0.1-beta.4",
3
+ "version": "0.0.1-beta.5",
4
4
  "description": "Official JavaScript library for interacting with Universal Visual Editor (UVE)",
5
5
  "repository": {
6
6
  "type": "git",
@@ -26,6 +26,12 @@
26
26
  "types": "./types.esm.d.ts",
27
27
  "import": "./types.cjs.mjs",
28
28
  "default": "./types.cjs.js"
29
+ },
30
+ "./internal": {
31
+ "module": "./internal.esm.js",
32
+ "types": "./internal.esm.d.ts",
33
+ "import": "./internal.cjs.mjs",
34
+ "default": "./internal.cjs.js"
29
35
  }
30
36
  },
31
37
  "typesVersions": {
@@ -35,6 +41,9 @@
35
41
  ],
36
42
  "types": [
37
43
  "./src/types.d.ts"
44
+ ],
45
+ "internal": [
46
+ "./src/internal.d.ts"
38
47
  ]
39
48
  }
40
49
  },
package/src/index.d.ts CHANGED
@@ -1,2 +1 @@
1
- import { getUVEState } from './lib/utils';
2
- export { getUVEState };
1
+ export * from './lib/utils';
@@ -0,0 +1,8 @@
1
+ import { UVEEvent } from '../lib/types';
2
+ /**
3
+ * Events that can be subscribed to in the UVE
4
+ *
5
+ * @internal
6
+ * @type {Record<string, UVEEvent>}
7
+ */
8
+ export declare const __UVE_EVENTS__: Record<string, UVEEvent>;
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Actions received from the dotCMS UVE
3
+ *
4
+ * @internal
5
+ * @enum {string}
6
+ */
7
+ export declare enum __NOTIFY_CLIENT__ {
8
+ /**
9
+ * Request to page to reload
10
+ */
11
+ UVE_RELOAD_PAGE = "uve-reload-page",
12
+ /**
13
+ * Request the bounds for the elements
14
+ */
15
+ UVE_REQUEST_BOUNDS = "uve-request-bounds",
16
+ /**
17
+ * Received pong from the editor
18
+ */
19
+ UVE_EDITOR_PONG = "uve-editor-pong",
20
+ /**
21
+ * Received scroll event trigger from the editor
22
+ */
23
+ UVE_SCROLL_INSIDE_IFRAME = "uve-scroll-inside-iframe",
24
+ /**
25
+ * Set the page data
26
+ */
27
+ UVE_SET_PAGE_DATA = "uve-set-page-data",
28
+ /**
29
+ * Copy contentlet inline editing success
30
+ */
31
+ UVE_COPY_CONTENTLET_INLINE_EDITING_SUCCESS = "uve-copy-contentlet-inline-editing-success"
32
+ }
@@ -0,0 +1,2 @@
1
+ export * from './constants';
2
+ export * from './enums';
@@ -0,0 +1 @@
1
+ export * from './internal/index';
@@ -31,3 +31,29 @@ export declare enum UVE_MODE {
31
31
  LIVE = "LIVE",
32
32
  UNKNOWN = "UNKNOWN"
33
33
  }
34
+ /**
35
+ * Callback function for UVE events
36
+ * @callback UVECallback
37
+ * @param {unknown} payload - The payload of the event
38
+ */
39
+ export type UVECallback = (payload: unknown) => void;
40
+ /**
41
+ * Unsubscribe function for UVE events
42
+ * @callback UnsubscribeUVE
43
+ */
44
+ export type UnsubscribeUVE = () => void;
45
+ /**
46
+ * UVESubscription type
47
+ * @typedef {Object} UVESubscription
48
+ * @property {UnsubscribeUVE} unsubscribe - The unsubscribe function for the UVE event
49
+ * @property {string} event - The event name
50
+ */
51
+ export type UVESubscription = {
52
+ unsubscribe: UnsubscribeUVE;
53
+ event: string;
54
+ };
55
+ /**
56
+ * UVE event type
57
+ * @typedef {function} UVEEvent
58
+ */
59
+ export type UVEEvent = (callback: UVECallback) => UVESubscription;
@@ -1,4 +1,4 @@
1
- import { UVEState } from './types';
1
+ import { UVECallback, UVEState, UVESubscription } from './types';
2
2
  /**
3
3
  * Gets the current state of the Universal Visual Editor (UVE).
4
4
  *
@@ -28,3 +28,18 @@ import { UVEState } from './types';
28
28
  * ```
29
29
  */
30
30
  export declare function getUVEState(): UVEState | undefined;
31
+ /**
32
+ * Creates a subscription to a UVE event.
33
+ *
34
+ * @param {string} event - The event to subscribe to.
35
+ * @param {UVECallback} callback - The callback to call when the event is triggered.
36
+ * @return {UnsubscribeUVE | undefined} The unsubscribe function if the event is valid, undefined otherwise.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * const unsubscribeChanges = createUVESubscription('changes', (payload) => {
41
+ * console.log(payload);
42
+ * });
43
+ * ```
44
+ */
45
+ export declare function createUVESubscription(event: string, callback: UVECallback): UVESubscription;
package/src/types.d.ts CHANGED
@@ -1,2 +1 @@
1
- import { UVE_MODE, UVEState } from './lib/types';
2
- export { UVE_MODE, UVEState };
1
+ export * from './lib/types';