@wordpress/widgets 4.32.0 → 4.32.1-next.ff1cebbba.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/blocks/legacy-widget/block.json +29 -0
- package/build/blocks/legacy-widget/edit/control.js +137 -167
- package/build/blocks/legacy-widget/edit/control.js.map +7 -1
- package/build/blocks/legacy-widget/edit/convert-to-blocks-button.js +51 -39
- package/build/blocks/legacy-widget/edit/convert-to-blocks-button.js.map +7 -1
- package/build/blocks/legacy-widget/edit/form.js +105 -75
- package/build/blocks/legacy-widget/edit/form.js.map +7 -1
- package/build/blocks/legacy-widget/edit/index.js +121 -131
- package/build/blocks/legacy-widget/edit/index.js.map +7 -1
- package/build/blocks/legacy-widget/edit/inspector-card.js +28 -20
- package/build/blocks/legacy-widget/edit/inspector-card.js.map +7 -1
- package/build/blocks/legacy-widget/edit/no-preview.js +29 -23
- package/build/blocks/legacy-widget/edit/no-preview.js.map +7 -1
- package/build/blocks/legacy-widget/edit/preview.js +107 -108
- package/build/blocks/legacy-widget/edit/preview.js.map +7 -1
- package/build/blocks/legacy-widget/edit/widget-type-selector.js +61 -52
- package/build/blocks/legacy-widget/edit/widget-type-selector.js.map +7 -1
- package/build/blocks/legacy-widget/index.js +49 -53
- package/build/blocks/legacy-widget/index.js.map +7 -1
- package/build/blocks/legacy-widget/transforms.js +189 -198
- package/build/blocks/legacy-widget/transforms.js.map +7 -1
- package/build/blocks/widget-group/block.json +20 -0
- package/build/blocks/widget-group/deprecated.js +38 -24
- package/build/blocks/widget-group/deprecated.js.map +7 -1
- package/build/blocks/widget-group/edit.js +63 -67
- package/build/blocks/widget-group/edit.js.map +7 -1
- package/build/blocks/widget-group/index.js +93 -80
- package/build/blocks/widget-group/index.js.map +7 -1
- package/build/blocks/widget-group/save.js +36 -25
- package/build/blocks/widget-group/save.js.map +7 -1
- package/build/components/index.js +36 -12
- package/build/components/index.js.map +7 -1
- package/build/components/move-to-widget-area/index.js +48 -37
- package/build/components/move-to-widget-area/index.js.map +7 -1
- package/build/index.js +67 -98
- package/build/index.js.map +7 -1
- package/build/register-legacy-widget-variations.js +42 -30
- package/build/register-legacy-widget-variations.js.map +7 -1
- package/build/utils.js +29 -29
- package/build/utils.js.map +7 -1
- package/build-module/blocks/legacy-widget/block.json +29 -0
- package/build-module/blocks/legacy-widget/edit/control.js +107 -158
- package/build-module/blocks/legacy-widget/edit/control.js.map +7 -1
- package/build-module/blocks/legacy-widget/edit/convert-to-blocks-button.js +33 -33
- package/build-module/blocks/legacy-widget/edit/convert-to-blocks-button.js.map +7 -1
- package/build-module/blocks/legacy-widget/edit/form.js +75 -64
- package/build-module/blocks/legacy-widget/edit/form.js.map +7 -1
- package/build-module/blocks/legacy-widget/edit/index.js +96 -122
- package/build-module/blocks/legacy-widget/edit/index.js.map +7 -1
- package/build-module/blocks/legacy-widget/edit/inspector-card.js +10 -15
- package/build-module/blocks/legacy-widget/edit/inspector-card.js.map +7 -1
- package/build-module/blocks/legacy-widget/edit/no-preview.js +11 -17
- package/build-module/blocks/legacy-widget/edit/no-preview.js.map +7 -1
- package/build-module/blocks/legacy-widget/edit/preview.js +76 -98
- package/build-module/blocks/legacy-widget/edit/preview.js.map +7 -1
- package/build-module/blocks/legacy-widget/edit/widget-type-selector.js +43 -46
- package/build-module/blocks/legacy-widget/edit/widget-type-selector.js.map +7 -1
- package/build-module/blocks/legacy-widget/index.js +12 -45
- package/build-module/blocks/legacy-widget/index.js.map +7 -1
- package/build-module/blocks/legacy-widget/transforms.js +171 -192
- package/build-module/blocks/legacy-widget/transforms.js.map +7 -1
- package/build-module/blocks/widget-group/block.json +20 -0
- package/build-module/blocks/widget-group/deprecated.js +20 -18
- package/build-module/blocks/widget-group/deprecated.js.map +7 -1
- package/build-module/blocks/widget-group/edit.js +53 -62
- package/build-module/blocks/widget-group/edit.js.map +7 -1
- package/build-module/blocks/widget-group/index.js +55 -71
- package/build-module/blocks/widget-group/index.js.map +7 -1
- package/build-module/blocks/widget-group/save.js +18 -19
- package/build-module/blocks/widget-group/save.js.map +7 -1
- package/build-module/components/index.js +5 -2
- package/build-module/components/index.js.map +7 -1
- package/build-module/components/move-to-widget-area/index.js +37 -32
- package/build-module/components/move-to-widget-area/index.js.map +7 -1
- package/build-module/index.js +34 -60
- package/build-module/index.js.map +7 -1
- package/build-module/register-legacy-widget-variations.js +24 -24
- package/build-module/register-legacy-widget-variations.js.map +7 -1
- package/build-module/utils.js +8 -25
- package/build-module/utils.js.map +7 -1
- package/build-style/style-rtl.css +10 -145
- package/build-style/style.css +10 -145
- package/package.json +21 -14
- package/src/blocks/legacy-widget/editor.scss +3 -0
- package/src/blocks/widget-group/editor.scss +3 -0
- package/src/style.scss +3 -2
|
@@ -1,21 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import apiFetch from '@wordpress/api-fetch';
|
|
6
|
-
import { debounce } from '@wordpress/compose';
|
|
7
|
-
import { __ } from '@wordpress/i18n';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* An API for creating and loading a widget control (a <div class="widget">
|
|
11
|
-
* element) that is compatible with most third party widget scripts. By not
|
|
12
|
-
* using React for this, we ensure that we have complete control over the DOM
|
|
13
|
-
* and do not accidentally remove any elements that a third party widget script
|
|
14
|
-
* has attached an event listener to.
|
|
15
|
-
*
|
|
16
|
-
* @property {Element} element The control's DOM element.
|
|
17
|
-
*/
|
|
18
|
-
export default class Control {
|
|
1
|
+
import apiFetch from "@wordpress/api-fetch";
|
|
2
|
+
import { debounce } from "@wordpress/compose";
|
|
3
|
+
import { __ } from "@wordpress/i18n";
|
|
4
|
+
class Control {
|
|
19
5
|
/**
|
|
20
6
|
* Creates and loads a new control.
|
|
21
7
|
*
|
|
@@ -43,18 +29,16 @@ export default class Control {
|
|
|
43
29
|
this.onChangeInstance = onChangeInstance;
|
|
44
30
|
this.onChangeHasPreview = onChangeHasPreview;
|
|
45
31
|
this.onError = onError;
|
|
46
|
-
|
|
47
|
-
// We can't use the real widget number as this is calculated by the
|
|
48
|
-
// server and we may not ever *actually* save this widget. Instead, use
|
|
49
|
-
// a fake but unique number.
|
|
50
32
|
this.number = ++lastNumber;
|
|
51
|
-
this.handleFormChange = debounce(
|
|
33
|
+
this.handleFormChange = debounce(
|
|
34
|
+
this.handleFormChange.bind(this),
|
|
35
|
+
200
|
|
36
|
+
);
|
|
52
37
|
this.handleFormSubmit = this.handleFormSubmit.bind(this);
|
|
53
38
|
this.initDOM();
|
|
54
39
|
this.bindEvents();
|
|
55
40
|
this.loadContent();
|
|
56
41
|
}
|
|
57
|
-
|
|
58
42
|
/**
|
|
59
43
|
* Clean up the control so that it can be garbage collected.
|
|
60
44
|
*
|
|
@@ -63,84 +47,79 @@ export default class Control {
|
|
|
63
47
|
destroy() {
|
|
64
48
|
this.unbindEvents();
|
|
65
49
|
this.element.remove();
|
|
66
|
-
// TODO: How do we make third party widget scripts remove their event
|
|
67
|
-
// listeners?
|
|
68
50
|
}
|
|
69
|
-
|
|
70
51
|
/**
|
|
71
52
|
* Creates the control's DOM structure.
|
|
72
53
|
*
|
|
73
54
|
* @access private
|
|
74
55
|
*/
|
|
75
56
|
initDOM() {
|
|
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
|
-
|
|
57
|
+
this.element = el("div", { class: "widget open" }, [
|
|
58
|
+
el("div", { class: "widget-inside" }, [
|
|
59
|
+
this.form = el("form", { class: "form", method: "post" }, [
|
|
60
|
+
// These hidden form inputs are what most widgets' scripts
|
|
61
|
+
// use to access data about the widget.
|
|
62
|
+
el("input", {
|
|
63
|
+
class: "widget-id",
|
|
64
|
+
type: "hidden",
|
|
65
|
+
name: "widget-id",
|
|
66
|
+
value: this.id ?? `${this.idBase}-${this.number}`
|
|
67
|
+
}),
|
|
68
|
+
el("input", {
|
|
69
|
+
class: "id_base",
|
|
70
|
+
type: "hidden",
|
|
71
|
+
name: "id_base",
|
|
72
|
+
value: this.idBase ?? this.id
|
|
73
|
+
}),
|
|
74
|
+
el("input", {
|
|
75
|
+
class: "widget-width",
|
|
76
|
+
type: "hidden",
|
|
77
|
+
name: "widget-width",
|
|
78
|
+
value: "250"
|
|
79
|
+
}),
|
|
80
|
+
el("input", {
|
|
81
|
+
class: "widget-height",
|
|
82
|
+
type: "hidden",
|
|
83
|
+
name: "widget-height",
|
|
84
|
+
value: "200"
|
|
85
|
+
}),
|
|
86
|
+
el("input", {
|
|
87
|
+
class: "widget_number",
|
|
88
|
+
type: "hidden",
|
|
89
|
+
name: "widget_number",
|
|
90
|
+
value: this.idBase ? this.number.toString() : ""
|
|
91
|
+
}),
|
|
92
|
+
this.content = el("div", { class: "widget-content" }),
|
|
93
|
+
// Non-multi widgets can be saved via a Save button.
|
|
94
|
+
this.id && el(
|
|
95
|
+
"button",
|
|
96
|
+
{
|
|
97
|
+
class: "button is-primary",
|
|
98
|
+
type: "submit"
|
|
99
|
+
},
|
|
100
|
+
__("Save")
|
|
101
|
+
)
|
|
102
|
+
])
|
|
103
|
+
])
|
|
104
|
+
]);
|
|
120
105
|
}
|
|
121
|
-
|
|
122
106
|
/**
|
|
123
107
|
* Adds the control's event listeners.
|
|
124
108
|
*
|
|
125
109
|
* @access private
|
|
126
110
|
*/
|
|
127
111
|
bindEvents() {
|
|
128
|
-
// Prefer jQuery 'change' event instead of the native 'change' event
|
|
129
|
-
// because many widgets use jQuery's event bus to trigger an update.
|
|
130
112
|
if (window.jQuery) {
|
|
131
|
-
const {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
$(this.form).on(
|
|
135
|
-
$(this.form).on('input', null, this.handleFormChange);
|
|
136
|
-
$(this.form).on('submit', this.handleFormSubmit);
|
|
113
|
+
const { jQuery: $ } = window;
|
|
114
|
+
$(this.form).on("change", null, this.handleFormChange);
|
|
115
|
+
$(this.form).on("input", null, this.handleFormChange);
|
|
116
|
+
$(this.form).on("submit", this.handleFormSubmit);
|
|
137
117
|
} else {
|
|
138
|
-
this.form.addEventListener(
|
|
139
|
-
this.form.addEventListener(
|
|
140
|
-
this.form.addEventListener(
|
|
118
|
+
this.form.addEventListener("change", this.handleFormChange);
|
|
119
|
+
this.form.addEventListener("input", this.handleFormChange);
|
|
120
|
+
this.form.addEventListener("submit", this.handleFormSubmit);
|
|
141
121
|
}
|
|
142
122
|
}
|
|
143
|
-
|
|
144
123
|
/**
|
|
145
124
|
* Removes the control's event listeners.
|
|
146
125
|
*
|
|
@@ -148,19 +127,16 @@ export default class Control {
|
|
|
148
127
|
*/
|
|
149
128
|
unbindEvents() {
|
|
150
129
|
if (window.jQuery) {
|
|
151
|
-
const {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
$(this.form).off(
|
|
155
|
-
$(this.form).off('input', null, this.handleFormChange);
|
|
156
|
-
$(this.form).off('submit', this.handleFormSubmit);
|
|
130
|
+
const { jQuery: $ } = window;
|
|
131
|
+
$(this.form).off("change", null, this.handleFormChange);
|
|
132
|
+
$(this.form).off("input", null, this.handleFormChange);
|
|
133
|
+
$(this.form).off("submit", this.handleFormSubmit);
|
|
157
134
|
} else {
|
|
158
|
-
this.form.removeEventListener(
|
|
159
|
-
this.form.removeEventListener(
|
|
160
|
-
this.form.removeEventListener(
|
|
135
|
+
this.form.removeEventListener("change", this.handleFormChange);
|
|
136
|
+
this.form.removeEventListener("input", this.handleFormChange);
|
|
137
|
+
this.form.removeEventListener("submit", this.handleFormSubmit);
|
|
161
138
|
}
|
|
162
139
|
}
|
|
163
|
-
|
|
164
140
|
/**
|
|
165
141
|
* Fetches the widget's form HTML from the REST API and loads it into the
|
|
166
142
|
* control's form.
|
|
@@ -170,28 +146,18 @@ export default class Control {
|
|
|
170
146
|
async loadContent() {
|
|
171
147
|
try {
|
|
172
148
|
if (this.id) {
|
|
173
|
-
const {
|
|
174
|
-
form
|
|
175
|
-
} = await saveWidget(this.id);
|
|
149
|
+
const { form } = await saveWidget(this.id);
|
|
176
150
|
this.content.innerHTML = form;
|
|
177
151
|
} else if (this.idBase) {
|
|
178
|
-
const {
|
|
179
|
-
form,
|
|
180
|
-
preview
|
|
181
|
-
} = await encodeWidget({
|
|
152
|
+
const { form, preview } = await encodeWidget({
|
|
182
153
|
idBase: this.idBase,
|
|
183
154
|
instance: this.instance,
|
|
184
155
|
number: this.number
|
|
185
156
|
});
|
|
186
157
|
this.content.innerHTML = form;
|
|
187
158
|
this.hasPreview = !isEmptyHTML(preview);
|
|
188
|
-
|
|
189
|
-
// If we don't have an instance, perform a save right away. This
|
|
190
|
-
// happens when creating a new Legacy Widget block.
|
|
191
159
|
if (!this.instance.hash) {
|
|
192
|
-
const {
|
|
193
|
-
instance
|
|
194
|
-
} = await encodeWidget({
|
|
160
|
+
const { instance } = await encodeWidget({
|
|
195
161
|
idBase: this.idBase,
|
|
196
162
|
instance: this.instance,
|
|
197
163
|
number: this.number,
|
|
@@ -200,23 +166,14 @@ export default class Control {
|
|
|
200
166
|
this.instance = instance;
|
|
201
167
|
}
|
|
202
168
|
}
|
|
203
|
-
|
|
204
|
-
// Trigger 'widget-added' when widget is ready. This event is what
|
|
205
|
-
// widgets' scripts use to initialize, attach events, etc. The event
|
|
206
|
-
// must be fired using jQuery's event bus as this is what widget
|
|
207
|
-
// scripts expect. If jQuery is not loaded, do nothing - some
|
|
208
|
-
// widgets will still work regardless.
|
|
209
169
|
if (window.jQuery) {
|
|
210
|
-
const {
|
|
211
|
-
|
|
212
|
-
} = window;
|
|
213
|
-
$(document).trigger('widget-added', [$(this.element)]);
|
|
170
|
+
const { jQuery: $ } = window;
|
|
171
|
+
$(document).trigger("widget-added", [$(this.element)]);
|
|
214
172
|
}
|
|
215
173
|
} catch (error) {
|
|
216
174
|
this.onError(error);
|
|
217
175
|
}
|
|
218
176
|
}
|
|
219
|
-
|
|
220
177
|
/**
|
|
221
178
|
* Perform a save when a multi widget's form is changed. Non-multi widgets
|
|
222
179
|
* are saved manually.
|
|
@@ -228,7 +185,6 @@ export default class Control {
|
|
|
228
185
|
this.saveForm();
|
|
229
186
|
}
|
|
230
187
|
}
|
|
231
|
-
|
|
232
188
|
/**
|
|
233
189
|
* Perform a save when the control's form is manually submitted.
|
|
234
190
|
*
|
|
@@ -239,7 +195,6 @@ export default class Control {
|
|
|
239
195
|
event.preventDefault();
|
|
240
196
|
this.saveForm();
|
|
241
197
|
}
|
|
242
|
-
|
|
243
198
|
/**
|
|
244
199
|
* Serialize the control's form, send it to the REST API, and update the
|
|
245
200
|
* instance with the encoded instance that the REST API returns.
|
|
@@ -250,21 +205,16 @@ export default class Control {
|
|
|
250
205
|
const formData = serializeForm(this.form);
|
|
251
206
|
try {
|
|
252
207
|
if (this.id) {
|
|
253
|
-
const {
|
|
254
|
-
form
|
|
255
|
-
} = await saveWidget(this.id, formData);
|
|
208
|
+
const { form } = await saveWidget(this.id, formData);
|
|
256
209
|
this.content.innerHTML = form;
|
|
257
210
|
if (window.jQuery) {
|
|
258
|
-
const {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
211
|
+
const { jQuery: $ } = window;
|
|
212
|
+
$(document).trigger("widget-updated", [
|
|
213
|
+
$(this.element)
|
|
214
|
+
]);
|
|
262
215
|
}
|
|
263
216
|
} else if (this.idBase) {
|
|
264
|
-
const {
|
|
265
|
-
instance,
|
|
266
|
-
preview
|
|
267
|
-
} = await encodeWidget({
|
|
217
|
+
const { instance, preview } = await encodeWidget({
|
|
268
218
|
idBase: this.idBase,
|
|
269
219
|
instance: this.instance,
|
|
270
220
|
number: this.number,
|
|
@@ -277,7 +227,6 @@ export default class Control {
|
|
|
277
227
|
this.onError(error);
|
|
278
228
|
}
|
|
279
229
|
}
|
|
280
|
-
|
|
281
230
|
/**
|
|
282
231
|
* The widget's instance object.
|
|
283
232
|
*
|
|
@@ -286,7 +235,6 @@ export default class Control {
|
|
|
286
235
|
get instance() {
|
|
287
236
|
return this._instance;
|
|
288
237
|
}
|
|
289
|
-
|
|
290
238
|
/**
|
|
291
239
|
* The widget's instance object.
|
|
292
240
|
*
|
|
@@ -298,7 +246,6 @@ export default class Control {
|
|
|
298
246
|
this.onChangeInstance(instance);
|
|
299
247
|
}
|
|
300
248
|
}
|
|
301
|
-
|
|
302
249
|
/**
|
|
303
250
|
* Whether or not the widget can be previewed.
|
|
304
251
|
*
|
|
@@ -307,7 +254,6 @@ export default class Control {
|
|
|
307
254
|
get hasPreview() {
|
|
308
255
|
return this._hasPreview;
|
|
309
256
|
}
|
|
310
|
-
|
|
311
257
|
/**
|
|
312
258
|
* Whether or not the widget can be previewed.
|
|
313
259
|
*
|
|
@@ -332,7 +278,7 @@ function el(tagName, attributes = {}, content = null) {
|
|
|
332
278
|
element.appendChild(child);
|
|
333
279
|
}
|
|
334
280
|
}
|
|
335
|
-
} else if (typeof content ===
|
|
281
|
+
} else if (typeof content === "string") {
|
|
336
282
|
element.innerText = content;
|
|
337
283
|
}
|
|
338
284
|
return element;
|
|
@@ -342,7 +288,7 @@ async function saveWidget(id, formData = null) {
|
|
|
342
288
|
if (formData) {
|
|
343
289
|
widget = await apiFetch({
|
|
344
290
|
path: `/wp/v2/widgets/${id}?context=edit`,
|
|
345
|
-
method:
|
|
291
|
+
method: "PUT",
|
|
346
292
|
data: {
|
|
347
293
|
form_data: formData
|
|
348
294
|
}
|
|
@@ -350,22 +296,15 @@ async function saveWidget(id, formData = null) {
|
|
|
350
296
|
} else {
|
|
351
297
|
widget = await apiFetch({
|
|
352
298
|
path: `/wp/v2/widgets/${id}?context=edit`,
|
|
353
|
-
method:
|
|
299
|
+
method: "GET"
|
|
354
300
|
});
|
|
355
301
|
}
|
|
356
|
-
return {
|
|
357
|
-
form: widget.rendered_form
|
|
358
|
-
};
|
|
302
|
+
return { form: widget.rendered_form };
|
|
359
303
|
}
|
|
360
|
-
async function encodeWidget({
|
|
361
|
-
idBase,
|
|
362
|
-
instance,
|
|
363
|
-
number,
|
|
364
|
-
formData = null
|
|
365
|
-
}) {
|
|
304
|
+
async function encodeWidget({ idBase, instance, number, formData = null }) {
|
|
366
305
|
const response = await apiFetch({
|
|
367
306
|
path: `/wp/v2/widget-types/${idBase}/encode`,
|
|
368
|
-
method:
|
|
307
|
+
method: "POST",
|
|
369
308
|
data: {
|
|
370
309
|
instance,
|
|
371
310
|
number,
|
|
@@ -379,32 +318,42 @@ async function encodeWidget({
|
|
|
379
318
|
};
|
|
380
319
|
}
|
|
381
320
|
function isEmptyHTML(html) {
|
|
382
|
-
const element = document.createElement(
|
|
321
|
+
const element = document.createElement("div");
|
|
383
322
|
element.innerHTML = html;
|
|
384
323
|
return isEmptyNode(element);
|
|
385
324
|
}
|
|
386
325
|
function isEmptyNode(node) {
|
|
387
326
|
switch (node.nodeType) {
|
|
388
327
|
case node.TEXT_NODE:
|
|
389
|
-
|
|
390
|
-
return node.nodeValue.trim() === '';
|
|
328
|
+
return node.nodeValue.trim() === "";
|
|
391
329
|
case node.ELEMENT_NODE:
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
330
|
+
if ([
|
|
331
|
+
"AUDIO",
|
|
332
|
+
"CANVAS",
|
|
333
|
+
"EMBED",
|
|
334
|
+
"IFRAME",
|
|
335
|
+
"IMG",
|
|
336
|
+
"MATH",
|
|
337
|
+
"OBJECT",
|
|
338
|
+
"SVG",
|
|
339
|
+
"VIDEO"
|
|
340
|
+
].includes(node.tagName)) {
|
|
395
341
|
return false;
|
|
396
342
|
}
|
|
397
|
-
// Elements with no children are empty.
|
|
398
343
|
if (!node.hasChildNodes()) {
|
|
399
344
|
return true;
|
|
400
345
|
}
|
|
401
|
-
// Elements with children are empty if all their children are empty.
|
|
402
346
|
return Array.from(node.childNodes).every(isEmptyNode);
|
|
403
347
|
default:
|
|
404
348
|
return true;
|
|
405
349
|
}
|
|
406
350
|
}
|
|
407
351
|
function serializeForm(form) {
|
|
408
|
-
return new window.URLSearchParams(
|
|
352
|
+
return new window.URLSearchParams(
|
|
353
|
+
Array.from(new window.FormData(form))
|
|
354
|
+
).toString();
|
|
409
355
|
}
|
|
410
|
-
|
|
356
|
+
export {
|
|
357
|
+
Control as default
|
|
358
|
+
};
|
|
359
|
+
//# sourceMappingURL=control.js.map
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{"version":3,"names":["apiFetch","debounce","__","Control","constructor","id","idBase","instance","onChangeInstance","onChangeHasPreview","onError","_instance","_hasPreview","number","lastNumber","handleFormChange","bind","handleFormSubmit","initDOM","bindEvents","loadContent","destroy","unbindEvents","element","remove","_this$id","_this$idBase","el","class","form","method","type","name","value","toString","content","window","jQuery","$","on","addEventListener","off","removeEventListener","saveWidget","innerHTML","preview","encodeWidget","hasPreview","isEmptyHTML","hash","formData","serializeForm","document","trigger","error","saveForm","event","preventDefault","tagName","attributes","createElement","attribute","Object","entries","setAttribute","Array","isArray","child","appendChild","innerText","widget","path","data","form_data","rendered_form","response","html","isEmptyNode","node","nodeType","TEXT_NODE","nodeValue","trim","ELEMENT_NODE","includes","hasChildNodes","from","childNodes","every","URLSearchParams","FormData"],"sources":["@wordpress/widgets/src/blocks/legacy-widget/edit/control.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\nimport { debounce } from '@wordpress/compose';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * An API for creating and loading a widget control (a <div class=\"widget\">\n * element) that is compatible with most third party widget scripts. By not\n * using React for this, we ensure that we have complete control over the DOM\n * and do not accidentally remove any elements that a third party widget script\n * has attached an event listener to.\n *\n * @property {Element} element The control's DOM element.\n */\nexport default class Control {\n\t/**\n\t * Creates and loads a new control.\n\t *\n\t * @access public\n\t * @param {Object} params\n\t * @param {string} params.id\n\t * @param {string} params.idBase\n\t * @param {Object} params.instance\n\t * @param {Function} params.onChangeInstance\n\t * @param {Function} params.onChangeHasPreview\n\t * @param {Function} params.onError\n\t */\n\tconstructor( {\n\t\tid,\n\t\tidBase,\n\t\tinstance,\n\t\tonChangeInstance,\n\t\tonChangeHasPreview,\n\t\tonError,\n\t} ) {\n\t\tthis.id = id;\n\t\tthis.idBase = idBase;\n\t\tthis._instance = instance;\n\t\tthis._hasPreview = null;\n\t\tthis.onChangeInstance = onChangeInstance;\n\t\tthis.onChangeHasPreview = onChangeHasPreview;\n\t\tthis.onError = onError;\n\n\t\t// We can't use the real widget number as this is calculated by the\n\t\t// server and we may not ever *actually* save this widget. Instead, use\n\t\t// a fake but unique number.\n\t\tthis.number = ++lastNumber;\n\n\t\tthis.handleFormChange = debounce(\n\t\t\tthis.handleFormChange.bind( this ),\n\t\t\t200\n\t\t);\n\t\tthis.handleFormSubmit = this.handleFormSubmit.bind( this );\n\n\t\tthis.initDOM();\n\t\tthis.bindEvents();\n\t\tthis.loadContent();\n\t}\n\n\t/**\n\t * Clean up the control so that it can be garbage collected.\n\t *\n\t * @access public\n\t */\n\tdestroy() {\n\t\tthis.unbindEvents();\n\t\tthis.element.remove();\n\t\t// TODO: How do we make third party widget scripts remove their event\n\t\t// listeners?\n\t}\n\n\t/**\n\t * Creates the control's DOM structure.\n\t *\n\t * @access private\n\t */\n\tinitDOM() {\n\t\tthis.element = el( 'div', { class: 'widget open' }, [\n\t\t\tel( 'div', { class: 'widget-inside' }, [\n\t\t\t\t( this.form = el( 'form', { class: 'form', method: 'post' }, [\n\t\t\t\t\t// These hidden form inputs are what most widgets' scripts\n\t\t\t\t\t// use to access data about the widget.\n\t\t\t\t\tel( 'input', {\n\t\t\t\t\t\tclass: 'widget-id',\n\t\t\t\t\t\ttype: 'hidden',\n\t\t\t\t\t\tname: 'widget-id',\n\t\t\t\t\t\tvalue: this.id ?? `${ this.idBase }-${ this.number }`,\n\t\t\t\t\t} ),\n\t\t\t\t\tel( 'input', {\n\t\t\t\t\t\tclass: 'id_base',\n\t\t\t\t\t\ttype: 'hidden',\n\t\t\t\t\t\tname: 'id_base',\n\t\t\t\t\t\tvalue: this.idBase ?? this.id,\n\t\t\t\t\t} ),\n\t\t\t\t\tel( 'input', {\n\t\t\t\t\t\tclass: 'widget-width',\n\t\t\t\t\t\ttype: 'hidden',\n\t\t\t\t\t\tname: 'widget-width',\n\t\t\t\t\t\tvalue: '250',\n\t\t\t\t\t} ),\n\t\t\t\t\tel( 'input', {\n\t\t\t\t\t\tclass: 'widget-height',\n\t\t\t\t\t\ttype: 'hidden',\n\t\t\t\t\t\tname: 'widget-height',\n\t\t\t\t\t\tvalue: '200',\n\t\t\t\t\t} ),\n\t\t\t\t\tel( 'input', {\n\t\t\t\t\t\tclass: 'widget_number',\n\t\t\t\t\t\ttype: 'hidden',\n\t\t\t\t\t\tname: 'widget_number',\n\t\t\t\t\t\tvalue: this.idBase ? this.number.toString() : '',\n\t\t\t\t\t} ),\n\t\t\t\t\t( this.content = el( 'div', { class: 'widget-content' } ) ),\n\t\t\t\t\t// Non-multi widgets can be saved via a Save button.\n\t\t\t\t\tthis.id &&\n\t\t\t\t\t\tel(\n\t\t\t\t\t\t\t'button',\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tclass: 'button is-primary',\n\t\t\t\t\t\t\t\ttype: 'submit',\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t__( 'Save' )\n\t\t\t\t\t\t),\n\t\t\t\t] ) ),\n\t\t\t] ),\n\t\t] );\n\t}\n\n\t/**\n\t * Adds the control's event listeners.\n\t *\n\t * @access private\n\t */\n\tbindEvents() {\n\t\t// Prefer jQuery 'change' event instead of the native 'change' event\n\t\t// because many widgets use jQuery's event bus to trigger an update.\n\t\tif ( window.jQuery ) {\n\t\t\tconst { jQuery: $ } = window;\n\t\t\t$( this.form ).on( 'change', null, this.handleFormChange );\n\t\t\t$( this.form ).on( 'input', null, this.handleFormChange );\n\t\t\t$( this.form ).on( 'submit', this.handleFormSubmit );\n\t\t} else {\n\t\t\tthis.form.addEventListener( 'change', this.handleFormChange );\n\t\t\tthis.form.addEventListener( 'input', this.handleFormChange );\n\t\t\tthis.form.addEventListener( 'submit', this.handleFormSubmit );\n\t\t}\n\t}\n\n\t/**\n\t * Removes the control's event listeners.\n\t *\n\t * @access private\n\t */\n\tunbindEvents() {\n\t\tif ( window.jQuery ) {\n\t\t\tconst { jQuery: $ } = window;\n\t\t\t$( this.form ).off( 'change', null, this.handleFormChange );\n\t\t\t$( this.form ).off( 'input', null, this.handleFormChange );\n\t\t\t$( this.form ).off( 'submit', this.handleFormSubmit );\n\t\t} else {\n\t\t\tthis.form.removeEventListener( 'change', this.handleFormChange );\n\t\t\tthis.form.removeEventListener( 'input', this.handleFormChange );\n\t\t\tthis.form.removeEventListener( 'submit', this.handleFormSubmit );\n\t\t}\n\t}\n\n\t/**\n\t * Fetches the widget's form HTML from the REST API and loads it into the\n\t * control's form.\n\t *\n\t * @access private\n\t */\n\tasync loadContent() {\n\t\ttry {\n\t\t\tif ( this.id ) {\n\t\t\t\tconst { form } = await saveWidget( this.id );\n\t\t\t\tthis.content.innerHTML = form;\n\t\t\t} else if ( this.idBase ) {\n\t\t\t\tconst { form, preview } = await encodeWidget( {\n\t\t\t\t\tidBase: this.idBase,\n\t\t\t\t\tinstance: this.instance,\n\t\t\t\t\tnumber: this.number,\n\t\t\t\t} );\n\t\t\t\tthis.content.innerHTML = form;\n\t\t\t\tthis.hasPreview = ! isEmptyHTML( preview );\n\n\t\t\t\t// If we don't have an instance, perform a save right away. This\n\t\t\t\t// happens when creating a new Legacy Widget block.\n\t\t\t\tif ( ! this.instance.hash ) {\n\t\t\t\t\tconst { instance } = await encodeWidget( {\n\t\t\t\t\t\tidBase: this.idBase,\n\t\t\t\t\t\tinstance: this.instance,\n\t\t\t\t\t\tnumber: this.number,\n\t\t\t\t\t\tformData: serializeForm( this.form ),\n\t\t\t\t\t} );\n\t\t\t\t\tthis.instance = instance;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Trigger 'widget-added' when widget is ready. This event is what\n\t\t\t// widgets' scripts use to initialize, attach events, etc. The event\n\t\t\t// must be fired using jQuery's event bus as this is what widget\n\t\t\t// scripts expect. If jQuery is not loaded, do nothing - some\n\t\t\t// widgets will still work regardless.\n\t\t\tif ( window.jQuery ) {\n\t\t\t\tconst { jQuery: $ } = window;\n\t\t\t\t$( document ).trigger( 'widget-added', [ $( this.element ) ] );\n\t\t\t}\n\t\t} catch ( error ) {\n\t\t\tthis.onError( error );\n\t\t}\n\t}\n\n\t/**\n\t * Perform a save when a multi widget's form is changed. Non-multi widgets\n\t * are saved manually.\n\t *\n\t * @access private\n\t */\n\thandleFormChange() {\n\t\tif ( this.idBase ) {\n\t\t\tthis.saveForm();\n\t\t}\n\t}\n\n\t/**\n\t * Perform a save when the control's form is manually submitted.\n\t *\n\t * @access private\n\t * @param {Event} event\n\t */\n\thandleFormSubmit( event ) {\n\t\tevent.preventDefault();\n\t\tthis.saveForm();\n\t}\n\n\t/**\n\t * Serialize the control's form, send it to the REST API, and update the\n\t * instance with the encoded instance that the REST API returns.\n\t *\n\t * @access private\n\t */\n\tasync saveForm() {\n\t\tconst formData = serializeForm( this.form );\n\n\t\ttry {\n\t\t\tif ( this.id ) {\n\t\t\t\tconst { form } = await saveWidget( this.id, formData );\n\t\t\t\tthis.content.innerHTML = form;\n\n\t\t\t\tif ( window.jQuery ) {\n\t\t\t\t\tconst { jQuery: $ } = window;\n\t\t\t\t\t$( document ).trigger( 'widget-updated', [\n\t\t\t\t\t\t$( this.element ),\n\t\t\t\t\t] );\n\t\t\t\t}\n\t\t\t} else if ( this.idBase ) {\n\t\t\t\tconst { instance, preview } = await encodeWidget( {\n\t\t\t\t\tidBase: this.idBase,\n\t\t\t\t\tinstance: this.instance,\n\t\t\t\t\tnumber: this.number,\n\t\t\t\t\tformData,\n\t\t\t\t} );\n\t\t\t\tthis.instance = instance;\n\t\t\t\tthis.hasPreview = ! isEmptyHTML( preview );\n\t\t\t}\n\t\t} catch ( error ) {\n\t\t\tthis.onError( error );\n\t\t}\n\t}\n\n\t/**\n\t * The widget's instance object.\n\t *\n\t * @access private\n\t */\n\tget instance() {\n\t\treturn this._instance;\n\t}\n\n\t/**\n\t * The widget's instance object.\n\t *\n\t * @access private\n\t */\n\tset instance( instance ) {\n\t\tif ( this._instance !== instance ) {\n\t\t\tthis._instance = instance;\n\t\t\tthis.onChangeInstance( instance );\n\t\t}\n\t}\n\n\t/**\n\t * Whether or not the widget can be previewed.\n\t *\n\t * @access public\n\t */\n\tget hasPreview() {\n\t\treturn this._hasPreview;\n\t}\n\n\t/**\n\t * Whether or not the widget can be previewed.\n\t *\n\t * @access private\n\t */\n\tset hasPreview( hasPreview ) {\n\t\tif ( this._hasPreview !== hasPreview ) {\n\t\t\tthis._hasPreview = hasPreview;\n\t\t\tthis.onChangeHasPreview( hasPreview );\n\t\t}\n\t}\n}\n\nlet lastNumber = 0;\n\nfunction el( tagName, attributes = {}, content = null ) {\n\tconst element = document.createElement( tagName );\n\tfor ( const [ attribute, value ] of Object.entries( attributes ) ) {\n\t\telement.setAttribute( attribute, value );\n\t}\n\tif ( Array.isArray( content ) ) {\n\t\tfor ( const child of content ) {\n\t\t\tif ( child ) {\n\t\t\t\telement.appendChild( child );\n\t\t\t}\n\t\t}\n\t} else if ( typeof content === 'string' ) {\n\t\telement.innerText = content;\n\t}\n\treturn element;\n}\n\nasync function saveWidget( id, formData = null ) {\n\tlet widget;\n\tif ( formData ) {\n\t\twidget = await apiFetch( {\n\t\t\tpath: `/wp/v2/widgets/${ id }?context=edit`,\n\t\t\tmethod: 'PUT',\n\t\t\tdata: {\n\t\t\t\tform_data: formData,\n\t\t\t},\n\t\t} );\n\t} else {\n\t\twidget = await apiFetch( {\n\t\t\tpath: `/wp/v2/widgets/${ id }?context=edit`,\n\t\t\tmethod: 'GET',\n\t\t} );\n\t}\n\treturn { form: widget.rendered_form };\n}\n\nasync function encodeWidget( { idBase, instance, number, formData = null } ) {\n\tconst response = await apiFetch( {\n\t\tpath: `/wp/v2/widget-types/${ idBase }/encode`,\n\t\tmethod: 'POST',\n\t\tdata: {\n\t\t\tinstance,\n\t\t\tnumber,\n\t\t\tform_data: formData,\n\t\t},\n\t} );\n\treturn {\n\t\tinstance: response.instance,\n\t\tform: response.form,\n\t\tpreview: response.preview,\n\t};\n}\n\nfunction isEmptyHTML( html ) {\n\tconst element = document.createElement( 'div' );\n\telement.innerHTML = html;\n\treturn isEmptyNode( element );\n}\n\nfunction isEmptyNode( node ) {\n\tswitch ( node.nodeType ) {\n\t\tcase node.TEXT_NODE:\n\t\t\t// Text nodes are empty if it's entirely whitespace.\n\t\t\treturn node.nodeValue.trim() === '';\n\t\tcase node.ELEMENT_NODE:\n\t\t\t// Elements that are \"embedded content\" are not empty.\n\t\t\t// https://dev.w3.org/html5/spec-LC/content-models.html#embedded-content-0\n\t\t\tif (\n\t\t\t\t[\n\t\t\t\t\t'AUDIO',\n\t\t\t\t\t'CANVAS',\n\t\t\t\t\t'EMBED',\n\t\t\t\t\t'IFRAME',\n\t\t\t\t\t'IMG',\n\t\t\t\t\t'MATH',\n\t\t\t\t\t'OBJECT',\n\t\t\t\t\t'SVG',\n\t\t\t\t\t'VIDEO',\n\t\t\t\t].includes( node.tagName )\n\t\t\t) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\t// Elements with no children are empty.\n\t\t\tif ( ! node.hasChildNodes() ) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\t// Elements with children are empty if all their children are empty.\n\t\t\treturn Array.from( node.childNodes ).every( isEmptyNode );\n\t\tdefault:\n\t\t\treturn true;\n\t}\n}\n\nfunction serializeForm( form ) {\n\treturn new window.URLSearchParams(\n\t\tArray.from( new window.FormData( form ) )\n\t).toString();\n}\n"],"mappings":";AAAA;AACA;AACA;AACA,OAAOA,QAAQ,MAAM,sBAAsB;AAC3C,SAASC,QAAQ,QAAQ,oBAAoB;AAC7C,SAASC,EAAE,QAAQ,iBAAiB;;AAEpC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,MAAMC,OAAO,CAAC;EAC5B;AACD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;EACCC,WAAWA,CAAE;IACZC,EAAE;IACFC,MAAM;IACNC,QAAQ;IACRC,gBAAgB;IAChBC,kBAAkB;IAClBC;EACD,CAAC,EAAG;IACH,IAAI,CAACL,EAAE,GAAGA,EAAE;IACZ,IAAI,CAACC,MAAM,GAAGA,MAAM;IACpB,IAAI,CAACK,SAAS,GAAGJ,QAAQ;IACzB,IAAI,CAACK,WAAW,GAAG,IAAI;IACvB,IAAI,CAACJ,gBAAgB,GAAGA,gBAAgB;IACxC,IAAI,CAACC,kBAAkB,GAAGA,kBAAkB;IAC5C,IAAI,CAACC,OAAO,GAAGA,OAAO;;IAEtB;IACA;IACA;IACA,IAAI,CAACG,MAAM,GAAG,EAAEC,UAAU;IAE1B,IAAI,CAACC,gBAAgB,GAAGd,QAAQ,CAC/B,IAAI,CAACc,gBAAgB,CAACC,IAAI,CAAE,IAAK,CAAC,EAClC,GACD,CAAC;IACD,IAAI,CAACC,gBAAgB,GAAG,IAAI,CAACA,gBAAgB,CAACD,IAAI,CAAE,IAAK,CAAC;IAE1D,IAAI,CAACE,OAAO,CAAC,CAAC;IACd,IAAI,CAACC,UAAU,CAAC,CAAC;IACjB,IAAI,CAACC,WAAW,CAAC,CAAC;EACnB;;EAEA;AACD;AACA;AACA;AACA;EACCC,OAAOA,CAAA,EAAG;IACT,IAAI,CAACC,YAAY,CAAC,CAAC;IACnB,IA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EACP,QAAQ,EACR,OAAO,EACP,QAAQ,EACR,KAAK,EACL,MAAM,EACN,QAAQ,EACR,KAAK,EACL,OAAO,CACP,CAACC,QAAQ,CAAEN,IAAI,CAACpB,OAAQ,CAAC,EACzB;QACD,OAAO,KAAK;MACb;MACA;MACA,IAAK,CAAEoB,IAAI,CAACO,aAAa,CAAC,CAAC,EAAG;QAC7B,OAAO,IAAI;MACZ;MACA;MACA,OAAOpB,KAAK,CAACqB,IAAI,CAAER,IAAI,CAACS,UAAW,CAAC,CAACC,KAAK,CAAEX,WAAY,CAAC;IAC1D;MACC,OAAO,IAAI;EACb;AACD;AAEA,SAAS1B,aAAaA,CAAEtB,IAAI,EAAG;EAC9B,OAAO,IAAIO,MAAM,CAACqD,eAAe,CAChCxB,KAAK,CAACqB,IAAI,CAAE,IAAIlD,MAAM,CAACsD,QAAQ,CAAE7D,IAAK,CAAE,CACzC,CAAC,CAACK,QAAQ,CAAC,CAAC;AACb","ignoreList":[]}
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/blocks/legacy-widget/edit/control.js"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\nimport { debounce } from '@wordpress/compose';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * An API for creating and loading a widget control (a <div class=\"widget\">\n * element) that is compatible with most third party widget scripts. By not\n * using React for this, we ensure that we have complete control over the DOM\n * and do not accidentally remove any elements that a third party widget script\n * has attached an event listener to.\n *\n * @property {Element} element The control's DOM element.\n */\nexport default class Control {\n\t/**\n\t * Creates and loads a new control.\n\t *\n\t * @access public\n\t * @param {Object} params\n\t * @param {string} params.id\n\t * @param {string} params.idBase\n\t * @param {Object} params.instance\n\t * @param {Function} params.onChangeInstance\n\t * @param {Function} params.onChangeHasPreview\n\t * @param {Function} params.onError\n\t */\n\tconstructor( {\n\t\tid,\n\t\tidBase,\n\t\tinstance,\n\t\tonChangeInstance,\n\t\tonChangeHasPreview,\n\t\tonError,\n\t} ) {\n\t\tthis.id = id;\n\t\tthis.idBase = idBase;\n\t\tthis._instance = instance;\n\t\tthis._hasPreview = null;\n\t\tthis.onChangeInstance = onChangeInstance;\n\t\tthis.onChangeHasPreview = onChangeHasPreview;\n\t\tthis.onError = onError;\n\n\t\t// We can't use the real widget number as this is calculated by the\n\t\t// server and we may not ever *actually* save this widget. Instead, use\n\t\t// a fake but unique number.\n\t\tthis.number = ++lastNumber;\n\n\t\tthis.handleFormChange = debounce(\n\t\t\tthis.handleFormChange.bind( this ),\n\t\t\t200\n\t\t);\n\t\tthis.handleFormSubmit = this.handleFormSubmit.bind( this );\n\n\t\tthis.initDOM();\n\t\tthis.bindEvents();\n\t\tthis.loadContent();\n\t}\n\n\t/**\n\t * Clean up the control so that it can be garbage collected.\n\t *\n\t * @access public\n\t */\n\tdestroy() {\n\t\tthis.unbindEvents();\n\t\tthis.element.remove();\n\t\t// TODO: How do we make third party widget scripts remove their event\n\t\t// listeners?\n\t}\n\n\t/**\n\t * Creates the control's DOM structure.\n\t *\n\t * @access private\n\t */\n\tinitDOM() {\n\t\tthis.element = el( 'div', { class: 'widget open' }, [\n\t\t\tel( 'div', { class: 'widget-inside' }, [\n\t\t\t\t( this.form = el( 'form', { class: 'form', method: 'post' }, [\n\t\t\t\t\t// These hidden form inputs are what most widgets' scripts\n\t\t\t\t\t// use to access data about the widget.\n\t\t\t\t\tel( 'input', {\n\t\t\t\t\t\tclass: 'widget-id',\n\t\t\t\t\t\ttype: 'hidden',\n\t\t\t\t\t\tname: 'widget-id',\n\t\t\t\t\t\tvalue: this.id ?? `${ this.idBase }-${ this.number }`,\n\t\t\t\t\t} ),\n\t\t\t\t\tel( 'input', {\n\t\t\t\t\t\tclass: 'id_base',\n\t\t\t\t\t\ttype: 'hidden',\n\t\t\t\t\t\tname: 'id_base',\n\t\t\t\t\t\tvalue: this.idBase ?? this.id,\n\t\t\t\t\t} ),\n\t\t\t\t\tel( 'input', {\n\t\t\t\t\t\tclass: 'widget-width',\n\t\t\t\t\t\ttype: 'hidden',\n\t\t\t\t\t\tname: 'widget-width',\n\t\t\t\t\t\tvalue: '250',\n\t\t\t\t\t} ),\n\t\t\t\t\tel( 'input', {\n\t\t\t\t\t\tclass: 'widget-height',\n\t\t\t\t\t\ttype: 'hidden',\n\t\t\t\t\t\tname: 'widget-height',\n\t\t\t\t\t\tvalue: '200',\n\t\t\t\t\t} ),\n\t\t\t\t\tel( 'input', {\n\t\t\t\t\t\tclass: 'widget_number',\n\t\t\t\t\t\ttype: 'hidden',\n\t\t\t\t\t\tname: 'widget_number',\n\t\t\t\t\t\tvalue: this.idBase ? this.number.toString() : '',\n\t\t\t\t\t} ),\n\t\t\t\t\t( this.content = el( 'div', { class: 'widget-content' } ) ),\n\t\t\t\t\t// Non-multi widgets can be saved via a Save button.\n\t\t\t\t\tthis.id &&\n\t\t\t\t\t\tel(\n\t\t\t\t\t\t\t'button',\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\tclass: 'button is-primary',\n\t\t\t\t\t\t\t\ttype: 'submit',\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t__( 'Save' )\n\t\t\t\t\t\t),\n\t\t\t\t] ) ),\n\t\t\t] ),\n\t\t] );\n\t}\n\n\t/**\n\t * Adds the control's event listeners.\n\t *\n\t * @access private\n\t */\n\tbindEvents() {\n\t\t// Prefer jQuery 'change' event instead of the native 'change' event\n\t\t// because many widgets use jQuery's event bus to trigger an update.\n\t\tif ( window.jQuery ) {\n\t\t\tconst { jQuery: $ } = window;\n\t\t\t$( this.form ).on( 'change', null, this.handleFormChange );\n\t\t\t$( this.form ).on( 'input', null, this.handleFormChange );\n\t\t\t$( this.form ).on( 'submit', this.handleFormSubmit );\n\t\t} else {\n\t\t\tthis.form.addEventListener( 'change', this.handleFormChange );\n\t\t\tthis.form.addEventListener( 'input', this.handleFormChange );\n\t\t\tthis.form.addEventListener( 'submit', this.handleFormSubmit );\n\t\t}\n\t}\n\n\t/**\n\t * Removes the control's event listeners.\n\t *\n\t * @access private\n\t */\n\tunbindEvents() {\n\t\tif ( window.jQuery ) {\n\t\t\tconst { jQuery: $ } = window;\n\t\t\t$( this.form ).off( 'change', null, this.handleFormChange );\n\t\t\t$( this.form ).off( 'input', null, this.handleFormChange );\n\t\t\t$( this.form ).off( 'submit', this.handleFormSubmit );\n\t\t} else {\n\t\t\tthis.form.removeEventListener( 'change', this.handleFormChange );\n\t\t\tthis.form.removeEventListener( 'input', this.handleFormChange );\n\t\t\tthis.form.removeEventListener( 'submit', this.handleFormSubmit );\n\t\t}\n\t}\n\n\t/**\n\t * Fetches the widget's form HTML from the REST API and loads it into the\n\t * control's form.\n\t *\n\t * @access private\n\t */\n\tasync loadContent() {\n\t\ttry {\n\t\t\tif ( this.id ) {\n\t\t\t\tconst { form } = await saveWidget( this.id );\n\t\t\t\tthis.content.innerHTML = form;\n\t\t\t} else if ( this.idBase ) {\n\t\t\t\tconst { form, preview } = await encodeWidget( {\n\t\t\t\t\tidBase: this.idBase,\n\t\t\t\t\tinstance: this.instance,\n\t\t\t\t\tnumber: this.number,\n\t\t\t\t} );\n\t\t\t\tthis.content.innerHTML = form;\n\t\t\t\tthis.hasPreview = ! isEmptyHTML( preview );\n\n\t\t\t\t// If we don't have an instance, perform a save right away. This\n\t\t\t\t// happens when creating a new Legacy Widget block.\n\t\t\t\tif ( ! this.instance.hash ) {\n\t\t\t\t\tconst { instance } = await encodeWidget( {\n\t\t\t\t\t\tidBase: this.idBase,\n\t\t\t\t\t\tinstance: this.instance,\n\t\t\t\t\t\tnumber: this.number,\n\t\t\t\t\t\tformData: serializeForm( this.form ),\n\t\t\t\t\t} );\n\t\t\t\t\tthis.instance = instance;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Trigger 'widget-added' when widget is ready. This event is what\n\t\t\t// widgets' scripts use to initialize, attach events, etc. The event\n\t\t\t// must be fired using jQuery's event bus as this is what widget\n\t\t\t// scripts expect. If jQuery is not loaded, do nothing - some\n\t\t\t// widgets will still work regardless.\n\t\t\tif ( window.jQuery ) {\n\t\t\t\tconst { jQuery: $ } = window;\n\t\t\t\t$( document ).trigger( 'widget-added', [ $( this.element ) ] );\n\t\t\t}\n\t\t} catch ( error ) {\n\t\t\tthis.onError( error );\n\t\t}\n\t}\n\n\t/**\n\t * Perform a save when a multi widget's form is changed. Non-multi widgets\n\t * are saved manually.\n\t *\n\t * @access private\n\t */\n\thandleFormChange() {\n\t\tif ( this.idBase ) {\n\t\t\tthis.saveForm();\n\t\t}\n\t}\n\n\t/**\n\t * Perform a save when the control's form is manually submitted.\n\t *\n\t * @access private\n\t * @param {Event} event\n\t */\n\thandleFormSubmit( event ) {\n\t\tevent.preventDefault();\n\t\tthis.saveForm();\n\t}\n\n\t/**\n\t * Serialize the control's form, send it to the REST API, and update the\n\t * instance with the encoded instance that the REST API returns.\n\t *\n\t * @access private\n\t */\n\tasync saveForm() {\n\t\tconst formData = serializeForm( this.form );\n\n\t\ttry {\n\t\t\tif ( this.id ) {\n\t\t\t\tconst { form } = await saveWidget( this.id, formData );\n\t\t\t\tthis.content.innerHTML = form;\n\n\t\t\t\tif ( window.jQuery ) {\n\t\t\t\t\tconst { jQuery: $ } = window;\n\t\t\t\t\t$( document ).trigger( 'widget-updated', [\n\t\t\t\t\t\t$( this.element ),\n\t\t\t\t\t] );\n\t\t\t\t}\n\t\t\t} else if ( this.idBase ) {\n\t\t\t\tconst { instance, preview } = await encodeWidget( {\n\t\t\t\t\tidBase: this.idBase,\n\t\t\t\t\tinstance: this.instance,\n\t\t\t\t\tnumber: this.number,\n\t\t\t\t\tformData,\n\t\t\t\t} );\n\t\t\t\tthis.instance = instance;\n\t\t\t\tthis.hasPreview = ! isEmptyHTML( preview );\n\t\t\t}\n\t\t} catch ( error ) {\n\t\t\tthis.onError( error );\n\t\t}\n\t}\n\n\t/**\n\t * The widget's instance object.\n\t *\n\t * @access private\n\t */\n\tget instance() {\n\t\treturn this._instance;\n\t}\n\n\t/**\n\t * The widget's instance object.\n\t *\n\t * @access private\n\t */\n\tset instance( instance ) {\n\t\tif ( this._instance !== instance ) {\n\t\t\tthis._instance = instance;\n\t\t\tthis.onChangeInstance( instance );\n\t\t}\n\t}\n\n\t/**\n\t * Whether or not the widget can be previewed.\n\t *\n\t * @access public\n\t */\n\tget hasPreview() {\n\t\treturn this._hasPreview;\n\t}\n\n\t/**\n\t * Whether or not the widget can be previewed.\n\t *\n\t * @access private\n\t */\n\tset hasPreview( hasPreview ) {\n\t\tif ( this._hasPreview !== hasPreview ) {\n\t\t\tthis._hasPreview = hasPreview;\n\t\t\tthis.onChangeHasPreview( hasPreview );\n\t\t}\n\t}\n}\n\nlet lastNumber = 0;\n\nfunction el( tagName, attributes = {}, content = null ) {\n\tconst element = document.createElement( tagName );\n\tfor ( const [ attribute, value ] of Object.entries( attributes ) ) {\n\t\telement.setAttribute( attribute, value );\n\t}\n\tif ( Array.isArray( content ) ) {\n\t\tfor ( const child of content ) {\n\t\t\tif ( child ) {\n\t\t\t\telement.appendChild( child );\n\t\t\t}\n\t\t}\n\t} else if ( typeof content === 'string' ) {\n\t\telement.innerText = content;\n\t}\n\treturn element;\n}\n\nasync function saveWidget( id, formData = null ) {\n\tlet widget;\n\tif ( formData ) {\n\t\twidget = await apiFetch( {\n\t\t\tpath: `/wp/v2/widgets/${ id }?context=edit`,\n\t\t\tmethod: 'PUT',\n\t\t\tdata: {\n\t\t\t\tform_data: formData,\n\t\t\t},\n\t\t} );\n\t} else {\n\t\twidget = await apiFetch( {\n\t\t\tpath: `/wp/v2/widgets/${ id }?context=edit`,\n\t\t\tmethod: 'GET',\n\t\t} );\n\t}\n\treturn { form: widget.rendered_form };\n}\n\nasync function encodeWidget( { idBase, instance, number, formData = null } ) {\n\tconst response = await apiFetch( {\n\t\tpath: `/wp/v2/widget-types/${ idBase }/encode`,\n\t\tmethod: 'POST',\n\t\tdata: {\n\t\t\tinstance,\n\t\t\tnumber,\n\t\t\tform_data: formData,\n\t\t},\n\t} );\n\treturn {\n\t\tinstance: response.instance,\n\t\tform: response.form,\n\t\tpreview: response.preview,\n\t};\n}\n\nfunction isEmptyHTML( html ) {\n\tconst element = document.createElement( 'div' );\n\telement.innerHTML = html;\n\treturn isEmptyNode( element );\n}\n\nfunction isEmptyNode( node ) {\n\tswitch ( node.nodeType ) {\n\t\tcase node.TEXT_NODE:\n\t\t\t// Text nodes are empty if it's entirely whitespace.\n\t\t\treturn node.nodeValue.trim() === '';\n\t\tcase node.ELEMENT_NODE:\n\t\t\t// Elements that are \"embedded content\" are not empty.\n\t\t\t// https://dev.w3.org/html5/spec-LC/content-models.html#embedded-content-0\n\t\t\tif (\n\t\t\t\t[\n\t\t\t\t\t'AUDIO',\n\t\t\t\t\t'CANVAS',\n\t\t\t\t\t'EMBED',\n\t\t\t\t\t'IFRAME',\n\t\t\t\t\t'IMG',\n\t\t\t\t\t'MATH',\n\t\t\t\t\t'OBJECT',\n\t\t\t\t\t'SVG',\n\t\t\t\t\t'VIDEO',\n\t\t\t\t].includes( node.tagName )\n\t\t\t) {\n\t\t\t\treturn false;\n\t\t\t}\n\t\t\t// Elements with no children are empty.\n\t\t\tif ( ! node.hasChildNodes() ) {\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\t// Elements with children are empty if all their children are empty.\n\t\t\treturn Array.from( node.childNodes ).every( isEmptyNode );\n\t\tdefault:\n\t\t\treturn true;\n\t}\n}\n\nfunction serializeForm( form ) {\n\treturn new window.URLSearchParams(\n\t\tArray.from( new window.FormData( form ) )\n\t).toString();\n}\n"],
|
|
5
|
+
"mappings": "AAGA,OAAO,cAAc;AACrB,SAAS,gBAAgB;AACzB,SAAS,UAAU;AAWnB,MAAO,QAAsB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAa5B,YAAa;AAAA,IACZ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD,GAA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KAAK,UAAU,KAAK,MAAM;AAAA,IAClC,KAAK,KAAK;AAGT,UACC;AAAA,QACC;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACD,EAAE,SAAU,KAAK,OAAQ,GACxB;AACD,eAAO;AAAA,MACR;AAEA,UAAK,CAAE,KAAK,cAAc,GAAI;AAC7B,eAAO;AAAA,MACR;AAEA,aAAO,MAAM,KAAM,KAAK,UAAW,EAAE,MAAO,WAAY;AAAA,IACzD;AACC,aAAO;AAAA,EACT;AACD;AAEA,SAAS,cAAe,MAAO;AAC9B,SAAO,IAAI,OAAO;AAAA,IACjB,MAAM,KAAM,IAAI,OAAO,SAAU,IAAK,CAAE;AAAA,EACzC,EAAE,SAAS;AACZ;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
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
|
-
children: __('Convert to blocks')
|
|
32
|
-
});
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useDispatch } from "@wordpress/data";
|
|
3
|
+
import { store as blockEditorStore } from "@wordpress/block-editor";
|
|
4
|
+
import { ToolbarButton } from "@wordpress/components";
|
|
5
|
+
import { createBlock, rawHandler } from "@wordpress/blocks";
|
|
6
|
+
import { __ } from "@wordpress/i18n";
|
|
7
|
+
function ConvertToBlocksButton({ clientId, rawInstance }) {
|
|
8
|
+
const { replaceBlocks } = useDispatch(blockEditorStore);
|
|
9
|
+
return /* @__PURE__ */ jsx(
|
|
10
|
+
ToolbarButton,
|
|
11
|
+
{
|
|
12
|
+
onClick: () => {
|
|
13
|
+
if (rawInstance.title) {
|
|
14
|
+
replaceBlocks(clientId, [
|
|
15
|
+
createBlock("core/heading", {
|
|
16
|
+
content: rawInstance.title
|
|
17
|
+
}),
|
|
18
|
+
...rawHandler({ HTML: rawInstance.text })
|
|
19
|
+
]);
|
|
20
|
+
} else {
|
|
21
|
+
replaceBlocks(
|
|
22
|
+
clientId,
|
|
23
|
+
rawHandler({ HTML: rawInstance.text })
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
children: __("Convert to blocks")
|
|
28
|
+
}
|
|
29
|
+
);
|
|
33
30
|
}
|
|
34
|
-
|
|
31
|
+
export {
|
|
32
|
+
ConvertToBlocksButton as default
|
|
33
|
+
};
|
|
34
|
+
//# sourceMappingURL=convert-to-blocks-button.js.map
|
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../../src/blocks/legacy-widget/edit/convert-to-blocks-button.js"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useDispatch } from '@wordpress/data';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\nimport { ToolbarButton } from '@wordpress/components';\nimport { createBlock, rawHandler } from '@wordpress/blocks';\nimport { __ } from '@wordpress/i18n';\n\nexport default function ConvertToBlocksButton( { clientId, rawInstance } ) {\n\tconst { replaceBlocks } = useDispatch( blockEditorStore );\n\n\treturn (\n\t\t<ToolbarButton\n\t\t\tonClick={ () => {\n\t\t\t\tif ( rawInstance.title ) {\n\t\t\t\t\treplaceBlocks( clientId, [\n\t\t\t\t\t\tcreateBlock( 'core/heading', {\n\t\t\t\t\t\t\tcontent: rawInstance.title,\n\t\t\t\t\t\t} ),\n\t\t\t\t\t\t...rawHandler( { HTML: rawInstance.text } ),\n\t\t\t\t\t] );\n\t\t\t\t} else {\n\t\t\t\t\treplaceBlocks(\n\t\t\t\t\t\tclientId,\n\t\t\t\t\t\trawHandler( { HTML: rawInstance.text } )\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t} }\n\t\t>\n\t\t\t{ __( 'Convert to blocks' ) }\n\t\t</ToolbarButton>\n\t);\n}\n"],
|
|
5
|
+
"mappings": "AAaE;AAVF,SAAS,mBAAmB;AAC5B,SAAS,SAAS,wBAAwB;AAC1C,SAAS,qBAAqB;AAC9B,SAAS,aAAa,kBAAkB;AACxC,SAAS,UAAU;AAEJ,SAAR,sBAAwC,EAAE,UAAU,YAAY,GAAI;AAC1E,QAAM,EAAE,cAAc,IAAI,YAAa,gBAAiB;AAExD,SACC;AAAA,IAAC;AAAA;AAAA,MACA,SAAU,MAAM;AACf,YAAK,YAAY,OAAQ;AACxB,wBAAe,UAAU;AAAA,YACxB,YAAa,gBAAgB;AAAA,cAC5B,SAAS,YAAY;AAAA,YACtB,CAAE;AAAA,YACF,GAAG,WAAY,EAAE,MAAM,YAAY,KAAK,CAAE;AAAA,UAC3C,CAAE;AAAA,QACH,OAAO;AACN;AAAA,YACC;AAAA,YACA,WAAY,EAAE,MAAM,YAAY,KAAK,CAAE;AAAA,UACxC;AAAA,QACD;AAAA,MACD;AAAA,MAEE,aAAI,mBAAoB;AAAA;AAAA,EAC3B;AAEF;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|