@adobe/uix-host-react 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2022 Adobe
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ This is the `@adobe/uix-host-react` package alone. See [/README.md](../../README.md) for details.
@@ -0,0 +1,41 @@
1
+ import type { PropsWithChildren } from "react";
2
+ import type { ExtensionsProvider, HostConfig, PortOptions, SharedContextValues } from "@adobe/uix-host";
3
+ /** @public */
4
+ export interface ExtensibleProps extends Omit<HostConfig, "hostName"> {
5
+ /**
6
+ * Unique name for identifying this extensible app. May be used as metadata in
7
+ * extension registry in the future.
8
+ */
9
+ appName?: string;
10
+ /**
11
+ * Function which returns a promise for the full list of extensions.
12
+ */
13
+ extensionsProvider: ExtensionsProvider;
14
+ /**
15
+ * {@inheritDoc HostConfig.guestOptions}
16
+ */
17
+ guestOptions?: PortOptions;
18
+ /**
19
+ * {@inheritDoc HostConfig.sharedContext}
20
+ */
21
+ sharedContext?: SharedContextValues;
22
+ }
23
+ /**
24
+ * Declares an extensible area in an app, and provides host and extension
25
+ * objects to all descendents. The {@link useExtensions} hook can only be called
26
+ * in a descendent of this component.
27
+ *
28
+ * @remarks
29
+ * For many apps, there will be only one Extensible provider, fairly high in the
30
+ * component tree. It is a context provider component that may be bundled with
31
+ * other context providers in your app architecture.
32
+ *
33
+ * Each Extensible element creates one {@link @adobe/uix-host#Host} object and
34
+ * one call to an extensions provider. If multiple Extensible elements are used,
35
+ * this may cause redundant calls. Such a design should be carefully considered.
36
+ *
37
+ * @public
38
+ */
39
+ export declare function Extensible({ appName, children, extensionsProvider, guestOptions, runtimeContainer, debug, sharedContext, }: PropsWithChildren<ExtensibleProps>): JSX.Element;
40
+ export default Extensible;
41
+ //# sourceMappingURL=Extensible.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Extensible.d.ts","sourceRoot":"","sources":["../../src/components/Extensible.tsx"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAC;AAC/C,OAAO,KAAK,EAEV,kBAAkB,EAClB,UAAU,EACV,WAAW,EACX,mBAAmB,EACpB,MAAM,iBAAiB,CAAC;AAIzB,cAAc;AACd,MAAM,WAAW,eAAgB,SAAQ,IAAI,CAAC,UAAU,EAAE,UAAU,CAAC;IACnE;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB;;OAEG;IACH,kBAAkB,EAAE,kBAAkB,CAAC;IACvC;;OAEG;IACH,YAAY,CAAC,EAAE,WAAW,CAAC;IAC3B;;OAEG;IACH,aAAa,CAAC,EAAE,mBAAmB,CAAC;CACrC;AAaD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,UAAU,CAAC,EACzB,OAAO,EACP,QAAQ,EACR,kBAAkB,EAClB,YAAY,EACZ,gBAAgB,EAChB,KAAK,EACL,aAAkB,GACnB,EAAE,iBAAiB,CAAC,eAAe,CAAC,eAmDpC;AACD,eAAe,UAAU,CAAC"}
@@ -0,0 +1,43 @@
1
+ import type { VirtualApi } from "@adobe/uix-core";
2
+ import type { PropsWithChildren, IframeHTMLAttributes } from "react";
3
+ /**
4
+ * @public
5
+ */
6
+ declare type FrameProps = IframeHTMLAttributes<HTMLIFrameElement>;
7
+ /** @public */
8
+ export interface GuestUIProps extends FrameProps {
9
+ guestId: string;
10
+ /**
11
+ * Receives the Penpal context when the frame is mounted.
12
+ */
13
+ onConnect: () => unknown;
14
+ /**
15
+ * Called when the frame disconnects and unmounts.
16
+ */
17
+ onDisconnect: () => unknown;
18
+ /**
19
+ * Called when the connection process throws an exception
20
+ */
21
+ onConnectionError?: (error: Error) => void;
22
+ /**
23
+ * Optional custom URL or path.
24
+ */
25
+ src: string;
26
+ /**
27
+ * Host methods to provide only to the guest inside this iframe.
28
+ */
29
+ methods?: VirtualApi;
30
+ /**
31
+ *
32
+ * @defaultValue "100%"
33
+ */
34
+ width: FrameProps["width"];
35
+ }
36
+ /**
37
+ * An iframe that attaches to a running GuestServer, to display visible UI pages
38
+ * delivered by the Extension server.
39
+ * @public
40
+ */
41
+ export declare function GuestUIFrame({ guestId, src, onConnect, onDisconnect, onConnectionError, methods, ...customFrameProps }: PropsWithChildren<GuestUIProps>): JSX.Element;
42
+ export default GuestUIFrame;
43
+ //# sourceMappingURL=GuestUIFrame.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GuestUIFrame.d.ts","sourceRoot":"","sources":["../../src/components/GuestUIFrame.tsx"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAElD,OAAO,KAAK,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAGrE;;GAEG;AACH,aAAK,UAAU,GAAG,oBAAoB,CAAC,iBAAiB,CAAC,CAAC;AAE1D,cAAc;AACd,MAAM,WAAW,YAAa,SAAQ,UAAU;IAC9C,OAAO,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,SAAS,EAAE,MAAM,OAAO,CAAC;IACzB;;OAEG;IACH,YAAY,EAAE,MAAM,OAAO,CAAC;IAC5B;;OAEG;IACH,iBAAiB,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,CAAC;IAC3C;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,OAAO,CAAC,EAAE,UAAU,CAAC;IAErB;;;OAGG;IACH,KAAK,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC;CAC5B;AAWD;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,EAC3B,OAAO,EACP,GAAQ,EACR,SAAS,EACT,YAAY,EACZ,iBAAiB,EACjB,OAAO,EACP,GAAG,gBAAgB,EACpB,EAAE,iBAAiB,CAAC,YAAY,CAAC,eA4CjC;AACD,eAAe,YAAY,CAAC"}
@@ -0,0 +1,3 @@
1
+ export * from "./Extensible.js";
2
+ export * from "./GuestUIFrame.js";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/components/index.ts"],"names":[],"mappings":"AAYA,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC"}
@@ -0,0 +1,194 @@
1
+ import { Host } from '@adobe/uix-host';
2
+ export * from '@adobe/uix-host';
3
+ import React, { createContext, useState, useEffect, useMemo, useCallback, useContext } from 'react';
4
+
5
+ // src/index.ts
6
+ var ExtensionContext = createContext({});
7
+
8
+ // src/components/Extensible.tsx
9
+ function areExtensionsDifferent(set1, set2) {
10
+ const ids1 = Object.keys(set1).sort();
11
+ const ids2 = Object.keys(set2).sort();
12
+ return ids1.length !== ids2.length || ids1.some((id, index) => id !== ids2[index]);
13
+ }
14
+ function Extensible({
15
+ appName,
16
+ children,
17
+ extensionsProvider,
18
+ guestOptions,
19
+ runtimeContainer,
20
+ debug,
21
+ sharedContext = {}
22
+ }) {
23
+ const [extensions, setExtensions] = useState({});
24
+ useEffect(() => {
25
+ extensionsProvider().then((loadedExtensions) => {
26
+ if (areExtensionsDifferent(extensions, loadedExtensions)) {
27
+ setExtensions(loadedExtensions);
28
+ }
29
+ }).catch((e) => {
30
+ console.error("Fetching list of extensions failed!", e);
31
+ });
32
+ }, [extensionsProvider]);
33
+ const hostName = appName || window.location.host || "mainframe";
34
+ const host = useMemo(() => {
35
+ const host2 = new Host({
36
+ debug,
37
+ hostName,
38
+ runtimeContainer,
39
+ sharedContext
40
+ });
41
+ return host2;
42
+ }, [debug, hostName, runtimeContainer, sharedContext]);
43
+ useEffect(() => {
44
+ function logError(msg) {
45
+ return (e) => {
46
+ const error = e instanceof Error ? e : new Error(String(e));
47
+ console.error(msg, error, extensions, guestOptions);
48
+ };
49
+ }
50
+ if (!Object.entries(extensions).length) {
51
+ return;
52
+ }
53
+ host.load(extensions, guestOptions).catch(logError("Load of extensions failed!"));
54
+ return () => {
55
+ host.unload().catch(logError("Unload of extensions failed!"));
56
+ };
57
+ }, [host, extensions]);
58
+ return /* @__PURE__ */ React.createElement(ExtensionContext.Provider, {
59
+ value: host
60
+ }, children);
61
+ }
62
+ var OutsideOfExtensionContextError = class extends Error {
63
+ constructor(msg) {
64
+ super(msg);
65
+ this.outsideOfExtensionContext = true;
66
+ Object.setPrototypeOf(this, OutsideOfExtensionContextError.prototype);
67
+ }
68
+ };
69
+ function useHost() {
70
+ const host = useContext(ExtensionContext);
71
+ if (!(host instanceof Host)) {
72
+ const error = new OutsideOfExtensionContextError(
73
+ "Attempt to use extensions outside of ExtensionContext. Wrap extensible part of application with Extensible component."
74
+ );
75
+ return {
76
+ host: void 0,
77
+ error
78
+ };
79
+ }
80
+ return { error: void 0, host };
81
+ }
82
+
83
+ // src/components/GuestUIFrame.tsx
84
+ var defaultFrameProps = {
85
+ width: "100%",
86
+ height: "100%",
87
+ sandbox: "allow-scripts",
88
+ style: {
89
+ border: "none"
90
+ }
91
+ };
92
+ function GuestUIFrame({
93
+ guestId,
94
+ src = "",
95
+ onConnect,
96
+ onDisconnect,
97
+ onConnectionError,
98
+ methods,
99
+ ...customFrameProps
100
+ }) {
101
+ const { host } = useHost();
102
+ if (!host) {
103
+ return null;
104
+ }
105
+ const guest = host.guests.get(guestId);
106
+ const frameUrl = new URL(src, guest.url.href);
107
+ const ref = useCallback((iframe) => {
108
+ if (iframe) {
109
+ if (methods) {
110
+ guest.provide(methods);
111
+ }
112
+ const connection = guest.attachUI(iframe);
113
+ connection.promise.then(() => {
114
+ if (onConnect) {
115
+ onConnect();
116
+ }
117
+ }).catch((error) => {
118
+ if (onConnectionError)
119
+ onConnectionError(error);
120
+ else
121
+ throw error;
122
+ });
123
+ return async () => {
124
+ if (onDisconnect) {
125
+ await onDisconnect();
126
+ return guest.unload();
127
+ }
128
+ };
129
+ }
130
+ }, []);
131
+ const frameProps = { ...defaultFrameProps, ...customFrameProps };
132
+ return /* @__PURE__ */ React.createElement("iframe", {
133
+ ref,
134
+ src: frameUrl.href,
135
+ name: `uix-guest-${guest.id}`,
136
+ ...frameProps,
137
+ sandbox: "allow-scripts allow-downloads allow-same-origin allow-presentation"
138
+ });
139
+ }
140
+ var NO_EXTENSIONS = [];
141
+ function useExtensions(configFactory, deps = []) {
142
+ const { host, error } = useHost();
143
+ if (error) {
144
+ return {
145
+ extensions: NO_EXTENSIONS,
146
+ loading: false,
147
+ error
148
+ };
149
+ }
150
+ const baseDeps = [host, ...deps];
151
+ const {
152
+ requires,
153
+ provides,
154
+ updateOn = "each"
155
+ } = useMemo(() => configFactory(host), baseDeps);
156
+ const getExtensions = useCallback(() => {
157
+ const newExtensions = [];
158
+ const guests = host.getLoadedGuests(requires);
159
+ for (const guest of guests) {
160
+ if (provides) {
161
+ guest.provide(provides);
162
+ }
163
+ newExtensions.push(guest);
164
+ }
165
+ return newExtensions.length === 0 ? NO_EXTENSIONS : newExtensions;
166
+ }, [...baseDeps, requires]);
167
+ const subscribe = useCallback(
168
+ (handler) => {
169
+ const eventName = updateOn === "all" ? "loadallguests" : "guestload";
170
+ host.addEventListener(eventName, handler);
171
+ return () => host.removeEventListener(eventName, handler);
172
+ },
173
+ [...baseDeps, updateOn]
174
+ );
175
+ const [extensions, setExtensions] = useState(() => getExtensions());
176
+ useEffect(() => {
177
+ return subscribe(() => {
178
+ setExtensions(getExtensions());
179
+ });
180
+ }, [subscribe, getExtensions]);
181
+ const [hostError, setHostError] = useState();
182
+ useEffect(
183
+ () => host.addEventListener(
184
+ "error",
185
+ (event) => setHostError(event.detail.error)
186
+ ),
187
+ baseDeps
188
+ );
189
+ return { extensions, loading: !host.loading, error: hostError };
190
+ }
191
+
192
+ export { Extensible, GuestUIFrame, useExtensions };
193
+ //# sourceMappingURL=out.js.map
194
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/index.ts","../../src/components/Extensible.tsx","../../src/extension-context.ts","../../src/components/GuestUIFrame.tsx","../../src/hooks/useHost.ts","../../src/hoo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KAAK,gBAAgB,QAAQ;AAC5C,eAAW,SAAS,QAAQ;AAC1B,UAAI,UAAU;AACZ,cAAM,QAAQ,QAAQ;AAAA,MACxB;AACA,oBAAc,KAAK,KAAkD;AAAA,IACvE;AACA,WAAO,cAAc,WAAW,IAAI,gBAAgB;AAAA,EACtD,GAAG,CAAC,GAAG,UAAU,QAAQ,CAAC;AAE1B,QAAM,YAAYA;AAAA,IAChB,CAAC,YAA2B;AAC1B,YAAM,YAAY,aAAa,QAAQ,kBAAkB;AACzD,WAAK,iBAAiB,WAAW,OAAO;AACxC,aAAO,MAAM,KAAK,oBAAoB,WAAW,OAAO;AAAA,IAC1D;AAAA,IACA,CAAC,GAAG,UAAU,QAAQ;AAAA,EACxB;AAEA,QAAM,CAAC,YAAY,aAAa,IAAIG,UAAS,MAAM,cAAc,CAAC;AAClE,EAAAF,WAAU,MAAM;AACd,WAAO,UAAU,MAAM;AACrB,oBAAc,cAAc,CAAC;AAAA,IAC/B,CAAC;AAAA,EACH,GAAG,CAAC,WAAW,aAAa,CAAC;AAE7B,QAAM,CAAC,WAAW,YAAY,IAAIE,UAAgB;AAClD,EAAAF;AAAA,IACE,MACE,KAAK;AAAA,MACH;AAAA,MACA,CAAC,UACC,aAAa,MAAM,OAAO,KAAK;AAAA,IACnC;AAAA,IACF;AAAA,EACF;AAEA,SAAO,EAAE,YAAY,SAAS,CAAC,KAAK,SAAS,OAAO,UAAU;AAChE","sourcesContent":["/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nexport * from \"@adobe/uix-host\";\nexport * from \"./components/index.js\";\nexport * from \"./hooks/index.js\";\n","/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport React, { useState, useEffect, useMemo } from \"react\";\nimport type { PropsWithChildren } from \"react\";\nimport type {\n InstalledExtensions,\n ExtensionsProvider,\n HostConfig,\n PortOptions,\n SharedContextValues,\n} from \"@adobe/uix-host\";\nimport { Host } from \"@adobe/uix-host\";\nimport { ExtensionContext } from \"../extension-context.js\";\n\n/** @public */\nexport interface ExtensibleProps extends Omit<HostConfig, \"hostName\"> {\n /**\n * Unique name for identifying this extensible app. May be used as metadata in\n * extension registry in the future.\n */\n appName?: string;\n /**\n * Function which returns a promise for the full list of extensions.\n */\n extensionsProvider: ExtensionsProvider;\n /**\n * {@inheritDoc HostConfig.guestOptions}\n */\n guestOptions?: PortOptions;\n /**\n * {@inheritDoc HostConfig.sharedContext}\n */\n sharedContext?: SharedContextValues;\n}\n\nfunction areExtensionsDifferent(\n set1: InstalledExtensions,\n set2: InstalledExtensions\n) {\n const ids1 = Object.keys(set1).sort();\n const ids2 = Object.keys(set2).sort();\n return (\n ids1.length !== ids2.length || ids1.some((id, index) => id !== ids2[index])\n );\n}\n\n/**\n * Declares an extensible area in an app, and provides host and extension\n * objects to all descendents. The {@link useExtensions} hook can only be called\n * in a descendent of this component.\n *\n * @remarks\n * For many apps, there will be only one Extensible provider, fairly high in the\n * component tree. It is a context provider component that may be bundled with\n * other context providers in your app architecture.\n *\n * Each Extensible element creates one {@link @adobe/uix-host#Host} object and\n * one call to an extensions provider. If multiple Extensible elements are used,\n * this may cause redundant calls. Such a design should be carefully considered.\n *\n * @public\n */\nexport function Extensible({\n appName,\n children,\n extensionsProvider,\n guestOptions,\n runtimeContainer,\n debug,\n sharedContext = {},\n}: PropsWithChildren<ExtensibleProps>) {\n const [extensions, setExtensions] = useState({});\n\n useEffect(() => {\n extensionsProvider()\n .then((loadedExtensions: InstalledExtensions) => {\n if (areExtensionsDifferent(extensions, loadedExtensions)) {\n setExtensions(loadedExtensions);\n }\n })\n .catch((e: Error | unknown) => {\n console.error(\"Fetching list of extensions failed!\", e);\n });\n }, [extensionsProvider]);\n\n const hostName = appName || window.location.host || \"mainframe\";\n const host = useMemo(() => {\n const host = new Host({\n debug,\n hostName,\n runtimeContainer,\n sharedContext,\n });\n return host;\n }, [debug, hostName, runtimeContainer, sharedContext]);\n\n useEffect(() => {\n function logError(msg: string) {\n return (e: Error | unknown) => {\n const error = e instanceof Error ? e : new Error(String(e));\n console.error(msg, error, extensions, guestOptions);\n };\n }\n\n if (!Object.entries(extensions).length) {\n return;\n }\n\n host\n .load(extensions, guestOptions)\n .catch(logError(\"Load of extensions failed!\"));\n return () => {\n host.unload().catch(logError(\"Unload of extensions failed!\"));\n };\n }, [host, extensions]);\n\n return (\n <ExtensionContext.Provider value={host}>\n {children}\n </ExtensionContext.Provider>\n );\n}\nexport default Extensible;\n","/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\n/**\n * @hidden\n */\nimport { createContext } from \"react\";\nimport { Host } from \"@adobe/uix-host\";\n\nexport const ExtensionContext = createContext<Host>({} as unknown as Host);\n","/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport type { VirtualApi } from \"@adobe/uix-core\";\nimport React, { useCallback } from \"react\";\nimport type { PropsWithChildren, IframeHTMLAttributes } from \"react\";\nimport { useHost } from \"../hooks/useHost.js\";\n\n/**\n * @public\n */\ntype FrameProps = IframeHTMLAttributes<HTMLIFrameElement>;\n\n/** @public */\nexport interface GuestUIProps extends FrameProps {\n guestId: string;\n /**\n * Receives the Penpal context when the frame is mounted.\n */\n onConnect: () => unknown;\n /**\n * Called when the frame disconnects and unmounts.\n */\n onDisconnect: () => unknown;\n /**\n * Called when the connection process throws an exception\n */\n onConnectionError?: (error: Error) => void;\n /**\n * Optional custom URL or path.\n */\n src: string;\n /**\n * Host methods to provide only to the guest inside this iframe.\n */\n methods?: VirtualApi;\n\n /**\n *\n * @defaultValue \"100%\"\n */\n width: FrameProps[\"width\"];\n}\n\nconst defaultFrameProps: FrameProps = {\n width: \"100%\",\n height: \"100%\",\n sandbox: \"allow-scripts\",\n style: {\n border: \"none\",\n },\n};\n\n/**\n * An iframe that attaches to a running GuestServer, to display visible UI pages\n * delivered by the Extension server.\n * @public\n */\nexport function GuestUIFrame({\n guestId,\n src = \"\",\n onConnect,\n onDisconnect,\n onConnectionError,\n methods,\n ...customFrameProps\n}: PropsWithChildren<GuestUIProps>) {\n const { host } = useHost();\n if (!host) {\n return null;\n }\n const guest = host.guests.get(guestId);\n const frameUrl = new URL(src, guest.url.href);\n\n const ref = useCallback((iframe: HTMLIFrameElement) => {\n if (iframe) {\n if (methods) {\n guest.provide(methods);\n }\n const connection = guest.attachUI(iframe);\n connection.promise\n .then(() => {\n if (onConnect) {\n onConnect();\n }\n })\n .catch((error: Error) => {\n if (onConnectionError) onConnectionError(error);\n else throw error;\n });\n return async () => {\n if (onDisconnect) {\n await onDisconnect();\n return guest.unload();\n }\n };\n }\n }, []);\n\n const frameProps = { ...defaultFrameProps, ...customFrameProps };\n\n return (\n <iframe\n ref={ref}\n src={frameUrl.href}\n name={`uix-guest-${guest.id}`}\n {...frameProps}\n sandbox=\"allow-scripts allow-downloads allow-same-origin allow-presentation\"\n />\n );\n}\nexport default GuestUIFrame;\n","/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport { useContext } from \"react\";\nimport { Host } from \"@adobe/uix-host\";\nimport { ExtensionContext } from \"../extension-context.js\";\n\n/**\n * @public\n */\nexport class OutsideOfExtensionContextError extends Error {\n outsideOfExtensionContext: boolean;\n constructor(msg: string) {\n super(msg);\n this.outsideOfExtensionContext = true;\n Object.setPrototypeOf(this, OutsideOfExtensionContextError.prototype);\n }\n}\n\n/** @public */\ntype UseHostResponse =\n | { host: undefined; error: Error }\n | { host: Host; error: undefined };\n\n/**\n * Retrieve the {@link @adobe/uix-host#Host} object hosting all extensions inside the current parent provider.\n *\n * @remarks Returns a `{ host, error }` tuple, not the host object directly.\n * @beta\n */\nexport function useHost(): UseHostResponse {\n const host = useContext(ExtensionContext);\n if (!(host instanceof Host)) {\n const error = new OutsideOfExtensionContextError(\n \"Attempt to use extensions outside of ExtensionContext. Wrap extensible part of application with Extensible component.\"\n );\n return {\n host: undefined,\n error,\n };\n }\n return { error: undefined, host };\n}\n","/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport { useCallback, useEffect, useMemo, useState } from \"react\";\nimport type {\n GuestConnection,\n GuestApis,\n RemoteGuestApis,\n VirtualApi,\n} from \"@adobe/uix-core\";\n\nimport { Host, HostEvents } from \"@adobe/uix-host\";\nimport type { CapabilitySpec } from \"@adobe/uix-host\";\nimport { useHost } from \"./useHost.js\";\n\n/**\n * @internal\n */\nexport interface TypedGuestConnection<T extends GuestApis>\n extends GuestConnection {\n id: GuestConnection[\"id\"];\n apis: RemoteGuestApis<T>;\n}\n\n/** @public */\nexport interface UseExtensionsConfig<\n Incoming extends GuestApis,\n Outgoing extends VirtualApi\n> {\n /**\n * A {@link @adobe/uix-host#CapabilitySpec} describing the namespaced methods\n * extensions must implement to be used by this component.\n *\n * @remarks\n * This declaration is used to filter the extensions that will be\n * returned; if they don't implement those methods, they will be filtered out.\n */\n requires?: CapabilitySpec<Incoming>;\n /**\n * A namespaced object of methods which extensions will be able to call.\n *\n * @remarks This is the counterpart of `requires`; in `requires`, the you\n * describes methods the extension must implement that your host code will\n * call, and in `provides`, you implement host methods that extensions will be\n * able to call.\n *\n * Most cases for host-side methods will use the state of the component. This\n * can cause unexpected bugs in React if the config callback is run on every\n * render. **useExtensions caches the config callback by default!**\n * So remember to pass a deps array, so that the config callback re-runs under\n * the right conditions.\n */\n provides?: Outgoing;\n /**\n * Sets when re-render is triggered on extension load.\n *\n * @remarks\n * Set to `each` to trigger a component re-render every time an individual\n * extension loads, which may result in multiple UI updates. Set to `all` to\n * wait until all extensions have loaded to re-render the component.\n * @defaultValue \"each\"\n */\n updateOn?: \"each\" | \"all\";\n}\n\n/** @public */\nexport interface UseExtensionsResult<T extends GuestApis> {\n /**\n * A list of loaded guests which implement the methods specified in\n * `requires`, represented as {@link @adobe/uix-host#Port} objects which\n * present methods to be called.\n */\n extensions: TypedGuestConnection<T>[];\n /**\n * This is `true` until all extensions are loaded. Use for rendering spinners\n * or other intermediate UI.\n */\n loading: boolean;\n /**\n * Populated with an Error if there were any problems during the load process.\n */\n error?: Error;\n}\n\nconst NO_EXTENSIONS: [] = [];\n\n/**\n * Fetch extensions which implement an API, provide them methods, and use them.\n *\n * @remarks `useExtensions` does three things at once:\n * - Gets all extensions which implement the APIs described in the `require` field\n * - Exposes any functions defined in the `provide` field to those extensions\n * - Returns an object whose `extensions` property is a list of `Port` objects representing those extensions\n *\n * useExtensions will trigger a re-render when extensions load. You can choose whether it triggers that rerender as each extension loads, or only after all extensions have loaded.\n * @public\n */\nexport function useExtensions<\n Incoming extends GuestApis,\n Outgoing extends VirtualApi\n>(\n configFactory: (host: Host) => UseExtensionsConfig<Incoming, Outgoing>,\n deps: unknown[] = []\n): UseExtensionsResult<Incoming> {\n const { host, error } = useHost();\n if (error) {\n return {\n extensions: NO_EXTENSIONS,\n loading: false,\n error,\n };\n }\n\n const baseDeps = [host, ...deps];\n const {\n requires,\n provides,\n updateOn = \"each\",\n } = useMemo(() => configFactory(host), baseDeps);\n\n const getExtensions = useCallback(() => {\n const newExtensions = [];\n const guests = host.getLoadedGuests(requires);\n for (const guest of guests) {\n if (provides) {\n guest.provide(provides);\n }\n newExtensions.push(guest as unknown as TypedGuestConnection<Incoming>);\n }\n return newExtensions.length === 0 ? NO_EXTENSIONS : newExtensions;\n }, [...baseDeps, requires]);\n\n const subscribe = useCallback(\n (handler: EventListener) => {\n const eventName = updateOn === \"all\" ? \"loadallguests\" : \"guestload\";\n host.addEventListener(eventName, handler);\n return () => host.removeEventListener(eventName, handler);\n },\n [...baseDeps, updateOn]\n );\n\n const [extensions, setExtensions] = useState(() => getExtensions());\n useEffect(() => {\n return subscribe(() => {\n setExtensions(getExtensions());\n });\n }, [subscribe, getExtensions]);\n\n const [hostError, setHostError] = useState<Error>();\n useEffect(\n () =>\n host.addEventListener(\n \"error\",\n (event: Extract<HostEvents, { detail: { error: Error } }>) =>\n setHostError(event.detail.error)\n ),\n baseDeps\n );\n\n return { extensions, loading: !host.loading, error: hostError };\n}\n"]}
@@ -0,0 +1,4 @@
1
+ /// <reference types="react" />
2
+ import { Host } from "@adobe/uix-host";
3
+ export declare const ExtensionContext: import("react").Context<Host>;
4
+ //# sourceMappingURL=extension-context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extension-context.d.ts","sourceRoot":"","sources":["../src/extension-context.ts"],"names":[],"mappings":";AAgBA,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAEvC,eAAO,MAAM,gBAAgB,+BAA6C,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from "./useExtensions.js";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/hooks/index.ts"],"names":[],"mappings":"AAYA,cAAc,oBAAoB,CAAC"}
@@ -0,0 +1,78 @@
1
+ import type { GuestConnection, GuestApis, RemoteGuestApis, VirtualApi } from "@adobe/uix-core";
2
+ import { Host } from "@adobe/uix-host";
3
+ import type { CapabilitySpec } from "@adobe/uix-host";
4
+ /**
5
+ * @internal
6
+ */
7
+ export interface TypedGuestConnection<T extends GuestApis> extends GuestConnection {
8
+ id: GuestConnection["id"];
9
+ apis: RemoteGuestApis<T>;
10
+ }
11
+ /** @public */
12
+ export interface UseExtensionsConfig<Incoming extends GuestApis, Outgoing extends VirtualApi> {
13
+ /**
14
+ * A {@link @adobe/uix-host#CapabilitySpec} describing the namespaced methods
15
+ * extensions must implement to be used by this component.
16
+ *
17
+ * @remarks
18
+ * This declaration is used to filter the extensions that will be
19
+ * returned; if they don't implement those methods, they will be filtered out.
20
+ */
21
+ requires?: CapabilitySpec<Incoming>;
22
+ /**
23
+ * A namespaced object of methods which extensions will be able to call.
24
+ *
25
+ * @remarks This is the counterpart of `requires`; in `requires`, the you
26
+ * describes methods the extension must implement that your host code will
27
+ * call, and in `provides`, you implement host methods that extensions will be
28
+ * able to call.
29
+ *
30
+ * Most cases for host-side methods will use the state of the component. This
31
+ * can cause unexpected bugs in React if the config callback is run on every
32
+ * render. **useExtensions caches the config callback by default!**
33
+ * So remember to pass a deps array, so that the config callback re-runs under
34
+ * the right conditions.
35
+ */
36
+ provides?: Outgoing;
37
+ /**
38
+ * Sets when re-render is triggered on extension load.
39
+ *
40
+ * @remarks
41
+ * Set to `each` to trigger a component re-render every time an individual
42
+ * extension loads, which may result in multiple UI updates. Set to `all` to
43
+ * wait until all extensions have loaded to re-render the component.
44
+ * @defaultValue "each"
45
+ */
46
+ updateOn?: "each" | "all";
47
+ }
48
+ /** @public */
49
+ export interface UseExtensionsResult<T extends GuestApis> {
50
+ /**
51
+ * A list of loaded guests which implement the methods specified in
52
+ * `requires`, represented as {@link @adobe/uix-host#Port} objects which
53
+ * present methods to be called.
54
+ */
55
+ extensions: TypedGuestConnection<T>[];
56
+ /**
57
+ * This is `true` until all extensions are loaded. Use for rendering spinners
58
+ * or other intermediate UI.
59
+ */
60
+ loading: boolean;
61
+ /**
62
+ * Populated with an Error if there were any problems during the load process.
63
+ */
64
+ error?: Error;
65
+ }
66
+ /**
67
+ * Fetch extensions which implement an API, provide them methods, and use them.
68
+ *
69
+ * @remarks `useExtensions` does three things at once:
70
+ * - Gets all extensions which implement the APIs described in the `require` field
71
+ * - Exposes any functions defined in the `provide` field to those extensions
72
+ * - Returns an object whose `extensions` property is a list of `Port` objects representing those extensions
73
+ *
74
+ * useExtensions will trigger a re-render when extensions load. You can choose whether it triggers that rerender as each extension loads, or only after all extensions have loaded.
75
+ * @public
76
+ */
77
+ export declare function useExtensions<Incoming extends GuestApis, Outgoing extends VirtualApi>(configFactory: (host: Host) => UseExtensionsConfig<Incoming, Outgoing>, deps?: unknown[]): UseExtensionsResult<Incoming>;
78
+ //# sourceMappingURL=useExtens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