-
Notifications
You must be signed in to change notification settings - Fork 38
/
index.tsx
176 lines (147 loc) · 4.52 KB
/
index.tsx
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
import { addFilter } from '@wordpress/hooks';
import { createHigherOrderComponent } from '@wordpress/compose';
import classnames from 'classnames';
import { FC } from 'react';
interface BlockEditProps {
name: string;
isSelected: boolean;
attributes: Record<string, any>;
setAttributes: (attributes: Record<string, any>) => void;
className: string;
style: Record<string, any>;
wrapperProps: {
[key: string]: any;
};
[key: string]: unknown;
}
interface BlockOptionOptions {
attributes: Record<string, any>;
classNameGenerator: (attributes: Record<string, unknown>) => string;
inlineStyleGenerator: (attributes: Record<string, unknown>) => Record<string, any>;
Edit: FC<BlockEditProps>;
extensionName: string;
order?: 'before' | 'after';
}
function registerBlockExtension(
blockName: string | string[],
{
attributes,
classNameGenerator,
inlineStyleGenerator,
Edit,
extensionName,
order = 'after',
}: BlockOptionOptions,
): void {
const isMultiBlock = Array.isArray(blockName);
const shouldApplyBlockExtension = (blockType: string): boolean => {
if (blockName === '*') {
return true;
}
if (isMultiBlock) {
return blockName.includes(blockType);
}
return blockType === blockName;
};
const blockNamespace = isMultiBlock ? blockName.join('-') : blockName;
const addAttributesToBlock = (settings: Record<string, any>, name: string) => {
if (!shouldApplyBlockExtension(name)) {
return settings;
}
return {
...settings,
attributes: {
...settings.attributes,
...attributes,
},
};
};
addFilter(
'blocks.registerBlockType',
`namespace/${blockNamespace}/${extensionName}/addAttributesToBlock`,
addAttributesToBlock,
);
const addSettingsToBlock = createHigherOrderComponent((BlockEdit: FC<any>) => {
return (props: any) => {
const { name, isSelected } = props;
if (!shouldApplyBlockExtension(name)) {
return <BlockEdit {...props} />;
}
const shouldRenderBefore = order === 'before' && isSelected;
const shouldRenderAfter = order === 'after' && isSelected;
const shouldRenderFallback = !shouldRenderBefore && !shouldRenderAfter && isSelected;
return (
<>
{shouldRenderBefore && <Edit {...props} />}
<BlockEdit {...props} />
{shouldRenderAfter && <Edit {...props} />}
{shouldRenderFallback && <Edit {...props} />}
</>
);
};
}, 'addSettingsToBlock');
addFilter(
'editor.BlockEdit',
`namespace/${blockNamespace}/${extensionName}/addSettingsToBlock`,
addSettingsToBlock,
);
const addAdditionalPropertiesInEditor = createHigherOrderComponent((BlockList: FC<any>) => {
return (props: BlockEditProps) => {
const { name, attributes, className = '', style = {}, wrapperProps } = props;
if (!shouldApplyBlockExtension(name)) {
return <BlockList {...props} />;
}
const additionalClassName = classNameGenerator(attributes);
const newClassName = classnames(className, additionalClassName);
let additionalStyles = null;
let newStyles = { ...style };
if (typeof inlineStyleGenerator === 'function') {
additionalStyles = inlineStyleGenerator(attributes);
newStyles = { ...style, ...wrapperProps?.style, ...additionalStyles };
}
if (!additionalClassName && !additionalStyles) {
return <BlockList {...props} />;
}
return (
<BlockList
{...props}
className={newClassName}
wrapperProps={{ ...wrapperProps, style: newStyles }}
/>
);
};
}, 'addAdditionalPropertiesInEditor');
addFilter(
'editor.BlockListBlock',
`namespace/${blockNamespace}/${extensionName}/addAdditionalPropertiesInEditor`,
addAdditionalPropertiesInEditor,
);
const addAdditionalPropertiesToSavedMarkup = (
props: BlockEditProps,
block: { name: string },
attributes: Record<string, any>,
) => {
const { className, style } = props;
if (!shouldApplyBlockExtension(block.name)) {
return props;
}
const additionalClassName = classNameGenerator(attributes);
const newClassName = classnames(className, additionalClassName);
let additionalStyles = null;
let newStyles = { ...style };
if (typeof inlineStyleGenerator === 'function') {
additionalStyles = inlineStyleGenerator(attributes);
newStyles = { ...style, ...additionalStyles };
}
if (!additionalClassName && !additionalStyles) {
return props;
}
return { ...props, className: newClassName, style: newStyles };
};
addFilter(
'blocks.getSaveContent.extraProps',
`namespace/${blockNamespace}/${extensionName}/addAdditionalPropertiesToSavedMarkup`,
addAdditionalPropertiesToSavedMarkup,
);
}
export { registerBlockExtension };