@livepreso/content-react 0.7.2 → 1.0.0

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/lib/utils.js ADDED
@@ -0,0 +1,152 @@
1
+ import { CWE_JSON_KEY } from "./constants";
2
+
3
+ /**
4
+ * Creates the base key for a CWE JSON field.
5
+ * Note: This does not apply any modifications required by the PM template
6
+ * and user template uses of CWE JSON. For those, you will need to run the
7
+ * return value of this key through `getPMTemplateFieldKey` or `getUserTemplateFieldKey`.
8
+ *
9
+ * @param {String} slideKey
10
+ * @returns {String} - Composed base key used for CWE JSON (aka. Companywide JSON)
11
+ */
12
+ export function getCWEJSONKey(slideKey) {
13
+ if (!slideKey) {
14
+ throw new Error("'slideKey' is required");
15
+ }
16
+
17
+ return `${slideKey}-${CWE_JSON_KEY}`;
18
+ }
19
+
20
+ /**
21
+ * Checks CWE keys for the current slide for the relevant CWE key.
22
+ * CWE JSON key is modified as required for PM template and user template.
23
+ *
24
+ * @param {Object} slideState - slide details returned by `useSlide()` hook.
25
+ * @param {String} slideState.slideID
26
+ * @param {String} slideState.slideKey
27
+ * @param {Boolean} slideState.isPMTemplate
28
+ * @param {Boolean} slideState.isUserTemplate
29
+ * @returns
30
+ */
31
+ export function testCWEJSONExists({
32
+ slideID,
33
+ slideKey,
34
+ isPMTemplate,
35
+ isUserTemplate,
36
+ } = {}) {
37
+ const key = getCWEJSONKey(slideKey);
38
+ const cweKeys = window.Bridge.Companywide?.getSlideKeys?.() || [];
39
+
40
+ const presomanagerTemplateKey = getPMTemplateFieldKey(key, slideID);
41
+ const userTemplateKey = getUserTemplateFieldKey(key, slideID);
42
+
43
+ if (isUserTemplate) return cweKeys.includes(userTemplateKey);
44
+ if (isPMTemplate) return cweKeys.includes(presomanagerTemplateKey);
45
+ return cweKeys.includes(key);
46
+ }
47
+
48
+ /**
49
+ * Captures all the digits within the slide name,
50
+ * prefaced by a hyphen and optional trailed by -temp.
51
+ *
52
+ * Results are used when composing field keys for PM templates
53
+ * and user templates, eg. getPMTemplateFieldKey() and getUserTemplateFieldKey().
54
+ */
55
+ function getSlideNumericID(slideID) {
56
+ const numericRegex = /-(\d+)(?:-temp)*$/;
57
+ return slideID.match(numericRegex)?.[1];
58
+ }
59
+
60
+ /**
61
+ * Constructs a PM template field key based on the provided key and slide details.
62
+ *
63
+ * Slide ID variations:
64
+ * saved PM template: template-slide-11
65
+ * unsaved PM template: template-slide-1120-temp
66
+ *
67
+ * Constructed field keys:
68
+ * saved PM template: key-autoadjunct-11
69
+ * unsaved PM template: key-autoadjunct-1120-temp
70
+ *
71
+ * @param {string} key - The base key of the target field.
72
+ * @returns {string} - The constructed field key for the PM template.
73
+ */
74
+ export function getPMTemplateFieldKey(key, slideID) {
75
+ const slideNumericID = getSlideNumericID(slideID);
76
+ // example: image_plus_text_columns-options-autoadjunct-767-temp
77
+ const isUnsavedTemplate = slideID.indexOf("-temp") > -1;
78
+ const templateFieldKey = `${key}-autoadjunct-${slideNumericID}`;
79
+
80
+ if (isUnsavedTemplate) {
81
+ return `${templateFieldKey}-temp`;
82
+ }
83
+
84
+ return templateFieldKey;
85
+ }
86
+
87
+ /**
88
+ * Constructs a user template field key based on the provided key and slide details.
89
+ *
90
+ * Slide ID variations:
91
+ * saved user template: adjunct-template-slide-415
92
+ * unsaved user template: adjunct-template-slide-tempkey-1168
93
+ *
94
+ * Constructed field keys:
95
+ * saved user template: key-tempkey-11
96
+ * unsaved user template: key-adjunct-1120
97
+ *
98
+ * @param {string} key - The base key of the target field.
99
+ * @returns {string} - The constructed field key for the user template.
100
+ */
101
+ export function getUserTemplateFieldKey(key, slideID) {
102
+ const slideNumericID = getSlideNumericID(slideID);
103
+ const isUnsavedTemplate = slideID.indexOf("-tempkey") > -1;
104
+
105
+ if (isUnsavedTemplate) {
106
+ return `${key}-tempkey-${slideNumericID}`;
107
+ }
108
+
109
+ return `${key}-adjunct-${slideNumericID}`;
110
+ }
111
+
112
+ /**
113
+ * Returns PM template or standard slide companywide editable data.
114
+ *
115
+ * @param {string} fieldKey
116
+ * @returns {object} - Saved PM editable field data
117
+ */
118
+ export function getCWEData(fieldKey) {
119
+ const cweVals = window.Bridge.Companywide?.getWithKeys([fieldKey]) || [];
120
+ return cweVals.find((vals) => vals.key === fieldKey) || {};
121
+ }
122
+
123
+ /**
124
+ * Finds value saved to requested user template field. This function expects the supplied
125
+ * key to have been generated by the useEditableFieldKey() hook, this is to ensure the correct
126
+ * parent slide information has been included in the key.
127
+ *
128
+ * Note: field keys are prefixed with the slide ID when saved to the Context (_userTemplateCMSvals)
129
+ * however they are not prefixed when used for the data-companywide-editable attribute.
130
+ *
131
+ * Constructed field keys:
132
+ * saved user template: slideID--key-tempkey-415
133
+ * unsaved user template: slideID--key-adjunct-1168
134
+ *
135
+ * @param {string} key - The base key of the target field.
136
+ * @returns {object} - information saved to the requested user template field.
137
+ */
138
+ export function getUserTemplateData(key, slideID) {
139
+ let fieldKey = key;
140
+
141
+ // Add slideID prefix if not included in key
142
+ if (!fieldKey.startsWith(`${slideID}--`)) {
143
+ fieldKey = `${slideID}--${key}`;
144
+ }
145
+
146
+ const userValues =
147
+ window.Bridge.Context?.getDict()._userTemplateCMSvals || {};
148
+
149
+ return (
150
+ Object.values(userValues)?.find?.((vals) => vals.key === fieldKey) || {}
151
+ );
152
+ }
package/package.json CHANGED
@@ -1,30 +1,30 @@
1
1
  {
2
- "name": "@livepreso/content-react",
3
- "version": "0.7.2",
4
- "description": "",
5
- "main": "index.js",
6
- "scripts": {
7
- "build": ""
8
- },
9
- "publishConfig": {
10
- "access": "public"
11
- },
12
- "keywords": [],
13
- "author": "",
14
- "license": "ISC",
15
- "devDependencies": {
16
- "@livepreso/eslint-config": "workspace:*",
17
- "eslint": "~9.15.0",
18
- "@babel/preset-react": "~7.25.9"
19
- },
20
- "dependencies": {
21
- "react": "~18.2.0",
22
- "react-dom": "~18.2.0",
23
- "@livepreso/content-base": "workspace:~1.1.8",
24
- "ulog": "~2.0.0-beta.18",
25
- "lodash.get": "~4.4.2",
26
- "lodash.isplainobject": "~4.0.6",
27
- "lodash.set": "~4.3.2",
28
- "lodash.unescape": "~4.0.1"
29
- }
30
- }
2
+ "name": "@livepreso/content-react",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "publishConfig": {
7
+ "access": "public"
8
+ },
9
+ "keywords": [],
10
+ "author": "",
11
+ "license": "ISC",
12
+ "devDependencies": {
13
+ "eslint": "~9.15.0",
14
+ "@babel/preset-react": "~7.25.9",
15
+ "@livepreso/eslint-config": "1.0.0"
16
+ },
17
+ "dependencies": {
18
+ "react": "~18.2.0",
19
+ "react-dom": "~18.2.0",
20
+ "@livepreso/content-base": "~1.1.8",
21
+ "ulog": "~2.0.0-beta.18",
22
+ "lodash.get": "~4.4.2",
23
+ "lodash.isplainobject": "~4.0.6",
24
+ "lodash.set": "~4.3.2",
25
+ "lodash.unescape": "~4.0.1"
26
+ },
27
+ "scripts": {
28
+ "build": ""
29
+ }
30
+ }
@@ -1,50 +0,0 @@
1
- import { useCallback, useEffect, useState } from "react";
2
-
3
- import { useSlide } from "../hooks/use-slide";
4
-
5
- /**
6
- * Returns information about a prep editable on the current slide.
7
- *
8
- * Example:
9
- *
10
- * usePrepEditable("test");
11
- * => {
12
- * key: "myslide--myslide-test,
13
- * value: "User edited text,
14
- * hasValue: true
15
- * }
16
- */
17
- export function usePrepEditable(id) {
18
- const { slideKey } = useSlide();
19
-
20
- // TODO: This won't work with user templates, and might
21
- // have problems with PM templates too. Fix this.
22
- const prepKey = `${slideKey}--${slideKey}-${id}`;
23
- const keyPath = `_editableContent.${prepKey}.value`;
24
-
25
- const getValue = useCallback(() => {
26
- return Bridge.Context.get(keyPath, null);
27
- }, [keyPath]);
28
-
29
- const [value, setValue] = useState(getValue());
30
-
31
- useEffect(() => {
32
- function onContextChange() {
33
- const newValue = getValue();
34
-
35
- if (value === newValue) {
36
- return;
37
- }
38
-
39
- setValue(newValue);
40
- }
41
-
42
- Bridge.presenter.getPresentation().on("change:context", onContextChange);
43
-
44
- return () => {
45
- Bridge.presenter.getPresentation().off("change:context", onContextChange);
46
- };
47
- }, [value, getValue]);
48
-
49
- return { key: prepKey, value, hasValue: value !== null };
50
- }