wip
This commit is contained in:
+111
-176
@@ -36,6 +36,8 @@
|
||||
/************************************************************************/
|
||||
var __webpack_exports__ = {};
|
||||
|
||||
;// external "ReactJSXRuntime"
|
||||
const external_ReactJSXRuntime_namespaceObject = window["ReactJSXRuntime"];
|
||||
;// external ["wp","element"]
|
||||
const external_wp_element_namespaceObject = window["wp"]["element"];
|
||||
;// external ["wp","i18n"]
|
||||
@@ -546,61 +548,41 @@ var external_wp_apiFetch_default = /*#__PURE__*/__webpack_require__.n(external_w
|
||||
;// external ["wp","blob"]
|
||||
const external_wp_blob_namespaceObject = window["wp"]["blob"];
|
||||
;// ./node_modules/@wordpress/list-reusable-blocks/build-module/utils/export.js
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* WordPress dependencies
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Export a reusable block as a JSON file.
|
||||
*
|
||||
* @param {number} id
|
||||
*/
|
||||
async function exportReusableBlock(id) {
|
||||
const postType = await external_wp_apiFetch_default()({
|
||||
path: `/wp/v2/types/wp_block`
|
||||
});
|
||||
const postType = await external_wp_apiFetch_default()({ path: `/wp/v2/types/wp_block` });
|
||||
const post = await external_wp_apiFetch_default()({
|
||||
path: `/wp/v2/${postType.rest_base}/${id}?context=edit`
|
||||
});
|
||||
const title = post.title.raw;
|
||||
const content = post.content.raw;
|
||||
const syncStatus = post.wp_pattern_sync_status;
|
||||
const fileContent = JSON.stringify({
|
||||
__file: 'wp_block',
|
||||
title,
|
||||
content,
|
||||
syncStatus
|
||||
}, null, 2);
|
||||
const fileName = paramCase(title) + '.json';
|
||||
(0,external_wp_blob_namespaceObject.downloadBlob)(fileName, fileContent, 'application/json');
|
||||
const fileContent = JSON.stringify(
|
||||
{
|
||||
__file: "wp_block",
|
||||
title,
|
||||
content,
|
||||
syncStatus
|
||||
},
|
||||
null,
|
||||
2
|
||||
);
|
||||
const fileName = paramCase(title) + ".json";
|
||||
(0,external_wp_blob_namespaceObject.downloadBlob)(fileName, fileContent, "application/json");
|
||||
}
|
||||
/* harmony default export */ const utils_export = (exportReusableBlock);
|
||||
var export_default = exportReusableBlock;
|
||||
|
||||
|
||||
;// external ["wp","compose"]
|
||||
const external_wp_compose_namespaceObject = window["wp"]["compose"];
|
||||
;// external ["wp","components"]
|
||||
const external_wp_components_namespaceObject = window["wp"]["components"];
|
||||
;// ./node_modules/@wordpress/list-reusable-blocks/build-module/utils/file.js
|
||||
/**
|
||||
* Reads the textual content of the given file.
|
||||
*
|
||||
* @param {File} file File.
|
||||
* @return {Promise<string>} Content of the file.
|
||||
*/
|
||||
function readTextFile(file) {
|
||||
const reader = new window.FileReader();
|
||||
return new Promise(resolve => {
|
||||
return new Promise((resolve) => {
|
||||
reader.onload = () => {
|
||||
resolve(reader.result);
|
||||
};
|
||||
@@ -608,110 +590,80 @@ function readTextFile(file) {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
;// ./node_modules/@wordpress/list-reusable-blocks/build-module/utils/import.js
|
||||
/**
|
||||
* WordPress dependencies
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Import a reusable block from a JSON file.
|
||||
*
|
||||
* @param {File} file File.
|
||||
* @return {Promise} Promise returning the imported reusable block.
|
||||
*/
|
||||
async function importReusableBlock(file) {
|
||||
const fileContent = await readTextFile(file);
|
||||
let parsedContent;
|
||||
try {
|
||||
parsedContent = JSON.parse(fileContent);
|
||||
} catch (e) {
|
||||
throw new Error('Invalid JSON file');
|
||||
throw new Error("Invalid JSON file");
|
||||
}
|
||||
if (parsedContent.__file !== 'wp_block' || !parsedContent.title || !parsedContent.content || typeof parsedContent.title !== 'string' || typeof parsedContent.content !== 'string' || parsedContent.syncStatus && typeof parsedContent.syncStatus !== 'string') {
|
||||
throw new Error('Invalid pattern JSON file');
|
||||
if (parsedContent.__file !== "wp_block" || !parsedContent.title || !parsedContent.content || typeof parsedContent.title !== "string" || typeof parsedContent.content !== "string" || parsedContent.syncStatus && typeof parsedContent.syncStatus !== "string") {
|
||||
throw new Error("Invalid pattern JSON file");
|
||||
}
|
||||
const postType = await external_wp_apiFetch_default()({
|
||||
path: `/wp/v2/types/wp_block`
|
||||
});
|
||||
const postType = await external_wp_apiFetch_default()({ path: `/wp/v2/types/wp_block` });
|
||||
const reusableBlock = await external_wp_apiFetch_default()({
|
||||
path: `/wp/v2/${postType.rest_base}`,
|
||||
data: {
|
||||
title: parsedContent.title,
|
||||
content: parsedContent.content,
|
||||
status: 'publish',
|
||||
meta: parsedContent.syncStatus === 'unsynced' ? {
|
||||
wp_pattern_sync_status: parsedContent.syncStatus
|
||||
} : undefined
|
||||
status: "publish",
|
||||
meta: parsedContent.syncStatus === "unsynced" ? { wp_pattern_sync_status: parsedContent.syncStatus } : void 0
|
||||
},
|
||||
method: 'POST'
|
||||
method: "POST"
|
||||
});
|
||||
return reusableBlock;
|
||||
}
|
||||
/* harmony default export */ const utils_import = (importReusableBlock);
|
||||
var import_default = importReusableBlock;
|
||||
|
||||
|
||||
;// external "ReactJSXRuntime"
|
||||
const external_ReactJSXRuntime_namespaceObject = window["ReactJSXRuntime"];
|
||||
;// ./node_modules/@wordpress/list-reusable-blocks/build-module/components/import-form/index.js
|
||||
/**
|
||||
* WordPress dependencies
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
|
||||
|
||||
function ImportForm({
|
||||
instanceId,
|
||||
onUpload
|
||||
}) {
|
||||
const inputId = 'list-reusable-blocks-import-form-' + instanceId;
|
||||
function ImportForm({ instanceId, onUpload }) {
|
||||
const inputId = "list-reusable-blocks-import-form-" + instanceId;
|
||||
const formRef = (0,external_wp_element_namespaceObject.useRef)();
|
||||
const [isLoading, setIsLoading] = (0,external_wp_element_namespaceObject.useState)(false);
|
||||
const [error, setError] = (0,external_wp_element_namespaceObject.useState)(null);
|
||||
const [file, setFile] = (0,external_wp_element_namespaceObject.useState)(null);
|
||||
const onChangeFile = event => {
|
||||
const onChangeFile = (event) => {
|
||||
setFile(event.target.files[0]);
|
||||
setError(null);
|
||||
};
|
||||
const onSubmit = event => {
|
||||
const onSubmit = (event) => {
|
||||
event.preventDefault();
|
||||
if (!file) {
|
||||
return;
|
||||
}
|
||||
setIsLoading({
|
||||
isLoading: true
|
||||
});
|
||||
utils_import(file).then(reusableBlock => {
|
||||
setIsLoading({ isLoading: true });
|
||||
import_default(file).then((reusableBlock) => {
|
||||
if (!formRef) {
|
||||
return;
|
||||
}
|
||||
setIsLoading(false);
|
||||
onUpload(reusableBlock);
|
||||
}).catch(errors => {
|
||||
}).catch((errors) => {
|
||||
if (!formRef) {
|
||||
return;
|
||||
}
|
||||
let uiMessage;
|
||||
switch (errors.message) {
|
||||
case 'Invalid JSON file':
|
||||
uiMessage = (0,external_wp_i18n_namespaceObject.__)('Invalid JSON file');
|
||||
case "Invalid JSON file":
|
||||
uiMessage = (0,external_wp_i18n_namespaceObject.__)("Invalid JSON file");
|
||||
break;
|
||||
case 'Invalid pattern JSON file':
|
||||
uiMessage = (0,external_wp_i18n_namespaceObject.__)('Invalid pattern JSON file');
|
||||
case "Invalid pattern JSON file":
|
||||
uiMessage = (0,external_wp_i18n_namespaceObject.__)("Invalid pattern JSON file");
|
||||
break;
|
||||
default:
|
||||
uiMessage = (0,external_wp_i18n_namespaceObject.__)('Unknown error');
|
||||
uiMessage = (0,external_wp_i18n_namespaceObject.__)("Unknown error");
|
||||
}
|
||||
setIsLoading(false);
|
||||
setError(uiMessage);
|
||||
@@ -720,124 +672,107 @@ function ImportForm({
|
||||
const onDismissError = () => {
|
||||
setError(null);
|
||||
};
|
||||
return /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsxs)("form", {
|
||||
className: "list-reusable-blocks-import-form",
|
||||
onSubmit: onSubmit,
|
||||
ref: formRef,
|
||||
children: [error && /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)(external_wp_components_namespaceObject.Notice, {
|
||||
status: "error",
|
||||
onRemove: () => onDismissError(),
|
||||
children: error
|
||||
}), /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)("label", {
|
||||
htmlFor: inputId,
|
||||
className: "list-reusable-blocks-import-form__label",
|
||||
children: (0,external_wp_i18n_namespaceObject.__)('File')
|
||||
}), /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)("input", {
|
||||
id: inputId,
|
||||
type: "file",
|
||||
onChange: onChangeFile
|
||||
}), /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)(external_wp_components_namespaceObject.Button, {
|
||||
__next40pxDefaultSize: true,
|
||||
type: "submit",
|
||||
isBusy: isLoading,
|
||||
accessibleWhenDisabled: true,
|
||||
disabled: !file || isLoading,
|
||||
variant: "secondary",
|
||||
className: "list-reusable-blocks-import-form__button",
|
||||
children: (0,external_wp_i18n_namespaceObject._x)('Import', 'button label')
|
||||
})]
|
||||
});
|
||||
return /* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsxs)(
|
||||
"form",
|
||||
{
|
||||
className: "list-reusable-blocks-import-form",
|
||||
onSubmit,
|
||||
ref: formRef,
|
||||
children: [
|
||||
error && /* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsx)(external_wp_components_namespaceObject.Notice, { status: "error", onRemove: () => onDismissError(), children: error }),
|
||||
/* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsx)(
|
||||
"label",
|
||||
{
|
||||
htmlFor: inputId,
|
||||
className: "list-reusable-blocks-import-form__label",
|
||||
children: (0,external_wp_i18n_namespaceObject.__)("File")
|
||||
}
|
||||
),
|
||||
/* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsx)("input", { id: inputId, type: "file", onChange: onChangeFile }),
|
||||
/* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsx)(
|
||||
external_wp_components_namespaceObject.Button,
|
||||
{
|
||||
__next40pxDefaultSize: true,
|
||||
type: "submit",
|
||||
isBusy: isLoading,
|
||||
accessibleWhenDisabled: true,
|
||||
disabled: !file || isLoading,
|
||||
variant: "secondary",
|
||||
className: "list-reusable-blocks-import-form__button",
|
||||
children: (0,external_wp_i18n_namespaceObject._x)("Import", "button label")
|
||||
}
|
||||
)
|
||||
]
|
||||
}
|
||||
);
|
||||
}
|
||||
/* harmony default export */ const import_form = ((0,external_wp_compose_namespaceObject.withInstanceId)(ImportForm));
|
||||
var import_form_default = (0,external_wp_compose_namespaceObject.withInstanceId)(ImportForm);
|
||||
|
||||
|
||||
;// ./node_modules/@wordpress/list-reusable-blocks/build-module/components/import-dropdown/index.js
|
||||
/**
|
||||
* WordPress dependencies
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
|
||||
|
||||
function ImportDropdown({
|
||||
onUpload
|
||||
}) {
|
||||
return /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)(external_wp_components_namespaceObject.Dropdown, {
|
||||
popoverProps: {
|
||||
placement: 'bottom-start'
|
||||
},
|
||||
contentClassName: "list-reusable-blocks-import-dropdown__content",
|
||||
renderToggle: ({
|
||||
isOpen,
|
||||
onToggle
|
||||
}) => /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)(external_wp_components_namespaceObject.Button, {
|
||||
size: "compact",
|
||||
className: "list-reusable-blocks-import-dropdown__button",
|
||||
"aria-expanded": isOpen,
|
||||
onClick: onToggle,
|
||||
variant: "primary",
|
||||
children: (0,external_wp_i18n_namespaceObject.__)('Import from JSON')
|
||||
}),
|
||||
renderContent: ({
|
||||
onClose
|
||||
}) => /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)(import_form, {
|
||||
onUpload: (0,external_wp_compose_namespaceObject.pipe)(onClose, onUpload)
|
||||
})
|
||||
});
|
||||
function ImportDropdown({ onUpload }) {
|
||||
return /* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsx)(
|
||||
external_wp_components_namespaceObject.Dropdown,
|
||||
{
|
||||
popoverProps: { placement: "bottom-start" },
|
||||
contentClassName: "list-reusable-blocks-import-dropdown__content",
|
||||
renderToggle: ({ isOpen, onToggle }) => /* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsx)(
|
||||
external_wp_components_namespaceObject.Button,
|
||||
{
|
||||
size: "compact",
|
||||
className: "list-reusable-blocks-import-dropdown__button",
|
||||
"aria-expanded": isOpen,
|
||||
onClick: onToggle,
|
||||
variant: "primary",
|
||||
children: (0,external_wp_i18n_namespaceObject.__)("Import from JSON")
|
||||
}
|
||||
),
|
||||
renderContent: ({ onClose }) => /* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsx)(import_form_default, { onUpload: (0,external_wp_compose_namespaceObject.pipe)(onClose, onUpload) })
|
||||
}
|
||||
);
|
||||
}
|
||||
/* harmony default export */ const import_dropdown = (ImportDropdown);
|
||||
var import_dropdown_default = ImportDropdown;
|
||||
|
||||
|
||||
;// ./node_modules/@wordpress/list-reusable-blocks/build-module/index.js
|
||||
/**
|
||||
* WordPress dependencies
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
|
||||
|
||||
|
||||
// Setup Export Links.
|
||||
|
||||
document.body.addEventListener('click', event => {
|
||||
if (!event.target.classList.contains('wp-list-reusable-blocks__export')) {
|
||||
document.body.addEventListener("click", (event) => {
|
||||
if (!event.target.classList.contains("wp-list-reusable-blocks__export")) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
utils_export(event.target.dataset.id);
|
||||
export_default(event.target.dataset.id);
|
||||
});
|
||||
|
||||
// Setup Import Form.
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const button = document.querySelector('.page-title-action');
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
const button = document.querySelector(".page-title-action");
|
||||
if (!button) {
|
||||
return;
|
||||
}
|
||||
const showNotice = () => {
|
||||
const notice = document.createElement('div');
|
||||
notice.className = 'notice notice-success is-dismissible';
|
||||
notice.innerHTML = `<p>${(0,external_wp_i18n_namespaceObject.__)('Pattern imported successfully!')}</p>`;
|
||||
const headerEnd = document.querySelector('.wp-header-end');
|
||||
const notice = document.createElement("div");
|
||||
notice.className = "notice notice-success is-dismissible";
|
||||
notice.innerHTML = `<p>${(0,external_wp_i18n_namespaceObject.__)("Pattern imported successfully!")}</p>`;
|
||||
const headerEnd = document.querySelector(".wp-header-end");
|
||||
if (!headerEnd) {
|
||||
return;
|
||||
}
|
||||
headerEnd.parentNode.insertBefore(notice, headerEnd);
|
||||
};
|
||||
const container = document.createElement('div');
|
||||
container.className = 'list-reusable-blocks__container';
|
||||
const container = document.createElement("div");
|
||||
container.className = "list-reusable-blocks__container";
|
||||
button.parentNode.insertBefore(container, button);
|
||||
(0,external_wp_element_namespaceObject.createRoot)(container).render(/*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)(external_wp_element_namespaceObject.StrictMode, {
|
||||
children: /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)(import_dropdown, {
|
||||
onUpload: showNotice
|
||||
})
|
||||
}));
|
||||
(0,external_wp_element_namespaceObject.createRoot)(container).render(
|
||||
/* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsx)(external_wp_element_namespaceObject.StrictMode, { children: /* @__PURE__ */ (0,external_ReactJSXRuntime_namespaceObject.jsx)(import_dropdown_default, { onUpload: showNotice }) })
|
||||
);
|
||||
});
|
||||
|
||||
(window.wp = window.wp || {}).listReusableBlocks = __webpack_exports__;
|
||||
|
||||
Reference in New Issue
Block a user