@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.
Files changed (55) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/LICENSE.md +788 -0
  3. package/README.md +65 -0
  4. package/build/component.js +100 -0
  5. package/build/component.js.map +7 -0
  6. package/build/hooks/use-editor-assets.js +84 -0
  7. package/build/hooks/use-editor-assets.js.map +7 -0
  8. package/build/hooks/use-editor-settings.js +68 -0
  9. package/build/hooks/use-editor-settings.js.map +7 -0
  10. package/build/hooks/use-global-styles.js +81 -0
  11. package/build/hooks/use-global-styles.js.map +7 -0
  12. package/build/hooks/use-styles-id.js +50 -0
  13. package/build/hooks/use-styles-id.js.map +7 -0
  14. package/build/index.js +31 -0
  15. package/build/index.js.map +7 -0
  16. package/build/lock-unlock.js +35 -0
  17. package/build/lock-unlock.js.map +7 -0
  18. package/build-module/component.js +75 -0
  19. package/build-module/component.js.map +7 -0
  20. package/build-module/hooks/use-editor-assets.js +48 -0
  21. package/build-module/hooks/use-editor-assets.js.map +7 -0
  22. package/build-module/hooks/use-editor-settings.js +43 -0
  23. package/build-module/hooks/use-editor-settings.js.map +7 -0
  24. package/build-module/hooks/use-global-styles.js +56 -0
  25. package/build-module/hooks/use-global-styles.js.map +7 -0
  26. package/build-module/hooks/use-styles-id.js +25 -0
  27. package/build-module/hooks/use-styles-id.js.map +7 -0
  28. package/build-module/index.js +6 -0
  29. package/build-module/index.js.map +7 -0
  30. package/build-module/lock-unlock.js +10 -0
  31. package/build-module/lock-unlock.js.map +7 -0
  32. package/build-types/component.d.ts +23 -0
  33. package/build-types/component.d.ts.map +1 -0
  34. package/build-types/hooks/use-editor-assets.d.ts +11 -0
  35. package/build-types/hooks/use-editor-assets.d.ts.map +1 -0
  36. package/build-types/hooks/use-editor-settings.d.ts +14 -0
  37. package/build-types/hooks/use-editor-settings.d.ts.map +1 -0
  38. package/build-types/hooks/use-global-styles.d.ts +10 -0
  39. package/build-types/hooks/use-global-styles.d.ts.map +1 -0
  40. package/build-types/hooks/use-styles-id.d.ts +13 -0
  41. package/build-types/hooks/use-styles-id.d.ts.map +1 -0
  42. package/build-types/index.d.ts +5 -0
  43. package/build-types/index.d.ts.map +1 -0
  44. package/build-types/lock-unlock.d.ts +2 -0
  45. package/build-types/lock-unlock.d.ts.map +1 -0
  46. package/package.json +55 -0
  47. package/src/component.tsx +110 -0
  48. package/src/hooks/use-editor-assets.tsx +65 -0
  49. package/src/hooks/use-editor-settings.tsx +56 -0
  50. package/src/hooks/use-global-styles.tsx +75 -0
  51. package/src/hooks/use-styles-id.tsx +45 -0
  52. package/src/index.tsx +4 -0
  53. package/src/lock-unlock.ts +9 -0
  54. package/tsconfig.json +18 -0
  55. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,65 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import loadAssets from '@wordpress/asset-loader';
5
+ import { store as coreDataStore } from '@wordpress/core-data';
6
+ import { useState, useEffect } from '@wordpress/element';
7
+ import { resolveSelect, useSelect } from '@wordpress/data';
8
+
9
+ /**
10
+ * Internal dependencies
11
+ */
12
+ import { unlock } from '../lock-unlock';
13
+
14
+ let loadAssetsPromise: Promise< void >;
15
+
16
+ export async function loadEditorAssets() {
17
+ const load = async () => {
18
+ const editorAssets = await unlock(
19
+ resolveSelect( coreDataStore )
20
+ ).getEditorAssets();
21
+ await loadAssets(
22
+ editorAssets.scripts || {},
23
+ editorAssets.inline_scripts || { before: {}, after: {} },
24
+ editorAssets.styles || {},
25
+ editorAssets.inline_styles || { before: {}, after: {} }
26
+ );
27
+ };
28
+
29
+ if ( ! loadAssetsPromise ) {
30
+ loadAssetsPromise = load();
31
+ }
32
+
33
+ return loadAssetsPromise;
34
+ }
35
+
36
+ /**
37
+ * This is a React hook that handles loading editor assets from the REST API.
38
+ *
39
+ * @return Editor assets loading state.
40
+ */
41
+ export function useEditorAssets() {
42
+ const editorAssets = useSelect( ( select ) => {
43
+ return unlock( select( coreDataStore ) ).getEditorAssets();
44
+ }, [] );
45
+
46
+ const [ assetsLoaded, setAssetsLoaded ] = useState( false );
47
+
48
+ useEffect( () => {
49
+ if ( editorAssets && ! assetsLoaded ) {
50
+ loadEditorAssets()
51
+ .then( () => {
52
+ setAssetsLoaded( true );
53
+ } )
54
+ .catch( ( error: Error ) => {
55
+ // eslint-disable-next-line no-console
56
+ console.error( 'Failed to load editor assets:', error );
57
+ } );
58
+ }
59
+ }, [ editorAssets, assetsLoaded ] );
60
+
61
+ return {
62
+ isReady: !! editorAssets && assetsLoaded,
63
+ assetsLoaded,
64
+ };
65
+ }
@@ -0,0 +1,56 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { generateGlobalStyles } from '@wordpress/global-styles-engine';
5
+ import { store as coreDataStore } from '@wordpress/core-data';
6
+ import { useMemo } from '@wordpress/element';
7
+ import { useSelect } from '@wordpress/data';
8
+
9
+ /**
10
+ * Internal dependencies
11
+ */
12
+ import { useUserGlobalStyles } from './use-global-styles';
13
+ import { unlock } from '../lock-unlock';
14
+
15
+ /**
16
+ * This is a React hook that provides the editor settings from the REST API.
17
+ *
18
+ * @param {Object} props - The props object.
19
+ * @param {string} [props.stylesId] - The ID of the user's global styles to use.
20
+ * @return Editor settings.
21
+ */
22
+ export function useEditorSettings( { stylesId }: { stylesId: string } ) {
23
+ const { editorSettings } = useSelect(
24
+ ( select ) => ( {
25
+ editorSettings: unlock(
26
+ select( coreDataStore )
27
+ ).getEditorSettings(),
28
+ } ),
29
+ []
30
+ );
31
+
32
+ const { user: globalStyles } = useUserGlobalStyles( stylesId );
33
+ const [ globalStylesCSS ] = generateGlobalStyles( globalStyles );
34
+
35
+ const hasEditorSettings = !! editorSettings;
36
+ const styles = useMemo( () => {
37
+ if ( ! hasEditorSettings ) {
38
+ return [];
39
+ }
40
+ return [
41
+ ...( ( editorSettings?.styles as Array< any > ) ?? [] ),
42
+ ...globalStylesCSS,
43
+ ];
44
+ }, [ hasEditorSettings, editorSettings?.styles, globalStylesCSS ] );
45
+
46
+ return {
47
+ isReady: hasEditorSettings,
48
+ editorSettings: useMemo(
49
+ () => ( {
50
+ ...( editorSettings ?? {} ),
51
+ styles,
52
+ } ),
53
+ [ editorSettings, styles ]
54
+ ),
55
+ };
56
+ }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import type { GlobalStylesConfig } from '@wordpress/global-styles-engine';
5
+ import { store as coreStore } from '@wordpress/core-data';
6
+ import { useSelect } from '@wordpress/data';
7
+ import { useMemo } from '@wordpress/element';
8
+
9
+ export function useUserGlobalStyles( id: string ) {
10
+ const { userGlobalStyles } = useSelect(
11
+ ( select ) => {
12
+ const { getEntityRecord, getEditedEntityRecord, canUser } =
13
+ select( coreStore );
14
+
15
+ /*
16
+ * Ensure that the global styles ID request is complete by testing `_globalStylesId`,
17
+ * before firing off the `canUser` OPTIONS request for user capabilities, otherwise it will
18
+ * fetch `/wp/v2/global-styles` instead of `/wp/v2/global-styles/{id}`.
19
+ * NOTE: Please keep in sync any preload paths sent to `block_editor_rest_api_preload()`,
20
+ * or set using the `block_editor_rest_api_preload_paths` filter, if this changes.
21
+ */
22
+ const userCanEditGlobalStyles = canUser( 'update', {
23
+ kind: 'root',
24
+ name: 'globalStyles',
25
+ id,
26
+ } );
27
+
28
+ let record;
29
+ if (
30
+ /*
31
+ * Test that the OPTIONS request for user capabilities is complete
32
+ * before fetching the global styles entity record.
33
+ * This is to avoid fetching the global styles entity unnecessarily.
34
+ */
35
+ typeof userCanEditGlobalStyles === 'boolean'
36
+ ) {
37
+ /*
38
+ * Fetch the global styles entity record based on the user's capabilities.
39
+ * The default context is `edit` for users who can edit global styles.
40
+ * Otherwise, the context is `view`.
41
+ */
42
+ if ( userCanEditGlobalStyles ) {
43
+ record = getEditedEntityRecord(
44
+ 'root',
45
+ 'globalStyles',
46
+ id
47
+ );
48
+ } else {
49
+ record = getEntityRecord( 'root', 'globalStyles', id, {
50
+ context: 'view',
51
+ } );
52
+ }
53
+ }
54
+
55
+ return {
56
+ userGlobalStyles: record,
57
+ };
58
+ },
59
+ [ id ]
60
+ );
61
+
62
+ return useMemo( () => {
63
+ if ( ! userGlobalStyles ) {
64
+ return {
65
+ user: undefined,
66
+ };
67
+ }
68
+
69
+ const user = userGlobalStyles as GlobalStylesConfig;
70
+
71
+ return {
72
+ user,
73
+ };
74
+ }, [ userGlobalStyles ] );
75
+ }
@@ -0,0 +1,45 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { store as coreStore } from '@wordpress/core-data';
5
+ import { useSelect } from '@wordpress/data';
6
+
7
+ // Define interface for template with optional styles_id
8
+ interface Template {
9
+ styles_id?: string;
10
+ [ key: string ]: any;
11
+ }
12
+
13
+ /**
14
+ * This is a React hook that provides the styles ID.
15
+ * Styles ID can be associated with a template.
16
+ * If a template has a styles ID, it will be used otherwise the global styles ID will be used.
17
+ *
18
+ * @param {Object} props - The props object.
19
+ * @param {string} [props.templateId] - The ID of the template to use.
20
+ * @return The styles ID.
21
+ */
22
+ export function useStylesId( { templateId }: { templateId?: string } ) {
23
+ const { globalStylesId, stylesId } = useSelect(
24
+ ( select ) => {
25
+ const coreDataSelect = select( coreStore );
26
+ const template = templateId
27
+ ? ( coreDataSelect.getEntityRecord(
28
+ 'postType',
29
+ 'wp_template',
30
+ templateId
31
+ ) as Template | null )
32
+ : null;
33
+
34
+ return {
35
+ globalStylesId:
36
+ coreDataSelect.__experimentalGetCurrentGlobalStylesId(),
37
+ stylesId: template?.styles_id,
38
+ };
39
+ },
40
+ [ templateId ]
41
+ );
42
+
43
+ // Otherwise, fall back to the global styles ID
44
+ return stylesId || globalStylesId;
45
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ export { Editor } from './component';
@@ -0,0 +1,9 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';
5
+
6
+ export const { unlock } = __dangerousOptInToUnstableAPIsOnlyForCoreModules(
7
+ 'I acknowledge private features are not for use in themes or plugins and doing so will break in the next version of WordPress.',
8
+ '@wordpress/lazy-editor'
9
+ );
package/tsconfig.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "$schema": "https://json.schemastore.org/tsconfig.json",
3
+ "extends": "../../tsconfig.base.json",
4
+ "compilerOptions": {
5
+ "checkJs": false
6
+ },
7
+ "references": [
8
+ { "path": "../asset-loader" },
9
+ { "path": "../block-editor" },
10
+ { "path": "../components" },
11
+ { "path": "../core-data" },
12
+ { "path": "../data" },
13
+ { "path": "../element" },
14
+ { "path": "../editor" },
15
+ { "path": "../global-styles-engine" },
16
+ { "path": "../private-apis" }
17
+ ]
18
+ }