PluginProbe
Gutenberg / 23.7.2
Gutenberg v23.7.2
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / scripts / list-reusable-blocks / index.js

index.js in Gutenberg 23.7.2, at build/scripts/list-reusable-blocks/index.js

370 lines 13.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 "use strict";
2 var wp;
3 (wp ||= {}).listReusableBlocks = (() => {
4 var __create = Object.create;
5 var __defProp = Object.defineProperty;
6 var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7 var __getOwnPropNames = Object.getOwnPropertyNames;
8 var __getProtoOf = Object.getPrototypeOf;
9 var __hasOwnProp = Object.prototype.hasOwnProperty;
10 var __commonJS = (cb, mod) => function __require() {
11 return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
12 };
13 var __copyProps = (to, from, except, desc) => {
14 if (from && typeof from === "object" || typeof from === "function") {
15 for (let key of __getOwnPropNames(from))
16 if (!__hasOwnProp.call(to, key) && key !== except)
17 __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18 }
19 return to;
20 };
21 var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22 // If the importer is in node compatibility mode or this is not an ESM
23 // file that has been converted to a CommonJS file using a Babel-
24 // compatible transform (i.e. "__esModule" has not been set), then set
25 // "default" to the CommonJS "module.exports" for node compatibility.
26 isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27 mod
28 ));
29
30 // package-external:@wordpress/element
31 var require_element = __commonJS({
32 "package-external:@wordpress/element"(exports, module) {
33 module.exports = window.wp.element;
34 }
35 });
36
37 // package-external:@wordpress/i18n
38 var require_i18n = __commonJS({
39 "package-external:@wordpress/i18n"(exports, module) {
40 module.exports = window.wp.i18n;
41 }
42 });
43
44 // package-external:@wordpress/components
45 var require_components = __commonJS({
46 "package-external:@wordpress/components"(exports, module) {
47 module.exports = window.wp.components;
48 }
49 });
50
51 // package-external:@wordpress/compose
52 var require_compose = __commonJS({
53 "package-external:@wordpress/compose"(exports, module) {
54 module.exports = window.wp.compose;
55 }
56 });
57
58 // package-external:@wordpress/api-fetch
59 var require_api_fetch = __commonJS({
60 "package-external:@wordpress/api-fetch"(exports, module) {
61 module.exports = window.wp.apiFetch;
62 }
63 });
64
65 // vendor-external:react/jsx-runtime
66 var require_jsx_runtime = __commonJS({
67 "vendor-external:react/jsx-runtime"(exports, module) {
68 module.exports = window.ReactJSXRuntime;
69 }
70 });
71
72 // package-external:@wordpress/blob
73 var require_blob = __commonJS({
74 "package-external:@wordpress/blob"(exports, module) {
75 module.exports = window.wp.blob;
76 }
77 });
78
79 // packages/list-reusable-blocks/build-module/index.mjs
80 var import_element2 = __toESM(require_element(), 1);
81 var import_i18n3 = __toESM(require_i18n(), 1);
82
83 // packages/list-reusable-blocks/build-module/components/import-dropdown/index.mjs
84 var import_components2 = __toESM(require_components(), 1);
85 var import_compose2 = __toESM(require_compose(), 1);
86 var import_i18n2 = __toESM(require_i18n(), 1);
87
88 // packages/list-reusable-blocks/build-module/components/import-form/index.mjs
89 var import_components = __toESM(require_components(), 1);
90 var import_compose = __toESM(require_compose(), 1);
91 var import_element = __toESM(require_element(), 1);
92 var import_i18n = __toESM(require_i18n(), 1);
93
94 // packages/list-reusable-blocks/build-module/utils/import.mjs
95 var import_api_fetch = __toESM(require_api_fetch(), 1);
96
97 // packages/list-reusable-blocks/build-module/utils/file.mjs
98 function readTextFile(file) {
99 const reader = new window.FileReader();
100 return new Promise((resolve) => {
101 reader.onload = () => {
102 resolve(reader.result);
103 };
104 reader.readAsText(file);
105 });
106 }
107
108 // packages/list-reusable-blocks/build-module/utils/import.mjs
109 async function importReusableBlock(file) {
110 const fileContent = await readTextFile(file);
111 let parsedContent;
112 try {
113 parsedContent = JSON.parse(fileContent);
114 } catch {
115 throw new Error("Invalid JSON file");
116 }
117 if (parsedContent.__file !== "wp_block" || !parsedContent.title || !parsedContent.content || typeof parsedContent.title !== "string" || typeof parsedContent.content !== "string" || parsedContent.syncStatus && typeof parsedContent.syncStatus !== "string") {
118 throw new Error("Invalid pattern JSON file");
119 }
120 const postType = await (0, import_api_fetch.default)({
121 path: `/wp/v2/types/wp_block`
122 });
123 const reusableBlock = await (0, import_api_fetch.default)({
124 path: `/wp/v2/${postType.rest_base}`,
125 data: {
126 title: parsedContent.title,
127 content: parsedContent.content,
128 status: "publish",
129 meta: parsedContent.syncStatus === "unsynced" ? { wp_pattern_sync_status: parsedContent.syncStatus } : void 0
130 },
131 method: "POST"
132 });
133 return reusableBlock;
134 }
135 var import_default = importReusableBlock;
136
137 // packages/list-reusable-blocks/build-module/components/import-form/index.mjs
138 var import_jsx_runtime = __toESM(require_jsx_runtime(), 1);
139 function ImportForm({ instanceId, onUpload }) {
140 const inputId = "list-reusable-blocks-import-form-" + instanceId;
141 const formRef = (0, import_element.useRef)(null);
142 const [isLoading, setIsLoading] = (0, import_element.useState)(false);
143 const [error, setError] = (0, import_element.useState)(null);
144 const [file, setFile] = (0, import_element.useState)(null);
145 const onChangeFile = (event) => {
146 setFile(event.target.files?.[0] || null);
147 setError(null);
148 };
149 const onSubmit = (event) => {
150 event.preventDefault();
151 if (!file) {
152 return;
153 }
154 setIsLoading(true);
155 import_default(file).then((reusableBlock) => {
156 if (!formRef.current) {
157 return;
158 }
159 setIsLoading(false);
160 onUpload(reusableBlock);
161 }).catch((errors) => {
162 if (!formRef.current) {
163 return;
164 }
165 let uiMessage;
166 switch (errors.message) {
167 case "Invalid JSON file":
168 uiMessage = (0, import_i18n.__)("Invalid JSON file");
169 break;
170 case "Invalid pattern JSON file":
171 uiMessage = (0, import_i18n.__)("Invalid pattern JSON file");
172 break;
173 default:
174 uiMessage = (0, import_i18n.__)("Unknown error");
175 }
176 setIsLoading(false);
177 setError(uiMessage);
178 });
179 };
180 const onDismissError = () => {
181 setError(null);
182 };
183 return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
184 "form",
185 {
186 className: "list-reusable-blocks-import-form",
187 onSubmit,
188 ref: formRef,
189 children: [
190 error && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Notice, { status: "error", onRemove: () => onDismissError(), children: error }),
191 /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
192 "label",
193 {
194 htmlFor: inputId,
195 className: "list-reusable-blocks-import-form__label",
196 children: (0, import_i18n.__)("File")
197 }
198 ),
199 /* @__PURE__ */ (0, import_jsx_runtime.jsx)("input", { id: inputId, type: "file", onChange: onChangeFile }),
200 /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
201 import_components.Button,
202 {
203 __next40pxDefaultSize: true,
204 type: "submit",
205 isBusy: isLoading,
206 accessibleWhenDisabled: true,
207 disabled: !file || isLoading,
208 variant: "secondary",
209 className: "list-reusable-blocks-import-form__button",
210 children: (0, import_i18n._x)("Import", "button label")
211 }
212 )
213 ]
214 }
215 );
216 }
217 var import_form_default = (0, import_compose.withInstanceId)(ImportForm);
218
219 // packages/list-reusable-blocks/build-module/components/import-dropdown/index.mjs
220 var import_jsx_runtime2 = __toESM(require_jsx_runtime(), 1);
221 function ImportDropdown({ onUpload }) {
222 return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
223 import_components2.Dropdown,
224 {
225 popoverProps: { placement: "bottom-start" },
226 contentClassName: "list-reusable-blocks-import-dropdown__content",
227 renderToggle: ({ isOpen, onToggle }) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
228 import_components2.Button,
229 {
230 size: "compact",
231 className: "list-reusable-blocks-import-dropdown__button",
232 "aria-expanded": isOpen,
233 onClick: onToggle,
234 variant: "primary",
235 children: (0, import_i18n2.__)("Import from JSON")
236 }
237 ),
238 renderContent: ({ onClose }) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_form_default, { onUpload: (0, import_compose2.pipe)(onClose, onUpload) })
239 }
240 );
241 }
242 var import_dropdown_default = ImportDropdown;
243
244 // node_modules/tslib/tslib.es6.mjs
245 var __assign = function() {
246 __assign = Object.assign || function __assign2(t) {
247 for (var s, i = 1, n = arguments.length; i < n; i++) {
248 s = arguments[i];
249 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
250 }
251 return t;
252 };
253 return __assign.apply(this, arguments);
254 };
255
256 // node_modules/lower-case/dist.es2015/index.js
257 function lowerCase(str) {
258 return str.toLowerCase();
259 }
260
261 // node_modules/no-case/dist.es2015/index.js
262 var DEFAULT_SPLIT_REGEXP = [/([a-z0-9])([A-Z])/g, /([A-Z])([A-Z][a-z])/g];
263 var DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi;
264 function noCase(input, options) {
265 if (options === void 0) {
266 options = {};
267 }
268 var _a = options.splitRegexp, splitRegexp = _a === void 0 ? DEFAULT_SPLIT_REGEXP : _a, _b = options.stripRegexp, stripRegexp = _b === void 0 ? DEFAULT_STRIP_REGEXP : _b, _c = options.transform, transform = _c === void 0 ? lowerCase : _c, _d = options.delimiter, delimiter = _d === void 0 ? " " : _d;
269 var result = replace(replace(input, splitRegexp, "$1\0$2"), stripRegexp, "\0");
270 var start = 0;
271 var end = result.length;
272 while (result.charAt(start) === "\0")
273 start++;
274 while (result.charAt(end - 1) === "\0")
275 end--;
276 return result.slice(start, end).split("\0").map(transform).join(delimiter);
277 }
278 function replace(input, re, value) {
279 if (re instanceof RegExp)
280 return input.replace(re, value);
281 return re.reduce(function(input2, re2) {
282 return input2.replace(re2, value);
283 }, input);
284 }
285
286 // node_modules/dot-case/dist.es2015/index.js
287 function dotCase(input, options) {
288 if (options === void 0) {
289 options = {};
290 }
291 return noCase(input, __assign({ delimiter: "." }, options));
292 }
293
294 // node_modules/param-case/dist.es2015/index.js
295 function paramCase(input, options) {
296 if (options === void 0) {
297 options = {};
298 }
299 return dotCase(input, __assign({ delimiter: "-" }, options));
300 }
301
302 // packages/list-reusable-blocks/build-module/utils/export.mjs
303 var import_api_fetch2 = __toESM(require_api_fetch(), 1);
304 var import_blob = __toESM(require_blob(), 1);
305 async function exportReusableBlock(id) {
306 const postType = await (0, import_api_fetch2.default)({
307 path: `/wp/v2/types/wp_block`
308 });
309 const post = await (0, import_api_fetch2.default)({
310 path: `/wp/v2/${postType.rest_base}/${id}?context=edit`
311 });
312 const title = post.title.raw;
313 const content = post.content.raw;
314 const syncStatus = post.wp_pattern_sync_status;
315 const fileContent = JSON.stringify(
316 {
317 __file: "wp_block",
318 title,
319 content,
320 syncStatus
321 },
322 null,
323 2
324 );
325 const fileName = paramCase(title) + ".json";
326 (0, import_blob.downloadBlob)(fileName, fileContent, "application/json");
327 }
328 var export_default = exportReusableBlock;
329
330 // packages/list-reusable-blocks/build-module/index.mjs
331 var import_jsx_runtime3 = __toESM(require_jsx_runtime(), 1);
332 document.body.addEventListener("click", (event) => {
333 const target = event.target;
334 if (!target.classList.contains("wp-list-reusable-blocks__export")) {
335 return;
336 }
337 event.preventDefault();
338 const blockId = target.dataset.id;
339 if (blockId) {
340 export_default(Number(blockId));
341 }
342 });
343 document.addEventListener("DOMContentLoaded", () => {
344 const button = document.querySelector(".page-title-action");
345 if (!button) {
346 return;
347 }
348 const showNotice = () => {
349 const notice = document.createElement("div");
350 notice.className = "notice notice-success is-dismissible";
351 notice.innerHTML = `<p>${(0, import_i18n3.__)("Pattern imported successfully!")}</p>`;
352 const headerEnd = document.querySelector(".wp-header-end");
353 if (!headerEnd || !headerEnd.parentNode) {
354 return;
355 }
356 headerEnd.parentNode.insertBefore(notice, headerEnd);
357 };
358 const container = document.createElement("div");
359 container.className = "list-reusable-blocks__container";
360 if (!button.parentNode) {
361 return;
362 }
363 button.parentNode.insertBefore(container, button);
364 (0, import_element2.createRoot)(container).render(
365 /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_element2.StrictMode, { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_dropdown_default, { onUpload: showNotice }) })
366 );
367 });
368 })();
369 //# sourceMappingURL=index.js.map
370