@secretstache/wordpress-gutenberg 0.3.6 → 0.3.8
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 +3 -3
- package/build/index.js.map +1 -1
- package/package.json +1 -1
- package/src/hooks/index.js +1 -1
- package/src/hooks/useDataQuery.js +3 -0
- package/src/hooks/useTabs.js +97 -0
- package/src/utils/attributes.js +27 -12
- package/src/hooks/useBlockTabsData.js +0 -90
package/package.json
CHANGED
package/src/hooks/index.js
CHANGED
@@ -6,7 +6,7 @@ export { useThemeColors } from './useThemeColors';
|
|
6
6
|
export { useUpdateAttribute } from './useUpdateAttribute';
|
7
7
|
export { useDataQuery } from './useDataQuery';
|
8
8
|
export { useAccordionItem } from './useAccordionItem.js';
|
9
|
-
export {
|
9
|
+
export { useTabs } from './useTabs.js';
|
10
10
|
export { useAllowedBlocks } from './useAllowedBlocks.js';
|
11
11
|
export { useChildBlockPosition } from './useChildBlockPosition.js';
|
12
12
|
export { useFilterBlocks } from './useFilterBlocks.js';
|
@@ -37,9 +37,12 @@ export const useDataQuery = (props) => {
|
|
37
37
|
const isResolving = select('core/data').isResolving('core', 'getEntityRecords', ['postType', postType, queryArgs]);
|
38
38
|
const isLoading = isResolving || postsToShow === undefined;
|
39
39
|
|
40
|
+
const isEmpty = postsToShow !== null && postsToShow?.length === 0;
|
41
|
+
|
40
42
|
return {
|
41
43
|
postsToShow,
|
42
44
|
isLoading,
|
45
|
+
isEmpty,
|
43
46
|
};
|
44
47
|
}, dependencies);
|
45
48
|
}
|
@@ -0,0 +1,97 @@
|
|
1
|
+
import { useSelect, useDispatch } from '@wordpress/data';
|
2
|
+
import { store as blockEditorStore } from '@wordpress/block-editor';
|
3
|
+
import { useLayoutEffect, useState } from '@wordpress/element';
|
4
|
+
import { Button } from '@wordpress/components';
|
5
|
+
import { plus as plusIcon } from '@wordpress/icons';
|
6
|
+
import { createBlock } from '@wordpress/blocks';
|
7
|
+
|
8
|
+
import { useParentBlock } from './useParentBlock';
|
9
|
+
|
10
|
+
export const useTabs = (tabsClientId, tabItemName) => {
|
11
|
+
const { insertBlock } = useDispatch(blockEditorStore);
|
12
|
+
|
13
|
+
const {
|
14
|
+
tabs,
|
15
|
+
tabsCount,
|
16
|
+
tabsOrder,
|
17
|
+
|
18
|
+
selectedBlock,
|
19
|
+
selectedBlockClientId,
|
20
|
+
} = useSelect(
|
21
|
+
(select) => {
|
22
|
+
const {
|
23
|
+
getBlock,
|
24
|
+
getBlockCount,
|
25
|
+
getSelectedBlock,
|
26
|
+
getSelectedBlockClientId,
|
27
|
+
getBlockOrder,
|
28
|
+
getBlockRootClientId,
|
29
|
+
} = select(blockEditorStore);
|
30
|
+
|
31
|
+
return {
|
32
|
+
tabs: getBlock(tabsClientId)?.innerBlocks || [],
|
33
|
+
tabsCount: getBlockCount(tabsClientId),
|
34
|
+
tabsOrder: getBlockOrder(tabsClientId),
|
35
|
+
|
36
|
+
selectedBlock: getSelectedBlock(),
|
37
|
+
selectedBlockClientId: getSelectedBlockClientId(),
|
38
|
+
};
|
39
|
+
},
|
40
|
+
[ tabsClientId ],
|
41
|
+
);
|
42
|
+
|
43
|
+
const [ activeItemId, setActiveItemId ] = useState(null);
|
44
|
+
|
45
|
+
useLayoutEffect(() => {
|
46
|
+
if (tabs.length > 0 && !activeItemId) {
|
47
|
+
setActiveItemId(tabs[0].clientId);
|
48
|
+
}
|
49
|
+
}, [ tabs, activeItemId ]);
|
50
|
+
|
51
|
+
const parentBlock = useParentBlock(tabItemName, tabsClientId);
|
52
|
+
|
53
|
+
useLayoutEffect(() => {
|
54
|
+
if (parentBlock) {
|
55
|
+
setActiveItemId(parentBlock.clientId);
|
56
|
+
}
|
57
|
+
}, [ parentBlock ]);
|
58
|
+
|
59
|
+
const createTab = (blockName, attributes = {}, position = tabsCount) => {
|
60
|
+
const newTab = createBlock(blockName, attributes);
|
61
|
+
insertBlock(newTab, position, tabsClientId);
|
62
|
+
|
63
|
+
return newTab;
|
64
|
+
};
|
65
|
+
|
66
|
+
const onTabAppenderClick = () => {
|
67
|
+
const newItem = createTab(tabItemName);
|
68
|
+
setActiveItemId(newItem.clientId);
|
69
|
+
};
|
70
|
+
|
71
|
+
const TabAppender = ({ label = 'Add new tab', ...other }) => (
|
72
|
+
<Button
|
73
|
+
className="bc-add-new-child-btn"
|
74
|
+
icon={plusIcon}
|
75
|
+
label={label}
|
76
|
+
onClick={onTabAppenderClick}
|
77
|
+
{...other}
|
78
|
+
/>
|
79
|
+
);
|
80
|
+
|
81
|
+
return {
|
82
|
+
tabs,
|
83
|
+
tabsCount,
|
84
|
+
tabsOrder,
|
85
|
+
|
86
|
+
selectedBlock,
|
87
|
+
selectedBlockClientId,
|
88
|
+
parentBlock,
|
89
|
+
|
90
|
+
activeItemId,
|
91
|
+
setActiveItemId,
|
92
|
+
|
93
|
+
TabAppender,
|
94
|
+
onTabAppenderClick,
|
95
|
+
createTab,
|
96
|
+
};
|
97
|
+
};
|
package/src/utils/attributes.js
CHANGED
@@ -96,6 +96,7 @@ export const numberOfPostsAttribute = {
|
|
96
96
|
},
|
97
97
|
};
|
98
98
|
|
99
|
+
// TODO: make dataSource optional
|
99
100
|
export const getDataQueryAttributes = (
|
100
101
|
sourcesList,
|
101
102
|
queriesList,
|
@@ -135,21 +136,38 @@ export const getDataQueryAttributes = (
|
|
135
136
|
/**
|
136
137
|
* Returns the base background attribute object with configurable default background color and media type.
|
137
138
|
*
|
138
|
-
* @param {
|
139
|
-
* @param {
|
140
|
-
* @param {
|
141
|
-
* @param {string} defaultBackgroundColor.
|
139
|
+
* @param {Object} options - The options for configuring the background attributes.
|
140
|
+
* @param {string} [options.defaultBackgroundMediaType=''] - The default background media type.
|
141
|
+
* @param {Object} options.defaultBackgroundColor - The background color configuration.
|
142
|
+
* @param {string} options.defaultBackgroundColor.value - The hex value of the background color.
|
143
|
+
* @param {string} options.defaultBackgroundColor.slug - The slug of the background color.
|
144
|
+
* @param {boolean} [options.hasIncludeBackgroundMediaAttribute=false] - Whether to include the background media attribute.
|
145
|
+
* @param {boolean} [options.hasIncludeOverlayAttribute=false] - Whether to include the overlay attribute.
|
142
146
|
* @returns {Object} The base background attribute object.
|
143
147
|
*/
|
144
|
-
export const getBaseBackgroundAttributes = (
|
148
|
+
export const getBaseBackgroundAttributes = ({
|
145
149
|
defaultBackgroundMediaType = '',
|
146
150
|
defaultBackgroundColor = { value: '', slug: '' },
|
147
|
-
|
148
|
-
|
151
|
+
hasIncludeBackgroundMediaAttribute = false,
|
152
|
+
hasIncludeOverlayAttribute = false,
|
153
|
+
} = {}) => {
|
154
|
+
const isIncludeOverlayAttribute = {
|
155
|
+
isIncludeOverlay: {
|
156
|
+
type: 'boolean',
|
157
|
+
default: false,
|
158
|
+
}
|
159
|
+
};
|
160
|
+
|
161
|
+
const isIncludeBackgroundMediaAttribute = {
|
149
162
|
isIncludeBackgroundMedia: {
|
150
163
|
type: 'boolean',
|
151
164
|
default: false,
|
152
|
-
}
|
165
|
+
}
|
166
|
+
};
|
167
|
+
|
168
|
+
return {
|
169
|
+
...(hasIncludeBackgroundMediaAttribute ? isIncludeBackgroundMediaAttribute : {}),
|
170
|
+
|
153
171
|
backgroundMediaType: {
|
154
172
|
type: 'string',
|
155
173
|
default: defaultBackgroundMediaType,
|
@@ -161,10 +179,7 @@ export const getBaseBackgroundAttributes = (
|
|
161
179
|
|
162
180
|
...mediaAttribute,
|
163
181
|
|
164
|
-
|
165
|
-
type: 'boolean',
|
166
|
-
default: false,
|
167
|
-
},
|
182
|
+
...(hasIncludeOverlayAttribute ? isIncludeOverlayAttribute : {}),
|
168
183
|
};
|
169
184
|
};
|
170
185
|
|
@@ -1,90 +0,0 @@
|
|
1
|
-
import { useSelect, useDispatch } from '@wordpress/data';
|
2
|
-
import { store as blockEditorStore } from '@wordpress/block-editor';
|
3
|
-
import { useLayoutEffect, useState } from '@wordpress/element';
|
4
|
-
import { Button } from '@wordpress/components';
|
5
|
-
import { plus as plusIcon } from '@wordpress/icons';
|
6
|
-
import { createBlock } from '@wordpress/blocks';
|
7
|
-
|
8
|
-
import { useParentBlock } from './useParentBlock';
|
9
|
-
|
10
|
-
export const useBlockTabsData = (clientId, itemBlockName) => {
|
11
|
-
const { insertBlock } = useDispatch(blockEditorStore);
|
12
|
-
|
13
|
-
const {
|
14
|
-
childBlocks,
|
15
|
-
innerBlocksCount,
|
16
|
-
selectedBlock,
|
17
|
-
selectedBlockClientId,
|
18
|
-
parentBlockId,
|
19
|
-
getBlockRootClientId,
|
20
|
-
} = useSelect(
|
21
|
-
(select) => {
|
22
|
-
const {
|
23
|
-
getBlock,
|
24
|
-
getBlockCount,
|
25
|
-
getSelectedBlock,
|
26
|
-
getSelectedBlockClientId,
|
27
|
-
getBlockRootClientId,
|
28
|
-
} = select(blockEditorStore);
|
29
|
-
|
30
|
-
return {
|
31
|
-
childBlocks: getBlock(clientId)?.innerBlocks || [],
|
32
|
-
innerBlocksCount: getBlockCount(clientId),
|
33
|
-
selectedBlock: getSelectedBlock(),
|
34
|
-
selectedBlockClientId: getSelectedBlockClientId(),
|
35
|
-
parentBlockId: getBlockRootClientId(getSelectedBlockClientId()),
|
36
|
-
getBlockRootClientId,
|
37
|
-
};
|
38
|
-
},
|
39
|
-
[clientId]
|
40
|
-
);
|
41
|
-
|
42
|
-
const [activeItemId, setActiveItemId] = useState(null);
|
43
|
-
|
44
|
-
useLayoutEffect(() => {
|
45
|
-
if (childBlocks.length > 0 && !activeItemId) {
|
46
|
-
setActiveItemId(childBlocks[0].clientId);
|
47
|
-
}
|
48
|
-
}, [childBlocks, activeItemId]);
|
49
|
-
|
50
|
-
const parentItem = useParentBlock(selectedBlock?.clientId, clientId);
|
51
|
-
|
52
|
-
useLayoutEffect(() => {
|
53
|
-
if (parentItem) {
|
54
|
-
setActiveItemId(parentItem.clientId);
|
55
|
-
} else if (clientId === parentBlockId && selectedBlock?.clientId) {
|
56
|
-
setActiveItemId(selectedBlock.clientId);
|
57
|
-
}
|
58
|
-
}, [selectedBlock, parentItem, clientId, parentBlockId]);
|
59
|
-
|
60
|
-
// TODO: export
|
61
|
-
const addNewChildBlock = (blockName, attributes = {}, position = innerBlocksCount) => {
|
62
|
-
const newBlock = createBlock(blockName, attributes);
|
63
|
-
insertBlock(newBlock, position, clientId);
|
64
|
-
|
65
|
-
return newBlock;
|
66
|
-
};
|
67
|
-
|
68
|
-
// TODO: rename/export
|
69
|
-
const handleAddNewItem = () => {
|
70
|
-
const newItem = addNewChildBlock(itemBlockName);
|
71
|
-
setActiveItemId(newItem.clientId);
|
72
|
-
};
|
73
|
-
|
74
|
-
// TODO: make more flexible
|
75
|
-
const AddNewTabButton = ({ label = 'Add new tab' }) => (
|
76
|
-
<Button className="add-new-child-btn" icon={plusIcon} label={label} onClick={handleAddNewItem} />
|
77
|
-
);
|
78
|
-
|
79
|
-
return {
|
80
|
-
childBlocks,
|
81
|
-
innerBlocksCount,
|
82
|
-
selectedBlock,
|
83
|
-
selectedBlockClientId,
|
84
|
-
parentBlockId,
|
85
|
-
activeItemId,
|
86
|
-
setActiveItemId,
|
87
|
-
getBlockRootClientId,
|
88
|
-
AddNewTabButton,
|
89
|
-
};
|
90
|
-
};
|