} Content of the file.
*/
function readTextFile(file) {
const reader = new window.FileReader();
return new Promise(resolve => {
reader.onload = () => {
resolve(reader.result);
};
reader.readAsText(file);
});
}
;// CONCATENATED MODULE: ./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');
}
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 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
},
method: 'POST'
});
return reusableBlock;
}
/* harmony default export */ const utils_import = (importReusableBlock);
;// CONCATENATED MODULE: external "ReactJSXRuntime"
const external_ReactJSXRuntime_namespaceObject = window["ReactJSXRuntime"];
;// CONCATENATED MODULE: ./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;
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 => {
setFile(event.target.files[0]);
setError(null);
};
const onSubmit = event => {
event.preventDefault();
if (!file) {
return;
}
setIsLoading({
isLoading: true
});
utils_import(file).then(reusableBlock => {
if (!formRef) {
return;
}
setIsLoading(false);
onUpload(reusableBlock);
}).catch(errors => {
if (!formRef) {
return;
}
let uiMessage;
switch (errors.message) {
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');
break;
default:
uiMessage = (0,external_wp_i18n_namespaceObject.__)('Unknown error');
}
setIsLoading(false);
setError(uiMessage);
});
};
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, {
type: "submit",
isBusy: isLoading,
__experimentalIsFocusable: 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));
;// CONCATENATED MODULE: ./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, {
"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)
})
});
}
/* harmony default export */ const import_dropdown = (ImportDropdown);
;// CONCATENATED MODULE: ./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')) {
return;
}
event.preventDefault();
utils_export(event.target.dataset.id);
});
// Setup Import Form.
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 = `${(0,external_wp_i18n_namespaceObject.__)('Pattern imported successfully!')}
`;
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';
button.parentNode.insertBefore(container, button);
(0,external_wp_element_namespaceObject.createRoot)(container).render( /*#__PURE__*/(0,external_ReactJSXRuntime_namespaceObject.jsx)(import_dropdown, {
onUpload: showNotice
}));
});
(window.wp = window.wp || {}).listReusableBlocks = __webpack_exports__;
/******/ })()
;