@secretstache/wordpress-gutenberg 0.6.9 → 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.9",
3
+ "version": "0.6.10",
4
4
  "description": "",
5
5
  "author": "Secret Stache",
6
6
  "license": "GPL-2.0-or-later",
@@ -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,10 +1,11 @@
1
1
  import { filters } from '@wordpress/hooks';
2
2
  import apiFetch from '@wordpress/api-fetch';
3
- import { select, subscribe } from '@wordpress/data';
3
+ import { select, subscribe, useSelect } from '@wordpress/data';
4
4
  import { getBlockType, registerBlockType, unregisterBlockType } from '@wordpress/blocks';
5
5
  import slugify from 'slugify';
6
6
  import classNames from 'classnames';
7
7
  import debounce from 'debounce-promise';
8
+ import { useMemo } from '@wordpress/element';
8
9
 
9
10
  let controller;
10
11
 
@@ -341,3 +342,40 @@ export const getFocalPointStyle = (focalPoint) => {
341
342
  return { objectPosition: `${x}% ${y}%` };
342
343
  };
343
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
+ };