piral-page-layouts 0.14.10-beta.3621
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 +115 -0
- package/esm/actions.d.ts +4 -0
- package/esm/actions.js +8 -0
- package/esm/actions.js.map +1 -0
- package/esm/create.d.ts +21 -0
- package/esm/create.js +62 -0
- package/esm/create.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/types.d.ts +67 -0
- package/esm/types.js +2 -0
- package/esm/types.js.map +1 -0
- package/lib/actions.d.ts +4 -0
- package/lib/actions.js +13 -0
- package/lib/actions.js.map +1 -0
- package/lib/create.d.ts +21 -0
- package/lib/create.js +66 -0
- package/lib/create.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/types.d.ts +67 -0
- package/lib/types.js +3 -0
- package/lib/types.js.map +1 -0
- package/package.json +52 -0
- package/src/actions.ts +22 -0
- package/src/create.ts +100 -0
- package/src/index.ts +2 -0
- package/src/types.ts +82 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 - 2022 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,115 @@
|
|
|
1
|
+
[](https://piral.io)
|
|
2
|
+
|
|
3
|
+
# [Piral Page Layouts](https://piral.io) · [](https://github.com/smapiot/piral/blob/main/LICENSE) [](https://www.npmjs.com/package/piral-page-layouts) [](https://jestjs.io) [](https://gitter.im/piral-io/community)
|
|
4
|
+
|
|
5
|
+
This is a plugin that only has a peer dependency to `piral-core`. What `piral-page-layouts` brings to the table is a set of Pilet API extensions that can be used with `piral` or `piral-core` for using different layouts on different pages.
|
|
6
|
+
|
|
7
|
+
By default, these API extensions are not integrated in `piral`, so you'd need to add them to your Piral instance.
|
|
8
|
+
|
|
9
|
+
## Why and When
|
|
10
|
+
|
|
11
|
+
tbd
|
|
12
|
+
|
|
13
|
+
## Documentation
|
|
14
|
+
|
|
15
|
+
The following functions are brought to the Pilet API.
|
|
16
|
+
|
|
17
|
+
### `registerPageLayout()`
|
|
18
|
+
|
|
19
|
+
Adds the definition of a page layout to the app shell.
|
|
20
|
+
|
|
21
|
+
### `unregisterPageLayout()`
|
|
22
|
+
|
|
23
|
+
Removes a page layout from the app shell. Pages using this layout will fall back to the default layout.
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
::: summary: For pilet authors
|
|
28
|
+
|
|
29
|
+
You can use the `registerPageLayout` function from the Pilet API to add a new page layout in the app shell.
|
|
30
|
+
|
|
31
|
+
Example use:
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { PiletApi } from '<name-of-piral-instance>';
|
|
35
|
+
import { FullscreenLayout } from './FullscreenLayout';
|
|
36
|
+
|
|
37
|
+
export function setup(piral: PiletApi) {
|
|
38
|
+
piral.registerPageLayout('fullscreen', FullscreenLayout);
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
You can use the `unregisterPageLayout` function from the Pilet API to remove a previously added page layout from the app shell.
|
|
43
|
+
|
|
44
|
+
Example use:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { PiletApi } from '<name-of-piral-instance>';
|
|
48
|
+
import { FullscreenLayout } from './FullscreenLayout';
|
|
49
|
+
|
|
50
|
+
export function setup(piral: PiletApi) {
|
|
51
|
+
// first register a page layout
|
|
52
|
+
piral.registerPageLayout('fullscreen', FullscreenLayout);
|
|
53
|
+
// and unregister; maybe some time later?
|
|
54
|
+
piral.unregisterPageLayout('fullscreen');
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**Note**: A page layout is accessible across all pilets, however, only the pilet that has registerd a page layout can also unregister it. Once a page layout has been taken no other pilet can override it - it first needs to be unregistered.
|
|
59
|
+
|
|
60
|
+
:::
|
|
61
|
+
|
|
62
|
+
::: summary: For Piral instance developers
|
|
63
|
+
|
|
64
|
+
The provided library only brings API extensions for pilets to a Piral instance.
|
|
65
|
+
|
|
66
|
+
For the setup of the library itself you'll need to import `createPageLayoutsApi` from the `piral-page-layouts` package.
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
import { createPageLayoutsApi } from 'piral-page-layouts';
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
The integration looks like:
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
const instance = createInstance({
|
|
76
|
+
// important part
|
|
77
|
+
plugins: [createPageLayoutsApi()],
|
|
78
|
+
// ...
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Via the options the initially available `layouts` can be defined.
|
|
83
|
+
|
|
84
|
+
Consider for example:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
const instance = createInstance({
|
|
88
|
+
// important part
|
|
89
|
+
plugins: [createPageLayoutsApi({
|
|
90
|
+
layouts: {
|
|
91
|
+
default: DefaultLayout,
|
|
92
|
+
empty: EmptyLayout,
|
|
93
|
+
}
|
|
94
|
+
})],
|
|
95
|
+
// ...
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Furthermore, the name of the default layout can be chosen with the `fallback` option, too. By default, the name is set to be `default`. If you want to change it to, e.g., `standard` you can do the following:
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
const instance = createInstance({
|
|
103
|
+
// important part
|
|
104
|
+
plugins: [createPageLayoutsApi({
|
|
105
|
+
fallback: 'standard',
|
|
106
|
+
})],
|
|
107
|
+
// ...
|
|
108
|
+
});
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
:::
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
Piral is released using the MIT license. For more information see the [license file](./LICENSE).
|
package/esm/actions.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { GlobalStateContext } from 'piral-core';
|
|
2
|
+
import type { PageLayoutRegistration } from './types';
|
|
3
|
+
export declare function registerPageLayout(ctx: GlobalStateContext, name: string, value: PageLayoutRegistration): void;
|
|
4
|
+
export declare function unregisterPageLayout(ctx: GlobalStateContext, name: string): void;
|
package/esm/actions.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { withKey, withoutKey } from 'piral-core';
|
|
2
|
+
export function registerPageLayout(ctx, name, value) {
|
|
3
|
+
ctx.dispatch((state) => (Object.assign(Object.assign({}, state), { registry: Object.assign(Object.assign({}, state.registry), { pageLayouts: withKey(state.registry.pageLayouts, name, value) }) })));
|
|
4
|
+
}
|
|
5
|
+
export function unregisterPageLayout(ctx, name) {
|
|
6
|
+
ctx.dispatch((state) => (Object.assign(Object.assign({}, state), { registry: Object.assign(Object.assign({}, state.registry), { pageLayouts: withoutKey(state.registry.pageLayouts, name) }) })));
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=actions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"actions.js","sourceRoot":"","sources":["../src/actions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,UAAU,EAAsB,MAAM,YAAY,CAAC;AAGrE,MAAM,UAAU,kBAAkB,CAAC,GAAuB,EAAE,IAAY,EAAE,KAA6B;IACrG,GAAG,CAAC,QAAQ,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,iCACnB,KAAK,KACR,QAAQ,kCACH,KAAK,CAAC,QAAQ,KACjB,WAAW,EAAE,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,WAAW,EAAE,IAAI,EAAE,KAAK,CAAC,OAE/D,CAAC,CAAC;AACN,CAAC;AAED,MAAM,UAAU,oBAAoB,CAAC,GAAuB,EAAE,IAAY;IACxE,GAAG,CAAC,QAAQ,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,iCACnB,KAAK,KACR,QAAQ,kCACH,KAAK,CAAC,QAAQ,KACjB,WAAW,EAAE,UAAU,CAAC,KAAK,CAAC,QAAQ,CAAC,WAAW,EAAE,IAAI,CAAC,OAE3D,CAAC,CAAC;AACN,CAAC"}
|
package/esm/create.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ComponentType } from 'react';
|
|
2
|
+
import { PiralPlugin, PageComponentProps } from 'piral-core';
|
|
3
|
+
import type { PiletPageLayoutsApi } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* Available configuration options for the page layout plugin.
|
|
6
|
+
*/
|
|
7
|
+
export interface PageLayoutsConfig {
|
|
8
|
+
/**
|
|
9
|
+
* Defines the name of the layout that should be used by default,
|
|
10
|
+
* i.e., in case no layout was specified or found.
|
|
11
|
+
*/
|
|
12
|
+
fallback?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Defines the initially available layouts that can be used.
|
|
15
|
+
*/
|
|
16
|
+
layouts?: Record<string, ComponentType<PageComponentProps>>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Creates new Pilet API extensions for dealing with page layouts.
|
|
20
|
+
*/
|
|
21
|
+
export declare function createPageLayoutsApi(config?: PageLayoutsConfig): PiralPlugin<PiletPageLayoutsApi>;
|
package/esm/create.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import * as actions from './actions';
|
|
2
|
+
import { createElement } from 'react';
|
|
3
|
+
import { withApi, defaultRender, useGlobalState } from 'piral-core';
|
|
4
|
+
function getPageLayouts(items) {
|
|
5
|
+
const layouts = {};
|
|
6
|
+
if (items && typeof items === 'object') {
|
|
7
|
+
Object.keys(items).forEach((name) => {
|
|
8
|
+
layouts[name] = {
|
|
9
|
+
pilet: undefined,
|
|
10
|
+
component: items[name],
|
|
11
|
+
};
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
return layouts;
|
|
15
|
+
}
|
|
16
|
+
const DefaultWrapper = (props) => defaultRender(props.children);
|
|
17
|
+
function createPageWrapper(Wrapper = DefaultWrapper, fallback = 'default') {
|
|
18
|
+
return (props) => {
|
|
19
|
+
var _a;
|
|
20
|
+
const layout = ((_a = props.meta) === null || _a === void 0 ? void 0 : _a.layout) || fallback;
|
|
21
|
+
const registration = useGlobalState((s) => s.registry.pageLayouts[layout] || s.registry.pageLayouts[fallback]);
|
|
22
|
+
const Layout = (registration === null || registration === void 0 ? void 0 : registration.component) || DefaultWrapper;
|
|
23
|
+
return createElement(Wrapper, props, createElement(Layout, props));
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function withPageLayouts(pageLayouts, fallback) {
|
|
27
|
+
return (state) => (Object.assign(Object.assign({}, state), { registry: Object.assign(Object.assign({}, state.registry), { wrappers: Object.assign(Object.assign({}, state.registry.wrappers), { page: createPageWrapper(state.registry.wrappers.page, fallback) }), pageLayouts }) }));
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Creates new Pilet API extensions for dealing with page layouts.
|
|
31
|
+
*/
|
|
32
|
+
export function createPageLayoutsApi(config = {}) {
|
|
33
|
+
const { layouts = {}, fallback } = config;
|
|
34
|
+
return (context) => {
|
|
35
|
+
const findPageLayout = (name) => context.readState((s) => s.registry.pageLayouts[name]);
|
|
36
|
+
context.defineActions(actions);
|
|
37
|
+
context.dispatch(withPageLayouts(getPageLayouts(layouts), fallback));
|
|
38
|
+
return (api) => {
|
|
39
|
+
const pilet = api.meta.name;
|
|
40
|
+
return {
|
|
41
|
+
registerPageLayout(name, pageLayout) {
|
|
42
|
+
const current = findPageLayout(name);
|
|
43
|
+
if (!current || current.pilet === pilet) {
|
|
44
|
+
const component = withApi(context, pageLayout, api, 'pageLayout');
|
|
45
|
+
context.registerPageLayout(name, {
|
|
46
|
+
component,
|
|
47
|
+
pilet,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
return () => api.unregisterPageLayout(name);
|
|
51
|
+
},
|
|
52
|
+
unregisterPageLayout(name) {
|
|
53
|
+
const current = findPageLayout(name);
|
|
54
|
+
if ((current === null || current === void 0 ? void 0 : current.pilet) === pilet) {
|
|
55
|
+
context.unregisterPageLayout(name);
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=create.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,WAAW,CAAC;AACrC,OAAO,EAAiB,aAAa,EAAE,MAAM,OAAO,CAAC;AACrD,OAAO,EAAsD,OAAO,EAAE,aAAa,EAAE,cAA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9C,CAAC;gBACD,oBAAoB,CAAC,IAAI;oBACvB,MAAM,OAAO,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;oBAErC,IAAI,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,KAAK,MAAK,KAAK,EAAE;wBAC5B,OAAO,CAAC,oBAAoB,CAAC,IAAI,CAAC,CAAC;qBACpC;gBACH,CAAC;aACF,CAAC;QACJ,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,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/types.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { RouteComponentProps } from 'react-router';
|
|
2
|
+
import type { AnyComponent, BaseRegistration, Dict, PageComponentProps, PiralPageMeta, RegistrationDisposer, WrappedComponent } from 'piral-core';
|
|
3
|
+
declare module 'piral-core/lib/types/custom' {
|
|
4
|
+
interface PiletCustomApi extends PiletPageLayoutsApi {
|
|
5
|
+
}
|
|
6
|
+
interface PiralCustomActions {
|
|
7
|
+
/**
|
|
8
|
+
* Registers a new page layout.
|
|
9
|
+
* @param name The name of the layout.
|
|
10
|
+
* @param value The page layout registration.
|
|
11
|
+
*/
|
|
12
|
+
registerPageLayout(name: string, value: PageLayoutRegistration): void;
|
|
13
|
+
/**
|
|
14
|
+
* Unregisters an existing page layout.
|
|
15
|
+
* @param name The name of the page layout to be removed.
|
|
16
|
+
*/
|
|
17
|
+
unregisterPageLayout(name: string): void;
|
|
18
|
+
}
|
|
19
|
+
interface PiralCustomPageMeta {
|
|
20
|
+
/**
|
|
21
|
+
* The layout to use for the page.
|
|
22
|
+
*/
|
|
23
|
+
layout?: string;
|
|
24
|
+
}
|
|
25
|
+
interface PiralCustomRegistryState {
|
|
26
|
+
/**
|
|
27
|
+
* The registered page layouts.
|
|
28
|
+
*/
|
|
29
|
+
pageLayouts: Dict<PageLayoutRegistration>;
|
|
30
|
+
}
|
|
31
|
+
interface PiralCustomErrors {
|
|
32
|
+
pageLayout: PageLayoutErrorInfoProps;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export interface PageLayoutRegistration extends BaseRegistration {
|
|
36
|
+
component: WrappedComponent<PageComponentProps>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The error used when a page layout crashed.
|
|
40
|
+
*/
|
|
41
|
+
export interface PageLayoutErrorInfoProps extends RouteComponentProps {
|
|
42
|
+
/**
|
|
43
|
+
* The type of the error.
|
|
44
|
+
*/
|
|
45
|
+
type: 'pageLayout';
|
|
46
|
+
/**
|
|
47
|
+
* The provided error details.
|
|
48
|
+
*/
|
|
49
|
+
error: any;
|
|
50
|
+
/**
|
|
51
|
+
* The available page meta data.
|
|
52
|
+
*/
|
|
53
|
+
meta: PiralPageMeta;
|
|
54
|
+
}
|
|
55
|
+
export interface PiletPageLayoutsApi {
|
|
56
|
+
/**
|
|
57
|
+
* Registers a page layout.
|
|
58
|
+
* @param name The name of the layout.
|
|
59
|
+
* @param layout The component responsible for the layout.
|
|
60
|
+
*/
|
|
61
|
+
registerPageLayout(name: string, layout: AnyComponent<PageComponentProps>): RegistrationDisposer;
|
|
62
|
+
/**
|
|
63
|
+
* Unregisters a page layout.
|
|
64
|
+
* @param name The name of the layout.
|
|
65
|
+
*/
|
|
66
|
+
unregisterPageLayout(name: string): void;
|
|
67
|
+
}
|
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":""}
|
package/lib/actions.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { GlobalStateContext } from 'piral-core';
|
|
2
|
+
import type { PageLayoutRegistration } from './types';
|
|
3
|
+
export declare function registerPageLayout(ctx: GlobalStateContext, name: string, value: PageLayoutRegistration): void;
|
|
4
|
+
export declare function unregisterPageLayout(ctx: GlobalStateContext, name: string): void;
|
package/lib/actions.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.unregisterPageLayout = exports.registerPageLayout = void 0;
|
|
4
|
+
const piral_core_1 = require("piral-core");
|
|
5
|
+
function registerPageLayout(ctx, name, value) {
|
|
6
|
+
ctx.dispatch((state) => (Object.assign(Object.assign({}, state), { registry: Object.assign(Object.assign({}, state.registry), { pageLayouts: (0, piral_core_1.withKey)(state.registry.pageLayouts, name, value) }) })));
|
|
7
|
+
}
|
|
8
|
+
exports.registerPageLayout = registerPageLayout;
|
|
9
|
+
function unregisterPageLayout(ctx, name) {
|
|
10
|
+
ctx.dispatch((state) => (Object.assign(Object.assign({}, state), { registry: Object.assign(Object.assign({}, state.registry), { pageLayouts: (0, piral_core_1.withoutKey)(state.registry.pageLayouts, name) }) })));
|
|
11
|
+
}
|
|
12
|
+
exports.unregisterPageLayout = unregisterPageLayout;
|
|
13
|
+
//# sourceMappingURL=actions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"actions.js","sourceRoot":"","sources":["../src/actions.ts"],"names":[],"mappings":";;;AAAA,2CAAqE;AAGrE,SAAgB,kBAAkB,CAAC,GAAuB,EAAE,IAAY,EAAE,KAA6B;IACrG,GAAG,CAAC,QAAQ,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,iCACnB,KAAK,KACR,QAAQ,kCACH,KAAK,CAAC,QAAQ,KACjB,WAAW,EAAE,IAAA,oBAAO,EAAC,KAAK,CAAC,QAAQ,CAAC,WAAW,EAAE,IAAI,EAAE,KAAK,CAAC,OAE/D,CAAC,CAAC;AACN,CAAC;AARD,gDAQC;AAED,SAAgB,oBAAoB,CAAC,GAAuB,EAAE,IAAY;IACxE,GAAG,CAAC,QAAQ,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,iCACnB,KAAK,KACR,QAAQ,kCACH,KAAK,CAAC,QAAQ,KACjB,WAAW,EAAE,IAAA,uBAAU,EAAC,KAAK,CAAC,QAAQ,CAAC,WAAW,EAAE,IAAI,CAAC,OAE3D,CAAC,CAAC;AACN,CAAC;AARD,oDAQC"}
|
package/lib/create.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ComponentType } from 'react';
|
|
2
|
+
import { PiralPlugin, PageComponentProps } from 'piral-core';
|
|
3
|
+
import type { PiletPageLayoutsApi } from './types';
|
|
4
|
+
/**
|
|
5
|
+
* Available configuration options for the page layout plugin.
|
|
6
|
+
*/
|
|
7
|
+
export interface PageLayoutsConfig {
|
|
8
|
+
/**
|
|
9
|
+
* Defines the name of the layout that should be used by default,
|
|
10
|
+
* i.e., in case no layout was specified or found.
|
|
11
|
+
*/
|
|
12
|
+
fallback?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Defines the initially available layouts that can be used.
|
|
15
|
+
*/
|
|
16
|
+
layouts?: Record<string, ComponentType<PageComponentProps>>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Creates new Pilet API extensions for dealing with page layouts.
|
|
20
|
+
*/
|
|
21
|
+
export declare function createPageLayoutsApi(config?: PageLayoutsConfig): PiralPlugin<PiletPageLayoutsApi>;
|
package/lib/create.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createPageLayoutsApi = void 0;
|
|
4
|
+
const actions = require("./actions");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const piral_core_1 = require("piral-core");
|
|
7
|
+
function getPageLayouts(items) {
|
|
8
|
+
const layouts = {};
|
|
9
|
+
if (items && typeof items === 'object') {
|
|
10
|
+
Object.keys(items).forEach((name) => {
|
|
11
|
+
layouts[name] = {
|
|
12
|
+
pilet: undefined,
|
|
13
|
+
component: items[name],
|
|
14
|
+
};
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return layouts;
|
|
18
|
+
}
|
|
19
|
+
const DefaultWrapper = (props) => (0, piral_core_1.defaultRender)(props.children);
|
|
20
|
+
function createPageWrapper(Wrapper = DefaultWrapper, fallback = 'default') {
|
|
21
|
+
return (props) => {
|
|
22
|
+
var _a;
|
|
23
|
+
const layout = ((_a = props.meta) === null || _a === void 0 ? void 0 : _a.layout) || fallback;
|
|
24
|
+
const registration = (0, piral_core_1.useGlobalState)((s) => s.registry.pageLayouts[layout] || s.registry.pageLayouts[fallback]);
|
|
25
|
+
const Layout = (registration === null || registration === void 0 ? void 0 : registration.component) || DefaultWrapper;
|
|
26
|
+
return (0, react_1.createElement)(Wrapper, props, (0, react_1.createElement)(Layout, props));
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function withPageLayouts(pageLayouts, fallback) {
|
|
30
|
+
return (state) => (Object.assign(Object.assign({}, state), { registry: Object.assign(Object.assign({}, state.registry), { wrappers: Object.assign(Object.assign({}, state.registry.wrappers), { page: createPageWrapper(state.registry.wrappers.page, fallback) }), pageLayouts }) }));
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Creates new Pilet API extensions for dealing with page layouts.
|
|
34
|
+
*/
|
|
35
|
+
function createPageLayoutsApi(config = {}) {
|
|
36
|
+
const { layouts = {}, fallback } = config;
|
|
37
|
+
return (context) => {
|
|
38
|
+
const findPageLayout = (name) => context.readState((s) => s.registry.pageLayouts[name]);
|
|
39
|
+
context.defineActions(actions);
|
|
40
|
+
context.dispatch(withPageLayouts(getPageLayouts(layouts), fallback));
|
|
41
|
+
return (api) => {
|
|
42
|
+
const pilet = api.meta.name;
|
|
43
|
+
return {
|
|
44
|
+
registerPageLayout(name, pageLayout) {
|
|
45
|
+
const current = findPageLayout(name);
|
|
46
|
+
if (!current || current.pilet === pilet) {
|
|
47
|
+
const component = (0, piral_core_1.withApi)(context, pageLayout, api, 'pageLayout');
|
|
48
|
+
context.registerPageLayout(name, {
|
|
49
|
+
component,
|
|
50
|
+
pilet,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return () => api.unregisterPageLayout(name);
|
|
54
|
+
},
|
|
55
|
+
unregisterPageLayout(name) {
|
|
56
|
+
const current = findPageLayout(name);
|
|
57
|
+
if ((current === null || current === void 0 ? void 0 : current.pilet) === pilet) {
|
|
58
|
+
context.unregisterPageLayout(name);
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
exports.createPageLayoutsApi = createPageLayoutsApi;
|
|
66
|
+
//# sourceMappingURL=create.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":";;;AAAA,qCAAqC;AACrC,iCAAqD;AACrD,2CAAwH;AAkBxH,SAAS,cAAc,CAAC,KAAwD;IAC9E,MAAM,OAAO,GAAiC,EAAE,CAAC;IAEjD,IAAI,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;QACtC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YAClC,OAAO,CAAC,IAAI,CAAC,GAAG;gBACd,KAAK,EAAE,SAAS;gBAChB,SAAS,EAAE,KAAK,CAAC,IAAI,CAAC;aACvB,CAAC;QACJ,CAAC,CAAC,CAAC;KACJ;IAED,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,MAAM,cAAc,GAAsC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAA,0BAAa,EAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;AAEnG,SAAS,iBAAiB,CAAC,OAAO,GAAG,cAAc,EAAE,QAAQ,GAAG,SAAS;IACvE,O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AO,CAAC,CAAC;QAE/B,OAAO,CAAC,QAAQ,CAAC,eAAe,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC;QAErE,OAAO,CAAC,GAAG,EAAE,EAAE;YACb,MAAM,KAAK,GAAG,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC;YAC5B,OAAO;gBACL,kBAAkB,CAAC,IAAI,EAAE,UAAU;oBACjC,MAAM,OAAO,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;oBAErC,IAAI,CAAC,OAAO,IAAI,OAAO,CAAC,KAAK,KAAK,KAAK,EAAE;wBACvC,MAAM,SAAS,GAAG,IAAA,oBAAO,EAAC,OAAO,EAAE,UAAU,EAAE,GAAG,EAAE,YAAY,CAAC,CAAC;wBAClE,OAAO,CAAC,kBAAkB,CAAC,IAAI,EAAE;4BAC/B,SAAS;4BACT,KAAK;yBACN,CAAC,CAAC;qBACJ;oBAED,OAAO,GAAG,EAAE,CAAC,GAAG,CAAC,oBAAoB,CAAC,IAAI,CAAC,CAAC;gBAC9C,CAAC;gBACD,oBAAoB,CAAC,IAAI;oBACvB,MAAM,OAAO,GAAG,cAAc,CAAC,IAAI,CAAC,CAAC;oBAErC,IAAI,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,KAAK,MAAK,KAAK,EAAE;wBAC5B,OAAO,CAAC,oBAAoB,CAAC,IAAI,CAAC,CAAC;qBACpC;gBACH,CAAC;aACF,CAAC;QACJ,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AApCD,oDAoCC"}
|
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,wDAAyB;AACzB,uDAAwB"}
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { RouteComponentProps } from 'react-router';
|
|
2
|
+
import type { AnyComponent, BaseRegistration, Dict, PageComponentProps, PiralPageMeta, RegistrationDisposer, WrappedComponent } from 'piral-core';
|
|
3
|
+
declare module 'piral-core/lib/types/custom' {
|
|
4
|
+
interface PiletCustomApi extends PiletPageLayoutsApi {
|
|
5
|
+
}
|
|
6
|
+
interface PiralCustomActions {
|
|
7
|
+
/**
|
|
8
|
+
* Registers a new page layout.
|
|
9
|
+
* @param name The name of the layout.
|
|
10
|
+
* @param value The page layout registration.
|
|
11
|
+
*/
|
|
12
|
+
registerPageLayout(name: string, value: PageLayoutRegistration): void;
|
|
13
|
+
/**
|
|
14
|
+
* Unregisters an existing page layout.
|
|
15
|
+
* @param name The name of the page layout to be removed.
|
|
16
|
+
*/
|
|
17
|
+
unregisterPageLayout(name: string): void;
|
|
18
|
+
}
|
|
19
|
+
interface PiralCustomPageMeta {
|
|
20
|
+
/**
|
|
21
|
+
* The layout to use for the page.
|
|
22
|
+
*/
|
|
23
|
+
layout?: string;
|
|
24
|
+
}
|
|
25
|
+
interface PiralCustomRegistryState {
|
|
26
|
+
/**
|
|
27
|
+
* The registered page layouts.
|
|
28
|
+
*/
|
|
29
|
+
pageLayouts: Dict<PageLayoutRegistration>;
|
|
30
|
+
}
|
|
31
|
+
interface PiralCustomErrors {
|
|
32
|
+
pageLayout: PageLayoutErrorInfoProps;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export interface PageLayoutRegistration extends BaseRegistration {
|
|
36
|
+
component: WrappedComponent<PageComponentProps>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The error used when a page layout crashed.
|
|
40
|
+
*/
|
|
41
|
+
export interface PageLayoutErrorInfoProps extends RouteComponentProps {
|
|
42
|
+
/**
|
|
43
|
+
* The type of the error.
|
|
44
|
+
*/
|
|
45
|
+
type: 'pageLayout';
|
|
46
|
+
/**
|
|
47
|
+
* The provided error details.
|
|
48
|
+
*/
|
|
49
|
+
error: any;
|
|
50
|
+
/**
|
|
51
|
+
* The available page meta data.
|
|
52
|
+
*/
|
|
53
|
+
meta: PiralPageMeta;
|
|
54
|
+
}
|
|
55
|
+
export interface PiletPageLayoutsApi {
|
|
56
|
+
/**
|
|
57
|
+
* Registers a page layout.
|
|
58
|
+
* @param name The name of the layout.
|
|
59
|
+
* @param layout The component responsible for the layout.
|
|
60
|
+
*/
|
|
61
|
+
registerPageLayout(name: string, layout: AnyComponent<PageComponentProps>): RegistrationDisposer;
|
|
62
|
+
/**
|
|
63
|
+
* Unregisters a page layout.
|
|
64
|
+
* @param name The name of the layout.
|
|
65
|
+
*/
|
|
66
|
+
unregisterPageLayout(name: string): void;
|
|
67
|
+
}
|
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,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "piral-page-layouts",
|
|
3
|
+
"version": "0.14.10-beta.3621",
|
|
4
|
+
"description": "Plugin for providing different page layouts in Piral.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"piral",
|
|
7
|
+
"pilet-api",
|
|
8
|
+
"smapiot",
|
|
9
|
+
"portal",
|
|
10
|
+
"modules",
|
|
11
|
+
"api",
|
|
12
|
+
"plugin",
|
|
13
|
+
"plugin-component",
|
|
14
|
+
"page",
|
|
15
|
+
"layouts"
|
|
16
|
+
],
|
|
17
|
+
"author": "smapiot",
|
|
18
|
+
"homepage": "https://piral.io",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"module": "esm/index.js",
|
|
21
|
+
"main": "lib/index.js",
|
|
22
|
+
"typings": "lib/index.d.ts",
|
|
23
|
+
"sideEffects": false,
|
|
24
|
+
"files": [
|
|
25
|
+
"esm",
|
|
26
|
+
"lib",
|
|
27
|
+
"src"
|
|
28
|
+
],
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/smapiot/piral.git"
|
|
32
|
+
},
|
|
33
|
+
"bugs": {
|
|
34
|
+
"url": "https://github.com/smapiot/piral/issues"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "yarn build:commonjs && yarn build:esnext",
|
|
38
|
+
"build:commonjs": "tsc --project tsconfig.json --outDir lib --module commonjs",
|
|
39
|
+
"build:esnext": "tsc --project tsconfig.json --outDir esm --module esnext",
|
|
40
|
+
"typedoc": "typedoc --json ../../../docs/types/piral-page-layouts.json src --exclude \"src/**/*.test.*\"",
|
|
41
|
+
"test": "echo \"Error: run tests from root\" && exit 1"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"piral-core": "0.14.10-beta.3621"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"piral-core": "0.14.x",
|
|
48
|
+
"react": ">=16.8.0",
|
|
49
|
+
"react-router": "5.x"
|
|
50
|
+
},
|
|
51
|
+
"gitHead": "161b6a945b0044bf356475aea053f436c0cfb89c"
|
|
52
|
+
}
|
package/src/actions.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { withKey, withoutKey, GlobalStateContext } from 'piral-core';
|
|
2
|
+
import type { PageLayoutRegistration } from './types';
|
|
3
|
+
|
|
4
|
+
export function registerPageLayout(ctx: GlobalStateContext, name: string, value: PageLayoutRegistration) {
|
|
5
|
+
ctx.dispatch((state) => ({
|
|
6
|
+
...state,
|
|
7
|
+
registry: {
|
|
8
|
+
...state.registry,
|
|
9
|
+
pageLayouts: withKey(state.registry.pageLayouts, name, value),
|
|
10
|
+
},
|
|
11
|
+
}));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function unregisterPageLayout(ctx: GlobalStateContext, name: string) {
|
|
15
|
+
ctx.dispatch((state) => ({
|
|
16
|
+
...state,
|
|
17
|
+
registry: {
|
|
18
|
+
...state.registry,
|
|
19
|
+
pageLayouts: withoutKey(state.registry.pageLayouts, name),
|
|
20
|
+
},
|
|
21
|
+
}));
|
|
22
|
+
}
|
package/src/create.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import * as actions from './actions';
|
|
2
|
+
import { ComponentType, createElement } from 'react';
|
|
3
|
+
import { PiralPlugin, PageComponentProps, Dict, GlobalState, withApi, defaultRender, useGlobalState } from 'piral-core';
|
|
4
|
+
import type { PiletPageLayoutsApi, PageLayoutRegistration } from './types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Available configuration options for the page layout plugin.
|
|
8
|
+
*/
|
|
9
|
+
export interface PageLayoutsConfig {
|
|
10
|
+
/**
|
|
11
|
+
* Defines the name of the layout that should be used by default,
|
|
12
|
+
* i.e., in case no layout was specified or found.
|
|
13
|
+
*/
|
|
14
|
+
fallback?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Defines the initially available layouts that can be used.
|
|
17
|
+
*/
|
|
18
|
+
layouts?: Record<string, ComponentType<PageComponentProps>>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function getPageLayouts(items: Record<string, ComponentType<PageComponentProps>>) {
|
|
22
|
+
const layouts: Dict<PageLayoutRegistration> = {};
|
|
23
|
+
|
|
24
|
+
if (items && typeof items === 'object') {
|
|
25
|
+
Object.keys(items).forEach((name) => {
|
|
26
|
+
layouts[name] = {
|
|
27
|
+
pilet: undefined,
|
|
28
|
+
component: items[name],
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return layouts;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const DefaultWrapper: ComponentType<PageComponentProps> = (props) => defaultRender(props.children);
|
|
37
|
+
|
|
38
|
+
function createPageWrapper(Wrapper = DefaultWrapper, fallback = 'default'): ComponentType<PageComponentProps> {
|
|
39
|
+
return (props) => {
|
|
40
|
+
const layout = props.meta?.layout || fallback;
|
|
41
|
+
const registration = useGlobalState((s) => s.registry.pageLayouts[layout] || s.registry.pageLayouts[fallback]);
|
|
42
|
+
const Layout = registration?.component || DefaultWrapper;
|
|
43
|
+
return createElement(Wrapper, props, createElement(Layout, props));
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function withPageLayouts(pageLayouts: Dict<PageLayoutRegistration>, fallback: string) {
|
|
48
|
+
return (state: GlobalState): GlobalState => ({
|
|
49
|
+
...state,
|
|
50
|
+
registry: {
|
|
51
|
+
...state.registry,
|
|
52
|
+
wrappers: {
|
|
53
|
+
...state.registry.wrappers,
|
|
54
|
+
page: createPageWrapper(state.registry.wrappers.page, fallback),
|
|
55
|
+
},
|
|
56
|
+
pageLayouts,
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Creates new Pilet API extensions for dealing with page layouts.
|
|
63
|
+
*/
|
|
64
|
+
export function createPageLayoutsApi(config: PageLayoutsConfig = {}): PiralPlugin<PiletPageLayoutsApi> {
|
|
65
|
+
const { layouts = {}, fallback } = config;
|
|
66
|
+
|
|
67
|
+
return (context) => {
|
|
68
|
+
const findPageLayout = (name: string) => context.readState((s) => s.registry.pageLayouts[name]);
|
|
69
|
+
|
|
70
|
+
context.defineActions(actions);
|
|
71
|
+
|
|
72
|
+
context.dispatch(withPageLayouts(getPageLayouts(layouts), fallback));
|
|
73
|
+
|
|
74
|
+
return (api) => {
|
|
75
|
+
const pilet = api.meta.name;
|
|
76
|
+
return {
|
|
77
|
+
registerPageLayout(name, pageLayout) {
|
|
78
|
+
const current = findPageLayout(name);
|
|
79
|
+
|
|
80
|
+
if (!current || current.pilet === pilet) {
|
|
81
|
+
const component = withApi(context, pageLayout, api, 'pageLayout');
|
|
82
|
+
context.registerPageLayout(name, {
|
|
83
|
+
component,
|
|
84
|
+
pilet,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return () => api.unregisterPageLayout(name);
|
|
89
|
+
},
|
|
90
|
+
unregisterPageLayout(name) {
|
|
91
|
+
const current = findPageLayout(name);
|
|
92
|
+
|
|
93
|
+
if (current?.pilet === pilet) {
|
|
94
|
+
context.unregisterPageLayout(name);
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
}
|
package/src/index.ts
ADDED
package/src/types.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { RouteComponentProps } from 'react-router';
|
|
2
|
+
import type {
|
|
3
|
+
AnyComponent,
|
|
4
|
+
BaseRegistration,
|
|
5
|
+
Dict,
|
|
6
|
+
PageComponentProps,
|
|
7
|
+
PiralPageMeta,
|
|
8
|
+
RegistrationDisposer,
|
|
9
|
+
WrappedComponent,
|
|
10
|
+
} from 'piral-core';
|
|
11
|
+
|
|
12
|
+
declare module 'piral-core/lib/types/custom' {
|
|
13
|
+
interface PiletCustomApi extends PiletPageLayoutsApi {}
|
|
14
|
+
|
|
15
|
+
interface PiralCustomActions {
|
|
16
|
+
/**
|
|
17
|
+
* Registers a new page layout.
|
|
18
|
+
* @param name The name of the layout.
|
|
19
|
+
* @param value The page layout registration.
|
|
20
|
+
*/
|
|
21
|
+
registerPageLayout(name: string, value: PageLayoutRegistration): void;
|
|
22
|
+
/**
|
|
23
|
+
* Unregisters an existing page layout.
|
|
24
|
+
* @param name The name of the page layout to be removed.
|
|
25
|
+
*/
|
|
26
|
+
unregisterPageLayout(name: string): void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface PiralCustomPageMeta {
|
|
30
|
+
/**
|
|
31
|
+
* The layout to use for the page.
|
|
32
|
+
*/
|
|
33
|
+
layout?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
interface PiralCustomRegistryState {
|
|
37
|
+
/**
|
|
38
|
+
* The registered page layouts.
|
|
39
|
+
*/
|
|
40
|
+
pageLayouts: Dict<PageLayoutRegistration>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface PiralCustomErrors {
|
|
44
|
+
pageLayout: PageLayoutErrorInfoProps;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface PageLayoutRegistration extends BaseRegistration {
|
|
49
|
+
component: WrappedComponent<PageComponentProps>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The error used when a page layout crashed.
|
|
54
|
+
*/
|
|
55
|
+
export interface PageLayoutErrorInfoProps extends RouteComponentProps {
|
|
56
|
+
/**
|
|
57
|
+
* The type of the error.
|
|
58
|
+
*/
|
|
59
|
+
type: 'pageLayout';
|
|
60
|
+
/**
|
|
61
|
+
* The provided error details.
|
|
62
|
+
*/
|
|
63
|
+
error: any;
|
|
64
|
+
/**
|
|
65
|
+
* The available page meta data.
|
|
66
|
+
*/
|
|
67
|
+
meta: PiralPageMeta;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface PiletPageLayoutsApi {
|
|
71
|
+
/**
|
|
72
|
+
* Registers a page layout.
|
|
73
|
+
* @param name The name of the layout.
|
|
74
|
+
* @param layout The component responsible for the layout.
|
|
75
|
+
*/
|
|
76
|
+
registerPageLayout(name: string, layout: AnyComponent<PageComponentProps>): RegistrationDisposer;
|
|
77
|
+
/**
|
|
78
|
+
* Unregisters a page layout.
|
|
79
|
+
* @param name The name of the layout.
|
|
80
|
+
*/
|
|
81
|
+
unregisterPageLayout(name: string): void;
|
|
82
|
+
}
|