@wordpress/plugins 5.8.0 → 6.1.0
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/CHANGELOG.md +8 -0
- package/README.md +9 -9
- package/build/api/index.js +12 -31
- package/build/api/index.js.map +1 -1
- package/build/components/plugin-area/index.js +4 -4
- package/build/components/plugin-area/index.js.map +1 -1
- package/build/components/plugin-context/index.js +3 -3
- package/build/components/plugin-context/index.js.map +1 -1
- package/build/components/plugin-error-boundary/index.js +7 -0
- package/build/components/plugin-error-boundary/index.js.map +1 -1
- package/build-module/api/index.js +12 -31
- package/build-module/api/index.js.map +1 -1
- package/build-module/components/plugin-area/index.js +4 -4
- package/build-module/components/plugin-area/index.js.map +1 -1
- package/build-module/components/plugin-context/index.js +7 -3
- package/build-module/components/plugin-context/index.js.map +1 -1
- package/build-module/components/plugin-error-boundary/index.js +7 -0
- package/build-module/components/plugin-error-boundary/index.js.map +1 -1
- package/build-types/api/index.d.ts +149 -0
- package/build-types/api/index.d.ts.map +1 -0
- package/build-types/components/index.d.ts +3 -0
- package/build-types/components/index.d.ts.map +1 -0
- package/build-types/components/plugin-area/index.d.ts +44 -0
- package/build-types/components/plugin-area/index.d.ts.map +1 -0
- package/build-types/components/plugin-context/index.d.ts +22 -0
- package/build-types/components/plugin-context/index.d.ts.map +1 -0
- package/build-types/components/plugin-error-boundary/index.d.ts +19 -0
- package/build-types/components/plugin-error-boundary/index.d.ts.map +1 -0
- package/build-types/index.d.ts +3 -0
- package/build-types/index.d.ts.map +1 -0
- package/package.json +9 -7
- package/src/api/{index.js → index.ts} +53 -33
- package/src/components/plugin-area/{index.js → index.tsx} +25 -7
- package/src/components/plugin-context/{index.js → index.tsx} +20 -5
- package/src/components/plugin-error-boundary/index.js +6 -0
- package/tsconfig.json +18 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import type { IconType } from '@wordpress/components';
|
|
2
|
+
import type { WPComponent } from '@wordpress/element';
|
|
3
|
+
/**
|
|
4
|
+
* Defined behavior of a plugin type.
|
|
5
|
+
*/
|
|
6
|
+
export interface WPPlugin {
|
|
7
|
+
/**
|
|
8
|
+
* A string identifying the plugin. Must be unique across all registered plugins.
|
|
9
|
+
*/
|
|
10
|
+
name: string;
|
|
11
|
+
/**
|
|
12
|
+
* An icon to be shown in the UI. It can be a slug of the Dashicon, or an
|
|
13
|
+
* element (or function returning an element) if you choose to render your
|
|
14
|
+
* own SVG.
|
|
15
|
+
*/
|
|
16
|
+
icon?: IconType;
|
|
17
|
+
/**
|
|
18
|
+
* A component containing the UI elements to be rendered.
|
|
19
|
+
*/
|
|
20
|
+
render: WPComponent;
|
|
21
|
+
/**
|
|
22
|
+
* The optional scope to be used when rendering inside a plugin area.
|
|
23
|
+
* No scope by default.
|
|
24
|
+
*/
|
|
25
|
+
scope?: string;
|
|
26
|
+
}
|
|
27
|
+
type PluginSettings = Omit<WPPlugin, 'name'>;
|
|
28
|
+
/**
|
|
29
|
+
* Registers a plugin to the editor.
|
|
30
|
+
*
|
|
31
|
+
* @param name A string identifying the plugin. Must be
|
|
32
|
+
* unique across all registered plugins.
|
|
33
|
+
* @param settings The settings for this plugin.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```js
|
|
37
|
+
* // Using ES5 syntax
|
|
38
|
+
* var el = wp.element.createElement;
|
|
39
|
+
* var Fragment = wp.element.Fragment;
|
|
40
|
+
* var PluginSidebar = wp.editPost.PluginSidebar;
|
|
41
|
+
* var PluginSidebarMoreMenuItem = wp.editPost.PluginSidebarMoreMenuItem;
|
|
42
|
+
* var registerPlugin = wp.plugins.registerPlugin;
|
|
43
|
+
* var moreIcon = wp.element.createElement( 'svg' ); //... svg element.
|
|
44
|
+
*
|
|
45
|
+
* function Component() {
|
|
46
|
+
* return el(
|
|
47
|
+
* Fragment,
|
|
48
|
+
* {},
|
|
49
|
+
* el(
|
|
50
|
+
* PluginSidebarMoreMenuItem,
|
|
51
|
+
* {
|
|
52
|
+
* target: 'sidebar-name',
|
|
53
|
+
* },
|
|
54
|
+
* 'My Sidebar'
|
|
55
|
+
* ),
|
|
56
|
+
* el(
|
|
57
|
+
* PluginSidebar,
|
|
58
|
+
* {
|
|
59
|
+
* name: 'sidebar-name',
|
|
60
|
+
* title: 'My Sidebar',
|
|
61
|
+
* },
|
|
62
|
+
* 'Content of the sidebar'
|
|
63
|
+
* )
|
|
64
|
+
* );
|
|
65
|
+
* }
|
|
66
|
+
* registerPlugin( 'plugin-name', {
|
|
67
|
+
* icon: moreIcon,
|
|
68
|
+
* render: Component,
|
|
69
|
+
* scope: 'my-page',
|
|
70
|
+
* } );
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```js
|
|
75
|
+
* // Using ESNext syntax
|
|
76
|
+
* import { PluginSidebar, PluginSidebarMoreMenuItem } from '@wordpress/edit-post';
|
|
77
|
+
* import { registerPlugin } from '@wordpress/plugins';
|
|
78
|
+
* import { more } from '@wordpress/icons';
|
|
79
|
+
*
|
|
80
|
+
* const Component = () => (
|
|
81
|
+
* <>
|
|
82
|
+
* <PluginSidebarMoreMenuItem
|
|
83
|
+
* target="sidebar-name"
|
|
84
|
+
* >
|
|
85
|
+
* My Sidebar
|
|
86
|
+
* </PluginSidebarMoreMenuItem>
|
|
87
|
+
* <PluginSidebar
|
|
88
|
+
* name="sidebar-name"
|
|
89
|
+
* title="My Sidebar"
|
|
90
|
+
* >
|
|
91
|
+
* Content of the sidebar
|
|
92
|
+
* </PluginSidebar>
|
|
93
|
+
* </>
|
|
94
|
+
* );
|
|
95
|
+
*
|
|
96
|
+
* registerPlugin( 'plugin-name', {
|
|
97
|
+
* icon: more,
|
|
98
|
+
* render: Component,
|
|
99
|
+
* scope: 'my-page',
|
|
100
|
+
* } );
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* @return The final plugin settings object.
|
|
104
|
+
*/
|
|
105
|
+
export declare function registerPlugin(name: string, settings: PluginSettings): PluginSettings | null;
|
|
106
|
+
/**
|
|
107
|
+
* Unregisters a plugin by name.
|
|
108
|
+
*
|
|
109
|
+
* @param name Plugin name.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```js
|
|
113
|
+
* // Using ES5 syntax
|
|
114
|
+
* var unregisterPlugin = wp.plugins.unregisterPlugin;
|
|
115
|
+
*
|
|
116
|
+
* unregisterPlugin( 'plugin-name' );
|
|
117
|
+
* ```
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```js
|
|
121
|
+
* // Using ESNext syntax
|
|
122
|
+
* import { unregisterPlugin } from '@wordpress/plugins';
|
|
123
|
+
*
|
|
124
|
+
* unregisterPlugin( 'plugin-name' );
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* @return The previous plugin settings object, if it has been
|
|
128
|
+
* successfully unregistered; otherwise `undefined`.
|
|
129
|
+
*/
|
|
130
|
+
export declare function unregisterPlugin(name: string): WPPlugin | undefined;
|
|
131
|
+
/**
|
|
132
|
+
* Returns a registered plugin settings.
|
|
133
|
+
*
|
|
134
|
+
* @param name Plugin name.
|
|
135
|
+
*
|
|
136
|
+
* @return Plugin setting.
|
|
137
|
+
*/
|
|
138
|
+
export declare function getPlugin(name: string): WPPlugin | undefined;
|
|
139
|
+
/**
|
|
140
|
+
* Returns all registered plugins without a scope or for a given scope.
|
|
141
|
+
*
|
|
142
|
+
* @param scope The scope to be used when rendering inside
|
|
143
|
+
* a plugin area. No scope by default.
|
|
144
|
+
*
|
|
145
|
+
* @return The list of plugins without a scope or for a given scope.
|
|
146
|
+
*/
|
|
147
|
+
export declare function getPlugins(scope?: string): WPPlugin[];
|
|
148
|
+
export {};
|
|
149
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/api/index.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEtD;;GAEG;AACH,MAAM,WAAW,QAAQ;IACxB;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;;;OAIG;IACH,IAAI,CAAC,EAAE,QAAQ,CAAC;IAEhB;;OAEG;IACH,MAAM,EAAE,WAAW,CAAC;IAEpB;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;CACf;AAED,KAAK,cAAc,GAAG,IAAI,CAAE,QAAQ,EAAE,MAAM,CAAE,CAAC;AAO/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4EG;AACH,wBAAgB,cAAc,CAC7B,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,cAAc,GACtB,cAAc,GAAG,IAAI,CAyDvB;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,gBAAgB,CAAE,IAAI,EAAE,MAAM,GAAI,QAAQ,GAAG,SAAS,CAWrE;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAE,IAAI,EAAE,MAAM,GAAI,QAAQ,GAAG,SAAS,CAE9D;AAED;;;;;;;GAOG;AACH,wBAAgB,UAAU,CAAE,KAAK,CAAC,EAAE,MAAM,GAAI,QAAQ,EAAE,CAIvD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/components/index.js"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { WPPlugin } from '../../api';
|
|
2
|
+
/**
|
|
3
|
+
* A component that renders all plugin fills in a hidden div.
|
|
4
|
+
*
|
|
5
|
+
* @param props
|
|
6
|
+
* @param props.scope
|
|
7
|
+
* @param props.onError
|
|
8
|
+
* @example
|
|
9
|
+
* ```js
|
|
10
|
+
* // Using ES5 syntax
|
|
11
|
+
* var el = wp.element.createElement;
|
|
12
|
+
* var PluginArea = wp.plugins.PluginArea;
|
|
13
|
+
*
|
|
14
|
+
* function Layout() {
|
|
15
|
+
* return el(
|
|
16
|
+
* 'div',
|
|
17
|
+
* { scope: 'my-page' },
|
|
18
|
+
* 'Content of the page',
|
|
19
|
+
* PluginArea
|
|
20
|
+
* );
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```js
|
|
26
|
+
* // Using ESNext syntax
|
|
27
|
+
* import { PluginArea } from '@wordpress/plugins';
|
|
28
|
+
*
|
|
29
|
+
* const Layout = () => (
|
|
30
|
+
* <div>
|
|
31
|
+
* Content of the page
|
|
32
|
+
* <PluginArea scope="my-page" />
|
|
33
|
+
* </div>
|
|
34
|
+
* );
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @return {WPComponent} The component to be rendered.
|
|
38
|
+
*/
|
|
39
|
+
declare function PluginArea({ scope, onError, }: {
|
|
40
|
+
scope?: string;
|
|
41
|
+
onError?: (name: WPPlugin['name'], error: Error) => void;
|
|
42
|
+
}): JSX.Element;
|
|
43
|
+
export default PluginArea;
|
|
44
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/plugin-area/index.tsx"],"names":[],"mappings":"AAmBA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAS1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,iBAAS,UAAU,CAAE,EACpB,KAAK,EACL,OAAO,GACP,EAAE;IACF,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,CAAE,IAAI,EAAE,QAAQ,CAAE,MAAM,CAAE,EAAE,KAAK,EAAE,KAAK,KAAM,IAAI,CAAC;CAC7D,eA4DA;AAED,eAAe,UAAU,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { WPPlugin } from '../../api';
|
|
5
|
+
export interface PluginContext {
|
|
6
|
+
name: null | WPPlugin['name'];
|
|
7
|
+
icon: null | WPPlugin['icon'];
|
|
8
|
+
}
|
|
9
|
+
declare const Provider: import("react").Provider<PluginContext>;
|
|
10
|
+
export { Provider as PluginContextProvider };
|
|
11
|
+
/**
|
|
12
|
+
* A Higher Order Component used to inject Plugin context to the
|
|
13
|
+
* wrapped component.
|
|
14
|
+
*
|
|
15
|
+
* @param mapContextToProps Function called on every context change,
|
|
16
|
+
* expected to return object of props to
|
|
17
|
+
* merge with the component's own props.
|
|
18
|
+
*
|
|
19
|
+
* @return {WPComponent} Enhanced component with injected context as props.
|
|
20
|
+
*/
|
|
21
|
+
export declare const withPluginContext: (mapContextToProps: <T>(context: PluginContext, props: T) => T & PluginContext) => (Inner: import("react").ComponentType<any>) => (props: any) => JSX.Element;
|
|
22
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/plugin-context/index.tsx"],"names":[],"mappings":"AAMA;;GAEG;AACH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AAE1C,MAAM,WAAW,aAAa;IAC7B,IAAI,EAAE,IAAI,GAAG,QAAQ,CAAE,MAAM,CAAE,CAAC;IAChC,IAAI,EAAE,IAAI,GAAG,QAAQ,CAAE,MAAM,CAAE,CAAC;CAChC;AAED,QAAA,MAAkB,QAAQ,yCAGvB,CAAC;AAEJ,OAAO,EAAE,QAAQ,IAAI,qBAAqB,EAAE,CAAC;AAE7C;;;;;;;;;GASG;AACH,eAAO,MAAM,iBAAiB,mCAEnB,aAAa,+GAeC,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export class PluginErrorBoundary extends Component<any, any, any> {
|
|
2
|
+
static getDerivedStateFromError(): {
|
|
3
|
+
hasError: boolean;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* @param {Object} props
|
|
7
|
+
*/
|
|
8
|
+
constructor(props: Object);
|
|
9
|
+
state: {
|
|
10
|
+
hasError: boolean;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* @param {Error} error Error object passed by React.
|
|
14
|
+
*/
|
|
15
|
+
componentDidCatch(error: Error): void;
|
|
16
|
+
render(): any;
|
|
17
|
+
}
|
|
18
|
+
import { Component } from "@wordpress/element/build-types/react";
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/plugin-error-boundary/index.js"],"names":[],"mappings":"AAKA;IAWC;;MAEC;IAZD;;OAEG;IACH,mBAFW,MAAM,EAOhB;IAHA;;MAEC;IAOF;;OAEG;IACH,yBAFW,KAAK,QAOf;IAED,cAMC;CACD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.js"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/plugins",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.1.0",
|
|
4
4
|
"description": "Plugins module for WordPress.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -24,13 +24,15 @@
|
|
|
24
24
|
"main": "build/index.js",
|
|
25
25
|
"module": "build-module/index.js",
|
|
26
26
|
"react-native": "src/index",
|
|
27
|
+
"types": "build-types",
|
|
27
28
|
"dependencies": {
|
|
28
29
|
"@babel/runtime": "^7.16.0",
|
|
29
|
-
"@wordpress/
|
|
30
|
-
"@wordpress/
|
|
31
|
-
"@wordpress/
|
|
32
|
-
"@wordpress/
|
|
33
|
-
"@wordpress/
|
|
30
|
+
"@wordpress/components": "^24.0.0",
|
|
31
|
+
"@wordpress/compose": "^6.10.0",
|
|
32
|
+
"@wordpress/element": "^5.10.0",
|
|
33
|
+
"@wordpress/hooks": "^3.33.0",
|
|
34
|
+
"@wordpress/icons": "^9.24.0",
|
|
35
|
+
"@wordpress/is-shallow-equal": "^4.33.0",
|
|
34
36
|
"memize": "^1.1.0"
|
|
35
37
|
},
|
|
36
38
|
"peerDependencies": {
|
|
@@ -39,5 +41,5 @@
|
|
|
39
41
|
"publishConfig": {
|
|
40
42
|
"access": "public"
|
|
41
43
|
},
|
|
42
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "e936127e1e13881f1a940b7bd1593a9e500147f3"
|
|
43
45
|
}
|
|
@@ -5,37 +5,50 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { applyFilters, doAction } from '@wordpress/hooks';
|
|
7
7
|
import { plugins as pluginsIcon } from '@wordpress/icons';
|
|
8
|
+
import type { IconType } from '@wordpress/components';
|
|
9
|
+
import type { WPComponent } from '@wordpress/element';
|
|
8
10
|
|
|
9
11
|
/**
|
|
10
12
|
* Defined behavior of a plugin type.
|
|
11
|
-
*
|
|
12
|
-
* @typedef {Object} WPPlugin
|
|
13
|
-
*
|
|
14
|
-
* @property {string} name A string identifying the plugin. Must be
|
|
15
|
-
* unique across all registered plugins.
|
|
16
|
-
* @property {string|WPElement|Function} [icon] An icon to be shown in the UI. It can
|
|
17
|
-
* be a slug of the Dashicon, or an element
|
|
18
|
-
* (or function returning an element) if you
|
|
19
|
-
* choose to render your own SVG.
|
|
20
|
-
* @property {Function} render A component containing the UI elements
|
|
21
|
-
* to be rendered.
|
|
22
|
-
* @property {string} [scope] The optional scope to be used when rendering inside
|
|
23
|
-
* a plugin area. No scope by default.
|
|
24
13
|
*/
|
|
14
|
+
export interface WPPlugin {
|
|
15
|
+
/**
|
|
16
|
+
* A string identifying the plugin. Must be unique across all registered plugins.
|
|
17
|
+
*/
|
|
18
|
+
name: string;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* An icon to be shown in the UI. It can be a slug of the Dashicon, or an
|
|
22
|
+
* element (or function returning an element) if you choose to render your
|
|
23
|
+
* own SVG.
|
|
24
|
+
*/
|
|
25
|
+
icon?: IconType;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A component containing the UI elements to be rendered.
|
|
29
|
+
*/
|
|
30
|
+
render: WPComponent;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The optional scope to be used when rendering inside a plugin area.
|
|
34
|
+
* No scope by default.
|
|
35
|
+
*/
|
|
36
|
+
scope?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
type PluginSettings = Omit< WPPlugin, 'name' >;
|
|
25
40
|
|
|
26
41
|
/**
|
|
27
42
|
* Plugin definitions keyed by plugin name.
|
|
28
|
-
*
|
|
29
|
-
* @type {Object.<string,WPPlugin>}
|
|
30
43
|
*/
|
|
31
|
-
const plugins = {}
|
|
44
|
+
const plugins = {} as Record< string, WPPlugin >;
|
|
32
45
|
|
|
33
46
|
/**
|
|
34
47
|
* Registers a plugin to the editor.
|
|
35
48
|
*
|
|
36
|
-
* @param
|
|
37
|
-
*
|
|
38
|
-
* @param
|
|
49
|
+
* @param name A string identifying the plugin. Must be
|
|
50
|
+
* unique across all registered plugins.
|
|
51
|
+
* @param settings The settings for this plugin.
|
|
39
52
|
*
|
|
40
53
|
* @example
|
|
41
54
|
* ```js
|
|
@@ -105,9 +118,12 @@ const plugins = {};
|
|
|
105
118
|
* } );
|
|
106
119
|
* ```
|
|
107
120
|
*
|
|
108
|
-
* @return
|
|
121
|
+
* @return The final plugin settings object.
|
|
109
122
|
*/
|
|
110
|
-
export function registerPlugin(
|
|
123
|
+
export function registerPlugin(
|
|
124
|
+
name: string,
|
|
125
|
+
settings: PluginSettings
|
|
126
|
+
): PluginSettings | null {
|
|
111
127
|
if ( typeof settings !== 'object' ) {
|
|
112
128
|
console.error( 'No settings object provided!' );
|
|
113
129
|
return null;
|
|
@@ -126,7 +142,11 @@ export function registerPlugin( name, settings ) {
|
|
|
126
142
|
console.error( `Plugin "${ name }" is already registered.` );
|
|
127
143
|
}
|
|
128
144
|
|
|
129
|
-
settings = applyFilters(
|
|
145
|
+
settings = applyFilters(
|
|
146
|
+
'plugins.registerPlugin',
|
|
147
|
+
settings,
|
|
148
|
+
name
|
|
149
|
+
) as PluginSettings;
|
|
130
150
|
|
|
131
151
|
const { render, scope } = settings;
|
|
132
152
|
|
|
@@ -165,7 +185,7 @@ export function registerPlugin( name, settings ) {
|
|
|
165
185
|
/**
|
|
166
186
|
* Unregisters a plugin by name.
|
|
167
187
|
*
|
|
168
|
-
* @param
|
|
188
|
+
* @param name Plugin name.
|
|
169
189
|
*
|
|
170
190
|
* @example
|
|
171
191
|
* ```js
|
|
@@ -183,10 +203,10 @@ export function registerPlugin( name, settings ) {
|
|
|
183
203
|
* unregisterPlugin( 'plugin-name' );
|
|
184
204
|
* ```
|
|
185
205
|
*
|
|
186
|
-
* @return
|
|
187
|
-
*
|
|
206
|
+
* @return The previous plugin settings object, if it has been
|
|
207
|
+
* successfully unregistered; otherwise `undefined`.
|
|
188
208
|
*/
|
|
189
|
-
export function unregisterPlugin( name ) {
|
|
209
|
+
export function unregisterPlugin( name: string ): WPPlugin | undefined {
|
|
190
210
|
if ( ! plugins[ name ] ) {
|
|
191
211
|
console.error( 'Plugin "' + name + '" is not registered.' );
|
|
192
212
|
return;
|
|
@@ -202,23 +222,23 @@ export function unregisterPlugin( name ) {
|
|
|
202
222
|
/**
|
|
203
223
|
* Returns a registered plugin settings.
|
|
204
224
|
*
|
|
205
|
-
* @param
|
|
225
|
+
* @param name Plugin name.
|
|
206
226
|
*
|
|
207
|
-
* @return
|
|
227
|
+
* @return Plugin setting.
|
|
208
228
|
*/
|
|
209
|
-
export function getPlugin( name ) {
|
|
229
|
+
export function getPlugin( name: string ): WPPlugin | undefined {
|
|
210
230
|
return plugins[ name ];
|
|
211
231
|
}
|
|
212
232
|
|
|
213
233
|
/**
|
|
214
234
|
* Returns all registered plugins without a scope or for a given scope.
|
|
215
235
|
*
|
|
216
|
-
* @param
|
|
217
|
-
*
|
|
236
|
+
* @param scope The scope to be used when rendering inside
|
|
237
|
+
* a plugin area. No scope by default.
|
|
218
238
|
*
|
|
219
|
-
* @return
|
|
239
|
+
* @return The list of plugins without a scope or for a given scope.
|
|
220
240
|
*/
|
|
221
|
-
export function getPlugins( scope ) {
|
|
241
|
+
export function getPlugins( scope?: string ): WPPlugin[] {
|
|
222
242
|
return Object.values( plugins ).filter(
|
|
223
243
|
( plugin ) => plugin.scope === scope
|
|
224
244
|
);
|
|
@@ -16,15 +16,22 @@ import isShallowEqual from '@wordpress/is-shallow-equal';
|
|
|
16
16
|
import { PluginContextProvider } from '../plugin-context';
|
|
17
17
|
import { PluginErrorBoundary } from '../plugin-error-boundary';
|
|
18
18
|
import { getPlugins } from '../../api';
|
|
19
|
+
import type { PluginContext } from '../plugin-context';
|
|
20
|
+
import type { WPPlugin } from '../../api';
|
|
19
21
|
|
|
20
|
-
const getPluginContext = memoize(
|
|
22
|
+
const getPluginContext = memoize(
|
|
23
|
+
( icon: PluginContext[ 'icon' ], name: PluginContext[ 'name' ] ) => ( {
|
|
24
|
+
icon,
|
|
25
|
+
name,
|
|
26
|
+
} )
|
|
27
|
+
);
|
|
21
28
|
|
|
22
29
|
/**
|
|
23
30
|
* A component that renders all plugin fills in a hidden div.
|
|
24
31
|
*
|
|
25
|
-
* @param
|
|
26
|
-
* @param
|
|
27
|
-
* @param
|
|
32
|
+
* @param props
|
|
33
|
+
* @param props.scope
|
|
34
|
+
* @param props.onError
|
|
28
35
|
* @example
|
|
29
36
|
* ```js
|
|
30
37
|
* // Using ES5 syntax
|
|
@@ -56,12 +63,23 @@ const getPluginContext = memoize( ( icon, name ) => ( { icon, name } ) );
|
|
|
56
63
|
*
|
|
57
64
|
* @return {WPComponent} The component to be rendered.
|
|
58
65
|
*/
|
|
59
|
-
function PluginArea( {
|
|
66
|
+
function PluginArea( {
|
|
67
|
+
scope,
|
|
68
|
+
onError,
|
|
69
|
+
}: {
|
|
70
|
+
scope?: string;
|
|
71
|
+
onError?: ( name: WPPlugin[ 'name' ], error: Error ) => void;
|
|
72
|
+
} ) {
|
|
60
73
|
const store = useMemo( () => {
|
|
61
|
-
let lastValue;
|
|
74
|
+
let lastValue: WPPlugin[] = [];
|
|
62
75
|
|
|
63
76
|
return {
|
|
64
|
-
subscribe(
|
|
77
|
+
subscribe(
|
|
78
|
+
listener: (
|
|
79
|
+
plugin: Omit< WPPlugin, 'name' >,
|
|
80
|
+
name: WPPlugin[ 'name' ]
|
|
81
|
+
) => void
|
|
82
|
+
) {
|
|
65
83
|
addAction(
|
|
66
84
|
'plugins.pluginRegistered',
|
|
67
85
|
'core/plugins/plugin-area/plugins-registered',
|
|
@@ -4,7 +4,17 @@
|
|
|
4
4
|
import { createContext } from '@wordpress/element';
|
|
5
5
|
import { createHigherOrderComponent } from '@wordpress/compose';
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Internal dependencies
|
|
9
|
+
*/
|
|
10
|
+
import type { WPPlugin } from '../../api';
|
|
11
|
+
|
|
12
|
+
export interface PluginContext {
|
|
13
|
+
name: null | WPPlugin[ 'name' ];
|
|
14
|
+
icon: null | WPPlugin[ 'icon' ];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const { Consumer, Provider } = createContext< PluginContext >( {
|
|
8
18
|
name: null,
|
|
9
19
|
icon: null,
|
|
10
20
|
} );
|
|
@@ -15,13 +25,18 @@ export { Provider as PluginContextProvider };
|
|
|
15
25
|
* A Higher Order Component used to inject Plugin context to the
|
|
16
26
|
* wrapped component.
|
|
17
27
|
*
|
|
18
|
-
* @param
|
|
19
|
-
*
|
|
20
|
-
*
|
|
28
|
+
* @param mapContextToProps Function called on every context change,
|
|
29
|
+
* expected to return object of props to
|
|
30
|
+
* merge with the component's own props.
|
|
21
31
|
*
|
|
22
32
|
* @return {WPComponent} Enhanced component with injected context as props.
|
|
23
33
|
*/
|
|
24
|
-
export const withPluginContext = (
|
|
34
|
+
export const withPluginContext = (
|
|
35
|
+
mapContextToProps: < T >(
|
|
36
|
+
context: PluginContext,
|
|
37
|
+
props: T
|
|
38
|
+
) => T & PluginContext
|
|
39
|
+
) =>
|
|
25
40
|
createHigherOrderComponent( ( OriginalComponent ) => {
|
|
26
41
|
return ( props ) => (
|
|
27
42
|
<Consumer>
|
|
@@ -4,6 +4,9 @@
|
|
|
4
4
|
import { Component } from '@wordpress/element';
|
|
5
5
|
|
|
6
6
|
export class PluginErrorBoundary extends Component {
|
|
7
|
+
/**
|
|
8
|
+
* @param {Object} props
|
|
9
|
+
*/
|
|
7
10
|
constructor( props ) {
|
|
8
11
|
super( props );
|
|
9
12
|
this.state = {
|
|
@@ -15,6 +18,9 @@ export class PluginErrorBoundary extends Component {
|
|
|
15
18
|
return { hasError: true };
|
|
16
19
|
}
|
|
17
20
|
|
|
21
|
+
/**
|
|
22
|
+
* @param {Error} error Error object passed by React.
|
|
23
|
+
*/
|
|
18
24
|
componentDidCatch( error ) {
|
|
19
25
|
const { name, onError } = this.props;
|
|
20
26
|
if ( onError ) {
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"rootDir": "src",
|
|
5
|
+
"declarationDir": "build-types",
|
|
6
|
+
"types": [ "gutenberg-env" ],
|
|
7
|
+
"skipLibCheck": true
|
|
8
|
+
},
|
|
9
|
+
"references": [
|
|
10
|
+
{ "path": "../components" },
|
|
11
|
+
{ "path": "../compose" },
|
|
12
|
+
{ "path": "../element" },
|
|
13
|
+
{ "path": "../hooks" },
|
|
14
|
+
{ "path": "../icons" },
|
|
15
|
+
{ "path": "../is-shallow-equal" }
|
|
16
|
+
],
|
|
17
|
+
"include": [ "src/**/*" ]
|
|
18
|
+
}
|