@wordpress/lazy-editor 1.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 +9 -0
- package/LICENSE.md +788 -0
- package/README.md +65 -0
- package/build/component.js +100 -0
- package/build/component.js.map +7 -0
- package/build/hooks/use-editor-assets.js +84 -0
- package/build/hooks/use-editor-assets.js.map +7 -0
- package/build/hooks/use-editor-settings.js +68 -0
- package/build/hooks/use-editor-settings.js.map +7 -0
- package/build/hooks/use-global-styles.js +81 -0
- package/build/hooks/use-global-styles.js.map +7 -0
- package/build/hooks/use-styles-id.js +50 -0
- package/build/hooks/use-styles-id.js.map +7 -0
- package/build/index.js +31 -0
- package/build/index.js.map +7 -0
- package/build/lock-unlock.js +35 -0
- package/build/lock-unlock.js.map +7 -0
- package/build-module/component.js +75 -0
- package/build-module/component.js.map +7 -0
- package/build-module/hooks/use-editor-assets.js +48 -0
- package/build-module/hooks/use-editor-assets.js.map +7 -0
- package/build-module/hooks/use-editor-settings.js +43 -0
- package/build-module/hooks/use-editor-settings.js.map +7 -0
- package/build-module/hooks/use-global-styles.js +56 -0
- package/build-module/hooks/use-global-styles.js.map +7 -0
- package/build-module/hooks/use-styles-id.js +25 -0
- package/build-module/hooks/use-styles-id.js.map +7 -0
- package/build-module/index.js +6 -0
- package/build-module/index.js.map +7 -0
- package/build-module/lock-unlock.js +10 -0
- package/build-module/lock-unlock.js.map +7 -0
- package/build-types/component.d.ts +23 -0
- package/build-types/component.d.ts.map +1 -0
- package/build-types/hooks/use-editor-assets.d.ts +11 -0
- package/build-types/hooks/use-editor-assets.d.ts.map +1 -0
- package/build-types/hooks/use-editor-settings.d.ts +14 -0
- package/build-types/hooks/use-editor-settings.d.ts.map +1 -0
- package/build-types/hooks/use-global-styles.d.ts +10 -0
- package/build-types/hooks/use-global-styles.d.ts.map +1 -0
- package/build-types/hooks/use-styles-id.d.ts +13 -0
- package/build-types/hooks/use-styles-id.d.ts.map +1 -0
- package/build-types/index.d.ts +5 -0
- package/build-types/index.d.ts.map +1 -0
- package/build-types/lock-unlock.d.ts +2 -0
- package/build-types/lock-unlock.d.ts.map +1 -0
- package/package.json +55 -0
- package/src/component.tsx +110 -0
- package/src/hooks/use-editor-assets.tsx +65 -0
- package/src/hooks/use-editor-settings.tsx +56 -0
- package/src/hooks/use-global-styles.tsx +75 -0
- package/src/hooks/use-styles-id.tsx +45 -0
- package/src/index.tsx +4 -0
- package/src/lock-unlock.ts +9 -0
- package/tsconfig.json +18 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// packages/lazy-editor/src/component.tsx
|
|
2
|
+
import { privateApis as editorPrivateApis } from "@wordpress/editor";
|
|
3
|
+
import { store as coreDataStore } from "@wordpress/core-data";
|
|
4
|
+
import { useSelect } from "@wordpress/data";
|
|
5
|
+
import { Spinner } from "@wordpress/components";
|
|
6
|
+
import { useMemo } from "@wordpress/element";
|
|
7
|
+
import { useStylesId } from "./hooks/use-styles-id";
|
|
8
|
+
import { useEditorSettings } from "./hooks/use-editor-settings";
|
|
9
|
+
import { useEditorAssets } from "./hooks/use-editor-assets";
|
|
10
|
+
import { unlock } from "./lock-unlock";
|
|
11
|
+
import { jsx } from "react/jsx-runtime";
|
|
12
|
+
var { Editor: PrivateEditor, BackButton } = unlock(editorPrivateApis);
|
|
13
|
+
function Editor({
|
|
14
|
+
postType,
|
|
15
|
+
postId,
|
|
16
|
+
settings,
|
|
17
|
+
backButton
|
|
18
|
+
}) {
|
|
19
|
+
const templateId = useSelect(
|
|
20
|
+
(select) => {
|
|
21
|
+
if (!postType || !postId) {
|
|
22
|
+
return void 0;
|
|
23
|
+
}
|
|
24
|
+
if (postType === "wp_template") {
|
|
25
|
+
return postId;
|
|
26
|
+
}
|
|
27
|
+
return unlock(select(coreDataStore)).getTemplateId(
|
|
28
|
+
postType,
|
|
29
|
+
postId
|
|
30
|
+
);
|
|
31
|
+
},
|
|
32
|
+
[postType, postId]
|
|
33
|
+
);
|
|
34
|
+
const stylesId = useStylesId({ templateId });
|
|
35
|
+
const { isReady: settingsReady, editorSettings } = useEditorSettings({
|
|
36
|
+
stylesId
|
|
37
|
+
});
|
|
38
|
+
const { isReady: assetsReady } = useEditorAssets();
|
|
39
|
+
const finalSettings = useMemo(
|
|
40
|
+
() => ({
|
|
41
|
+
...editorSettings,
|
|
42
|
+
...settings
|
|
43
|
+
}),
|
|
44
|
+
[editorSettings, settings]
|
|
45
|
+
);
|
|
46
|
+
if (!settingsReady || !assetsReady) {
|
|
47
|
+
return /* @__PURE__ */ jsx(
|
|
48
|
+
"div",
|
|
49
|
+
{
|
|
50
|
+
style: {
|
|
51
|
+
display: "flex",
|
|
52
|
+
justifyContent: "center",
|
|
53
|
+
alignItems: "center",
|
|
54
|
+
height: "100vh"
|
|
55
|
+
},
|
|
56
|
+
children: /* @__PURE__ */ jsx(Spinner, {})
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
return /* @__PURE__ */ jsx(
|
|
61
|
+
PrivateEditor,
|
|
62
|
+
{
|
|
63
|
+
postType,
|
|
64
|
+
postId,
|
|
65
|
+
templateId,
|
|
66
|
+
settings: finalSettings,
|
|
67
|
+
styles: finalSettings.styles,
|
|
68
|
+
children: backButton && /* @__PURE__ */ jsx(BackButton, { children: backButton })
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
export {
|
|
73
|
+
Editor
|
|
74
|
+
};
|
|
75
|
+
//# sourceMappingURL=component.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/component.tsx"],
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport type { ReactNode } from 'react';\n\n/**\n * WordPress dependencies\n */\nimport { privateApis as editorPrivateApis } from '@wordpress/editor';\nimport { store as coreDataStore } from '@wordpress/core-data';\nimport { useSelect } from '@wordpress/data';\nimport { Spinner } from '@wordpress/components';\nimport { useMemo } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { useStylesId } from './hooks/use-styles-id';\nimport { useEditorSettings } from './hooks/use-editor-settings';\nimport { useEditorAssets } from './hooks/use-editor-assets';\nimport { unlock } from './lock-unlock';\n\nconst { Editor: PrivateEditor, BackButton } = unlock( editorPrivateApis );\n\ninterface EditorProps {\n\tpostType: string;\n\tpostId: string;\n\tsettings?: Record< string, any >;\n\tbackButton?: ReactNode;\n}\n\n/**\n * Lazy-loading editor component that handles asset loading and settings initialization.\n *\n * @param {Object} props Component props\n * @param {string} props.postType The post type to edit\n * @param {string} props.postId The post ID to edit\n * @param {Object} props.settings Optional extra settings to merge with editor settings\n * @param {ReactNode} props.backButton Optional back button to render in editor header\n * @return The editor component with loading states\n */\nexport function Editor( {\n\tpostType,\n\tpostId,\n\tsettings,\n\tbackButton,\n}: EditorProps ) {\n\t// Resolve template ID from post type/ID\n\tconst templateId = useSelect(\n\t\t( select ) => {\n\t\t\tif ( ! postType || ! postId ) {\n\t\t\t\treturn undefined;\n\t\t\t}\n\t\t\tif ( postType === 'wp_template' ) {\n\t\t\t\treturn postId;\n\t\t\t}\n\t\t\t// Use private API to get template ID for this post\n\t\t\treturn unlock( select( coreDataStore ) ).getTemplateId(\n\t\t\t\tpostType,\n\t\t\t\tpostId\n\t\t\t);\n\t\t},\n\t\t[ postType, postId ]\n\t);\n\n\t// Resolve styles ID from template\n\tconst stylesId = useStylesId( { templateId } );\n\n\t// Load editor settings and assets\n\tconst { isReady: settingsReady, editorSettings } = useEditorSettings( {\n\t\tstylesId,\n\t} );\n\tconst { isReady: assetsReady } = useEditorAssets();\n\tconst finalSettings = useMemo(\n\t\t() => ( {\n\t\t\t...editorSettings,\n\t\t\t...settings,\n\t\t} ),\n\t\t[ editorSettings, settings ]\n\t);\n\n\t// Show loading spinner while assets or settings are loading\n\tif ( ! settingsReady || ! assetsReady ) {\n\t\treturn (\n\t\t\t<div\n\t\t\t\tstyle={ {\n\t\t\t\t\tdisplay: 'flex',\n\t\t\t\t\tjustifyContent: 'center',\n\t\t\t\t\talignItems: 'center',\n\t\t\t\t\theight: '100vh',\n\t\t\t\t} }\n\t\t\t>\n\t\t\t\t<Spinner />\n\t\t\t</div>\n\t\t);\n\t}\n\n\t// Render the editor when ready\n\treturn (\n\t\t<PrivateEditor\n\t\t\tpostType={ postType }\n\t\t\tpostId={ postId }\n\t\t\ttemplateId={ templateId }\n\t\t\tsettings={ finalSettings }\n\t\t\tstyles={ finalSettings.styles }\n\t\t>\n\t\t\t{ backButton && <BackButton>{ backButton }</BackButton> }\n\t\t</PrivateEditor>\n\t);\n}\n"],
|
|
5
|
+
"mappings": ";AAQA,SAAS,eAAe,yBAAyB;AACjD,SAAS,SAAS,qBAAqB;AACvC,SAAS,iBAAiB;AAC1B,SAAS,eAAe;AACxB,SAAS,eAAe;AAKxB,SAAS,mBAAmB;AAC5B,SAAS,yBAAyB;AAClC,SAAS,uBAAuB;AAChC,SAAS,cAAc;AAwEnB;AAtEJ,IAAM,EAAE,QAAQ,eAAe,WAAW,IAAI,OAAQ,iBAAkB;AAmBjE,SAAS,OAAQ;AAAA,EACvB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,GAAiB;AAEhB,QAAM,aAAa;AAAA,IAClB,CAAE,WAAY;AACb,UAAK,CAAE,YAAY,CAAE,QAAS;AAC7B,eAAO;AAAA,MACR;AACA,UAAK,aAAa,eAAgB;AACjC,eAAO;AAAA,MACR;AAEA,aAAO,OAAQ,OAAQ,aAAc,CAAE,EAAE;AAAA,QACxC;AAAA,QACA;AAAA,MACD;AAAA,IACD;AAAA,IACA,CAAE,UAAU,MAAO;AAAA,EACpB;AAGA,QAAM,WAAW,YAAa,EAAE,WAAW,CAAE;AAG7C,QAAM,EAAE,SAAS,eAAe,eAAe,IAAI,kBAAmB;AAAA,IACrE;AAAA,EACD,CAAE;AACF,QAAM,EAAE,SAAS,YAAY,IAAI,gBAAgB;AACjD,QAAM,gBAAgB;AAAA,IACrB,OAAQ;AAAA,MACP,GAAG;AAAA,MACH,GAAG;AAAA,IACJ;AAAA,IACA,CAAE,gBAAgB,QAAS;AAAA,EAC5B;AAGA,MAAK,CAAE,iBAAiB,CAAE,aAAc;AACvC,WACC;AAAA,MAAC;AAAA;AAAA,QACA,OAAQ;AAAA,UACP,SAAS;AAAA,UACT,gBAAgB;AAAA,UAChB,YAAY;AAAA,UACZ,QAAQ;AAAA,QACT;AAAA,QAEA,8BAAC,WAAQ;AAAA;AAAA,IACV;AAAA,EAEF;AAGA,SACC;AAAA,IAAC;AAAA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,UAAW;AAAA,MACX,QAAS,cAAc;AAAA,MAErB,wBAAc,oBAAC,cAAa,sBAAY;AAAA;AAAA,EAC3C;AAEF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// packages/lazy-editor/src/hooks/use-editor-assets.tsx
|
|
2
|
+
import loadAssets from "@wordpress/asset-loader";
|
|
3
|
+
import { store as coreDataStore } from "@wordpress/core-data";
|
|
4
|
+
import { useState, useEffect } from "@wordpress/element";
|
|
5
|
+
import { resolveSelect, useSelect } from "@wordpress/data";
|
|
6
|
+
import { unlock } from "../lock-unlock";
|
|
7
|
+
var loadAssetsPromise;
|
|
8
|
+
async function loadEditorAssets() {
|
|
9
|
+
const load = async () => {
|
|
10
|
+
const editorAssets = await unlock(
|
|
11
|
+
resolveSelect(coreDataStore)
|
|
12
|
+
).getEditorAssets();
|
|
13
|
+
await loadAssets(
|
|
14
|
+
editorAssets.scripts || {},
|
|
15
|
+
editorAssets.inline_scripts || { before: {}, after: {} },
|
|
16
|
+
editorAssets.styles || {},
|
|
17
|
+
editorAssets.inline_styles || { before: {}, after: {} }
|
|
18
|
+
);
|
|
19
|
+
};
|
|
20
|
+
if (!loadAssetsPromise) {
|
|
21
|
+
loadAssetsPromise = load();
|
|
22
|
+
}
|
|
23
|
+
return loadAssetsPromise;
|
|
24
|
+
}
|
|
25
|
+
function useEditorAssets() {
|
|
26
|
+
const editorAssets = useSelect((select) => {
|
|
27
|
+
return unlock(select(coreDataStore)).getEditorAssets();
|
|
28
|
+
}, []);
|
|
29
|
+
const [assetsLoaded, setAssetsLoaded] = useState(false);
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
if (editorAssets && !assetsLoaded) {
|
|
32
|
+
loadEditorAssets().then(() => {
|
|
33
|
+
setAssetsLoaded(true);
|
|
34
|
+
}).catch((error) => {
|
|
35
|
+
console.error("Failed to load editor assets:", error);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}, [editorAssets, assetsLoaded]);
|
|
39
|
+
return {
|
|
40
|
+
isReady: !!editorAssets && assetsLoaded,
|
|
41
|
+
assetsLoaded
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
loadEditorAssets,
|
|
46
|
+
useEditorAssets
|
|
47
|
+
};
|
|
48
|
+
//# sourceMappingURL=use-editor-assets.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/hooks/use-editor-assets.tsx"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport loadAssets from '@wordpress/asset-loader';\nimport { store as coreDataStore } from '@wordpress/core-data';\nimport { useState, useEffect } from '@wordpress/element';\nimport { resolveSelect, useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { unlock } from '../lock-unlock';\n\nlet loadAssetsPromise: Promise< void >;\n\nexport async function loadEditorAssets() {\n\tconst load = async () => {\n\t\tconst editorAssets = await unlock(\n\t\t\tresolveSelect( coreDataStore )\n\t\t).getEditorAssets();\n\t\tawait loadAssets(\n\t\t\teditorAssets.scripts || {},\n\t\t\teditorAssets.inline_scripts || { before: {}, after: {} },\n\t\t\teditorAssets.styles || {},\n\t\t\teditorAssets.inline_styles || { before: {}, after: {} }\n\t\t);\n\t};\n\n\tif ( ! loadAssetsPromise ) {\n\t\tloadAssetsPromise = load();\n\t}\n\n\treturn loadAssetsPromise;\n}\n\n/**\n * This is a React hook that handles loading editor assets from the REST API.\n *\n * @return Editor assets loading state.\n */\nexport function useEditorAssets() {\n\tconst editorAssets = useSelect( ( select ) => {\n\t\treturn unlock( select( coreDataStore ) ).getEditorAssets();\n\t}, [] );\n\n\tconst [ assetsLoaded, setAssetsLoaded ] = useState( false );\n\n\tuseEffect( () => {\n\t\tif ( editorAssets && ! assetsLoaded ) {\n\t\t\tloadEditorAssets()\n\t\t\t\t.then( () => {\n\t\t\t\t\tsetAssetsLoaded( true );\n\t\t\t\t} )\n\t\t\t\t.catch( ( error: Error ) => {\n\t\t\t\t\t// eslint-disable-next-line no-console\n\t\t\t\t\tconsole.error( 'Failed to load editor assets:', error );\n\t\t\t\t} );\n\t\t}\n\t}, [ editorAssets, assetsLoaded ] );\n\n\treturn {\n\t\tisReady: !! editorAssets && assetsLoaded,\n\t\tassetsLoaded,\n\t};\n}\n"],
|
|
5
|
+
"mappings": ";AAGA,OAAO,gBAAgB;AACvB,SAAS,SAAS,qBAAqB;AACvC,SAAS,UAAU,iBAAiB;AACpC,SAAS,eAAe,iBAAiB;AAKzC,SAAS,cAAc;AAEvB,IAAI;AAEJ,eAAsB,mBAAmB;AACxC,QAAM,OAAO,YAAY;AACxB,UAAM,eAAe,MAAM;AAAA,MAC1B,cAAe,aAAc;AAAA,IAC9B,EAAE,gBAAgB;AAClB,UAAM;AAAA,MACL,aAAa,WAAW,CAAC;AAAA,MACzB,aAAa,kBAAkB,EAAE,QAAQ,CAAC,GAAG,OAAO,CAAC,EAAE;AAAA,MACvD,aAAa,UAAU,CAAC;AAAA,MACxB,aAAa,iBAAiB,EAAE,QAAQ,CAAC,GAAG,OAAO,CAAC,EAAE;AAAA,IACvD;AAAA,EACD;AAEA,MAAK,CAAE,mBAAoB;AAC1B,wBAAoB,KAAK;AAAA,EAC1B;AAEA,SAAO;AACR;AAOO,SAAS,kBAAkB;AACjC,QAAM,eAAe,UAAW,CAAE,WAAY;AAC7C,WAAO,OAAQ,OAAQ,aAAc,CAAE,EAAE,gBAAgB;AAAA,EAC1D,GAAG,CAAC,CAAE;AAEN,QAAM,CAAE,cAAc,eAAgB,IAAI,SAAU,KAAM;AAE1D,YAAW,MAAM;AAChB,QAAK,gBAAgB,CAAE,cAAe;AACrC,uBAAiB,EACf,KAAM,MAAM;AACZ,wBAAiB,IAAK;AAAA,MACvB,CAAE,EACD,MAAO,CAAE,UAAkB;AAE3B,gBAAQ,MAAO,iCAAiC,KAAM;AAAA,MACvD,CAAE;AAAA,IACJ;AAAA,EACD,GAAG,CAAE,cAAc,YAAa,CAAE;AAElC,SAAO;AAAA,IACN,SAAS,CAAC,CAAE,gBAAgB;AAAA,IAC5B;AAAA,EACD;AACD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// packages/lazy-editor/src/hooks/use-editor-settings.tsx
|
|
2
|
+
import { generateGlobalStyles } from "@wordpress/global-styles-engine";
|
|
3
|
+
import { store as coreDataStore } from "@wordpress/core-data";
|
|
4
|
+
import { useMemo } from "@wordpress/element";
|
|
5
|
+
import { useSelect } from "@wordpress/data";
|
|
6
|
+
import { useUserGlobalStyles } from "./use-global-styles";
|
|
7
|
+
import { unlock } from "../lock-unlock";
|
|
8
|
+
function useEditorSettings({ stylesId }) {
|
|
9
|
+
const { editorSettings } = useSelect(
|
|
10
|
+
(select) => ({
|
|
11
|
+
editorSettings: unlock(
|
|
12
|
+
select(coreDataStore)
|
|
13
|
+
).getEditorSettings()
|
|
14
|
+
}),
|
|
15
|
+
[]
|
|
16
|
+
);
|
|
17
|
+
const { user: globalStyles } = useUserGlobalStyles(stylesId);
|
|
18
|
+
const [globalStylesCSS] = generateGlobalStyles(globalStyles);
|
|
19
|
+
const hasEditorSettings = !!editorSettings;
|
|
20
|
+
const styles = useMemo(() => {
|
|
21
|
+
if (!hasEditorSettings) {
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
return [
|
|
25
|
+
...editorSettings?.styles ?? [],
|
|
26
|
+
...globalStylesCSS
|
|
27
|
+
];
|
|
28
|
+
}, [hasEditorSettings, editorSettings?.styles, globalStylesCSS]);
|
|
29
|
+
return {
|
|
30
|
+
isReady: hasEditorSettings,
|
|
31
|
+
editorSettings: useMemo(
|
|
32
|
+
() => ({
|
|
33
|
+
...editorSettings ?? {},
|
|
34
|
+
styles
|
|
35
|
+
}),
|
|
36
|
+
[editorSettings, styles]
|
|
37
|
+
)
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export {
|
|
41
|
+
useEditorSettings
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=use-editor-settings.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/hooks/use-editor-settings.tsx"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { generateGlobalStyles } from '@wordpress/global-styles-engine';\nimport { store as coreDataStore } from '@wordpress/core-data';\nimport { useMemo } from '@wordpress/element';\nimport { useSelect } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport { useUserGlobalStyles } from './use-global-styles';\nimport { unlock } from '../lock-unlock';\n\n/**\n * This is a React hook that provides the editor settings from the REST API.\n *\n * @param {Object} props - The props object.\n * @param {string} [props.stylesId] - The ID of the user's global styles to use.\n * @return Editor settings.\n */\nexport function useEditorSettings( { stylesId }: { stylesId: string } ) {\n\tconst { editorSettings } = useSelect(\n\t\t( select ) => ( {\n\t\t\teditorSettings: unlock(\n\t\t\t\tselect( coreDataStore )\n\t\t\t).getEditorSettings(),\n\t\t} ),\n\t\t[]\n\t);\n\n\tconst { user: globalStyles } = useUserGlobalStyles( stylesId );\n\tconst [ globalStylesCSS ] = generateGlobalStyles( globalStyles );\n\n\tconst hasEditorSettings = !! editorSettings;\n\tconst styles = useMemo( () => {\n\t\tif ( ! hasEditorSettings ) {\n\t\t\treturn [];\n\t\t}\n\t\treturn [\n\t\t\t...( ( editorSettings?.styles as Array< any > ) ?? [] ),\n\t\t\t...globalStylesCSS,\n\t\t];\n\t}, [ hasEditorSettings, editorSettings?.styles, globalStylesCSS ] );\n\n\treturn {\n\t\tisReady: hasEditorSettings,\n\t\teditorSettings: useMemo(\n\t\t\t() => ( {\n\t\t\t\t...( editorSettings ?? {} ),\n\t\t\t\tstyles,\n\t\t\t} ),\n\t\t\t[ editorSettings, styles ]\n\t\t),\n\t};\n}\n"],
|
|
5
|
+
"mappings": ";AAGA,SAAS,4BAA4B;AACrC,SAAS,SAAS,qBAAqB;AACvC,SAAS,eAAe;AACxB,SAAS,iBAAiB;AAK1B,SAAS,2BAA2B;AACpC,SAAS,cAAc;AAShB,SAAS,kBAAmB,EAAE,SAAS,GAA0B;AACvE,QAAM,EAAE,eAAe,IAAI;AAAA,IAC1B,CAAE,YAAc;AAAA,MACf,gBAAgB;AAAA,QACf,OAAQ,aAAc;AAAA,MACvB,EAAE,kBAAkB;AAAA,IACrB;AAAA,IACA,CAAC;AAAA,EACF;AAEA,QAAM,EAAE,MAAM,aAAa,IAAI,oBAAqB,QAAS;AAC7D,QAAM,CAAE,eAAgB,IAAI,qBAAsB,YAAa;AAE/D,QAAM,oBAAoB,CAAC,CAAE;AAC7B,QAAM,SAAS,QAAS,MAAM;AAC7B,QAAK,CAAE,mBAAoB;AAC1B,aAAO,CAAC;AAAA,IACT;AACA,WAAO;AAAA,MACN,GAAO,gBAAgB,UAA4B,CAAC;AAAA,MACpD,GAAG;AAAA,IACJ;AAAA,EACD,GAAG,CAAE,mBAAmB,gBAAgB,QAAQ,eAAgB,CAAE;AAElE,SAAO;AAAA,IACN,SAAS;AAAA,IACT,gBAAgB;AAAA,MACf,OAAQ;AAAA,QACP,GAAK,kBAAkB,CAAC;AAAA,QACxB;AAAA,MACD;AAAA,MACA,CAAE,gBAAgB,MAAO;AAAA,IAC1B;AAAA,EACD;AACD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// packages/lazy-editor/src/hooks/use-global-styles.tsx
|
|
2
|
+
import { store as coreStore } from "@wordpress/core-data";
|
|
3
|
+
import { useSelect } from "@wordpress/data";
|
|
4
|
+
import { useMemo } from "@wordpress/element";
|
|
5
|
+
function useUserGlobalStyles(id) {
|
|
6
|
+
const { userGlobalStyles } = useSelect(
|
|
7
|
+
(select) => {
|
|
8
|
+
const { getEntityRecord, getEditedEntityRecord, canUser } = select(coreStore);
|
|
9
|
+
const userCanEditGlobalStyles = canUser("update", {
|
|
10
|
+
kind: "root",
|
|
11
|
+
name: "globalStyles",
|
|
12
|
+
id
|
|
13
|
+
});
|
|
14
|
+
let record;
|
|
15
|
+
if (
|
|
16
|
+
/*
|
|
17
|
+
* Test that the OPTIONS request for user capabilities is complete
|
|
18
|
+
* before fetching the global styles entity record.
|
|
19
|
+
* This is to avoid fetching the global styles entity unnecessarily.
|
|
20
|
+
*/
|
|
21
|
+
typeof userCanEditGlobalStyles === "boolean"
|
|
22
|
+
) {
|
|
23
|
+
if (userCanEditGlobalStyles) {
|
|
24
|
+
record = getEditedEntityRecord(
|
|
25
|
+
"root",
|
|
26
|
+
"globalStyles",
|
|
27
|
+
id
|
|
28
|
+
);
|
|
29
|
+
} else {
|
|
30
|
+
record = getEntityRecord("root", "globalStyles", id, {
|
|
31
|
+
context: "view"
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
userGlobalStyles: record
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
[id]
|
|
40
|
+
);
|
|
41
|
+
return useMemo(() => {
|
|
42
|
+
if (!userGlobalStyles) {
|
|
43
|
+
return {
|
|
44
|
+
user: void 0
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
const user = userGlobalStyles;
|
|
48
|
+
return {
|
|
49
|
+
user
|
|
50
|
+
};
|
|
51
|
+
}, [userGlobalStyles]);
|
|
52
|
+
}
|
|
53
|
+
export {
|
|
54
|
+
useUserGlobalStyles
|
|
55
|
+
};
|
|
56
|
+
//# sourceMappingURL=use-global-styles.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/hooks/use-global-styles.tsx"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport type { GlobalStylesConfig } from '@wordpress/global-styles-engine';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { useSelect } from '@wordpress/data';\nimport { useMemo } from '@wordpress/element';\n\nexport function useUserGlobalStyles( id: string ) {\n\tconst { userGlobalStyles } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getEntityRecord, getEditedEntityRecord, canUser } =\n\t\t\t\tselect( coreStore );\n\n\t\t\t/*\n\t\t\t * Ensure that the global styles ID request is complete by testing `_globalStylesId`,\n\t\t\t * before firing off the `canUser` OPTIONS request for user capabilities, otherwise it will\n\t\t\t * fetch `/wp/v2/global-styles` instead of `/wp/v2/global-styles/{id}`.\n\t\t\t * NOTE: Please keep in sync any preload paths sent to `block_editor_rest_api_preload()`,\n\t\t\t * or set using the `block_editor_rest_api_preload_paths` filter, if this changes.\n\t\t\t */\n\t\t\tconst userCanEditGlobalStyles = canUser( 'update', {\n\t\t\t\tkind: 'root',\n\t\t\t\tname: 'globalStyles',\n\t\t\t\tid,\n\t\t\t} );\n\n\t\t\tlet record;\n\t\t\tif (\n\t\t\t\t/*\n\t\t\t\t * Test that the OPTIONS request for user capabilities is complete\n\t\t\t\t * before fetching the global styles entity record.\n\t\t\t\t * This is to avoid fetching the global styles entity unnecessarily.\n\t\t\t\t */\n\t\t\t\ttypeof userCanEditGlobalStyles === 'boolean'\n\t\t\t) {\n\t\t\t\t/*\n\t\t\t\t * Fetch the global styles entity record based on the user's capabilities.\n\t\t\t\t * The default context is `edit` for users who can edit global styles.\n\t\t\t\t * Otherwise, the context is `view`.\n\t\t\t\t */\n\t\t\t\tif ( userCanEditGlobalStyles ) {\n\t\t\t\t\trecord = getEditedEntityRecord(\n\t\t\t\t\t\t'root',\n\t\t\t\t\t\t'globalStyles',\n\t\t\t\t\t\tid\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\trecord = getEntityRecord( 'root', 'globalStyles', id, {\n\t\t\t\t\t\tcontext: 'view',\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tuserGlobalStyles: record,\n\t\t\t};\n\t\t},\n\t\t[ id ]\n\t);\n\n\treturn useMemo( () => {\n\t\tif ( ! userGlobalStyles ) {\n\t\t\treturn {\n\t\t\t\tuser: undefined,\n\t\t\t};\n\t\t}\n\n\t\tconst user = userGlobalStyles as GlobalStylesConfig;\n\n\t\treturn {\n\t\t\tuser,\n\t\t};\n\t}, [ userGlobalStyles ] );\n}\n"],
|
|
5
|
+
"mappings": ";AAIA,SAAS,SAAS,iBAAiB;AACnC,SAAS,iBAAiB;AAC1B,SAAS,eAAe;AAEjB,SAAS,oBAAqB,IAAa;AACjD,QAAM,EAAE,iBAAiB,IAAI;AAAA,IAC5B,CAAE,WAAY;AACb,YAAM,EAAE,iBAAiB,uBAAuB,QAAQ,IACvD,OAAQ,SAAU;AASnB,YAAM,0BAA0B,QAAS,UAAU;AAAA,QAClD,MAAM;AAAA,QACN,MAAM;AAAA,QACN;AAAA,MACD,CAAE;AAEF,UAAI;AACJ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,QAMC,OAAO,4BAA4B;AAAA,QAClC;AAMD,YAAK,yBAA0B;AAC9B,mBAAS;AAAA,YACR;AAAA,YACA;AAAA,YACA;AAAA,UACD;AAAA,QACD,OAAO;AACN,mBAAS,gBAAiB,QAAQ,gBAAgB,IAAI;AAAA,YACrD,SAAS;AAAA,UACV,CAAE;AAAA,QACH;AAAA,MACD;AAEA,aAAO;AAAA,QACN,kBAAkB;AAAA,MACnB;AAAA,IACD;AAAA,IACA,CAAE,EAAG;AAAA,EACN;AAEA,SAAO,QAAS,MAAM;AACrB,QAAK,CAAE,kBAAmB;AACzB,aAAO;AAAA,QACN,MAAM;AAAA,MACP;AAAA,IACD;AAEA,UAAM,OAAO;AAEb,WAAO;AAAA,MACN;AAAA,IACD;AAAA,EACD,GAAG,CAAE,gBAAiB,CAAE;AACzB;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// packages/lazy-editor/src/hooks/use-styles-id.tsx
|
|
2
|
+
import { store as coreStore } from "@wordpress/core-data";
|
|
3
|
+
import { useSelect } from "@wordpress/data";
|
|
4
|
+
function useStylesId({ templateId }) {
|
|
5
|
+
const { globalStylesId, stylesId } = useSelect(
|
|
6
|
+
(select) => {
|
|
7
|
+
const coreDataSelect = select(coreStore);
|
|
8
|
+
const template = templateId ? coreDataSelect.getEntityRecord(
|
|
9
|
+
"postType",
|
|
10
|
+
"wp_template",
|
|
11
|
+
templateId
|
|
12
|
+
) : null;
|
|
13
|
+
return {
|
|
14
|
+
globalStylesId: coreDataSelect.__experimentalGetCurrentGlobalStylesId(),
|
|
15
|
+
stylesId: template?.styles_id
|
|
16
|
+
};
|
|
17
|
+
},
|
|
18
|
+
[templateId]
|
|
19
|
+
);
|
|
20
|
+
return stylesId || globalStylesId;
|
|
21
|
+
}
|
|
22
|
+
export {
|
|
23
|
+
useStylesId
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=use-styles-id.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/hooks/use-styles-id.tsx"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { store as coreStore } from '@wordpress/core-data';\nimport { useSelect } from '@wordpress/data';\n\n// Define interface for template with optional styles_id\ninterface Template {\n\tstyles_id?: string;\n\t[ key: string ]: any;\n}\n\n/**\n * This is a React hook that provides the styles ID.\n * Styles ID can be associated with a template.\n * If a template has a styles ID, it will be used otherwise the global styles ID will be used.\n *\n * @param {Object} props - The props object.\n * @param {string} [props.templateId] - The ID of the template to use.\n * @return The styles ID.\n */\nexport function useStylesId( { templateId }: { templateId?: string } ) {\n\tconst { globalStylesId, stylesId } = useSelect(\n\t\t( select ) => {\n\t\t\tconst coreDataSelect = select( coreStore );\n\t\t\tconst template = templateId\n\t\t\t\t? ( coreDataSelect.getEntityRecord(\n\t\t\t\t\t\t'postType',\n\t\t\t\t\t\t'wp_template',\n\t\t\t\t\t\ttemplateId\n\t\t\t\t ) as Template | null )\n\t\t\t\t: null;\n\n\t\t\treturn {\n\t\t\t\tglobalStylesId:\n\t\t\t\t\tcoreDataSelect.__experimentalGetCurrentGlobalStylesId(),\n\t\t\t\tstylesId: template?.styles_id,\n\t\t\t};\n\t\t},\n\t\t[ templateId ]\n\t);\n\n\t// Otherwise, fall back to the global styles ID\n\treturn stylesId || globalStylesId;\n}\n"],
|
|
5
|
+
"mappings": ";AAGA,SAAS,SAAS,iBAAiB;AACnC,SAAS,iBAAiB;AAiBnB,SAAS,YAAa,EAAE,WAAW,GAA6B;AACtE,QAAM,EAAE,gBAAgB,SAAS,IAAI;AAAA,IACpC,CAAE,WAAY;AACb,YAAM,iBAAiB,OAAQ,SAAU;AACzC,YAAM,WAAW,aACZ,eAAe;AAAA,QACjB;AAAA,QACA;AAAA,QACA;AAAA,MACA,IACA;AAEH,aAAO;AAAA,QACN,gBACC,eAAe,uCAAuC;AAAA,QACvD,UAAU,UAAU;AAAA,MACrB;AAAA,IACD;AAAA,IACA,CAAE,UAAW;AAAA,EACd;AAGA,SAAO,YAAY;AACpB;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// packages/lazy-editor/src/lock-unlock.ts
|
|
2
|
+
import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from "@wordpress/private-apis";
|
|
3
|
+
var { unlock } = __dangerousOptInToUnstableAPIsOnlyForCoreModules(
|
|
4
|
+
"I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.",
|
|
5
|
+
"@wordpress/lazy-editor"
|
|
6
|
+
);
|
|
7
|
+
export {
|
|
8
|
+
unlock
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=lock-unlock.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/lock-unlock.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';\n\nexport const { unlock } = __dangerousOptInToUnstableAPIsOnlyForCoreModules(\n\t'I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.',\n\t'@wordpress/lazy-editor'\n);\n"],
|
|
5
|
+
"mappings": ";AAGA,SAAS,wDAAwD;AAE1D,IAAM,EAAE,OAAO,IAAI;AAAA,EACzB;AAAA,EACA;AACD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { ReactNode } from 'react';
|
|
5
|
+
interface EditorProps {
|
|
6
|
+
postType: string;
|
|
7
|
+
postId: string;
|
|
8
|
+
settings?: Record<string, any>;
|
|
9
|
+
backButton?: ReactNode;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Lazy-loading editor component that handles asset loading and settings initialization.
|
|
13
|
+
*
|
|
14
|
+
* @param {Object} props Component props
|
|
15
|
+
* @param {string} props.postType The post type to edit
|
|
16
|
+
* @param {string} props.postId The post ID to edit
|
|
17
|
+
* @param {Object} props.settings Optional extra settings to merge with editor settings
|
|
18
|
+
* @param {ReactNode} props.backButton Optional back button to render in editor header
|
|
19
|
+
* @return The editor component with loading states
|
|
20
|
+
*/
|
|
21
|
+
export declare function Editor({ postType, postId, settings, backButton, }: EditorProps): import("react").JSX.Element;
|
|
22
|
+
export {};
|
|
23
|
+
//# sourceMappingURL=component.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"component.d.ts","sourceRoot":"","sources":["../src/component.tsx"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAC;AAqBvC,UAAU,WAAW;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAE,MAAM,EAAE,GAAG,CAAE,CAAC;IACjC,UAAU,CAAC,EAAE,SAAS,CAAC;CACvB;AAED;;;;;;;;;GASG;AACH,wBAAgB,MAAM,CAAE,EACvB,QAAQ,EACR,MAAM,EACN,QAAQ,EACR,UAAU,GACV,EAAE,WAAW,+BA+Db"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare function loadEditorAssets(): Promise<void>;
|
|
2
|
+
/**
|
|
3
|
+
* This is a React hook that handles loading editor assets from the REST API.
|
|
4
|
+
*
|
|
5
|
+
* @return Editor assets loading state.
|
|
6
|
+
*/
|
|
7
|
+
export declare function useEditorAssets(): {
|
|
8
|
+
isReady: boolean;
|
|
9
|
+
assetsLoaded: boolean;
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=use-editor-assets.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-editor-assets.d.ts","sourceRoot":"","sources":["../../src/hooks/use-editor-assets.tsx"],"names":[],"mappings":"AAeA,wBAAsB,gBAAgB,kBAkBrC;AAED;;;;GAIG;AACH,wBAAgB,eAAe;;;EAwB9B"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is a React hook that provides the editor settings from the REST API.
|
|
3
|
+
*
|
|
4
|
+
* @param {Object} props - The props object.
|
|
5
|
+
* @param {string} [props.stylesId] - The ID of the user's global styles to use.
|
|
6
|
+
* @return Editor settings.
|
|
7
|
+
*/
|
|
8
|
+
export declare function useEditorSettings({ stylesId }: {
|
|
9
|
+
stylesId: string;
|
|
10
|
+
}): {
|
|
11
|
+
isReady: boolean;
|
|
12
|
+
editorSettings: any;
|
|
13
|
+
};
|
|
14
|
+
//# sourceMappingURL=use-editor-settings.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-editor-settings.d.ts","sourceRoot":"","sources":["../../src/hooks/use-editor-settings.tsx"],"names":[],"mappings":"AAcA;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAE,EAAE,QAAQ,EAAE,EAAE;IAAE,QAAQ,EAAE,MAAM,CAAA;CAAE;;;EAkCpE"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { GlobalStylesConfig } from '@wordpress/global-styles-engine';
|
|
5
|
+
export declare function useUserGlobalStyles(id: string): {
|
|
6
|
+
user: undefined;
|
|
7
|
+
} | {
|
|
8
|
+
user: GlobalStylesConfig;
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=use-global-styles.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-global-styles.d.ts","sourceRoot":"","sources":["../../src/hooks/use-global-styles.tsx"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAC;AAK1E,wBAAgB,mBAAmB,CAAE,EAAE,EAAE,MAAM;;;;EAkE9C"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is a React hook that provides the styles ID.
|
|
3
|
+
* Styles ID can be associated with a template.
|
|
4
|
+
* If a template has a styles ID, it will be used otherwise the global styles ID will be used.
|
|
5
|
+
*
|
|
6
|
+
* @param {Object} props - The props object.
|
|
7
|
+
* @param {string} [props.templateId] - The ID of the template to use.
|
|
8
|
+
* @return The styles ID.
|
|
9
|
+
*/
|
|
10
|
+
export declare function useStylesId({ templateId }: {
|
|
11
|
+
templateId?: string;
|
|
12
|
+
}): string;
|
|
13
|
+
//# sourceMappingURL=use-styles-id.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-styles-id.d.ts","sourceRoot":"","sources":["../../src/hooks/use-styles-id.tsx"],"names":[],"mappings":"AAYA;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAE,EAAE,UAAU,EAAE,EAAE;IAAE,UAAU,CAAC,EAAE,MAAM,CAAA;CAAE,UAuBnE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lock-unlock.d.ts","sourceRoot":"","sources":["../src/lock-unlock.ts"],"names":[],"mappings":"AAKA,eAAO,MAAQ,MAAM,iCAGpB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wordpress/lazy-editor",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Lazy-loading editor component with automatic asset and settings management.",
|
|
5
|
+
"author": "The WordPress Contributors",
|
|
6
|
+
"license": "GPL-2.0-or-later",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"wordpress",
|
|
9
|
+
"gutenberg",
|
|
10
|
+
"editor",
|
|
11
|
+
"lazy-loading",
|
|
12
|
+
"assets"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/WordPress/gutenberg/tree/HEAD/packages/lazy-editor/README.md",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/WordPress/gutenberg.git",
|
|
18
|
+
"directory": "packages/lazy-editor"
|
|
19
|
+
},
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/WordPress/gutenberg/issues"
|
|
22
|
+
},
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=18.12.0",
|
|
25
|
+
"npm": ">=8.19.2"
|
|
26
|
+
},
|
|
27
|
+
"main": "build/index.js",
|
|
28
|
+
"module": "build-module/index.js",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./build-types/index.d.ts",
|
|
32
|
+
"import": "./build-module/index.js",
|
|
33
|
+
"default": "./build/index.js"
|
|
34
|
+
},
|
|
35
|
+
"./package.json": "./package.json"
|
|
36
|
+
},
|
|
37
|
+
"react-native": "src/index",
|
|
38
|
+
"wpScriptModuleExports": "./build-module/index.js",
|
|
39
|
+
"types": "build-types",
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@wordpress/asset-loader": "^1.1.0",
|
|
42
|
+
"@wordpress/block-editor": "^15.8.0",
|
|
43
|
+
"@wordpress/components": "^30.8.0",
|
|
44
|
+
"@wordpress/core-data": "^7.35.0",
|
|
45
|
+
"@wordpress/data": "^10.35.0",
|
|
46
|
+
"@wordpress/editor": "^14.35.0",
|
|
47
|
+
"@wordpress/element": "^6.35.0",
|
|
48
|
+
"@wordpress/global-styles-engine": "^1.2.0",
|
|
49
|
+
"@wordpress/private-apis": "^1.35.0"
|
|
50
|
+
},
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public"
|
|
53
|
+
},
|
|
54
|
+
"gitHead": "77aa1f194edceafe8ac2a1b9438bf84b557e76e3"
|
|
55
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { ReactNode } from 'react';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* WordPress dependencies
|
|
8
|
+
*/
|
|
9
|
+
import { privateApis as editorPrivateApis } from '@wordpress/editor';
|
|
10
|
+
import { store as coreDataStore } from '@wordpress/core-data';
|
|
11
|
+
import { useSelect } from '@wordpress/data';
|
|
12
|
+
import { Spinner } from '@wordpress/components';
|
|
13
|
+
import { useMemo } from '@wordpress/element';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Internal dependencies
|
|
17
|
+
*/
|
|
18
|
+
import { useStylesId } from './hooks/use-styles-id';
|
|
19
|
+
import { useEditorSettings } from './hooks/use-editor-settings';
|
|
20
|
+
import { useEditorAssets } from './hooks/use-editor-assets';
|
|
21
|
+
import { unlock } from './lock-unlock';
|
|
22
|
+
|
|
23
|
+
const { Editor: PrivateEditor, BackButton } = unlock( editorPrivateApis );
|
|
24
|
+
|
|
25
|
+
interface EditorProps {
|
|
26
|
+
postType: string;
|
|
27
|
+
postId: string;
|
|
28
|
+
settings?: Record< string, any >;
|
|
29
|
+
backButton?: ReactNode;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Lazy-loading editor component that handles asset loading and settings initialization.
|
|
34
|
+
*
|
|
35
|
+
* @param {Object} props Component props
|
|
36
|
+
* @param {string} props.postType The post type to edit
|
|
37
|
+
* @param {string} props.postId The post ID to edit
|
|
38
|
+
* @param {Object} props.settings Optional extra settings to merge with editor settings
|
|
39
|
+
* @param {ReactNode} props.backButton Optional back button to render in editor header
|
|
40
|
+
* @return The editor component with loading states
|
|
41
|
+
*/
|
|
42
|
+
export function Editor( {
|
|
43
|
+
postType,
|
|
44
|
+
postId,
|
|
45
|
+
settings,
|
|
46
|
+
backButton,
|
|
47
|
+
}: EditorProps ) {
|
|
48
|
+
// Resolve template ID from post type/ID
|
|
49
|
+
const templateId = useSelect(
|
|
50
|
+
( select ) => {
|
|
51
|
+
if ( ! postType || ! postId ) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
if ( postType === 'wp_template' ) {
|
|
55
|
+
return postId;
|
|
56
|
+
}
|
|
57
|
+
// Use private API to get template ID for this post
|
|
58
|
+
return unlock( select( coreDataStore ) ).getTemplateId(
|
|
59
|
+
postType,
|
|
60
|
+
postId
|
|
61
|
+
);
|
|
62
|
+
},
|
|
63
|
+
[ postType, postId ]
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
// Resolve styles ID from template
|
|
67
|
+
const stylesId = useStylesId( { templateId } );
|
|
68
|
+
|
|
69
|
+
// Load editor settings and assets
|
|
70
|
+
const { isReady: settingsReady, editorSettings } = useEditorSettings( {
|
|
71
|
+
stylesId,
|
|
72
|
+
} );
|
|
73
|
+
const { isReady: assetsReady } = useEditorAssets();
|
|
74
|
+
const finalSettings = useMemo(
|
|
75
|
+
() => ( {
|
|
76
|
+
...editorSettings,
|
|
77
|
+
...settings,
|
|
78
|
+
} ),
|
|
79
|
+
[ editorSettings, settings ]
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
// Show loading spinner while assets or settings are loading
|
|
83
|
+
if ( ! settingsReady || ! assetsReady ) {
|
|
84
|
+
return (
|
|
85
|
+
<div
|
|
86
|
+
style={ {
|
|
87
|
+
display: 'flex',
|
|
88
|
+
justifyContent: 'center',
|
|
89
|
+
alignItems: 'center',
|
|
90
|
+
height: '100vh',
|
|
91
|
+
} }
|
|
92
|
+
>
|
|
93
|
+
<Spinner />
|
|
94
|
+
</div>
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Render the editor when ready
|
|
99
|
+
return (
|
|
100
|
+
<PrivateEditor
|
|
101
|
+
postType={ postType }
|
|
102
|
+
postId={ postId }
|
|
103
|
+
templateId={ templateId }
|
|
104
|
+
settings={ finalSettings }
|
|
105
|
+
styles={ finalSettings.styles }
|
|
106
|
+
>
|
|
107
|
+
{ backButton && <BackButton>{ backButton }</BackButton> }
|
|
108
|
+
</PrivateEditor>
|
|
109
|
+
);
|
|
110
|
+
}
|