piral-react 1.4.0-beta.6224
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/LICENSE +21 -0
- package/README.md +105 -0
- package/convert.d.ts +18 -0
- package/convert.js +16 -0
- package/esm/converter.d.ts +13 -0
- package/esm/converter.js +20 -0
- package/esm/converter.js.map +1 -0
- package/esm/create.d.ts +12 -0
- package/esm/create.js +20 -0
- package/esm/create.js.map +1 -0
- package/esm/extension.d.ts +1 -0
- package/esm/extension.js +57 -0
- package/esm/extension.js.map +1 -0
- package/esm/index.d.ts +2 -0
- package/esm/index.js +3 -0
- package/esm/index.js.map +1 -0
- package/esm/mount.d.ts +5 -0
- package/esm/mount.js +25 -0
- package/esm/mount.js.map +1 -0
- package/esm/types.d.ts +34 -0
- package/esm/types.js +2 -0
- package/esm/types.js.map +1 -0
- package/lib/converter.d.ts +13 -0
- package/lib/converter.js +24 -0
- package/lib/converter.js.map +1 -0
- package/lib/create.d.ts +12 -0
- package/lib/create.js +24 -0
- package/lib/create.js.map +1 -0
- package/lib/extension.d.ts +1 -0
- package/lib/extension.js +61 -0
- package/lib/extension.js.map +1 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +6 -0
- package/lib/index.js.map +1 -0
- package/lib/mount.d.ts +5 -0
- package/lib/mount.js +31 -0
- package/lib/mount.js.map +1 -0
- package/lib/types.d.ts +34 -0
- package/lib/types.js +3 -0
- package/lib/types.js.map +1 -0
- package/package.json +73 -0
- package/src/converter.ts +30 -0
- package/src/create.ts +28 -0
- package/src/extension.ts +62 -0
- package/src/index.ts +2 -0
- package/src/mount.ts +41 -0
- package/src/types.ts +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 - 2023 smapiot
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
[](https://piral.io)
|
|
2
|
+
|
|
3
|
+
# [Piral React](https://piral.io) · [](https://github.com/smapiot/piral/blob/main/LICENSE) [](https://www.npmjs.com/package/piral-react) [](https://jestjs.io) [](https://gitter.im/piral-io/community)
|
|
4
|
+
|
|
5
|
+
This is a plugin that only has a peer dependency to `react@>=16` and `react-dom@>=16`. What `piral-react` brings to the table is a set of Pilet API extensions that can be used with `piral` or `piral-core`.
|
|
6
|
+
|
|
7
|
+
The set includes a React 16+ converter for any component registration, as well as a `fromReact` shortcut and a `ReactExtension` component.
|
|
8
|
+
|
|
9
|
+
By default, these API extensions are not integrated in `piral`, so you'd need to add them to your Piral instance.
|
|
10
|
+
|
|
11
|
+
## Documentation
|
|
12
|
+
|
|
13
|
+
The following functions are brought to the Pilet API.
|
|
14
|
+
|
|
15
|
+
### `fromReact()`
|
|
16
|
+
|
|
17
|
+
Transforms a standard React 16+ component into a component that can be used in Piral, essentially wrapping it with a reference to the corresponding converter.
|
|
18
|
+
|
|
19
|
+
### `ReactExtension`
|
|
20
|
+
|
|
21
|
+
The extension slot component to be used in React 16+ components.
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
::: summary: For pilet authors
|
|
26
|
+
|
|
27
|
+
You can use the `fromReact` function from the Pilet API to convert your React 16+ components to components usable by your Piral instance.
|
|
28
|
+
|
|
29
|
+
Example use:
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import { PiletApi } from '<name-of-piral-instance>';
|
|
33
|
+
import { ReactPage } from './ReactPage';
|
|
34
|
+
|
|
35
|
+
export function setup(piral: PiletApi) {
|
|
36
|
+
piral.registerPage('/sample', piral.fromReact(ReactPage));
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Within React 16+ components the Piral React 16+ extension component can be used by referring to `ReactExtension`, e.g.,
|
|
41
|
+
|
|
42
|
+
```jsx
|
|
43
|
+
<ReactExtension name="name-of-extension" />
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Alternatively, if `piral-react` has not been added to the Piral instance you can install and use the package also from a pilet directly.
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
import { PiletApi } from '<name-of-piral-instance>';
|
|
50
|
+
import { fromReact } from 'piral-react/convert';
|
|
51
|
+
import { ReactPage } from './ReactPage';
|
|
52
|
+
|
|
53
|
+
export function setup(piral: PiletApi) {
|
|
54
|
+
piral.registerPage('/sample', fromReact(ReactPage));
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
:::
|
|
59
|
+
|
|
60
|
+
::: summary: For Piral instance developers
|
|
61
|
+
|
|
62
|
+
Using React 16+ with Piral is as simple as installing `piral-react` and `react`. For `react` add the following two packages to your project's dependencies:
|
|
63
|
+
|
|
64
|
+
```json
|
|
65
|
+
{
|
|
66
|
+
"dependencies": {
|
|
67
|
+
"react`": "^18",
|
|
68
|
+
"react-dom": "^18"
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Now you are ready to use the `piral-react` converter:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { createReactApi } from 'piral-react';
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
The integration looks like:
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
const instance = createInstance({
|
|
83
|
+
// important part
|
|
84
|
+
plugins: [createReactApi()],
|
|
85
|
+
// ...
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
The `react` package (or whatever alias you've chosen) should be shared with the pilets via the *package.json*:
|
|
90
|
+
|
|
91
|
+
```json
|
|
92
|
+
{
|
|
93
|
+
"importmap": {
|
|
94
|
+
"imports": {
|
|
95
|
+
"react": ""
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
:::
|
|
102
|
+
|
|
103
|
+
## License
|
|
104
|
+
|
|
105
|
+
Piral is released using the MIT license. For more information see the [license file](./LICENSE).
|
package/convert.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { createConverter } from './esm/converter';
|
|
2
|
+
export interface HtmlComponent<TProps> {
|
|
3
|
+
component: {
|
|
4
|
+
mount(element: HTMLElement, props: TProps, ctx: any, locals: any): void;
|
|
5
|
+
update?(element: HTMLElement, props: TProps, ctx: any, locals: any): void;
|
|
6
|
+
unmount?(element: HTMLElement, locals: any): void;
|
|
7
|
+
};
|
|
8
|
+
type: 'html';
|
|
9
|
+
}
|
|
10
|
+
export interface ReactConverter {
|
|
11
|
+
(...params: Parameters<ReturnType<typeof createConverter>>): HtmlComponent<any>;
|
|
12
|
+
}
|
|
13
|
+
export declare function createReactConverter(...params: Parameters<typeof createConverter>): {
|
|
14
|
+
from: ReactConverter;
|
|
15
|
+
Extension: any;
|
|
16
|
+
};
|
|
17
|
+
declare const fromReact: ReactConverter, ReactExtension: any;
|
|
18
|
+
export { fromReact, ReactExtension };
|
package/convert.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { createConverter } from './esm/converter';
|
|
2
|
+
export function createReactConverter() {
|
|
3
|
+
var params = [];
|
|
4
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
5
|
+
params[_i] = arguments[_i];
|
|
6
|
+
}
|
|
7
|
+
var convert = createConverter.apply(void 0, params);
|
|
8
|
+
var Extension = convert.Extension;
|
|
9
|
+
var from = function (root) { return ({
|
|
10
|
+
type: 'html',
|
|
11
|
+
component: convert(root),
|
|
12
|
+
}); };
|
|
13
|
+
return { from: from, Extension: Extension };
|
|
14
|
+
}
|
|
15
|
+
var _a = createReactConverter(), fromReact = _a.from, ReactExtension = _a.Extension;
|
|
16
|
+
export { fromReact, ReactExtension };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ForeignComponent, BaseComponentProps } from 'piral-core';
|
|
2
|
+
import type { ComponentType } from 'react';
|
|
3
|
+
export interface ReactConverterOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Defines the name of the root element.
|
|
6
|
+
* @default piral-slot
|
|
7
|
+
*/
|
|
8
|
+
rootName?: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function createConverter(config?: ReactConverterOptions): {
|
|
11
|
+
<TProps extends BaseComponentProps>(root: ComponentType<TProps>): ForeignComponent<TProps>;
|
|
12
|
+
Extension: any;
|
|
13
|
+
};
|
package/esm/converter.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { createExtension } from './extension';
|
|
2
|
+
import { mountReact, unmountReact } from './mount';
|
|
3
|
+
export function createConverter(config = {}) {
|
|
4
|
+
const { rootName = 'piral-slot' } = config;
|
|
5
|
+
const Extension = createExtension(rootName);
|
|
6
|
+
const convert = (root) => ({
|
|
7
|
+
mount(el, props, ctx) {
|
|
8
|
+
mountReact(el, root, props, ctx);
|
|
9
|
+
},
|
|
10
|
+
update(el, props, ctx) {
|
|
11
|
+
mountReact(el, root, props, ctx);
|
|
12
|
+
},
|
|
13
|
+
unmount(el) {
|
|
14
|
+
unmountReact(el);
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
convert.Extension = Extension;
|
|
18
|
+
return convert;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=converter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"converter.js","sourceRoot":"","sources":["../src/converter.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAUnD,MAAM,UAAU,eAAe,CAAC,SAAgC,EAAE;IAChE,MAAM,EAAE,QAAQ,GAAG,YAAY,EAAE,GAAG,MAAM,CAAC;IAC3C,MAAM,SAAS,GAAG,eAAe,CAAC,QAAQ,CAAC,CAAC;IAC5C,MAAM,OAAO,GAAG,CAAoC,IAA2B,EAA4B,EAAE,CAAC,CAAC;QAC7G,KAAK,CAAC,EAAE,EAAE,KAAK,EAAE,GAAG;YAClB,UAAU,CAAC,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;QACnC,CAAC;QACD,MAAM,CAAC,EAAE,EAAE,KAAK,EAAE,GAAG;YACnB,UAAU,CAAC,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;QACnC,CAAC;QACD,OAAO,CAAC,EAAE;YACR,YAAY,CAAC,EAAE,CAAC,CAAC;QACnB,CAAC;KACF,CAAC,CAAC;IACH,OAAO,CAAC,SAAS,GAAG,SAAS,CAAC;IAC9B,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
package/esm/create.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { PiralPlugin } from 'piral-core';
|
|
2
|
+
import { ReactConverterOptions } from './converter';
|
|
3
|
+
import type { PiletReactApi } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* Available configuration options for the React 16+ plugin.
|
|
6
|
+
*/
|
|
7
|
+
export interface ReactConfig extends ReactConverterOptions {
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Creates Pilet API extensions for integrating React 16+.
|
|
11
|
+
*/
|
|
12
|
+
export declare function createReactApi(config?: ReactConfig): PiralPlugin<PiletReactApi>;
|
package/esm/create.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { createConverter } from './converter';
|
|
2
|
+
/**
|
|
3
|
+
* Creates Pilet API extensions for integrating React 16+.
|
|
4
|
+
*/
|
|
5
|
+
export function createReactApi(config = {}) {
|
|
6
|
+
return (context) => {
|
|
7
|
+
const convert = createConverter(config);
|
|
8
|
+
context.converters.react = ({ root }) => convert(root);
|
|
9
|
+
return {
|
|
10
|
+
fromReact(root) {
|
|
11
|
+
return {
|
|
12
|
+
type: 'react',
|
|
13
|
+
root,
|
|
14
|
+
};
|
|
15
|
+
},
|
|
16
|
+
ReactExtension: convert.Extension,
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=create.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAyB,MAAM,aAAa,CAAC;AAQrE;;GAEG;AACH,MAAM,UAAU,cAAc,CAAC,SAAsB,EAAE;IACrD,OAAO,CAAC,OAAO,EAAE,EAAE;QACjB,MAAM,OAAO,GAAG,eAAe,CAAC,MAAM,CAAC,CAAC;QACxC,OAAO,CAAC,UAAU,CAAC,KAAK,GAAG,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAEvD,OAAO;YACL,SAAS,CAAC,IAAI;gBACZ,OAAO;oBACL,IAAI,EAAE,OAAO;oBACb,IAAI;iBACL,CAAC;YACJ,CAAC;YACD,cAAc,EAAE,OAAO,CAAC,SAAS;SAClC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function createExtension(rootName: string): any;
|
package/esm/extension.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { __setFunctionName } from "tslib";
|
|
2
|
+
import { createElement, Component } from 'react';
|
|
3
|
+
import { anyPropType } from './mount';
|
|
4
|
+
function compareObjects(a, b) {
|
|
5
|
+
for (const i in a) {
|
|
6
|
+
if (!(i in b)) {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
for (const i in b) {
|
|
11
|
+
if (!compare(a[i], b[i])) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
function compare(a, b) {
|
|
18
|
+
if (a !== b) {
|
|
19
|
+
const ta = typeof a;
|
|
20
|
+
const tb = typeof b;
|
|
21
|
+
if (ta === tb && ta === 'object' && a && b) {
|
|
22
|
+
return compareObjects(a, b);
|
|
23
|
+
}
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
export function createExtension(rootName) {
|
|
29
|
+
var _a;
|
|
30
|
+
const ReactExtension = (_a = class extends Component {
|
|
31
|
+
constructor() {
|
|
32
|
+
super(...arguments);
|
|
33
|
+
this.onRefChange = (element) => {
|
|
34
|
+
if (element) {
|
|
35
|
+
const { piral } = this.context;
|
|
36
|
+
element.innerHTML = '';
|
|
37
|
+
piral.renderHtmlExtension(element, this.props);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
shouldComponentUpdate(nextProps) {
|
|
42
|
+
return !compare(this.props, nextProps);
|
|
43
|
+
}
|
|
44
|
+
render() {
|
|
45
|
+
return createElement(rootName, {
|
|
46
|
+
ref: this.onRefChange,
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
__setFunctionName(_a, "ReactExtension"),
|
|
51
|
+
_a.contextTypes = {
|
|
52
|
+
piral: anyPropType,
|
|
53
|
+
},
|
|
54
|
+
_a);
|
|
55
|
+
return ReactExtension;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=extension.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extension.js","sourceRoot":"","sources":["../src/extension.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAEtC,SAAS,cAAc,CAAC,CAAM,EAAE,CAAM;IACpC,KAAK,MAAM,CAAC,IAAI,CAAC,EAAE;QACjB,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE;YACb,OAAO,KAAK,CAAC;SACd;KACF;IAED,KAAK,MAAM,CAAC,IAAI,CAAC,EAAE;QACjB,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE;YACxB,OAAO,KAAK,CAAC;SACd;KACF;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,OAAO,CAAI,CAAI,EAAE,CAAI;IAC5B,IAAI,CAAC,KAAK,CAAC,EAAE;QACX,MAAM,EAAE,GAAG,OAAO,CAAC,CAAC;QACpB,MAAM,EAAE,GAAG,OAAO,CAAC,CAAC;QAEpB,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,QAAQ,IAAI,CAAC,IAAI,CAAC,EAAE;YAC1C,OAAO,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;SAC7B;QAED,OAAO,KAAK,CAAC;KACd;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,QAAgB;;IAC9C,MAAM,cAAc,SAAQ,KAAM,SAAQ,SAA6B;YAA3C;;gBAKlB,gBAAW,GAAG,CAAC,OAAoB,EAAE,EAAE;oBAC7C,IAAI,OAAO,EAAE;wBACX,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC,OAA8B,CAAC;wBACtD,OAAO,CAAC,SAAS,GAAG,EAAE,CAAC;wBACvB,KAAK,CAAC,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;qBAChD;gBACH,CAAC,CAAC;YAWJ,CAAC;YATC,qBAAqB,CAAC,SAA6B;gBACjD,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;YACzC,CAAC;YAED,MAAM;gBACJ,OAAO,aAAa,CAAC,QAAQ,EAAE;oBAC7B,GAAG,EAAE,IAAI,CAAC,WAAW;iBACtB,CAAC,CAAC;YACL,CAAC;SACF;;QArBQ,eAAY,GAAG;YACpB,KAAK,EAAE,WAAW;SAClB;WAmBH,CAAC;IAEF,OAAO,cAAc,CAAC;AACxB,CAAC"}
|
package/esm/index.d.ts
ADDED
package/esm/index.js
ADDED
package/esm/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC"}
|
package/esm/mount.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { BaseComponentProps } from 'piral-core';
|
|
2
|
+
import { ComponentType } from 'react';
|
|
3
|
+
export declare const anyPropType: () => any;
|
|
4
|
+
export declare function mountReact<T extends BaseComponentProps>(el: HTMLElement, root: ComponentType<T>, props: T, ctx?: any): void;
|
|
5
|
+
export declare function unmountReact(el: HTMLElement): void;
|
package/esm/mount.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { render } from 'react-dom';
|
|
2
|
+
import { createElement, Component } from 'react';
|
|
3
|
+
// tslint:disable-next-line:no-null-keyword
|
|
4
|
+
export const anyPropType = () => null;
|
|
5
|
+
export function mountReact(el, root, props, ctx = {}) {
|
|
6
|
+
const contextTypes = {};
|
|
7
|
+
['piral', ...Object.keys(ctx)].forEach((key) => {
|
|
8
|
+
contextTypes[key] = anyPropType;
|
|
9
|
+
});
|
|
10
|
+
class Provider extends Component {
|
|
11
|
+
getChildContext() {
|
|
12
|
+
return Object.assign({ piral: props.piral }, ctx);
|
|
13
|
+
}
|
|
14
|
+
render() {
|
|
15
|
+
return this.props.children;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
Provider.childContextTypes = contextTypes;
|
|
19
|
+
render(createElement(Provider, {}, createElement(root, props)), el);
|
|
20
|
+
}
|
|
21
|
+
export function unmountReact(el) {
|
|
22
|
+
// tslint:disable-next-line:no-null-keyword
|
|
23
|
+
render(null, el);
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=mount.js.map
|
package/esm/mount.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mount.js","sourceRoot":"","sources":["../src/mount.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AACnC,OAAO,EAAE,aAAa,EAAiB,SAAS,EAAE,MAAM,OAAO,CAAC;AAEhE,2CAA2C;AAC3C,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC;AAEtC,MAAM,UAAU,UAAU,CACxB,EAAe,EACf,IAAsB,EACtB,KAAQ,EACR,MAAW,EAAE;IAEb,MAAM,YAAY,GAAG,EAAE,CAAC;IAExB,CAAC,OAAO,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;QAC7C,YAAY,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC;IAClC,CAAC,CAAC,CAAC;IAEH,MAAM,QAAS,SAAQ,SAA6B;QAGlD,eAAe;YACb,uBACE,KAAK,EAAE,KAAK,CAAC,KAAK,IACf,GAAG,EACN;QACJ,CAAC;QAED,MAAM;YACJ,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC;QAC7B,CAAC;;IAXM,0BAAiB,GAAG,YAAY,CAAC;IAc1C,MAAM,CAAC,aAAa,CAAC,QAAQ,EAAE,EAAE,EAAE,aAAa,CAAC,IAAW,EAAE,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AAC7E,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,EAAe;IAC1C,2CAA2C;IAC3C,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;AACnB,CAAC"}
|
package/esm/types.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { ForeignComponent, ExtensionSlotProps } from 'piral-core';
|
|
2
|
+
import type { Component, ComponentType } from 'react';
|
|
3
|
+
declare module 'piral-core/lib/types/custom' {
|
|
4
|
+
interface PiletCustomApi extends PiletReactApi {
|
|
5
|
+
}
|
|
6
|
+
interface PiralCustomComponentConverters<TProps> {
|
|
7
|
+
react(component: ReactComponent<TProps>): ForeignComponent<TProps>;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export interface ReactComponent<TProps> {
|
|
11
|
+
/**
|
|
12
|
+
* The component root.
|
|
13
|
+
*/
|
|
14
|
+
root: ComponentType<TProps>;
|
|
15
|
+
/**
|
|
16
|
+
* The type of the React 16+ component.
|
|
17
|
+
*/
|
|
18
|
+
type: 'react';
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Defines the provided set of Pilet API extensions from the React 16+ plugin.
|
|
22
|
+
*/
|
|
23
|
+
export interface PiletReactApi {
|
|
24
|
+
/**
|
|
25
|
+
* Wraps an React 16+ component for use in Piral.
|
|
26
|
+
* @param component The component root.
|
|
27
|
+
* @returns The Piral React 16+ component.
|
|
28
|
+
*/
|
|
29
|
+
fromReact<TProps>(component: ComponentType<TProps>): ReactComponent<TProps>;
|
|
30
|
+
/**
|
|
31
|
+
* React 16+ component for displaying extensions of the given name.
|
|
32
|
+
*/
|
|
33
|
+
ReactExtension: Component<ExtensionSlotProps>;
|
|
34
|
+
}
|
package/esm/types.js
ADDED
package/esm/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ForeignComponent, BaseComponentProps } from 'piral-core';
|
|
2
|
+
import type { ComponentType } from 'react';
|
|
3
|
+
export interface ReactConverterOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Defines the name of the root element.
|
|
6
|
+
* @default piral-slot
|
|
7
|
+
*/
|
|
8
|
+
rootName?: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function createConverter(config?: ReactConverterOptions): {
|
|
11
|
+
<TProps extends BaseComponentProps>(root: ComponentType<TProps>): ForeignComponent<TProps>;
|
|
12
|
+
Extension: any;
|
|
13
|
+
};
|
package/lib/converter.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createConverter = void 0;
|
|
4
|
+
const extension_1 = require("./extension");
|
|
5
|
+
const mount_1 = require("./mount");
|
|
6
|
+
function createConverter(config = {}) {
|
|
7
|
+
const { rootName = 'piral-slot' } = config;
|
|
8
|
+
const Extension = (0, extension_1.createExtension)(rootName);
|
|
9
|
+
const convert = (root) => ({
|
|
10
|
+
mount(el, props, ctx) {
|
|
11
|
+
(0, mount_1.mountReact)(el, root, props, ctx);
|
|
12
|
+
},
|
|
13
|
+
update(el, props, ctx) {
|
|
14
|
+
(0, mount_1.mountReact)(el, root, props, ctx);
|
|
15
|
+
},
|
|
16
|
+
unmount(el) {
|
|
17
|
+
(0, mount_1.unmountReact)(el);
|
|
18
|
+
},
|
|
19
|
+
});
|
|
20
|
+
convert.Extension = Extension;
|
|
21
|
+
return convert;
|
|
22
|
+
}
|
|
23
|
+
exports.createConverter = createConverter;
|
|
24
|
+
//# sourceMappingURL=converter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"converter.js","sourceRoot":"","sources":["../src/converter.ts"],"names":[],"mappings":";;;AAEA,2CAA8C;AAC9C,mCAAmD;AAUnD,SAAgB,eAAe,CAAC,SAAgC,EAAE;IAChE,MAAM,EAAE,QAAQ,GAAG,YAAY,EAAE,GAAG,MAAM,CAAC;IAC3C,MAAM,SAAS,GAAG,IAAA,2BAAe,EAAC,QAAQ,CAAC,CAAC;IAC5C,MAAM,OAAO,GAAG,CAAoC,IAA2B,EAA4B,EAAE,CAAC,CAAC;QAC7G,KAAK,CAAC,EAAE,EAAE,KAAK,EAAE,GAAG;YAClB,IAAA,kBAAU,EAAC,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;QACnC,CAAC;QACD,MAAM,CAAC,EAAE,EAAE,KAAK,EAAE,GAAG;YACnB,IAAA,kBAAU,EAAC,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;QACnC,CAAC;QACD,OAAO,CAAC,EAAE;YACR,IAAA,oBAAY,EAAC,EAAE,CAAC,CAAC;QACnB,CAAC;KACF,CAAC,CAAC;IACH,OAAO,CAAC,SAAS,GAAG,SAAS,CAAC;IAC9B,OAAO,OAAO,CAAC;AACjB,CAAC;AAhBD,0CAgBC"}
|
package/lib/create.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { PiralPlugin } from 'piral-core';
|
|
2
|
+
import { ReactConverterOptions } from './converter';
|
|
3
|
+
import type { PiletReactApi } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* Available configuration options for the React 16+ plugin.
|
|
6
|
+
*/
|
|
7
|
+
export interface ReactConfig extends ReactConverterOptions {
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Creates Pilet API extensions for integrating React 16+.
|
|
11
|
+
*/
|
|
12
|
+
export declare function createReactApi(config?: ReactConfig): PiralPlugin<PiletReactApi>;
|
package/lib/create.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createReactApi = void 0;
|
|
4
|
+
const converter_1 = require("./converter");
|
|
5
|
+
/**
|
|
6
|
+
* Creates Pilet API extensions for integrating React 16+.
|
|
7
|
+
*/
|
|
8
|
+
function createReactApi(config = {}) {
|
|
9
|
+
return (context) => {
|
|
10
|
+
const convert = (0, converter_1.createConverter)(config);
|
|
11
|
+
context.converters.react = ({ root }) => convert(root);
|
|
12
|
+
return {
|
|
13
|
+
fromReact(root) {
|
|
14
|
+
return {
|
|
15
|
+
type: 'react',
|
|
16
|
+
root,
|
|
17
|
+
};
|
|
18
|
+
},
|
|
19
|
+
ReactExtension: convert.Extension,
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
exports.createReactApi = createReactApi;
|
|
24
|
+
//# sourceMappingURL=create.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":";;;AACA,2CAAqE;AAQrE;;GAEG;AACH,SAAgB,cAAc,CAAC,SAAsB,EAAE;IACrD,OAAO,CAAC,OAAO,EAAE,EAAE;QACjB,MAAM,OAAO,GAAG,IAAA,2BAAe,EAAC,MAAM,CAAC,CAAC;QACxC,OAAO,CAAC,UAAU,CAAC,KAAK,GAAG,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAEvD,OAAO;YACL,SAAS,CAAC,IAAI;gBACZ,OAAO;oBACL,IAAI,EAAE,OAAO;oBACb,IAAI;iBACL,CAAC;YACJ,CAAC;YACD,cAAc,EAAE,OAAO,CAAC,SAAS;SAClC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAfD,wCAeC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function createExtension(rootName: string): any;
|
package/lib/extension.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createExtension = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const mount_1 = require("./mount");
|
|
7
|
+
function compareObjects(a, b) {
|
|
8
|
+
for (const i in a) {
|
|
9
|
+
if (!(i in b)) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
for (const i in b) {
|
|
14
|
+
if (!compare(a[i], b[i])) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
function compare(a, b) {
|
|
21
|
+
if (a !== b) {
|
|
22
|
+
const ta = typeof a;
|
|
23
|
+
const tb = typeof b;
|
|
24
|
+
if (ta === tb && ta === 'object' && a && b) {
|
|
25
|
+
return compareObjects(a, b);
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
function createExtension(rootName) {
|
|
32
|
+
var _a;
|
|
33
|
+
const ReactExtension = (_a = class extends react_1.Component {
|
|
34
|
+
constructor() {
|
|
35
|
+
super(...arguments);
|
|
36
|
+
this.onRefChange = (element) => {
|
|
37
|
+
if (element) {
|
|
38
|
+
const { piral } = this.context;
|
|
39
|
+
element.innerHTML = '';
|
|
40
|
+
piral.renderHtmlExtension(element, this.props);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
shouldComponentUpdate(nextProps) {
|
|
45
|
+
return !compare(this.props, nextProps);
|
|
46
|
+
}
|
|
47
|
+
render() {
|
|
48
|
+
return (0, react_1.createElement)(rootName, {
|
|
49
|
+
ref: this.onRefChange,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
tslib_1.__setFunctionName(_a, "ReactExtension"),
|
|
54
|
+
_a.contextTypes = {
|
|
55
|
+
piral: mount_1.anyPropType,
|
|
56
|
+
},
|
|
57
|
+
_a);
|
|
58
|
+
return ReactExtension;
|
|
59
|
+
}
|
|
60
|
+
exports.createExtension = createExtension;
|
|
61
|
+
//# sourceMappingURL=extension.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extension.js","sourceRoot":"","sources":["../src/extension.ts"],"names":[],"mappings":";;;;AACA,iCAAiD;AACjD,mCAAsC;AAEtC,SAAS,cAAc,CAAC,CAAM,EAAE,CAAM;IACpC,KAAK,MAAM,CAAC,IAAI,CAAC,EAAE;QACjB,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE;YACb,OAAO,KAAK,CAAC;SACd;KACF;IAED,KAAK,MAAM,CAAC,IAAI,CAAC,EAAE;QACjB,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE;YACxB,OAAO,KAAK,CAAC;SACd;KACF;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAS,OAAO,CAAI,CAAI,EAAE,CAAI;IAC5B,IAAI,CAAC,KAAK,CAAC,EAAE;QACX,MAAM,EAAE,GAAG,OAAO,CAAC,CAAC;QACpB,MAAM,EAAE,GAAG,OAAO,CAAC,CAAC;QAEpB,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,QAAQ,IAAI,CAAC,IAAI,CAAC,EAAE;YAC1C,OAAO,cAAc,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;SAC7B;QAED,OAAO,KAAK,CAAC;KACd;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED,SAAgB,eAAe,CAAC,QAAgB;;IAC9C,MAAM,cAAc,SAAQ,KAAM,SAAQ,iBAA6B;YAA3C;;gBAKlB,gBAAW,GAAG,CAAC,OAAoB,EAAE,EAAE;oBAC7C,IAAI,OAAO,EAAE;wBACX,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC,OAA8B,CAAC;wBACtD,OAAO,CAAC,SAAS,GAAG,EAAE,CAAC;wBACvB,KAAK,CAAC,mBAAmB,CAAC,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;qBAChD;gBACH,CAAC,CAAC;YAWJ,CAAC;YATC,qBAAqB,CAAC,SAA6B;gBACjD,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;YACzC,CAAC;YAED,MAAM;gBACJ,OAAO,IAAA,qBAAa,EAAC,QAAQ,EAAE;oBAC7B,GAAG,EAAE,IAAI,CAAC,WAAW;iBACtB,CAAC,CAAC;YACL,CAAC;SACF;;QArBQ,eAAY,GAAG;YACpB,KAAK,EAAE,mBAAW;SAClB;WAmBH,CAAC;IAEF,OAAO,cAAc,CAAC;AACxB,CAAC;AA1BD,0CA0BC"}
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mDAAyB;AACzB,kDAAwB"}
|
package/lib/mount.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { BaseComponentProps } from 'piral-core';
|
|
2
|
+
import { ComponentType } from 'react';
|
|
3
|
+
export declare const anyPropType: () => any;
|
|
4
|
+
export declare function mountReact<T extends BaseComponentProps>(el: HTMLElement, root: ComponentType<T>, props: T, ctx?: any): void;
|
|
5
|
+
export declare function unmountReact(el: HTMLElement): void;
|
package/lib/mount.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.unmountReact = exports.mountReact = exports.anyPropType = void 0;
|
|
4
|
+
const react_dom_1 = require("react-dom");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
// tslint:disable-next-line:no-null-keyword
|
|
7
|
+
const anyPropType = () => null;
|
|
8
|
+
exports.anyPropType = anyPropType;
|
|
9
|
+
function mountReact(el, root, props, ctx = {}) {
|
|
10
|
+
const contextTypes = {};
|
|
11
|
+
['piral', ...Object.keys(ctx)].forEach((key) => {
|
|
12
|
+
contextTypes[key] = exports.anyPropType;
|
|
13
|
+
});
|
|
14
|
+
class Provider extends react_1.Component {
|
|
15
|
+
getChildContext() {
|
|
16
|
+
return Object.assign({ piral: props.piral }, ctx);
|
|
17
|
+
}
|
|
18
|
+
render() {
|
|
19
|
+
return this.props.children;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
Provider.childContextTypes = contextTypes;
|
|
23
|
+
(0, react_dom_1.render)((0, react_1.createElement)(Provider, {}, (0, react_1.createElement)(root, props)), el);
|
|
24
|
+
}
|
|
25
|
+
exports.mountReact = mountReact;
|
|
26
|
+
function unmountReact(el) {
|
|
27
|
+
// tslint:disable-next-line:no-null-keyword
|
|
28
|
+
(0, react_dom_1.render)(null, el);
|
|
29
|
+
}
|
|
30
|
+
exports.unmountReact = unmountReact;
|
|
31
|
+
//# sourceMappingURL=mount.js.map
|
package/lib/mount.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mount.js","sourceRoot":"","sources":["../src/mount.ts"],"names":[],"mappings":";;;AACA,yCAAmC;AACnC,iCAAgE;AAEhE,2CAA2C;AACpC,MAAM,WAAW,GAAG,GAAG,EAAE,CAAC,IAAI,CAAC;AAAzB,QAAA,WAAW,eAAc;AAEtC,SAAgB,UAAU,CACxB,EAAe,EACf,IAAsB,EACtB,KAAQ,EACR,MAAW,EAAE;IAEb,MAAM,YAAY,GAAG,EAAE,CAAC;IAExB,CAAC,OAAO,EAAE,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;QAC7C,YAAY,CAAC,GAAG,CAAC,GAAG,mBAAW,CAAC;IAClC,CAAC,CAAC,CAAC;IAEH,MAAM,QAAS,SAAQ,iBAA6B;QAGlD,eAAe;YACb,uBACE,KAAK,EAAE,KAAK,CAAC,KAAK,IACf,GAAG,EACN;QACJ,CAAC;QAED,MAAM;YACJ,OAAO,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC;QAC7B,CAAC;;IAXM,0BAAiB,GAAG,YAAY,CAAC;IAc1C,IAAA,kBAAM,EAAC,IAAA,qBAAa,EAAC,QAAQ,EAAE,EAAE,EAAE,IAAA,qBAAa,EAAC,IAAW,EAAE,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AAC7E,CAAC;AA5BD,gCA4BC;AAED,SAAgB,YAAY,CAAC,EAAe;IAC1C,2CAA2C;IAC3C,IAAA,kBAAM,EAAC,IAAI,EAAE,EAAE,CAAC,CAAC;AACnB,CAAC;AAHD,oCAGC"}
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { ForeignComponent, ExtensionSlotProps } from 'piral-core';
|
|
2
|
+
import type { Component, ComponentType } from 'react';
|
|
3
|
+
declare module 'piral-core/lib/types/custom' {
|
|
4
|
+
interface PiletCustomApi extends PiletReactApi {
|
|
5
|
+
}
|
|
6
|
+
interface PiralCustomComponentConverters<TProps> {
|
|
7
|
+
react(component: ReactComponent<TProps>): ForeignComponent<TProps>;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export interface ReactComponent<TProps> {
|
|
11
|
+
/**
|
|
12
|
+
* The component root.
|
|
13
|
+
*/
|
|
14
|
+
root: ComponentType<TProps>;
|
|
15
|
+
/**
|
|
16
|
+
* The type of the React 16+ component.
|
|
17
|
+
*/
|
|
18
|
+
type: 'react';
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Defines the provided set of Pilet API extensions from the React 16+ plugin.
|
|
22
|
+
*/
|
|
23
|
+
export interface PiletReactApi {
|
|
24
|
+
/**
|
|
25
|
+
* Wraps an React 16+ component for use in Piral.
|
|
26
|
+
* @param component The component root.
|
|
27
|
+
* @returns The Piral React 16+ component.
|
|
28
|
+
*/
|
|
29
|
+
fromReact<TProps>(component: ComponentType<TProps>): ReactComponent<TProps>;
|
|
30
|
+
/**
|
|
31
|
+
* React 16+ component for displaying extensions of the given name.
|
|
32
|
+
*/
|
|
33
|
+
ReactExtension: Component<ExtensionSlotProps>;
|
|
34
|
+
}
|
package/lib/types.js
ADDED
package/lib/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "piral-react",
|
|
3
|
+
"version": "1.4.0-beta.6224",
|
|
4
|
+
"description": "Plugin for integrating React 16+ components in Piral.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"piral",
|
|
7
|
+
"pilet-api",
|
|
8
|
+
"smapiot",
|
|
9
|
+
"portal",
|
|
10
|
+
"modules",
|
|
11
|
+
"api",
|
|
12
|
+
"plugin",
|
|
13
|
+
"plugin-converter",
|
|
14
|
+
"react",
|
|
15
|
+
"react16",
|
|
16
|
+
"react17",
|
|
17
|
+
"react18"
|
|
18
|
+
],
|
|
19
|
+
"author": "smapiot",
|
|
20
|
+
"homepage": "https://piral.io",
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"module": "esm/index.js",
|
|
23
|
+
"main": "lib/index.js",
|
|
24
|
+
"typings": "lib/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"import": "./esm/index.js",
|
|
28
|
+
"require": "./lib/index.js"
|
|
29
|
+
},
|
|
30
|
+
"./convert": {
|
|
31
|
+
"import": "./convert.js"
|
|
32
|
+
},
|
|
33
|
+
"./esm/*": {
|
|
34
|
+
"import": "./esm/*"
|
|
35
|
+
},
|
|
36
|
+
"./lib/*": {
|
|
37
|
+
"require": "./lib/*"
|
|
38
|
+
},
|
|
39
|
+
"./_/*": {
|
|
40
|
+
"import": "./esm/*.js",
|
|
41
|
+
"require": "./lib/*.js"
|
|
42
|
+
},
|
|
43
|
+
"./package.json": "./package.json"
|
|
44
|
+
},
|
|
45
|
+
"sideEffects": false,
|
|
46
|
+
"files": [
|
|
47
|
+
"esm",
|
|
48
|
+
"lib",
|
|
49
|
+
"src",
|
|
50
|
+
"convert.d.ts",
|
|
51
|
+
"convert.js"
|
|
52
|
+
],
|
|
53
|
+
"repository": {
|
|
54
|
+
"type": "git",
|
|
55
|
+
"url": "git+https://github.com/smapiot/piral.git"
|
|
56
|
+
},
|
|
57
|
+
"bugs": {
|
|
58
|
+
"url": "https://github.com/smapiot/piral/issues"
|
|
59
|
+
},
|
|
60
|
+
"scripts": {
|
|
61
|
+
"cleanup": "rimraf esm lib convert.d.ts convert.js",
|
|
62
|
+
"build": "yarn build:commonjs && yarn build:esnext && yarn build:convert",
|
|
63
|
+
"build:convert": "tsc convert.ts --skipLibCheck --declaration --module esnext",
|
|
64
|
+
"build:commonjs": "tsc --project tsconfig.json --outDir lib --module commonjs",
|
|
65
|
+
"build:esnext": "tsc --project tsconfig.json --outDir esm --module esnext",
|
|
66
|
+
"typedoc": "typedoc --json ../../../docs/types/piral-react.json src --exclude \"src/**/*.test.*\"",
|
|
67
|
+
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
68
|
+
},
|
|
69
|
+
"devDependencies": {
|
|
70
|
+
"piral-core": "1.4.0-beta.6224"
|
|
71
|
+
},
|
|
72
|
+
"gitHead": "848d6e355c7d3ded789e5d54d853555822fd77ad"
|
|
73
|
+
}
|
package/src/converter.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ForeignComponent, BaseComponentProps } from 'piral-core';
|
|
2
|
+
import type { ComponentType } from 'react';
|
|
3
|
+
import { createExtension } from './extension';
|
|
4
|
+
import { mountReact, unmountReact } from './mount';
|
|
5
|
+
|
|
6
|
+
export interface ReactConverterOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Defines the name of the root element.
|
|
9
|
+
* @default piral-slot
|
|
10
|
+
*/
|
|
11
|
+
rootName?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function createConverter(config: ReactConverterOptions = {}) {
|
|
15
|
+
const { rootName = 'piral-slot' } = config;
|
|
16
|
+
const Extension = createExtension(rootName);
|
|
17
|
+
const convert = <TProps extends BaseComponentProps>(root: ComponentType<TProps>): ForeignComponent<TProps> => ({
|
|
18
|
+
mount(el, props, ctx) {
|
|
19
|
+
mountReact(el, root, props, ctx);
|
|
20
|
+
},
|
|
21
|
+
update(el, props, ctx) {
|
|
22
|
+
mountReact(el, root, props, ctx);
|
|
23
|
+
},
|
|
24
|
+
unmount(el) {
|
|
25
|
+
unmountReact(el);
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
convert.Extension = Extension;
|
|
29
|
+
return convert;
|
|
30
|
+
}
|
package/src/create.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { PiralPlugin } from 'piral-core';
|
|
2
|
+
import { createConverter, ReactConverterOptions } from './converter';
|
|
3
|
+
import type { PiletReactApi } from './types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Available configuration options for the React 16+ plugin.
|
|
7
|
+
*/
|
|
8
|
+
export interface ReactConfig extends ReactConverterOptions {}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Creates Pilet API extensions for integrating React 16+.
|
|
12
|
+
*/
|
|
13
|
+
export function createReactApi(config: ReactConfig = {}): PiralPlugin<PiletReactApi> {
|
|
14
|
+
return (context) => {
|
|
15
|
+
const convert = createConverter(config);
|
|
16
|
+
context.converters.react = ({ root }) => convert(root);
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
fromReact(root) {
|
|
20
|
+
return {
|
|
21
|
+
type: 'react',
|
|
22
|
+
root,
|
|
23
|
+
};
|
|
24
|
+
},
|
|
25
|
+
ReactExtension: convert.Extension,
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
}
|
package/src/extension.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { ExtensionSlotProps, PiletApi } from 'piral-core';
|
|
2
|
+
import { createElement, Component } from 'react';
|
|
3
|
+
import { anyPropType } from './mount';
|
|
4
|
+
|
|
5
|
+
function compareObjects(a: any, b: any) {
|
|
6
|
+
for (const i in a) {
|
|
7
|
+
if (!(i in b)) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
for (const i in b) {
|
|
13
|
+
if (!compare(a[i], b[i])) {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function compare<T>(a: T, b: T) {
|
|
22
|
+
if (a !== b) {
|
|
23
|
+
const ta = typeof a;
|
|
24
|
+
const tb = typeof b;
|
|
25
|
+
|
|
26
|
+
if (ta === tb && ta === 'object' && a && b) {
|
|
27
|
+
return compareObjects(a, b);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function createExtension(rootName: string) {
|
|
37
|
+
const ReactExtension: any = class extends Component<ExtensionSlotProps> {
|
|
38
|
+
static contextTypes = {
|
|
39
|
+
piral: anyPropType,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
private onRefChange = (element: HTMLElement) => {
|
|
43
|
+
if (element) {
|
|
44
|
+
const { piral } = this.context as { piral: PiletApi };
|
|
45
|
+
element.innerHTML = '';
|
|
46
|
+
piral.renderHtmlExtension(element, this.props);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
shouldComponentUpdate(nextProps: ExtensionSlotProps) {
|
|
51
|
+
return !compare(this.props, nextProps);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
render() {
|
|
55
|
+
return createElement(rootName, {
|
|
56
|
+
ref: this.onRefChange,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
return ReactExtension;
|
|
62
|
+
}
|
package/src/index.ts
ADDED
package/src/mount.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { BaseComponentProps } from 'piral-core';
|
|
2
|
+
import { render } from 'react-dom';
|
|
3
|
+
import { createElement, ComponentType, Component } from 'react';
|
|
4
|
+
|
|
5
|
+
// tslint:disable-next-line:no-null-keyword
|
|
6
|
+
export const anyPropType = () => null;
|
|
7
|
+
|
|
8
|
+
export function mountReact<T extends BaseComponentProps>(
|
|
9
|
+
el: HTMLElement,
|
|
10
|
+
root: ComponentType<T>,
|
|
11
|
+
props: T,
|
|
12
|
+
ctx: any = {},
|
|
13
|
+
) {
|
|
14
|
+
const contextTypes = {};
|
|
15
|
+
|
|
16
|
+
['piral', ...Object.keys(ctx)].forEach((key) => {
|
|
17
|
+
contextTypes[key] = anyPropType;
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
class Provider extends Component<{ children?: any }> {
|
|
21
|
+
static childContextTypes = contextTypes;
|
|
22
|
+
|
|
23
|
+
getChildContext() {
|
|
24
|
+
return {
|
|
25
|
+
piral: props.piral,
|
|
26
|
+
...ctx,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
render() {
|
|
31
|
+
return this.props.children;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
render(createElement(Provider, {}, createElement(root as any, props)), el);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function unmountReact(el: HTMLElement) {
|
|
39
|
+
// tslint:disable-next-line:no-null-keyword
|
|
40
|
+
render(null, el);
|
|
41
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { ForeignComponent, ExtensionSlotProps } from 'piral-core';
|
|
2
|
+
import type { Component, ComponentType } from 'react';
|
|
3
|
+
|
|
4
|
+
declare module 'piral-core/lib/types/custom' {
|
|
5
|
+
interface PiletCustomApi extends PiletReactApi {}
|
|
6
|
+
|
|
7
|
+
interface PiralCustomComponentConverters<TProps> {
|
|
8
|
+
react(component: ReactComponent<TProps>): ForeignComponent<TProps>;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ReactComponent<TProps> {
|
|
13
|
+
/**
|
|
14
|
+
* The component root.
|
|
15
|
+
*/
|
|
16
|
+
root: ComponentType<TProps>;
|
|
17
|
+
/**
|
|
18
|
+
* The type of the React 16+ component.
|
|
19
|
+
*/
|
|
20
|
+
type: 'react';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Defines the provided set of Pilet API extensions from the React 16+ plugin.
|
|
25
|
+
*/
|
|
26
|
+
export interface PiletReactApi {
|
|
27
|
+
/**
|
|
28
|
+
* Wraps an React 16+ component for use in Piral.
|
|
29
|
+
* @param component The component root.
|
|
30
|
+
* @returns The Piral React 16+ component.
|
|
31
|
+
*/
|
|
32
|
+
fromReact<TProps>(component: ComponentType<TProps>): ReactComponent<TProps>;
|
|
33
|
+
/**
|
|
34
|
+
* React 16+ component for displaying extensions of the given name.
|
|
35
|
+
*/
|
|
36
|
+
ReactExtension: Component<ExtensionSlotProps>;
|
|
37
|
+
}
|