@secretstache/wordpress-gutenberg 0.6.9 → 0.6.11
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/build/index.js +1154 -1132
- package/build/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/DataQueryControls.jsx +2 -6
- package/src/hooks/useTabs.jsx +3 -0
- package/src/utils/helpers.js +39 -1
package/package.json
CHANGED
|
@@ -10,7 +10,7 @@ import { arrayMove } from 'react-sortable-hoc';
|
|
|
10
10
|
import Select from 'react-select';
|
|
11
11
|
|
|
12
12
|
import { SortableSelectAsync } from './SortableSelect.jsx';
|
|
13
|
-
import { decodeHtmlEntities, loadSelectOptions } from '../utils/index.js';
|
|
13
|
+
import { decodeHtmlEntities, loadSelectOptions, useDefaultSelectOptions } from '../utils/index.js';
|
|
14
14
|
|
|
15
15
|
const DataQueryContext = createContext({});
|
|
16
16
|
|
|
@@ -192,11 +192,7 @@ const CuratedPosts = memo(({
|
|
|
192
192
|
return await loadSelectOptions(inputValue, postType);
|
|
193
193
|
}, []);
|
|
194
194
|
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
useEffect(() => {
|
|
198
|
-
loadSelectOptions('', postType).then(setDefaultPostsOptions);
|
|
199
|
-
}, []);
|
|
195
|
+
const { options: defaultPostsOptions } = useDefaultSelectOptions(postType);
|
|
200
196
|
|
|
201
197
|
const onSortEnd = useCallback(({ oldIndex, newIndex }) => {
|
|
202
198
|
const newCuratedPosts = arrayMove(curatedPosts, oldIndex, newIndex);
|
package/src/hooks/useTabs.jsx
CHANGED
|
@@ -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
|
|
package/src/utils/helpers.js
CHANGED
|
@@ -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
|
+
};
|