-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscripts.js
172 lines (143 loc) · 4.9 KB
/
scripts.js
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
(function () {
"use strict";
//iconData PASSED FROM LOCALIZE SCRIPT
const ICONS = createSVGWPElements(iconData);
function createSVGWPElements(iconData) {
const result = [];
for (const [key, value] of Object.entries(iconData)) {
const label = wp.i18n.__(key, 'enable-button-icons');
const svgAttributes = extractSvgAttributes(value);
// LOOP THROUGH PATHS
const pathElements = svgAttributes.pathD.map(pathDValue => (
wp.element.createElement("path", {
key: pathDValue,
d: pathDValue
})
));
const icon = wp.element.createElement("svg", {
width: svgAttributes.width,
height: svgAttributes.height,
viewBox: svgAttributes.viewBox,
xmlns: "http://www.w3.org/2000/svg"
}, ...pathElements);
result.push({ label, value: key, icon });
}
return result;
}
//ADD ATTRIBUTES TO THE BLOCK
function addAttributes(settings) {
if ('core/button' !== settings.name) {
return settings;
}
// Add the block visibility attributes.
const iconAttributes = {
icon: {
type: 'string'
},
iconPositionLeft: {
type: 'boolean',
default: false
}
};
const newSettings = {
...settings,
attributes: {
...settings.attributes,
...iconAttributes
}
};
return newSettings;
}
wp.hooks.addFilter('blocks.registerBlockType', 'enable-button-icons/add-attributes', addAttributes);
//CREATE THE CORE BLOCK ADDITIONAL OPTIONS
function addInspectorControls(BlockEdit) {
return function (props) {
if (props.name !== 'core/button') {
return wp.element.createElement(BlockEdit, props);
}
var attributes = props.attributes,
setAttributes = props.setAttributes;
var currentIcon = attributes.icon,
currentIconSVG = attributes.iconSVG,
iconPositionLeft = attributes.iconPositionLeft;
return wp.element.createElement(
wp.element.Fragment,
null,
wp.element.createElement(BlockEdit, props),
wp.element.createElement(wp.blockEditor.InspectorControls, null,
wp.element.createElement(wp.components.PanelBody, {
title: wp.i18n.__('Icon settings', 'enable-button-icons'),
className: "button-icon-picker",
initialOpen: true
},
wp.element.createElement(wp.components.PanelRow, null,
wp.element.createElement(wp.components.__experimentalGrid, {
className: "button-icon-picker__grid",
columns: "5",
gap: "0"
},
ICONS.map(function (icon, index) {
var _icon$icon;
return wp.element.createElement(wp.components.Button, {
key: index,
label: icon ? icon.label : '',
isPressed: currentIcon === icon.value,
className: "button-icon-picker__button",
onClick: function () {
setAttributes({
icon: currentIcon === icon.value ? null : icon.value,
iconSVG: currentIconSVG === icon.icon ? null : icon.icon
});
}
}, (_icon$icon = icon.icon) !== null && _icon$icon !== void 0 ? _icon$icon : icon.value);
})
)
),
wp.element.createElement(wp.components.PanelRow, null,
wp.element.createElement(wp.components.ToggleControl, {
label: wp.i18n.__('Show icon on left', 'enable-button-icons'),
checked: iconPositionLeft,
onChange: function () {
setAttributes({
iconPositionLeft: !iconPositionLeft
});
}
})
)
)
)
);
};
}
wp.hooks.addFilter('editor.BlockEdit', 'enable-button-icons/add-inspector-controls', addInspectorControls);
//ADD CLASSES WITHIN THE EDITOR TO THE ELEMENT
function addClasses(BlockListBlock) {
return function (props) {
var name = props.name,
attributes = props.attributes;
if ('core/button' !== name || !attributes || !attributes.icon) {
return wp.element.createElement(BlockListBlock, props);
}
var classes = (props && props.className || '') + ' ' +
(attributes.icon ? 'has-icon__' + attributes.icon : '') +
(attributes.iconPositionLeft ? ' has-icon-position__left' : '');
return wp.element.createElement(BlockListBlock, Object.assign({}, props, {
className: classes
}));
};
}
wp.hooks.addFilter('editor.BlockListBlock', 'enable-button-icons/add-classes', addClasses);
//EXTRACT SVG ATTRIBUTES
function extractSvgAttributes(svgMarkup) {
const widthMatch = svgMarkup.match(/width="([^"]+)"/);
const heightMatch = svgMarkup.match(/height="([^"]+)"/);
const viewBoxMatch = svgMarkup.match(/viewBox="([^"]+)"/);
const width = widthMatch ? widthMatch[1] : '22';
const height = heightMatch ? heightMatch[1] : '20';
const viewBox = viewBoxMatch ? viewBoxMatch[1] : '0 0 448 512';
// Extract the 'd' attribute values for each <path>
const pathMatches = svgMarkup.match(/<path d="([^"]+)"/g) || [];
const pathDValues = pathMatches.map(match => match.match(/<path d="([^"]+)"/)[1]);
return { width, height, viewBox, pathD: pathDValues };
}
})();