@yschimke/remote-compose-player-cmp 2.2.1 → 2.4.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 +29 -3
- package/dist/fonts/GoogleSansFlex-OFL.txt +93 -0
- package/dist/fonts/NotoSansMono-OFL.txt +93 -0
- package/dist/fonts/NotoSansMono-Variable.ttf +0 -0
- package/dist/fonts/NotoSerif-OFL.txt +93 -0
- package/dist/fonts/NotoSerif-Variable.ttf +0 -0
- package/dist/fonts/README.md +40 -52
- package/dist/fonts/fonts.json +216 -12
- package/dist/fonts/google-sans-flex-variable.ttf +0 -0
- package/dist/fonts/inter-variable.ttf +0 -0
- package/dist/fonts/jetbrains-mono-variable.ttf +0 -0
- package/dist/fonts/orbitron-variable.ttf +0 -0
- package/dist/fonts/space-grotesk-variable.ttf +0 -0
- package/dist/rc-cmp-player.js +411 -0
- package/dist/rcPlayer.import-object.mjs +68 -12
- package/dist/rcPlayer.wasm +0 -0
- package/package.json +1 -1
- package/dist/fonts/DroidSansMono.ttf +0 -0
- package/dist/fonts/LICENSE.txt +0 -201
- package/dist/fonts/NotoSerif-Regular.ttf +0 -0
- package/dist/fonts/Roboto-Medium.ttf +0 -0
- package/dist/fonts/Roboto-Regular.ttf +0 -0
- package/dist/fonts/google-sans-flex-400.ttf +0 -0
- package/dist/fonts/google-sans-flex-700.ttf +0 -0
- package/dist/fonts/inter-400.ttf +0 -0
- package/dist/fonts/inter-700.ttf +0 -0
- package/dist/fonts/jetbrains-mono-400.ttf +0 -0
- package/dist/fonts/jetbrains-mono-700.ttf +0 -0
- package/dist/fonts/orbitron-400.ttf +0 -0
- package/dist/fonts/orbitron-700.ttf +0 -0
- package/dist/fonts/space-grotesk-400.ttf +0 -0
- package/dist/fonts/space-grotesk-700.ttf +0 -0
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* rc-cmp-player.js — include the CMP Remote Compose player in an HTML page.
|
|
3
|
+
*
|
|
4
|
+
* <script src="https://cdn.example/rc-player/rc-cmp-player.js"></script>
|
|
5
|
+
* <rc-cmp-player src="/documents/watch-face.rc" width="400" height="400"></rc-cmp-player>
|
|
6
|
+
*
|
|
7
|
+
* const player = RcCmp.createPlayer(container, { width: 400, height: 400 });
|
|
8
|
+
* await player.loadFromArrayBuffer(bytes); // resolves once the document is on screen
|
|
9
|
+
*
|
|
10
|
+
* The player itself is the Compose Multiplatform renderer compiled to Wasm, which runs as its own
|
|
11
|
+
* page (`index.html` beside this file) inside an iframe; this script owns that iframe and the embed
|
|
12
|
+
* contract it speaks (docs/design/RC_PLAYER_EMBED.md). It talks to the page only by `postMessage`
|
|
13
|
+
* — `rc-player-hello` until the page answers, then `rc-player-load` per document, each settled by an
|
|
14
|
+
* `rc-player-state` reply carrying its id — so the player may be served from **any origin**, a CDN
|
|
15
|
+
* included, and the page including it needs no access to the iframe. The API mirrors the
|
|
16
|
+
* TypeScript player's `RC.createPlayer`, so a page can drive both the same way.
|
|
17
|
+
*
|
|
18
|
+
* Documents always cross as bytes. `loadFromUrl` fetches on *this* page, with this page's
|
|
19
|
+
* credentials and origin, so a document beside the page needs no CORS headers for the player.
|
|
20
|
+
*
|
|
21
|
+
* By default the player page is resolved next to this script. Wherever it is served from, it must
|
|
22
|
+
* come back as `text/html`, with `.wasm` as `application/wasm`, and be allowed in a frame.
|
|
23
|
+
*
|
|
24
|
+
* Plain script, no dependencies, no build step; defines `window.RcCmp` and `<rc-cmp-player>`.
|
|
25
|
+
*/
|
|
26
|
+
(function () {
|
|
27
|
+
'use strict';
|
|
28
|
+
|
|
29
|
+
// Resolved now: `document.currentScript` is only set while this file is first evaluated.
|
|
30
|
+
var scriptBase =
|
|
31
|
+
(document.currentScript && document.currentScript.src) || document.baseURI || location.href;
|
|
32
|
+
var DEFAULT_PLAYER_URL = new URL('index.html', scriptBase).href;
|
|
33
|
+
|
|
34
|
+
var HELLO_INTERVAL_MS = 100;
|
|
35
|
+
var START_TIMEOUT_MS = 60000;
|
|
36
|
+
var LOAD_TIMEOUT_MS = 60000;
|
|
37
|
+
|
|
38
|
+
var instanceCounter = 0;
|
|
39
|
+
|
|
40
|
+
function superseded() {
|
|
41
|
+
var error = new Error('superseded by a newer load');
|
|
42
|
+
error.name = 'AbortError';
|
|
43
|
+
return error;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** A fresh copy of the bytes, which this script owns and may transfer to the player. */
|
|
47
|
+
function copyBytes(data) {
|
|
48
|
+
// A brand check, not `instanceof`, so a buffer from another frame's realm is accepted too.
|
|
49
|
+
if (Object.prototype.toString.call(data) === '[object ArrayBuffer]') {
|
|
50
|
+
return new Uint8Array(data).slice();
|
|
51
|
+
}
|
|
52
|
+
if (ArrayBuffer.isView(data)) {
|
|
53
|
+
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength).slice();
|
|
54
|
+
}
|
|
55
|
+
throw new TypeError('expected an ArrayBuffer or an ArrayBufferView');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Create a CMP player inside `container`.
|
|
60
|
+
*
|
|
61
|
+
* Options (all optional):
|
|
62
|
+
* width, height CSS pixels of the player; the document lays out into exactly this box.
|
|
63
|
+
* theme 'light' | 'dark' | 'auto' (default: follow prefers-color-scheme).
|
|
64
|
+
* lenient true draws nothing for unsupported operations instead of refusing.
|
|
65
|
+
* namedValues [{ kind, name, value }] overrides, as RC_PLAYER_EMBED.md describes.
|
|
66
|
+
* fontsBase directory of the player's `fonts.json`, relative to the player page.
|
|
67
|
+
* handoffDelayMs cold-start tail before `ready`; keep the default unless you composite.
|
|
68
|
+
* playerUrl the player page; default `index.html` next to this script.
|
|
69
|
+
* src | buffer a first document, as a URL or bytes.
|
|
70
|
+
* onLoad(info) called after each document is on screen.
|
|
71
|
+
* onError(error) called when a document fails to load.
|
|
72
|
+
* onEvent(message) the player's structured messages: host actions, debug messages.
|
|
73
|
+
*/
|
|
74
|
+
function createPlayer(container, options) {
|
|
75
|
+
options = Object.assign({}, options);
|
|
76
|
+
var instance = 'rc-cmp-' + ++instanceCounter;
|
|
77
|
+
var playerOrigin = new URL(options.playerUrl || DEFAULT_PLAYER_URL, location.href).origin;
|
|
78
|
+
|
|
79
|
+
var iframe = null; // replaced on every navigation, so its window identifies the page
|
|
80
|
+
var page = 0; // navigation counter; a navigation cancels every wait on the old page
|
|
81
|
+
var generation = 0; // load counter; a newer load cancels an older one's wait
|
|
82
|
+
var started = null; // promise: the current page answered `rc-player-hello`
|
|
83
|
+
var onHello = null; // resolver for `started`, keyed to the hello id
|
|
84
|
+
var pending = null; // { id, resolve, reject } of the load awaiting its `rc-player-state`
|
|
85
|
+
var current = null; // the last document requested, as bytes, kept for reloads
|
|
86
|
+
var destroyed = false;
|
|
87
|
+
|
|
88
|
+
function playerUrl() {
|
|
89
|
+
var url = new URL(options.playerUrl || DEFAULT_PLAYER_URL, location.href);
|
|
90
|
+
if (options.theme === 'light' || options.theme === 'dark') {
|
|
91
|
+
url.searchParams.set('theme', options.theme);
|
|
92
|
+
}
|
|
93
|
+
if (options.lenient) url.searchParams.set('lenient', '1');
|
|
94
|
+
if (options.namedValues) url.searchParams.set('namedValues', JSON.stringify(options.namedValues));
|
|
95
|
+
if (options.fontsBase) url.searchParams.set('fontsBase', options.fontsBase);
|
|
96
|
+
if (options.handoffDelayMs != null) {
|
|
97
|
+
url.searchParams.set('handoffDelayMs', String(options.handoffDelayMs));
|
|
98
|
+
}
|
|
99
|
+
return url.href;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function applySize() {
|
|
103
|
+
if (!iframe) return;
|
|
104
|
+
if (options.width != null) iframe.style.width = options.width + 'px';
|
|
105
|
+
if (options.height != null) iframe.style.height = options.height + 'px';
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function settlePending(error) {
|
|
109
|
+
if (!pending) return;
|
|
110
|
+
var settling = pending;
|
|
111
|
+
pending = null;
|
|
112
|
+
settling.reject(error);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function onMessage(event) {
|
|
116
|
+
if (!iframe || event.source !== iframe.contentWindow || event.origin !== playerOrigin) return;
|
|
117
|
+
var data = event.data;
|
|
118
|
+
if (!data || typeof data !== 'object') return;
|
|
119
|
+
if (data.type === 'rc-player-hello') {
|
|
120
|
+
if (onHello && data.id === onHello.id) onHello.resolve(data);
|
|
121
|
+
} else if (data.type === 'rc-player-state') {
|
|
122
|
+
if (!pending || data.id !== pending.id) return; // an earlier load's answer
|
|
123
|
+
var settling = pending;
|
|
124
|
+
pending = null;
|
|
125
|
+
if (data.state === 'ready') settling.resolve();
|
|
126
|
+
else settling.reject(new Error(data.error || 'the player reported an error'));
|
|
127
|
+
} else if (
|
|
128
|
+
options.onEvent &&
|
|
129
|
+
typeof data.type === 'string' &&
|
|
130
|
+
data.type.indexOf('cp-rc-') === 0
|
|
131
|
+
) {
|
|
132
|
+
options.onEvent(data);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
window.addEventListener('message', onMessage);
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* (Re)start the player page in a new iframe and say hello until it answers. The page only
|
|
139
|
+
* installs its listener once the Wasm module is running, so the first hellos go unheard; and a
|
|
140
|
+
* hello posted while the frame still holds `about:blank` is dropped by the target origin.
|
|
141
|
+
*/
|
|
142
|
+
function navigate() {
|
|
143
|
+
page += 1;
|
|
144
|
+
var forPage = page;
|
|
145
|
+
settlePending(superseded());
|
|
146
|
+
var next = document.createElement('iframe');
|
|
147
|
+
next.title = options.title || 'Remote Compose player';
|
|
148
|
+
next.style.border = '0';
|
|
149
|
+
next.style.display = 'block';
|
|
150
|
+
next.style.background = 'transparent';
|
|
151
|
+
next.setAttribute('allowtransparency', 'true');
|
|
152
|
+
next.src = playerUrl();
|
|
153
|
+
if (iframe) iframe.replaceWith(next);
|
|
154
|
+
else container.appendChild(next);
|
|
155
|
+
iframe = next;
|
|
156
|
+
applySize();
|
|
157
|
+
|
|
158
|
+
var helloId = instance + '.hello.' + forPage;
|
|
159
|
+
started = new Promise(function (resolve, reject) {
|
|
160
|
+
var deadline = Date.now() + START_TIMEOUT_MS;
|
|
161
|
+
onHello = { id: helloId, resolve: resolve };
|
|
162
|
+
(function hello() {
|
|
163
|
+
if (destroyed || forPage !== page) return reject(superseded());
|
|
164
|
+
if (!onHello || onHello.id !== helloId) return; // answered
|
|
165
|
+
if (Date.now() > deadline) {
|
|
166
|
+
return reject(
|
|
167
|
+
new Error(
|
|
168
|
+
'rc-cmp-player: the player page at ' +
|
|
169
|
+
playerOrigin +
|
|
170
|
+
' did not answer within ' +
|
|
171
|
+
START_TIMEOUT_MS / 1000 +
|
|
172
|
+
's; check it is served as text/html (and .wasm as application/wasm) and may ' +
|
|
173
|
+
'be framed',
|
|
174
|
+
),
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
try {
|
|
178
|
+
next.contentWindow.postMessage({ type: 'rc-player-hello', id: helloId }, playerOrigin);
|
|
179
|
+
} catch (e) {
|
|
180
|
+
// The frame is between documents; the next tick tries again.
|
|
181
|
+
}
|
|
182
|
+
setTimeout(hello, HELLO_INTERVAL_MS);
|
|
183
|
+
})();
|
|
184
|
+
}).then(function (reply) {
|
|
185
|
+
if (onHello && onHello.id === helloId) onHello = null;
|
|
186
|
+
return reply;
|
|
187
|
+
});
|
|
188
|
+
// A rejection here is reported by whichever load awaits it.
|
|
189
|
+
started.catch(function () {});
|
|
190
|
+
return started;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function load(bytes) {
|
|
194
|
+
if (destroyed) return Promise.reject(new Error('rc-cmp-player: destroyed'));
|
|
195
|
+
current = bytes;
|
|
196
|
+
generation += 1;
|
|
197
|
+
var forGeneration = generation;
|
|
198
|
+
var forPage = page;
|
|
199
|
+
settlePending(superseded());
|
|
200
|
+
return started
|
|
201
|
+
.then(function () {
|
|
202
|
+
if (destroyed || forGeneration !== generation || forPage !== page) throw superseded();
|
|
203
|
+
var id = instance + '.load.' + forGeneration;
|
|
204
|
+
return new Promise(function (resolve, reject) {
|
|
205
|
+
var timer = setTimeout(function () {
|
|
206
|
+
if (pending && pending.id === id) {
|
|
207
|
+
pending = null;
|
|
208
|
+
reject(
|
|
209
|
+
new Error(
|
|
210
|
+
'rc-cmp-player: the document did not finish loading within ' +
|
|
211
|
+
LOAD_TIMEOUT_MS / 1000 +
|
|
212
|
+
's',
|
|
213
|
+
),
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
}, LOAD_TIMEOUT_MS);
|
|
217
|
+
pending = {
|
|
218
|
+
id: id,
|
|
219
|
+
resolve: function () {
|
|
220
|
+
clearTimeout(timer);
|
|
221
|
+
resolve();
|
|
222
|
+
},
|
|
223
|
+
reject: function (error) {
|
|
224
|
+
clearTimeout(timer);
|
|
225
|
+
reject(error);
|
|
226
|
+
},
|
|
227
|
+
};
|
|
228
|
+
// Our own copy each time, transferred rather than cloned; `current` stays intact for
|
|
229
|
+
// a reload.
|
|
230
|
+
var payload = bytes.slice();
|
|
231
|
+
iframe.contentWindow.postMessage(
|
|
232
|
+
{ type: 'rc-player-load', id: id, bytes: payload.buffer },
|
|
233
|
+
playerOrigin,
|
|
234
|
+
[payload.buffer],
|
|
235
|
+
);
|
|
236
|
+
});
|
|
237
|
+
})
|
|
238
|
+
.then(function () {
|
|
239
|
+
return { iframe: iframe };
|
|
240
|
+
})
|
|
241
|
+
// A separate stage, so the player's own `error` above reaches `onError` too: a rejection
|
|
242
|
+
// handler never sees what its sibling fulfilment handler throws.
|
|
243
|
+
.then(
|
|
244
|
+
function (info) {
|
|
245
|
+
if (options.onLoad) options.onLoad(info);
|
|
246
|
+
return info;
|
|
247
|
+
},
|
|
248
|
+
function (error) {
|
|
249
|
+
if (error && error.name !== 'AbortError' && options.onError) options.onError(error);
|
|
250
|
+
throw error;
|
|
251
|
+
},
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function reloadCurrent() {
|
|
256
|
+
navigate();
|
|
257
|
+
if (current) return load(current);
|
|
258
|
+
return started.then(function () {});
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function failed(error) {
|
|
262
|
+
if (options.onError) options.onError(error);
|
|
263
|
+
return Promise.reject(error);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
var handle = {
|
|
267
|
+
/** The current iframe; replaced when the player page restarts (theme, lenient). */
|
|
268
|
+
get iframe() {
|
|
269
|
+
return iframe;
|
|
270
|
+
},
|
|
271
|
+
/** Resolves when the player page is up and ready for documents. */
|
|
272
|
+
ready: function () {
|
|
273
|
+
return started.then(function () {});
|
|
274
|
+
},
|
|
275
|
+
/** Fetched here, on the including page, then handed over as bytes. */
|
|
276
|
+
loadFromUrl: function (url) {
|
|
277
|
+
var forGeneration = ++generation;
|
|
278
|
+
settlePending(superseded());
|
|
279
|
+
return fetch(new URL(url, location.href).href).then(
|
|
280
|
+
function (response) {
|
|
281
|
+
if (!response.ok) return failed(new Error('HTTP ' + response.status + ' for ' + url));
|
|
282
|
+
return response.arrayBuffer().then(function (buffer) {
|
|
283
|
+
// A newer request made while this one was downloading wins.
|
|
284
|
+
if (forGeneration !== generation) throw superseded();
|
|
285
|
+
return load(new Uint8Array(buffer));
|
|
286
|
+
});
|
|
287
|
+
},
|
|
288
|
+
function (error) {
|
|
289
|
+
return failed(error);
|
|
290
|
+
},
|
|
291
|
+
);
|
|
292
|
+
},
|
|
293
|
+
loadFromArrayBuffer: function (data) {
|
|
294
|
+
var bytes;
|
|
295
|
+
try {
|
|
296
|
+
bytes = copyBytes(data);
|
|
297
|
+
} catch (error) {
|
|
298
|
+
return Promise.reject(error);
|
|
299
|
+
}
|
|
300
|
+
return load(bytes);
|
|
301
|
+
},
|
|
302
|
+
loadFromBase64: function (base64) {
|
|
303
|
+
var binary = atob(base64);
|
|
304
|
+
var bytes = new Uint8Array(binary.length);
|
|
305
|
+
for (var i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
306
|
+
return load(bytes);
|
|
307
|
+
},
|
|
308
|
+
/** The iframe's viewport *is* the layout size, so a resize reflows without a reload. */
|
|
309
|
+
resize: function (width, height) {
|
|
310
|
+
options.width = width;
|
|
311
|
+
options.height = height;
|
|
312
|
+
applySize();
|
|
313
|
+
},
|
|
314
|
+
/** `?theme` belongs to the player page, so this restarts it and reloads the document. */
|
|
315
|
+
setTheme: function (theme) {
|
|
316
|
+
options.theme = theme;
|
|
317
|
+
return reloadCurrent();
|
|
318
|
+
},
|
|
319
|
+
/** Same: `?lenient` belongs to the page. */
|
|
320
|
+
setLenient: function (lenient) {
|
|
321
|
+
options.lenient = !!lenient;
|
|
322
|
+
return reloadCurrent();
|
|
323
|
+
},
|
|
324
|
+
destroy: function () {
|
|
325
|
+
destroyed = true;
|
|
326
|
+
settlePending(superseded());
|
|
327
|
+
window.removeEventListener('message', onMessage);
|
|
328
|
+
if (iframe) iframe.remove();
|
|
329
|
+
iframe = null;
|
|
330
|
+
},
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
navigate();
|
|
334
|
+
if (options.buffer) handle.loadFromArrayBuffer(options.buffer).catch(function () {});
|
|
335
|
+
else if (options.src) handle.loadFromUrl(options.src).catch(function () {});
|
|
336
|
+
return handle;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
var BaseElement = typeof HTMLElement !== 'undefined' ? HTMLElement : function () {};
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* <rc-cmp-player src width height theme lenient player-url>
|
|
343
|
+
*
|
|
344
|
+
* Dispatches `rc-load` and `rc-error` (detail: the Error) after each document, and `rc-event`
|
|
345
|
+
* (detail: the player's message) for host actions and debug messages.
|
|
346
|
+
*/
|
|
347
|
+
class RcCmpPlayerElement extends BaseElement {
|
|
348
|
+
static get observedAttributes() {
|
|
349
|
+
return ['src', 'width', 'height', 'theme', 'lenient'];
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
connectedCallback() {
|
|
353
|
+
if (this._handle) return;
|
|
354
|
+
var self = this;
|
|
355
|
+
var shadow = this.shadowRoot || this.attachShadow({ mode: 'open' });
|
|
356
|
+
shadow.innerHTML = '<style>:host{display:inline-block}</style>';
|
|
357
|
+
this._handle = createPlayer(shadow, {
|
|
358
|
+
width: this._number('width', 400),
|
|
359
|
+
height: this._number('height', 400),
|
|
360
|
+
theme: this.getAttribute('theme') || undefined,
|
|
361
|
+
lenient: this.hasAttribute('lenient'),
|
|
362
|
+
playerUrl: this.getAttribute('player-url') || undefined,
|
|
363
|
+
onLoad: function () {
|
|
364
|
+
self.dispatchEvent(new CustomEvent('rc-load'));
|
|
365
|
+
},
|
|
366
|
+
onError: function (error) {
|
|
367
|
+
self.dispatchEvent(new CustomEvent('rc-error', { detail: error }));
|
|
368
|
+
},
|
|
369
|
+
onEvent: function (message) {
|
|
370
|
+
self.dispatchEvent(new CustomEvent('rc-event', { detail: message }));
|
|
371
|
+
},
|
|
372
|
+
});
|
|
373
|
+
var src = this.getAttribute('src');
|
|
374
|
+
if (src) this._handle.loadFromUrl(src).catch(function () {});
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
disconnectedCallback() {
|
|
378
|
+
if (this._handle) this._handle.destroy();
|
|
379
|
+
this._handle = null;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
attributeChangedCallback(name, oldValue, value) {
|
|
383
|
+
var handle = this._handle;
|
|
384
|
+
if (!handle || oldValue === value) return;
|
|
385
|
+
if (name === 'src' && value) handle.loadFromUrl(value).catch(function () {});
|
|
386
|
+
else if (name === 'width' || name === 'height') {
|
|
387
|
+
handle.resize(this._number('width', 400), this._number('height', 400));
|
|
388
|
+
} else if (name === 'theme') handle.setTheme(value || 'auto').catch(function () {});
|
|
389
|
+
else if (name === 'lenient') handle.setLenient(value !== null).catch(function () {});
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/** The underlying handle, for loading bytes: `element.player.loadFromArrayBuffer(buffer)`. */
|
|
393
|
+
get player() {
|
|
394
|
+
return this._handle;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
_number(name, fallback) {
|
|
398
|
+
var value = parseInt(this.getAttribute(name) || '', 10);
|
|
399
|
+
return value > 0 ? value : fallback;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
window.RcCmp = {
|
|
404
|
+
createPlayer: createPlayer,
|
|
405
|
+
RcCmpPlayerElement: RcCmpPlayerElement,
|
|
406
|
+
defaultPlayerUrl: DEFAULT_PLAYER_URL,
|
|
407
|
+
};
|
|
408
|
+
if (typeof customElements !== 'undefined' && !customElements.get('rc-cmp-player')) {
|
|
409
|
+
customElements.define('rc-cmp-player', RcCmpPlayerElement);
|
|
410
|
+
}
|
|
411
|
+
})();
|
|
@@ -446,8 +446,15 @@ const js_code = {
|
|
|
446
446
|
}),
|
|
447
447
|
'ee.schimke.composeai.rcplayer.wasm.installDocumentSwap' : () => {
|
|
448
448
|
window.__rcPlayerSwap = { pending: null, resolve: null };
|
|
449
|
-
|
|
450
|
-
|
|
449
|
+
// The origin of the parent that last sent a message; see `__rcPlayerPost`.
|
|
450
|
+
var host = { origin: null };
|
|
451
|
+
window.__rcPlayerPost = function (message) {
|
|
452
|
+
window.parent.postMessage(message, window.location.origin);
|
|
453
|
+
if (host.origin && host.origin !== window.location.origin) {
|
|
454
|
+
window.parent.postMessage(message, host.origin);
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
var deliver = function (request) {
|
|
451
458
|
var root = document.documentElement;
|
|
452
459
|
root.dataset.rcPlayerState = 'loading';
|
|
453
460
|
delete root.dataset.rcPlayerError;
|
|
@@ -460,6 +467,52 @@ const js_code = {
|
|
|
460
467
|
swap.pending = request;
|
|
461
468
|
}
|
|
462
469
|
};
|
|
470
|
+
window.rcPlayerLoad = function (source) {
|
|
471
|
+
deliver({ url: String(source) });
|
|
472
|
+
};
|
|
473
|
+
var base64Of = function (data) {
|
|
474
|
+
var bytes;
|
|
475
|
+
// A brand check, not `instanceof`: a same-origin parent calling in hands over an
|
|
476
|
+
// ArrayBuffer from its own realm, which is not an instance of this page's ArrayBuffer.
|
|
477
|
+
if (Object.prototype.toString.call(data) === '[object ArrayBuffer]') {
|
|
478
|
+
bytes = new Uint8Array(data);
|
|
479
|
+
} else if (ArrayBuffer.isView(data)) {
|
|
480
|
+
bytes = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
481
|
+
} else {
|
|
482
|
+
throw new TypeError('rcPlayerLoadBytes expects an ArrayBuffer or an ArrayBufferView');
|
|
483
|
+
}
|
|
484
|
+
var chunks = [], chunkSize = 0x8000;
|
|
485
|
+
for (var i = 0; i < bytes.length; i += chunkSize) {
|
|
486
|
+
chunks.push(String.fromCharCode.apply(null, bytes.subarray(i, i + chunkSize)));
|
|
487
|
+
}
|
|
488
|
+
return btoa(chunks.join(''));
|
|
489
|
+
};
|
|
490
|
+
window.rcPlayerLoadBytes = function (data) {
|
|
491
|
+
deliver({ base64: base64Of(data) });
|
|
492
|
+
};
|
|
493
|
+
window.addEventListener('message', function (event) {
|
|
494
|
+
if (window.parent === window || event.source !== window.parent) return;
|
|
495
|
+
var data = event.data;
|
|
496
|
+
if (!data || typeof data !== 'object' || typeof data.type !== 'string') return;
|
|
497
|
+
var id = data.id == null ? null : String(data.id);
|
|
498
|
+
if (data.type === 'rc-player-hello') {
|
|
499
|
+
host.origin = event.origin;
|
|
500
|
+
event.source.postMessage(
|
|
501
|
+
{ type: 'rc-player-hello', id: id, contractVersion: window.rcPlayerContractVersion },
|
|
502
|
+
event.origin);
|
|
503
|
+
} else if (data.type === 'rc-player-load') {
|
|
504
|
+
host.origin = event.origin;
|
|
505
|
+
try {
|
|
506
|
+
if (data.bytes != null) deliver({ base64: base64Of(data.bytes), id: id });
|
|
507
|
+
else if (data.src != null) deliver({ url: String(data.src), id: id });
|
|
508
|
+
else throw new TypeError('rc-player-load needs bytes or src');
|
|
509
|
+
} catch (error) {
|
|
510
|
+
event.source.postMessage(
|
|
511
|
+
{ type: 'rc-player-state', id: id, state: 'error', error: String(error.message || error) },
|
|
512
|
+
event.origin);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
});
|
|
463
516
|
},
|
|
464
517
|
'ee.schimke.composeai.rcplayer.wasm.nextDocumentSwap' : () => new Promise(function (resolve) {
|
|
465
518
|
var swap = window.__rcPlayerSwap;
|
|
@@ -471,6 +524,9 @@ const js_code = {
|
|
|
471
524
|
swap.resolve = resolve;
|
|
472
525
|
}
|
|
473
526
|
}),
|
|
527
|
+
'ee.schimke.composeai.rcplayer.wasm.swapUrl' : (request) => request.url === undefined ? null : request.url,
|
|
528
|
+
'ee.schimke.composeai.rcplayer.wasm.swapBase64' : (request) => request.base64 === undefined ? null : request.base64,
|
|
529
|
+
'ee.schimke.composeai.rcplayer.wasm.swapId' : (request) => request.id == null ? null : request.id,
|
|
474
530
|
'ee.schimke.composeai.rcplayer.wasm.queryParameterFromLocation' : (name) => new URL(window.location.href).searchParams.get(name),
|
|
475
531
|
'ee.schimke.composeai.rcplayer.wasm.flattenNamedValuesFromLocation' : () => (function () {
|
|
476
532
|
try {
|
|
@@ -485,16 +541,16 @@ const js_code = {
|
|
|
485
541
|
})(),
|
|
486
542
|
'ee.schimke.composeai.rcplayer.wasm.decodeUriComponentJs' : (value) => decodeURIComponent(value),
|
|
487
543
|
'ee.schimke.composeai.rcplayer.wasm.publishContractVersion' : (version) => (window.rcPlayerContractVersion = version, document.documentElement.dataset.rcPlayerContract = String(version)),
|
|
488
|
-
'ee.schimke.composeai.rcplayer.wasm.postReady' : () => (document.documentElement.dataset.rcPlayerState = 'ready', window.
|
|
489
|
-
'ee.schimke.composeai.rcplayer.wasm.reportFailure' : (message) => (document.documentElement.dataset.rcPlayerState = 'error', document.documentElement.dataset.rcPlayerError = message, console.error('[rc-player-wasm] ' + message), window.
|
|
490
|
-
'ee.schimke.composeai.rcplayer.wasm.postDebugMessage' : (message, value, flags) => (document.documentElement.dataset.rcPlayerDebugMessage = message, document.documentElement.dataset.rcPlayerDebugValue = String(value), document.documentElement.dataset.rcPlayerDebugFlags = String(flags), console.debug('[rc-player-wasm] ' + message + ' ' + String(value)), window.
|
|
491
|
-
'ee.schimke.composeai.rcplayer.wasm.postHostAction' : (actionId) => (document.documentElement.dataset.rcPlayerAction = String(actionId), document.documentElement.dataset.rcPlayerActionTrace = (document.documentElement.dataset.rcPlayerActionTrace ? document.documentElement.dataset.rcPlayerActionTrace + ',' : '') + String(actionId), window.
|
|
492
|
-
'ee.schimke.composeai.rcplayer.wasm.postHostMetadataAction' : (actionId, metadata) => (document.documentElement.dataset.rcPlayerAction = String(actionId), document.documentElement.dataset.rcPlayerActionTrace = (document.documentElement.dataset.rcPlayerActionTrace ? document.documentElement.dataset.rcPlayerActionTrace + ',' : '') + String(actionId), document.documentElement.dataset.rcPlayerMetadata = metadata, window.
|
|
493
|
-
'ee.schimke.composeai.rcplayer.wasm.postHostNamedActionNone' : (name) => (document.documentElement.dataset.rcPlayerNamedAction = name, document.documentElement.dataset.rcPlayerNamedActionValue = 'none', window.
|
|
494
|
-
'ee.schimke.composeai.rcplayer.wasm.postHostNamedActionFloat' : (name, value) => (document.documentElement.dataset.rcPlayerNamedAction = name, document.documentElement.dataset.rcPlayerNamedActionValue = 'float:' + String(value), window.
|
|
495
|
-
'ee.schimke.composeai.rcplayer.wasm.postHostNamedActionInt' : (name, value) => (document.documentElement.dataset.rcPlayerNamedAction = name, document.documentElement.dataset.rcPlayerNamedActionValue = 'int:' + String(value), window.
|
|
496
|
-
'ee.schimke.composeai.rcplayer.wasm.postHostNamedActionText' : (name, value) => (document.documentElement.dataset.rcPlayerNamedAction = name, document.documentElement.dataset.rcPlayerNamedActionValue = 'string:' + value, window.
|
|
497
|
-
'ee.schimke.composeai.rcplayer.wasm.postHostNamedActionFloatList' : (name, encoded) => (document.documentElement.dataset.rcPlayerNamedAction = name, document.documentElement.dataset.rcPlayerNamedActionValue = 'float-array:' + encoded, window.
|
|
544
|
+
'ee.schimke.composeai.rcplayer.wasm.postReady' : (hostId) => (document.documentElement.dataset.rcPlayerState = 'ready', window.__rcPlayerPost('cp-rc-wasm-ready'), window.__rcPlayerPost({ type: 'rc-player-state', id: hostId, state: 'ready' })),
|
|
545
|
+
'ee.schimke.composeai.rcplayer.wasm.reportFailure' : (message, hostId) => (document.documentElement.dataset.rcPlayerState = 'error', document.documentElement.dataset.rcPlayerError = message, console.error('[rc-player-wasm] ' + message), window.__rcPlayerPost('cp-rc-wasm-error:' + message), window.__rcPlayerPost({ type: 'rc-player-state', id: hostId, state: 'error', error: message })),
|
|
546
|
+
'ee.schimke.composeai.rcplayer.wasm.postDebugMessage' : (message, value, flags) => (document.documentElement.dataset.rcPlayerDebugMessage = message, document.documentElement.dataset.rcPlayerDebugValue = String(value), document.documentElement.dataset.rcPlayerDebugFlags = String(flags), console.debug('[rc-player-wasm] ' + message + ' ' + String(value)), window.__rcPlayerPost({ type: 'cp-rc-debug-message', message: message, value: value, flags: flags })),
|
|
547
|
+
'ee.schimke.composeai.rcplayer.wasm.postHostAction' : (actionId) => (document.documentElement.dataset.rcPlayerAction = String(actionId), document.documentElement.dataset.rcPlayerActionTrace = (document.documentElement.dataset.rcPlayerActionTrace ? document.documentElement.dataset.rcPlayerActionTrace + ',' : '') + String(actionId), window.__rcPlayerPost({ type: 'cp-rc-host-action', actionId: actionId })),
|
|
548
|
+
'ee.schimke.composeai.rcplayer.wasm.postHostMetadataAction' : (actionId, metadata) => (document.documentElement.dataset.rcPlayerAction = String(actionId), document.documentElement.dataset.rcPlayerActionTrace = (document.documentElement.dataset.rcPlayerActionTrace ? document.documentElement.dataset.rcPlayerActionTrace + ',' : '') + String(actionId), document.documentElement.dataset.rcPlayerMetadata = metadata, window.__rcPlayerPost({ type: 'cp-rc-host-action', actionId: actionId, metadata: metadata })),
|
|
549
|
+
'ee.schimke.composeai.rcplayer.wasm.postHostNamedActionNone' : (name) => (document.documentElement.dataset.rcPlayerNamedAction = name, document.documentElement.dataset.rcPlayerNamedActionValue = 'none', window.__rcPlayerPost({ type: 'cp-rc-host-named-action', name: name, valueType: 'none', value: null })),
|
|
550
|
+
'ee.schimke.composeai.rcplayer.wasm.postHostNamedActionFloat' : (name, value) => (document.documentElement.dataset.rcPlayerNamedAction = name, document.documentElement.dataset.rcPlayerNamedActionValue = 'float:' + String(value), window.__rcPlayerPost({ type: 'cp-rc-host-named-action', name: name, valueType: 'float', value: value })),
|
|
551
|
+
'ee.schimke.composeai.rcplayer.wasm.postHostNamedActionInt' : (name, value) => (document.documentElement.dataset.rcPlayerNamedAction = name, document.documentElement.dataset.rcPlayerNamedActionValue = 'int:' + String(value), window.__rcPlayerPost({ type: 'cp-rc-host-named-action', name: name, valueType: 'int', value: value })),
|
|
552
|
+
'ee.schimke.composeai.rcplayer.wasm.postHostNamedActionText' : (name, value) => (document.documentElement.dataset.rcPlayerNamedAction = name, document.documentElement.dataset.rcPlayerNamedActionValue = 'string:' + value, window.__rcPlayerPost({ type: 'cp-rc-host-named-action', name: name, valueType: 'string', value: value })),
|
|
553
|
+
'ee.schimke.composeai.rcplayer.wasm.postHostNamedActionFloatList' : (name, encoded) => (document.documentElement.dataset.rcPlayerNamedAction = name, document.documentElement.dataset.rcPlayerNamedActionValue = 'float-array:' + encoded, window.__rcPlayerPost({ type: 'cp-rc-host-named-action', name: name, valueType: 'float-array', value: encoded === '' ? [] : encoded.split(',').map(Number) }))
|
|
498
554
|
}
|
|
499
555
|
|
|
500
556
|
const StringConstantsProxy = new Proxy({}, {
|
package/dist/rcPlayer.wasm
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yschimke/remote-compose-player-cmp",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"description": "The Compose Multiplatform / Wasm Remote Compose player, as a drop-in browser bundle. Renders .rc documents in an iframe driven by query parameters and window.rcPlayerLoad.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"remote-compose",
|
|
Binary file
|