@croct/plug 0.13.1 → 0.13.2
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/constants.d.ts +1 -1
- package/constants.js +1 -1
- package/package.json +2 -2
- package/preview.d.ts +1 -1
- package/preview.js +28 -9
- package/preview.js.map +1 -1
- package/src/preview.ts +39 -8
package/constants.d.ts
CHANGED
|
@@ -2,4 +2,4 @@ export declare const CDN_URL = "https://cdn.croct.io/js/v1/lib/plug.js";
|
|
|
2
2
|
export declare const PLAYGROUND_ORIGIN = "https://play.croct.com";
|
|
3
3
|
export declare const PLAYGROUND_CONNECT_URL = "https://play.croct.com/connect.html";
|
|
4
4
|
export declare const PREVIEW_WIDGET_ORIGIN = "https://cdn.croct.io";
|
|
5
|
-
export declare const PREVIEW_WIDGET_URL = "https://cdn.croct.io/js/v1/lib/plug/widget-0.13.
|
|
5
|
+
export declare const PREVIEW_WIDGET_URL = "https://cdn.croct.io/js/v1/lib/plug/widget-0.13.2.html";
|
package/constants.js
CHANGED
|
@@ -5,5 +5,5 @@ exports.CDN_URL = 'https://cdn.croct.io/js/v1/lib/plug.js';
|
|
|
5
5
|
exports.PLAYGROUND_ORIGIN = 'https://play.croct.com';
|
|
6
6
|
exports.PLAYGROUND_CONNECT_URL = 'https://play.croct.com/connect.html';
|
|
7
7
|
exports.PREVIEW_WIDGET_ORIGIN = 'https://cdn.croct.io';
|
|
8
|
-
exports.PREVIEW_WIDGET_URL = 'https://cdn.croct.io/js/v1/lib/plug/widget-0.13.
|
|
8
|
+
exports.PREVIEW_WIDGET_URL = 'https://cdn.croct.io/js/v1/lib/plug/widget-0.13.2.html';
|
|
9
9
|
//# sourceMappingURL=constants.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@croct/plug",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.2",
|
|
4
4
|
"description": "A fully-featured devkit for building natively personalized applications.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@croct/eslint-plugin": "^0.6.3",
|
|
42
|
-
"@rollup/plugin-commonjs": "^
|
|
42
|
+
"@rollup/plugin-commonjs": "^25.0.0",
|
|
43
43
|
"@rollup/plugin-node-resolve": "^15.0.1",
|
|
44
44
|
"@rollup/plugin-replace": "^5.0.1",
|
|
45
45
|
"@rollup/plugin-terser": "^0.4.0",
|
package/preview.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export declare class PreviewPlugin implements Plugin {
|
|
|
9
9
|
private static readonly PREVIEW_PARAMS;
|
|
10
10
|
private readonly tokenStore;
|
|
11
11
|
private readonly logger;
|
|
12
|
-
private
|
|
12
|
+
private cleanUp;
|
|
13
13
|
constructor(configuration: Configuration);
|
|
14
14
|
enable(): void;
|
|
15
15
|
disable(): void;
|
package/preview.js
CHANGED
|
@@ -2,14 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.factory = exports.PreviewPlugin = void 0;
|
|
4
4
|
const error_1 = require("@croct/sdk/error");
|
|
5
|
-
const uuid_1 = require("@croct/sdk/uuid");
|
|
6
5
|
const token_1 = require("./sdk/token");
|
|
7
6
|
const constants_1 = require("./constants");
|
|
8
7
|
const PREVIEW_PARAMETER = 'croct-preview';
|
|
9
8
|
const PREVIEW_EXIT = 'exit';
|
|
10
9
|
class PreviewPlugin {
|
|
11
10
|
constructor(configuration) {
|
|
12
|
-
this.
|
|
11
|
+
this.cleanUp = [];
|
|
13
12
|
this.tokenStore = configuration.tokenStore;
|
|
14
13
|
this.logger = configuration.logger;
|
|
15
14
|
}
|
|
@@ -30,8 +29,8 @@ class PreviewPlugin {
|
|
|
30
29
|
}
|
|
31
30
|
}
|
|
32
31
|
disable() {
|
|
33
|
-
|
|
34
|
-
(
|
|
32
|
+
const callbacks = this.cleanUp.splice(0);
|
|
33
|
+
callbacks.forEach(cleanUp => cleanUp());
|
|
35
34
|
}
|
|
36
35
|
updateToken(data) {
|
|
37
36
|
if (data === PREVIEW_EXIT) {
|
|
@@ -46,6 +45,7 @@ class PreviewPlugin {
|
|
|
46
45
|
this.logger.debug('Preview token expired.');
|
|
47
46
|
token = null;
|
|
48
47
|
}
|
|
48
|
+
this.logger.debug('Preview token updated.');
|
|
49
49
|
this.tokenStore.setToken(token);
|
|
50
50
|
}
|
|
51
51
|
catch (error) {
|
|
@@ -77,7 +77,7 @@ class PreviewPlugin {
|
|
|
77
77
|
}
|
|
78
78
|
insertWidget(url) {
|
|
79
79
|
const widget = this.createWidget(url);
|
|
80
|
-
|
|
80
|
+
const onMessage = (event) => {
|
|
81
81
|
if (event.origin !== constants_1.PREVIEW_WIDGET_ORIGIN) {
|
|
82
82
|
return;
|
|
83
83
|
}
|
|
@@ -94,12 +94,32 @@ class PreviewPlugin {
|
|
|
94
94
|
widget.style.height = `${event.data.height}px`;
|
|
95
95
|
break;
|
|
96
96
|
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
97
|
+
};
|
|
98
|
+
window.addEventListener('message', onMessage);
|
|
99
|
+
this.cleanUp.push(() => window.removeEventListener('message', onMessage));
|
|
100
|
+
const insert = () => {
|
|
101
|
+
const container = document.body;
|
|
102
|
+
container.prepend(widget);
|
|
103
|
+
this.cleanUp.push(() => container.removeChild(widget));
|
|
104
|
+
this.logger.debug('Preview widget mounted.');
|
|
105
|
+
// Reinsert the widget if it is removed from the DOM (e.g. by a framework)
|
|
106
|
+
const observer = new MutationObserver(() => {
|
|
107
|
+
if (!container.contains(widget)) {
|
|
108
|
+
container.prepend(widget);
|
|
109
|
+
this.logger.debug('Preview widget removed from DOM, reinserting.');
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
observer.observe(container, { childList: true });
|
|
113
|
+
// Make sure the observer is disconnected before the widget is removed.
|
|
114
|
+
// Otherwise, the observer may reinsert the widget after it is removed.
|
|
115
|
+
this.cleanUp.unshift(() => observer.disconnect());
|
|
116
|
+
};
|
|
117
|
+
if (document.readyState !== 'loading') {
|
|
100
118
|
insert();
|
|
101
119
|
}
|
|
102
120
|
else {
|
|
121
|
+
this.logger.debug('Waiting for document to be ready.');
|
|
122
|
+
this.cleanUp.push(() => window.removeEventListener('DOMContentLoaded', insert));
|
|
103
123
|
window.addEventListener('DOMContentLoaded', insert);
|
|
104
124
|
}
|
|
105
125
|
}
|
|
@@ -112,7 +132,6 @@ class PreviewPlugin {
|
|
|
112
132
|
}
|
|
113
133
|
createWidget(url) {
|
|
114
134
|
const widget = document.createElement('iframe');
|
|
115
|
-
widget.setAttribute('id', this.widgetId);
|
|
116
135
|
widget.setAttribute('src', url);
|
|
117
136
|
widget.setAttribute('sandbox', 'allow-scripts allow-same-origin');
|
|
118
137
|
widget.style.position = 'fixed';
|
package/preview.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"preview.js","sourceRoot":"","sources":["src/preview.ts"],"names":[],"mappings":";;;AAAA,4CAA6C;
|
|
1
|
+
{"version":3,"file":"preview.js","sourceRoot":"","sources":["src/preview.ts"],"names":[],"mappings":";;;AAAA,4CAA6C;AAG7C,uCAA8C;AAC9C,2CAAsE;AAEtE,MAAM,iBAAiB,GAAG,eAAe,CAAC;AAC1C,MAAM,YAAY,GAAG,MAAM,CAAC;AAO5B,MAAa,aAAa;IActB,YAAmB,aAA4B;QAFvC,YAAO,GAAsB,EAAE,CAAC;QAGpC,IAAI,CAAC,UAAU,GAAG,aAAa,CAAC,UAAU,CAAC;QAC3C,IAAI,CAAC,MAAM,GAAG,aAAa,CAAC,MAAM,CAAC;IACvC,CAAC;IAEM,MAAM;QACT,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QAC1C,MAAM,WAAW,GAAG,GAAG,CAAC,YAAY,CAAC,GAAG,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ED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import {formatCause} from '@croct/sdk/error';
|
|
2
|
-
import {uuid4} from '@croct/sdk/uuid';
|
|
3
2
|
import {Logger} from './sdk';
|
|
4
3
|
import {Plugin, PluginFactory} from './plugin';
|
|
5
4
|
import {Token, TokenStore} from './sdk/token';
|
|
@@ -25,7 +24,7 @@ export class PreviewPlugin implements Plugin {
|
|
|
25
24
|
|
|
26
25
|
private readonly logger: Logger;
|
|
27
26
|
|
|
28
|
-
private
|
|
27
|
+
private cleanUp: Array<() => void> = [];
|
|
29
28
|
|
|
30
29
|
public constructor(configuration: Configuration) {
|
|
31
30
|
this.tokenStore = configuration.tokenStore;
|
|
@@ -55,7 +54,9 @@ export class PreviewPlugin implements Plugin {
|
|
|
55
54
|
}
|
|
56
55
|
|
|
57
56
|
public disable(): void {
|
|
58
|
-
|
|
57
|
+
const callbacks = this.cleanUp.splice(0);
|
|
58
|
+
|
|
59
|
+
callbacks.forEach(cleanUp => cleanUp());
|
|
59
60
|
}
|
|
60
61
|
|
|
61
62
|
private updateToken(data: string): void {
|
|
@@ -77,6 +78,8 @@ export class PreviewPlugin implements Plugin {
|
|
|
77
78
|
token = null;
|
|
78
79
|
}
|
|
79
80
|
|
|
81
|
+
this.logger.debug('Preview token updated.');
|
|
82
|
+
|
|
80
83
|
this.tokenStore.setToken(token);
|
|
81
84
|
} catch (error) {
|
|
82
85
|
this.tokenStore.setToken(null);
|
|
@@ -118,7 +121,7 @@ export class PreviewPlugin implements Plugin {
|
|
|
118
121
|
private insertWidget(url: string): void {
|
|
119
122
|
const widget = this.createWidget(url);
|
|
120
123
|
|
|
121
|
-
|
|
124
|
+
const onMessage = (event: MessageEvent): void => {
|
|
122
125
|
if (event.origin !== PREVIEW_WIDGET_ORIGIN) {
|
|
123
126
|
return;
|
|
124
127
|
}
|
|
@@ -142,13 +145,42 @@ export class PreviewPlugin implements Plugin {
|
|
|
142
145
|
|
|
143
146
|
break;
|
|
144
147
|
}
|
|
145
|
-
}
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
window.addEventListener('message', onMessage);
|
|
151
|
+
|
|
152
|
+
this.cleanUp.push(() => window.removeEventListener('message', onMessage));
|
|
153
|
+
|
|
154
|
+
const insert = (): void => {
|
|
155
|
+
const container = document.body;
|
|
156
|
+
|
|
157
|
+
container.prepend(widget);
|
|
158
|
+
|
|
159
|
+
this.cleanUp.push(() => container.removeChild(widget));
|
|
160
|
+
|
|
161
|
+
this.logger.debug('Preview widget mounted.');
|
|
162
|
+
|
|
163
|
+
// Reinsert the widget if it is removed from the DOM (e.g. by a framework)
|
|
164
|
+
const observer = new MutationObserver(() => {
|
|
165
|
+
if (!container.contains(widget)) {
|
|
166
|
+
container.prepend(widget);
|
|
167
|
+
|
|
168
|
+
this.logger.debug('Preview widget removed from DOM, reinserting.');
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
observer.observe(container, {childList: true});
|
|
146
173
|
|
|
147
|
-
|
|
174
|
+
// Make sure the observer is disconnected before the widget is removed.
|
|
175
|
+
// Otherwise, the observer may reinsert the widget after it is removed.
|
|
176
|
+
this.cleanUp.unshift(() => observer.disconnect());
|
|
177
|
+
};
|
|
148
178
|
|
|
149
|
-
if (document.readyState
|
|
179
|
+
if (document.readyState !== 'loading') {
|
|
150
180
|
insert();
|
|
151
181
|
} else {
|
|
182
|
+
this.logger.debug('Waiting for document to be ready.');
|
|
183
|
+
this.cleanUp.push(() => window.removeEventListener('DOMContentLoaded', insert));
|
|
152
184
|
window.addEventListener('DOMContentLoaded', insert);
|
|
153
185
|
}
|
|
154
186
|
}
|
|
@@ -166,7 +198,6 @@ export class PreviewPlugin implements Plugin {
|
|
|
166
198
|
private createWidget(url: string): HTMLIFrameElement {
|
|
167
199
|
const widget = document.createElement('iframe');
|
|
168
200
|
|
|
169
|
-
widget.setAttribute('id', this.widgetId);
|
|
170
201
|
widget.setAttribute('src', url);
|
|
171
202
|
widget.setAttribute('sandbox', 'allow-scripts allow-same-origin');
|
|
172
203
|
|