piral-core 0.14.8 → 0.14.10-beta.3615
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/esm/components/ErrorBoundary.d.ts +12 -33
- package/esm/components/ErrorBoundary.js +12 -14
- package/esm/components/ErrorBoundary.js.map +1 -1
- package/esm/components/ForeignComponentContainer.d.ts +19 -0
- package/esm/components/ForeignComponentContainer.js +52 -0
- package/esm/components/ForeignComponentContainer.js.map +1 -0
- package/esm/components/index.d.ts +1 -0
- package/esm/components/index.js +1 -0
- package/esm/components/index.js.map +1 -1
- package/esm/components/wrapComponent.d.ts +7 -0
- package/esm/components/wrapComponent.js +42 -0
- package/esm/components/wrapComponent.js.map +1 -0
- package/esm/modules/core.js +3 -2
- package/esm/modules/core.js.map +1 -1
- package/esm/state/withApi.d.ts +1 -1
- package/esm/state/withApi.js +17 -103
- package/esm/state/withApi.js.map +1 -1
- package/esm/types/api.d.ts +2 -2
- package/esm/types/extension.d.ts +5 -1
- package/lib/components/ErrorBoundary.d.ts +12 -33
- package/lib/components/ErrorBoundary.js +12 -14
- package/lib/components/ErrorBoundary.js.map +1 -1
- package/lib/components/ForeignComponentContainer.d.ts +19 -0
- package/lib/components/ForeignComponentContainer.js +56 -0
- package/lib/components/ForeignComponentContainer.js.map +1 -0
- package/lib/components/index.d.ts +1 -0
- package/lib/components/index.js +1 -0
- package/lib/components/index.js.map +1 -1
- package/lib/components/wrapComponent.d.ts +7 -0
- package/lib/components/wrapComponent.js +46 -0
- package/lib/components/wrapComponent.js.map +1 -0
- package/lib/modules/core.js +3 -2
- package/lib/modules/core.js.map +1 -1
- package/lib/state/withApi.d.ts +1 -1
- package/lib/state/withApi.js +15 -101
- package/lib/state/withApi.js.map +1 -1
- package/lib/types/api.d.ts +2 -2
- package/lib/types/extension.d.ts +5 -1
- package/package.json +4 -4
- package/src/components/ErrorBoundary.tsx +19 -51
- package/src/components/ForeignComponentContainer.tsx +69 -0
- package/src/components/index.ts +1 -0
- package/src/components/wrapComponent.tsx +74 -0
- package/src/modules/core.ts +3 -2
- package/src/state/withApi.test.tsx +20 -4
- package/src/state/withApi.tsx +31 -157
- package/src/types/api.ts +2 -2
- package/src/types/extension.ts +12 -3
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { ForeignComponent, BaseComponentProps, ComponentContext } from '../types';
|
|
3
|
+
interface ForeignComponentContainerProps<T> {
|
|
4
|
+
$portalId: string;
|
|
5
|
+
$component: ForeignComponent<T>;
|
|
6
|
+
$context: ComponentContext;
|
|
7
|
+
innerProps: T & BaseComponentProps;
|
|
8
|
+
}
|
|
9
|
+
export declare class ForeignComponentContainer<T> extends React.Component<ForeignComponentContainerProps<T>> {
|
|
10
|
+
private current?;
|
|
11
|
+
private previous?;
|
|
12
|
+
private handler;
|
|
13
|
+
private setNode;
|
|
14
|
+
componentDidMount(): void;
|
|
15
|
+
componentDidUpdate(): void;
|
|
16
|
+
componentWillUnmount(): void;
|
|
17
|
+
render(): JSX.Element;
|
|
18
|
+
}
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ForeignComponentContainer = void 0;
|
|
4
|
+
const React = require("react");
|
|
5
|
+
const piral_base_1 = require("piral-base");
|
|
6
|
+
class ForeignComponentContainer extends React.Component {
|
|
7
|
+
constructor() {
|
|
8
|
+
super(...arguments);
|
|
9
|
+
this.handler = (ev) => {
|
|
10
|
+
const { innerProps } = this.props;
|
|
11
|
+
ev.stopPropagation();
|
|
12
|
+
innerProps.piral.renderHtmlExtension(ev.detail.target, ev.detail.props);
|
|
13
|
+
};
|
|
14
|
+
this.setNode = (node) => {
|
|
15
|
+
this.current = node;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
componentDidMount() {
|
|
19
|
+
const node = this.current;
|
|
20
|
+
const { $component, $context, innerProps } = this.props;
|
|
21
|
+
const { mount } = $component;
|
|
22
|
+
if (node && (0, piral_base_1.isfunc)(mount)) {
|
|
23
|
+
mount(node, innerProps, $context);
|
|
24
|
+
node.addEventListener('render-html', this.handler, false);
|
|
25
|
+
}
|
|
26
|
+
this.previous = node;
|
|
27
|
+
}
|
|
28
|
+
componentDidUpdate() {
|
|
29
|
+
const { current, previous } = this;
|
|
30
|
+
const { $component, $context, innerProps } = this.props;
|
|
31
|
+
const { update } = $component;
|
|
32
|
+
if (current !== previous) {
|
|
33
|
+
previous && this.componentWillUnmount();
|
|
34
|
+
current && this.componentDidMount();
|
|
35
|
+
}
|
|
36
|
+
else if ((0, piral_base_1.isfunc)(update)) {
|
|
37
|
+
update(current, innerProps, $context);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
componentWillUnmount() {
|
|
41
|
+
const node = this.previous;
|
|
42
|
+
const { $component } = this.props;
|
|
43
|
+
const { unmount } = $component;
|
|
44
|
+
if (node && (0, piral_base_1.isfunc)(unmount)) {
|
|
45
|
+
unmount(node);
|
|
46
|
+
node.removeEventListener('render-html', this.handler, false);
|
|
47
|
+
}
|
|
48
|
+
this.previous = undefined;
|
|
49
|
+
}
|
|
50
|
+
render() {
|
|
51
|
+
const { $portalId } = this.props;
|
|
52
|
+
return React.createElement("div", { "data-portal-id": $portalId, ref: this.setNode });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
exports.ForeignComponentContainer = ForeignComponentContainer;
|
|
56
|
+
//# sourceMappingURL=ForeignComponentContainer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ForeignComponentContainer.js","sourceRoot":"","sources":["../../src/components/ForeignComponentContainer.tsx"],"names":[],"mappings":";;;AAAA,+BAA+B;AAC/B,2CAAoC;AAWpC,MAAa,yBAA6B,SAAQ,KAAK,CAAC,SAA4C;IAApG;;QAGU,YAAO,GAAG,CAAC,EAAe,EAAE,EAAE;YACpC,MAAM,EAAE,UAAU,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;YAClC,EAAE,CAAC,eAAe,EAAE,CAAC;YACrB,UAAU,CAAC,KAAK,CAAC,mBAAmB,CAAC,EAAE,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC1E,CAAC,CAAC;QAEM,YAAO,GAAG,CAAC,IAAoB,EAAE,EAAE;YACzC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC;QACtB,CAAC,CAAC;IA6CJ,CAAC;IA3CC,iBAAiB;QACf,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC;QAC1B,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,UAAU,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;QACxD,MAAM,EAAE,KAAK,EAAE,GAAG,UAAU,CAAC;QAE7B,IAAI,IAAI,IAAI,IAAA,mBAAM,EAAC,KAAK,CAAC,EAAE;YACzB,KAAK,CAAC,IAAI,EAAE,UAAU,EAAE,QAAQ,CAAC,CAAC;YAClC,IAAI,CAAC,gBAAgB,CAAC,aAAa,EAAE,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;SAC3D;QAED,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;IACvB,CAAC;IAED,kBAAkB;QAChB,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,IAAI,CAAC;QACnC,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,UAAU,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;QACxD,MAAM,EAAE,MAAM,EAAE,GAAG,UAAU,CAAC;QAE9B,IAAI,OAAO,KAAK,QAAQ,EAAE;YACxB,QAAQ,IAAI,IAAI,CAAC,oBAAoB,EAAE,CAAC;YACxC,OAAO,IAAI,IAAI,CAAC,iBAAiB,EAAE,CAAC;SACrC;aAAM,IAAI,IAAA,mBAAM,EAAC,MAAM,CAAC,EAAE;YACzB,MAAM,CAAC,OAAO,EAAE,UAAU,EAAE,QAAQ,CAAC,CAAC;SACvC;IACH,CAAC;IAED,oBAAoB;QAClB,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,CAAC;QAC3B,MAAM,EAAE,UAAU,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;QAClC,MAAM,EAAE,OAAO,EAAE,GAAG,UAAU,CAAC;QAE/B,IAAI,IAAI,IAAI,IAAA,mBAAM,EAAC,OAAO,CAAC,EAAE;YAC3B,OAAO,CAAC,IAAI,CAAC,CAAC;YACd,IAAI,CAAC,mBAAmB,CAAC,aAAa,EAAE,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;SAC9D;QAED,IAAI,CAAC,QAAQ,GAAG,SAAS,CAAC;IAC5B,CAAC;IAED,MAAM;QACJ,MAAM,EAAE,SAAS,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;QACjC,OAAO,+CAAqB,SAAS,EAAE,GAAG,EAAE,IAAI,CAAC,OAAO,GAAI,CAAC;IAC/D,CAAC;CACF;AAxDD,8DAwDC"}
|
package/lib/components/index.js
CHANGED
|
@@ -21,4 +21,5 @@ const tslib_1 = require("tslib");
|
|
|
21
21
|
(0, tslib_1.__exportStar)(require("./SetRedirect"), exports);
|
|
22
22
|
(0, tslib_1.__exportStar)(require("./SetRoute"), exports);
|
|
23
23
|
(0, tslib_1.__exportStar)(require("./SwitchErrorInfo"), exports);
|
|
24
|
+
(0, tslib_1.__exportStar)(require("./wrapComponent"), exports);
|
|
24
25
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/components/index.ts"],"names":[],"mappings":";;;AAAA,4DAA6B;AAC7B,kEAAmC;AACnC,+DAAgC;AAChC,+DAAgC;AAChC,+DAAgC;AAChC,+DAAgC;AAChC,+DAAgC;AAChC,0DAA2B;AAC3B,6DAA8B;AAC9B,2DAA4B;AAC5B,gEAAiC;AACjC,kEAAmC;AACnC,8DAA+B;AAC/B,0DAA2B;AAC3B,2DAA4B;AAC5B,2DAA4B;AAC5B,6DAA8B;AAC9B,6DAA8B;AAC9B,0DAA2B;AAC3B,iEAAkC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/components/index.ts"],"names":[],"mappings":";;;AAAA,4DAA6B;AAC7B,kEAAmC;AACnC,+DAAgC;AAChC,+DAAgC;AAChC,+DAAgC;AAChC,+DAAgC;AAChC,+DAAgC;AAChC,0DAA2B;AAC3B,6DAA8B;AAC9B,2DAA4B;AAC5B,gEAAiC;AACjC,kEAAmC;AACnC,8DAA+B;AAC/B,0DAA2B;AAC3B,2DAA4B;AAC5B,2DAA4B;AAC5B,6DAA8B;AAC9B,6DAA8B;AAC9B,0DAA2B;AAC3B,iEAAkC;AAClC,+DAAgC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { AnyComponent, ComponentConverters, PiletApi, BaseComponentProps } from '../types';
|
|
3
|
+
interface CapturedProps {
|
|
4
|
+
piral: PiletApi;
|
|
5
|
+
}
|
|
6
|
+
export declare function wrapComponent<T>(converters: ComponentConverters<T & BaseComponentProps>, component: AnyComponent<T & BaseComponentProps>, captured: CapturedProps, Wrapper: React.FC<T>): React.MemoExoticComponent<(props: T) => JSX.Element> | React.ComponentType<T>;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.wrapComponent = void 0;
|
|
4
|
+
const React = require("react");
|
|
5
|
+
const react_router_1 = require("react-router");
|
|
6
|
+
const PortalRenderer_1 = require("./PortalRenderer");
|
|
7
|
+
const ForeignComponentContainer_1 = require("./ForeignComponentContainer");
|
|
8
|
+
const hooks_1 = require("../hooks");
|
|
9
|
+
const utils_1 = require("../utils");
|
|
10
|
+
// this is an arbitrary start number to have 6 digits
|
|
11
|
+
let portalIdBase = 123456;
|
|
12
|
+
function wrapReactComponent(Component, captured, Wrapper) {
|
|
13
|
+
return (props) => (React.createElement(Wrapper, Object.assign({}, props),
|
|
14
|
+
React.createElement(Component, Object.assign({}, props, captured))));
|
|
15
|
+
}
|
|
16
|
+
function wrapForeignComponent(component, captured, Wrapper) {
|
|
17
|
+
return React.memo((props) => {
|
|
18
|
+
const { state, readState, destroyPortal } = (0, hooks_1.useGlobalStateContext)();
|
|
19
|
+
const router = React.useContext(react_router_1.__RouterContext);
|
|
20
|
+
const id = React.useMemo(() => (portalIdBase++).toString(26), utils_1.none);
|
|
21
|
+
const context = React.useMemo(() => ({ router, state, readState }), [router, state]);
|
|
22
|
+
const innerProps = React.useMemo(() => (Object.assign(Object.assign({}, props), captured)), [props]);
|
|
23
|
+
React.useEffect(() => () => destroyPortal(id), utils_1.none);
|
|
24
|
+
return (React.createElement(Wrapper, Object.assign({}, props),
|
|
25
|
+
React.createElement(PortalRenderer_1.PortalRenderer, { id: id }),
|
|
26
|
+
React.createElement(ForeignComponentContainer_1.ForeignComponentContainer, { innerProps: innerProps, "$portalId": id, "$component": component, "$context": context })));
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
function isNotExotic(component) {
|
|
30
|
+
return !component.$$typeof;
|
|
31
|
+
}
|
|
32
|
+
function wrapComponent(converters, component, captured, Wrapper) {
|
|
33
|
+
if (!component) {
|
|
34
|
+
const pilet = captured.piral.meta.name;
|
|
35
|
+
console.error(`[${pilet}] The given value is not a valid component.`);
|
|
36
|
+
// tslint:disable-next-line:no-null-keyword
|
|
37
|
+
component = () => null;
|
|
38
|
+
}
|
|
39
|
+
if (typeof component === 'object' && isNotExotic(component)) {
|
|
40
|
+
const result = (0, utils_1.convertComponent)(converters[component.type], component);
|
|
41
|
+
return wrapForeignComponent(result, captured, Wrapper);
|
|
42
|
+
}
|
|
43
|
+
return wrapReactComponent(component, captured, Wrapper);
|
|
44
|
+
}
|
|
45
|
+
exports.wrapComponent = wrapComponent;
|
|
46
|
+
//# sourceMappingURL=wrapComponent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wrapComponent.js","sourceRoot":"","sources":["../../src/components/wrapComponent.tsx"],"names":[],"mappings":";;;AAAA,+BAA+B;AAC/B,+CAA+C;AAC/C,qDAAkD;AAClD,2EAAwE;AACxE,oCAAiD;AACjD,oCAAkD;AAGlD,qDAAqD;AACrD,IAAI,YAAY,GAAG,MAAM,CAAC;AAM1B,SAAS,kBAAkB,CACzB,SAAsD,EACtD,QAAuB,EACvB,OAAoB;IAEpB,OAAO,CAAC,KAAQ,EAAE,EAAE,CAAC,CACnB,oBAAC,OAAO,oBAAK,KAAK;QAChB,oBAAC,SAAS,oBAAK,KAAK,EAAM,QAAQ,EAAI,CAC9B,CACX,CAAC;AACJ,CAAC;AAED,SAAS,oBAAoB,CAC3B,SAAmD,EACnD,QAAuB,EACvB,OAAoB;IAEpB,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,KAAQ,EAAE,EAAE;QAC7B,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,GAAG,IAAA,6BAAqB,GAAE,CAAC;QACpE,MAAM,MAAM,GAAG,KAAK,CAAC,UAAU,CAAC,8BAAe,CAAC,CAAC;QACjD,MAAM,EAAE,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,YAAY,EAAE,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,YAAI,CAAC,CAAC;QACpE,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,EAAE,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC;QACrF,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,iCAAM,KAAK,GAAK,QAAQ,EAAG,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;QAE7E,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,aAAa,CAAC,EAAE,CAAC,EAAE,YAAI,CAAC,CAAC;QAErD,OAAO,CACL,oBAAC,OAAO,oBAAK,KAAK;YAChB,oBAAC,+BAAc,IAAC,EAAE,EAAE,EAAE,GAAI;YAC1B,oBAAC,qDAAyB,IAAC,UAAU,EAAE,UAAU,eAAa,EAAE,gBAAc,SAAS,cAAY,OAAO,GAAI,CACtG,CACX,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC;AAED,SAAS,WAAW,CAAC,SAAc;IACjC,OAAO,CAAE,SAAmC,CAAC,QAAQ,CAAC;AACxD,CAAC;AAED,SAAgB,aAAa,CAC3B,UAAuD,EACvD,SAA+C,EAC/C,QAAuB,EACvB,OAAoB;IAEpB,IAAI,CAAC,SAAS,EAAE;QACd,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC;QACvC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,6CAA6C,CAAC,CAAC;QACtE,2CAA2C;QAC3C,SAAS,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC;KACxB;IAED,IAAI,OAAO,SAAS,KAAK,QAAQ,IAAI,WAAW,CAAC,SAAS,CAAC,EAAE;QAC3D,MAAM,MAAM,GAAG,IAAA,wBAAgB,EAAC,UAAU,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,SAAS,CAAC,CAAC;QACvE,OAAO,oBAAoB,CAAI,MAAM,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC;KAC3D;IAED,OAAO,kBAAkB,CAAI,SAAS,EAAE,QAAQ,EAAE,OAAO,CAAC,CAAC;AAC7D,CAAC;AAnBD,sCAmBC"}
|
package/lib/modules/core.js
CHANGED
|
@@ -17,11 +17,12 @@ function createCoreApi(context) {
|
|
|
17
17
|
const expiration = (0, utils_1.getDataExpiration)(expires);
|
|
18
18
|
return context.tryWriteDataItem(name, value, pilet, target, expiration);
|
|
19
19
|
},
|
|
20
|
-
registerPage(route, arg, meta) {
|
|
20
|
+
registerPage(route, arg, meta = {}) {
|
|
21
|
+
const component = (0, state_1.withApi)(context, arg, api, 'page', undefined, { meta });
|
|
21
22
|
context.registerPage(route, {
|
|
22
23
|
pilet,
|
|
23
24
|
meta,
|
|
24
|
-
component
|
|
25
|
+
component,
|
|
25
26
|
});
|
|
26
27
|
return () => api.unregisterPage(route);
|
|
27
28
|
},
|
package/lib/modules/core.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"core.js","sourceRoot":"","sources":["../../src/modules/core.ts"],"names":[],"mappings":";;;AACA,uCAA0C;AAC1C,oCAAmC;AACnC,8CAA8C;AAC9C,oCAAgE;AAGhE,SAAgB,aAAa,CAAC,OAA2B;IACvD,OAAO,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE;QACnB,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC;QACxB,OAAO;YACL,OAAO,CAAC,IAAI;gBACV,OAAO,OAAO,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;YACrC,CAAC;YACD,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,OAAO;gBAC1B,MAAM,EAAE,MAAM,GAAG,QAAQ,EAAE,OAAO,EAAE,GAAG,IAAA,yBAAiB,EAAC,OAAO,CAAC,CAAC;gBAClE,MAAM,UAAU,GAAG,IAAA,yBAAiB,EAAC,OAAO,CAAC,CAAC;gBAC9C,OAAO,OAAO,CAAC,gBAAgB,CAAC,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC;YAC1E,CAAC;YACD,YAAY,CAAC,KAAK,EAAE,GAAG,EAAE,IAAI
|
|
1
|
+
{"version":3,"file":"core.js","sourceRoot":"","sources":["../../src/modules/core.ts"],"names":[],"mappings":";;;AACA,uCAA0C;AAC1C,oCAAmC;AACnC,8CAA8C;AAC9C,oCAAgE;AAGhE,SAAgB,aAAa,CAAC,OAA2B;IACvD,OAAO,CAAC,GAAG,EAAE,IAAI,EAAE,EAAE;QACnB,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC;QACxB,OAAO;YACL,OAAO,CAAC,IAAI;gBACV,OAAO,OAAO,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC;YACrC,CAAC;YACD,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,OAAO;gBAC1B,MAAM,EAAE,MAAM,GAAG,QAAQ,EAAE,OAAO,EAAE,GAAG,IAAA,yBAAiB,EAAC,OAAO,CAAC,CAAC;gBAClE,MAAM,UAAU,GAAG,IAAA,yBAAiB,EAAC,OAAO,CAAC,CAAC;gBAC9C,OAAO,OAAO,CAAC,gBAAgB,CAAC,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC;YAC1E,CAAC;YACD,YAAY,CAAC,KAAK,EAAE,GAAG,EAAE,IAAI,GAAG,EAAE;gBAChC,MAAM,SAAS,GAAG,IAAA,eAAO,EAAC,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,EAAE,IAAI,EAAE,CAAC,CAAC;gBAC1E,OAAO,CAAC,YAAY,CAAC,KAAK,EAAE;oBAC1B,KAAK;oBACL,IAAI;oBACJ,SAAS;iBACV,CAAC,CAAC;gBACH,OAAO,GAAG,EAAE,CAAC,GAAG,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;YACzC,CAAC;YACD,cAAc,CAAC,KAAK;gBAClB,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;YAChC,CAAC;YACD,iBAAiB,CAAC,IAAI,EAAE,GAAG,EAAE,QAAQ;gBACnC,OAAO,CAAC,iBAAiB,CAAC,IAAc,EAAE;oBACxC,KAAK;oBACL,SAAS,EAAE,IAAA,eAAO,EAAC,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,WAAW,CAAC;oBAClD,SAAS,EAAE,GAAG;oBACd,QAAQ;iBACT,CAAC,CAAC;gBACH,OAAO,GAAG,EAAE,CAAC,GAAG,CAAC,mBAAmB,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;YAClD,CAAC;YACD,mBAAmB,CAAC,IAAI,EAAE,GAAG;gBAC3B,OAAO,CAAC,mBAAmB,CAAC,IAAc,EAAE,GAAG,CAAC,CAAC;YACnD,CAAC;YACD,mBAAmB,CAAC,OAAO,EAAE,KAAK;gBAChC,MAAM,CAAC,OAAO,CAAC,GAAG,IAAA,uBAAa,EAAC,OAAO,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC;gBACzD,OAAO,OAAO,CAAC;YACjB,CAAC;YACD,SAAS,EAAE,0BAAa;SACzB,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AA3CD,sCA2CC"}
|
package/lib/state/withApi.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { AnyComponent, Errors, PiletApi, BaseComponentProps, GlobalStateContext } from '../types';
|
|
3
|
-
export declare function withApi<TProps>(context: GlobalStateContext, component: AnyComponent<TProps & BaseComponentProps>, piral: PiletApi, errorType: keyof Errors, wrapperType?: string): React.MemoExoticComponent<(props: TProps) => JSX.Element> | React.ComponentType<TProps>;
|
|
3
|
+
export declare function withApi<TProps>(context: GlobalStateContext, component: AnyComponent<TProps & BaseComponentProps>, piral: PiletApi, errorType: keyof Errors, wrapperType?: string, captured?: {}): React.MemoExoticComponent<(props: TProps) => JSX.Element> | React.ComponentType<TProps>;
|
package/lib/state/withApi.js
CHANGED
|
@@ -2,114 +2,28 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.withApi = void 0;
|
|
4
4
|
const React = require("react");
|
|
5
|
-
const piral_base_1 = require("piral-base");
|
|
6
|
-
const react_router_1 = require("react-router");
|
|
7
5
|
const components_1 = require("../components");
|
|
8
|
-
const hooks_1 = require("../hooks");
|
|
9
6
|
const utils_1 = require("../utils");
|
|
10
|
-
// this is an arbitrary start number to have 6 digits
|
|
11
|
-
let portalIdBase = 123456;
|
|
12
7
|
const DefaultWrapper = (props) => (0, utils_1.defaultRender)(props.children);
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
innerProps.piral.renderHtmlExtension(ev.detail.target, ev.detail.props);
|
|
20
|
-
};
|
|
21
|
-
this.setNode = (node) => {
|
|
22
|
-
this.current = node;
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
componentDidMount() {
|
|
26
|
-
const node = this.current;
|
|
27
|
-
const { $component, $context, innerProps } = this.props;
|
|
28
|
-
const { mount } = $component;
|
|
29
|
-
if (node && (0, piral_base_1.isfunc)(mount)) {
|
|
30
|
-
mount(node, innerProps, $context);
|
|
31
|
-
node.addEventListener('render-html', this.handler, false);
|
|
32
|
-
}
|
|
33
|
-
this.previous = node;
|
|
34
|
-
}
|
|
35
|
-
componentDidUpdate() {
|
|
36
|
-
const { current, previous } = this;
|
|
37
|
-
const { $component, $context, innerProps } = this.props;
|
|
38
|
-
const { update } = $component;
|
|
39
|
-
if (current !== previous) {
|
|
40
|
-
previous && this.componentWillUnmount();
|
|
41
|
-
current && this.componentDidMount();
|
|
42
|
-
}
|
|
43
|
-
else if ((0, piral_base_1.isfunc)(update)) {
|
|
44
|
-
update(current, innerProps, $context);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
componentWillUnmount() {
|
|
48
|
-
const node = this.previous;
|
|
49
|
-
const { $component } = this.props;
|
|
50
|
-
const { unmount } = $component;
|
|
51
|
-
if (node && (0, piral_base_1.isfunc)(unmount)) {
|
|
52
|
-
unmount(node);
|
|
53
|
-
node.removeEventListener('render-html', this.handler, false);
|
|
54
|
-
}
|
|
55
|
-
this.previous = undefined;
|
|
56
|
-
}
|
|
57
|
-
render() {
|
|
58
|
-
const { $portalId } = this.props;
|
|
59
|
-
return React.createElement("div", { "data-portal-id": $portalId, ref: this.setNode });
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
function wrapReactComponent(Component, stasisOptions, piral, Wrapper) {
|
|
63
|
-
return (props) => (React.createElement(Wrapper, Object.assign({}, props, { piral: piral }),
|
|
64
|
-
React.createElement(components_1.ErrorBoundary, Object.assign({}, stasisOptions, { renderProps: props }),
|
|
65
|
-
React.createElement(Component, Object.assign({}, props, { piral: piral })))));
|
|
66
|
-
}
|
|
67
|
-
function wrapForeignComponent(component, stasisOptions, piral, Wrapper) {
|
|
68
|
-
return React.memo((props) => {
|
|
69
|
-
const { state, readState, destroyPortal } = (0, hooks_1.useGlobalStateContext)();
|
|
70
|
-
const router = React.useContext(react_router_1.__RouterContext);
|
|
71
|
-
const id = React.useMemo(() => (portalIdBase++).toString(26), utils_1.none);
|
|
72
|
-
const context = React.useMemo(() => ({ router, state, readState }), [router, state]);
|
|
73
|
-
const innerProps = React.useMemo(() => (Object.assign(Object.assign({}, props), { piral })), [props]);
|
|
74
|
-
React.useEffect(() => () => destroyPortal(id), utils_1.none);
|
|
75
|
-
return (React.createElement(Wrapper, Object.assign({}, innerProps),
|
|
76
|
-
React.createElement(components_1.ErrorBoundary, Object.assign({}, stasisOptions, { renderProps: props }),
|
|
77
|
-
React.createElement(components_1.PortalRenderer, { id: id }),
|
|
78
|
-
React.createElement(ForeignComponentContainer, { innerProps: innerProps, "$portalId": id, "$component": component, "$context": context }))));
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
function isNotExotic(component) {
|
|
82
|
-
return !component.$$typeof;
|
|
83
|
-
}
|
|
84
|
-
function wrapComponent(converters, component, piral, Wrapper, stasisOptions) {
|
|
85
|
-
if (!component) {
|
|
86
|
-
console.error('The given value is not a valid component.');
|
|
87
|
-
// tslint:disable-next-line:no-null-keyword
|
|
88
|
-
component = () => null;
|
|
89
|
-
}
|
|
90
|
-
if (typeof component === 'object' && isNotExotic(component)) {
|
|
91
|
-
const result = (0, utils_1.convertComponent)(converters[component.type], component);
|
|
92
|
-
return wrapForeignComponent(result, stasisOptions, piral, Wrapper);
|
|
8
|
+
function getWrapper(wrappers, wrapperType) {
|
|
9
|
+
const WrapAll = wrappers['*'];
|
|
10
|
+
const WrapType = wrappers[wrapperType];
|
|
11
|
+
if (WrapAll && WrapType) {
|
|
12
|
+
return (props) => (React.createElement(WrapAll, Object.assign({}, props),
|
|
13
|
+
React.createElement(WrapType, Object.assign({}, props))));
|
|
93
14
|
}
|
|
94
|
-
return
|
|
15
|
+
return WrapType || WrapAll || DefaultWrapper;
|
|
95
16
|
}
|
|
96
|
-
function
|
|
97
|
-
|
|
17
|
+
function makeWrapper(context, outerProps, wrapperType, errorType) {
|
|
18
|
+
const OuterWrapper = context.readState((m) => getWrapper(m.registry.wrappers, wrapperType));
|
|
19
|
+
return (props) => (React.createElement(OuterWrapper, Object.assign({}, outerProps, props),
|
|
20
|
+
React.createElement(components_1.ErrorBoundary, Object.assign({}, outerProps, props, { errorType: errorType }), props.children)));
|
|
98
21
|
}
|
|
99
|
-
function withApi(context, component, piral, errorType, wrapperType = errorType) {
|
|
22
|
+
function withApi(context, component, piral, errorType, wrapperType = errorType, captured = {}) {
|
|
23
|
+
const outerProps = Object.assign(Object.assign({}, captured), { piral });
|
|
100
24
|
const converters = context.converters;
|
|
101
|
-
const Wrapper = context
|
|
102
|
-
return wrapComponent(converters, component,
|
|
103
|
-
onError(error) {
|
|
104
|
-
console.error(piral, error);
|
|
105
|
-
},
|
|
106
|
-
renderChild(child) {
|
|
107
|
-
return React.createElement(React.Suspense, { fallback: React.createElement(components_1.PiralLoadingIndicator, null) }, child);
|
|
108
|
-
},
|
|
109
|
-
renderError(error, props) {
|
|
110
|
-
return React.createElement(components_1.PiralError, Object.assign({ type: errorType, error: error }, props));
|
|
111
|
-
},
|
|
112
|
-
});
|
|
25
|
+
const Wrapper = makeWrapper(context, outerProps, wrapperType, errorType);
|
|
26
|
+
return (0, components_1.wrapComponent)(converters, component, outerProps, Wrapper);
|
|
113
27
|
}
|
|
114
28
|
exports.withApi = withApi;
|
|
115
29
|
//# sourceMappingURL=withApi.js.map
|
package/lib/state/withApi.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"withApi.js","sourceRoot":"","sources":["../../src/state/withApi.tsx"],"names":[],"mappings":";;;AAAA,+BAA+B;
|
|
1
|
+
{"version":3,"file":"withApi.js","sourceRoot":"","sources":["../../src/state/withApi.tsx"],"names":[],"mappings":";;;AAAA,+BAA+B;AAE/B,8CAA6D;AAC7D,oCAAyC;AAGzC,MAAM,cAAc,GAAa,CAAC,KAAK,EAAE,EAAE,CAAC,IAAA,qBAAa,EAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;AAE1E,SAAS,UAAU,CAAC,QAAkD,EAAE,WAAmB;IACzF,MAAM,OAAO,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC;IAC9B,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,CAAC,CAAC;IAEvC,IAAI,OAAO,IAAI,QAAQ,EAAE;QACvB,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAChB,oBAAC,OAAO,oBAAK,KAAK;YAChB,oBAAC,QAAQ,oBAAK,KAAK,EAAI,CACf,CACX,CAAC;KACH;IAED,OAAO,QAAQ,IAAI,OAAO,IAAI,cAAc,CAAC;AAC/C,CAAC;AAED,SAAS,WAAW,CAClB,OAA2B,EAC3B,UAAe,EACf,WAAmB,EACnB,SAAuB;IAEvB,MAAM,YAAY,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC,QAAQ,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC,CAAC;IAE5F,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAChB,oBAAC,YAAY,oBAAK,UAAU,EAAM,KAAK;QACrC,oBAAC,0BAAa,oBAAK,UAAU,EAAM,KAAK,IAAE,SAAS,EAAE,SAAS,KAC3D,KAAK,CAAC,QAAQ,CACD,CACH,CAChB,CAAC;AACJ,CAAC;AAED,SAAgB,OAAO,CACrB,OAA2B,EAC3B,SAAoD,EACpD,KAAe,EACf,SAAuB,EACvB,cAAsB,SAAS,EAC/B,QAAQ,GAAG,EAAE;IAEb,MAAM,UAAU,mCAAQ,QAAQ,KAAE,KAAK,GAAE,CAAC;IAC1C,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;IACtC,MAAM,OAAO,GAAG,WAAW,CAAS,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,SAAS,CAAC,CAAC;IACjF,OAAO,IAAA,0BAAa,EAAC,UAAU,EAAE,SAAS,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;AACnE,CAAC;AAZD,0BAYC"}
|
package/lib/types/api.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { RouteComponentProps } from 'react-router';
|
|
|
3
3
|
import type { PiletApi, Pilet, PiletMetadata, EventEmitter, SinglePilet, MultiPilet } from 'piral-base';
|
|
4
4
|
import type { PiletCustomApi, PiralCustomPageMeta } from './custom';
|
|
5
5
|
import type { AnyComponent } from './components';
|
|
6
|
-
import type { ExtensionSlotProps, PiralExtensionSlotMap } from './extension';
|
|
6
|
+
import type { ExtensionParams, ExtensionSlotProps, PiralExtensionSlotMap } from './extension';
|
|
7
7
|
import type { SharedData, DataStoreOptions } from './data';
|
|
8
8
|
import type { Disposable } from './utils';
|
|
9
9
|
export { PiletApi, Pilet, PiletMetadata, EventEmitter, SinglePilet, MultiPilet };
|
|
@@ -89,7 +89,7 @@ export interface PiletCoreApi {
|
|
|
89
89
|
* @param Component The component to be rendered.
|
|
90
90
|
* @param defaults Optionally, sets the default values for the expected data.
|
|
91
91
|
*/
|
|
92
|
-
registerExtension<TName>(name: TName extends string ? TName : string, Component: AnyComponent<ExtensionComponentProps<TName>>, defaults?: TName): RegistrationDisposer;
|
|
92
|
+
registerExtension<TName>(name: TName extends string ? TName : string, Component: AnyComponent<ExtensionComponentProps<TName>>, defaults?: Partial<ExtensionParams<TName>>): RegistrationDisposer;
|
|
93
93
|
/**
|
|
94
94
|
* Unregisters a global extension component.
|
|
95
95
|
* Only previously registered extension components can be unregistered.
|
package/lib/types/extension.d.ts
CHANGED
|
@@ -32,7 +32,11 @@ export interface BaseExtensionSlotProps<TName, TParams> {
|
|
|
32
32
|
*/
|
|
33
33
|
name: TName;
|
|
34
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Gives the extension params shape for the given extension slot name.
|
|
37
|
+
*/
|
|
38
|
+
export declare type ExtensionParams<TName> = TName extends keyof PiralExtensionSlotMap ? PiralExtensionSlotMap[TName] : TName extends string ? any : TName;
|
|
35
39
|
/**
|
|
36
40
|
* The props for defining an extension slot.
|
|
37
41
|
*/
|
|
38
|
-
export declare type ExtensionSlotProps<
|
|
42
|
+
export declare type ExtensionSlotProps<TName = string> = BaseExtensionSlotProps<TName extends string ? TName : string, ExtensionParams<TName>>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "piral-core",
|
|
3
|
-
"version": "0.14.
|
|
3
|
+
"version": "0.14.10-beta.3615",
|
|
4
4
|
"description": "The core library for creating a Piral instance.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"portal",
|
|
@@ -50,8 +50,8 @@
|
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
52
|
"@dbeining/react-atom": "^4.0.0",
|
|
53
|
-
"piral-base": "
|
|
54
|
-
"piral-debug-utils": "
|
|
53
|
+
"piral-base": "0.14.10-beta.3615",
|
|
54
|
+
"piral-debug-utils": "0.14.10-beta.3615"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
57
|
"react": ">=16.8.0",
|
|
@@ -78,5 +78,5 @@
|
|
|
78
78
|
"@libre/atom",
|
|
79
79
|
"@dbeining/react-atom"
|
|
80
80
|
],
|
|
81
|
-
"gitHead": "
|
|
81
|
+
"gitHead": "57d7688876ae35920a429d1580136d5f18c1d085"
|
|
82
82
|
}
|
|
@@ -1,42 +1,18 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { PiralError, PiralLoadingIndicator } from './components';
|
|
3
|
+
import { Errors, PiletApi } from '../types';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
* The options to be used for determining the boundary's behavior.
|
|
6
|
-
*/
|
|
7
|
-
export interface ErrorBoundaryOptions<T> {
|
|
8
|
-
/**
|
|
9
|
-
* Callback to be used in case of an emitted error.
|
|
10
|
-
* @param error The error caught by the boundary.
|
|
11
|
-
*/
|
|
12
|
-
onError(error: Error): void;
|
|
5
|
+
export interface ErrorBoundaryProps {
|
|
13
6
|
/**
|
|
14
|
-
*
|
|
15
|
-
* @param error The error caught by the boundary.
|
|
16
|
-
* @param props The props used by the boundary.
|
|
7
|
+
* The type of error to indicate when caught.
|
|
17
8
|
*/
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Callback to used when no error should be rendered.
|
|
21
|
-
* @param children The standard children to render.
|
|
22
|
-
* @param props The props used by the boundary.
|
|
23
|
-
*/
|
|
24
|
-
renderChild(children: React.ReactNode, props: T): React.ReactNode;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* The props of the ErrorBoundary component.
|
|
29
|
-
*/
|
|
30
|
-
export interface ErrorBoundaryProps<T> extends ErrorBoundaryOptions<T> {
|
|
9
|
+
errorType: keyof Errors;
|
|
31
10
|
/**
|
|
32
|
-
* The
|
|
11
|
+
* The associated pilet api for the metadata.
|
|
33
12
|
*/
|
|
34
|
-
|
|
13
|
+
piral: PiletApi;
|
|
35
14
|
}
|
|
36
15
|
|
|
37
|
-
/**
|
|
38
|
-
* The state of the ErrorBoundary component.
|
|
39
|
-
*/
|
|
40
16
|
export interface ErrorBoundaryState {
|
|
41
17
|
/**
|
|
42
18
|
* The current error (if any) caught by the boundary.
|
|
@@ -45,22 +21,17 @@ export interface ErrorBoundaryState {
|
|
|
45
21
|
}
|
|
46
22
|
|
|
47
23
|
/**
|
|
48
|
-
* The
|
|
24
|
+
* The component for catching errors and displaying error information.
|
|
49
25
|
*/
|
|
50
|
-
export class ErrorBoundary
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
error: undefined,
|
|
55
|
-
};
|
|
56
|
-
}
|
|
26
|
+
export class ErrorBoundary extends React.Component<ErrorBoundaryProps, ErrorBoundaryState> {
|
|
27
|
+
state = {
|
|
28
|
+
error: undefined,
|
|
29
|
+
};
|
|
57
30
|
|
|
58
31
|
componentDidCatch(error: Error) {
|
|
59
|
-
const {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
onError(error);
|
|
63
|
-
}
|
|
32
|
+
const { piral, errorType } = this.props;
|
|
33
|
+
const pilet = piral.meta.name;
|
|
34
|
+
console.error(`[${pilet}] Exception in component of type "${errorType}".`, error);
|
|
64
35
|
|
|
65
36
|
this.setState({
|
|
66
37
|
error,
|
|
@@ -68,17 +39,14 @@ export class ErrorBoundary<T> extends React.Component<ErrorBoundaryProps<T>, Err
|
|
|
68
39
|
}
|
|
69
40
|
|
|
70
41
|
render() {
|
|
71
|
-
const { children,
|
|
42
|
+
const { children, piral, errorType, ...renderProps } = this.props;
|
|
72
43
|
const { error } = this.state;
|
|
44
|
+
const rest: any = renderProps;
|
|
73
45
|
|
|
74
46
|
if (error) {
|
|
75
|
-
|
|
76
|
-
return renderError(error, renderProps);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return <div style={{ whiteSpace: 'pre-wrap' }}>{error && error.message}</div>;
|
|
47
|
+
return <PiralError type={errorType} error={error} {...rest} />;
|
|
80
48
|
}
|
|
81
49
|
|
|
82
|
-
return
|
|
50
|
+
return <React.Suspense fallback={<PiralLoadingIndicator />}>{children}</React.Suspense>;
|
|
83
51
|
}
|
|
84
52
|
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { isfunc } from 'piral-base';
|
|
3
|
+
import { __RouterContext } from 'react-router';
|
|
4
|
+
import { ForeignComponent, BaseComponentProps, ComponentContext } from '../types';
|
|
5
|
+
|
|
6
|
+
interface ForeignComponentContainerProps<T> {
|
|
7
|
+
$portalId: string;
|
|
8
|
+
$component: ForeignComponent<T>;
|
|
9
|
+
$context: ComponentContext;
|
|
10
|
+
innerProps: T & BaseComponentProps;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class ForeignComponentContainer<T> extends React.Component<ForeignComponentContainerProps<T>> {
|
|
14
|
+
private current?: HTMLElement;
|
|
15
|
+
private previous?: HTMLElement;
|
|
16
|
+
private handler = (ev: CustomEvent) => {
|
|
17
|
+
const { innerProps } = this.props;
|
|
18
|
+
ev.stopPropagation();
|
|
19
|
+
innerProps.piral.renderHtmlExtension(ev.detail.target, ev.detail.props);
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
private setNode = (node: HTMLDivElement) => {
|
|
23
|
+
this.current = node;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
componentDidMount() {
|
|
27
|
+
const node = this.current;
|
|
28
|
+
const { $component, $context, innerProps } = this.props;
|
|
29
|
+
const { mount } = $component;
|
|
30
|
+
|
|
31
|
+
if (node && isfunc(mount)) {
|
|
32
|
+
mount(node, innerProps, $context);
|
|
33
|
+
node.addEventListener('render-html', this.handler, false);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
this.previous = node;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
componentDidUpdate() {
|
|
40
|
+
const { current, previous } = this;
|
|
41
|
+
const { $component, $context, innerProps } = this.props;
|
|
42
|
+
const { update } = $component;
|
|
43
|
+
|
|
44
|
+
if (current !== previous) {
|
|
45
|
+
previous && this.componentWillUnmount();
|
|
46
|
+
current && this.componentDidMount();
|
|
47
|
+
} else if (isfunc(update)) {
|
|
48
|
+
update(current, innerProps, $context);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
componentWillUnmount() {
|
|
53
|
+
const node = this.previous;
|
|
54
|
+
const { $component } = this.props;
|
|
55
|
+
const { unmount } = $component;
|
|
56
|
+
|
|
57
|
+
if (node && isfunc(unmount)) {
|
|
58
|
+
unmount(node);
|
|
59
|
+
node.removeEventListener('render-html', this.handler, false);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
this.previous = undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
render() {
|
|
66
|
+
const { $portalId } = this.props;
|
|
67
|
+
return <div data-portal-id={$portalId} ref={this.setNode} />;
|
|
68
|
+
}
|
|
69
|
+
}
|
package/src/components/index.ts
CHANGED