@secretstache/wordpress-gutenberg 0.3.7 → 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@secretstache/wordpress-gutenberg",
3
- "version": "0.3.7",
3
+ "version": "0.3.8",
4
4
  "description": "",
5
5
  "author": "Secret Stache",
6
6
  "license": "GPL-2.0-or-later",
@@ -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 { useBlockTabsData } from './useBlockTabsData.js';
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';
@@ -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
+ };
@@ -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,
@@ -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
- };