@livepreso/content-react 0.3.8 → 0.3.9

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.
@@ -1,5 +1,5 @@
1
1
  {
2
- "../../content/content-react": "../../content/content-react:NqGkXPM43/K6D7T3XLbV5Ej0Y3f363/umU3YZtBBCIw=:",
2
+ "../../content/content-react": "../../content/content-react:EzqiqRduP5X0d1LxP9lAJ1h2NzQwqXXZ5qngKBr1MMQ=:",
3
3
  "/@babel/helper-annotate-as-pure/7.16.7": "sha512-s6t2w/IPQVTAET1HitoowRGXooX8mCgtuP5195wD/QJPV6wYjpujCGF7JuMODVX2ZAJOf1GT6DT9MHEZvLOFSw==",
4
4
  "/@babel/helper-module-imports/7.16.7": "sha512-LVtS6TqjJHFc+nYeITRo6VLXve70xmq7wPhWTqDJusJEgGmkAACWwMiTNrvfoQo6hEhFwAIixNkvB0jPXDL8Wg==",
5
5
  "/@babel/helper-plugin-utils/7.16.7": "sha512-Qg3Nk7ZxpgMrsox6HreY1ZNKdBq7K72tDSliA6dCl5f007jR4ne8iD5UzuNnCJH2xBf2BEEVGr+/OL6Gdp7RxA==",
package/CHANGELOG.json CHANGED
@@ -1,6 +1,18 @@
1
1
  {
2
2
  "name": "@livepreso/content-react",
3
3
  "entries": [
4
+ {
5
+ "version": "0.3.9",
6
+ "tag": "@livepreso/content-react_v0.3.9",
7
+ "date": "Fri, 23 Aug 2024 05:02:46 GMT",
8
+ "comments": {
9
+ "patch": [
10
+ {
11
+ "comment": "Company wide editables fixes"
12
+ }
13
+ ]
14
+ }
15
+ },
4
16
  {
5
17
  "version": "0.3.8",
6
18
  "tag": "@livepreso/content-react_v0.3.8",
package/CHANGELOG.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # Change Log - @livepreso/content-react
2
2
 
3
- This log was last generated on Wed, 24 Jul 2024 06:53:26 GMT and should not be manually modified.
3
+ This log was last generated on Fri, 23 Aug 2024 05:02:46 GMT and should not be manually modified.
4
+
5
+ ## 0.3.9
6
+ Fri, 23 Aug 2024 05:02:46 GMT
7
+
8
+ ### Patches
9
+
10
+ - Company wide editables fixes
4
11
 
5
12
  ## 0.3.8
6
13
  Wed, 24 Jul 2024 06:53:26 GMT
@@ -1,28 +1,25 @@
1
- import { CompanywideJSONContext } from "../contexts.js";
2
1
  import React, { useMemo, useRef, useState } from "react";
3
2
 
3
+ import { CWE_JSON_KEY } from "../constants.js";
4
+ import { CompanywideJSONContext } from "../contexts.js";
4
5
  import unescape from "lodash.unescape";
6
+ import { useEditableInitialState } from "../hooks/use-editable-initial-state.js";
7
+ import { useSlide } from "../hooks/use-slide.js";
5
8
 
6
9
  export function CompanywideJSON({ children }) {
7
- const cweJsonKey = "cwe-json"
10
+ const { slideKey } = useSlide();
8
11
  const editableRef = useRef();
9
-
10
- const editableName = window.Bridge.Companywide?.getSlideKeysForArticle?.()?.find(
11
- (s) => s.includes(cweJsonKey)
12
+ const [editableValue, editableName] = useEditableInitialState(
13
+ `${slideKey}-${CWE_JSON_KEY}`,
14
+ "{}"
12
15
  );
13
16
 
14
17
  let initialState = {};
15
18
 
16
19
  try {
17
- const result = window.Bridge.Companywide.getWithKeys([editableName]);
18
- const data = result[0];
19
-
20
- initialState = JSON.parse(
21
- unescape(
22
- data?.value || data?.accepted?.value || data?.saved?.value || "{}"
23
- )
24
- );
20
+ initialState = JSON.parse(unescape(editableValue));
25
21
  } catch (e) {
22
+ /* eslint-disable-next-line no-console */
26
23
  console.error(e);
27
24
  // Failed to parse json
28
25
  }
@@ -30,15 +27,10 @@ export function CompanywideJSON({ children }) {
30
27
  const [state, setState] = useState(initialState);
31
28
 
32
29
  function updateField(json) {
33
- setState(json);
34
-
35
- const currEl = document.activeElement;
36
-
30
+ window.$(editableRef.current).text(JSON.stringify(json));
31
+ window.$(editableRef.current).trigger("save");
37
32
  setTimeout(() => {
38
- editableRef.current.focus();
39
- if (currEl) {
40
- currEl.focus();
41
- }
33
+ setState(json);
42
34
  }, 0);
43
35
  }
44
36
 
@@ -63,9 +55,7 @@ export function CompanywideJSON({ children }) {
63
55
  top: -1000,
64
56
  left: -1000,
65
57
  }}
66
- >
67
- {JSON.stringify(state)}
68
- </div>
58
+ />
69
59
  )}
70
60
  {children}
71
61
  </CompanywideJSONContext.Provider>
@@ -0,0 +1 @@
1
+ export const CWE_JSON_KEY = "cwe-json";
package/lib/contexts.js CHANGED
@@ -2,8 +2,11 @@ import React from "react";
2
2
 
3
3
  export const SlideContext = React.createContext({
4
4
  transitions: null,
5
+ slideID: null,
5
6
  slideKey: null,
6
7
  isCurrentSlide: false,
8
+ isUserTemplate: false,
9
+ isPMTemplate: false,
7
10
  });
8
11
 
9
12
  export const SubslidesContext = React.createContext({
@@ -9,3 +9,4 @@ export * from "./use-transitions.js";
9
9
  export * from "./use-subslides.js";
10
10
  export * from "./use-prep-editable.js";
11
11
  export * from "./use-slide-done.js";
12
+ export * from "./use-editable-initial-state.js";
@@ -0,0 +1,113 @@
1
+ import { CWE_JSON_KEY } from "../constants.js";
2
+ import { useSlide } from "./use-slide.js";
3
+
4
+ // captures all the digits within the slide name, prefaced by a hyphen and optional trailed by -temp
5
+ function getSlideNumericID() {
6
+ const { slideID } = useSlide();
7
+ const numericRegex = /-(\d+)(?:-temp)*$/;
8
+ return slideID.match(numericRegex)?.[1];
9
+ }
10
+
11
+ /**
12
+ * Constructs a user template field key based on the provided key and slide details.
13
+ *
14
+ * Slide ID variations:
15
+ * saved user template: adjunct-template-slide-415
16
+ * unsaved user template: adjunct-template-slide-tempkey-1168
17
+ *
18
+ * Constructed field keys:
19
+ * saved user template: slideID--key-tempkey-415
20
+ * unsaved user template: slideID--key-adjunct-1168
21
+ *
22
+ * @param {string} key - The base key of the target field.
23
+ * @returns {string} - The constructed field key for the user template.
24
+ */
25
+ function getUserTemplateFieldKey(key) {
26
+ const { slideID } = useSlide();
27
+ const slideNumericID = getSlideNumericID();
28
+ const isUnsavedTemplate = slideID.indexOf("-tempkey") > -1;
29
+ const keyPrefix = `${slideID}--${key}`;
30
+
31
+ // unsaved user template adds all this on the end
32
+ if (isUnsavedTemplate) {
33
+ return `${keyPrefix}-tempkey-${slideNumericID}`;
34
+ }
35
+
36
+ return `${keyPrefix}-adjunct-${slideNumericID}`;
37
+ }
38
+
39
+ function getUserTemplateData(fieldKey) {
40
+ const userValues = Bridge.Context.getDict()._userTemplateCMSvals;
41
+ return userValues?.find?.((vals) => vals.key === fieldKey) || {};
42
+ }
43
+
44
+ /**
45
+ * Constructs a PM template field key based on the provided key and slide details.
46
+ *
47
+ * Slide ID variations:
48
+ * saved PM template: template-slide-11
49
+ * unsaved PM template: template-slide-1120-temp
50
+ *
51
+ * Constructed field keys:
52
+ * saved PM template: key-autoadjunct-11
53
+ * unsaved PM template: key-autoadjunct-1120-temp
54
+ *
55
+ * @param {string} key - The base key of the target field.
56
+ * @returns {string} - The constructed field key for the PM template.
57
+ */
58
+ function getPMTemplateFieldKey(key) {
59
+ const { slideID, slideKey } = useSlide();
60
+ const isCWEJSON = key.endsWith(CWE_JSON_KEY);
61
+ const slideNumericID = getSlideNumericID();
62
+ // example: image_plus_text_columns-options-autoadjunct-767-temp
63
+ const isUnsavedTemplate = slideID.indexOf("-temp") > -1;
64
+ // "template-" is added to the start of the slideKey of PM templates
65
+ // this is not included in CWE field keys and needs to be removed if
66
+ // slideKey was used to generate the field key request.
67
+ // NOTE: CWE JSON field keys require the "template-" prefix
68
+ const fieldKey =
69
+ !isCWEJSON && key.startsWith(slideKey) && key.startsWith("template-")
70
+ ? key.replace("template-", "")
71
+ : key;
72
+
73
+ if (isUnsavedTemplate) {
74
+ return `${fieldKey}-autoadjunct-${slideNumericID}-temp`;
75
+ }
76
+
77
+ return `${fieldKey}-autoadjunct-${slideNumericID}`;
78
+ }
79
+
80
+ function getCWEData(fieldKey) {
81
+ // PM template or standard slide companywide editable field
82
+ const cweVals = Bridge.Companywide.getWithKeys([fieldKey]);
83
+ return cweVals.find((vals) => vals.key === fieldKey) || {};
84
+ }
85
+
86
+ /**
87
+ * Retrieves the key and value of a requested slide CWE, PM template or user template field.
88
+ * Adjusts the field key as required for user and PM templates.
89
+ *
90
+ * @param {string} key - The base key of the field to retrieve.
91
+ * @param {string} [defaultValue=''] - The default value to use if the field is not found.
92
+ * @returns {Array} - An array containing the value of the field and the adjusted field key.
93
+ */
94
+ export function useEditableInitialState(key, defaultValue = "") {
95
+ const { isUserTemplate, isPMTemplate } = useSlide();
96
+ let fieldKey = key;
97
+ let data;
98
+
99
+ if (isUserTemplate) {
100
+ fieldKey = getUserTemplateFieldKey(key);
101
+ data = getUserTemplateData(fieldKey);
102
+ } else if (isPMTemplate) {
103
+ fieldKey = getPMTemplateFieldKey(key);
104
+ data = getCWEData(fieldKey);
105
+ } else {
106
+ data = getCWEData(fieldKey);
107
+ }
108
+
109
+ const value =
110
+ data?.value || data?.accepted?.value || data?.saved?.value || defaultValue;
111
+
112
+ return [value, fieldKey];
113
+ }
@@ -17,6 +17,8 @@ import { useSlide } from "../hooks/use-slide";
17
17
  export function usePrepEditable(id) {
18
18
  const { slideKey } = useSlide();
19
19
 
20
+ // TODO: This won't work with user templates, and might
21
+ // have problems with PM templates too. Fix this.
20
22
  const prepKey = `${slideKey}--${slideKey}-${id}`;
21
23
  const keyPath = `_editableContent.${prepKey}.value`;
22
24
 
@@ -22,13 +22,14 @@ const DEFAULT_TRANSITION_STATE = {
22
22
  */
23
23
  export function useRootSlide(article) {
24
24
  const { isRemote, isClient, isMaster } = useModes();
25
+ const slideID = article.attr("id");
25
26
  const key = article.data("slide-key");
26
27
  const isCurrent = article.closest(".presoSlide").hasClass("current");
27
28
  const [synced, setSynced] = useState(!isRemote);
28
29
  const [internalSlideState, setInternalSlideState] = useAsyncReference({});
29
30
  const [totalSubslides, setTotalSubslides] = useAsyncReference(0);
30
31
  const [currentSubslide, setCurrentSubslide] = useAsyncReference(
31
- totalSubslides ? 1 : null,
32
+ totalSubslides ? 1 : null
32
33
  );
33
34
  const [subslideAnimating, setSubslideAnimating] = useAsyncReference({
34
35
  up: false,
@@ -139,10 +140,13 @@ export function useRootSlide(article) {
139
140
  return {
140
141
  isSlideReady: slideReady,
141
142
  transitions,
143
+ slideID,
142
144
  slideKey: key,
143
145
  isCurrentSlide: isCurrent,
144
146
  doneRegister,
145
147
  rootElement: article.get(0),
148
+ isUserTemplate: slideID.startsWith("adjunct-template-slide"),
149
+ isPMTemplate: slideID.startsWith("template-slide"),
146
150
 
147
151
  registerSubslide: () => setTotalSubslides(totalSubslides.current + 1),
148
152
  unregisterSubslide: () => setTotalSubslides(totalSubslides.current - 1),
@@ -3,14 +3,28 @@ import { useContext } from "react";
3
3
 
4
4
  export function useSlide() {
5
5
  const {
6
+ slideID,
6
7
  slideKey,
7
8
  isSlideReady,
8
9
  doneRegister,
9
10
  isCurrentSlide,
10
11
  totalSubslides,
11
12
  currentSubslide,
12
- transitions
13
+ transitions,
14
+ isUserTemplate,
15
+ isPMTemplate,
13
16
  } = useContext(SlideContext);
14
17
 
15
- return { slideKey, isSlideReady, doneRegister, isCurrentSlide, currentSubslide, totalSubslides, transitions };
18
+ return {
19
+ slideID,
20
+ slideKey,
21
+ isSlideReady,
22
+ doneRegister,
23
+ isCurrentSlide,
24
+ currentSubslide,
25
+ totalSubslides,
26
+ transitions,
27
+ isUserTemplate,
28
+ isPMTemplate,
29
+ };
16
30
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@livepreso/content-react",
3
- "version": "0.3.8",
3
+ "version": "0.3.9",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "publishConfig": {