@secretstache/wordpress-gutenberg 0.6.16 → 0.7.0
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/editor-canvas.css +1 -1
- package/build/index.js +3208 -3187
- package/build/index.js.map +1 -1
- package/package.json +8 -2
- package/src/components/EmptyBlockPlaceholder.jsx +67 -0
- package/src/components/IconPicker.jsx +4 -5
- package/src/components/MediaControl.jsx +3 -3
- package/src/components/PatternAppender.jsx +24 -0
- package/src/components/PatternsModal.jsx +33 -35
- package/src/components/SpacingControl.jsx +5 -6
- package/src/components/index.js +19 -16
- package/src/filters/block-categories.js +50 -0
- package/src/filters/hide-root-block-for-other.js +53 -0
- package/src/filters/index.js +24 -0
- package/src/filters/inner-blocks-cleanup.jsx +53 -0
- package/src/filters/root-block.js +41 -0
- package/src/hooks/useTabs.jsx +2 -2
- package/src/icons/index.jsx +74 -38
- package/src/index.js +2 -0
- package/src/plugins/index.js +22 -0
- package/src/plugins/root-block-appender.jsx +51 -0
- package/src/plugins/root-pattern-appender.jsx +53 -0
- package/src/styles/{_empty-block-appender.scss → _empty-block-placeholder.scss} +6 -1
- package/src/styles/_root-block-appender.scss +13 -0
- package/src/styles/_root-pattern-appender.scss +58 -0
- package/src/styles/editor-canvas.scss +9 -3
- package/src/utils/helpers.js +53 -0
- package/src/utils/index.js +0 -3
- package/src/components/EmptyBlockAppender.jsx +0 -38
- package/src/utils/attributes.js +0 -252
- package/src/utils/filters.jsx +0 -40
- package/src/utils/rootBlock/appender.js +0 -62
- package/src/utils/rootBlock/hideRootBlockForInlineInserter.js +0 -24
- package/src/utils/rootBlock/index.js +0 -6
- package/src/utils/rootBlock/rootBlockVisibilityFilter.js +0 -35
- package/src/utils/rootBlock/setRootBlockFilter.js +0 -30
- package/src/utils/rootBlock/setRootBlockForPostTypes.js +0 -73
- package/src/utils/rootBlock/unsetRootBlockFilter.js +0 -26
package/src/utils/attributes.js
DELETED
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @deprecated since 0.4.9
|
|
3
|
-
* @type {{linkLabel: {default: string, type: string}, linkIsOpenInNewTab: {default: boolean, type: string}, linkSource: {default: string, type: string}}}
|
|
4
|
-
*/
|
|
5
|
-
export const linkControlAttribute = {
|
|
6
|
-
linkLabel: {
|
|
7
|
-
type: 'string',
|
|
8
|
-
default: '',
|
|
9
|
-
},
|
|
10
|
-
linkSource: {
|
|
11
|
-
type: 'string',
|
|
12
|
-
default: '',
|
|
13
|
-
},
|
|
14
|
-
linkIsOpenInNewTab: {
|
|
15
|
-
type: 'boolean',
|
|
16
|
-
default: false,
|
|
17
|
-
},
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* @param name
|
|
22
|
-
* @returns {{[p: string]: {default: {filename: null, mime: null, alt: null, name: null, id: null, url: null}, type: string}}}
|
|
23
|
-
*/
|
|
24
|
-
export const getMediaAttribute = (name = 'media') => ({
|
|
25
|
-
[`${name}`]: {
|
|
26
|
-
type: 'object',
|
|
27
|
-
default: {
|
|
28
|
-
id: null,
|
|
29
|
-
url: null,
|
|
30
|
-
alt: null,
|
|
31
|
-
name: null,
|
|
32
|
-
filename: null,
|
|
33
|
-
mime: null,
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* @deprecated since 0.4.9
|
|
40
|
-
* @type {{description: {default: string, type: string}, title: {default: string, type: string}}}
|
|
41
|
-
*/
|
|
42
|
-
export const contentAttribute = {
|
|
43
|
-
title: {
|
|
44
|
-
type: 'string',
|
|
45
|
-
default: 'Title',
|
|
46
|
-
},
|
|
47
|
-
description: {
|
|
48
|
-
type: 'string',
|
|
49
|
-
default: 'Description',
|
|
50
|
-
},
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* @param name
|
|
55
|
-
* @returns {{[p: string]: {default: {filename: null, mime: null, alt: null, name: null, id: null, url: null}, type: string}, isAnimationLooped: {default: boolean, type: string}}}
|
|
56
|
-
*/
|
|
57
|
-
export const getAnimationAttribute = (name = 'animationFile') => ({
|
|
58
|
-
...getMediaAttribute(name),
|
|
59
|
-
isAnimationLooped: {
|
|
60
|
-
type: 'boolean',
|
|
61
|
-
default: true,
|
|
62
|
-
},
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
export const curatedPostsAttribute = {
|
|
66
|
-
curatedPosts: {
|
|
67
|
-
type: 'array',
|
|
68
|
-
default: [],
|
|
69
|
-
items: {
|
|
70
|
-
type: 'object',
|
|
71
|
-
properties: {
|
|
72
|
-
value: {
|
|
73
|
-
type: 'number'
|
|
74
|
-
},
|
|
75
|
-
label: {
|
|
76
|
-
type: 'string'
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
export const curatedCategoriesAttribute = {
|
|
84
|
-
curatedCategories: {
|
|
85
|
-
type: 'array',
|
|
86
|
-
default: [],
|
|
87
|
-
items: {
|
|
88
|
-
type: 'object',
|
|
89
|
-
properties: {
|
|
90
|
-
value: {
|
|
91
|
-
type: 'number'
|
|
92
|
-
},
|
|
93
|
-
label: {
|
|
94
|
-
type: 'string'
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
export const numberOfPostsAttribute = {
|
|
102
|
-
numberOfPosts: {
|
|
103
|
-
type: 'number',
|
|
104
|
-
default: 5,
|
|
105
|
-
},
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
// TODO: make dataSource optional, rebuild it's not easy to use(need to pass sourcesList and queriesList in complex format)
|
|
109
|
-
export const getDataQueryAttributes = (
|
|
110
|
-
sourcesList,
|
|
111
|
-
queriesList,
|
|
112
|
-
hasCuratedPosts = true,
|
|
113
|
-
hasCuratedCategories = false,
|
|
114
|
-
hasNumberOfPosts = false,
|
|
115
|
-
) => {
|
|
116
|
-
let dataSourceConfig = {
|
|
117
|
-
type: 'string'
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
if (sourcesList && sourcesList?.length > 0) {
|
|
121
|
-
dataSourceConfig.enum = sourcesList;
|
|
122
|
-
dataSourceConfig.default = sourcesList[0].value;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
let queryTypeConfig = {
|
|
126
|
-
type: 'string'
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
if (queriesList && queriesList?.length > 0) {
|
|
130
|
-
queryTypeConfig.enum = [
|
|
131
|
-
...queriesList
|
|
132
|
-
];
|
|
133
|
-
queryTypeConfig.default = queriesList[0].value;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
return {
|
|
137
|
-
dataSource: dataSourceConfig,
|
|
138
|
-
queryType: queryTypeConfig,
|
|
139
|
-
...(hasCuratedPosts ? curatedPostsAttribute : {}),
|
|
140
|
-
...(hasCuratedCategories ? curatedCategoriesAttribute : {}),
|
|
141
|
-
...(hasNumberOfPosts ? numberOfPostsAttribute : {}),
|
|
142
|
-
};
|
|
143
|
-
};
|
|
144
|
-
/**
|
|
145
|
-
* Generates a set of attributes for background settings in a block, including options
|
|
146
|
-
* for background color, media, and overlays based on the provided configurations.
|
|
147
|
-
*
|
|
148
|
-
* @param {Object} [options={}] - The options for generating background attributes.
|
|
149
|
-
* @param {boolean} [options.hasBackgroundMedia=false] - Flag to determine if background media attributes should be included.
|
|
150
|
-
* @param {string} [options.mediaAttributeName='media'] - Background media attribute name.
|
|
151
|
-
* @param {boolean} [options.hasOverlay=false] - Flag to determine if overlay attributes should be included.
|
|
152
|
-
*
|
|
153
|
-
* @returns {{[p: string]: {default: {filename: null, mime: null, alt: null, name: null, id: null, url: null}, type: string}, backgroundMediaType?: {type: string}, backgroundColor: {type: string}, isIncludeBackgroundMedia?: {type: string}, isIncludeOverlay?: {type: string}, overlayColor?: {type: string}}}
|
|
154
|
-
*/
|
|
155
|
-
export const getBaseBackgroundAttributes = ({
|
|
156
|
-
hasBackgroundMedia = false,
|
|
157
|
-
mediaAttributeName = 'media',
|
|
158
|
-
hasOverlay = false,
|
|
159
|
-
} = {}) => {
|
|
160
|
-
const backgroundMediaAttribute = {
|
|
161
|
-
isIncludeBackgroundMedia: {
|
|
162
|
-
type: 'boolean',
|
|
163
|
-
},
|
|
164
|
-
backgroundMediaType: {
|
|
165
|
-
type: 'string',
|
|
166
|
-
},
|
|
167
|
-
...getMediaAttribute(mediaAttributeName),
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
const overlayAttribute = {
|
|
171
|
-
isIncludeOverlay: {
|
|
172
|
-
type: 'boolean',
|
|
173
|
-
},
|
|
174
|
-
overlayColor: {
|
|
175
|
-
type: 'object',
|
|
176
|
-
},
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
return {
|
|
180
|
-
backgroundColor: {
|
|
181
|
-
type: 'object',
|
|
182
|
-
},
|
|
183
|
-
|
|
184
|
-
...(hasBackgroundMedia ? backgroundMediaAttribute : {}),
|
|
185
|
-
|
|
186
|
-
...(hasOverlay ? overlayAttribute : {}),
|
|
187
|
-
};
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
export const spacingAttribute = {
|
|
191
|
-
spacing: {
|
|
192
|
-
type: 'object',
|
|
193
|
-
default: {
|
|
194
|
-
margin: {
|
|
195
|
-
top: -1,
|
|
196
|
-
bottom: -1,
|
|
197
|
-
},
|
|
198
|
-
padding: {
|
|
199
|
-
top: -1,
|
|
200
|
-
bottom: -1,
|
|
201
|
-
}
|
|
202
|
-
},
|
|
203
|
-
},
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
export const responsiveSpacingAttribute = {
|
|
207
|
-
spacing: {
|
|
208
|
-
type: 'object',
|
|
209
|
-
default: {
|
|
210
|
-
desktop: {
|
|
211
|
-
margin: {
|
|
212
|
-
top: -1,
|
|
213
|
-
bottom: -1,
|
|
214
|
-
},
|
|
215
|
-
padding: {
|
|
216
|
-
top: -1,
|
|
217
|
-
bottom: -1,
|
|
218
|
-
}
|
|
219
|
-
},
|
|
220
|
-
mobile: {
|
|
221
|
-
margin: {
|
|
222
|
-
top: -1,
|
|
223
|
-
bottom: -1,
|
|
224
|
-
},
|
|
225
|
-
padding: {
|
|
226
|
-
top: -1,
|
|
227
|
-
bottom: -1,
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* @since 0.4.9
|
|
236
|
-
* @param prefix
|
|
237
|
-
* @returns {{[p: string]: {default: string, type: string}|{default: string, type: string}|{default: boolean, type: string}}}
|
|
238
|
-
*/
|
|
239
|
-
export const getLinkAttributes = (prefix = 'link') => ({
|
|
240
|
-
[`${prefix}Label`]: {
|
|
241
|
-
type: 'string',
|
|
242
|
-
default: '',
|
|
243
|
-
},
|
|
244
|
-
[`${prefix}Source`]: {
|
|
245
|
-
type: 'string',
|
|
246
|
-
default: '',
|
|
247
|
-
},
|
|
248
|
-
[`${prefix}IsOpenInNewTab`]: {
|
|
249
|
-
type: 'boolean',
|
|
250
|
-
default: false,
|
|
251
|
-
},
|
|
252
|
-
});
|
package/src/utils/filters.jsx
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { createBlock } from '@wordpress/blocks';
|
|
2
|
-
import { dispatch } from '@wordpress/data';
|
|
3
|
-
|
|
4
|
-
import { getRootContainer } from '../rootContainer/index.js';
|
|
5
|
-
|
|
6
|
-
const ROOT_BLOCK_APPENDER_SELECTOR = '.root-block-appender';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Initializes the custom button for the root appender.
|
|
10
|
-
* @param {Element} rootContainer - The root container of the editor.
|
|
11
|
-
* @param {string} blockName - The name of the block to be created when the appender is clicked.
|
|
12
|
-
* @param {string} tooltipText - The tooltip text displayed on the appender.
|
|
13
|
-
*/
|
|
14
|
-
const initialize = (rootContainer, blockName, tooltipText) => {
|
|
15
|
-
const button = document.createElement('button');
|
|
16
|
-
|
|
17
|
-
button.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false"><path d="M11 12.5V17.5H12.5V12.5H17.5V11H12.5V6H11V11H6V12.5H11Z"></path></svg>';
|
|
18
|
-
button.className = 'components-button block-editor-button-block-appender root-block-appender';
|
|
19
|
-
button.setAttribute('aria-label', tooltipText);
|
|
20
|
-
button.setAttribute('data-tooltip', tooltipText);
|
|
21
|
-
|
|
22
|
-
button.addEventListener('click', () => {
|
|
23
|
-
dispatch('core/block-editor').insertBlock(createBlock(blockName));
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
rootContainer.prepend(button);
|
|
27
|
-
|
|
28
|
-
return !!rootContainer.querySelector(ROOT_BLOCK_APPENDER_SELECTOR);
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Creates a new top-level block appender. It allows to specify the block name to be
|
|
33
|
-
* created when the appender is clicked and customize the tooltip text displayed on the appender.
|
|
34
|
-
*
|
|
35
|
-
* @param {string} blockName - The name of the block to be created when the appender is clicked.
|
|
36
|
-
* @param {string} [tooltipText='Add Row'] - The tooltip text displayed on the appender.
|
|
37
|
-
*/
|
|
38
|
-
export const setRootBlockAppender = (blockName, tooltipText = 'Add Row') => {
|
|
39
|
-
const rootContainer = getRootContainer();
|
|
40
|
-
|
|
41
|
-
if (rootContainer) {
|
|
42
|
-
initialize(rootContainer, blockName, tooltipText);
|
|
43
|
-
} else {
|
|
44
|
-
console.error('Root container is not found.')
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
export const unsetRootBlockAppender = () => {
|
|
49
|
-
const rootContainer = getRootContainer();
|
|
50
|
-
|
|
51
|
-
if (rootContainer) {
|
|
52
|
-
const appender = rootContainer.querySelector(ROOT_BLOCK_APPENDER_SELECTOR);
|
|
53
|
-
|
|
54
|
-
if (appender) {
|
|
55
|
-
appender.remove();
|
|
56
|
-
} else {
|
|
57
|
-
console.error('Root block appender is not found.');
|
|
58
|
-
}
|
|
59
|
-
} else {
|
|
60
|
-
console.error('Root container is not found.')
|
|
61
|
-
}
|
|
62
|
-
};
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
const styleElementId = 'ssm-hide-root-block-for-inline-inserter';
|
|
2
|
-
|
|
3
|
-
export const hideRootBlockForInlineInserter = (blockName) => {
|
|
4
|
-
// Construct the CSS rule
|
|
5
|
-
const cssRule = `[id*="-block-${blockName}"] { display: none !important; }`;
|
|
6
|
-
|
|
7
|
-
// Create a style element
|
|
8
|
-
const styleElement = document.createElement('style');
|
|
9
|
-
styleElement.id = styleElementId;
|
|
10
|
-
|
|
11
|
-
// Set the CSS rule as the content of the style element
|
|
12
|
-
styleElement.appendChild(document.createTextNode(cssRule));
|
|
13
|
-
|
|
14
|
-
// Append the style element to the document's head
|
|
15
|
-
document.head.appendChild(styleElement);
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export const showRootBlockForInlineInserter = (blockName) => {
|
|
19
|
-
const styleElement = document.getElementById(styleElementId);
|
|
20
|
-
|
|
21
|
-
if (styleElement) {
|
|
22
|
-
document.head.removeChild(styleElement);
|
|
23
|
-
}
|
|
24
|
-
};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export * from './setRootBlockForPostTypes.js'
|
|
2
|
-
export * from './setRootBlockFilter.js'
|
|
3
|
-
export * from './unsetRootBlockFilter.js';
|
|
4
|
-
export * from './rootBlockVisibilityFilter.js';
|
|
5
|
-
export * from './appender.js';
|
|
6
|
-
export * from './hideRootBlockForInlineInserter.js';
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { addFilter, removeFilter } from '@wordpress/hooks';
|
|
2
|
-
import { getBlockTypes } from '@wordpress/blocks';
|
|
3
|
-
|
|
4
|
-
export const rootBlockVisibilityFilter = {
|
|
5
|
-
add({ rootBlockName }) {
|
|
6
|
-
addFilter(
|
|
7
|
-
'blocks.registerBlockType',
|
|
8
|
-
'ssm/root-block-visibility',
|
|
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
|
-
remove() {
|
|
33
|
-
removeFilter('blocks.registerBlockType', 'ssm/root-block-visibility');
|
|
34
|
-
},
|
|
35
|
-
};
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { addFilter, removeFilter } from '@wordpress/hooks';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Adds a filter to set the specified block as the root block by modifying block settings during registration.
|
|
5
|
-
* Blocks other than the root block will have their 'ancestor' property set to the root block name,
|
|
6
|
-
* making them only insertable within the root block.
|
|
7
|
-
*/
|
|
8
|
-
export const setRootBlockFilter = {
|
|
9
|
-
add({ rootBlockName }) {
|
|
10
|
-
addFilter(
|
|
11
|
-
'blocks.registerBlockType',
|
|
12
|
-
'ssm/set-root-block',
|
|
13
|
-
(settings, name) => {
|
|
14
|
-
const isRootBlock = name === rootBlockName;
|
|
15
|
-
const isBaseBlock = name === 'core/block';
|
|
16
|
-
const hasAncestor = !!settings?.ancestor;
|
|
17
|
-
const hasParent = !!settings?.parent;
|
|
18
|
-
|
|
19
|
-
if (!isRootBlock && !isBaseBlock && !hasAncestor && !hasParent) {
|
|
20
|
-
settings.ancestor = [rootBlockName];
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return settings;
|
|
24
|
-
},
|
|
25
|
-
);
|
|
26
|
-
},
|
|
27
|
-
remove() {
|
|
28
|
-
removeFilter('blocks.registerBlockType', 'ssm/set-root-block');
|
|
29
|
-
},
|
|
30
|
-
};
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { dispatch, select, subscribe } from '@wordpress/data';
|
|
2
|
-
import { setRootBlockFilter } from './setRootBlockFilter.js';
|
|
3
|
-
import { unsetRootBlockFilter } from './unsetRootBlockFilter.js';
|
|
4
|
-
import { rootBlockVisibilityFilter } from './rootBlockVisibilityFilter.js';
|
|
5
|
-
import { waitForRootContainer } from '../rootContainer/index.js';
|
|
6
|
-
import { setRootBlockAppender, unsetRootBlockAppender } from './appender.js';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Configures a root block for specific post types
|
|
10
|
-
*
|
|
11
|
-
* @param {string} rootBlockName - The name of the root block to set.
|
|
12
|
-
* @param {Array<string>} [postTypes=['page', 'post']] - The post types for which the root block should be enabled.
|
|
13
|
-
* @param {Function} [callback] - Optional callback to execute when the root block state changes.
|
|
14
|
-
* @param {Array<Object>} [filters=[rootBlockVisibilityFilter]] - Filters to apply or remove when enabling/disabling the root block.
|
|
15
|
-
* @param {boolean} [initAppender=true] - Whether to initialize the root block appender.
|
|
16
|
-
* @param {string} [appenderTooltipText='Add Row'] - Tooltip text for the root block appender.
|
|
17
|
-
*/
|
|
18
|
-
export const setRootBlockForPostTypes = (
|
|
19
|
-
rootBlockName,
|
|
20
|
-
postTypes = ['page', 'post'],
|
|
21
|
-
callback,
|
|
22
|
-
filters = [ rootBlockVisibilityFilter ],
|
|
23
|
-
initAppender = true,
|
|
24
|
-
appenderTooltipText = 'Add Row',
|
|
25
|
-
) => {
|
|
26
|
-
let isRootBlockEnabled = false;
|
|
27
|
-
|
|
28
|
-
waitForRootContainer().then(() => {
|
|
29
|
-
console.log('Root Container found.');
|
|
30
|
-
|
|
31
|
-
subscribe(() => {
|
|
32
|
-
const currentPostType = select('core/editor').getCurrentPostType();
|
|
33
|
-
|
|
34
|
-
if (postTypes.includes(currentPostType) && !isRootBlockEnabled) {
|
|
35
|
-
isRootBlockEnabled = true;
|
|
36
|
-
|
|
37
|
-
setRootBlockFilter.add({ rootBlockName });
|
|
38
|
-
unsetRootBlockFilter.remove();
|
|
39
|
-
|
|
40
|
-
if (filters?.length > 0) {
|
|
41
|
-
filters.forEach((filter) => filter.add({ rootBlockName, isRootBlockEnabled, currentPostType }));
|
|
42
|
-
dispatch('core/blocks').reapplyBlockTypeFilters();
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (callback) {
|
|
46
|
-
callback({ isRootBlockEnabled, currentPostType });
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (initAppender) {
|
|
50
|
-
setRootBlockAppender(rootBlockName, appenderTooltipText);
|
|
51
|
-
}
|
|
52
|
-
} else if (!postTypes.includes(currentPostType) && isRootBlockEnabled) {
|
|
53
|
-
isRootBlockEnabled = false;
|
|
54
|
-
|
|
55
|
-
setRootBlockFilter.remove()
|
|
56
|
-
unsetRootBlockFilter.add(rootBlockName);
|
|
57
|
-
|
|
58
|
-
if (filters?.length > 0) {
|
|
59
|
-
filters.forEach((filter) => filter.remove({ rootBlockName, isRootBlockEnabled, currentPostType }));
|
|
60
|
-
dispatch('core/blocks').reapplyBlockTypeFilters();
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (callback) {
|
|
64
|
-
callback({ isRootBlockEnabled, currentPostType });
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (initAppender) {
|
|
68
|
-
unsetRootBlockAppender();
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}, 'core/block-editor');
|
|
72
|
-
})
|
|
73
|
-
};
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { addFilter, removeFilter } from '@wordpress/hooks';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Adds a filter to unset the root block restrictions by removing the 'ancestor' property from block settings
|
|
5
|
-
* if it includes the specified root block name.
|
|
6
|
-
*/
|
|
7
|
-
export const unsetRootBlockFilter = {
|
|
8
|
-
add(rootBlockName) {
|
|
9
|
-
addFilter(
|
|
10
|
-
'blocks.registerBlockType',
|
|
11
|
-
'ssm/unset-root-block',
|
|
12
|
-
(settings) => {
|
|
13
|
-
const hasRootAncestor = settings.ancestor && settings.ancestor.includes(rootBlockName);
|
|
14
|
-
|
|
15
|
-
if (hasRootAncestor) {
|
|
16
|
-
settings.ancestor = null;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return settings;
|
|
20
|
-
},
|
|
21
|
-
);
|
|
22
|
-
},
|
|
23
|
-
remove() {
|
|
24
|
-
removeFilter('blocks.registerBlockType', 'ssm/unset-root-block');
|
|
25
|
-
},
|
|
26
|
-
};
|