-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathedit.js
More file actions
185 lines (161 loc) · 5.26 KB
/
Copy pathedit.js
File metadata and controls
185 lines (161 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import classnames from 'classnames';
import {__, sprintf} from '@wordpress/i18n';
import {
RichText,
useBlockProps,
useInnerBlocksProps,
BlockControls,
} from '@wordpress/block-editor';
import {
Icon,
ToolbarButton,
ToolbarGroup,
} from '@wordpress/components';
import {createBlock} from '@wordpress/blocks';
import {useSelect, useDispatch} from '@wordpress/data';
import {useEffect, useState, useRef} from '@wordpress/element';
const Edit = (props) => {
const {isSelected, clientId} = props;
const contentRef = useRef(null);
const [activeTab, setActiveTab] = useState('');
const children = useSelect(select => {
const {getBlock} = select('core/block-editor');
return getBlock(clientId).innerBlocks;
});
const isParentOfSelectedBlock = useSelect(
(select) => select('core/block-editor').hasSelectedInnerBlock(clientId, true),
);
const {
insertBlock,
removeBlock,
// selectBlock,
moveBlockToPosition,
updateBlockAttributes,
} = useDispatch('core/block-editor');
// const selectTab = (blockId) => {
// if (0 < children?.length) {
// const block = children.filter(block => block.clientId === blockId)[0];
// selectBlock(block.clientId);
// }
// };
const toggleActiveTab = (blockId) => {
if (contentRef.current) {
children.forEach(block => {
const blockContent = contentRef.current.querySelector(`#block-${block.clientId} .wp-block-gds-tab__content`);
blockContent?.classList.toggle('active', block.clientId === blockId);
});
setActiveTab(blockId);
}
};
const moveTab = (blockId, position) => {
const blockClientId = children.filter(block => block.clientId === blockId)[0]?.clientId;
if (blockClientId) {
moveBlockToPosition(blockClientId, clientId, clientId, position);
}
};
const deleteTab = (blockId) => {
if (0 < children?.length) {
const block = children.filter(block => block.clientId === blockId)[0];
removeBlock(block.clientId, false);
if (activeTab === blockId) {
setActiveTab('');
}
}
};
const addTab = () => {
const itemBlock = createBlock('gds/tab', {label: sprintf(__('Tab #%d'), children.length + 1)});
insertBlock(itemBlock, (children?.length) || 0, clientId, false);
};
useEffect(() => {
// Handle breakpoint
const activeContent = contentRef.current.querySelector(`#block-${activeTab} .wp-block-gds-tab__content.active`)
if (activeTab && !activeContent) {
toggleActiveTab(activeTab);
}
});
useEffect(() => {
// Activate the first tab when no tabs are selected
if (0 < children?.length && ('' === activeTab || 0 === children?.filter(block => block.clientId === activeTab).length)) {
toggleActiveTab(children[0].clientId);
}
}, [children]);
const Controls = () => {
const index = children?.findIndex(({clientId}) => clientId === activeTab);
return (
<BlockControls>
<ToolbarGroup>
<ToolbarButton
label={__('Delete tab')}
icon="trash"
disabled={children?.length < 2}
onClick={() => deleteTab(activeTab)}
/>
<ToolbarButton
label={__('Move tab left')}
icon="arrow-left-alt2"
disabled={0 === index}
onClick={() => moveTab(activeTab, index - 1)}
/>
<ToolbarButton
label={__('Move tab right')}
icon="arrow-right-alt2"
disabled={children?.length - 1 === index}
onClick={() => moveTab(activeTab, index + 1)}
/>
</ToolbarGroup>
</BlockControls>
);
};
const blockProps = useBlockProps({
className: '',
});
const innerBlocksProps = useInnerBlocksProps({
ref: contentRef,
className: 'wp-block-gds-tabs__panels',
}, {
orientation: 'horizontal',
renderAppender: false,
allowedBlocks: ['gds/tab'],
template: [
['gds/tab', {label: sprintf(__('Tab #%d'), 1)}],
],
});
return (
<>
<Controls/>
<div {...blockProps}>
<div className="wp-block-buttons">
{children?.map((tab, index) => {
return (
<div className="wp-block-button" key={index}>
<button
className={classnames('wp-block-button__link', {'active': tab.clientId === activeTab})}
onClick={() => toggleActiveTab(tab.clientId)}
>
<RichText
tagName="span"
value={tab.attributes.label}
allowedFormats={[]}
onChange={nextLabel => {
updateBlockAttributes(tab.clientId, {label: nextLabel});
}}
placeholder={__('Enter label')}
/>
</button>
</div>
);
}) || ''}
{(isSelected || isParentOfSelectedBlock || 0 === children.length) && (
<li className="wp-block-button">
<button className="wp-block-button__link has-secondary-background-color has-background" onClick={addTab}>
<Icon icon="plus-alt2"/>
</button>
</li>
)}
</div>
<div {...innerBlocksProps}/>
</div>
</>
);
};
export default Edit;