@merkur/integration-custom-element 0.38.0 → 0.40.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/README.md +1 -1
- package/cli/index.mjs +1 -1
- package/lib/index.cjs +46 -107
- package/lib/index.js +46 -107
- package/lib/index.mjs +46 -107
- package/package.json +5 -6
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<p align="center">
|
|
2
2
|
<a href="https://merkur.js.org/docs/getting-started" title="Getting started">
|
|
3
|
-
<img src="https://raw.githubusercontent.com/mjancarik/merkur/master/images/merkur-
|
|
3
|
+
<img src="https://raw.githubusercontent.com/mjancarik/merkur/master/images/merkur-logo.png" width="100px" height="100px" alt="Merkur illustration"/>
|
|
4
4
|
</a>
|
|
5
5
|
</p>
|
|
6
6
|
|
package/cli/index.mjs
CHANGED
|
@@ -109,7 +109,7 @@ export default function ({ emitter, EMITTER_EVENTS }) {
|
|
|
109
109
|
|
|
110
110
|
emitter.on(EMITTER_EVENTS.CLI_CONFIG, function removeNodeTask({ cliConfig }) {
|
|
111
111
|
if (cliConfig.runTasks.length === 0) {
|
|
112
|
-
cliConfig.runTasks = ['
|
|
112
|
+
cliConfig.runTasks = ['es15', 'es9'];
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
cliConfig.runTasks = cliConfig.runTasks.filter((task) => task !== 'node');
|
package/lib/index.cjs
CHANGED
|
@@ -6,22 +6,18 @@ var integration = require('@merkur/integration');
|
|
|
6
6
|
async function createSPAWidget(widgetDefinition, root) {
|
|
7
7
|
const definition = {
|
|
8
8
|
...widgetDefinition,
|
|
9
|
-
createWidget: widgetDefinition.createWidget
|
|
9
|
+
createWidget: widgetDefinition.createWidget
|
|
10
10
|
};
|
|
11
|
-
|
|
12
11
|
const merkur = core.getMerkur();
|
|
13
12
|
if (!merkur.isRegistered(definition.name + definition.version)) {
|
|
14
13
|
core.getMerkur().register(definition);
|
|
15
14
|
}
|
|
16
|
-
|
|
17
15
|
await afterDOMLoad();
|
|
18
16
|
await integration.loadAssets(definition.assets, root);
|
|
19
|
-
|
|
20
17
|
return await core.getMerkur().create(definition);
|
|
21
18
|
}
|
|
22
|
-
|
|
23
19
|
function afterDOMLoad() {
|
|
24
|
-
return new Promise(
|
|
20
|
+
return new Promise(resolve => {
|
|
25
21
|
if (typeof document !== 'undefined') {
|
|
26
22
|
if (document.readyState !== 'loading') {
|
|
27
23
|
resolve();
|
|
@@ -35,10 +31,13 @@ function afterDOMLoad() {
|
|
|
35
31
|
}
|
|
36
32
|
});
|
|
37
33
|
}
|
|
38
|
-
|
|
39
34
|
function registerCustomElement(options) {
|
|
40
|
-
const {
|
|
41
|
-
|
|
35
|
+
const {
|
|
36
|
+
widgetDefinition,
|
|
37
|
+
callbacks,
|
|
38
|
+
observedAttributes,
|
|
39
|
+
attributesParser
|
|
40
|
+
} = deepMerge({}, options);
|
|
42
41
|
class HTMLCustomElement extends HTMLElement {
|
|
43
42
|
static get observedAttributes() {
|
|
44
43
|
return observedAttributes ?? [];
|
|
@@ -50,80 +49,59 @@ function registerCustomElement(options) {
|
|
|
50
49
|
}
|
|
51
50
|
_init() {}
|
|
52
51
|
}
|
|
53
|
-
|
|
54
52
|
class WidgetElement extends HTMLCustomElement {
|
|
55
53
|
_init() {
|
|
56
54
|
try {
|
|
57
55
|
super._init();
|
|
58
56
|
const customWidgetDefinition = deepMerge({}, widgetDefinition);
|
|
59
|
-
|
|
60
57
|
this._widgetPromise = (async () => {
|
|
61
|
-
this._shadow = this.attachShadow({
|
|
62
|
-
|
|
58
|
+
this._shadow = this.attachShadow({
|
|
59
|
+
mode: 'open'
|
|
60
|
+
});
|
|
63
61
|
try {
|
|
64
62
|
const widget = await callbacks?.getInstance?.();
|
|
65
|
-
|
|
66
63
|
if (widget && widget.name && widget.version) {
|
|
67
64
|
this._widget = widget;
|
|
68
|
-
|
|
69
65
|
await afterDOMLoad();
|
|
70
66
|
await integration.loadAssets(widget.assets, this._shadow);
|
|
71
|
-
|
|
72
67
|
this._setDefaultProps();
|
|
73
|
-
|
|
74
68
|
await callbacks?.reconstructor?.(this._widget, {
|
|
75
69
|
shadow: this._shadow,
|
|
76
|
-
customElement: this
|
|
70
|
+
customElement: this
|
|
77
71
|
});
|
|
78
|
-
|
|
79
72
|
if (typeof callbacks?.remount === 'function') {
|
|
80
73
|
await callbacks?.remount?.(this._widget, {
|
|
81
74
|
shadow: this._shadow,
|
|
82
|
-
customElement: this
|
|
75
|
+
customElement: this
|
|
83
76
|
});
|
|
84
77
|
} else {
|
|
85
78
|
widget.root = this._shadow;
|
|
86
79
|
widget.customElement = this;
|
|
87
80
|
this._shadow.appendChild(widget.container);
|
|
88
81
|
}
|
|
89
|
-
|
|
90
82
|
return;
|
|
91
83
|
}
|
|
92
84
|
} catch (error) {
|
|
93
85
|
console.error(error);
|
|
94
|
-
|
|
95
86
|
return;
|
|
96
87
|
}
|
|
97
|
-
|
|
98
88
|
try {
|
|
99
89
|
customWidgetDefinition.root = this._shadow;
|
|
100
90
|
customWidgetDefinition.customElement = this;
|
|
101
|
-
|
|
102
91
|
if (!customWidgetDefinition.container) {
|
|
103
92
|
customWidgetDefinition.container = document.createElement('div');
|
|
104
|
-
customWidgetDefinition.container.setAttribute(
|
|
105
|
-
'id',
|
|
106
|
-
'merkur-container',
|
|
107
|
-
);
|
|
93
|
+
customWidgetDefinition.container.setAttribute('id', 'merkur-container');
|
|
108
94
|
}
|
|
109
|
-
|
|
110
95
|
this._shadow.appendChild(customWidgetDefinition.container);
|
|
111
|
-
|
|
112
|
-
this._widget = await createSPAWidget(
|
|
113
|
-
customWidgetDefinition,
|
|
114
|
-
this._shadow,
|
|
115
|
-
);
|
|
116
|
-
|
|
96
|
+
this._widget = await createSPAWidget(customWidgetDefinition, this._shadow);
|
|
117
97
|
this._setDefaultProps();
|
|
118
|
-
|
|
119
98
|
await callbacks?.constructor?.(this._widget, {
|
|
120
99
|
shadow: this._shadow,
|
|
121
|
-
customElement: this
|
|
100
|
+
customElement: this
|
|
122
101
|
});
|
|
123
|
-
|
|
124
102
|
(await callbacks?.mount?.(this._widget, {
|
|
125
103
|
shadow: this._shadow,
|
|
126
|
-
customElement: this
|
|
104
|
+
customElement: this
|
|
127
105
|
})) ?? (await this._widget.mount());
|
|
128
106
|
} catch (error) {
|
|
129
107
|
console.error(error);
|
|
@@ -133,126 +111,88 @@ function registerCustomElement(options) {
|
|
|
133
111
|
console.error(error);
|
|
134
112
|
}
|
|
135
113
|
}
|
|
136
|
-
|
|
137
114
|
async connectedCallback() {
|
|
138
115
|
await this._widgetPromise;
|
|
139
|
-
|
|
140
116
|
this._widget?.connectedCallback?.({
|
|
141
117
|
shadow: this._shadow,
|
|
142
|
-
customElement: this
|
|
118
|
+
customElement: this
|
|
143
119
|
});
|
|
144
|
-
|
|
145
120
|
callbacks?.connectedCallback?.(this._widget, {
|
|
146
121
|
shadow: this._shadow,
|
|
147
|
-
customElement: this
|
|
122
|
+
customElement: this
|
|
148
123
|
});
|
|
149
124
|
}
|
|
150
|
-
|
|
151
125
|
async disconnectedCallback() {
|
|
152
126
|
await this._widgetPromise;
|
|
153
|
-
|
|
154
127
|
this._widget?.disconnectedCallback?.({
|
|
155
128
|
shadow: this._shadow,
|
|
156
|
-
customElement: this
|
|
129
|
+
customElement: this
|
|
157
130
|
});
|
|
158
|
-
|
|
159
131
|
callbacks?.disconnectedCallback?.(this._widget, {
|
|
160
132
|
shadow: this._shadow,
|
|
161
|
-
customElement: this
|
|
133
|
+
customElement: this
|
|
162
134
|
});
|
|
163
135
|
}
|
|
164
|
-
|
|
165
136
|
async adoptedCallback() {
|
|
166
137
|
await this._widgetPromise;
|
|
167
|
-
|
|
168
138
|
this._widget?.adoptedCallback?.({
|
|
169
139
|
shadow: this._shadow,
|
|
170
|
-
customElement: this
|
|
140
|
+
customElement: this
|
|
171
141
|
});
|
|
172
|
-
|
|
173
142
|
callbacks?.adoptedCallback?.(this._widget, {
|
|
174
143
|
shadow: this._shadow,
|
|
175
|
-
customElement: this
|
|
144
|
+
customElement: this
|
|
176
145
|
});
|
|
177
146
|
}
|
|
178
|
-
|
|
179
147
|
async attributeChangedCallback(name, oldValue, newValue) {
|
|
180
148
|
await this._widgetPromise;
|
|
181
|
-
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
this
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
customElement: this,
|
|
195
|
-
},
|
|
196
|
-
);
|
|
197
|
-
|
|
198
|
-
callbacks?.attributeChangedCallback?.(
|
|
199
|
-
this._widget,
|
|
200
|
-
name,
|
|
201
|
-
oldValue,
|
|
202
|
-
newValue,
|
|
203
|
-
{
|
|
204
|
-
shadow: this._shadow,
|
|
205
|
-
customElement: this,
|
|
206
|
-
},
|
|
207
|
-
);
|
|
149
|
+
const camelCaseKey = name.replace(/-([a-z])/g, g => g[1].toUpperCase());
|
|
150
|
+
const parser = attributesParser?.[name] ?? (value => value);
|
|
151
|
+
this._widget?.setProps?.({
|
|
152
|
+
[camelCaseKey]: parser(newValue)
|
|
153
|
+
});
|
|
154
|
+
this._widget?.attributeChangedCallback?.(this._widget, name, oldValue, newValue, {
|
|
155
|
+
shadow: this._shadow,
|
|
156
|
+
customElement: this
|
|
157
|
+
});
|
|
158
|
+
callbacks?.attributeChangedCallback?.(this._widget, name, oldValue, newValue, {
|
|
159
|
+
shadow: this._shadow,
|
|
160
|
+
customElement: this
|
|
161
|
+
});
|
|
208
162
|
}
|
|
209
|
-
|
|
210
163
|
_setDefaultProps() {
|
|
211
164
|
const attributes = this.constructor.observedAttributes;
|
|
212
|
-
if (
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
attributes.forEach((key) => {
|
|
165
|
+
if (Array.isArray(attributes) && typeof this._widget.setProps === 'function') {
|
|
166
|
+
this._widget.props = {
|
|
167
|
+
...this._widget.props
|
|
168
|
+
};
|
|
169
|
+
attributes.forEach(key => {
|
|
218
170
|
if (this.hasAttribute(key)) {
|
|
219
|
-
const camelCaseKey = key.replace(/-([a-z])/g,
|
|
220
|
-
|
|
221
|
-
);
|
|
222
|
-
const parser = attributesParser?.[key] ?? ((value) => value);
|
|
223
|
-
|
|
224
|
-
this._widget.props[camelCaseKey] = parser(
|
|
225
|
-
this.getAttribute(key) ?? this._widget.props[key],
|
|
226
|
-
);
|
|
171
|
+
const camelCaseKey = key.replace(/-([a-z])/g, g => g[1].toUpperCase());
|
|
172
|
+
const parser = attributesParser?.[key] ?? (value => value);
|
|
173
|
+
this._widget.props[camelCaseKey] = parser(this.getAttribute(key) ?? this._widget.props[key]);
|
|
227
174
|
}
|
|
228
175
|
});
|
|
229
176
|
}
|
|
230
177
|
}
|
|
231
178
|
}
|
|
232
|
-
|
|
233
179
|
if (customElements.get(widgetDefinition.name) === undefined) {
|
|
234
180
|
customElements.define(widgetDefinition.name, WidgetElement);
|
|
235
181
|
}
|
|
236
|
-
|
|
237
182
|
return WidgetElement;
|
|
238
183
|
}
|
|
239
|
-
|
|
240
184
|
const PROTECTED_FIELDS = ['__proto__', 'prototype', 'constructor'];
|
|
241
185
|
function deepMerge(target, source) {
|
|
242
|
-
const isObject =
|
|
243
|
-
|
|
186
|
+
const isObject = obj => !!obj && obj.constructor === Object;
|
|
244
187
|
if (!isObject(target) || !isObject(source)) {
|
|
245
188
|
return source;
|
|
246
189
|
}
|
|
247
|
-
|
|
248
|
-
Object.keys(source).forEach((key) => {
|
|
190
|
+
Object.keys(source).forEach(key => {
|
|
249
191
|
if (PROTECTED_FIELDS.includes(key)) {
|
|
250
192
|
return;
|
|
251
193
|
}
|
|
252
|
-
|
|
253
194
|
const targetValue = target[key];
|
|
254
195
|
const sourceValue = source[key];
|
|
255
|
-
|
|
256
196
|
if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
|
|
257
197
|
target[key] = targetValue.concat(sourceValue);
|
|
258
198
|
} else if (isObject(targetValue) && isObject(sourceValue)) {
|
|
@@ -261,7 +201,6 @@ function deepMerge(target, source) {
|
|
|
261
201
|
target[key] = sourceValue;
|
|
262
202
|
}
|
|
263
203
|
});
|
|
264
|
-
|
|
265
204
|
return target;
|
|
266
205
|
}
|
|
267
206
|
|
package/lib/index.js
CHANGED
|
@@ -6,22 +6,18 @@ var integration = require('@merkur/integration');
|
|
|
6
6
|
async function createSPAWidget(widgetDefinition, root) {
|
|
7
7
|
const definition = {
|
|
8
8
|
...widgetDefinition,
|
|
9
|
-
createWidget: widgetDefinition.createWidget
|
|
9
|
+
createWidget: widgetDefinition.createWidget
|
|
10
10
|
};
|
|
11
|
-
|
|
12
11
|
const merkur = core.getMerkur();
|
|
13
12
|
if (!merkur.isRegistered(definition.name + definition.version)) {
|
|
14
13
|
core.getMerkur().register(definition);
|
|
15
14
|
}
|
|
16
|
-
|
|
17
15
|
await afterDOMLoad();
|
|
18
16
|
await integration.loadAssets(definition.assets, root);
|
|
19
|
-
|
|
20
17
|
return await core.getMerkur().create(definition);
|
|
21
18
|
}
|
|
22
|
-
|
|
23
19
|
function afterDOMLoad() {
|
|
24
|
-
return new Promise(
|
|
20
|
+
return new Promise(resolve => {
|
|
25
21
|
if (typeof document !== 'undefined') {
|
|
26
22
|
if (document.readyState !== 'loading') {
|
|
27
23
|
resolve();
|
|
@@ -35,10 +31,13 @@ function afterDOMLoad() {
|
|
|
35
31
|
}
|
|
36
32
|
});
|
|
37
33
|
}
|
|
38
|
-
|
|
39
34
|
function registerCustomElement(options) {
|
|
40
|
-
const {
|
|
41
|
-
|
|
35
|
+
const {
|
|
36
|
+
widgetDefinition,
|
|
37
|
+
callbacks,
|
|
38
|
+
observedAttributes,
|
|
39
|
+
attributesParser
|
|
40
|
+
} = deepMerge({}, options);
|
|
42
41
|
class HTMLCustomElement extends HTMLElement {
|
|
43
42
|
static get observedAttributes() {
|
|
44
43
|
return observedAttributes ?? [];
|
|
@@ -50,80 +49,59 @@ function registerCustomElement(options) {
|
|
|
50
49
|
}
|
|
51
50
|
_init() {}
|
|
52
51
|
}
|
|
53
|
-
|
|
54
52
|
class WidgetElement extends HTMLCustomElement {
|
|
55
53
|
_init() {
|
|
56
54
|
try {
|
|
57
55
|
super._init();
|
|
58
56
|
const customWidgetDefinition = deepMerge({}, widgetDefinition);
|
|
59
|
-
|
|
60
57
|
this._widgetPromise = (async () => {
|
|
61
|
-
this._shadow = this.attachShadow({
|
|
62
|
-
|
|
58
|
+
this._shadow = this.attachShadow({
|
|
59
|
+
mode: 'open'
|
|
60
|
+
});
|
|
63
61
|
try {
|
|
64
62
|
const widget = await callbacks?.getInstance?.();
|
|
65
|
-
|
|
66
63
|
if (widget && widget.name && widget.version) {
|
|
67
64
|
this._widget = widget;
|
|
68
|
-
|
|
69
65
|
await afterDOMLoad();
|
|
70
66
|
await integration.loadAssets(widget.assets, this._shadow);
|
|
71
|
-
|
|
72
67
|
this._setDefaultProps();
|
|
73
|
-
|
|
74
68
|
await callbacks?.reconstructor?.(this._widget, {
|
|
75
69
|
shadow: this._shadow,
|
|
76
|
-
customElement: this
|
|
70
|
+
customElement: this
|
|
77
71
|
});
|
|
78
|
-
|
|
79
72
|
if (typeof callbacks?.remount === 'function') {
|
|
80
73
|
await callbacks?.remount?.(this._widget, {
|
|
81
74
|
shadow: this._shadow,
|
|
82
|
-
customElement: this
|
|
75
|
+
customElement: this
|
|
83
76
|
});
|
|
84
77
|
} else {
|
|
85
78
|
widget.root = this._shadow;
|
|
86
79
|
widget.customElement = this;
|
|
87
80
|
this._shadow.appendChild(widget.container);
|
|
88
81
|
}
|
|
89
|
-
|
|
90
82
|
return;
|
|
91
83
|
}
|
|
92
84
|
} catch (error) {
|
|
93
85
|
console.error(error);
|
|
94
|
-
|
|
95
86
|
return;
|
|
96
87
|
}
|
|
97
|
-
|
|
98
88
|
try {
|
|
99
89
|
customWidgetDefinition.root = this._shadow;
|
|
100
90
|
customWidgetDefinition.customElement = this;
|
|
101
|
-
|
|
102
91
|
if (!customWidgetDefinition.container) {
|
|
103
92
|
customWidgetDefinition.container = document.createElement('div');
|
|
104
|
-
customWidgetDefinition.container.setAttribute(
|
|
105
|
-
'id',
|
|
106
|
-
'merkur-container',
|
|
107
|
-
);
|
|
93
|
+
customWidgetDefinition.container.setAttribute('id', 'merkur-container');
|
|
108
94
|
}
|
|
109
|
-
|
|
110
95
|
this._shadow.appendChild(customWidgetDefinition.container);
|
|
111
|
-
|
|
112
|
-
this._widget = await createSPAWidget(
|
|
113
|
-
customWidgetDefinition,
|
|
114
|
-
this._shadow,
|
|
115
|
-
);
|
|
116
|
-
|
|
96
|
+
this._widget = await createSPAWidget(customWidgetDefinition, this._shadow);
|
|
117
97
|
this._setDefaultProps();
|
|
118
|
-
|
|
119
98
|
await callbacks?.constructor?.(this._widget, {
|
|
120
99
|
shadow: this._shadow,
|
|
121
|
-
customElement: this
|
|
100
|
+
customElement: this
|
|
122
101
|
});
|
|
123
|
-
|
|
124
102
|
(await callbacks?.mount?.(this._widget, {
|
|
125
103
|
shadow: this._shadow,
|
|
126
|
-
customElement: this
|
|
104
|
+
customElement: this
|
|
127
105
|
})) ?? (await this._widget.mount());
|
|
128
106
|
} catch (error) {
|
|
129
107
|
console.error(error);
|
|
@@ -133,126 +111,88 @@ function registerCustomElement(options) {
|
|
|
133
111
|
console.error(error);
|
|
134
112
|
}
|
|
135
113
|
}
|
|
136
|
-
|
|
137
114
|
async connectedCallback() {
|
|
138
115
|
await this._widgetPromise;
|
|
139
|
-
|
|
140
116
|
this._widget?.connectedCallback?.({
|
|
141
117
|
shadow: this._shadow,
|
|
142
|
-
customElement: this
|
|
118
|
+
customElement: this
|
|
143
119
|
});
|
|
144
|
-
|
|
145
120
|
callbacks?.connectedCallback?.(this._widget, {
|
|
146
121
|
shadow: this._shadow,
|
|
147
|
-
customElement: this
|
|
122
|
+
customElement: this
|
|
148
123
|
});
|
|
149
124
|
}
|
|
150
|
-
|
|
151
125
|
async disconnectedCallback() {
|
|
152
126
|
await this._widgetPromise;
|
|
153
|
-
|
|
154
127
|
this._widget?.disconnectedCallback?.({
|
|
155
128
|
shadow: this._shadow,
|
|
156
|
-
customElement: this
|
|
129
|
+
customElement: this
|
|
157
130
|
});
|
|
158
|
-
|
|
159
131
|
callbacks?.disconnectedCallback?.(this._widget, {
|
|
160
132
|
shadow: this._shadow,
|
|
161
|
-
customElement: this
|
|
133
|
+
customElement: this
|
|
162
134
|
});
|
|
163
135
|
}
|
|
164
|
-
|
|
165
136
|
async adoptedCallback() {
|
|
166
137
|
await this._widgetPromise;
|
|
167
|
-
|
|
168
138
|
this._widget?.adoptedCallback?.({
|
|
169
139
|
shadow: this._shadow,
|
|
170
|
-
customElement: this
|
|
140
|
+
customElement: this
|
|
171
141
|
});
|
|
172
|
-
|
|
173
142
|
callbacks?.adoptedCallback?.(this._widget, {
|
|
174
143
|
shadow: this._shadow,
|
|
175
|
-
customElement: this
|
|
144
|
+
customElement: this
|
|
176
145
|
});
|
|
177
146
|
}
|
|
178
|
-
|
|
179
147
|
async attributeChangedCallback(name, oldValue, newValue) {
|
|
180
148
|
await this._widgetPromise;
|
|
181
|
-
|
|
182
|
-
const
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
this
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
customElement: this,
|
|
195
|
-
},
|
|
196
|
-
);
|
|
197
|
-
|
|
198
|
-
callbacks?.attributeChangedCallback?.(
|
|
199
|
-
this._widget,
|
|
200
|
-
name,
|
|
201
|
-
oldValue,
|
|
202
|
-
newValue,
|
|
203
|
-
{
|
|
204
|
-
shadow: this._shadow,
|
|
205
|
-
customElement: this,
|
|
206
|
-
},
|
|
207
|
-
);
|
|
149
|
+
const camelCaseKey = name.replace(/-([a-z])/g, g => g[1].toUpperCase());
|
|
150
|
+
const parser = attributesParser?.[name] ?? (value => value);
|
|
151
|
+
this._widget?.setProps?.({
|
|
152
|
+
[camelCaseKey]: parser(newValue)
|
|
153
|
+
});
|
|
154
|
+
this._widget?.attributeChangedCallback?.(this._widget, name, oldValue, newValue, {
|
|
155
|
+
shadow: this._shadow,
|
|
156
|
+
customElement: this
|
|
157
|
+
});
|
|
158
|
+
callbacks?.attributeChangedCallback?.(this._widget, name, oldValue, newValue, {
|
|
159
|
+
shadow: this._shadow,
|
|
160
|
+
customElement: this
|
|
161
|
+
});
|
|
208
162
|
}
|
|
209
|
-
|
|
210
163
|
_setDefaultProps() {
|
|
211
164
|
const attributes = this.constructor.observedAttributes;
|
|
212
|
-
if (
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
attributes.forEach((key) => {
|
|
165
|
+
if (Array.isArray(attributes) && typeof this._widget.setProps === 'function') {
|
|
166
|
+
this._widget.props = {
|
|
167
|
+
...this._widget.props
|
|
168
|
+
};
|
|
169
|
+
attributes.forEach(key => {
|
|
218
170
|
if (this.hasAttribute(key)) {
|
|
219
|
-
const camelCaseKey = key.replace(/-([a-z])/g,
|
|
220
|
-
|
|
221
|
-
);
|
|
222
|
-
const parser = attributesParser?.[key] ?? ((value) => value);
|
|
223
|
-
|
|
224
|
-
this._widget.props[camelCaseKey] = parser(
|
|
225
|
-
this.getAttribute(key) ?? this._widget.props[key],
|
|
226
|
-
);
|
|
171
|
+
const camelCaseKey = key.replace(/-([a-z])/g, g => g[1].toUpperCase());
|
|
172
|
+
const parser = attributesParser?.[key] ?? (value => value);
|
|
173
|
+
this._widget.props[camelCaseKey] = parser(this.getAttribute(key) ?? this._widget.props[key]);
|
|
227
174
|
}
|
|
228
175
|
});
|
|
229
176
|
}
|
|
230
177
|
}
|
|
231
178
|
}
|
|
232
|
-
|
|
233
179
|
if (customElements.get(widgetDefinition.name) === undefined) {
|
|
234
180
|
customElements.define(widgetDefinition.name, WidgetElement);
|
|
235
181
|
}
|
|
236
|
-
|
|
237
182
|
return WidgetElement;
|
|
238
183
|
}
|
|
239
|
-
|
|
240
184
|
const PROTECTED_FIELDS = ['__proto__', 'prototype', 'constructor'];
|
|
241
185
|
function deepMerge(target, source) {
|
|
242
|
-
const isObject =
|
|
243
|
-
|
|
186
|
+
const isObject = obj => !!obj && obj.constructor === Object;
|
|
244
187
|
if (!isObject(target) || !isObject(source)) {
|
|
245
188
|
return source;
|
|
246
189
|
}
|
|
247
|
-
|
|
248
|
-
Object.keys(source).forEach((key) => {
|
|
190
|
+
Object.keys(source).forEach(key => {
|
|
249
191
|
if (PROTECTED_FIELDS.includes(key)) {
|
|
250
192
|
return;
|
|
251
193
|
}
|
|
252
|
-
|
|
253
194
|
const targetValue = target[key];
|
|
254
195
|
const sourceValue = source[key];
|
|
255
|
-
|
|
256
196
|
if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
|
|
257
197
|
target[key] = targetValue.concat(sourceValue);
|
|
258
198
|
} else if (isObject(targetValue) && isObject(sourceValue)) {
|
|
@@ -261,7 +201,6 @@ function deepMerge(target, source) {
|
|
|
261
201
|
target[key] = sourceValue;
|
|
262
202
|
}
|
|
263
203
|
});
|
|
264
|
-
|
|
265
204
|
return target;
|
|
266
205
|
}
|
|
267
206
|
|
package/lib/index.mjs
CHANGED
|
@@ -4,22 +4,18 @@ import { loadAssets } from '@merkur/integration';
|
|
|
4
4
|
async function createSPAWidget(widgetDefinition, root) {
|
|
5
5
|
const definition = {
|
|
6
6
|
...widgetDefinition,
|
|
7
|
-
createWidget: widgetDefinition.createWidget
|
|
7
|
+
createWidget: widgetDefinition.createWidget
|
|
8
8
|
};
|
|
9
|
-
|
|
10
9
|
const merkur = getMerkur();
|
|
11
10
|
if (!merkur.isRegistered(definition.name + definition.version)) {
|
|
12
11
|
getMerkur().register(definition);
|
|
13
12
|
}
|
|
14
|
-
|
|
15
13
|
await afterDOMLoad();
|
|
16
14
|
await loadAssets(definition.assets, root);
|
|
17
|
-
|
|
18
15
|
return await getMerkur().create(definition);
|
|
19
16
|
}
|
|
20
|
-
|
|
21
17
|
function afterDOMLoad() {
|
|
22
|
-
return new Promise(
|
|
18
|
+
return new Promise(resolve => {
|
|
23
19
|
if (typeof document !== 'undefined') {
|
|
24
20
|
if (document.readyState !== 'loading') {
|
|
25
21
|
resolve();
|
|
@@ -33,10 +29,13 @@ function afterDOMLoad() {
|
|
|
33
29
|
}
|
|
34
30
|
});
|
|
35
31
|
}
|
|
36
|
-
|
|
37
32
|
function registerCustomElement(options) {
|
|
38
|
-
const {
|
|
39
|
-
|
|
33
|
+
const {
|
|
34
|
+
widgetDefinition,
|
|
35
|
+
callbacks,
|
|
36
|
+
observedAttributes,
|
|
37
|
+
attributesParser
|
|
38
|
+
} = deepMerge({}, options);
|
|
40
39
|
class HTMLCustomElement extends HTMLElement {
|
|
41
40
|
static get observedAttributes() {
|
|
42
41
|
return observedAttributes ?? [];
|
|
@@ -48,80 +47,59 @@ function registerCustomElement(options) {
|
|
|
48
47
|
}
|
|
49
48
|
_init() {}
|
|
50
49
|
}
|
|
51
|
-
|
|
52
50
|
class WidgetElement extends HTMLCustomElement {
|
|
53
51
|
_init() {
|
|
54
52
|
try {
|
|
55
53
|
super._init();
|
|
56
54
|
const customWidgetDefinition = deepMerge({}, widgetDefinition);
|
|
57
|
-
|
|
58
55
|
this._widgetPromise = (async () => {
|
|
59
|
-
this._shadow = this.attachShadow({
|
|
60
|
-
|
|
56
|
+
this._shadow = this.attachShadow({
|
|
57
|
+
mode: 'open'
|
|
58
|
+
});
|
|
61
59
|
try {
|
|
62
60
|
const widget = await callbacks?.getInstance?.();
|
|
63
|
-
|
|
64
61
|
if (widget && widget.name && widget.version) {
|
|
65
62
|
this._widget = widget;
|
|
66
|
-
|
|
67
63
|
await afterDOMLoad();
|
|
68
64
|
await loadAssets(widget.assets, this._shadow);
|
|
69
|
-
|
|
70
65
|
this._setDefaultProps();
|
|
71
|
-
|
|
72
66
|
await callbacks?.reconstructor?.(this._widget, {
|
|
73
67
|
shadow: this._shadow,
|
|
74
|
-
customElement: this
|
|
68
|
+
customElement: this
|
|
75
69
|
});
|
|
76
|
-
|
|
77
70
|
if (typeof callbacks?.remount === 'function') {
|
|
78
71
|
await callbacks?.remount?.(this._widget, {
|
|
79
72
|
shadow: this._shadow,
|
|
80
|
-
customElement: this
|
|
73
|
+
customElement: this
|
|
81
74
|
});
|
|
82
75
|
} else {
|
|
83
76
|
widget.root = this._shadow;
|
|
84
77
|
widget.customElement = this;
|
|
85
78
|
this._shadow.appendChild(widget.container);
|
|
86
79
|
}
|
|
87
|
-
|
|
88
80
|
return;
|
|
89
81
|
}
|
|
90
82
|
} catch (error) {
|
|
91
83
|
console.error(error);
|
|
92
|
-
|
|
93
84
|
return;
|
|
94
85
|
}
|
|
95
|
-
|
|
96
86
|
try {
|
|
97
87
|
customWidgetDefinition.root = this._shadow;
|
|
98
88
|
customWidgetDefinition.customElement = this;
|
|
99
|
-
|
|
100
89
|
if (!customWidgetDefinition.container) {
|
|
101
90
|
customWidgetDefinition.container = document.createElement('div');
|
|
102
|
-
customWidgetDefinition.container.setAttribute(
|
|
103
|
-
'id',
|
|
104
|
-
'merkur-container',
|
|
105
|
-
);
|
|
91
|
+
customWidgetDefinition.container.setAttribute('id', 'merkur-container');
|
|
106
92
|
}
|
|
107
|
-
|
|
108
93
|
this._shadow.appendChild(customWidgetDefinition.container);
|
|
109
|
-
|
|
110
|
-
this._widget = await createSPAWidget(
|
|
111
|
-
customWidgetDefinition,
|
|
112
|
-
this._shadow,
|
|
113
|
-
);
|
|
114
|
-
|
|
94
|
+
this._widget = await createSPAWidget(customWidgetDefinition, this._shadow);
|
|
115
95
|
this._setDefaultProps();
|
|
116
|
-
|
|
117
96
|
await callbacks?.constructor?.(this._widget, {
|
|
118
97
|
shadow: this._shadow,
|
|
119
|
-
customElement: this
|
|
98
|
+
customElement: this
|
|
120
99
|
});
|
|
121
|
-
|
|
122
100
|
(await callbacks?.mount?.(this._widget, {
|
|
123
101
|
shadow: this._shadow,
|
|
124
|
-
customElement: this
|
|
102
|
+
customElement: this
|
|
125
103
|
})) ?? (await this._widget.mount());
|
|
126
104
|
} catch (error) {
|
|
127
105
|
console.error(error);
|
|
@@ -131,126 +109,88 @@ function registerCustomElement(options) {
|
|
|
131
109
|
console.error(error);
|
|
132
110
|
}
|
|
133
111
|
}
|
|
134
|
-
|
|
135
112
|
async connectedCallback() {
|
|
136
113
|
await this._widgetPromise;
|
|
137
|
-
|
|
138
114
|
this._widget?.connectedCallback?.({
|
|
139
115
|
shadow: this._shadow,
|
|
140
|
-
customElement: this
|
|
116
|
+
customElement: this
|
|
141
117
|
});
|
|
142
|
-
|
|
143
118
|
callbacks?.connectedCallback?.(this._widget, {
|
|
144
119
|
shadow: this._shadow,
|
|
145
|
-
customElement: this
|
|
120
|
+
customElement: this
|
|
146
121
|
});
|
|
147
122
|
}
|
|
148
|
-
|
|
149
123
|
async disconnectedCallback() {
|
|
150
124
|
await this._widgetPromise;
|
|
151
|
-
|
|
152
125
|
this._widget?.disconnectedCallback?.({
|
|
153
126
|
shadow: this._shadow,
|
|
154
|
-
customElement: this
|
|
127
|
+
customElement: this
|
|
155
128
|
});
|
|
156
|
-
|
|
157
129
|
callbacks?.disconnectedCallback?.(this._widget, {
|
|
158
130
|
shadow: this._shadow,
|
|
159
|
-
customElement: this
|
|
131
|
+
customElement: this
|
|
160
132
|
});
|
|
161
133
|
}
|
|
162
|
-
|
|
163
134
|
async adoptedCallback() {
|
|
164
135
|
await this._widgetPromise;
|
|
165
|
-
|
|
166
136
|
this._widget?.adoptedCallback?.({
|
|
167
137
|
shadow: this._shadow,
|
|
168
|
-
customElement: this
|
|
138
|
+
customElement: this
|
|
169
139
|
});
|
|
170
|
-
|
|
171
140
|
callbacks?.adoptedCallback?.(this._widget, {
|
|
172
141
|
shadow: this._shadow,
|
|
173
|
-
customElement: this
|
|
142
|
+
customElement: this
|
|
174
143
|
});
|
|
175
144
|
}
|
|
176
|
-
|
|
177
145
|
async attributeChangedCallback(name, oldValue, newValue) {
|
|
178
146
|
await this._widgetPromise;
|
|
179
|
-
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
this
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
customElement: this,
|
|
193
|
-
},
|
|
194
|
-
);
|
|
195
|
-
|
|
196
|
-
callbacks?.attributeChangedCallback?.(
|
|
197
|
-
this._widget,
|
|
198
|
-
name,
|
|
199
|
-
oldValue,
|
|
200
|
-
newValue,
|
|
201
|
-
{
|
|
202
|
-
shadow: this._shadow,
|
|
203
|
-
customElement: this,
|
|
204
|
-
},
|
|
205
|
-
);
|
|
147
|
+
const camelCaseKey = name.replace(/-([a-z])/g, g => g[1].toUpperCase());
|
|
148
|
+
const parser = attributesParser?.[name] ?? (value => value);
|
|
149
|
+
this._widget?.setProps?.({
|
|
150
|
+
[camelCaseKey]: parser(newValue)
|
|
151
|
+
});
|
|
152
|
+
this._widget?.attributeChangedCallback?.(this._widget, name, oldValue, newValue, {
|
|
153
|
+
shadow: this._shadow,
|
|
154
|
+
customElement: this
|
|
155
|
+
});
|
|
156
|
+
callbacks?.attributeChangedCallback?.(this._widget, name, oldValue, newValue, {
|
|
157
|
+
shadow: this._shadow,
|
|
158
|
+
customElement: this
|
|
159
|
+
});
|
|
206
160
|
}
|
|
207
|
-
|
|
208
161
|
_setDefaultProps() {
|
|
209
162
|
const attributes = this.constructor.observedAttributes;
|
|
210
|
-
if (
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
attributes.forEach((key) => {
|
|
163
|
+
if (Array.isArray(attributes) && typeof this._widget.setProps === 'function') {
|
|
164
|
+
this._widget.props = {
|
|
165
|
+
...this._widget.props
|
|
166
|
+
};
|
|
167
|
+
attributes.forEach(key => {
|
|
216
168
|
if (this.hasAttribute(key)) {
|
|
217
|
-
const camelCaseKey = key.replace(/-([a-z])/g,
|
|
218
|
-
|
|
219
|
-
);
|
|
220
|
-
const parser = attributesParser?.[key] ?? ((value) => value);
|
|
221
|
-
|
|
222
|
-
this._widget.props[camelCaseKey] = parser(
|
|
223
|
-
this.getAttribute(key) ?? this._widget.props[key],
|
|
224
|
-
);
|
|
169
|
+
const camelCaseKey = key.replace(/-([a-z])/g, g => g[1].toUpperCase());
|
|
170
|
+
const parser = attributesParser?.[key] ?? (value => value);
|
|
171
|
+
this._widget.props[camelCaseKey] = parser(this.getAttribute(key) ?? this._widget.props[key]);
|
|
225
172
|
}
|
|
226
173
|
});
|
|
227
174
|
}
|
|
228
175
|
}
|
|
229
176
|
}
|
|
230
|
-
|
|
231
177
|
if (customElements.get(widgetDefinition.name) === undefined) {
|
|
232
178
|
customElements.define(widgetDefinition.name, WidgetElement);
|
|
233
179
|
}
|
|
234
|
-
|
|
235
180
|
return WidgetElement;
|
|
236
181
|
}
|
|
237
|
-
|
|
238
182
|
const PROTECTED_FIELDS = ['__proto__', 'prototype', 'constructor'];
|
|
239
183
|
function deepMerge(target, source) {
|
|
240
|
-
const isObject =
|
|
241
|
-
|
|
184
|
+
const isObject = obj => !!obj && obj.constructor === Object;
|
|
242
185
|
if (!isObject(target) || !isObject(source)) {
|
|
243
186
|
return source;
|
|
244
187
|
}
|
|
245
|
-
|
|
246
|
-
Object.keys(source).forEach((key) => {
|
|
188
|
+
Object.keys(source).forEach(key => {
|
|
247
189
|
if (PROTECTED_FIELDS.includes(key)) {
|
|
248
190
|
return;
|
|
249
191
|
}
|
|
250
|
-
|
|
251
192
|
const targetValue = target[key];
|
|
252
193
|
const sourceValue = source[key];
|
|
253
|
-
|
|
254
194
|
if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
|
|
255
195
|
target[key] = targetValue.concat(sourceValue);
|
|
256
196
|
} else if (isObject(targetValue) && isObject(sourceValue)) {
|
|
@@ -259,7 +199,6 @@ function deepMerge(target, source) {
|
|
|
259
199
|
target[key] = sourceValue;
|
|
260
200
|
}
|
|
261
201
|
});
|
|
262
|
-
|
|
263
202
|
return target;
|
|
264
203
|
}
|
|
265
204
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@merkur/integration-custom-element",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.40.0",
|
|
4
4
|
"description": "Merkur module for easy integration with other apps with custom element.",
|
|
5
5
|
"main": "lib/index",
|
|
6
6
|
"module": "lib/index",
|
|
@@ -29,8 +29,7 @@
|
|
|
29
29
|
"preversion": "npm test",
|
|
30
30
|
"test": "jest --no-watchman -c ./jest.config.js",
|
|
31
31
|
"test:es:version": "es-check es11 ./lib/index.mjs --module && es-check es9 ./lib/index.es9.mjs --module && es-check es9 ./lib/index.es9.cjs --module",
|
|
32
|
-
"build": "rollup -c rollup.config.mjs"
|
|
33
|
-
"prepare": "npm run build"
|
|
32
|
+
"build": "rollup -c rollup.config.mjs"
|
|
34
33
|
},
|
|
35
34
|
"repository": {
|
|
36
35
|
"type": "git",
|
|
@@ -51,12 +50,12 @@
|
|
|
51
50
|
},
|
|
52
51
|
"homepage": "https://merkur.js.org/",
|
|
53
52
|
"devDependencies": {
|
|
54
|
-
"@merkur/core": "^0.
|
|
55
|
-
"@merkur/integration": "^0.
|
|
53
|
+
"@merkur/core": "^0.40.0",
|
|
54
|
+
"@merkur/integration": "^0.40.0"
|
|
56
55
|
},
|
|
57
56
|
"peerDependencies": {
|
|
58
57
|
"@merkur/core": "*",
|
|
59
58
|
"@merkur/integration": "*"
|
|
60
59
|
},
|
|
61
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "a7bf45d46a5c0fca7130ae6a86e1cd94e5894ca2"
|
|
62
61
|
}
|