@secretstache/wordpress-gutenberg 0.4.13 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,156 +0,0 @@
1
- ## setRootBlock
2
-
3
- The `setRootBlock` function configures a root block in the Gutenberg editor for specific post types. It ensures that only the specified root block can be inserted at the root level for those post types. The function dynamically applies or removes the root block restriction based on the current post type. Optionally, it initializes a custom root block appender with customizable tooltip text and allows specifying callbacks for when the post type matches or does not match.
4
-
5
- ### Function Signature
6
-
7
- ```javascript
8
- /**
9
- * Sets the root block in the Gutenberg editor for specific post types and optionally customizes the tooltip text for the root appender.
10
- * The function dynamically applies or removes the root block restriction based on the current post type.
11
- *
12
- * @param {string} rootBlockName - The name of the block to be set as the root block.
13
- * @param {string[]} [postTypes=['page', 'ssm_design_system']] - An array of post types where the root block should be set.
14
- * @param {boolean} [initAppender=true] - Flag to indicate whether to initialize the root appender.
15
- * @param {string} [appenderTooltipText='Add Row'] - The tooltip text to be displayed on the root appender.
16
- * @param {Function} [matchPostTypeCallback=null] - A callback function to execute when the current post type matches the specified post types.
17
- * @param {Function} [notMatchPostTypeCallback=null] - A callback function to execute when the current post type does not match the specified post types.
18
- */
19
- export const setRootBlock = (
20
- rootBlockName,
21
- postTypes = ['page', 'ssm_design_system'],
22
- initAppender = true,
23
- appenderTooltipText = 'Add Row',
24
- matchPostTypeCallback = null,
25
- notMatchPostTypeCallback = null,
26
- );
27
- ```
28
-
29
- #### Parameters
30
-
31
- - **rootBlockName** (`string`): The name of the block to be set as the root block. This parameter is mandatory.
32
- - **postTypes** (`string[]`, optional): An array of post types where the root block should be set. Default is `['page', 'ssm_design_system']`.
33
- - **initAppender** (`boolean`, optional): Flag to indicate whether to initialize the root appender. Default is `true`.
34
- - **appenderTooltipText** (`string`, optional): The tooltip text to be displayed on the root appender. Default is `'Add Row'`.
35
- - **matchPostTypeCallback** (`Function`, optional): A callback function to execute when the current post type matches the specified post types. Default is `null`.
36
- - **notMatchPostTypeCallback** (`Function`, optional): A callback function to execute when the current post type does not match the specified post types. Default is `null`.
37
-
38
- ### Usage Example
39
-
40
- To use the `setRootBlock` function, import it into your script and pass the necessary parameters:
41
-
42
- ```javascript
43
- import domReady from '@wordpress/dom-ready';
44
- import { setRootBlock } from '@secretstache/wordpress-gutenberg';
45
-
46
- domReady(() => {
47
- setRootBlock(
48
- 'ssm/section-wrapper',
49
- ['page', 'post'],
50
- true,
51
- 'Add Section',
52
- () => {
53
- console.log('Root block set for matching post type.');
54
- },
55
- () => {
56
- console.log('Root block unset for non-matching post type.');
57
- }
58
- );
59
- });
60
- ```
61
-
62
- In this example, the function will:
63
-
64
- - Set `'ssm/section-wrapper'` as the root block for 'page' and 'post' post types.
65
- - Initialize the root appender with the tooltip text 'Add Section'.
66
- - Execute the `matchPostTypeCallback` when the current post type matches 'page' or 'post'.
67
- - Execute the `notMatchPostTypeCallback` when the current post type does not match.
68
-
69
- The function monitors the current post type and dynamically applies or removes the root block restriction as needed.
70
-
71
- ---
72
-
73
- ## setRootBlockAppender
74
-
75
- The `setRootBlockAppender` function creates a new top-level block appender in the Gutenberg editor. It allows you to specify the block name to be created when the appender is clicked and customize the tooltip text displayed on the appender.
76
-
77
- ### Function Signature
78
-
79
- ```javascript
80
- /**
81
- * Creates a new top-level block appender. It allows specifying the block name to be
82
- * created when the appender is clicked and customizing the tooltip text displayed on the appender.
83
- *
84
- * @param {string} blockName - The name of the block to be created when the appender is clicked.
85
- * @param {string} [tooltipText='Add Row'] - The tooltip text displayed on the appender.
86
- */
87
- export const setRootBlockAppender = (blockName, tooltipText = 'Add Row');
88
- ```
89
-
90
- #### Parameters
91
-
92
- - **blockName** (`string`): The name of the block to be created when the appender is clicked. This parameter is mandatory.
93
- - **tooltipText** (`string`, optional): The tooltip text displayed on the appender. Default is `'Add Row'`.
94
-
95
- ### Usage Example
96
-
97
- In your custom theme or plugin, you can import and initialize the function with the desired block name and tooltip text:
98
-
99
- ```javascript
100
- import domReady from '@wordpress/dom-ready';
101
- import { setRootBlockAppender } from '@secretstache/wordpress-gutenberg';
102
-
103
- domReady(() => {
104
- setRootBlockAppender('ssm/section-wrapper', 'Add Section');
105
- });
106
- ```
107
-
108
- In this example, clicking the appender will insert the `'ssm/section-wrapper'` block, and the tooltip on the appender will display 'Add Section'.
109
-
110
- ---
111
-
112
- ## unsetRootBlockAppender
113
-
114
- The `unsetRootBlockAppender` function removes the custom root block appender from the Gutenberg editor.
115
-
116
- ### Function Signature
117
-
118
- ```javascript
119
- /**
120
- * Removes the custom root block appender from the Gutenberg editor.
121
- */
122
- export const unsetRootBlockAppender = () => {};
123
- ```
124
-
125
- ### Usage Example
126
-
127
- To remove the custom root block appender, simply call the function:
128
-
129
- ```javascript
130
- import { unsetRootBlockAppender } from '@secretstache/wordpress-gutenberg';
131
-
132
- unsetRootBlockAppender();
133
- ```
134
-
135
- ---
136
-
137
- ### Notes
138
-
139
- - The `setRootBlock` function internally manages the application and removal of the root block restrictions based on the current post type. It also handles the initialization and removal of the root block appender if `initAppender` is set to `true`.
140
- - The `matchPostTypeCallback` and `notMatchPostTypeCallback` parameters allow you to execute custom logic when the post type matches or does not match the specified `postTypes` array. This can be useful for additional configurations or side effects needed in your application.
141
- - The functions `setRootBlockAppender` and `unsetRootBlockAppender` can be used independently if you need to manually control the appender outside of the `setRootBlock` function.
142
-
143
- ### Additional Example
144
-
145
- If you want to set a root block without initializing the appender and without specifying callbacks:
146
-
147
- ```javascript
148
- import domReady from '@wordpress/dom-ready';
149
- import { setRootBlock } from '@secretstache/wordpress-gutenberg';
150
-
151
- domReady(() => {
152
- setRootBlock('ssm/section-wrapper', ['page'], false);
153
- });
154
- ```
155
-
156
- In this case, the root block `'ssm/section-wrapper'` will be set for the 'page' post type, but the root appender will not be initialized.
@@ -1,33 +0,0 @@
1
- import { addFilter } from '@wordpress/hooks';
2
- import { getBlockTypes } from '@wordpress/blocks';
3
- import { dispatch } from '@wordpress/data';
4
-
5
- export const hideRootBlockForOtherBlocks = (rootBlockName) => {
6
- addFilter(
7
- 'blocks.registerBlockType',
8
- 'ssm/hide-root-block-for-other-blocks',
9
- (blockSettings, blockName) => {
10
- const isRootBlock = blockName === rootBlockName;
11
- const hasOwnAllowedBlocks = !!blockSettings?.allowedBlocks;
12
- const hasParent = !!blockSettings?.parent;
13
-
14
- if (isRootBlock || hasParent || hasOwnAllowedBlocks) {
15
- return blockSettings;
16
- }
17
-
18
- // get all blockTypes
19
- blockSettings.allowedBlocks = getBlockTypes()
20
- ?.filter((allowedBlock) => {
21
- const isRootBlock = allowedBlock.name === rootBlockName;
22
- const hasParent = !!allowedBlock?.parent;
23
-
24
- return !isRootBlock && !hasParent;
25
- })
26
- ?.map(allowedBlock => allowedBlock.name);
27
-
28
- return blockSettings;
29
- },
30
- );
31
-
32
- dispatch('core/blocks').reapplyBlockTypeFilters();
33
- };
@@ -1,115 +0,0 @@
1
- import { addFilter, removeFilter } from '@wordpress/hooks';
2
- import { setRootBlockAppender, unsetRootBlockAppender } from './setRootBlockAppender.js';
3
- import { dispatch, select, subscribe } from '@wordpress/data';
4
-
5
- /**
6
- * Adds a filter to set the specified block as the root block by modifying block settings during registration.
7
- * Blocks other than the root block will have their 'ancestor' property set to the root block name,
8
- * making them only insertable within the root block.
9
- *
10
- * @param {string} rootBlockName - The name of the block to be set as the root block.
11
- */
12
- export const addSetRootBlockFilter = (rootBlockName) => {
13
- addFilter(
14
- 'blocks.registerBlockType',
15
- 'ssm/set-root-block',
16
- (settings, name) => {
17
- const isRootBlock = name === rootBlockName;
18
- const isBaseBlock = name === 'core/block';
19
- const hasAncestor = !!settings?.ancestor;
20
-
21
- if (!isRootBlock && !isBaseBlock && !hasAncestor) {
22
- settings.ancestor = [rootBlockName];
23
- }
24
-
25
- return settings;
26
- },
27
- );
28
- };
29
-
30
- /**
31
- * Adds a filter to unset the root block restrictions by removing the 'ancestor' property from block settings
32
- * if it includes the specified root block name.
33
- *
34
- * @param {string} rootBlockName - The name of the block previously set as the root block.
35
- */
36
- export const addUnsetRootBlockFilter = (rootBlockName) => {
37
- addFilter(
38
- 'blocks.registerBlockType',
39
- 'ssm/unset-root-block',
40
- (settings) => {
41
- const hasRootAncestor = settings.ancestor && settings.ancestor.includes(rootBlockName);
42
-
43
- if (hasRootAncestor) {
44
- settings.ancestor = null;
45
- }
46
-
47
- return settings;
48
- },
49
- );
50
- };
51
-
52
- /**
53
- * Configures the Gutenberg editor to use a specified block as the root block for certain post types.
54
- * It dynamically applies or removes the root block restriction based on the current post type.
55
- * Optionally initializes a custom root block appender and provides callbacks for post type matching.
56
- *
57
- * @param {string} rootBlockName - The name of the block to set as the root block.
58
- * @param {string[]} [postTypes=['page', 'ssm_design_system']] - Array of post types where the root block should be set.
59
- * @param {boolean} [initAppender=true] - Whether to initialize the root block appender.
60
- * @param {string} [appenderTooltipText='Add Row'] - Tooltip text for the root block appender.
61
- * @param {Function} [matchPostTypeCallback=null] - Callback function when the post type matches.
62
- * @param {Function} [notMatchPostTypeCallback=null] - Callback function when the post type does not match.
63
- */
64
- export const setRootBlock = (
65
- rootBlockName,
66
- postTypes = ['page', 'ssm_design_system'],
67
- initAppender = true,
68
- appenderTooltipText = 'Add Row',
69
- matchPostTypeCallback = null,
70
- notMatchPostTypeCallback = null,
71
- ) => {
72
- let isRootBlockEnabled = false;
73
-
74
- subscribe(() => {
75
- const currentPostType = select('core/editor').getCurrentPostType();
76
-
77
- if (!currentPostType) return;
78
-
79
- const isMatchPostType = postTypes.includes(currentPostType);
80
-
81
- if (isMatchPostType) {
82
- if (!isRootBlockEnabled) {
83
- removeFilter('blocks.registerBlockType', 'ssm/unset-root-block');
84
- addSetRootBlockFilter(rootBlockName);
85
- dispatch('core/blocks').reapplyBlockTypeFilters();
86
-
87
- if (initAppender) {
88
- setRootBlockAppender(rootBlockName, appenderTooltipText);
89
- }
90
-
91
- if (matchPostTypeCallback) {
92
- matchPostTypeCallback();
93
- }
94
-
95
- isRootBlockEnabled = true;
96
- }
97
- } else {
98
- if (isRootBlockEnabled) {
99
- removeFilter('blocks.registerBlockType', 'ssm/set-root-block');
100
- addUnsetRootBlockFilter(rootBlockName);
101
- dispatch('core/blocks').reapplyBlockTypeFilters();
102
-
103
- if (initAppender) {
104
- unsetRootBlockAppender(rootBlockName);
105
- }
106
-
107
- if (notMatchPostTypeCallback) {
108
- notMatchPostTypeCallback();
109
- }
110
-
111
- isRootBlockEnabled = false;
112
- }
113
- }
114
- }, 'core/editor');
115
- };
@@ -1,40 +0,0 @@
1
- ## Overview
2
-
3
- The `waitForContainer` function is a utility that periodically checks for the presence of a specified container element in the DOM. Once the container is found, it calls the provided initialization function. This is particularly useful for initializing scripts that depend on the presence of specific elements that may not be immediately available when the page loads.
4
-
5
- ## Function Signature
6
-
7
- ```javascript
8
- /**
9
- * Periodically checks for the presence of the container and initializes the script when found.
10
- *
11
- * @param {function} initializeFn - The initialization function to call when the root container is found.
12
- * @param {string} [containerClass='.is-root-container'] - The CSS class of the container to check for. Default is '.is-root-container'.
13
- * @param {number} [maxAttempts=50] - The maximum number of attempts to check for the root container.
14
- * @param {number} [interval=100] - The interval between each check in milliseconds.
15
- */
16
- export const waitForContainer = (initializeFn, containerClass = '.is-root-container', maxAttempts = 50, interval = 100);
17
- ```
18
-
19
- ### Parameters
20
-
21
- - **initializeFn** (`function`): The initialization function to call when the container is found.
22
- - **containerClass** (`string`, optional): The CSS class of the container to check for. Default is `.is-root-container`.
23
- - **maxAttempts** (`number`, optional): The maximum number of attempts to check for the root container. Default is `50`.
24
- - **interval** (`number`, optional): The interval between each check in milliseconds. Default is `100`.
25
-
26
- ## Usage example
27
-
28
- To use the `waitForContainer` function, import it into your script and pass the necessary parameters:
29
-
30
- ```javascript
31
- import { waitForContainer } from '@secretstache/wordpress-gutenberg';
32
-
33
- const initialize = () => {
34
- // Your initialization code here
35
- };
36
-
37
- waitForContainer(initialize, '.custom-container-class');
38
- ```
39
-
40
- In this example, the function will periodically check for the presence of an element with the class `.custom-container-class`. Once found, it will call the `initialize` function.
@@ -1,26 +0,0 @@
1
- /**
2
- * Periodically checks for the presence of the container and initializes the script when found.
3
- *
4
- * @param {function} initializeFn - The initialization function to call when the container is found.
5
- * This function receives the root container element as an argument.
6
- * @param {string} [containerClass='.is-root-container'] - The CSS class of the container to check for. Default is '.is-root-container'.
7
- * @param {number} [maxAttempts=50] - The maximum number of attempts to check for the root container.
8
- * @param {number} [interval=100] - The interval between each check in milliseconds.
9
- */
10
- export const waitForContainer = (initializeFn, containerClass = '.is-root-container', maxAttempts = 50, interval = 100) => {
11
- let attempts = 0;
12
- const checkInterval = setInterval(() => {
13
- const rootContainer = document.querySelector(containerClass);
14
-
15
- if (rootContainer) {
16
- clearInterval(checkInterval);
17
- initializeFn(rootContainer);
18
- } else {
19
- attempts += 1;
20
- if (attempts >= maxAttempts) {
21
- clearInterval(checkInterval);
22
- console.error('Max attempts reached. Root container not found.');
23
- }
24
- }
25
- }, interval);
26
- };