@secretstache/wordpress-gutenberg 0.3.9 → 0.3.11

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.9",
3
+ "version": "0.3.11",
4
4
  "description": "",
5
5
  "author": "Secret Stache",
6
6
  "license": "GPL-2.0-or-later",
@@ -1,6 +1,8 @@
1
1
  import { RadioControl } from '@wordpress/components';
2
2
 
3
- // TODO: add support of curated posts, categories; consider merging with the useDataQuery hook
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
4
6
  export const DataQueryControls = (props) => {
5
7
  const {
6
8
  dataSourceLabel = 'Data Source',
@@ -0,0 +1,36 @@
1
+ import { BlockControls } from '@wordpress/block-editor';
2
+ import { select, useDispatch } from '@wordpress/data';
3
+ import { createBlock } from '@wordpress/blocks';
4
+ import { useCallback } from '@wordpress/element';
5
+ import { Toolbar, ToolbarButton } from '@wordpress/components';
6
+
7
+ export const InsertBlockToolbar = ({ clientId, blockName, group }) => {
8
+ const { insertBlock } = useDispatch('core/block-editor');
9
+
10
+ const insertBlockBefore = useCallback(() => {
11
+ const block = createBlock(blockName);
12
+ insertBlock(block, select('core/block-editor').getBlockIndex(clientId));
13
+ }, []);
14
+
15
+ const insertBlockAfter = useCallback(() => {
16
+ const block = createBlock(blockName);
17
+ insertBlock(block, select('core/block-editor').getBlockIndex(clientId) + 1);
18
+ }, []);
19
+
20
+ return (
21
+ <BlockControls group={group}>
22
+ <Toolbar>
23
+ <ToolbarButton
24
+ icon="insert-before"
25
+ label="Add before"
26
+ onClick={insertBlockBefore}
27
+ />
28
+ <ToolbarButton
29
+ icon="insert-after"
30
+ label="Add after"
31
+ onClick={insertBlockAfter}
32
+ />
33
+ </Toolbar>
34
+ </BlockControls>
35
+ );
36
+ };
@@ -10,3 +10,4 @@ export { ResponsiveSpacingControl } from './ResponsiveSpacingControl.js';
10
10
  export { ResourcesWrapper } from './ResourcesWrapper.js'
11
11
  export { DividersControl } from './DividersControl.js'
12
12
  export { MediaTypeControl } from './MediaTypeControl.js'
13
+ export { InsertBlockToolbar } from './InsertBlockToolbar.js'
@@ -1,4 +1,4 @@
1
- export { usePreviewToggle } from './usePreviewToggle.js';
1
+ export { usePreviewControl } from './usePreviewControl.js';
2
2
  export { useSlider } from './useSlider.js';
3
3
  export { useParentBlock } from './useParentBlock.js';
4
4
  export { useColorChange } from './useColorChange';
@@ -1,6 +1,7 @@
1
1
  import { useSelect } from '@wordpress/data';
2
2
  import { QUERY_TYPES } from '../utils';
3
3
 
4
+ // TODO: numberOfPosts -1 is not a valid value, the API requires the per_page to be >= 1
4
5
  export const useDataQuery = (props) => {
5
6
  const {
6
7
  getPostType,
@@ -0,0 +1,31 @@
1
+ import { useCallback, useMemo, useState } from '@wordpress/element';
2
+ import { ToggleControl } from '@wordpress/components';
3
+
4
+ const defaultLabel = 'Enable Preview';
5
+ const defaultHelpText = 'Please check this option to see how the block will actually look and behave on the frontend.';
6
+
7
+ export const usePreviewControl = () => {
8
+ const [ isPreview, setIsPreview ] = useState(false);
9
+
10
+ const onChange = useCallback(() => {
11
+ setIsPreview(prev => !prev);
12
+ }, []);
13
+
14
+ const PreviewControl = useMemo(() => {
15
+ return ({ label = defaultLabel, helpText = defaultHelpText, ...props }) => (
16
+ <ToggleControl
17
+ label={label}
18
+ help={helpText}
19
+ checked={isPreview}
20
+ onChange={onChange}
21
+ {...props}
22
+ />
23
+ );
24
+ });
25
+
26
+ return {
27
+ isPreview,
28
+ setIsPreview,
29
+ PreviewControl,
30
+ };
31
+ };
@@ -15,7 +15,7 @@ export const useSlider = ({ isEnabled, setupSlider, dependencies = [] }) => {
15
15
  sliderInstance.current = null;
16
16
  }
17
17
  };
18
- }, [isEnabled, ...dependencies]);
18
+ }, [ isEnabled, ...dependencies ]);
19
19
 
20
20
  return {
21
21
  sliderElRef,
@@ -96,7 +96,7 @@ export const numberOfPostsAttribute = {
96
96
  },
97
97
  };
98
98
 
99
- // TODO: make dataSource optional
99
+ // TODO: make dataSource optional, rebuild it's not easy to use(need to pass sourcesList and queriesList in complex format)
100
100
  export const getDataQueryAttributes = (
101
101
  sourcesList,
102
102
  queriesList,
@@ -132,54 +132,60 @@ export const getDataQueryAttributes = (
132
132
  ...(hasNumberOfPosts ? numberOfPostsAttribute : {}),
133
133
  };
134
134
  };
135
-
136
135
  /**
137
- * Returns the base background attribute object with configurable default background color and media type.
136
+ * Generates a set of attributes for background settings in a block, including options
137
+ * for background color, media, and overlays based on the provided configurations.
138
+ *
139
+ * @param {Object} [options={}] - The options for generating background attributes.
140
+ * @param {Object} [options.defaultBackgroundColor={ value: '', slug: '' }] - The default background color attribute.
141
+ * @param {boolean} [options.hasBackgroundMedia=false] - Flag to determine if background media attributes should be included.
142
+ * @param {string} [options.defaultBackgroundMediaType=''] - The default type of background media if media attributes are included.
143
+ * @param {boolean} [options.hasOverlay=false] - Flag to determine if overlay attributes should be included.
144
+ * @param {Object} [options.defaultOverlayColor={ value: '', slug: '' }] - The default overlay color attribute if overlay attributes are included.
138
145
  *
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.
146
- * @returns {Object} The base background attribute object.
146
+ * @returns {Object} An object containing the attributes for the block's background configuration.
147
147
  */
148
148
  export const getBaseBackgroundAttributes = ({
149
- defaultBackgroundMediaType = '',
150
149
  defaultBackgroundColor = { value: '', slug: '' },
151
- hasIncludeBackgroundMediaAttribute = false,
152
- hasIncludeOverlayAttribute = false,
150
+
151
+ hasBackgroundMedia = false,
152
+ defaultBackgroundMediaType = '',
153
+
154
+ hasOverlay = false,
155
+ defaultOverlayColor = { value: '', slug: '' },
153
156
  } = {}) => {
154
- const isIncludeOverlayAttribute = {
155
- isIncludeOverlay: {
157
+ const backgroundMediaAttribute = {
158
+ isIncludeBackgroundMedia: {
156
159
  type: 'boolean',
157
160
  default: false,
158
- }
161
+ },
162
+ backgroundMediaType: {
163
+ type: 'string',
164
+ default: defaultBackgroundMediaType,
165
+ },
166
+ ...mediaAttribute,
159
167
  };
160
168
 
161
- const isIncludeBackgroundMediaAttribute = {
162
- isIncludeBackgroundMedia: {
169
+ const overlayAttribute = {
170
+ isIncludeOverlay: {
163
171
  type: 'boolean',
164
172
  default: false,
165
- }
173
+ },
174
+ overlayColor: {
175
+ type: 'object',
176
+ default: defaultOverlayColor,
177
+ },
166
178
  };
167
179
 
168
180
  return {
169
- ...(hasIncludeBackgroundMediaAttribute ? isIncludeBackgroundMediaAttribute : {}),
170
-
171
- backgroundMediaType: {
172
- type: 'string',
173
- default: defaultBackgroundMediaType,
174
- },
175
181
  backgroundColor: {
176
182
  type: 'object',
177
183
  default: defaultBackgroundColor,
178
184
  },
179
185
 
180
- ...mediaAttribute,
186
+ ...(hasBackgroundMedia ? backgroundMediaAttribute : {}),
181
187
 
182
- ...(hasIncludeOverlayAttribute ? isIncludeOverlayAttribute : {}),
188
+ ...(hasOverlay ? overlayAttribute : {}),
183
189
  };
184
190
  };
185
191
 
@@ -1,32 +0,0 @@
1
- import { useState } from '@wordpress/element';
2
- import { ToggleControl } from '@wordpress/components';
3
-
4
- const defaultLabel = 'Enable Preview';
5
- const defaultHelpText = 'Please check this option to see how the block will actually look and behave on the frontend.';
6
-
7
- export const usePreviewToggle = (props = {}) => {
8
- const {
9
- disabled = false,
10
- label = defaultLabel,
11
- helpText = defaultHelpText,
12
- } = props;
13
-
14
- const [ isPreview, setIsPreview ] = useState(false);
15
-
16
- const onChange = () => setIsPreview(prev => !prev);
17
-
18
- const renderPreviewToggle = () => (
19
- <ToggleControl
20
- label={label}
21
- help={helpText}
22
- checked={isPreview}
23
- onChange={onChange}
24
- disabled={disabled}
25
- />
26
- );
27
-
28
- return {
29
- isPreview,
30
- renderPreviewToggle
31
- };
32
- };