@secretstache/wordpress-gutenberg 0.6.8 → 0.6.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@secretstache/wordpress-gutenberg",
3
- "version": "0.6.8",
3
+ "version": "0.6.9",
4
4
  "description": "",
5
5
  "author": "Secret Stache",
6
6
  "license": "GPL-2.0-or-later",
@@ -57,6 +57,7 @@
57
57
  },
58
58
  "dependencies": {
59
59
  "classnames": "^2.5.1",
60
+ "debounce-promise": "^3.1.2",
60
61
  "es-toolkit": "^1.12.0",
61
62
  "react-select": "5.7.5",
62
63
  "react-sortable-hoc": "2.0.0",
@@ -1,54 +1,79 @@
1
1
  import { filters } from '@wordpress/hooks';
2
2
  import apiFetch from '@wordpress/api-fetch';
3
- import slugify from 'slugify';
4
- import classNames from 'classnames';
5
3
  import { select, subscribe } from '@wordpress/data';
6
4
  import { getBlockType, registerBlockType, unregisterBlockType } from '@wordpress/blocks';
5
+ import slugify from 'slugify';
6
+ import classNames from 'classnames';
7
+ import debounce from 'debounce-promise';
8
+
9
+ let controller;
7
10
 
8
11
  /**
9
- * Loads select options by fetching posts from WordPress REST API.
10
- * @async
11
- * @param {string} inputValue - Search term to filter posts
12
- * @param {string} postType - WordPress post type to query
13
- * @param {Function|null} [mapper=null] - Optional function to transform API response items
14
- * @param {Object} [extraParams={}] - Additional query parameters
15
- * @returns {Promise<Array<{value: number, label: string}>>} Array of select options
12
+ * Performs the raw REST API request for loading select options.
13
+ * Automatically aborts any previous pending request.
14
+ *
15
+ * @param {string} inputValue - Search term used to filter posts.
16
+ * @param {string} postType - WordPress post type slug (e.g., "post", "page", custom type).
17
+ * @param {Function|null} [mapper=null] - Optional callback to transform each API result.
18
+ * @param {Object} [extraParams={}] - Additional query parameters for the REST API call.
19
+ * @returns {Promise<Array<{ value: number, label: string }>>} Promise resolving to an array of select options.
16
20
  */
17
- export const loadSelectOptions = async (inputValue, postType, mapper = null, extraParams = {}) => {
18
- const defaultParams = {
21
+ export const loadSelectOptionsRaw = async (inputValue, postType, mapper = null, extraParams = {}) => {
22
+ // Cancel previous request if still active
23
+ if (controller) controller.abort();
24
+ controller = new AbortController();
25
+
26
+ const defaultParams = {
19
27
  per_page: -1,
20
28
  status: 'publish',
21
29
  orderby: 'title',
22
- order: 'asc'
30
+ order: 'asc',
23
31
  };
24
32
 
25
33
  const queryParams = { ...defaultParams, ...extraParams };
26
-
27
- if (inputValue && inputValue.trim()) {
28
- queryParams.search = inputValue;
29
- }
34
+ const q = inputValue?.trim();
35
+ if (q) queryParams.search = q;
30
36
 
31
37
  const queryString = new URLSearchParams(queryParams).toString();
32
38
 
33
- const response = await apiFetch({
34
- path: `/wp/v2/${postType}?${queryString}`,
35
- });
36
-
37
- if (mapper) {
38
- return response?.map(mapper);
39
- } else {
40
- return response?.map((post) => {
41
- const tempElement = document.createElement('div');
42
- tempElement.innerHTML = post?.title?.rendered;
43
-
44
- return {
45
- value: post.id,
46
- label: tempElement.textContent || tempElement.innerText || '',
47
- };
39
+ try {
40
+ const response = await apiFetch({
41
+ path: `/wp/v2/${postType}?${queryString}`,
42
+ signal: controller.signal,
48
43
  });
44
+
45
+ const list = mapper
46
+ ? response?.map(mapper)
47
+ : response?.map((post) => {
48
+ const temp = document.createElement('div');
49
+ temp.innerHTML = post?.title?.rendered;
50
+ return {
51
+ value: post.id,
52
+ label: temp.textContent || temp.innerText || '',
53
+ };
54
+ });
55
+
56
+ return Array.isArray(list) ? list : [];
57
+ } catch (err) {
58
+ if (err?.name === 'AbortError') return [];
59
+ throw err;
49
60
  }
50
61
  };
51
62
 
63
+ /**
64
+ * Debounced, abort-safe function to fetch WordPress posts for select options.
65
+ * Combines:
66
+ * - AbortController: cancels previous in-flight requests.
67
+ * - debounce-promise: delays execution and resolves to the final async result.
68
+ *
69
+ * @example
70
+ * const options = await loadSelectOptions('John', 'team');
71
+ */
72
+ export const loadSelectOptions = debounce(loadSelectOptionsRaw, 300, {
73
+ leading: false,
74
+ trailing: true,
75
+ });
76
+
52
77
  /**
53
78
  * Converts a string to a URL-friendly slug.
54
79
  * @param {string} name - String to convert to slug