@web-applets/sdk 0.2.0-alpha.9 → 0.2.1
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/dist/elements/applet-frame.js +3 -99
- package/dist/{applets → sdk/src/applets}/applet-scope.js +10 -6
- package/dist/sdk/src/debug.js +9 -0
- package/dist/sdk/src/elements/applet-frame.js +111 -0
- package/dist/utils/common-utils.d.mts +1 -0
- package/dist/utils/common-utils.mjs +3 -0
- package/dist/web-applets.min.js +3 -2
- package/package.json +8 -5
- package/dist/debug.js +0 -6
- /package/dist/{applets → sdk/src/applets}/actions.d.ts +0 -0
- /package/dist/{applets → sdk/src/applets}/actions.js +0 -0
- /package/dist/{applets → sdk/src/applets}/applet-factory.d.ts +0 -0
- /package/dist/{applets → sdk/src/applets}/applet-factory.js +0 -0
- /package/dist/{applets → sdk/src/applets}/applet-scope.d.ts +0 -0
- /package/dist/{applets → sdk/src/applets}/applet.d.ts +0 -0
- /package/dist/{applets → sdk/src/applets}/applet.js +0 -0
- /package/dist/{applets → sdk/src/applets}/errors.d.ts +0 -0
- /package/dist/{applets → sdk/src/applets}/errors.js +0 -0
- /package/dist/{applets → sdk/src/applets}/events.d.ts +0 -0
- /package/dist/{applets → sdk/src/applets}/events.js +0 -0
- /package/dist/{constants.d.ts → sdk/src/constants.d.ts} +0 -0
- /package/dist/{constants.js → sdk/src/constants.js} +0 -0
- /package/dist/{debug.d.ts → sdk/src/debug.d.ts} +0 -0
- /package/dist/{elements → sdk/src/elements}/applet-frame.d.ts +0 -0
- /package/dist/{index.d.ts → sdk/src/index.d.ts} +0 -0
- /package/dist/{index.js → sdk/src/index.js} +0 -0
- /package/dist/{messages.d.ts → sdk/src/messages.d.ts} +0 -0
- /package/dist/{messages.js → sdk/src/messages.js} +0 -0
- /package/dist/{polyfill.d.ts → sdk/src/polyfill.d.ts} +0 -0
- /package/dist/{polyfill.js → sdk/src/polyfill.js} +0 -0
- /package/dist/{utils.d.ts → sdk/src/utils.d.ts} +0 -0
- /package/dist/{utils.js → sdk/src/utils.js} +0 -0
@@ -1,70 +1,6 @@
|
|
1
|
-
var
|
2
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
5
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
6
|
-
};
|
7
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
11
|
-
};
|
12
|
-
var _AppletFrameElement_instances, _AppletFrameElement_root, _AppletFrameElement_src, _AppletFrameElement_applet, _AppletFrameElement_dispatchEventAndHandler, _AppletFrameElement_iframe, _AppletFrameElement_loadApplet, _AppletFrameElement_resizeContainer;
|
13
|
-
import { applets } from '../index.js';
|
14
|
-
import { dispatchEventAndHandler } from '../utils.js';
|
15
|
-
export class AppletFrameElement extends HTMLElement {
|
16
|
-
constructor() {
|
17
|
-
super(...arguments);
|
18
|
-
_AppletFrameElement_instances.add(this);
|
19
|
-
_AppletFrameElement_root.set(this, void 0);
|
20
|
-
_AppletFrameElement_src.set(this, void 0);
|
21
|
-
_AppletFrameElement_applet.set(this, void 0);
|
22
|
-
_AppletFrameElement_dispatchEventAndHandler.set(this, dispatchEventAndHandler.bind(this));
|
23
|
-
_AppletFrameElement_iframe.set(this, document.createElement('iframe'));
|
24
|
-
}
|
25
|
-
connectedCallback() {
|
26
|
-
__classPrivateFieldSet(this, _AppletFrameElement_root, this.attachShadow({ mode: 'closed' }), "f");
|
27
|
-
__classPrivateFieldGet(this, _AppletFrameElement_root, "f").appendChild(__classPrivateFieldGet(this, _AppletFrameElement_iframe, "f"));
|
28
|
-
const styles = document.createElement('style');
|
29
|
-
styles.textContent = this.styles;
|
30
|
-
__classPrivateFieldGet(this, _AppletFrameElement_root, "f").appendChild(styles);
|
31
|
-
this.src = this.getAttribute('src');
|
32
|
-
}
|
33
|
-
get contentWindow() {
|
34
|
-
return __classPrivateFieldGet(this, _AppletFrameElement_iframe, "f").contentWindow;
|
35
|
-
}
|
36
|
-
set src(value) {
|
37
|
-
__classPrivateFieldSet(this, _AppletFrameElement_src, value, "f");
|
38
|
-
__classPrivateFieldSet(this, _AppletFrameElement_applet, undefined, "f");
|
39
|
-
__classPrivateFieldGet(this, _AppletFrameElement_instances, "m", _AppletFrameElement_loadApplet).call(this, value);
|
40
|
-
}
|
41
|
-
get src() {
|
42
|
-
return __classPrivateFieldGet(this, _AppletFrameElement_src, "f") || '';
|
43
|
-
}
|
44
|
-
attributeChangedCallback(name, oldValue, newValue) {
|
45
|
-
if (name === 'src') {
|
46
|
-
this.src = newValue;
|
47
|
-
}
|
48
|
-
}
|
49
|
-
get applet() {
|
50
|
-
return __classPrivateFieldGet(this, _AppletFrameElement_applet, "f");
|
51
|
-
}
|
52
|
-
set data(data) {
|
53
|
-
if (this.applet) {
|
54
|
-
this.applet.data = data;
|
55
|
-
}
|
56
|
-
else {
|
57
|
-
const listener = () => {
|
58
|
-
if (this.applet)
|
59
|
-
this.applet.data = data;
|
60
|
-
this.removeEventListener('load', listener);
|
61
|
-
};
|
62
|
-
this.addEventListener('load', listener);
|
63
|
-
}
|
64
|
-
}
|
65
|
-
get styles() {
|
66
|
-
return /*css*/ `
|
1
|
+
(()=>{var d=class extends Error{constructor(t){super(t),this.name="AppletExecutionError"}},m=class extends Error{constructor(t){super(t),this.name="AppletConnectionError"}};function c(o){typeof this[`on${o.type}`]=="function"&&this[`on${o.type}`](o),this.dispatchEvent(o)}var r=class extends Event{constructor(t,e){super(t,{bubbles:e?.bubbles,composed:e?.composed,cancelable:e?.cancelable}),this.data=e?.data,this.actions=e?.actions}};function T(o,...t){}var a={log:T};var A=class extends EventTarget{#s;#o={};#t;#i;#n;#e;#a;#r;#p;constructor(t){super(),a.log("Applet","Constructor called"),this.#s=t,this.#n=c.bind(this),this.#c();let e=s=>{s.source===this.#s&&"type"in s.data&&s.data.type==="appletconnect"&&(a.log("Applet","Recieved message",s.data),this.#c(),this.removeEventListener("message",e))};window.addEventListener("message",e)}#c(){this.#e&&this.#e.close();let t=new MessageChannel,e={type:"appletconnect"};a.log("Applet","Send message",e),this.#e=t.port1,this.#e.onmessage=this.#h.bind(this),this.#s.postMessage(e,"*",[t.port2]),this.#a=this.#e.postMessage.bind(this.#e)}#h(t){let e=t.data;switch(a.log("Applet","Recieved message",e),e.type){case"register":let s=e;this.#t=s.manifest;let n=new r("connect");this.#n(n),this.#o=s.actions,this.#d(s.actions),this.#i=s.data,this.#l(s.data);break;case"data":let i=e;this.#i=i.data,this.#l(i.data);break;case"resize":let p=e;this.#r=p.width,this.#p=p.height;let h=new r("resize");this.#n(h);break;case"actions":let g=e;this.#o=g.actions,this.#d(g.actions);break}}#l(t){let e=new r("data",{data:t});this.#n(e)}#d(t){let e=new r("actions",{actions:t});this.#n(e)}async sendAction(t,e){let s={id:crypto.randomUUID(),type:"action",actionId:t,arguments:e};return new Promise((n,i)=>{this.#a(s);let p=setTimeout(()=>{i(new d(`Applet action handler failed to complete before timeout (${1e4}ms)`))},1e4),h=g=>{let l=g.data;if(["actioncomplete","actionerror"].includes(l.type)&&"id"in l&&l.id===s.id)if(this.#e.removeEventListener("message",h),clearTimeout(p),l.type==="actionerror"){let w=l;i(new d(w.message))}else n()};this.#e.addEventListener("message",h)})}get data(){return this.#i}set data(t){this.#i=t;let e={type:"data",data:t};this.#a(e)}get window(){return this.#s}get manifest(){return this.#t}get actions(){return this.#o}get width(){return this.#r}get height(){return this.#p}};function v(o){return Object.keys(o).length===0}var u=class extends EventTarget{#s={};#o;#t;#i;#n;#e;#a;#r;constructor(t){super(),a.log("AppletScope","Constructor called"),this.#n=c.bind(this),t&&(this.#o=t);let e=n=>{if(n.source===window.parent&&n.data.type==="appletconnect"&&n.ports&&n.ports.length>0){a.log("AppletScope","Recieved message",n.data);let i=n.ports[0];this.#e=i.postMessage.bind(i),i.onmessage=this.#c.bind(this),this.removeEventListener("message",e),this.#p()}};window.addEventListener("message",e);let s={type:"appletconnect"};window.parent.postMessage(s,"*"),a.log("AppletScope","Send message",s)}async#p(){let t=this.manifest??await this.#g();this.#o=t||{},this.#t=this.#t||t?.actions||{};let e={type:"register",manifest:this.#o,actions:this.#t,data:this.#i};this.#e(e),a.log("AppletScope","Send message",e);let s=new r("connect");this.#n(s),this.#l()}#c(t){let e=t.data;switch(a.log("AppletScope","Recieved message",e),e.type){case"data":"data"in e&&(this.data=e.data);break;case"action":"type"in e&&e.type==="action"&&"id"in e&&typeof e.id=="string"&&"actionId"in e&&typeof e.actionId=="string"&&"arguments"in e&&this.#h(e);break}}async#h(t){let{actionId:e,arguments:s,id:n}=t;if(Object.keys(this.#s).includes(e))try{await this.#s[e](s);let i={type:"actioncomplete",id:n};this.#e(i)}catch(i){let p={type:"actionerror",id:n,message:i.message};this.#e(p),console.error(i)}}#l(){new ResizeObserver(e=>{for(let s of e)this.#d({width:s.contentRect.width,height:s.contentRect.height})}).observe(document.querySelector("html"))}#d({width:t,height:e}){this.#a=t,this.#r=e;let s={type:"resize",width:t,height:e};a.log("AppletScope","Send message",s),this.#e(s)}async#g(){let t=document.querySelector('link[rel="manifest"]');if(!t){console.warn("No manifest link found");return}try{let s=await(await fetch(t.href)).json();for(let n in s.actions){let i=s.actions[n];i.params_schema&&!v(this.#t.params_schema)&&(i.params_schema=void 0)}return s}catch{return}}setActionHandler(t,e){this.#s[t]=e}defineAction(t,e){let{handler:s,...n}=e;s&&(this.#s[t]=s),this.actions={...this.actions,[t]:n}}set actions(t){if(!t)return;this.#t=t;let e={type:"actions",actions:this.#t};a.log("AppletScope","Send message",e),this.#e&&this.#e(e);let s=new r("actions",{actions:t});setTimeout(()=>this.#n(s),1)}get actions(){return this.#t}get manifest(){return this.#o}get actionHandlers(){return this.#s}set actionHandlers(t){this.#s=t}set data(t){this.#i=t;let e={type:"data",data:t};a.log("AppletScope","Send message",e),this.#e&&this.#e(e);let s=new r("data",{data:t});setTimeout(()=>this.#n(s),1)}get data(){return this.#i}get width(){return this.#a}get height(){return this.#r}};var f=class{async connect(t){return new Promise((e,s)=>{let n=new A(t),i=setTimeout(()=>{s(new m(`Applet failed to connect before the timeout was reached (${1e4}ms)`))},1e4),p=()=>{e(n),n.removeEventListener("connect",p),clearTimeout(i)};n.addEventListener("connect",p)})}register(t){return new u(t)}};var b=new f;var E=class extends HTMLElement{#s;#o;#t;#i=c.bind(this);#n=document.createElement("iframe");static{this.observedAttributes=["src"]}connectedCallback(){this.#s=this.attachShadow({mode:"closed"}),this.#s.appendChild(this.#n);let t=document.createElement("style");t.textContent=this.styles,this.#s.appendChild(t),this.src=this.getAttribute("src")}get contentWindow(){return this.#n.contentWindow}set src(t){this.#o=t,this.#t=void 0,this.#e(t)}get src(){return this.#o||""}attributeChangedCallback(t,e,s){t==="src"&&(this.src=s)}async#e(t){this.#n.src=t;let e=this.#n.contentWindow;e&&(this.#t=await b.connect(e),this.#t.ondata=s=>{this.#i(s)},this.#t.onresize=s=>{this.#a({width:this.#t.width,height:this.#t.height})},this.#t.onactions=s=>{this.#i(s)},this.#i(new Event("load")),this.load&&typeof this.load=="function"&&this.onload(new Event("load")))}get applet(){return this.#t}set data(t){if(this.applet)this.applet.data=t;else{let e=()=>{this.applet&&(this.applet.data=t),this.removeEventListener("load",e)};this.addEventListener("load",e)}}#a(t){this.style.height=`${t.height}px`}get styles(){return`
|
67
2
|
:host {
|
3
|
+
background: white;
|
68
4
|
display: flex;
|
69
5
|
flex-direction: column;
|
70
6
|
height: 350px;
|
@@ -75,36 +11,4 @@ export class AppletFrameElement extends HTMLElement {
|
|
75
11
|
height: 100%;
|
76
12
|
width: 100%;
|
77
13
|
}
|
78
|
-
|
79
|
-
}
|
80
|
-
}
|
81
|
-
_AppletFrameElement_root = new WeakMap(), _AppletFrameElement_src = new WeakMap(), _AppletFrameElement_applet = new WeakMap(), _AppletFrameElement_dispatchEventAndHandler = new WeakMap(), _AppletFrameElement_iframe = new WeakMap(), _AppletFrameElement_instances = new WeakSet(), _AppletFrameElement_loadApplet = async function _AppletFrameElement_loadApplet(url) {
|
82
|
-
__classPrivateFieldGet(this, _AppletFrameElement_iframe, "f").src = url;
|
83
|
-
const window = __classPrivateFieldGet(this, _AppletFrameElement_iframe, "f").contentWindow;
|
84
|
-
if (!window)
|
85
|
-
return;
|
86
|
-
__classPrivateFieldSet(this, _AppletFrameElement_applet, await applets.connect(window), "f");
|
87
|
-
// When data received, bubble the event up
|
88
|
-
__classPrivateFieldGet(this, _AppletFrameElement_applet, "f").ondata = (event) => {
|
89
|
-
__classPrivateFieldGet(this, _AppletFrameElement_dispatchEventAndHandler, "f").call(this, event);
|
90
|
-
};
|
91
|
-
// Resize
|
92
|
-
__classPrivateFieldGet(this, _AppletFrameElement_applet, "f").onresize = (event) => {
|
93
|
-
__classPrivateFieldGet(this, _AppletFrameElement_instances, "m", _AppletFrameElement_resizeContainer).call(this, {
|
94
|
-
width: __classPrivateFieldGet(this, _AppletFrameElement_applet, "f").width,
|
95
|
-
height: __classPrivateFieldGet(this, _AppletFrameElement_applet, "f").height,
|
96
|
-
});
|
97
|
-
};
|
98
|
-
__classPrivateFieldGet(this, _AppletFrameElement_applet, "f").onactions = (event) => {
|
99
|
-
__classPrivateFieldGet(this, _AppletFrameElement_dispatchEventAndHandler, "f").call(this, event);
|
100
|
-
};
|
101
|
-
// Emit load event when setup & connection complete
|
102
|
-
__classPrivateFieldGet(this, _AppletFrameElement_dispatchEventAndHandler, "f").call(this, new Event('load'));
|
103
|
-
if (this['load'] && typeof this['load'] === 'function') {
|
104
|
-
this.onload(new Event('load'));
|
105
|
-
}
|
106
|
-
}, _AppletFrameElement_resizeContainer = function _AppletFrameElement_resizeContainer(dimensions) {
|
107
|
-
this.style.height = `${dimensions.height}px`;
|
108
|
-
};
|
109
|
-
AppletFrameElement.observedAttributes = ['src'];
|
110
|
-
customElements.define('applet-frame', AppletFrameElement);
|
14
|
+
`}};customElements.define("applet-frame",E);})();
|
@@ -13,6 +13,7 @@ var _AppletScope_instances, _AppletScope_actionHandlers, _AppletScope_manifest,
|
|
13
13
|
import { debug } from '../debug.js';
|
14
14
|
import { AppletEvent } from './events.js';
|
15
15
|
import { dispatchEventAndHandler } from '../utils.js';
|
16
|
+
import { isEmpty } from '../../../utils/common-utils.mjs';
|
16
17
|
export class AppletScope extends EventTarget {
|
17
18
|
constructor(manifest) {
|
18
19
|
super();
|
@@ -149,19 +150,20 @@ _AppletScope_actionHandlers = new WeakMap(), _AppletScope_manifest = new WeakMap
|
|
149
150
|
break;
|
150
151
|
}
|
151
152
|
}, _AppletScope_handleActionMessage = async function _AppletScope_handleActionMessage(message) {
|
152
|
-
|
153
|
+
const { actionId, arguments: args, id } = message;
|
154
|
+
if (Object.keys(__classPrivateFieldGet(this, _AppletScope_actionHandlers, "f")).includes(actionId)) {
|
153
155
|
try {
|
154
|
-
await __classPrivateFieldGet(this, _AppletScope_actionHandlers, "f")[
|
156
|
+
await __classPrivateFieldGet(this, _AppletScope_actionHandlers, "f")[actionId](args);
|
155
157
|
const actionCompleteMessage = {
|
156
158
|
type: 'actioncomplete',
|
157
|
-
id
|
159
|
+
id,
|
158
160
|
};
|
159
161
|
__classPrivateFieldGet(this, _AppletScope_postMessage, "f").call(this, actionCompleteMessage);
|
160
162
|
}
|
161
163
|
catch (e) {
|
162
164
|
const actionErrorMessage = {
|
163
165
|
type: 'actionerror',
|
164
|
-
id
|
166
|
+
id,
|
165
167
|
message: e.message,
|
166
168
|
};
|
167
169
|
__classPrivateFieldGet(this, _AppletScope_postMessage, "f").call(this, actionErrorMessage);
|
@@ -189,15 +191,17 @@ _AppletScope_actionHandlers = new WeakMap(), _AppletScope_manifest = new WeakMap
|
|
189
191
|
__classPrivateFieldGet(this, _AppletScope_postMessage, "f").call(this, resizeMessage);
|
190
192
|
}, _AppletScope_loadManifest = async function _AppletScope_loadManifest() {
|
191
193
|
const manifestLinkElem = document.querySelector('link[rel="manifest"]');
|
192
|
-
if (!manifestLinkElem)
|
194
|
+
if (!manifestLinkElem) {
|
195
|
+
console.warn('No manifest link found');
|
193
196
|
return;
|
197
|
+
}
|
194
198
|
// TODO: Add timeout
|
195
199
|
try {
|
196
200
|
const manifestRequest = await fetch(manifestLinkElem.href);
|
197
201
|
const manifest = (await manifestRequest.json());
|
198
202
|
for (const key in manifest.actions) {
|
199
203
|
const action = manifest.actions[key];
|
200
|
-
if (action.params_schema && !
|
204
|
+
if (action.params_schema && !isEmpty(__classPrivateFieldGet(this, _AppletScope_actions, "f").params_schema)) {
|
201
205
|
action.params_schema = undefined;
|
202
206
|
}
|
203
207
|
}
|
@@ -0,0 +1,111 @@
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
6
|
+
};
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
11
|
+
};
|
12
|
+
var _AppletFrameElement_instances, _AppletFrameElement_root, _AppletFrameElement_src, _AppletFrameElement_applet, _AppletFrameElement_dispatchEventAndHandler, _AppletFrameElement_iframe, _AppletFrameElement_loadApplet, _AppletFrameElement_resizeContainer;
|
13
|
+
import { applets } from '../index.js';
|
14
|
+
import { dispatchEventAndHandler } from '../utils.js';
|
15
|
+
export class AppletFrameElement extends HTMLElement {
|
16
|
+
constructor() {
|
17
|
+
super(...arguments);
|
18
|
+
_AppletFrameElement_instances.add(this);
|
19
|
+
_AppletFrameElement_root.set(this, void 0);
|
20
|
+
_AppletFrameElement_src.set(this, void 0);
|
21
|
+
_AppletFrameElement_applet.set(this, void 0);
|
22
|
+
_AppletFrameElement_dispatchEventAndHandler.set(this, dispatchEventAndHandler.bind(this));
|
23
|
+
_AppletFrameElement_iframe.set(this, document.createElement('iframe'));
|
24
|
+
}
|
25
|
+
connectedCallback() {
|
26
|
+
__classPrivateFieldSet(this, _AppletFrameElement_root, this.attachShadow({ mode: 'closed' }), "f");
|
27
|
+
__classPrivateFieldGet(this, _AppletFrameElement_root, "f").appendChild(__classPrivateFieldGet(this, _AppletFrameElement_iframe, "f"));
|
28
|
+
const styles = document.createElement('style');
|
29
|
+
styles.textContent = this.styles;
|
30
|
+
__classPrivateFieldGet(this, _AppletFrameElement_root, "f").appendChild(styles);
|
31
|
+
this.src = this.getAttribute('src');
|
32
|
+
}
|
33
|
+
get contentWindow() {
|
34
|
+
return __classPrivateFieldGet(this, _AppletFrameElement_iframe, "f").contentWindow;
|
35
|
+
}
|
36
|
+
set src(value) {
|
37
|
+
__classPrivateFieldSet(this, _AppletFrameElement_src, value, "f");
|
38
|
+
__classPrivateFieldSet(this, _AppletFrameElement_applet, undefined, "f");
|
39
|
+
__classPrivateFieldGet(this, _AppletFrameElement_instances, "m", _AppletFrameElement_loadApplet).call(this, value);
|
40
|
+
}
|
41
|
+
get src() {
|
42
|
+
return __classPrivateFieldGet(this, _AppletFrameElement_src, "f") || '';
|
43
|
+
}
|
44
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
45
|
+
if (name === 'src') {
|
46
|
+
this.src = newValue;
|
47
|
+
}
|
48
|
+
}
|
49
|
+
get applet() {
|
50
|
+
return __classPrivateFieldGet(this, _AppletFrameElement_applet, "f");
|
51
|
+
}
|
52
|
+
set data(data) {
|
53
|
+
if (this.applet) {
|
54
|
+
this.applet.data = data;
|
55
|
+
}
|
56
|
+
else {
|
57
|
+
const listener = () => {
|
58
|
+
if (this.applet)
|
59
|
+
this.applet.data = data;
|
60
|
+
this.removeEventListener('load', listener);
|
61
|
+
};
|
62
|
+
this.addEventListener('load', listener);
|
63
|
+
}
|
64
|
+
}
|
65
|
+
get styles() {
|
66
|
+
return /*css*/ `
|
67
|
+
:host {
|
68
|
+
background: white;
|
69
|
+
display: flex;
|
70
|
+
flex-direction: column;
|
71
|
+
height: 350px;
|
72
|
+
}
|
73
|
+
|
74
|
+
iframe {
|
75
|
+
border: none;
|
76
|
+
height: 100%;
|
77
|
+
width: 100%;
|
78
|
+
}
|
79
|
+
`;
|
80
|
+
}
|
81
|
+
}
|
82
|
+
_AppletFrameElement_root = new WeakMap(), _AppletFrameElement_src = new WeakMap(), _AppletFrameElement_applet = new WeakMap(), _AppletFrameElement_dispatchEventAndHandler = new WeakMap(), _AppletFrameElement_iframe = new WeakMap(), _AppletFrameElement_instances = new WeakSet(), _AppletFrameElement_loadApplet = async function _AppletFrameElement_loadApplet(url) {
|
83
|
+
__classPrivateFieldGet(this, _AppletFrameElement_iframe, "f").src = url;
|
84
|
+
const window = __classPrivateFieldGet(this, _AppletFrameElement_iframe, "f").contentWindow;
|
85
|
+
if (!window)
|
86
|
+
return;
|
87
|
+
__classPrivateFieldSet(this, _AppletFrameElement_applet, await applets.connect(window), "f");
|
88
|
+
// When data received, bubble the event up
|
89
|
+
__classPrivateFieldGet(this, _AppletFrameElement_applet, "f").ondata = (event) => {
|
90
|
+
__classPrivateFieldGet(this, _AppletFrameElement_dispatchEventAndHandler, "f").call(this, event);
|
91
|
+
};
|
92
|
+
// Resize
|
93
|
+
__classPrivateFieldGet(this, _AppletFrameElement_applet, "f").onresize = (event) => {
|
94
|
+
__classPrivateFieldGet(this, _AppletFrameElement_instances, "m", _AppletFrameElement_resizeContainer).call(this, {
|
95
|
+
width: __classPrivateFieldGet(this, _AppletFrameElement_applet, "f").width,
|
96
|
+
height: __classPrivateFieldGet(this, _AppletFrameElement_applet, "f").height,
|
97
|
+
});
|
98
|
+
};
|
99
|
+
__classPrivateFieldGet(this, _AppletFrameElement_applet, "f").onactions = (event) => {
|
100
|
+
__classPrivateFieldGet(this, _AppletFrameElement_dispatchEventAndHandler, "f").call(this, event);
|
101
|
+
};
|
102
|
+
// Emit load event when setup & connection complete
|
103
|
+
__classPrivateFieldGet(this, _AppletFrameElement_dispatchEventAndHandler, "f").call(this, new Event('load'));
|
104
|
+
if (this['load'] && typeof this['load'] === 'function') {
|
105
|
+
this.onload(new Event('load'));
|
106
|
+
}
|
107
|
+
}, _AppletFrameElement_resizeContainer = function _AppletFrameElement_resizeContainer(dimensions) {
|
108
|
+
this.style.height = `${dimensions.height}px`;
|
109
|
+
};
|
110
|
+
AppletFrameElement.observedAttributes = ['src'];
|
111
|
+
customElements.define('applet-frame', AppletFrameElement);
|
@@ -0,0 +1 @@
|
|
1
|
+
export declare function isEmpty(obj: object): boolean;
|
package/dist/web-applets.min.js
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
(()=>{var h=class extends Error{constructor(
|
1
|
+
(()=>{var h=class extends Error{constructor(t){super(t),this.name="AppletExecutionError"}},A=class extends Error{constructor(t){super(t),this.name="AppletConnectionError"}};function c(o){typeof this[`on${o.type}`]=="function"&&this[`on${o.type}`](o),this.dispatchEvent(o)}var a=class extends Event{constructor(t,e){super(t,{bubbles:e?.bubbles,composed:e?.composed,cancelable:e?.cancelable}),this.data=e?.data,this.actions=e?.actions}};function T(o,...t){}var p={log:T};var f=class extends EventTarget{#s;#o={};#t;#i;#n;#e;#a;#p;#r;constructor(t){super(),p.log("Applet","Constructor called"),this.#s=t,this.#n=c.bind(this),this.#c();let e=s=>{s.source===this.#s&&"type"in s.data&&s.data.type==="appletconnect"&&(p.log("Applet","Recieved message",s.data),this.#c(),this.removeEventListener("message",e))};window.addEventListener("message",e)}#c(){this.#e&&this.#e.close();let t=new MessageChannel,e={type:"appletconnect"};p.log("Applet","Send message",e),this.#e=t.port1,this.#e.onmessage=this.#h.bind(this),this.#s.postMessage(e,"*",[t.port2]),this.#a=this.#e.postMessage.bind(this.#e)}#h(t){let e=t.data;switch(p.log("Applet","Recieved message",e),e.type){case"register":let s=e;this.#t=s.manifest;let n=new a("connect");this.#n(n),this.#o=s.actions,this.#d(s.actions),this.#i=s.data,this.#l(s.data);break;case"data":let i=e;this.#i=i.data,this.#l(i.data);break;case"resize":let r=e;this.#p=r.width,this.#r=r.height;let g=new a("resize");this.#n(g);break;case"actions":let m=e;this.#o=m.actions,this.#d(m.actions);break}}#l(t){let e=new a("data",{data:t});this.#n(e)}#d(t){let e=new a("actions",{actions:t});this.#n(e)}async sendAction(t,e){let s={id:crypto.randomUUID(),type:"action",actionId:t,arguments:e};return new Promise((n,i)=>{this.#a(s);let r=setTimeout(()=>{i(new h(`Applet action handler failed to complete before timeout (${1e4}ms)`))},1e4),g=m=>{let d=m.data;if(["actioncomplete","actionerror"].includes(d.type)&&"id"in d&&d.id===s.id)if(this.#e.removeEventListener("message",g),clearTimeout(r),d.type==="actionerror"){let b=d;i(new h(b.message))}else n()};this.#e.addEventListener("message",g)})}get data(){return this.#i}set data(t){this.#i=t;let e={type:"data",data:t};this.#a(e)}get window(){return this.#s}get manifest(){return this.#t}get actions(){return this.#o}get width(){return this.#p}get height(){return this.#r}};function M(o){return Object.keys(o).length===0}var E=class extends EventTarget{#s={};#o;#t;#i;#n;#e;#a;#p;constructor(t){super(),p.log("AppletScope","Constructor called"),this.#n=c.bind(this),t&&(this.#o=t);let e=n=>{if(n.source===window.parent&&n.data.type==="appletconnect"&&n.ports&&n.ports.length>0){p.log("AppletScope","Recieved message",n.data);let i=n.ports[0];this.#e=i.postMessage.bind(i),i.onmessage=this.#c.bind(this),this.removeEventListener("message",e),this.#r()}};window.addEventListener("message",e);let s={type:"appletconnect"};window.parent.postMessage(s,"*"),p.log("AppletScope","Send message",s)}async#r(){let t=this.manifest??await this.#g();this.#o=t||{},this.#t=this.#t||t?.actions||{};let e={type:"register",manifest:this.#o,actions:this.#t,data:this.#i};this.#e(e),p.log("AppletScope","Send message",e);let s=new a("connect");this.#n(s),this.#l()}#c(t){let e=t.data;switch(p.log("AppletScope","Recieved message",e),e.type){case"data":"data"in e&&(this.data=e.data);break;case"action":"type"in e&&e.type==="action"&&"id"in e&&typeof e.id=="string"&&"actionId"in e&&typeof e.actionId=="string"&&"arguments"in e&&this.#h(e);break}}async#h(t){let{actionId:e,arguments:s,id:n}=t;if(Object.keys(this.#s).includes(e))try{await this.#s[e](s);let i={type:"actioncomplete",id:n};this.#e(i)}catch(i){let r={type:"actionerror",id:n,message:i.message};this.#e(r),console.error(i)}}#l(){new ResizeObserver(e=>{for(let s of e)this.#d({width:s.contentRect.width,height:s.contentRect.height})}).observe(document.querySelector("html"))}#d({width:t,height:e}){this.#a=t,this.#p=e;let s={type:"resize",width:t,height:e};p.log("AppletScope","Send message",s),this.#e(s)}async#g(){let t=document.querySelector('link[rel="manifest"]');if(!t){console.warn("No manifest link found");return}try{let s=await(await fetch(t.href)).json();for(let n in s.actions){let i=s.actions[n];i.params_schema&&!M(this.#t.params_schema)&&(i.params_schema=void 0)}return s}catch{return}}setActionHandler(t,e){this.#s[t]=e}defineAction(t,e){let{handler:s,...n}=e;s&&(this.#s[t]=s),this.actions={...this.actions,[t]:n}}set actions(t){if(!t)return;this.#t=t;let e={type:"actions",actions:this.#t};p.log("AppletScope","Send message",e),this.#e&&this.#e(e);let s=new a("actions",{actions:t});setTimeout(()=>this.#n(s),1)}get actions(){return this.#t}get manifest(){return this.#o}get actionHandlers(){return this.#s}set actionHandlers(t){this.#s=t}set data(t){this.#i=t;let e={type:"data",data:t};p.log("AppletScope","Send message",e),this.#e&&this.#e(e);let s=new a("data",{data:t});setTimeout(()=>this.#n(s),1)}get data(){return this.#i}get width(){return this.#a}get height(){return this.#p}};var u=class{async connect(t){return new Promise((e,s)=>{let n=new f(t),i=setTimeout(()=>{s(new A(`Applet failed to connect before the timeout was reached (${1e4}ms)`))},1e4),r=()=>{e(n),n.removeEventListener("connect",r),clearTimeout(i)};n.addEventListener("connect",r)})}register(t){return new E(t)}};var l=class extends HTMLElement{#s;#o;#t;#i=c.bind(this);#n=document.createElement("iframe");static{this.observedAttributes=["src"]}connectedCallback(){this.#s=this.attachShadow({mode:"closed"}),this.#s.appendChild(this.#n);let t=document.createElement("style");t.textContent=this.styles,this.#s.appendChild(t),this.src=this.getAttribute("src")}get contentWindow(){return this.#n.contentWindow}set src(t){this.#o=t,this.#t=void 0,this.#e(t)}get src(){return this.#o||""}attributeChangedCallback(t,e,s){t==="src"&&(this.src=s)}async#e(t){this.#n.src=t;let e=this.#n.contentWindow;e&&(this.#t=await y.connect(e),this.#t.ondata=s=>{this.#i(s)},this.#t.onresize=s=>{this.#a({width:this.#t.width,height:this.#t.height})},this.#t.onactions=s=>{this.#i(s)},this.#i(new Event("load")),this.load&&typeof this.load=="function"&&this.onload(new Event("load")))}get applet(){return this.#t}set data(t){if(this.applet)this.applet.data=t;else{let e=()=>{this.applet&&(this.applet.data=t),this.removeEventListener("load",e)};this.addEventListener("load",e)}}#a(t){this.style.height=`${t.height}px`}get styles(){return`
|
2
2
|
:host {
|
3
|
+
background: white;
|
3
4
|
display: flex;
|
4
5
|
flex-direction: column;
|
5
6
|
height: 350px;
|
@@ -10,4 +11,4 @@
|
|
10
11
|
height: 100%;
|
11
12
|
width: 100%;
|
12
13
|
}
|
13
|
-
`}};customElements.define("applet-frame",l);var y=new u;window.applets=y;window.AppletEvent=
|
14
|
+
`}};customElements.define("applet-frame",l);var y=new u;window.applets=y;window.AppletEvent=a;window.AppletFrameElement=l;})();
|
package/package.json
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
{
|
2
2
|
"name": "@web-applets/sdk",
|
3
|
-
"version": "0.2.0-alpha.9",
|
4
3
|
"description": "The Web Applets SDK, for creating & hosting Web Applets.",
|
5
4
|
"author": "Rupert Manfredi <rupert@unternet.co>",
|
6
5
|
"license": "MIT",
|
@@ -18,14 +17,18 @@
|
|
18
17
|
"url": "git+https://github.com/unternet-co/web-applets.git"
|
19
18
|
},
|
20
19
|
"scripts": {
|
21
|
-
"build": "npm run build:module && npm run build:shim",
|
20
|
+
"build": "cross-env NODE_ENV=production npm run build:module && npm run build:shim:prod",
|
21
|
+
"build:dev": "cross-env NODE_ENV=development npm run build:module && npm run build:shim:dev",
|
22
22
|
"build:module": "tsc && cp ../README.md ./README.md",
|
23
|
-
"build:shim": "node ./scripts/build.js",
|
23
|
+
"build:shim:prod": "cross-env NODE_ENV=production node ./scripts/build.js",
|
24
|
+
"build:shim:dev": "cross-env NODE_ENV=development node ./scripts/build.js",
|
24
25
|
"prepublishOnly": "npm run build"
|
25
26
|
},
|
26
27
|
"devDependencies": {
|
27
|
-
"@types/node": "^22.13.
|
28
|
+
"@types/node": "^22.13.10",
|
29
|
+
"cross-env": "^7.0.3",
|
28
30
|
"esbuild": "^0.25.0",
|
29
31
|
"typescript": "^5.6.2"
|
30
|
-
}
|
32
|
+
},
|
33
|
+
"version": "0.2.1"
|
31
34
|
}
|
package/dist/debug.js
DELETED
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|