PluginProbe
Gutenberg / 22.3.0
Gutenberg v22.3.0
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 22.3.0, at build/scripts/list-reusable-blocks/index.js

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