@automattic/jetpack-shared-extension-utils 1.5.0 → 1.5.2

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 CHANGED
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [1.5.2] - 2026-03-02
9
+ ### Changed
10
+ - Convert `useAutosaveAndRedirect` and `getJetpackExtensionAvailability` to TypeScript. [#47382]
11
+
12
+ ## [1.5.1] - 2026-02-26
13
+ ### Changed
14
+ - Update package dependencies. [#47300]
15
+
8
16
  ## [1.5.0] - 2026-02-23
9
17
  ### Changed
10
18
  - Update `JetpackEditorPanelLogo` to respect the `jetpack_show_editor_panel_branding` PHP filter. [#47094]
@@ -878,6 +886,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
878
886
  ### Changed
879
887
  - Core: prepare utility for release
880
888
 
889
+ [1.5.2]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/1.5.1...1.5.2
890
+ [1.5.1]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/1.5.0...1.5.1
881
891
  [1.5.0]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/1.4.13...1.5.0
882
892
  [1.4.13]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/1.4.12...1.4.13
883
893
  [1.4.12]: https://github.com/Automattic/jetpack-shared-extension-utils/compare/1.4.11...1.4.12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automattic/jetpack-shared-extension-utils",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
4
4
  "description": "Utility functions used by the block editor extensions",
5
5
  "homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/shared-extension-utils/#readme",
6
6
  "bugs": {
@@ -31,23 +31,23 @@
31
31
  "dependencies": {
32
32
  "@automattic/color-studio": "4.1.0",
33
33
  "@automattic/jetpack-analytics": "^1.0.8",
34
- "@automattic/jetpack-base-styles": "^1.0.17",
35
- "@automattic/jetpack-components": "^1.4.15",
36
- "@automattic/jetpack-connection": "^1.4.36",
37
- "@automattic/jetpack-script-data": "^0.5.5",
34
+ "@automattic/jetpack-base-styles": "^1.0.18",
35
+ "@automattic/jetpack-components": "^1.4.16",
36
+ "@automattic/jetpack-connection": "^1.4.38",
37
+ "@automattic/jetpack-script-data": "^0.6.0",
38
38
  "@types/jest": "30.0.0",
39
- "@wordpress/api-fetch": "7.39.0",
40
- "@wordpress/block-editor": "15.12.0",
41
- "@wordpress/components": "32.1.0",
42
- "@wordpress/compose": "7.39.0",
43
- "@wordpress/data": "10.39.0",
44
- "@wordpress/dom-ready": "4.39.0",
45
- "@wordpress/element": "6.39.0",
46
- "@wordpress/hooks": "4.39.0",
47
- "@wordpress/i18n": "6.12.0",
48
- "@wordpress/plugins": "7.39.0",
49
- "@wordpress/primitives": "4.39.0",
50
- "@wordpress/url": "4.39.0",
39
+ "@wordpress/api-fetch": "7.40.0",
40
+ "@wordpress/block-editor": "15.13.1",
41
+ "@wordpress/components": "32.2.0",
42
+ "@wordpress/compose": "7.40.0",
43
+ "@wordpress/data": "10.40.0",
44
+ "@wordpress/dom-ready": "4.40.0",
45
+ "@wordpress/element": "6.40.0",
46
+ "@wordpress/hooks": "4.40.0",
47
+ "@wordpress/i18n": "6.13.0",
48
+ "@wordpress/plugins": "7.40.0",
49
+ "@wordpress/primitives": "4.40.0",
50
+ "@wordpress/url": "4.40.0",
51
51
  "clsx": "2.1.1",
52
52
  "debug": "4.4.3"
53
53
  },
@@ -0,0 +1,34 @@
1
+ import getJetpackData from './get-jetpack-data';
2
+
3
+ interface BlockAvailability {
4
+ available: boolean;
5
+ unavailable_reason?: string;
6
+ details?: Record< string, unknown >;
7
+ }
8
+
9
+ interface JetpackData {
10
+ available_blocks?: Record< string, BlockAvailability >;
11
+ }
12
+
13
+ export interface ExtensionAvailability {
14
+ available: boolean;
15
+ details?: Record< string, unknown >;
16
+ unavailableReason?: string;
17
+ }
18
+
19
+ /**
20
+ * Return whether a Jetpack Gutenberg extension is available or not.
21
+ *
22
+ * @param {string} name - The extension's name (without the `jetpack/` prefix)
23
+ * @return Object indicating if the extension is available and the reason why it is unavailable.
24
+ */
25
+ export default function getJetpackExtensionAvailability( name: string ): ExtensionAvailability {
26
+ const data = getJetpackData() as JetpackData | null;
27
+ const available = data?.available_blocks?.[ name ]?.available ?? false;
28
+ const unavailableReason = data?.available_blocks?.[ name ]?.unavailable_reason ?? 'unknown';
29
+ const details = data?.available_blocks?.[ name ]?.details ?? {};
30
+ return {
31
+ available,
32
+ ...( ! available && { details, unavailableReason } ),
33
+ };
34
+ }
@@ -3,6 +3,16 @@ import { useState } from '@wordpress/element';
3
3
 
4
4
  const noop = () => {};
5
5
 
6
+ interface PreventableEvent {
7
+ preventDefault: () => void;
8
+ }
9
+
10
+ export interface UseAutosaveAndRedirectReturn {
11
+ autosave: ( event?: PreventableEvent ) => Promise< void >;
12
+ autosaveAndRedirect: ( event?: PreventableEvent ) => Promise< void >;
13
+ isRedirecting: boolean;
14
+ }
15
+
6
16
  /**
7
17
  * To handle the redirection
8
18
  * @param {string} url - The redirect URL.
@@ -10,26 +20,32 @@ const noop = () => {};
10
20
  * @param {boolean} shouldOpenNewWindow - Whether to open the new window.
11
21
  * @return {Window | null} - The open window.
12
22
  */
13
- function redirect( url, callback, shouldOpenNewWindow = false ) {
23
+ function redirect( url: string, callback: ( url: string ) => void, shouldOpenNewWindow = false ) {
14
24
  if ( callback ) {
15
25
  callback( url );
16
26
  }
17
27
 
18
- return shouldOpenNewWindow ? window.open( url, '_blank' ) : ( window.top.location.href = url );
28
+ return shouldOpenNewWindow
29
+ ? window.open( url, '_blank' )
30
+ : ( ( window.top as Window ).location.href = url );
19
31
  }
20
32
 
21
33
  /**
22
- * Hook to get properties for AiImage
34
+ * Hook to get properties for autosave and redirect.
23
35
  *
24
36
  * @param {string} redirectUrl - The redirect URL.
25
37
  * @param {Function} onRedirect - To handle the redirection.
26
- * @return {object} - Object containing properties to handle autosave and redirect.
38
+ * @return Object containing properties to handle autosave and redirect.
27
39
  */
28
- export default function useAutosaveAndRedirect( redirectUrl = null, onRedirect = noop ) {
40
+ export default function useAutosaveAndRedirect(
41
+ redirectUrl: string | null = null,
42
+ onRedirect: ( url: string ) => void = noop
43
+ ): UseAutosaveAndRedirectReturn {
29
44
  const [ isRedirecting, setIsRedirecting ] = useState( false );
30
45
 
31
46
  const { isAutosaveablePost, isDirtyPost, currentPost } = useSelect( select => {
32
- const editorSelector = select( 'core/editor' );
47
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
48
+ const editorSelector = select( 'core/editor' ) as any;
33
49
 
34
50
  return {
35
51
  isAutosaveablePost: editorSelector.isEditedPostAutosaveable(),
@@ -41,7 +57,7 @@ export default function useAutosaveAndRedirect( redirectUrl = null, onRedirect =
41
57
  const isPostEditor = Object.keys( currentPost ).length > 0;
42
58
 
43
59
  const isWidgetEditor = useSelect( select => {
44
- if ( window.wp?.customize ) {
60
+ if ( ( window as { wp?: { customize?: unknown } } ).wp?.customize ) {
45
61
  return true;
46
62
  }
47
63
 
@@ -49,18 +65,21 @@ export default function useAutosaveAndRedirect( redirectUrl = null, onRedirect =
49
65
  } );
50
66
 
51
67
  // Alias. Save post by dispatch.
52
- const savePost = dispatch( 'core/editor' ).savePost;
68
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
69
+ const savePost = ( dispatch( 'core/editor' ) as any ).savePost;
53
70
 
54
71
  // For the site editor, save entities
55
72
  const entityRecords = useSelect( select => {
56
- return select( 'core' ).__experimentalGetDirtyEntityRecords();
73
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
74
+ return ( select( 'core' ) as any ).__experimentalGetDirtyEntityRecords();
57
75
  } );
58
76
 
59
77
  // Save
60
78
  const saveEntities = async () => {
61
79
  for ( let i = 0; i < entityRecords.length; i++ ) {
62
80
  // await is needed here due to the loop.
63
- await dispatch( 'core' ).saveEditedEntityRecord(
81
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
82
+ await ( dispatch( 'core' ) as any ).saveEditedEntityRecord(
64
83
  entityRecords[ i ].kind,
65
84
  entityRecords[ i ].name,
66
85
  entityRecords[ i ].key
@@ -68,8 +87,8 @@ export default function useAutosaveAndRedirect( redirectUrl = null, onRedirect =
68
87
  }
69
88
  };
70
89
 
71
- const autosave = async event => {
72
- event.preventDefault();
90
+ const autosave = async ( event?: PreventableEvent ) => {
91
+ event?.preventDefault();
73
92
 
74
93
  if ( isPostEditor ) {
75
94
  /**
@@ -81,12 +100,12 @@ export default function useAutosaveAndRedirect( redirectUrl = null, onRedirect =
81
100
  }
82
101
  } else {
83
102
  // Save entities in the site editor.
84
- await saveEntities( event );
103
+ await saveEntities();
85
104
  }
86
105
  };
87
106
 
88
- const autosaveAndRedirect = async event => {
89
- event.preventDefault();
107
+ const autosaveAndRedirect = async ( event?: PreventableEvent ) => {
108
+ event?.preventDefault();
90
109
 
91
110
  // Lock re-redirecting attempts.
92
111
  if ( isRedirecting ) {
@@ -1,19 +0,0 @@
1
- import getJetpackData from './get-jetpack-data';
2
-
3
- /**
4
- * Return whether a Jetpack Gutenberg extension is available or not.
5
- *
6
- * @param {string} name - The extension's name (without the `jetpack/` prefix)
7
- * @return {object} Object indicating if the extension is available (property `available`) and the reason why it is
8
- * unavailable (property `unavailable_reason`).
9
- */
10
- export default function getJetpackExtensionAvailability( name ) {
11
- const data = getJetpackData();
12
- const available = data?.available_blocks?.[ name ]?.available ?? false;
13
- const unavailableReason = data?.available_blocks?.[ name ]?.unavailable_reason ?? 'unknown';
14
- const details = data?.available_blocks?.[ name ]?.details ?? [];
15
- return {
16
- available,
17
- ...( ! available && { details, unavailableReason } ),
18
- };
19
- }