@secretstache/wordpress-gutenberg 0.6.8 → 0.6.10

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.10",
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",
@@ -12,6 +12,7 @@ export const useTabs = (tabsClientId, tabItemName) => {
12
12
 
13
13
  const {
14
14
  tabs,
15
+ tabsAttributes,
15
16
  tabsCount,
16
17
  tabsOrder,
17
18
 
@@ -30,6 +31,7 @@ export const useTabs = (tabsClientId, tabItemName) => {
30
31
 
31
32
  return {
32
33
  tabs: getBlock(tabsClientId)?.innerBlocks || [],
34
+ tabsAttributes: getBlock(tabsClientId)?.innerBlocks?.map((block) => block?.attributes) || [],
33
35
  tabsCount: getBlockCount(tabsClientId),
34
36
  tabsOrder: getBlockOrder(tabsClientId),
35
37
 
@@ -108,6 +110,7 @@ export const useTabs = (tabsClientId, tabItemName) => {
108
110
 
109
111
  return {
110
112
  tabs,
113
+ tabsAttributes,
111
114
  tabsCount,
112
115
  tabsOrder,
113
116
 
@@ -1,54 +1,80 @@
1
1
  import { filters } from '@wordpress/hooks';
2
2
  import apiFetch from '@wordpress/api-fetch';
3
+ import { select, subscribe, useSelect } from '@wordpress/data';
4
+ import { getBlockType, registerBlockType, unregisterBlockType } from '@wordpress/blocks';
3
5
  import slugify from 'slugify';
4
6
  import classNames from 'classnames';
5
- import { select, subscribe } from '@wordpress/data';
6
- import { getBlockType, registerBlockType, unregisterBlockType } from '@wordpress/blocks';
7
+ import debounce from 'debounce-promise';
8
+ import { useMemo } from '@wordpress/element';
9
+
10
+ let controller;
7
11
 
8
12
  /**
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
13
+ * Performs the raw REST API request for loading select options.
14
+ * Automatically aborts any previous pending request.
15
+ *
16
+ * @param {string} inputValue - Search term used to filter posts.
17
+ * @param {string} postType - WordPress post type slug (e.g., "post", "page", custom type).
18
+ * @param {Function|null} [mapper=null] - Optional callback to transform each API result.
19
+ * @param {Object} [extraParams={}] - Additional query parameters for the REST API call.
20
+ * @returns {Promise<Array<{ value: number, label: string }>>} Promise resolving to an array of select options.
16
21
  */
17
- export const loadSelectOptions = async (inputValue, postType, mapper = null, extraParams = {}) => {
18
- const defaultParams = {
22
+ export const loadSelectOptionsRaw = async (inputValue, postType, mapper = null, extraParams = {}) => {
23
+ // Cancel previous request if still active
24
+ if (controller) controller.abort();
25
+ controller = new AbortController();
26
+
27
+ const defaultParams = {
19
28
  per_page: -1,
20
29
  status: 'publish',
21
30
  orderby: 'title',
22
- order: 'asc'
31
+ order: 'asc',
23
32
  };
24
33
 
25
34
  const queryParams = { ...defaultParams, ...extraParams };
26
-
27
- if (inputValue && inputValue.trim()) {
28
- queryParams.search = inputValue;
29
- }
35
+ const q = inputValue?.trim();
36
+ if (q) queryParams.search = q;
30
37
 
31
38
  const queryString = new URLSearchParams(queryParams).toString();
32
39
 
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
- };
40
+ try {
41
+ const response = await apiFetch({
42
+ path: `/wp/v2/${postType}?${queryString}`,
43
+ signal: controller.signal,
48
44
  });
45
+
46
+ const list = mapper
47
+ ? response?.map(mapper)
48
+ : response?.map((post) => {
49
+ const temp = document.createElement('div');
50
+ temp.innerHTML = post?.title?.rendered;
51
+ return {
52
+ value: post.id,
53
+ label: temp.textContent || temp.innerText || '',
54
+ };
55
+ });
56
+
57
+ return Array.isArray(list) ? list : [];
58
+ } catch (err) {
59
+ if (err?.name === 'AbortError') return [];
60
+ throw err;
49
61
  }
50
62
  };
51
63
 
64
+ /**
65
+ * Debounced, abort-safe function to fetch WordPress posts for select options.
66
+ * Combines:
67
+ * - AbortController: cancels previous in-flight requests.
68
+ * - debounce-promise: delays execution and resolves to the final async result.
69
+ *
70
+ * @example
71
+ * const options = await loadSelectOptions('John', 'team');
72
+ */
73
+ export const loadSelectOptions = debounce(loadSelectOptionsRaw, 300, {
74
+ leading: false,
75
+ trailing: true,
76
+ });
77
+
52
78
  /**
53
79
  * Converts a string to a URL-friendly slug.
54
80
  * @param {string} name - String to convert to slug
@@ -316,3 +342,40 @@ export const getFocalPointStyle = (focalPoint) => {
316
342
  return { objectPosition: `${x}% ${y}%` };
317
343
  };
318
344
 
345
+ /**
346
+ * Default options for Select via core-data cache
347
+ *
348
+ * @param {string} postType
349
+ * @param {(post:any)=>{value:number,label:string}} [mapper]
350
+ * @param {Object} [extraParams={}]
351
+ *
352
+ * @returns {{ options: Array<{value:number,label:string}>, isResolving: boolean }}
353
+ */
354
+ export const useDefaultSelectOptions = (postType, mapper = null, extraParams = {}) => {
355
+ const query = useMemo(() => ({
356
+ per_page: 100,
357
+ status: 'publish',
358
+ order: 'asc',
359
+ orderby: 'title',
360
+ _fields: 'id,title,acf',
361
+ ...extraParams,
362
+ }), [ postType, JSON.stringify(extraParams) ]);
363
+
364
+ return useSelect((select) => {
365
+ const core = select('core');
366
+ const posts = core.getEntityRecords('postType', postType, query) || [];
367
+ const isResolving = core.isResolving('getEntityRecords', [ 'postType', postType, query ]);
368
+
369
+ const options = mapper
370
+ ? posts.map(mapper)
371
+ : posts.map((post) => ({
372
+ value: post.id,
373
+ label: decodeHtmlEntities(post?.title?.rendered || ''),
374
+ }));
375
+
376
+ return {
377
+ options,
378
+ isResolving,
379
+ };
380
+ }, [ postType, JSON.stringify(query) ]);
381
+ };