@sitecore-jss/sitecore-jss-react 21.3.0-canary.2 → 21.3.0-canary.20

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/README.md CHANGED
@@ -3,8 +3,8 @@
3
3
  This module is provided as a part of Sitecore JavaScript Rendering SDK (JSS). It contains React components and integration for JSS.
4
4
 
5
5
  <!---
6
- @TODO: Update to version 20.0.0 docs before release
6
+ @TODO: Update to next version docs before release
7
7
  -->
8
- [Documentation](https://doc.sitecore.com/xp/en/developers/hd/190/sitecore-headless-development/sitecore-javascript-rendering-sdk--jss--for-react.html)
8
+ [Documentation (Experience Platform)](https://doc.sitecore.com/xp/en/developers/hd/21/sitecore-headless-development/sitecore-javascript-rendering-sdk--jss--for-react.html)
9
9
 
10
- [API reference documentation](/ref-docs/sitecore-jss-react/).
10
+ [API reference documentation](/ref-docs/sitecore-jss-react/)
@@ -44,9 +44,9 @@ exports.FEAAS_COMPONENT_RENDERING_NAME = 'FEaaSComponent';
44
44
  * @param {FEaaSComponentProps} props component props
45
45
  */
46
46
  const FEaaSComponent = (props) => {
47
- var _a, _b, _c, _d, _e, _f;
47
+ var _a, _b, _c, _d, _e, _f, _g;
48
48
  const computedRevision = ((_a = props.params) === null || _a === void 0 ? void 0 : _a.ComponentRevision) || props.revisionFallback;
49
- if ((!props.lastModified || !props.template) &&
49
+ if (!props.template &&
50
50
  (!props.params ||
51
51
  !props.params.LibraryId ||
52
52
  !props.params.ComponentId ||
@@ -59,17 +59,21 @@ const FEaaSComponent = (props) => {
59
59
  let data = null;
60
60
  if ((_b = props.params) === null || _b === void 0 ? void 0 : _b.ComponentDataOverride) {
61
61
  // Use override data if provided
62
- data = props.params.ComponentDataOverride;
62
+ try {
63
+ data = JSON.parse(props.params.ComponentDataOverride);
64
+ }
65
+ catch (e) {
66
+ data = null;
67
+ }
63
68
  }
64
69
  else if (props.fields) {
65
70
  // Otherwise use datasource data (provided in fields)
66
- // FEaaS expects wrapping "_" as catch-all datasource template id
67
- data = JSON.stringify({ _: getDataFromFields(props.fields) });
71
+ data = getDataFromFields(props.fields);
68
72
  }
69
73
  // FEaaS control would still be hydrated by client
70
74
  // we pass all the props as a workaround to avoid hydration error, until we convert all JSS components to server side
71
75
  // this also allows component to fall back to full client-side rendering when template or src is empty
72
- return (react_1.default.createElement(FEAAS.Component, { data: data, template: props.template || '', "last-modified": props.lastModified, cdn: (_c = props.params) === null || _c === void 0 ? void 0 : _c.ComponentHostName, library: (_d = props.params) === null || _d === void 0 ? void 0 : _d.LibraryId, version: (_e = props.params) === null || _e === void 0 ? void 0 : _e.ComponentVersion, component: (_f = props.params) === null || _f === void 0 ? void 0 : _f.ComponentId, revision: computedRevision }));
76
+ return (react_1.default.createElement(FEAAS.Component, { data: data, template: props.template || '', cdn: (_c = props.params) === null || _c === void 0 ? void 0 : _c.ComponentHostName, library: (_d = props.params) === null || _d === void 0 ? void 0 : _d.LibraryId, version: (_e = props.params) === null || _e === void 0 ? void 0 : _e.ComponentVersion, component: (_f = props.params) === null || _f === void 0 ? void 0 : _f.ComponentId, instance: (_g = props.params) === null || _g === void 0 ? void 0 : _g.ComponentInstanceId, revision: computedRevision }));
73
77
  };
74
78
  exports.FEaaSComponent = FEaaSComponent;
75
79
  /**
@@ -84,11 +88,10 @@ function fetchFEaaSComponentServerProps(params, pageState, endpointOverride) {
84
88
  const revisionFallback = pageState && pageState !== layout_1.LayoutServicePageState.Normal ? 'staged' : 'published';
85
89
  const src = endpointOverride || (0, exports.composeComponentEndpoint)(params, revisionFallback);
86
90
  try {
87
- const { template, lastModified } = yield FEAAS.fetchComponent(src);
91
+ const { template } = yield FEAAS.fetchComponent(src);
88
92
  return {
89
93
  revisionFallback,
90
94
  template,
91
- lastModified,
92
95
  };
93
96
  }
94
97
  catch (e) {
@@ -18,8 +18,8 @@ exports.Image = exports.getEEMarkup = void 0;
18
18
  const media_1 = require("@sitecore-jss/sitecore-jss/media");
19
19
  const prop_types_1 = __importDefault(require("prop-types"));
20
20
  const react_1 = __importDefault(require("react"));
21
- const server_1 = __importDefault(require("react-dom/server"));
22
21
  const utils_1 = require("../utils");
22
+ const utils_2 = require("../utils");
23
23
  const getEditableWrapper = (editableMarkup, ...otherProps) => (
24
24
  // create an inline wrapper and use dangerouslySetInnerHTML.
25
25
  // if we try to parse the EE value, the parser will strip invalid or disallowed attributes from html elements - and EE uses several
@@ -61,7 +61,7 @@ const getEEMarkup = (imageField, imageParams, mediaUrlPrefix, otherProps) => {
61
61
  if (!imgAttrs) {
62
62
  return getEditableWrapper(imageField.editable);
63
63
  }
64
- const imgHtml = server_1.default.renderToStaticMarkup(react_1.default.createElement("img", Object.assign({}, imgAttrs)));
64
+ const imgHtml = `<img ${(0, utils_2.getAttributesString)(imgAttrs)} />`;
65
65
  const editableMarkup = imageField.editable.replace(foundImg.imgTag, imgHtml);
66
66
  return getEditableWrapper(editableMarkup);
67
67
  };
@@ -50,36 +50,8 @@ class PlaceholderComponent extends PlaceholderCommon_1.PlaceholderCommon {
50
50
  }
51
51
  const renderingData = childProps.rendering;
52
52
  const placeholderData = PlaceholderCommon_1.PlaceholderCommon.getPlaceholderDataFromRenderingData(renderingData, this.props.name);
53
- this.isEmpty = placeholderData.every((rendering) => isRawRendering(rendering));
54
- // This is a workaround to insert Experience Editor-specific markup on the page for empty placeholders.
55
- // It makes sure the markup is inserted by react and thus avoiding hydration errors when in editing mode
56
- if (this.isEmpty && utils_1.ExperienceEditor.isActive()) {
57
- const codePos = placeholderData.findIndex((component) => component.name === 'code' &&
58
- component.attributes.kind === 'open');
59
- const plhId = placeholderData[codePos].attributes.id;
60
- const emptyPlaceholderMarkup = [
61
- {
62
- name: 'span',
63
- contents: null,
64
- attributes: {
65
- type: 'text/sitecore',
66
- 'sc-part-of': 'placeholder rendering',
67
- style: 'display: none;',
68
- },
69
- },
70
- {
71
- name: 'div',
72
- contents: null,
73
- attributes: {
74
- class: 'scEnabledChrome scEmptyPlaceholder',
75
- 'sc-part-of': 'placeholder',
76
- 'sc-placeholder-id': plhId,
77
- },
78
- },
79
- ];
80
- placeholderData.splice(codePos + 1, 0, ...emptyPlaceholderMarkup);
81
- }
82
53
  const components = this.getComponentsForRenderingData(placeholderData);
54
+ this.isEmpty = placeholderData.every((rendering) => isRawRendering(rendering));
83
55
  if (this.props.renderEmpty && this.isEmpty) {
84
56
  const rendered = this.props.renderEmpty(components);
85
57
  return components.length ? this.renderEmptyPlaceholder(rendered) : rendered;
package/dist/cjs/utils.js CHANGED
@@ -1,6 +1,17 @@
1
1
  "use strict";
2
+ var __rest = (this && this.__rest) || function (s, e) {
3
+ var t = {};
4
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
+ t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
+ t[p[i]] = s[p[i]];
10
+ }
11
+ return t;
12
+ };
2
13
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.addClassName = exports.convertAttributesToReactProps = exports.convertStyleAttribute = exports.convertKebabCasetoCamelCase = void 0;
14
+ exports.getAttributesString = exports.addClassName = exports.convertAttributesToReactProps = exports.convertStyleAttribute = exports.convertKebabCasetoCamelCase = void 0;
4
15
  const style_attr_1 = require("style-attr");
5
16
  // https://stackoverflow.com/a/10426674/9324
6
17
  const convertKebabCasetoCamelCase = (str) => str.replace(/^.|-./g, (letter, index) => index === 0 ? letter.toLowerCase() : letter.substr(1).toUpperCase());
@@ -51,7 +62,7 @@ exports.convertAttributesToReactProps = convertAttributesToReactProps;
51
62
  /**
52
63
  * "class" property will be transformed into or appended to "className" instead.
53
64
  * @param {string} otherAttrs all other props included on the image component
54
- * * @returns {void}
65
+ * @returns {void}
55
66
  */
56
67
  const addClassName = (otherAttrs) => {
57
68
  if (otherAttrs.class) {
@@ -68,3 +79,25 @@ const addClassName = (otherAttrs) => {
68
79
  }
69
80
  };
70
81
  exports.addClassName = addClassName;
82
+ /**
83
+ * Converts the given tag attributes object to a string
84
+ * @param {Object.<string, unknown>} attributes the attributes object
85
+ * @returns {string} string representation of the attributes
86
+ */
87
+ const getAttributesString = (attributes) => {
88
+ const { className } = attributes, restAttributes = __rest(attributes, ["className"]);
89
+ const attributesEntries = Object.entries(restAttributes).map(([key, value]) => {
90
+ if (typeof value === 'object') {
91
+ const valueString = JSON.stringify(value)
92
+ .replace(/"|{|}/g, '')
93
+ .replace(/,/g, ';');
94
+ return `${key}="${valueString}"`;
95
+ }
96
+ return `${key}="${value}"`;
97
+ });
98
+ if (className) {
99
+ attributesEntries.push(`class="${className}"`);
100
+ }
101
+ return attributesEntries.join(' ');
102
+ };
103
+ exports.getAttributesString = getAttributesString;
@@ -15,9 +15,9 @@ export const FEAAS_COMPONENT_RENDERING_NAME = 'FEaaSComponent';
15
15
  * @param {FEaaSComponentProps} props component props
16
16
  */
17
17
  export const FEaaSComponent = (props) => {
18
- var _a, _b, _c, _d, _e, _f;
18
+ var _a, _b, _c, _d, _e, _f, _g;
19
19
  const computedRevision = ((_a = props.params) === null || _a === void 0 ? void 0 : _a.ComponentRevision) || props.revisionFallback;
20
- if ((!props.lastModified || !props.template) &&
20
+ if (!props.template &&
21
21
  (!props.params ||
22
22
  !props.params.LibraryId ||
23
23
  !props.params.ComponentId ||
@@ -30,17 +30,21 @@ export const FEaaSComponent = (props) => {
30
30
  let data = null;
31
31
  if ((_b = props.params) === null || _b === void 0 ? void 0 : _b.ComponentDataOverride) {
32
32
  // Use override data if provided
33
- data = props.params.ComponentDataOverride;
33
+ try {
34
+ data = JSON.parse(props.params.ComponentDataOverride);
35
+ }
36
+ catch (e) {
37
+ data = null;
38
+ }
34
39
  }
35
40
  else if (props.fields) {
36
41
  // Otherwise use datasource data (provided in fields)
37
- // FEaaS expects wrapping "_" as catch-all datasource template id
38
- data = JSON.stringify({ _: getDataFromFields(props.fields) });
42
+ data = getDataFromFields(props.fields);
39
43
  }
40
44
  // FEaaS control would still be hydrated by client
41
45
  // we pass all the props as a workaround to avoid hydration error, until we convert all JSS components to server side
42
46
  // this also allows component to fall back to full client-side rendering when template or src is empty
43
- return (React.createElement(FEAAS.Component, { data: data, template: props.template || '', "last-modified": props.lastModified, cdn: (_c = props.params) === null || _c === void 0 ? void 0 : _c.ComponentHostName, library: (_d = props.params) === null || _d === void 0 ? void 0 : _d.LibraryId, version: (_e = props.params) === null || _e === void 0 ? void 0 : _e.ComponentVersion, component: (_f = props.params) === null || _f === void 0 ? void 0 : _f.ComponentId, revision: computedRevision }));
47
+ return (React.createElement(FEAAS.Component, { data: data, template: props.template || '', cdn: (_c = props.params) === null || _c === void 0 ? void 0 : _c.ComponentHostName, library: (_d = props.params) === null || _d === void 0 ? void 0 : _d.LibraryId, version: (_e = props.params) === null || _e === void 0 ? void 0 : _e.ComponentVersion, component: (_f = props.params) === null || _f === void 0 ? void 0 : _f.ComponentId, instance: (_g = props.params) === null || _g === void 0 ? void 0 : _g.ComponentInstanceId, revision: computedRevision }));
44
48
  };
45
49
  /**
46
50
  * Fetches server component props required for server rendering, based on rendering params.
@@ -54,11 +58,10 @@ export function fetchFEaaSComponentServerProps(params, pageState, endpointOverri
54
58
  const revisionFallback = pageState && pageState !== LayoutServicePageState.Normal ? 'staged' : 'published';
55
59
  const src = endpointOverride || composeComponentEndpoint(params, revisionFallback);
56
60
  try {
57
- const { template, lastModified } = yield FEAAS.fetchComponent(src);
61
+ const { template } = yield FEAAS.fetchComponent(src);
58
62
  return {
59
63
  revisionFallback,
60
64
  template,
61
- lastModified,
62
65
  };
63
66
  }
64
67
  catch (e) {
@@ -12,8 +12,8 @@ var __rest = (this && this.__rest) || function (s, e) {
12
12
  import { mediaApi } from '@sitecore-jss/sitecore-jss/media';
13
13
  import PropTypes from 'prop-types';
14
14
  import React from 'react';
15
- import ReactDOMServer from 'react-dom/server';
16
15
  import { addClassName, convertAttributesToReactProps } from '../utils';
16
+ import { getAttributesString } from '../utils';
17
17
  const getEditableWrapper = (editableMarkup, ...otherProps) => (
18
18
  // create an inline wrapper and use dangerouslySetInnerHTML.
19
19
  // if we try to parse the EE value, the parser will strip invalid or disallowed attributes from html elements - and EE uses several
@@ -55,7 +55,7 @@ export const getEEMarkup = (imageField, imageParams, mediaUrlPrefix, otherProps)
55
55
  if (!imgAttrs) {
56
56
  return getEditableWrapper(imageField.editable);
57
57
  }
58
- const imgHtml = ReactDOMServer.renderToStaticMarkup(React.createElement("img", Object.assign({}, imgAttrs)));
58
+ const imgHtml = `<img ${getAttributesString(imgAttrs)} />`;
59
59
  const editableMarkup = imageField.editable.replace(foundImg.imgTag, imgHtml);
60
60
  return getEditableWrapper(editableMarkup);
61
61
  };
@@ -1,7 +1,7 @@
1
1
  import React from 'react';
2
2
  import { PlaceholderCommon } from './PlaceholderCommon';
3
3
  import { withComponentFactory } from '../enhancers/withComponentFactory';
4
- import { HorizonEditor, ExperienceEditor } from '@sitecore-jss/sitecore-jss/utils';
4
+ import { HorizonEditor } from '@sitecore-jss/sitecore-jss/utils';
5
5
  /**
6
6
  * @param {HtmlElementRendering | ComponentRendering} rendering
7
7
  */
@@ -44,36 +44,8 @@ class PlaceholderComponent extends PlaceholderCommon {
44
44
  }
45
45
  const renderingData = childProps.rendering;
46
46
  const placeholderData = PlaceholderCommon.getPlaceholderDataFromRenderingData(renderingData, this.props.name);
47
- this.isEmpty = placeholderData.every((rendering) => isRawRendering(rendering));
48
- // This is a workaround to insert Experience Editor-specific markup on the page for empty placeholders.
49
- // It makes sure the markup is inserted by react and thus avoiding hydration errors when in editing mode
50
- if (this.isEmpty && ExperienceEditor.isActive()) {
51
- const codePos = placeholderData.findIndex((component) => component.name === 'code' &&
52
- component.attributes.kind === 'open');
53
- const plhId = placeholderData[codePos].attributes.id;
54
- const emptyPlaceholderMarkup = [
55
- {
56
- name: 'span',
57
- contents: null,
58
- attributes: {
59
- type: 'text/sitecore',
60
- 'sc-part-of': 'placeholder rendering',
61
- style: 'display: none;',
62
- },
63
- },
64
- {
65
- name: 'div',
66
- contents: null,
67
- attributes: {
68
- class: 'scEnabledChrome scEmptyPlaceholder',
69
- 'sc-part-of': 'placeholder',
70
- 'sc-placeholder-id': plhId,
71
- },
72
- },
73
- ];
74
- placeholderData.splice(codePos + 1, 0, ...emptyPlaceholderMarkup);
75
- }
76
47
  const components = this.getComponentsForRenderingData(placeholderData);
48
+ this.isEmpty = placeholderData.every((rendering) => isRawRendering(rendering));
77
49
  if (this.props.renderEmpty && this.isEmpty) {
78
50
  const rendered = this.props.renderEmpty(components);
79
51
  return components.length ? this.renderEmptyPlaceholder(rendered) : rendered;
package/dist/esm/utils.js CHANGED
@@ -1,3 +1,14 @@
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
1
12
  import { parse as styleParse } from 'style-attr';
2
13
  // https://stackoverflow.com/a/10426674/9324
3
14
  export const convertKebabCasetoCamelCase = (str) => str.replace(/^.|-./g, (letter, index) => index === 0 ? letter.toLowerCase() : letter.substr(1).toUpperCase());
@@ -45,7 +56,7 @@ export const convertAttributesToReactProps = (attributes) => {
45
56
  /**
46
57
  * "class" property will be transformed into or appended to "className" instead.
47
58
  * @param {string} otherAttrs all other props included on the image component
48
- * * @returns {void}
59
+ * @returns {void}
49
60
  */
50
61
  export const addClassName = (otherAttrs) => {
51
62
  if (otherAttrs.class) {
@@ -61,3 +72,24 @@ export const addClassName = (otherAttrs) => {
61
72
  delete otherAttrs.class;
62
73
  }
63
74
  };
75
+ /**
76
+ * Converts the given tag attributes object to a string
77
+ * @param {Object.<string, unknown>} attributes the attributes object
78
+ * @returns {string} string representation of the attributes
79
+ */
80
+ export const getAttributesString = (attributes) => {
81
+ const { className } = attributes, restAttributes = __rest(attributes, ["className"]);
82
+ const attributesEntries = Object.entries(restAttributes).map(([key, value]) => {
83
+ if (typeof value === 'object') {
84
+ const valueString = JSON.stringify(value)
85
+ .replace(/"|{|}/g, '')
86
+ .replace(/,/g, ';');
87
+ return `${key}="${valueString}"`;
88
+ }
89
+ return `${key}="${value}"`;
90
+ });
91
+ if (className) {
92
+ attributesEntries.push(`class="${className}"`);
93
+ }
94
+ return attributesEntries.join(' ');
95
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sitecore-jss/sitecore-jss-react",
3
- "version": "21.3.0-canary.2",
3
+ "version": "21.3.0-canary.20",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "sideEffects": false,
@@ -62,14 +62,14 @@
62
62
  },
63
63
  "dependencies": {
64
64
  "@sitecore-feaas/clientside": "^0.3.0",
65
- "@sitecore-jss/sitecore-jss": "^21.3.0-canary.2",
65
+ "@sitecore-jss/sitecore-jss": "^21.3.0-canary.20",
66
66
  "deep-equal": "^2.1.0",
67
67
  "prop-types": "^15.8.1",
68
68
  "style-attr": "^1.3.0"
69
69
  },
70
70
  "description": "",
71
71
  "types": "types/index.d.ts",
72
- "gitHead": "a498d1b8b6bab066a58907a2a01a554b9d20bd42",
72
+ "gitHead": "3a9baacdffce61e550e640459137e1adaee16026",
73
73
  "files": [
74
74
  "dist",
75
75
  "types"
@@ -24,10 +24,6 @@ type FEaaSComponentServerProps = {
24
24
  * HTML template for presentation rendered inside the component
25
25
  */
26
26
  template?: string;
27
- /**
28
- * the date component data was last modified
29
- */
30
- lastModified?: string;
31
27
  /**
32
28
  * Default revision to be fetched. Should be 'staged' for editing/preview. Can be overriden by params.ComponentRevision
33
29
  */
package/types/utils.d.ts CHANGED
@@ -18,8 +18,16 @@ export declare const convertAttributesToReactProps: (attributes: {
18
18
  /**
19
19
  * "class" property will be transformed into or appended to "className" instead.
20
20
  * @param {string} otherAttrs all other props included on the image component
21
- * * @returns {void}
21
+ * @returns {void}
22
22
  */
23
23
  export declare const addClassName: (otherAttrs: {
24
24
  [key: string]: unknown;
25
25
  }) => void;
26
+ /**
27
+ * Converts the given tag attributes object to a string
28
+ * @param {Object.<string, unknown>} attributes the attributes object
29
+ * @returns {string} string representation of the attributes
30
+ */
31
+ export declare const getAttributesString: (attributes: {
32
+ [key: string]: unknown;
33
+ }) => string;