@merkur/integration-react 0.29.3 → 0.31.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/index.cjs +46 -125
- package/lib/index.es5.js +1 -1
- package/lib/index.es9.cjs +46 -143
- package/lib/index.es9.mjs +46 -138
- package/lib/index.js +46 -125
- package/lib/index.mjs +46 -125
- package/package.json +6 -6
package/lib/index.mjs
CHANGED
|
@@ -3,20 +3,17 @@ import { getMerkur } from '@merkur/core';
|
|
|
3
3
|
import { loadStyleAssets, loadScriptAssets } from '@merkur/integration';
|
|
4
4
|
|
|
5
5
|
function _extends() {
|
|
6
|
-
_extends = Object.assign
|
|
6
|
+
_extends = Object.assign ? Object.assign.bind() : function (target) {
|
|
7
7
|
for (var i = 1; i < arguments.length; i++) {
|
|
8
8
|
var source = arguments[i];
|
|
9
|
-
|
|
10
9
|
for (var key in source) {
|
|
11
10
|
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
12
11
|
target[key] = source[key];
|
|
13
12
|
}
|
|
14
13
|
}
|
|
15
14
|
}
|
|
16
|
-
|
|
17
15
|
return target;
|
|
18
16
|
};
|
|
19
|
-
|
|
20
17
|
return _extends.apply(this, arguments);
|
|
21
18
|
}
|
|
22
19
|
|
|
@@ -24,6 +21,7 @@ const SelectorIdentifierMap = Object.freeze({
|
|
|
24
21
|
'#': 'id',
|
|
25
22
|
'.': 'className'
|
|
26
23
|
});
|
|
24
|
+
|
|
27
25
|
/**
|
|
28
26
|
* Parses container selector into element property. Currently
|
|
29
27
|
* only works with IDs and classnames.
|
|
@@ -31,28 +29,24 @@ const SelectorIdentifierMap = Object.freeze({
|
|
|
31
29
|
* @param {string} [containerSelector='']
|
|
32
30
|
* @return {{ string: string }} Property name and value tuple.
|
|
33
31
|
*/
|
|
34
|
-
|
|
35
32
|
function selectorToAttribute(containerSelector = '') {
|
|
36
33
|
if (typeof containerSelector !== 'string' || containerSelector.length === 0) {
|
|
37
34
|
return {};
|
|
38
35
|
}
|
|
39
|
-
|
|
40
36
|
let lastIdentifierIndex = -1;
|
|
41
|
-
|
|
42
37
|
for (let identifierKey in SelectorIdentifierMap) {
|
|
43
38
|
let curLastIndex = containerSelector.lastIndexOf(identifierKey);
|
|
44
|
-
|
|
45
39
|
if (lastIdentifierIndex < curLastIndex) {
|
|
46
40
|
lastIdentifierIndex = curLastIndex;
|
|
47
41
|
}
|
|
48
42
|
}
|
|
49
|
-
|
|
50
43
|
const identifier = containerSelector[lastIdentifierIndex];
|
|
51
44
|
const selectorName = containerSelector.substr(lastIdentifierIndex + 1);
|
|
52
45
|
return {
|
|
53
46
|
[SelectorIdentifierMap[identifier]]: selectorName
|
|
54
47
|
};
|
|
55
48
|
}
|
|
49
|
+
|
|
56
50
|
/**
|
|
57
51
|
* Returns empty <div> with SSR html as it's contents (if provided) and
|
|
58
52
|
* container selector defined, which is used as merkur widget wrapper.
|
|
@@ -60,8 +54,6 @@ function selectorToAttribute(containerSelector = '') {
|
|
|
60
54
|
* @param {{ html, containerSelector, children }}
|
|
61
55
|
* @return {import('react').ReactElement}
|
|
62
56
|
*/
|
|
63
|
-
|
|
64
|
-
|
|
65
57
|
function WidgetWrapper({
|
|
66
58
|
html,
|
|
67
59
|
containerSelector,
|
|
@@ -87,51 +79,45 @@ class AbstractMerkurWidget extends React.Component {
|
|
|
87
79
|
static hasWidgetChanged(props, nextProps) {
|
|
88
80
|
return !!(props && props.name && props.version && nextProps && nextProps.name && nextProps.version && (props.version !== nextProps.version || props.name !== nextProps.name));
|
|
89
81
|
}
|
|
82
|
+
|
|
90
83
|
/**
|
|
91
84
|
* Validates widget properties, widget properties should not be an empty object.
|
|
92
85
|
*
|
|
93
86
|
* @param {object} widgetProperties
|
|
94
87
|
* @return {boolean} validation result.
|
|
95
88
|
*/
|
|
96
|
-
|
|
97
|
-
|
|
98
89
|
static validateProperties(widgetProperties) {
|
|
99
90
|
return !!(widgetProperties && widgetProperties.name && widgetProperties.version);
|
|
100
91
|
}
|
|
92
|
+
|
|
101
93
|
/**
|
|
102
94
|
* Widget SSR rendered html (from server response).
|
|
103
95
|
*/
|
|
104
|
-
|
|
105
|
-
|
|
106
96
|
get html() {
|
|
107
97
|
throw new Error('The html getter is abstract and must be overridden');
|
|
108
98
|
}
|
|
99
|
+
|
|
109
100
|
/**
|
|
110
101
|
* Widget container element.
|
|
111
102
|
*/
|
|
112
|
-
|
|
113
|
-
|
|
114
103
|
get container() {
|
|
115
104
|
throw new Error('The container getter is abstract and must be overridden');
|
|
116
105
|
}
|
|
117
|
-
|
|
118
106
|
constructor(props) {
|
|
119
107
|
super(props);
|
|
120
108
|
this._isMounted = false;
|
|
121
109
|
this._html = null;
|
|
122
110
|
}
|
|
123
|
-
|
|
124
111
|
componentDidMount() {
|
|
125
112
|
this._isMounted = true;
|
|
126
113
|
}
|
|
114
|
+
|
|
127
115
|
/**
|
|
128
116
|
* Renders fallback (children) when widget is already mounted
|
|
129
117
|
* but not ready or any error has occurred.
|
|
130
118
|
*
|
|
131
119
|
* @return {React.ReactElement|null}
|
|
132
120
|
*/
|
|
133
|
-
|
|
134
|
-
|
|
135
121
|
_renderFallback() {
|
|
136
122
|
const {
|
|
137
123
|
children
|
|
@@ -139,7 +125,6 @@ class AbstractMerkurWidget extends React.Component {
|
|
|
139
125
|
const {
|
|
140
126
|
encounteredError = null
|
|
141
127
|
} = this.state || {};
|
|
142
|
-
|
|
143
128
|
if (typeof children === 'function') {
|
|
144
129
|
return children({
|
|
145
130
|
error: encounteredError
|
|
@@ -147,66 +132,58 @@ class AbstractMerkurWidget extends React.Component {
|
|
|
147
132
|
} else if ( /*#__PURE__*/React.isValidElement(children)) {
|
|
148
133
|
return children;
|
|
149
134
|
}
|
|
150
|
-
|
|
151
135
|
return null;
|
|
152
136
|
}
|
|
137
|
+
|
|
153
138
|
/**
|
|
154
139
|
* Clears cached SSR rendered html.
|
|
155
140
|
*/
|
|
156
|
-
|
|
157
|
-
|
|
158
141
|
_clearCachedHtml() {
|
|
159
142
|
this._html = null;
|
|
160
143
|
}
|
|
144
|
+
|
|
161
145
|
/**
|
|
162
146
|
* @return {boolean} true in browser environment.
|
|
163
147
|
*/
|
|
164
|
-
|
|
165
|
-
|
|
166
148
|
_isClient() {
|
|
167
149
|
return typeof window !== 'undefined' && typeof document !== 'undefined';
|
|
168
150
|
}
|
|
151
|
+
|
|
169
152
|
/**
|
|
170
153
|
* @return {string} SSR rendered HTML, html from widgetProperties or ''.
|
|
171
154
|
*/
|
|
172
|
-
|
|
173
|
-
|
|
174
155
|
_getWidgetHTML() {
|
|
175
156
|
if (this._html !== null) {
|
|
176
157
|
return this._html;
|
|
177
158
|
}
|
|
178
|
-
|
|
179
159
|
this._html = this.html || this._getSSRHTML();
|
|
180
160
|
return this._html;
|
|
181
161
|
}
|
|
162
|
+
|
|
182
163
|
/**
|
|
183
164
|
* Return server-side rendered html, if its the first render on client
|
|
184
165
|
* after SSR.
|
|
185
166
|
*
|
|
186
167
|
* @return {string} server-side rendered html, if it's not available, return empty string.
|
|
187
168
|
*/
|
|
188
|
-
|
|
189
|
-
|
|
190
169
|
_getSSRHTML() {
|
|
191
170
|
if (!this._isMounted && this._isClient()) {
|
|
192
171
|
return this.container?.children?.[0]?.outerHTML || '';
|
|
193
172
|
}
|
|
194
|
-
|
|
195
173
|
return '';
|
|
196
174
|
}
|
|
175
|
+
|
|
197
176
|
/**
|
|
198
177
|
* Checks if it's the first render after SSR.
|
|
199
178
|
*
|
|
200
179
|
* @return {boolean} true in case of a first render after SSR, otherwise false.
|
|
201
180
|
*/
|
|
202
|
-
|
|
203
|
-
|
|
204
181
|
_isSSRHydrate() {
|
|
205
182
|
return this._getSSRHTML().length > 0;
|
|
206
183
|
}
|
|
207
|
-
|
|
208
184
|
}
|
|
209
185
|
|
|
186
|
+
// error event name from @merkur/plugin-error
|
|
210
187
|
const MERKUR_ERROR_EVENT_NAME = '@merkur/plugin-error.error';
|
|
211
188
|
class MerkurWidget extends AbstractMerkurWidget {
|
|
212
189
|
/**
|
|
@@ -215,15 +192,13 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
215
192
|
get html() {
|
|
216
193
|
return this.props.widgetProperties?.html || null;
|
|
217
194
|
}
|
|
195
|
+
|
|
218
196
|
/**
|
|
219
197
|
* @inheritdoc
|
|
220
198
|
*/
|
|
221
|
-
|
|
222
|
-
|
|
223
199
|
get container() {
|
|
224
200
|
return this._isClient() && document?.querySelector(this.props.widgetProperties?.containerSelector) || null;
|
|
225
201
|
}
|
|
226
|
-
|
|
227
202
|
constructor(props) {
|
|
228
203
|
super(props);
|
|
229
204
|
this._widget = null;
|
|
@@ -234,6 +209,7 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
234
209
|
cachedWidgetMeta: null
|
|
235
210
|
};
|
|
236
211
|
}
|
|
212
|
+
|
|
237
213
|
/**
|
|
238
214
|
* In case widget props change to new widget, we need to reset
|
|
239
215
|
* state before next render. This enables us to immediately render
|
|
@@ -242,15 +218,14 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
242
218
|
* @param {object} nextProps
|
|
243
219
|
* @param {object} prevState
|
|
244
220
|
*/
|
|
245
|
-
|
|
246
|
-
|
|
247
221
|
static getDerivedStateFromProps(nextProps, prevState) {
|
|
248
222
|
if (prevState && nextProps && AbstractMerkurWidget.validateProperties(nextProps.widgetProperties)) {
|
|
249
223
|
const {
|
|
250
224
|
version,
|
|
251
225
|
name
|
|
252
|
-
} = nextProps.widgetProperties;
|
|
226
|
+
} = nextProps.widgetProperties;
|
|
253
227
|
|
|
228
|
+
// Cache widget meta data (name & version)
|
|
254
229
|
if (!prevState.cachedWidgetMeta) {
|
|
255
230
|
return {
|
|
256
231
|
cachedWidgetMeta: {
|
|
@@ -258,9 +233,9 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
258
233
|
version
|
|
259
234
|
}
|
|
260
235
|
};
|
|
261
|
-
}
|
|
262
|
-
|
|
236
|
+
}
|
|
263
237
|
|
|
238
|
+
// Replace cached widget meta data with new ones and reset state
|
|
264
239
|
if (AbstractMerkurWidget.hasWidgetChanged(prevState.cachedWidgetMeta, nextProps.widgetProperties)) {
|
|
265
240
|
return {
|
|
266
241
|
encounteredError: false,
|
|
@@ -272,9 +247,9 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
272
247
|
};
|
|
273
248
|
}
|
|
274
249
|
}
|
|
275
|
-
|
|
276
250
|
return null;
|
|
277
251
|
}
|
|
252
|
+
|
|
278
253
|
/**
|
|
279
254
|
* Component should be updated only in these cases:
|
|
280
255
|
* 1) State of MerkurWidget has changed (excluding { @code this.state.cachedWidgetMeta }).
|
|
@@ -288,13 +263,10 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
288
263
|
* @param {object} nextProps
|
|
289
264
|
* @param {object} nextState
|
|
290
265
|
*/
|
|
291
|
-
|
|
292
|
-
|
|
293
266
|
shouldComponentUpdate(nextProps, nextState) {
|
|
294
267
|
if (this.state.assetsLoaded !== nextState.assetsLoaded || this.state.encounteredError !== nextState.encounteredError || !AbstractMerkurWidget.validateProperties(this.props.widgetProperties) || AbstractMerkurWidget.hasWidgetChanged(this.props.widgetProperties, nextProps.widgetProperties)) {
|
|
295
268
|
return true;
|
|
296
269
|
}
|
|
297
|
-
|
|
298
270
|
if (this._widget && nextProps.widgetProperties && nextProps.widgetProperties.props) {
|
|
299
271
|
if (Object.keys(nextProps.widgetProperties.props).length !== Object.keys(this.props.widgetProperties.props).length) {
|
|
300
272
|
/**
|
|
@@ -303,7 +275,6 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
303
275
|
* comparison below would not work.
|
|
304
276
|
*/
|
|
305
277
|
this._widget.setProps(nextProps.widgetProperties.props);
|
|
306
|
-
|
|
307
278
|
return false;
|
|
308
279
|
} else {
|
|
309
280
|
/**
|
|
@@ -313,43 +284,37 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
313
284
|
for (let key in nextProps.widgetProperties.props) {
|
|
314
285
|
if (!this.props.widgetProperties || !this.props.widgetProperties.props || nextProps.widgetProperties.props[key] !== this.props.widgetProperties.props[key]) {
|
|
315
286
|
this._widget.setProps(nextProps.widgetProperties.props);
|
|
316
|
-
|
|
317
287
|
return false;
|
|
318
288
|
}
|
|
319
289
|
}
|
|
320
290
|
}
|
|
321
291
|
}
|
|
322
|
-
|
|
323
292
|
if (this._widget && nextProps.widgetProperties && nextProps.widgetProperties.state) {
|
|
324
293
|
if (Object.keys(nextProps.widgetProperties.state).length !== Object.keys(this.props.widgetProperties.state).length) {
|
|
325
294
|
this._widget.setState(nextProps.widgetProperties.state);
|
|
326
|
-
|
|
327
295
|
return false;
|
|
328
296
|
} else {
|
|
329
297
|
for (let key in nextProps.widgetProperties.state) {
|
|
330
298
|
if (!this.props.widgetProperties || !this.props.widgetProperties.state || nextProps.widgetProperties.state[key] !== this.props.widgetProperties.state[key]) {
|
|
331
299
|
this._widget.setState(nextProps.widgetProperties.state);
|
|
332
|
-
|
|
333
300
|
return false;
|
|
334
301
|
}
|
|
335
302
|
}
|
|
336
303
|
}
|
|
337
304
|
}
|
|
338
|
-
|
|
339
305
|
return false;
|
|
340
306
|
}
|
|
307
|
+
|
|
341
308
|
/**
|
|
342
309
|
* In case of mounting the component, we always try to first load
|
|
343
310
|
* the widget assets. Be it first mount after SSR or mount after
|
|
344
311
|
* first render on client.
|
|
345
312
|
*/
|
|
346
|
-
|
|
347
|
-
|
|
348
313
|
componentDidMount() {
|
|
349
314
|
super.componentDidMount();
|
|
350
|
-
|
|
351
315
|
this._loadWidgetAssets();
|
|
352
316
|
}
|
|
317
|
+
|
|
353
318
|
/**
|
|
354
319
|
* After the component has been updated, we still need to handle few situations.
|
|
355
320
|
* 1) In case assets have been loaded, we need to mount the widget.
|
|
@@ -358,24 +323,22 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
358
323
|
* @param {object} prevProps
|
|
359
324
|
* @param {object} prevState
|
|
360
325
|
*/
|
|
361
|
-
|
|
362
|
-
|
|
363
326
|
componentDidUpdate(prevProps, prevState) {
|
|
364
327
|
const {
|
|
365
328
|
widgetProperties: currentWidgetProperties
|
|
366
329
|
} = this.props;
|
|
367
330
|
const {
|
|
368
331
|
widgetProperties: prevWidgetProperties
|
|
369
|
-
} = prevProps;
|
|
332
|
+
} = prevProps;
|
|
370
333
|
|
|
334
|
+
// 1) Assets have been loaded => mount the widget
|
|
371
335
|
if (this.state.assetsLoaded && prevState.assetsLoaded !== this.state.assetsLoaded) {
|
|
372
336
|
return this._mountWidget();
|
|
373
|
-
}
|
|
374
|
-
|
|
337
|
+
}
|
|
375
338
|
|
|
339
|
+
// 2.1) In case we receive empty new properties, we need to cleanup.
|
|
376
340
|
if (!AbstractMerkurWidget.validateProperties(currentWidgetProperties) && AbstractMerkurWidget.validateProperties(prevWidgetProperties)) {
|
|
377
341
|
this._removeWidget();
|
|
378
|
-
|
|
379
342
|
this.setState({
|
|
380
343
|
encounteredError: false,
|
|
381
344
|
assetsLoaded: false,
|
|
@@ -383,39 +346,35 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
383
346
|
});
|
|
384
347
|
return;
|
|
385
348
|
}
|
|
349
|
+
|
|
386
350
|
/**
|
|
387
351
|
* 2.2) In case there were no widget properties before, we try to
|
|
388
352
|
* initialize widget first by doing the same as if it first mounted
|
|
389
353
|
* (loading assets into the DOM).
|
|
390
354
|
*/
|
|
391
|
-
|
|
392
|
-
|
|
393
355
|
if (AbstractMerkurWidget.validateProperties(currentWidgetProperties) && !AbstractMerkurWidget.validateProperties(prevWidgetProperties)) {
|
|
394
356
|
return this._loadWidgetAssets();
|
|
395
357
|
}
|
|
358
|
+
|
|
396
359
|
/**
|
|
397
360
|
* 2.3) In case widget has changed, first we need to cleanup (remove previous widget),
|
|
398
361
|
* and then we again try to initialize the new widget same way as
|
|
399
362
|
* if it has mounted for the first time (if there are any new widget properties).
|
|
400
363
|
*/
|
|
401
|
-
|
|
402
|
-
|
|
403
364
|
if (AbstractMerkurWidget.hasWidgetChanged(currentWidgetProperties, prevWidgetProperties)) {
|
|
404
365
|
this._removeWidget();
|
|
405
|
-
|
|
406
366
|
this._loadWidgetAssets();
|
|
407
|
-
|
|
408
367
|
return;
|
|
409
368
|
}
|
|
410
369
|
}
|
|
370
|
+
|
|
411
371
|
/**
|
|
412
372
|
* In case of unmounting we only really need to do the cleanup.
|
|
413
373
|
*/
|
|
414
|
-
|
|
415
|
-
|
|
416
374
|
componentWillUnmount() {
|
|
417
375
|
this._removeWidget();
|
|
418
376
|
}
|
|
377
|
+
|
|
419
378
|
/**
|
|
420
379
|
* There are two possible outputs from the render method:
|
|
421
380
|
* 1) Fallback is rendered only, when assets are not yet loaded
|
|
@@ -426,8 +385,6 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
426
385
|
*
|
|
427
386
|
* @return {React.ReactElement|null}
|
|
428
387
|
*/
|
|
429
|
-
|
|
430
|
-
|
|
431
388
|
render() {
|
|
432
389
|
const {
|
|
433
390
|
widgetProperties
|
|
@@ -436,30 +393,25 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
436
393
|
encounteredError,
|
|
437
394
|
assetsLoaded
|
|
438
395
|
} = this.state;
|
|
439
|
-
|
|
440
396
|
if (!AbstractMerkurWidget.validateProperties(widgetProperties) || encounteredError || this._isClient() && !this._isSSRHydrate() && !assetsLoaded) {
|
|
441
397
|
return this._renderFallback();
|
|
442
398
|
}
|
|
443
|
-
|
|
444
399
|
if (!widgetProperties.containerSelector) {
|
|
445
400
|
throw new Error(`The widgetProperties.containerSelector is not defined`);
|
|
446
401
|
}
|
|
447
|
-
|
|
448
402
|
const html = this._getWidgetHTML();
|
|
449
|
-
|
|
450
403
|
return /*#__PURE__*/React.createElement(React.Fragment, null, (!this._isClient() || this._isSSRHydrate()) && this._renderStyleAssets(), /*#__PURE__*/React.createElement(WidgetWrapper$1, {
|
|
451
404
|
containerSelector: widgetProperties.containerSelector,
|
|
452
405
|
html: html
|
|
453
406
|
}));
|
|
454
407
|
}
|
|
408
|
+
|
|
455
409
|
/**
|
|
456
410
|
* Renders widget style assets inline. This is used on SSR
|
|
457
411
|
* and in first render after SSR hydration.
|
|
458
412
|
*
|
|
459
413
|
* @return {[React.ReactElement]}
|
|
460
414
|
*/
|
|
461
|
-
|
|
462
|
-
|
|
463
415
|
_renderStyleAssets() {
|
|
464
416
|
const {
|
|
465
417
|
widgetProperties
|
|
@@ -473,7 +425,6 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
473
425
|
href: asset.source,
|
|
474
426
|
key: key
|
|
475
427
|
});
|
|
476
|
-
|
|
477
428
|
case 'inlineStyle':
|
|
478
429
|
return /*#__PURE__*/React.createElement("style", {
|
|
479
430
|
key: key,
|
|
@@ -484,56 +435,46 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
484
435
|
}
|
|
485
436
|
});
|
|
486
437
|
}
|
|
487
|
-
|
|
488
438
|
_handleError(error) {
|
|
489
|
-
if (typeof this.props.onError === 'function') {
|
|
490
|
-
|
|
439
|
+
if (typeof this.props.onError === 'function' && this.props.onError(error)) {
|
|
440
|
+
return;
|
|
491
441
|
}
|
|
492
|
-
|
|
493
442
|
this.setState({
|
|
494
443
|
encounteredError: error
|
|
495
444
|
});
|
|
496
445
|
}
|
|
446
|
+
|
|
497
447
|
/**
|
|
498
448
|
* Handles widget unmounting and removal, while removing
|
|
499
449
|
* event listeners, calling unmount on props and widget.
|
|
500
450
|
*/
|
|
501
|
-
|
|
502
|
-
|
|
503
451
|
_removeWidget() {
|
|
504
452
|
if (!this._widget) {
|
|
505
453
|
return;
|
|
506
454
|
}
|
|
507
|
-
|
|
508
455
|
if (this.props.onWidgetUnmounting) {
|
|
509
456
|
this.props.onWidgetUnmounting(this._widget);
|
|
510
|
-
}
|
|
511
|
-
|
|
457
|
+
}
|
|
512
458
|
|
|
459
|
+
// widget might not be using @merkur/plugin-event-emitter
|
|
513
460
|
if (typeof this._widget.off === 'function') {
|
|
514
461
|
this._widget.off(MERKUR_ERROR_EVENT_NAME, this._handleClientError);
|
|
515
462
|
}
|
|
516
|
-
|
|
517
463
|
this._widget.unmount();
|
|
518
|
-
|
|
519
464
|
this._widget = null;
|
|
520
|
-
|
|
521
465
|
this._clearCachedHtml();
|
|
522
466
|
}
|
|
467
|
+
|
|
523
468
|
/**
|
|
524
469
|
* Loads widget assets into page.
|
|
525
470
|
*/
|
|
526
|
-
|
|
527
|
-
|
|
528
471
|
_loadWidgetAssets() {
|
|
529
472
|
const {
|
|
530
473
|
widgetProperties
|
|
531
474
|
} = this.props;
|
|
532
|
-
|
|
533
475
|
if (!AbstractMerkurWidget.validateProperties(widgetProperties) || this._widget) {
|
|
534
476
|
return;
|
|
535
477
|
}
|
|
536
|
-
|
|
537
478
|
return Promise.all([loadStyleAssets(widgetProperties.assets), loadScriptAssets(widgetProperties.assets)]).then(() => new Promise(resolve => {
|
|
538
479
|
this.setState({
|
|
539
480
|
assetsLoaded: true
|
|
@@ -542,32 +483,27 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
542
483
|
});
|
|
543
484
|
})).catch(error => this._handleError(error));
|
|
544
485
|
}
|
|
486
|
+
|
|
545
487
|
/**
|
|
546
488
|
* Creates and mounts widget instance after all resource loaded.
|
|
547
489
|
*/
|
|
548
|
-
|
|
549
|
-
|
|
550
490
|
async _mountWidget() {
|
|
551
491
|
const {
|
|
552
492
|
widgetProperties,
|
|
553
493
|
onWidgetMounted,
|
|
554
494
|
debug = false
|
|
555
495
|
} = this.props;
|
|
556
|
-
|
|
557
496
|
if (!AbstractMerkurWidget.validateProperties(widgetProperties) || this._widget) {
|
|
558
497
|
return;
|
|
559
498
|
}
|
|
560
|
-
|
|
561
499
|
try {
|
|
562
500
|
const merkur = getMerkur();
|
|
563
501
|
this._widget = await merkur.create(widgetProperties);
|
|
564
502
|
await this._widget.mount();
|
|
565
|
-
|
|
566
503
|
if (typeof this._widget.on === 'function') {
|
|
567
504
|
// widget might not be using @merkur/plugin-event-emitter
|
|
568
505
|
this._widget.on(MERKUR_ERROR_EVENT_NAME, this._handleClientError);
|
|
569
506
|
}
|
|
570
|
-
|
|
571
507
|
if (typeof onWidgetMounted === 'function') {
|
|
572
508
|
onWidgetMounted(this._widget);
|
|
573
509
|
}
|
|
@@ -577,7 +513,6 @@ class MerkurWidget extends AbstractMerkurWidget {
|
|
|
577
513
|
}
|
|
578
514
|
}
|
|
579
515
|
}
|
|
580
|
-
|
|
581
516
|
}
|
|
582
517
|
|
|
583
518
|
class MerkurSlot extends AbstractMerkurWidget {
|
|
@@ -592,22 +527,21 @@ class MerkurSlot extends AbstractMerkurWidget {
|
|
|
592
527
|
} = this.props;
|
|
593
528
|
return widgetProperties?.slot?.[slotName] ?? null;
|
|
594
529
|
}
|
|
530
|
+
|
|
595
531
|
/**
|
|
596
532
|
* @inheritdoc
|
|
597
533
|
*/
|
|
598
|
-
|
|
599
|
-
|
|
600
534
|
get html() {
|
|
601
535
|
return this.slot?.html || null;
|
|
602
536
|
}
|
|
537
|
+
|
|
603
538
|
/**
|
|
604
539
|
* @inheritdoc
|
|
605
540
|
*/
|
|
606
|
-
|
|
607
|
-
|
|
608
541
|
get container() {
|
|
609
542
|
return this._isClient() && document?.querySelector(this.slot?.containerSelector) || null;
|
|
610
543
|
}
|
|
544
|
+
|
|
611
545
|
/**
|
|
612
546
|
* The component should update only in following cases:
|
|
613
547
|
* 1) Component has no props.widgetProperties.
|
|
@@ -615,23 +549,19 @@ class MerkurSlot extends AbstractMerkurWidget {
|
|
|
615
549
|
*
|
|
616
550
|
* @param {object} nextProps
|
|
617
551
|
*/
|
|
618
|
-
|
|
619
|
-
|
|
620
552
|
shouldComponentUpdate(nextProps) {
|
|
621
553
|
if (!AbstractMerkurWidget.validateProperties(this.props.widgetProperties) || !AbstractMerkurWidget.validateProperties(nextProps.widgetProperties) || AbstractMerkurWidget.hasWidgetChanged(this.props.widgetProperties, nextProps.widgetProperties)) {
|
|
622
554
|
return true;
|
|
623
555
|
}
|
|
624
|
-
|
|
625
556
|
return false;
|
|
626
557
|
}
|
|
558
|
+
|
|
627
559
|
/**
|
|
628
560
|
* Cleanup when we receive empty widget properties.
|
|
629
561
|
*
|
|
630
562
|
* @param {object} prevProps
|
|
631
563
|
* @param {object} prevState
|
|
632
564
|
*/
|
|
633
|
-
|
|
634
|
-
|
|
635
565
|
componentDidUpdate(prevProps) {
|
|
636
566
|
const {
|
|
637
567
|
widgetProperties: currentWidgetProperties
|
|
@@ -639,21 +569,19 @@ class MerkurSlot extends AbstractMerkurWidget {
|
|
|
639
569
|
const {
|
|
640
570
|
widgetProperties: prevWidgetProperties
|
|
641
571
|
} = prevProps;
|
|
642
|
-
|
|
643
572
|
if (!AbstractMerkurWidget.validateProperties(currentWidgetProperties) && AbstractMerkurWidget.validateProperties(prevWidgetProperties)) {
|
|
644
573
|
this._removeSlot();
|
|
645
|
-
|
|
646
574
|
return;
|
|
647
575
|
}
|
|
648
576
|
}
|
|
577
|
+
|
|
649
578
|
/**
|
|
650
579
|
* Cleanup when unmounting
|
|
651
580
|
*/
|
|
652
|
-
|
|
653
|
-
|
|
654
581
|
componentWillUnmount() {
|
|
655
582
|
this._removeSlot();
|
|
656
583
|
}
|
|
584
|
+
|
|
657
585
|
/**
|
|
658
586
|
* There are two possible outputs from the render method:
|
|
659
587
|
* 1) Fallback is rendered only, when there are no widget properties.
|
|
@@ -663,20 +591,17 @@ class MerkurSlot extends AbstractMerkurWidget {
|
|
|
663
591
|
*
|
|
664
592
|
* @return {React.ReactElement|null}
|
|
665
593
|
*/
|
|
666
|
-
|
|
667
|
-
|
|
668
594
|
render() {
|
|
669
595
|
const {
|
|
670
596
|
widgetProperties
|
|
671
597
|
} = this.props;
|
|
672
|
-
|
|
673
598
|
if (!AbstractMerkurWidget.validateProperties(widgetProperties) || !this.slot) {
|
|
674
599
|
return this._renderFallback();
|
|
675
600
|
}
|
|
676
|
-
|
|
677
601
|
if (!this.slot['containerSelector']) {
|
|
678
602
|
throw new Error(`The ${this.slot.name}.containerSelector is not defined`);
|
|
679
603
|
}
|
|
604
|
+
|
|
680
605
|
/**
|
|
681
606
|
* In case of SPA rendering, we render fallback (which can also display
|
|
682
607
|
* loading placeholders) inside the component wrapper, until the widget
|
|
@@ -684,8 +609,6 @@ class MerkurSlot extends AbstractMerkurWidget {
|
|
|
684
609
|
* of the wrapper (containing fallback) with slot markup. In case of SPA
|
|
685
610
|
* we also don't want to render html to prevent FOUC.
|
|
686
611
|
*/
|
|
687
|
-
|
|
688
|
-
|
|
689
612
|
const isInitialSPARender = this._isClient() && !this._isSSRHydrate();
|
|
690
613
|
const html = isInitialSPARender ? '' : this._getWidgetHTML();
|
|
691
614
|
return /*#__PURE__*/React.createElement(WidgetWrapper$1, {
|
|
@@ -693,15 +616,13 @@ class MerkurSlot extends AbstractMerkurWidget {
|
|
|
693
616
|
html: html
|
|
694
617
|
}, isInitialSPARender && this._renderFallback());
|
|
695
618
|
}
|
|
619
|
+
|
|
696
620
|
/**
|
|
697
621
|
* Cleanup after slot removal.
|
|
698
622
|
*/
|
|
699
|
-
|
|
700
|
-
|
|
701
623
|
_removeSlot() {
|
|
702
624
|
this._clearCachedHtml();
|
|
703
625
|
}
|
|
704
|
-
|
|
705
626
|
}
|
|
706
627
|
|
|
707
628
|
export { MerkurSlot, MerkurWidget };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@merkur/integration-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.31.0",
|
|
4
4
|
"description": "Merkur module for easy integration with react.",
|
|
5
5
|
"main": "lib/index",
|
|
6
6
|
"module": "lib/index",
|
|
@@ -47,9 +47,9 @@
|
|
|
47
47
|
"homepage": "https://merkur.js.org/",
|
|
48
48
|
"devDependencies": {
|
|
49
49
|
"@babel/preset-react": "^7.16.7",
|
|
50
|
-
"@merkur/core": "^0.
|
|
51
|
-
"@merkur/plugin-component": "^0.
|
|
52
|
-
"@merkur/plugin-event-emitter": "^0.
|
|
50
|
+
"@merkur/core": "^0.31.0",
|
|
51
|
+
"@merkur/plugin-component": "^0.31.0",
|
|
52
|
+
"@merkur/plugin-event-emitter": "^0.31.0",
|
|
53
53
|
"enzyme": "3.11.0",
|
|
54
54
|
"enzyme-adapter-react-16": "1.15.6",
|
|
55
55
|
"enzyme-to-json": "^3.6.2",
|
|
@@ -63,7 +63,7 @@
|
|
|
63
63
|
"react-dom": ">=16"
|
|
64
64
|
},
|
|
65
65
|
"dependencies": {
|
|
66
|
-
"@merkur/integration": "^0.
|
|
66
|
+
"@merkur/integration": "^0.31.0"
|
|
67
67
|
},
|
|
68
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "ef1b38f23e43e2fd7ef6a2bec680fb40a2de3715"
|
|
69
69
|
}
|