@secretstache/wordpress-gutenberg 0.5.13 → 0.5.15

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/styles.css CHANGED
@@ -54,32 +54,46 @@
54
54
  display: none; }
55
55
  .editor-styles-wrapper .block-editor-block-list__layout.is-root-container .empty-block-appender {
56
56
  --button-color: #192f5f;
57
- --button-border: var(--button-color);
58
- --button-hover-color: var(--wp-admin-theme-color);
59
- --button-hover-border: var(--wp-admin-theme-color);
57
+ --button-color-hover: var(--wp-admin-theme-color);
58
+ --button-border: #192f5f;
59
+ --button-border-hover: var(--wp-admin-theme-color);
60
+ --button-background: transparent;
61
+ --button-background-hover: transparent;
62
+ --text-color: #192f5f;
60
63
  --tr: .3s;
61
64
  display: flex;
62
65
  flex-direction: column;
63
66
  align-items: center;
64
67
  justify-content: center;
65
68
  padding: 32px; }
69
+ .editor-styles-wrapper .block-editor-block-list__layout.is-root-container .empty-block-appender--light {
70
+ --button-color: #fff;
71
+ --button-color-hover: #192f5f;
72
+ --button-border: #fff;
73
+ --button-border-hover: #fff;
74
+ --button-background: transparent;
75
+ --button-background-hover: #fff;
76
+ --text-color: #fff; }
66
77
  .editor-styles-wrapper .block-editor-block-list__layout.is-root-container .empty-block-appender__content {
78
+ display: flex;
79
+ flex-direction: column;
80
+ align-items: center;
67
81
  text-align: center; }
68
82
  .editor-styles-wrapper .block-editor-block-list__layout.is-root-container .empty-block-appender__icon {
69
83
  width: 48px;
70
84
  height: 48px;
71
- color: var(--button-color);
85
+ color: var(--text-color);
72
86
  margin-bottom: 16px; }
73
87
  .editor-styles-wrapper .block-editor-block-list__layout.is-root-container .empty-block-appender__title {
74
88
  font-size: 32px;
75
89
  font-weight: 600;
76
90
  line-height: 110%;
77
- color: var(--button-color);
91
+ color: var(--text-color);
78
92
  margin-bottom: 0.5rem; }
79
93
  .editor-styles-wrapper .block-editor-block-list__layout.is-root-container .empty-block-appender__text {
80
94
  font-size: 1rem;
81
95
  font-weight: 400;
82
- color: var(--button-color);
96
+ color: var(--text-color);
83
97
  margin-bottom: 1.5rem; }
84
98
  .editor-styles-wrapper .block-editor-block-list__layout.is-root-container .empty-block-appender__button .block-editor-button-block-appender {
85
99
  display: inline-flex;
@@ -90,7 +104,7 @@
90
104
  border: 2px solid var(--button-border) !important;
91
105
  box-shadow: none;
92
106
  border-radius: 10px;
93
- background: transparent !important;
107
+ background: var(--button-background) !important;
94
108
  padding: 12px 24px;
95
109
  min-height: 50px;
96
110
  min-width: 200px;
@@ -98,10 +112,11 @@
98
112
  font-weight: 600;
99
113
  font-size: 22px; }
100
114
  .editor-styles-wrapper .block-editor-block-list__layout.is-root-container .empty-block-appender__button .block-editor-button-block-appender:hover, .editor-styles-wrapper .block-editor-block-list__layout.is-root-container .empty-block-appender__button .block-editor-button-block-appender:focus {
101
- color: var(--button-hover-color);
102
- border-color: var(--button-hover-border) !important;
115
+ color: var(--button-color-hover) !important;
116
+ border-color: var(--button-border-hover) !important;
103
117
  outline: none !important;
104
- box-shadow: none !important; }
118
+ box-shadow: none !important;
119
+ background: var(--button-background-hover) !important; }
105
120
  .editor-styles-wrapper .block-editor-block-list__layout.is-root-container .empty-block-appender__button .block-editor-button-block-appender::before, .editor-styles-wrapper .block-editor-block-list__layout.is-root-container .empty-block-appender__button .block-editor-button-block-appender::after {
106
121
  display: none !important; }
107
122
  .editor-styles-wrapper .block-editor-block-list__layout.is-root-container .empty-block-appender__button .block-editor-button-block-appender .block-editor-button-block-appender__content {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@secretstache/wordpress-gutenberg",
3
- "version": "0.5.13",
3
+ "version": "0.5.15",
4
4
  "description": "",
5
5
  "author": "Secret Stache",
6
6
  "license": "GPL-2.0-or-later",
@@ -1,53 +1,365 @@
1
- import { RadioControl } from '@wordpress/components';
1
+ import { createContext, useCallback, useContext, useState, useEffect, useMemo, memo } from '@wordpress/element';
2
+ import {
3
+ __experimentalDivider as Divider,
4
+ RangeControl,
5
+ RadioControl,
6
+ SelectControl,
7
+ } from '@wordpress/components';
8
+ import { useSelect } from '@wordpress/data';
9
+ import { arrayMove } from 'react-sortable-hoc';
10
+ import Select from 'react-select';
2
11
 
3
- // TODO: 1. add support of curated posts, categories
4
- // TODO: 2. consider merging with the useDataQuery hook
5
- // TODO: 3. can't pass disabled prop to RadioControl
6
- export const DataQueryControls = (props) => {
7
- const {
8
- dataSourceLabel = 'Data Source',
9
- dataSource,
10
- onDataSourceChange,
12
+ import { SortableSelectAsync } from './SortableSelect.js';
13
+ import { decodeHtmlEntities, loadSelectOptions } from '../utils/index.js';
11
14
 
12
- queryTypeLabel = 'Query',
13
- queryType,
14
- onQueryTypeChange,
15
+ const DataQueryContext = createContext({});
15
16
 
16
- settings,
17
- } = props;
17
+ /**
18
+ * Main DataQueryControls component that provides context to children
19
+ */
20
+ export const DataQueryControls = memo(({
21
+ attributes,
22
+ setAttributes,
23
+ children,
24
+ }) => {
25
+ const updateAttribute = useCallback((name, value) => {
26
+ setAttributes({ [name]: value });
27
+ }, []);
18
28
 
19
- const sourcesList = settings
20
- .filter((source) => source?.value && source?.label)
21
- .map((source) => ({ label: source.label, value: source.value }));
29
+ const contextValue = useMemo(() => ({
30
+ attributes,
31
+ setAttributes,
32
+ updateAttribute,
33
+ }), [ attributes ]);
22
34
 
23
- const queriesList = settings.find((source) => source.value === dataSource)?.queries || [];
35
+ return (
36
+ <DataQueryContext.Provider value={contextValue}>
37
+ {children}
38
+ </DataQueryContext.Provider>
39
+ );
40
+ });
41
+
42
+ /**
43
+ * Hook to access the query controls context
44
+ */
45
+ const useDataQueryContext = () => {
46
+ const context = useContext(DataQueryContext);
47
+
48
+ if (!context) {
49
+ throw new Error('Query control components must be used within a DataQueryControls component');
50
+ }
51
+
52
+ return context;
53
+ };
54
+
55
+ /**
56
+ * QueryType component for selecting the query type
57
+ */
58
+ const QueryType = memo(({
59
+ label = 'Query',
60
+ options = [],
61
+ onChange,
62
+ disabled = false,
63
+ help = '',
64
+ attributeName = 'queryType',
65
+ render = null,
66
+ }) => {
67
+ const { attributes, updateAttribute } = useDataQueryContext();
68
+ const queryType = attributes[attributeName];
69
+
70
+ const handleChange = useCallback((value) => {
71
+ updateAttribute(attributeName, value);
72
+
73
+ if (onChange) {
74
+ onChange(value);
75
+ }
76
+ }, [ attributeName, onChange ]);
77
+
78
+ if (render) {
79
+ return render({
80
+ value: queryType,
81
+ onChange: handleChange,
82
+ options,
83
+ disabled,
84
+ attributeName,
85
+ });
86
+ }
87
+
88
+ return (
89
+ <>
90
+ <RadioControl
91
+ label={label}
92
+ selected={queryType}
93
+ options={options}
94
+ onChange={handleChange}
95
+ disabled={disabled}
96
+ help={help}
97
+ />
98
+
99
+ <Divider />
100
+ </>
101
+ );
102
+ });
103
+
104
+ /**
105
+ * TaxonomySelect component for taxonomy-based filtering
106
+ */
107
+ const TaxonomySelect = memo(({
108
+ condition = true,
109
+ attributeName = 'selectedCategories',
110
+ taxonomy = 'category',
111
+ placeholder = 'Categories to show',
112
+ disabled = false,
113
+ render = null,
114
+ customOptions = null,
115
+ }) => {
116
+ const { attributes, updateAttribute } = useDataQueryContext();
117
+
118
+ const taxonomyTerms = useSelect((select) => {
119
+ if (customOptions) return null;
120
+
121
+ return select('core').getEntityRecords('taxonomy', taxonomy, { per_page: -1 });
122
+ }, [ taxonomy, customOptions ]);
123
+
124
+ const selectOptions = useMemo(() => {
125
+ if (customOptions) return customOptions;
126
+
127
+ return taxonomyTerms?.map(term => ({
128
+ value: term.id,
129
+ label: decodeHtmlEntities(term.name),
130
+ })) || [];
131
+ }, [ taxonomyTerms, customOptions ]);
132
+
133
+ const selectedTermIds = attributes[attributeName] || [];
134
+
135
+ const selectedOptions = useMemo(() => {
136
+ return selectedTermIds
137
+ .map(id => selectOptions.find(option => option.value === id))
138
+ .filter(Boolean);
139
+ }, [ selectedTermIds, selectOptions ]);
140
+
141
+ const onChange = useCallback((newSelected) => {
142
+ const newSelectedIds = newSelected ? newSelected.map(option => option.value) : [];
143
+ updateAttribute(attributeName, newSelectedIds);
144
+ }, [ attributeName ]);
145
+
146
+ if (!condition || !taxonomy) return null;
147
+
148
+ if (render) {
149
+ return render({
150
+ value: selectedOptions,
151
+ onChange,
152
+ options: selectOptions,
153
+ placeholder,
154
+ disabled,
155
+ attributeName,
156
+ });
157
+ }
158
+
159
+ return (
160
+ <>
161
+ <Select
162
+ isMulti
163
+ options={selectOptions}
164
+ value={selectedOptions}
165
+ onChange={onChange}
166
+ className="react-select-container"
167
+ classNamePrefix="react-select"
168
+ placeholder={placeholder}
169
+ isDisabled={disabled}
170
+ />
171
+
172
+ <Divider />
173
+ </>
174
+ );
175
+ });
24
176
 
25
- const hasSources = sourcesList && sourcesList?.length > 0;
26
- const hasQueries = queriesList && queriesList?.length > 0;
177
+ /**
178
+ * CuratedPosts component for selecting individual posts
179
+ */
180
+ const CuratedPosts = memo(({
181
+ condition = true,
182
+ attributeName = 'curatedPosts',
183
+ postType = 'posts',
184
+ placeholder = 'Posts to show',
185
+ disabled = false,
186
+ render = null,
187
+ }) => {
188
+ const { attributes, updateAttribute } = useDataQueryContext();
189
+ const curatedPosts = attributes[attributeName] || [];
190
+
191
+ const loadPostsOptions = useCallback(async (inputValue) => {
192
+ return await loadSelectOptions(inputValue, postType);
193
+ }, []);
194
+
195
+ const [ defaultPostsOptions, setDefaultPostsOptions ] = useState([]);
196
+
197
+ useEffect(() => {
198
+ loadSelectOptions('', postType).then(setDefaultPostsOptions);
199
+ }, []);
200
+
201
+ const onSortEnd = useCallback(({ oldIndex, newIndex }) => {
202
+ const newCuratedPosts = arrayMove(curatedPosts, oldIndex, newIndex);
203
+ updateAttribute(attributeName, newCuratedPosts);
204
+ }, [ curatedPosts, attributeName ]);
205
+
206
+ const onChange = useCallback((newValue) => {
207
+ updateAttribute(attributeName, newValue);
208
+ }, [ attributeName ]);
209
+
210
+ if (!condition) return null;
211
+
212
+ if (render) {
213
+ return render({
214
+ value: curatedPosts,
215
+ onChange,
216
+ onSortEnd,
217
+ loadOptions: loadPostsOptions,
218
+ defaultOptions: defaultPostsOptions,
219
+ placeholder,
220
+ disabled,
221
+ attributeName,
222
+ });
223
+ }
224
+
225
+ return (
226
+ <>
227
+ <SortableSelectAsync
228
+ onSortEnd={onSortEnd}
229
+ value={curatedPosts}
230
+ defaultOptions={defaultPostsOptions}
231
+ loadOptions={loadPostsOptions}
232
+ onChange={onChange}
233
+ placeholder={placeholder}
234
+ isDisabled={disabled}
235
+ />
236
+ <Divider />
237
+ </>
238
+ );
239
+ });
240
+
241
+ /**
242
+ * NumberOfPosts component for limiting the number of posts
243
+ */
244
+ const NumberOfPosts = memo(({
245
+ condition = true,
246
+ attributeName = 'numberOfPosts',
247
+ label = 'Number of Posts',
248
+ min = -1,
249
+ max = 12,
250
+ help = 'The maximum number of posts to show (-1 for no limit)',
251
+ disabled = false,
252
+ render = null,
253
+ }) => {
254
+ const { attributes, updateAttribute } = useDataQueryContext();
255
+ const numberOfPosts = attributes[attributeName];
256
+
257
+ const onChange = useCallback((value) => {
258
+ updateAttribute(attributeName, value);
259
+ }, [ attributeName ]);
260
+
261
+ if (!condition) return null;
262
+
263
+ if (render) {
264
+ return render({
265
+ value: numberOfPosts,
266
+ onChange,
267
+ min,
268
+ max,
269
+ help,
270
+ disabled,
271
+ attributeName,
272
+ });
273
+ }
274
+
275
+ return (
276
+ <>
277
+ <RangeControl
278
+ label={label}
279
+ value={numberOfPosts}
280
+ onChange={onChange}
281
+ min={min}
282
+ max={max}
283
+ help={help}
284
+ disabled={disabled}
285
+ />
286
+
287
+ <Divider />
288
+ </>
289
+ );
290
+ });
291
+
292
+ /**
293
+ * OrderByControl component for controlling ordering
294
+ */
295
+ const OrderByControl = memo(({
296
+ condition = true,
297
+ orderByAttributeName = 'orderBy',
298
+ orderAttributeName = 'order',
299
+ disabled = false,
300
+ orderByOptions = [
301
+ { label: 'Date', value: 'date' },
302
+ { label: 'Title', value: 'title' },
303
+ ],
304
+ orderOptions = [
305
+ { label: 'Ascending', value: 'asc' },
306
+ { label: 'Descending', value: 'desc' },
307
+ ],
308
+ render = null,
309
+ }) => {
310
+ const { attributes, updateAttribute } = useDataQueryContext();
311
+ const orderBy = attributes[orderByAttributeName] || 'date';
312
+ const order = attributes[orderAttributeName] || 'desc';
313
+
314
+ const onOrderByChange = useCallback((value) => {
315
+ updateAttribute(orderByAttributeName, value);
316
+ }, [ orderByAttributeName ]);
317
+
318
+ const onOrderChange = useCallback((value) => {
319
+ updateAttribute(orderAttributeName, value);
320
+ }, [ orderAttributeName ]);
321
+
322
+ if (!condition) return null;
323
+
324
+ if (render) {
325
+ return render({
326
+ orderBy,
327
+ order,
328
+ onOrderByChange,
329
+ onOrderChange,
330
+ orderByOptions,
331
+ orderOptions,
332
+ disabled,
333
+ orderByAttributeName,
334
+ orderAttributeName,
335
+ });
336
+ }
27
337
 
28
338
  return (
29
339
  <>
30
- {
31
- hasSources && (
32
- <RadioControl
33
- label={dataSourceLabel}
34
- selected={dataSource}
35
- options={sourcesList}
36
- onChange={onDataSourceChange}
37
- />
38
- )
39
- }
40
-
41
- {
42
- hasQueries && (
43
- <RadioControl
44
- label={queryTypeLabel}
45
- selected={queryType}
46
- options={queriesList}
47
- onChange={onQueryTypeChange}
48
- />
49
- )
50
- }
340
+ <SelectControl
341
+ label="Order By"
342
+ value={orderBy}
343
+ options={orderByOptions}
344
+ onChange={onOrderByChange}
345
+ disabled={disabled}
346
+ />
347
+
348
+ <SelectControl
349
+ label="Order"
350
+ value={order}
351
+ options={orderOptions}
352
+ onChange={onOrderChange}
353
+ disabled={disabled}
354
+ />
355
+
356
+ <Divider />
51
357
  </>
52
- )
53
- }
358
+ );
359
+ });
360
+
361
+ DataQueryControls.QueryType = QueryType;
362
+ DataQueryControls.TaxonomySelect = TaxonomySelect;
363
+ DataQueryControls.CuratedPosts = CuratedPosts;
364
+ DataQueryControls.NumberOfPosts = NumberOfPosts;
365
+ DataQueryControls.OrderBy = OrderByControl;
@@ -7,11 +7,12 @@ export const EmptyBlockAppender = (props) => {
7
7
  showAppender = true,
8
8
  title = 'This block is empty',
9
9
  text = 'Use the "+" button below to add content blocks',
10
+ isLight = false,
10
11
  className,
11
12
  } = props;
12
13
 
13
14
  return (
14
- <div className={classNames('empty-block-appender', className)}>
15
+ <div className={classNames('empty-block-appender', className, { 'empty-block-appender--light': isLight })}>
15
16
  <div className="empty-block-appender__content">
16
17
  {
17
18
  showIcon && (
@@ -1,4 +1,4 @@
1
- import { useEffect, useRef } from '@wordpress/element';
1
+ import { useCallback, useEffect, useMemo, useRef } from '@wordpress/element';
2
2
 
3
3
  export const useAccordionItem = ({
4
4
  itemId,
@@ -7,26 +7,26 @@ export const useAccordionItem = ({
7
7
  contentSelector,
8
8
  heightObserverDeps = []
9
9
  }) => {
10
- const isActive = itemId === activeItemId;
10
+ const isActive = useMemo(() => itemId === activeItemId, [ itemId, activeItemId ]);
11
11
  const blockRef = useRef(null);
12
12
 
13
- const openContent = () => {
13
+ const openContent = useCallback(() => {
14
14
  const content = blockRef.current?.querySelector(contentSelector);
15
15
  if (content) {
16
16
  content.style.maxHeight = content.scrollHeight + 'px';
17
17
  }
18
- };
18
+ }, [ contentSelector ]);
19
19
 
20
- const closeContent = () => {
20
+ const closeContent = useCallback(() => {
21
21
  const content = blockRef.current?.querySelector(contentSelector);
22
22
  if (content) {
23
23
  content.style.maxHeight = 0;
24
24
  }
25
- };
25
+ }, [ contentSelector ]);
26
26
 
27
- const toggleItem = () => {
27
+ const toggleItem = useCallback(() => {
28
28
  setActiveItemId(isActive ? null : itemId);
29
- };
29
+ }, [ isActive, setActiveItemId ]);
30
30
 
31
31
  useEffect(() => {
32
32
  if (isActive) {
@@ -1,8 +1,11 @@
1
1
  .empty-block-appender {
2
2
  --button-color: #192f5f;
3
- --button-border: var(--button-color);
4
- --button-hover-color: var(--wp-admin-theme-color);
5
- --button-hover-border: var(--wp-admin-theme-color);
3
+ --button-color-hover: var(--wp-admin-theme-color);
4
+ --button-border: #192f5f;
5
+ --button-border-hover: var(--wp-admin-theme-color);
6
+ --button-background: transparent;
7
+ --button-background-hover: transparent;
8
+ --text-color: #192f5f;
6
9
  --tr: .3s;
7
10
 
8
11
  display: flex;
@@ -11,14 +14,27 @@
11
14
  justify-content: center;
12
15
  padding: 32px;
13
16
 
17
+ &--light {
18
+ --button-color: #fff;
19
+ --button-color-hover: #192f5f;
20
+ --button-border: #fff;
21
+ --button-border-hover: #fff;
22
+ --button-background: transparent;
23
+ --button-background-hover: #fff;
24
+ --text-color: #fff;
25
+ }
26
+
14
27
  &__content {
28
+ display: flex;
29
+ flex-direction: column;
30
+ align-items: center;
15
31
  text-align: center;
16
32
  }
17
33
 
18
34
  &__icon {
19
35
  width: 48px;
20
36
  height:48px;
21
- color: var(--button-color);
37
+ color: var(--text-color);
22
38
  margin-bottom: 16px;
23
39
  }
24
40
 
@@ -26,14 +42,14 @@
26
42
  font-size: 32px;
27
43
  font-weight: 600;
28
44
  line-height: 110%;
29
- color: var(--button-color);
45
+ color: var(--text-color);
30
46
  margin-bottom: 0.5rem;
31
47
  }
32
48
 
33
49
  &__text {
34
50
  font-size: 1rem;
35
51
  font-weight: 400;
36
- color: var(--button-color);
52
+ color: var(--text-color);
37
53
  margin-bottom: 1.5rem;
38
54
  }
39
55
 
@@ -49,7 +65,7 @@
49
65
  border: 2px solid var(--button-border) !important;
50
66
  box-shadow: none;
51
67
  border-radius: 10px;
52
- background: transparent !important;
68
+ background: var(--button-background) !important;
53
69
  padding: 12px 24px;
54
70
  min-height: 50px;
55
71
  min-width: 200px;
@@ -59,10 +75,11 @@
59
75
 
60
76
  &:hover,
61
77
  &:focus {
62
- color: var(--button-hover-color);
63
- border-color: var(--button-hover-border) !important;
78
+ color: var(--button-color-hover) !important;
79
+ border-color: var(--button-border-hover) !important;
64
80
  outline: none !important;
65
81
  box-shadow: none !important;
82
+ background: var(--button-background-hover) !important;
66
83
  }
67
84
 
68
85
  &::before,
@@ -0,0 +1,40 @@
1
+ import { createHigherOrderComponent } from '@wordpress/compose';
2
+ import { dispatch, select, useSelect } from '@wordpress/data';
3
+ import { useEffect } from '@wordpress/element';
4
+ import { addFilter } from '@wordpress/hooks';
5
+
6
+ export const addInnerBlocksCleanupFilter = (blockName) => {
7
+ // eslint-disable-next-line
8
+ const withInnerBlocksCleanup = createHigherOrderComponent((BlockEdit) => (props) => {
9
+ const { clientId, attributes, name } = props;
10
+
11
+ if (name !== blockName) {
12
+ return <BlockEdit {...props} />;
13
+ }
14
+
15
+ const innerBlocks = useSelect(
16
+ (select) => select('core/block-editor').getBlock(clientId)?.innerBlocks || [],
17
+ []
18
+ );
19
+
20
+ useEffect(() => {
21
+ if (
22
+ attributes.dataSource !== 'none' &&
23
+ innerBlocks.length > 0
24
+ ) {
25
+ dispatch('core/block-editor').updateBlock(clientId, {
26
+ ...select('core/block-editor').getBlock(clientId),
27
+ innerBlocks: [],
28
+ });
29
+ }
30
+ }, [ attributes.dataSource, clientId, innerBlocks.length ]);
31
+
32
+ return <BlockEdit {...props} />;
33
+ }, 'withInnerBlocksCleanup');
34
+
35
+ addFilter(
36
+ 'editor.BlockEdit',
37
+ 'ssm/with-inner-blocks-cleanup',
38
+ withInnerBlocksCleanup
39
+ );
40
+ };