@wordpress/list-reusable-blocks 5.43.0 → 5.44.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/CHANGELOG.md +2 -0
- package/build/components/import-dropdown/index.cjs +3 -2
- package/build/components/import-dropdown/index.cjs.map +3 -3
- package/build/components/import-form/index.cjs +8 -7
- package/build/components/import-form/index.cjs.map +3 -3
- package/build/index.cjs +13 -5
- package/build/index.cjs.map +3 -3
- package/build/utils/export.cjs +5 -2
- package/build/utils/export.cjs.map +3 -3
- package/build/utils/file.cjs +2 -1
- package/build/utils/file.cjs.map +3 -3
- package/build/utils/import.cjs +6 -3
- package/build/utils/import.cjs.map +3 -3
- package/build/utils/types.cjs +19 -0
- package/build/utils/types.cjs.map +7 -0
- package/build-module/components/import-dropdown/index.mjs +2 -2
- package/build-module/components/import-dropdown/index.mjs.map +3 -3
- package/build-module/components/import-form/index.mjs +7 -7
- package/build-module/components/import-form/index.mjs.map +3 -3
- package/build-module/index.mjs +12 -5
- package/build-module/index.mjs.map +3 -3
- package/build-module/utils/export.mjs +4 -2
- package/build-module/utils/export.mjs.map +3 -3
- package/build-module/utils/file.mjs +1 -1
- package/build-module/utils/file.mjs.map +3 -3
- package/build-module/utils/import.mjs +5 -3
- package/build-module/utils/import.mjs.map +3 -3
- package/build-module/utils/types.mjs +1 -0
- package/build-module/utils/types.mjs.map +7 -0
- package/build-types/components/import-dropdown/index.d.ts +7 -0
- package/build-types/components/import-dropdown/index.d.ts.map +1 -0
- package/build-types/components/import-form/index.d.ts +7 -0
- package/build-types/components/import-form/index.d.ts.map +1 -0
- package/build-types/index.d.ts +2 -0
- package/build-types/index.d.ts.map +1 -0
- package/build-types/utils/export.d.ts +8 -0
- package/build-types/utils/export.d.ts.map +1 -0
- package/build-types/utils/file.d.ts +8 -0
- package/build-types/utils/file.d.ts.map +1 -0
- package/build-types/utils/import.d.ts +10 -0
- package/build-types/utils/import.d.ts.map +1 -0
- package/build-types/utils/types.d.ts +45 -0
- package/build-types/utils/types.d.ts.map +1 -0
- package/package.json +12 -9
- package/src/components/import-dropdown/{index.js → index.tsx} +3 -2
- package/src/components/import-form/{index.js → index.tsx} +15 -14
- package/src/{index.js → index.tsx} +17 -7
- package/src/utils/{export.js → export.ts} +11 -4
- package/src/utils/{file.js → file.ts} +4 -4
- package/src/utils/{import.js → import.ts} +20 -8
- package/src/utils/types.ts +50 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -26,15 +27,15 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
26
27
|
));
|
|
27
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
29
|
|
|
29
|
-
// packages/list-reusable-blocks/src/components/import-dropdown/index.
|
|
30
|
+
// packages/list-reusable-blocks/src/components/import-dropdown/index.tsx
|
|
30
31
|
var import_dropdown_exports = {};
|
|
31
32
|
__export(import_dropdown_exports, {
|
|
32
33
|
default: () => import_dropdown_default
|
|
33
34
|
});
|
|
34
35
|
module.exports = __toCommonJS(import_dropdown_exports);
|
|
36
|
+
var import_components = require("@wordpress/components");
|
|
35
37
|
var import_compose = require("@wordpress/compose");
|
|
36
38
|
var import_i18n = require("@wordpress/i18n");
|
|
37
|
-
var import_components = require("@wordpress/components");
|
|
38
39
|
var import_import_form = __toESM(require("../import-form/index.cjs"));
|
|
39
40
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
40
41
|
function ImportDropdown({ onUpload }) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/components/import-dropdown/index.
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {
|
|
5
|
-
"mappings": "
|
|
3
|
+
"sources": ["../../../src/components/import-dropdown/index.tsx"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { Button, Dropdown } from '@wordpress/components';\nimport { pipe } from '@wordpress/compose';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport type { ImportDropdownProps } from '../../utils/types';\nimport ImportForm from '../import-form';\n\nfunction ImportDropdown( { onUpload }: ImportDropdownProps ) {\n\treturn (\n\t\t<Dropdown\n\t\t\tpopoverProps={ { placement: 'bottom-start' } }\n\t\t\tcontentClassName=\"list-reusable-blocks-import-dropdown__content\"\n\t\t\trenderToggle={ ( { isOpen, onToggle } ) => (\n\t\t\t\t<Button\n\t\t\t\t\tsize=\"compact\"\n\t\t\t\t\tclassName=\"list-reusable-blocks-import-dropdown__button\"\n\t\t\t\t\taria-expanded={ isOpen }\n\t\t\t\t\tonClick={ onToggle }\n\t\t\t\t\tvariant=\"primary\"\n\t\t\t\t>\n\t\t\t\t\t{ __( 'Import from JSON' ) }\n\t\t\t\t</Button>\n\t\t\t) }\n\t\t\trenderContent={ ( { onClose } ) => (\n\t\t\t\t<ImportForm onUpload={ pipe( onClose, onUpload ) } />\n\t\t\t) }\n\t\t/>\n\t);\n}\n\nexport default ImportDropdown;\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,wBAAiC;AACjC,qBAAqB;AACrB,kBAAmB;AAMnB,yBAAuB;AAQnB;AANJ,SAAS,eAAgB,EAAE,SAAS,GAAyB;AAC5D,SACC;AAAA,IAAC;AAAA;AAAA,MACA,cAAe,EAAE,WAAW,eAAe;AAAA,MAC3C,kBAAiB;AAAA,MACjB,cAAe,CAAE,EAAE,QAAQ,SAAS,MACnC;AAAA,QAAC;AAAA;AAAA,UACA,MAAK;AAAA,UACL,WAAU;AAAA,UACV,iBAAgB;AAAA,UAChB,SAAU;AAAA,UACV,SAAQ;AAAA,UAEN,8BAAI,kBAAmB;AAAA;AAAA,MAC1B;AAAA,MAED,eAAgB,CAAE,EAAE,QAAQ,MAC3B,4CAAC,mBAAAA,SAAA,EAAW,cAAW,qBAAM,SAAS,QAAS,GAAI;AAAA;AAAA,EAErD;AAEF;AAEA,IAAO,0BAAQ;",
|
|
6
6
|
"names": ["ImportForm"]
|
|
7
7
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -26,26 +27,26 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
26
27
|
));
|
|
27
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
29
|
|
|
29
|
-
// packages/list-reusable-blocks/src/components/import-form/index.
|
|
30
|
+
// packages/list-reusable-blocks/src/components/import-form/index.tsx
|
|
30
31
|
var import_form_exports = {};
|
|
31
32
|
__export(import_form_exports, {
|
|
32
33
|
default: () => import_form_default
|
|
33
34
|
});
|
|
34
35
|
module.exports = __toCommonJS(import_form_exports);
|
|
35
|
-
var
|
|
36
|
+
var import_components = require("@wordpress/components");
|
|
36
37
|
var import_compose = require("@wordpress/compose");
|
|
38
|
+
var import_element = require("@wordpress/element");
|
|
37
39
|
var import_i18n = require("@wordpress/i18n");
|
|
38
|
-
var import_components = require("@wordpress/components");
|
|
39
40
|
var import_import = __toESM(require("../../utils/import.cjs"));
|
|
40
41
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
41
42
|
function ImportForm({ instanceId, onUpload }) {
|
|
42
43
|
const inputId = "list-reusable-blocks-import-form-" + instanceId;
|
|
43
|
-
const formRef = (0, import_element.useRef)();
|
|
44
|
+
const formRef = (0, import_element.useRef)(null);
|
|
44
45
|
const [isLoading, setIsLoading] = (0, import_element.useState)(false);
|
|
45
46
|
const [error, setError] = (0, import_element.useState)(null);
|
|
46
47
|
const [file, setFile] = (0, import_element.useState)(null);
|
|
47
48
|
const onChangeFile = (event) => {
|
|
48
|
-
setFile(event.target.files[0]);
|
|
49
|
+
setFile(event.target.files?.[0] || null);
|
|
49
50
|
setError(null);
|
|
50
51
|
};
|
|
51
52
|
const onSubmit = (event) => {
|
|
@@ -55,13 +56,13 @@ function ImportForm({ instanceId, onUpload }) {
|
|
|
55
56
|
}
|
|
56
57
|
setIsLoading(true);
|
|
57
58
|
(0, import_import.default)(file).then((reusableBlock) => {
|
|
58
|
-
if (!formRef) {
|
|
59
|
+
if (!formRef.current) {
|
|
59
60
|
return;
|
|
60
61
|
}
|
|
61
62
|
setIsLoading(false);
|
|
62
63
|
onUpload(reusableBlock);
|
|
63
64
|
}).catch((errors) => {
|
|
64
|
-
if (!formRef) {
|
|
65
|
+
if (!formRef.current) {
|
|
65
66
|
return;
|
|
66
67
|
}
|
|
67
68
|
let uiMessage;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/components/import-form/index.
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {
|
|
5
|
-
"mappings": "
|
|
3
|
+
"sources": ["../../../src/components/import-form/index.tsx"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { Button, Notice } from '@wordpress/components';\nimport { withInstanceId } from '@wordpress/compose';\nimport { useRef, useState } from '@wordpress/element';\nimport { __, _x } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport importReusableBlock from '../../utils/import';\nimport type { ImportFormProps, ReusableBlock } from '../../utils/types';\n\nfunction ImportForm( { instanceId, onUpload }: ImportFormProps ) {\n\tconst inputId = 'list-reusable-blocks-import-form-' + instanceId;\n\n\tconst formRef = useRef< HTMLFormElement >( null );\n\tconst [ isLoading, setIsLoading ] = useState( false );\n\tconst [ error, setError ] = useState< string | null >( null );\n\tconst [ file, setFile ] = useState< File | null >( null );\n\n\tconst onChangeFile = ( event: React.ChangeEvent< HTMLInputElement > ) => {\n\t\tsetFile( event.target.files?.[ 0 ] || null );\n\t\tsetError( null );\n\t};\n\n\tconst onSubmit = ( event: React.FormEvent< HTMLFormElement > ) => {\n\t\tevent.preventDefault();\n\t\tif ( ! file ) {\n\t\t\treturn;\n\t\t}\n\t\tsetIsLoading( true );\n\t\timportReusableBlock( file )\n\t\t\t.then( ( reusableBlock: ReusableBlock ) => {\n\t\t\t\tif ( ! formRef.current ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tsetIsLoading( false );\n\t\t\t\tonUpload( reusableBlock );\n\t\t\t} )\n\t\t\t.catch( ( errors: Error ) => {\n\t\t\t\tif ( ! formRef.current ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tlet uiMessage: string;\n\t\t\t\tswitch ( errors.message ) {\n\t\t\t\t\tcase 'Invalid JSON file':\n\t\t\t\t\t\tuiMessage = __( 'Invalid JSON file' );\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'Invalid pattern JSON file':\n\t\t\t\t\t\tuiMessage = __( 'Invalid pattern JSON file' );\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tuiMessage = __( 'Unknown error' );\n\t\t\t\t}\n\n\t\t\t\tsetIsLoading( false );\n\t\t\t\tsetError( uiMessage );\n\t\t\t} );\n\t};\n\n\tconst onDismissError = () => {\n\t\tsetError( null );\n\t};\n\n\treturn (\n\t\t<form\n\t\t\tclassName=\"list-reusable-blocks-import-form\"\n\t\t\tonSubmit={ onSubmit }\n\t\t\tref={ formRef }\n\t\t>\n\t\t\t{ error && (\n\t\t\t\t<Notice status=\"error\" onRemove={ () => onDismissError() }>\n\t\t\t\t\t{ error }\n\t\t\t\t</Notice>\n\t\t\t) }\n\t\t\t<label\n\t\t\t\thtmlFor={ inputId }\n\t\t\t\tclassName=\"list-reusable-blocks-import-form__label\"\n\t\t\t>\n\t\t\t\t{ __( 'File' ) }\n\t\t\t</label>\n\t\t\t<input id={ inputId } type=\"file\" onChange={ onChangeFile } />\n\t\t\t<Button\n\t\t\t\t__next40pxDefaultSize\n\t\t\t\ttype=\"submit\"\n\t\t\t\tisBusy={ isLoading }\n\t\t\t\taccessibleWhenDisabled\n\t\t\t\tdisabled={ ! file || isLoading }\n\t\t\t\tvariant=\"secondary\"\n\t\t\t\tclassName=\"list-reusable-blocks-import-form__button\"\n\t\t\t>\n\t\t\t\t{ _x( 'Import', 'button label' ) }\n\t\t\t</Button>\n\t\t</form>\n\t);\n}\n\nexport default withInstanceId( ImportForm );\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,wBAA+B;AAC/B,qBAA+B;AAC/B,qBAAiC;AACjC,kBAAuB;AAKvB,oBAAgC;AA0D9B;AAvDF,SAAS,WAAY,EAAE,YAAY,SAAS,GAAqB;AAChE,QAAM,UAAU,sCAAsC;AAEtD,QAAM,cAAU,uBAA2B,IAAK;AAChD,QAAM,CAAE,WAAW,YAAa,QAAI,yBAAU,KAAM;AACpD,QAAM,CAAE,OAAO,QAAS,QAAI,yBAA2B,IAAK;AAC5D,QAAM,CAAE,MAAM,OAAQ,QAAI,yBAAyB,IAAK;AAExD,QAAM,eAAe,CAAE,UAAkD;AACxE,YAAS,MAAM,OAAO,QAAS,CAAE,KAAK,IAAK;AAC3C,aAAU,IAAK;AAAA,EAChB;AAEA,QAAM,WAAW,CAAE,UAA+C;AACjE,UAAM,eAAe;AACrB,QAAK,CAAE,MAAO;AACb;AAAA,IACD;AACA,iBAAc,IAAK;AACnB,sBAAAA,SAAqB,IAAK,EACxB,KAAM,CAAE,kBAAkC;AAC1C,UAAK,CAAE,QAAQ,SAAU;AACxB;AAAA,MACD;AAEA,mBAAc,KAAM;AACpB,eAAU,aAAc;AAAA,IACzB,CAAE,EACD,MAAO,CAAE,WAAmB;AAC5B,UAAK,CAAE,QAAQ,SAAU;AACxB;AAAA,MACD;AAEA,UAAI;AACJ,cAAS,OAAO,SAAU;AAAA,QACzB,KAAK;AACJ,0BAAY,gBAAI,mBAAoB;AACpC;AAAA,QACD,KAAK;AACJ,0BAAY,gBAAI,2BAA4B;AAC5C;AAAA,QACD;AACC,0BAAY,gBAAI,eAAgB;AAAA,MAClC;AAEA,mBAAc,KAAM;AACpB,eAAU,SAAU;AAAA,IACrB,CAAE;AAAA,EACJ;AAEA,QAAM,iBAAiB,MAAM;AAC5B,aAAU,IAAK;AAAA,EAChB;AAEA,SACC;AAAA,IAAC;AAAA;AAAA,MACA,WAAU;AAAA,MACV;AAAA,MACA,KAAM;AAAA,MAEJ;AAAA,iBACD,4CAAC,4BAAO,QAAO,SAAQ,UAAW,MAAM,eAAe,GACpD,iBACH;AAAA,QAED;AAAA,UAAC;AAAA;AAAA,YACA,SAAU;AAAA,YACV,WAAU;AAAA,YAER,8BAAI,MAAO;AAAA;AAAA,QACd;AAAA,QACA,4CAAC,WAAM,IAAK,SAAU,MAAK,QAAO,UAAW,cAAe;AAAA,QAC5D;AAAA,UAAC;AAAA;AAAA,YACA,uBAAqB;AAAA,YACrB,MAAK;AAAA,YACL,QAAS;AAAA,YACT,wBAAsB;AAAA,YACtB,UAAW,CAAE,QAAQ;AAAA,YACrB,SAAQ;AAAA,YACR,WAAU;AAAA,YAER,8BAAI,UAAU,cAAe;AAAA;AAAA,QAChC;AAAA;AAAA;AAAA,EACD;AAEF;AAEA,IAAO,0BAAQ,+BAAgB,UAAW;",
|
|
6
6
|
"names": ["importReusableBlock"]
|
|
7
7
|
}
|
package/build/index.cjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -21,18 +22,22 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
21
22
|
mod
|
|
22
23
|
));
|
|
23
24
|
|
|
24
|
-
// packages/list-reusable-blocks/src/index.
|
|
25
|
+
// packages/list-reusable-blocks/src/index.tsx
|
|
25
26
|
var import_element = require("@wordpress/element");
|
|
26
27
|
var import_i18n = require("@wordpress/i18n");
|
|
27
|
-
var import_export = __toESM(require("./utils/export.cjs"));
|
|
28
28
|
var import_import_dropdown = __toESM(require("./components/import-dropdown/index.cjs"));
|
|
29
|
+
var import_export = __toESM(require("./utils/export.cjs"));
|
|
29
30
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
30
31
|
document.body.addEventListener("click", (event) => {
|
|
31
|
-
|
|
32
|
+
const target = event.target;
|
|
33
|
+
if (!target.classList.contains("wp-list-reusable-blocks__export")) {
|
|
32
34
|
return;
|
|
33
35
|
}
|
|
34
36
|
event.preventDefault();
|
|
35
|
-
|
|
37
|
+
const blockId = target.dataset.id;
|
|
38
|
+
if (blockId) {
|
|
39
|
+
(0, import_export.default)(Number(blockId));
|
|
40
|
+
}
|
|
36
41
|
});
|
|
37
42
|
document.addEventListener("DOMContentLoaded", () => {
|
|
38
43
|
const button = document.querySelector(".page-title-action");
|
|
@@ -44,13 +49,16 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
|
44
49
|
notice.className = "notice notice-success is-dismissible";
|
|
45
50
|
notice.innerHTML = `<p>${(0, import_i18n.__)("Pattern imported successfully!")}</p>`;
|
|
46
51
|
const headerEnd = document.querySelector(".wp-header-end");
|
|
47
|
-
if (!headerEnd) {
|
|
52
|
+
if (!headerEnd || !headerEnd.parentNode) {
|
|
48
53
|
return;
|
|
49
54
|
}
|
|
50
55
|
headerEnd.parentNode.insertBefore(notice, headerEnd);
|
|
51
56
|
};
|
|
52
57
|
const container = document.createElement("div");
|
|
53
58
|
container.className = "list-reusable-blocks__container";
|
|
59
|
+
if (!button.parentNode) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
54
62
|
button.parentNode.insertBefore(container, button);
|
|
55
63
|
(0, import_element.createRoot)(container).render(
|
|
56
64
|
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_element.StrictMode, { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_import_dropdown.default, { onUpload: showNotice }) })
|
package/build/index.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/index.
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { createRoot, StrictMode } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport
|
|
5
|
-
"mappings": "
|
|
3
|
+
"sources": ["../src/index.tsx"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { createRoot, StrictMode } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport ImportDropdown from './components/import-dropdown';\nimport exportReusableBlock from './utils/export';\n\n// Setup Export Links.\ndocument.body.addEventListener( 'click', ( event: MouseEvent ) => {\n\tconst target = event.target as HTMLElement;\n\n\tif ( ! target.classList.contains( 'wp-list-reusable-blocks__export' ) ) {\n\t\treturn;\n\t}\n\n\tevent.preventDefault();\n\n\tconst blockId = target.dataset.id;\n\tif ( blockId ) {\n\t\texportReusableBlock( Number( blockId ) );\n\t}\n} );\n\n// Setup Import Form.\ndocument.addEventListener( 'DOMContentLoaded', () => {\n\tconst button = document.querySelector( '.page-title-action' );\n\tif ( ! button ) {\n\t\treturn;\n\t}\n\n\tconst showNotice = () => {\n\t\tconst notice = document.createElement( 'div' );\n\t\tnotice.className = 'notice notice-success is-dismissible';\n\t\tnotice.innerHTML = `<p>${ __( 'Pattern imported successfully!' ) }</p>`;\n\n\t\tconst headerEnd = document.querySelector( '.wp-header-end' );\n\t\tif ( ! headerEnd || ! headerEnd.parentNode ) {\n\t\t\treturn;\n\t\t}\n\t\theaderEnd.parentNode.insertBefore( notice, headerEnd );\n\t};\n\n\tconst container = document.createElement( 'div' );\n\tcontainer.className = 'list-reusable-blocks__container';\n\n\tif ( ! button.parentNode ) {\n\t\treturn;\n\t}\n\n\tbutton.parentNode.insertBefore( container, button );\n\tcreateRoot( container ).render(\n\t\t<StrictMode>\n\t\t\t<ImportDropdown onUpload={ showNotice } />\n\t\t</StrictMode>\n\t);\n} );\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;AAGA,qBAAuC;AACvC,kBAAmB;AAKnB,6BAA2B;AAC3B,oBAAgC;AA+C7B;AA5CH,SAAS,KAAK,iBAAkB,SAAS,CAAE,UAAuB;AACjE,QAAM,SAAS,MAAM;AAErB,MAAK,CAAE,OAAO,UAAU,SAAU,iCAAkC,GAAI;AACvE;AAAA,EACD;AAEA,QAAM,eAAe;AAErB,QAAM,UAAU,OAAO,QAAQ;AAC/B,MAAK,SAAU;AACd,sBAAAA,SAAqB,OAAQ,OAAQ,CAAE;AAAA,EACxC;AACD,CAAE;AAGF,SAAS,iBAAkB,oBAAoB,MAAM;AACpD,QAAM,SAAS,SAAS,cAAe,oBAAqB;AAC5D,MAAK,CAAE,QAAS;AACf;AAAA,EACD;AAEA,QAAM,aAAa,MAAM;AACxB,UAAM,SAAS,SAAS,cAAe,KAAM;AAC7C,WAAO,YAAY;AACnB,WAAO,YAAY,UAAO,gBAAI,gCAAiC,CAAE;AAEjE,UAAM,YAAY,SAAS,cAAe,gBAAiB;AAC3D,QAAK,CAAE,aAAa,CAAE,UAAU,YAAa;AAC5C;AAAA,IACD;AACA,cAAU,WAAW,aAAc,QAAQ,SAAU;AAAA,EACtD;AAEA,QAAM,YAAY,SAAS,cAAe,KAAM;AAChD,YAAU,YAAY;AAEtB,MAAK,CAAE,OAAO,YAAa;AAC1B;AAAA,EACD;AAEA,SAAO,WAAW,aAAc,WAAW,MAAO;AAClD,iCAAY,SAAU,EAAE;AAAA,IACvB,4CAAC,6BACA,sDAAC,uBAAAC,SAAA,EAAe,UAAW,YAAa,GACzC;AAAA,EACD;AACD,CAAE;",
|
|
6
6
|
"names": ["exportReusableBlock", "ImportDropdown"]
|
|
7
7
|
}
|
package/build/utils/export.cjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -26,7 +27,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
26
27
|
));
|
|
27
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
29
|
|
|
29
|
-
// packages/list-reusable-blocks/src/utils/export.
|
|
30
|
+
// packages/list-reusable-blocks/src/utils/export.ts
|
|
30
31
|
var export_exports = {};
|
|
31
32
|
__export(export_exports, {
|
|
32
33
|
default: () => export_default
|
|
@@ -36,7 +37,9 @@ var import_change_case = require("change-case");
|
|
|
36
37
|
var import_api_fetch = __toESM(require("@wordpress/api-fetch"));
|
|
37
38
|
var import_blob = require("@wordpress/blob");
|
|
38
39
|
async function exportReusableBlock(id) {
|
|
39
|
-
const postType = await (0, import_api_fetch.default)({
|
|
40
|
+
const postType = await (0, import_api_fetch.default)({
|
|
41
|
+
path: `/wp/v2/types/wp_block`
|
|
42
|
+
});
|
|
40
43
|
const post = await (0, import_api_fetch.default)({
|
|
41
44
|
path: `/wp/v2/${postType.rest_base}/${id}?context=edit`
|
|
42
45
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../src/utils/export.
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport { paramCase as kebabCase } from 'change-case';\n\n/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\n\n/**\n * Internal dependencies\n */\nimport { downloadBlob } from '@wordpress/blob';\n\n/**\n * Export a reusable block as a JSON file.\n *\n * @param
|
|
5
|
-
"mappings": "
|
|
3
|
+
"sources": ["../../src/utils/export.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport { paramCase as kebabCase } from 'change-case';\n\n/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\n\n/**\n * Internal dependencies\n */\nimport { downloadBlob } from '@wordpress/blob';\nimport type { Post, PostType } from './types';\n\n/**\n * Export a reusable block as a JSON file.\n *\n * @param id - The ID of the reusable block to export\n */\nasync function exportReusableBlock( id: number ): Promise< void > {\n\tconst postType = await apiFetch< PostType >( {\n\t\tpath: `/wp/v2/types/wp_block`,\n\t} );\n\n\tconst post = await apiFetch< Post >( {\n\t\tpath: `/wp/v2/${ postType.rest_base }/${ id }?context=edit`,\n\t} );\n\n\tconst title = post.title.raw;\n\tconst content = post.content.raw;\n\tconst syncStatus = post.wp_pattern_sync_status;\n\n\tconst fileContent = JSON.stringify(\n\t\t{\n\t\t\t__file: 'wp_block',\n\t\t\ttitle,\n\t\t\tcontent,\n\t\t\tsyncStatus,\n\t\t},\n\t\tnull,\n\t\t2\n\t);\n\n\tconst fileName = kebabCase( title ) + '.json';\n\n\tdownloadBlob( fileName, fileContent, 'application/json' );\n}\n\nexport default exportReusableBlock;\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,yBAAuC;AAKvC,uBAAqB;AAKrB,kBAA6B;AAQ7B,eAAe,oBAAqB,IAA8B;AACjE,QAAM,WAAW,UAAM,iBAAAA,SAAsB;AAAA,IAC5C,MAAM;AAAA,EACP,CAAE;AAEF,QAAM,OAAO,UAAM,iBAAAA,SAAkB;AAAA,IACpC,MAAM,UAAW,SAAS,SAAU,IAAK,EAAG;AAAA,EAC7C,CAAE;AAEF,QAAM,QAAQ,KAAK,MAAM;AACzB,QAAM,UAAU,KAAK,QAAQ;AAC7B,QAAM,aAAa,KAAK;AAExB,QAAM,cAAc,KAAK;AAAA,IACxB;AAAA,MACC,QAAQ;AAAA,MACR;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,IACA;AAAA,IACA;AAAA,EACD;AAEA,QAAM,eAAW,mBAAAC,WAAW,KAAM,IAAI;AAEtC,gCAAc,UAAU,aAAa,kBAAmB;AACzD;AAEA,IAAO,iBAAQ;",
|
|
6
6
|
"names": ["apiFetch", "kebabCase"]
|
|
7
7
|
}
|
package/build/utils/file.cjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __defProp = Object.defineProperty;
|
|
2
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
@@ -16,7 +17,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
17
|
};
|
|
17
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
19
|
|
|
19
|
-
// packages/list-reusable-blocks/src/utils/file.
|
|
20
|
+
// packages/list-reusable-blocks/src/utils/file.ts
|
|
20
21
|
var file_exports = {};
|
|
21
22
|
__export(file_exports, {
|
|
22
23
|
readTextFile: () => readTextFile
|
package/build/utils/file.cjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../src/utils/file.
|
|
4
|
-
"sourcesContent": ["/**\n * Reads the textual content of the given file.\n *\n * @param
|
|
5
|
-
"mappings": "
|
|
3
|
+
"sources": ["../../src/utils/file.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Reads the textual content of the given file.\n *\n * @param file - File to read\n * @return Promise that resolves to the content of the file\n */\nexport function readTextFile( file: File ): Promise< string > {\n\tconst reader = new window.FileReader();\n\treturn new Promise( ( resolve ) => {\n\t\treader.onload = () => {\n\t\t\tresolve( reader.result as string );\n\t\t};\n\t\treader.readAsText( file );\n\t} );\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAMO,SAAS,aAAc,MAAgC;AAC7D,QAAM,SAAS,IAAI,OAAO,WAAW;AACrC,SAAO,IAAI,QAAS,CAAE,YAAa;AAClC,WAAO,SAAS,MAAM;AACrB,cAAS,OAAO,MAAiB;AAAA,IAClC;AACA,WAAO,WAAY,IAAK;AAAA,EACzB,CAAE;AACH;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/build/utils/import.cjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __create = Object.create;
|
|
2
3
|
var __defProp = Object.defineProperty;
|
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
@@ -26,7 +27,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
26
27
|
));
|
|
27
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
29
|
|
|
29
|
-
// packages/list-reusable-blocks/src/utils/import.
|
|
30
|
+
// packages/list-reusable-blocks/src/utils/import.ts
|
|
30
31
|
var import_exports = {};
|
|
31
32
|
__export(import_exports, {
|
|
32
33
|
default: () => import_default
|
|
@@ -39,13 +40,15 @@ async function importReusableBlock(file) {
|
|
|
39
40
|
let parsedContent;
|
|
40
41
|
try {
|
|
41
42
|
parsedContent = JSON.parse(fileContent);
|
|
42
|
-
} catch
|
|
43
|
+
} catch {
|
|
43
44
|
throw new Error("Invalid JSON file");
|
|
44
45
|
}
|
|
45
46
|
if (parsedContent.__file !== "wp_block" || !parsedContent.title || !parsedContent.content || typeof parsedContent.title !== "string" || typeof parsedContent.content !== "string" || parsedContent.syncStatus && typeof parsedContent.syncStatus !== "string") {
|
|
46
47
|
throw new Error("Invalid pattern JSON file");
|
|
47
48
|
}
|
|
48
|
-
const postType = await (0, import_api_fetch.default)({
|
|
49
|
+
const postType = await (0, import_api_fetch.default)({
|
|
50
|
+
path: `/wp/v2/types/wp_block`
|
|
51
|
+
});
|
|
49
52
|
const reusableBlock = await (0, import_api_fetch.default)({
|
|
50
53
|
path: `/wp/v2/${postType.rest_base}`,
|
|
51
54
|
data: {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../src/utils/import.
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\n\n/**\n * Internal dependencies\n */\nimport { readTextFile } from './file';\n\n/**\n * Import a reusable block from a JSON file.\n *\n * @param
|
|
5
|
-
"mappings": "
|
|
3
|
+
"sources": ["../../src/utils/import.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\n\n/**\n * Internal dependencies\n */\nimport { readTextFile } from './file';\nimport type {\n\tParsedContent,\n\tPostType,\n\tReusableBlock,\n\tReusableBlockData,\n} from './types';\n\n/**\n * Import a reusable block from a JSON file.\n *\n * @param file - File to import\n * @return Promise returning the imported reusable block\n */\nasync function importReusableBlock( file: File ): Promise< ReusableBlock > {\n\tconst fileContent = await readTextFile( file );\n\tlet parsedContent: ParsedContent;\n\n\ttry {\n\t\tparsedContent = JSON.parse( fileContent );\n\t} catch {\n\t\tthrow new Error( 'Invalid JSON file' );\n\t}\n\n\tif (\n\t\tparsedContent.__file !== 'wp_block' ||\n\t\t! parsedContent.title ||\n\t\t! parsedContent.content ||\n\t\ttypeof parsedContent.title !== 'string' ||\n\t\ttypeof parsedContent.content !== 'string' ||\n\t\t( parsedContent.syncStatus &&\n\t\t\ttypeof parsedContent.syncStatus !== 'string' )\n\t) {\n\t\tthrow new Error( 'Invalid pattern JSON file' );\n\t}\n\n\tconst postType = await apiFetch< PostType >( {\n\t\tpath: `/wp/v2/types/wp_block`,\n\t} );\n\n\tconst reusableBlock = await apiFetch< ReusableBlock >( {\n\t\tpath: `/wp/v2/${ postType.rest_base }`,\n\t\tdata: {\n\t\t\ttitle: parsedContent.title,\n\t\t\tcontent: parsedContent.content,\n\t\t\tstatus: 'publish',\n\t\t\tmeta:\n\t\t\t\tparsedContent.syncStatus === 'unsynced'\n\t\t\t\t\t? { wp_pattern_sync_status: parsedContent.syncStatus }\n\t\t\t\t\t: undefined,\n\t\t} satisfies ReusableBlockData,\n\t\tmethod: 'POST',\n\t} );\n\n\treturn reusableBlock;\n}\n\nexport default importReusableBlock;\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,uBAAqB;AAKrB,kBAA6B;AAc7B,eAAe,oBAAqB,MAAuC;AAC1E,QAAM,cAAc,UAAM,0BAAc,IAAK;AAC7C,MAAI;AAEJ,MAAI;AACH,oBAAgB,KAAK,MAAO,WAAY;AAAA,EACzC,QAAQ;AACP,UAAM,IAAI,MAAO,mBAAoB;AAAA,EACtC;AAEA,MACC,cAAc,WAAW,cACzB,CAAE,cAAc,SAChB,CAAE,cAAc,WAChB,OAAO,cAAc,UAAU,YAC/B,OAAO,cAAc,YAAY,YAC/B,cAAc,cACf,OAAO,cAAc,eAAe,UACpC;AACD,UAAM,IAAI,MAAO,2BAA4B;AAAA,EAC9C;AAEA,QAAM,WAAW,UAAM,iBAAAA,SAAsB;AAAA,IAC5C,MAAM;AAAA,EACP,CAAE;AAEF,QAAM,gBAAgB,UAAM,iBAAAA,SAA2B;AAAA,IACtD,MAAM,UAAW,SAAS,SAAU;AAAA,IACpC,MAAM;AAAA,MACL,OAAO,cAAc;AAAA,MACrB,SAAS,cAAc;AAAA,MACvB,QAAQ;AAAA,MACR,MACC,cAAc,eAAe,aAC1B,EAAE,wBAAwB,cAAc,WAAW,IACnD;AAAA,IACL;AAAA,IACA,QAAQ;AAAA,EACT,CAAE;AAEF,SAAO;AACR;AAEA,IAAO,iBAAQ;",
|
|
6
6
|
"names": ["apiFetch"]
|
|
7
7
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
|
|
16
|
+
// packages/list-reusable-blocks/src/utils/types.ts
|
|
17
|
+
var types_exports = {};
|
|
18
|
+
module.exports = __toCommonJS(types_exports);
|
|
19
|
+
//# sourceMappingURL=types.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/utils/types.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport type { Post as CorePost, Type } from '@wordpress/core-data';\n\nexport interface ImportDropdownProps {\n\tonUpload: ( data: ReusableBlock ) => void;\n}\n\nexport interface ImportFormProps {\n\tinstanceId: string | number;\n\tonUpload: ( reusableBlock: ReusableBlock ) => void;\n}\n\nexport type PostType = Type;\n\nexport type Post = CorePost & { wp_pattern_sync_status?: string };\n\nexport interface ParsedContent {\n\t__file: string;\n\ttitle: string;\n\tcontent: string;\n\tsyncStatus?: string;\n\t[ key: string ]: unknown;\n}\n\nexport interface ReusableBlockMeta {\n\twp_pattern_sync_status?: string;\n}\n\nexport interface ReusableBlockData {\n\ttitle: string;\n\tcontent: string;\n\tstatus: string;\n\tmeta?: ReusableBlockMeta;\n}\n\nexport interface ReusableBlock {\n\tid: number;\n\ttitle: {\n\t\traw: string;\n\t\trendered: string;\n\t};\n\tcontent: {\n\t\traw: string;\n\t\trendered: string;\n\t};\n\tstatus: string;\n\t[ key: string ]: unknown;\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;AAAA;AAAA;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
// packages/list-reusable-blocks/src/components/import-dropdown/index.
|
|
1
|
+
// packages/list-reusable-blocks/src/components/import-dropdown/index.tsx
|
|
2
|
+
import { Button, Dropdown } from "@wordpress/components";
|
|
2
3
|
import { pipe } from "@wordpress/compose";
|
|
3
4
|
import { __ } from "@wordpress/i18n";
|
|
4
|
-
import { Dropdown, Button } from "@wordpress/components";
|
|
5
5
|
import ImportForm from "../import-form/index.mjs";
|
|
6
6
|
import { jsx } from "react/jsx-runtime";
|
|
7
7
|
function ImportDropdown({ onUpload }) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/components/import-dropdown/index.
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {
|
|
5
|
-
"mappings": ";AAGA,SAAS,
|
|
3
|
+
"sources": ["../../../src/components/import-dropdown/index.tsx"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { Button, Dropdown } from '@wordpress/components';\nimport { pipe } from '@wordpress/compose';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport type { ImportDropdownProps } from '../../utils/types';\nimport ImportForm from '../import-form';\n\nfunction ImportDropdown( { onUpload }: ImportDropdownProps ) {\n\treturn (\n\t\t<Dropdown\n\t\t\tpopoverProps={ { placement: 'bottom-start' } }\n\t\t\tcontentClassName=\"list-reusable-blocks-import-dropdown__content\"\n\t\t\trenderToggle={ ( { isOpen, onToggle } ) => (\n\t\t\t\t<Button\n\t\t\t\t\tsize=\"compact\"\n\t\t\t\t\tclassName=\"list-reusable-blocks-import-dropdown__button\"\n\t\t\t\t\taria-expanded={ isOpen }\n\t\t\t\t\tonClick={ onToggle }\n\t\t\t\t\tvariant=\"primary\"\n\t\t\t\t>\n\t\t\t\t\t{ __( 'Import from JSON' ) }\n\t\t\t\t</Button>\n\t\t\t) }\n\t\t\trenderContent={ ( { onClose } ) => (\n\t\t\t\t<ImportForm onUpload={ pipe( onClose, onUpload ) } />\n\t\t\t) }\n\t\t/>\n\t);\n}\n\nexport default ImportDropdown;\n"],
|
|
5
|
+
"mappings": ";AAGA,SAAS,QAAQ,gBAAgB;AACjC,SAAS,YAAY;AACrB,SAAS,UAAU;AAMnB,OAAO,gBAAgB;AAQnB;AANJ,SAAS,eAAgB,EAAE,SAAS,GAAyB;AAC5D,SACC;AAAA,IAAC;AAAA;AAAA,MACA,cAAe,EAAE,WAAW,eAAe;AAAA,MAC3C,kBAAiB;AAAA,MACjB,cAAe,CAAE,EAAE,QAAQ,SAAS,MACnC;AAAA,QAAC;AAAA;AAAA,UACA,MAAK;AAAA,UACL,WAAU;AAAA,UACV,iBAAgB;AAAA,UAChB,SAAU;AAAA,UACV,SAAQ;AAAA,UAEN,aAAI,kBAAmB;AAAA;AAAA,MAC1B;AAAA,MAED,eAAgB,CAAE,EAAE,QAAQ,MAC3B,oBAAC,cAAW,UAAW,KAAM,SAAS,QAAS,GAAI;AAAA;AAAA,EAErD;AAEF;AAEA,IAAO,0BAAQ;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
// packages/list-reusable-blocks/src/components/import-form/index.
|
|
2
|
-
import {
|
|
1
|
+
// packages/list-reusable-blocks/src/components/import-form/index.tsx
|
|
2
|
+
import { Button, Notice } from "@wordpress/components";
|
|
3
3
|
import { withInstanceId } from "@wordpress/compose";
|
|
4
|
+
import { useRef, useState } from "@wordpress/element";
|
|
4
5
|
import { __, _x } from "@wordpress/i18n";
|
|
5
|
-
import { Button, Notice } from "@wordpress/components";
|
|
6
6
|
import importReusableBlock from "../../utils/import.mjs";
|
|
7
7
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
8
8
|
function ImportForm({ instanceId, onUpload }) {
|
|
9
9
|
const inputId = "list-reusable-blocks-import-form-" + instanceId;
|
|
10
|
-
const formRef = useRef();
|
|
10
|
+
const formRef = useRef(null);
|
|
11
11
|
const [isLoading, setIsLoading] = useState(false);
|
|
12
12
|
const [error, setError] = useState(null);
|
|
13
13
|
const [file, setFile] = useState(null);
|
|
14
14
|
const onChangeFile = (event) => {
|
|
15
|
-
setFile(event.target.files[0]);
|
|
15
|
+
setFile(event.target.files?.[0] || null);
|
|
16
16
|
setError(null);
|
|
17
17
|
};
|
|
18
18
|
const onSubmit = (event) => {
|
|
@@ -22,13 +22,13 @@ function ImportForm({ instanceId, onUpload }) {
|
|
|
22
22
|
}
|
|
23
23
|
setIsLoading(true);
|
|
24
24
|
importReusableBlock(file).then((reusableBlock) => {
|
|
25
|
-
if (!formRef) {
|
|
25
|
+
if (!formRef.current) {
|
|
26
26
|
return;
|
|
27
27
|
}
|
|
28
28
|
setIsLoading(false);
|
|
29
29
|
onUpload(reusableBlock);
|
|
30
30
|
}).catch((errors) => {
|
|
31
|
-
if (!formRef) {
|
|
31
|
+
if (!formRef.current) {
|
|
32
32
|
return;
|
|
33
33
|
}
|
|
34
34
|
let uiMessage;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/components/import-form/index.
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport {
|
|
5
|
-
"mappings": ";AAGA,SAAS,
|
|
3
|
+
"sources": ["../../../src/components/import-form/index.tsx"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { Button, Notice } from '@wordpress/components';\nimport { withInstanceId } from '@wordpress/compose';\nimport { useRef, useState } from '@wordpress/element';\nimport { __, _x } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport importReusableBlock from '../../utils/import';\nimport type { ImportFormProps, ReusableBlock } from '../../utils/types';\n\nfunction ImportForm( { instanceId, onUpload }: ImportFormProps ) {\n\tconst inputId = 'list-reusable-blocks-import-form-' + instanceId;\n\n\tconst formRef = useRef< HTMLFormElement >( null );\n\tconst [ isLoading, setIsLoading ] = useState( false );\n\tconst [ error, setError ] = useState< string | null >( null );\n\tconst [ file, setFile ] = useState< File | null >( null );\n\n\tconst onChangeFile = ( event: React.ChangeEvent< HTMLInputElement > ) => {\n\t\tsetFile( event.target.files?.[ 0 ] || null );\n\t\tsetError( null );\n\t};\n\n\tconst onSubmit = ( event: React.FormEvent< HTMLFormElement > ) => {\n\t\tevent.preventDefault();\n\t\tif ( ! file ) {\n\t\t\treturn;\n\t\t}\n\t\tsetIsLoading( true );\n\t\timportReusableBlock( file )\n\t\t\t.then( ( reusableBlock: ReusableBlock ) => {\n\t\t\t\tif ( ! formRef.current ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tsetIsLoading( false );\n\t\t\t\tonUpload( reusableBlock );\n\t\t\t} )\n\t\t\t.catch( ( errors: Error ) => {\n\t\t\t\tif ( ! formRef.current ) {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tlet uiMessage: string;\n\t\t\t\tswitch ( errors.message ) {\n\t\t\t\t\tcase 'Invalid JSON file':\n\t\t\t\t\t\tuiMessage = __( 'Invalid JSON file' );\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase 'Invalid pattern JSON file':\n\t\t\t\t\t\tuiMessage = __( 'Invalid pattern JSON file' );\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tdefault:\n\t\t\t\t\t\tuiMessage = __( 'Unknown error' );\n\t\t\t\t}\n\n\t\t\t\tsetIsLoading( false );\n\t\t\t\tsetError( uiMessage );\n\t\t\t} );\n\t};\n\n\tconst onDismissError = () => {\n\t\tsetError( null );\n\t};\n\n\treturn (\n\t\t<form\n\t\t\tclassName=\"list-reusable-blocks-import-form\"\n\t\t\tonSubmit={ onSubmit }\n\t\t\tref={ formRef }\n\t\t>\n\t\t\t{ error && (\n\t\t\t\t<Notice status=\"error\" onRemove={ () => onDismissError() }>\n\t\t\t\t\t{ error }\n\t\t\t\t</Notice>\n\t\t\t) }\n\t\t\t<label\n\t\t\t\thtmlFor={ inputId }\n\t\t\t\tclassName=\"list-reusable-blocks-import-form__label\"\n\t\t\t>\n\t\t\t\t{ __( 'File' ) }\n\t\t\t</label>\n\t\t\t<input id={ inputId } type=\"file\" onChange={ onChangeFile } />\n\t\t\t<Button\n\t\t\t\t__next40pxDefaultSize\n\t\t\t\ttype=\"submit\"\n\t\t\t\tisBusy={ isLoading }\n\t\t\t\taccessibleWhenDisabled\n\t\t\t\tdisabled={ ! file || isLoading }\n\t\t\t\tvariant=\"secondary\"\n\t\t\t\tclassName=\"list-reusable-blocks-import-form__button\"\n\t\t\t>\n\t\t\t\t{ _x( 'Import', 'button label' ) }\n\t\t\t</Button>\n\t\t</form>\n\t);\n}\n\nexport default withInstanceId( ImportForm );\n"],
|
|
5
|
+
"mappings": ";AAGA,SAAS,QAAQ,cAAc;AAC/B,SAAS,sBAAsB;AAC/B,SAAS,QAAQ,gBAAgB;AACjC,SAAS,IAAI,UAAU;AAKvB,OAAO,yBAAyB;AA0D9B,SAME,KANF;AAvDF,SAAS,WAAY,EAAE,YAAY,SAAS,GAAqB;AAChE,QAAM,UAAU,sCAAsC;AAEtD,QAAM,UAAU,OAA2B,IAAK;AAChD,QAAM,CAAE,WAAW,YAAa,IAAI,SAAU,KAAM;AACpD,QAAM,CAAE,OAAO,QAAS,IAAI,SAA2B,IAAK;AAC5D,QAAM,CAAE,MAAM,OAAQ,IAAI,SAAyB,IAAK;AAExD,QAAM,eAAe,CAAE,UAAkD;AACxE,YAAS,MAAM,OAAO,QAAS,CAAE,KAAK,IAAK;AAC3C,aAAU,IAAK;AAAA,EAChB;AAEA,QAAM,WAAW,CAAE,UAA+C;AACjE,UAAM,eAAe;AACrB,QAAK,CAAE,MAAO;AACb;AAAA,IACD;AACA,iBAAc,IAAK;AACnB,wBAAqB,IAAK,EACxB,KAAM,CAAE,kBAAkC;AAC1C,UAAK,CAAE,QAAQ,SAAU;AACxB;AAAA,MACD;AAEA,mBAAc,KAAM;AACpB,eAAU,aAAc;AAAA,IACzB,CAAE,EACD,MAAO,CAAE,WAAmB;AAC5B,UAAK,CAAE,QAAQ,SAAU;AACxB;AAAA,MACD;AAEA,UAAI;AACJ,cAAS,OAAO,SAAU;AAAA,QACzB,KAAK;AACJ,sBAAY,GAAI,mBAAoB;AACpC;AAAA,QACD,KAAK;AACJ,sBAAY,GAAI,2BAA4B;AAC5C;AAAA,QACD;AACC,sBAAY,GAAI,eAAgB;AAAA,MAClC;AAEA,mBAAc,KAAM;AACpB,eAAU,SAAU;AAAA,IACrB,CAAE;AAAA,EACJ;AAEA,QAAM,iBAAiB,MAAM;AAC5B,aAAU,IAAK;AAAA,EAChB;AAEA,SACC;AAAA,IAAC;AAAA;AAAA,MACA,WAAU;AAAA,MACV;AAAA,MACA,KAAM;AAAA,MAEJ;AAAA,iBACD,oBAAC,UAAO,QAAO,SAAQ,UAAW,MAAM,eAAe,GACpD,iBACH;AAAA,QAED;AAAA,UAAC;AAAA;AAAA,YACA,SAAU;AAAA,YACV,WAAU;AAAA,YAER,aAAI,MAAO;AAAA;AAAA,QACd;AAAA,QACA,oBAAC,WAAM,IAAK,SAAU,MAAK,QAAO,UAAW,cAAe;AAAA,QAC5D;AAAA,UAAC;AAAA;AAAA,YACA,uBAAqB;AAAA,YACrB,MAAK;AAAA,YACL,QAAS;AAAA,YACT,wBAAsB;AAAA,YACtB,UAAW,CAAE,QAAQ;AAAA,YACrB,SAAQ;AAAA,YACR,WAAU;AAAA,YAER,aAAI,UAAU,cAAe;AAAA;AAAA,QAChC;AAAA;AAAA;AAAA,EACD;AAEF;AAEA,IAAO,sBAAQ,eAAgB,UAAW;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/build-module/index.mjs
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
|
-
// packages/list-reusable-blocks/src/index.
|
|
1
|
+
// packages/list-reusable-blocks/src/index.tsx
|
|
2
2
|
import { createRoot, StrictMode } from "@wordpress/element";
|
|
3
3
|
import { __ } from "@wordpress/i18n";
|
|
4
|
-
import exportReusableBlock from "./utils/export.mjs";
|
|
5
4
|
import ImportDropdown from "./components/import-dropdown/index.mjs";
|
|
5
|
+
import exportReusableBlock from "./utils/export.mjs";
|
|
6
6
|
import { jsx } from "react/jsx-runtime";
|
|
7
7
|
document.body.addEventListener("click", (event) => {
|
|
8
|
-
|
|
8
|
+
const target = event.target;
|
|
9
|
+
if (!target.classList.contains("wp-list-reusable-blocks__export")) {
|
|
9
10
|
return;
|
|
10
11
|
}
|
|
11
12
|
event.preventDefault();
|
|
12
|
-
|
|
13
|
+
const blockId = target.dataset.id;
|
|
14
|
+
if (blockId) {
|
|
15
|
+
exportReusableBlock(Number(blockId));
|
|
16
|
+
}
|
|
13
17
|
});
|
|
14
18
|
document.addEventListener("DOMContentLoaded", () => {
|
|
15
19
|
const button = document.querySelector(".page-title-action");
|
|
@@ -21,13 +25,16 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
|
21
25
|
notice.className = "notice notice-success is-dismissible";
|
|
22
26
|
notice.innerHTML = `<p>${__("Pattern imported successfully!")}</p>`;
|
|
23
27
|
const headerEnd = document.querySelector(".wp-header-end");
|
|
24
|
-
if (!headerEnd) {
|
|
28
|
+
if (!headerEnd || !headerEnd.parentNode) {
|
|
25
29
|
return;
|
|
26
30
|
}
|
|
27
31
|
headerEnd.parentNode.insertBefore(notice, headerEnd);
|
|
28
32
|
};
|
|
29
33
|
const container = document.createElement("div");
|
|
30
34
|
container.className = "list-reusable-blocks__container";
|
|
35
|
+
if (!button.parentNode) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
31
38
|
button.parentNode.insertBefore(container, button);
|
|
32
39
|
createRoot(container).render(
|
|
33
40
|
/* @__PURE__ */ jsx(StrictMode, { children: /* @__PURE__ */ jsx(ImportDropdown, { onUpload: showNotice }) })
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/index.
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { createRoot, StrictMode } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport
|
|
5
|
-
"mappings": ";AAGA,SAAS,YAAY,kBAAkB;AACvC,SAAS,UAAU;AAKnB,OAAO,
|
|
3
|
+
"sources": ["../src/index.tsx"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { createRoot, StrictMode } from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\n\n/**\n * Internal dependencies\n */\nimport ImportDropdown from './components/import-dropdown';\nimport exportReusableBlock from './utils/export';\n\n// Setup Export Links.\ndocument.body.addEventListener( 'click', ( event: MouseEvent ) => {\n\tconst target = event.target as HTMLElement;\n\n\tif ( ! target.classList.contains( 'wp-list-reusable-blocks__export' ) ) {\n\t\treturn;\n\t}\n\n\tevent.preventDefault();\n\n\tconst blockId = target.dataset.id;\n\tif ( blockId ) {\n\t\texportReusableBlock( Number( blockId ) );\n\t}\n} );\n\n// Setup Import Form.\ndocument.addEventListener( 'DOMContentLoaded', () => {\n\tconst button = document.querySelector( '.page-title-action' );\n\tif ( ! button ) {\n\t\treturn;\n\t}\n\n\tconst showNotice = () => {\n\t\tconst notice = document.createElement( 'div' );\n\t\tnotice.className = 'notice notice-success is-dismissible';\n\t\tnotice.innerHTML = `<p>${ __( 'Pattern imported successfully!' ) }</p>`;\n\n\t\tconst headerEnd = document.querySelector( '.wp-header-end' );\n\t\tif ( ! headerEnd || ! headerEnd.parentNode ) {\n\t\t\treturn;\n\t\t}\n\t\theaderEnd.parentNode.insertBefore( notice, headerEnd );\n\t};\n\n\tconst container = document.createElement( 'div' );\n\tcontainer.className = 'list-reusable-blocks__container';\n\n\tif ( ! button.parentNode ) {\n\t\treturn;\n\t}\n\n\tbutton.parentNode.insertBefore( container, button );\n\tcreateRoot( container ).render(\n\t\t<StrictMode>\n\t\t\t<ImportDropdown onUpload={ showNotice } />\n\t\t</StrictMode>\n\t);\n} );\n"],
|
|
5
|
+
"mappings": ";AAGA,SAAS,YAAY,kBAAkB;AACvC,SAAS,UAAU;AAKnB,OAAO,oBAAoB;AAC3B,OAAO,yBAAyB;AA+C7B;AA5CH,SAAS,KAAK,iBAAkB,SAAS,CAAE,UAAuB;AACjE,QAAM,SAAS,MAAM;AAErB,MAAK,CAAE,OAAO,UAAU,SAAU,iCAAkC,GAAI;AACvE;AAAA,EACD;AAEA,QAAM,eAAe;AAErB,QAAM,UAAU,OAAO,QAAQ;AAC/B,MAAK,SAAU;AACd,wBAAqB,OAAQ,OAAQ,CAAE;AAAA,EACxC;AACD,CAAE;AAGF,SAAS,iBAAkB,oBAAoB,MAAM;AACpD,QAAM,SAAS,SAAS,cAAe,oBAAqB;AAC5D,MAAK,CAAE,QAAS;AACf;AAAA,EACD;AAEA,QAAM,aAAa,MAAM;AACxB,UAAM,SAAS,SAAS,cAAe,KAAM;AAC7C,WAAO,YAAY;AACnB,WAAO,YAAY,MAAO,GAAI,gCAAiC,CAAE;AAEjE,UAAM,YAAY,SAAS,cAAe,gBAAiB;AAC3D,QAAK,CAAE,aAAa,CAAE,UAAU,YAAa;AAC5C;AAAA,IACD;AACA,cAAU,WAAW,aAAc,QAAQ,SAAU;AAAA,EACtD;AAEA,QAAM,YAAY,SAAS,cAAe,KAAM;AAChD,YAAU,YAAY;AAEtB,MAAK,CAAE,OAAO,YAAa;AAC1B;AAAA,EACD;AAEA,SAAO,WAAW,aAAc,WAAW,MAAO;AAClD,aAAY,SAAU,EAAE;AAAA,IACvB,oBAAC,cACA,8BAAC,kBAAe,UAAW,YAAa,GACzC;AAAA,EACD;AACD,CAAE;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
// packages/list-reusable-blocks/src/utils/export.
|
|
1
|
+
// packages/list-reusable-blocks/src/utils/export.ts
|
|
2
2
|
import { paramCase as kebabCase } from "change-case";
|
|
3
3
|
import apiFetch from "@wordpress/api-fetch";
|
|
4
4
|
import { downloadBlob } from "@wordpress/blob";
|
|
5
5
|
async function exportReusableBlock(id) {
|
|
6
|
-
const postType = await apiFetch({
|
|
6
|
+
const postType = await apiFetch({
|
|
7
|
+
path: `/wp/v2/types/wp_block`
|
|
8
|
+
});
|
|
7
9
|
const post = await apiFetch({
|
|
8
10
|
path: `/wp/v2/${postType.rest_base}/${id}?context=edit`
|
|
9
11
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../src/utils/export.
|
|
4
|
-
"sourcesContent": ["/**\n * External dependencies\n */\nimport { paramCase as kebabCase } from 'change-case';\n\n/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\n\n/**\n * Internal dependencies\n */\nimport { downloadBlob } from '@wordpress/blob';\n\n/**\n * Export a reusable block as a JSON file.\n *\n * @param
|
|
5
|
-
"mappings": ";AAGA,SAAS,aAAa,iBAAiB;AAKvC,OAAO,cAAc;AAKrB,SAAS,oBAAoB;
|
|
3
|
+
"sources": ["../../src/utils/export.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * External dependencies\n */\nimport { paramCase as kebabCase } from 'change-case';\n\n/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\n\n/**\n * Internal dependencies\n */\nimport { downloadBlob } from '@wordpress/blob';\nimport type { Post, PostType } from './types';\n\n/**\n * Export a reusable block as a JSON file.\n *\n * @param id - The ID of the reusable block to export\n */\nasync function exportReusableBlock( id: number ): Promise< void > {\n\tconst postType = await apiFetch< PostType >( {\n\t\tpath: `/wp/v2/types/wp_block`,\n\t} );\n\n\tconst post = await apiFetch< Post >( {\n\t\tpath: `/wp/v2/${ postType.rest_base }/${ id }?context=edit`,\n\t} );\n\n\tconst title = post.title.raw;\n\tconst content = post.content.raw;\n\tconst syncStatus = post.wp_pattern_sync_status;\n\n\tconst fileContent = JSON.stringify(\n\t\t{\n\t\t\t__file: 'wp_block',\n\t\t\ttitle,\n\t\t\tcontent,\n\t\t\tsyncStatus,\n\t\t},\n\t\tnull,\n\t\t2\n\t);\n\n\tconst fileName = kebabCase( title ) + '.json';\n\n\tdownloadBlob( fileName, fileContent, 'application/json' );\n}\n\nexport default exportReusableBlock;\n"],
|
|
5
|
+
"mappings": ";AAGA,SAAS,aAAa,iBAAiB;AAKvC,OAAO,cAAc;AAKrB,SAAS,oBAAoB;AAQ7B,eAAe,oBAAqB,IAA8B;AACjE,QAAM,WAAW,MAAM,SAAsB;AAAA,IAC5C,MAAM;AAAA,EACP,CAAE;AAEF,QAAM,OAAO,MAAM,SAAkB;AAAA,IACpC,MAAM,UAAW,SAAS,SAAU,IAAK,EAAG;AAAA,EAC7C,CAAE;AAEF,QAAM,QAAQ,KAAK,MAAM;AACzB,QAAM,UAAU,KAAK,QAAQ;AAC7B,QAAM,aAAa,KAAK;AAExB,QAAM,cAAc,KAAK;AAAA,IACxB;AAAA,MACC,QAAQ;AAAA,MACR;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,IACA;AAAA,IACA;AAAA,EACD;AAEA,QAAM,WAAW,UAAW,KAAM,IAAI;AAEtC,eAAc,UAAU,aAAa,kBAAmB;AACzD;AAEA,IAAO,iBAAQ;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../src/utils/file.
|
|
4
|
-
"sourcesContent": ["/**\n * Reads the textual content of the given file.\n *\n * @param
|
|
5
|
-
"mappings": ";AAMO,SAAS,aAAc,
|
|
3
|
+
"sources": ["../../src/utils/file.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Reads the textual content of the given file.\n *\n * @param file - File to read\n * @return Promise that resolves to the content of the file\n */\nexport function readTextFile( file: File ): Promise< string > {\n\tconst reader = new window.FileReader();\n\treturn new Promise( ( resolve ) => {\n\t\treader.onload = () => {\n\t\t\tresolve( reader.result as string );\n\t\t};\n\t\treader.readAsText( file );\n\t} );\n}\n"],
|
|
5
|
+
"mappings": ";AAMO,SAAS,aAAc,MAAgC;AAC7D,QAAM,SAAS,IAAI,OAAO,WAAW;AACrC,SAAO,IAAI,QAAS,CAAE,YAAa;AAClC,WAAO,SAAS,MAAM;AACrB,cAAS,OAAO,MAAiB;AAAA,IAClC;AACA,WAAO,WAAY,IAAK;AAAA,EACzB,CAAE;AACH;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// packages/list-reusable-blocks/src/utils/import.
|
|
1
|
+
// packages/list-reusable-blocks/src/utils/import.ts
|
|
2
2
|
import apiFetch from "@wordpress/api-fetch";
|
|
3
3
|
import { readTextFile } from "./file.mjs";
|
|
4
4
|
async function importReusableBlock(file) {
|
|
@@ -6,13 +6,15 @@ async function importReusableBlock(file) {
|
|
|
6
6
|
let parsedContent;
|
|
7
7
|
try {
|
|
8
8
|
parsedContent = JSON.parse(fileContent);
|
|
9
|
-
} catch
|
|
9
|
+
} catch {
|
|
10
10
|
throw new Error("Invalid JSON file");
|
|
11
11
|
}
|
|
12
12
|
if (parsedContent.__file !== "wp_block" || !parsedContent.title || !parsedContent.content || typeof parsedContent.title !== "string" || typeof parsedContent.content !== "string" || parsedContent.syncStatus && typeof parsedContent.syncStatus !== "string") {
|
|
13
13
|
throw new Error("Invalid pattern JSON file");
|
|
14
14
|
}
|
|
15
|
-
const postType = await apiFetch({
|
|
15
|
+
const postType = await apiFetch({
|
|
16
|
+
path: `/wp/v2/types/wp_block`
|
|
17
|
+
});
|
|
16
18
|
const reusableBlock = await apiFetch({
|
|
17
19
|
path: `/wp/v2/${postType.rest_base}`,
|
|
18
20
|
data: {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../src/utils/import.
|
|
4
|
-
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\n\n/**\n * Internal dependencies\n */\nimport { readTextFile } from './file';\n\n/**\n * Import a reusable block from a JSON file.\n *\n * @param
|
|
5
|
-
"mappings": ";AAGA,OAAO,cAAc;AAKrB,SAAS,oBAAoB;
|
|
3
|
+
"sources": ["../../src/utils/import.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport apiFetch from '@wordpress/api-fetch';\n\n/**\n * Internal dependencies\n */\nimport { readTextFile } from './file';\nimport type {\n\tParsedContent,\n\tPostType,\n\tReusableBlock,\n\tReusableBlockData,\n} from './types';\n\n/**\n * Import a reusable block from a JSON file.\n *\n * @param file - File to import\n * @return Promise returning the imported reusable block\n */\nasync function importReusableBlock( file: File ): Promise< ReusableBlock > {\n\tconst fileContent = await readTextFile( file );\n\tlet parsedContent: ParsedContent;\n\n\ttry {\n\t\tparsedContent = JSON.parse( fileContent );\n\t} catch {\n\t\tthrow new Error( 'Invalid JSON file' );\n\t}\n\n\tif (\n\t\tparsedContent.__file !== 'wp_block' ||\n\t\t! parsedContent.title ||\n\t\t! parsedContent.content ||\n\t\ttypeof parsedContent.title !== 'string' ||\n\t\ttypeof parsedContent.content !== 'string' ||\n\t\t( parsedContent.syncStatus &&\n\t\t\ttypeof parsedContent.syncStatus !== 'string' )\n\t) {\n\t\tthrow new Error( 'Invalid pattern JSON file' );\n\t}\n\n\tconst postType = await apiFetch< PostType >( {\n\t\tpath: `/wp/v2/types/wp_block`,\n\t} );\n\n\tconst reusableBlock = await apiFetch< ReusableBlock >( {\n\t\tpath: `/wp/v2/${ postType.rest_base }`,\n\t\tdata: {\n\t\t\ttitle: parsedContent.title,\n\t\t\tcontent: parsedContent.content,\n\t\t\tstatus: 'publish',\n\t\t\tmeta:\n\t\t\t\tparsedContent.syncStatus === 'unsynced'\n\t\t\t\t\t? { wp_pattern_sync_status: parsedContent.syncStatus }\n\t\t\t\t\t: undefined,\n\t\t} satisfies ReusableBlockData,\n\t\tmethod: 'POST',\n\t} );\n\n\treturn reusableBlock;\n}\n\nexport default importReusableBlock;\n"],
|
|
5
|
+
"mappings": ";AAGA,OAAO,cAAc;AAKrB,SAAS,oBAAoB;AAc7B,eAAe,oBAAqB,MAAuC;AAC1E,QAAM,cAAc,MAAM,aAAc,IAAK;AAC7C,MAAI;AAEJ,MAAI;AACH,oBAAgB,KAAK,MAAO,WAAY;AAAA,EACzC,QAAQ;AACP,UAAM,IAAI,MAAO,mBAAoB;AAAA,EACtC;AAEA,MACC,cAAc,WAAW,cACzB,CAAE,cAAc,SAChB,CAAE,cAAc,WAChB,OAAO,cAAc,UAAU,YAC/B,OAAO,cAAc,YAAY,YAC/B,cAAc,cACf,OAAO,cAAc,eAAe,UACpC;AACD,UAAM,IAAI,MAAO,2BAA4B;AAAA,EAC9C;AAEA,QAAM,WAAW,MAAM,SAAsB;AAAA,IAC5C,MAAM;AAAA,EACP,CAAE;AAEF,QAAM,gBAAgB,MAAM,SAA2B;AAAA,IACtD,MAAM,UAAW,SAAS,SAAU;AAAA,IACpC,MAAM;AAAA,MACL,OAAO,cAAc;AAAA,MACrB,SAAS,cAAc;AAAA,MACvB,QAAQ;AAAA,MACR,MACC,cAAc,eAAe,aAC1B,EAAE,wBAAwB,cAAc,WAAW,IACnD;AAAA,IACL;AAAA,IACA,QAAQ;AAAA,EACT,CAAE;AAEF,SAAO;AACR;AAEA,IAAO,iBAAQ;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=types.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { ImportDropdownProps } from '../../utils/types';
|
|
5
|
+
declare function ImportDropdown({ onUpload }: ImportDropdownProps): import("react").JSX.Element;
|
|
6
|
+
export default ImportDropdown;
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/import-dropdown/index.tsx"],"names":[],"mappings":"AAOA;;GAEG;AACH,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAG7D,iBAAS,cAAc,CAAE,EAAE,QAAQ,EAAE,EAAE,mBAAmB,+BAqBzD;AAED,eAAe,cAAc,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ImportFormProps } from '../../utils/types';
|
|
2
|
+
declare function ImportForm({ instanceId, onUpload }: ImportFormProps): import("react").JSX.Element;
|
|
3
|
+
declare const _default: (props: import("@wordpress/compose").WithoutInjectedProps<typeof ImportForm, {
|
|
4
|
+
instanceId: string | number;
|
|
5
|
+
}>) => import("react").JSX.Element;
|
|
6
|
+
export default _default;
|
|
7
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/components/import-form/index.tsx"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,eAAe,EAAiB,MAAM,mBAAmB,CAAC;AAExE,iBAAS,UAAU,CAAE,EAAE,UAAU,EAAE,QAAQ,EAAE,EAAE,eAAe,+BAqF7D;;;;AAED,wBAA4C"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"export.d.ts","sourceRoot":"","sources":["../../src/utils/export.ts"],"names":[],"mappings":"AAgBA;;;;GAIG;AACH,iBAAe,mBAAmB,CAAE,EAAE,EAAE,MAAM,GAAI,OAAO,CAAE,IAAI,CAAE,CA2BhE;AAED,eAAe,mBAAmB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../../src/utils/file.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,YAAY,CAAE,IAAI,EAAE,IAAI,GAAI,OAAO,CAAE,MAAM,CAAE,CAQ5D"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ReusableBlock } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Import a reusable block from a JSON file.
|
|
4
|
+
*
|
|
5
|
+
* @param file - File to import
|
|
6
|
+
* @return Promise returning the imported reusable block
|
|
7
|
+
*/
|
|
8
|
+
declare function importReusableBlock(file: File): Promise<ReusableBlock>;
|
|
9
|
+
export default importReusableBlock;
|
|
10
|
+
//# sourceMappingURL=import.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"import.d.ts","sourceRoot":"","sources":["../../src/utils/import.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAGX,aAAa,EAEb,MAAM,SAAS,CAAC;AAEjB;;;;;GAKG;AACH,iBAAe,mBAAmB,CAAE,IAAI,EAAE,IAAI,GAAI,OAAO,CAAE,aAAa,CAAE,CAyCzE;AAED,eAAe,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { Post as CorePost, Type } from '@wordpress/core-data';
|
|
5
|
+
export interface ImportDropdownProps {
|
|
6
|
+
onUpload: (data: ReusableBlock) => void;
|
|
7
|
+
}
|
|
8
|
+
export interface ImportFormProps {
|
|
9
|
+
instanceId: string | number;
|
|
10
|
+
onUpload: (reusableBlock: ReusableBlock) => void;
|
|
11
|
+
}
|
|
12
|
+
export type PostType = Type;
|
|
13
|
+
export type Post = CorePost & {
|
|
14
|
+
wp_pattern_sync_status?: string;
|
|
15
|
+
};
|
|
16
|
+
export interface ParsedContent {
|
|
17
|
+
__file: string;
|
|
18
|
+
title: string;
|
|
19
|
+
content: string;
|
|
20
|
+
syncStatus?: string;
|
|
21
|
+
[key: string]: unknown;
|
|
22
|
+
}
|
|
23
|
+
export interface ReusableBlockMeta {
|
|
24
|
+
wp_pattern_sync_status?: string;
|
|
25
|
+
}
|
|
26
|
+
export interface ReusableBlockData {
|
|
27
|
+
title: string;
|
|
28
|
+
content: string;
|
|
29
|
+
status: string;
|
|
30
|
+
meta?: ReusableBlockMeta;
|
|
31
|
+
}
|
|
32
|
+
export interface ReusableBlock {
|
|
33
|
+
id: number;
|
|
34
|
+
title: {
|
|
35
|
+
raw: string;
|
|
36
|
+
rendered: string;
|
|
37
|
+
};
|
|
38
|
+
content: {
|
|
39
|
+
raw: string;
|
|
40
|
+
rendered: string;
|
|
41
|
+
};
|
|
42
|
+
status: string;
|
|
43
|
+
[key: string]: unknown;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/utils/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,IAAI,IAAI,QAAQ,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAEnE,MAAM,WAAW,mBAAmB;IACnC,QAAQ,EAAE,CAAE,IAAI,EAAE,aAAa,KAAM,IAAI,CAAC;CAC1C;AAED,MAAM,WAAW,eAAe;IAC/B,UAAU,EAAE,MAAM,GAAG,MAAM,CAAC;IAC5B,QAAQ,EAAE,CAAE,aAAa,EAAE,aAAa,KAAM,IAAI,CAAC;CACnD;AAED,MAAM,MAAM,QAAQ,GAAG,IAAI,CAAC;AAE5B,MAAM,MAAM,IAAI,GAAG,QAAQ,GAAG;IAAE,sBAAsB,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAElE,MAAM,WAAW,aAAa;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,CAAE,GAAG,EAAE,MAAM,GAAI,OAAO,CAAC;CACzB;AAED,MAAM,WAAW,iBAAiB;IACjC,sBAAsB,CAAC,EAAE,MAAM,CAAC;CAChC;AAED,MAAM,WAAW,iBAAiB;IACjC,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,iBAAiB,CAAC;CACzB;AAED,MAAM,WAAW,aAAa;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE;QACN,GAAG,EAAE,MAAM,CAAC;QACZ,QAAQ,EAAE,MAAM,CAAC;KACjB,CAAC;IACF,OAAO,EAAE;QACR,GAAG,EAAE,MAAM,CAAC;QACZ,QAAQ,EAAE,MAAM,CAAC;KACjB,CAAC;IACF,MAAM,EAAE,MAAM,CAAC;IACf,CAAE,GAAG,EAAE,MAAM,GAAI,OAAO,CAAC;CACzB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/list-reusable-blocks",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.44.0",
|
|
4
4
|
"description": "Adding Export/Import support to the reusable blocks listing.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"module": "build-module/index.mjs",
|
|
36
36
|
"exports": {
|
|
37
37
|
".": {
|
|
38
|
+
"types": "./build-types/index.d.ts",
|
|
38
39
|
"import": "./build-module/index.mjs",
|
|
39
40
|
"require": "./build/index.cjs"
|
|
40
41
|
},
|
|
@@ -42,14 +43,16 @@
|
|
|
42
43
|
"./build-style/": "./build-style/"
|
|
43
44
|
},
|
|
44
45
|
"wpScript": true,
|
|
46
|
+
"types": "build-types",
|
|
45
47
|
"dependencies": {
|
|
46
|
-
"@wordpress/api-fetch": "^7.
|
|
47
|
-
"@wordpress/base-styles": "^6.
|
|
48
|
-
"@wordpress/blob": "^4.
|
|
49
|
-
"@wordpress/components": "^32.
|
|
50
|
-
"@wordpress/compose": "^7.
|
|
51
|
-
"@wordpress/
|
|
52
|
-
"@wordpress/
|
|
48
|
+
"@wordpress/api-fetch": "^7.44.0",
|
|
49
|
+
"@wordpress/base-styles": "^6.20.0",
|
|
50
|
+
"@wordpress/blob": "^4.44.0",
|
|
51
|
+
"@wordpress/components": "^32.6.0",
|
|
52
|
+
"@wordpress/compose": "^7.44.0",
|
|
53
|
+
"@wordpress/core-data": "^7.44.0",
|
|
54
|
+
"@wordpress/element": "^6.44.0",
|
|
55
|
+
"@wordpress/i18n": "^6.17.0",
|
|
53
56
|
"change-case": "^4.1.2"
|
|
54
57
|
},
|
|
55
58
|
"peerDependencies": {
|
|
@@ -59,5 +62,5 @@
|
|
|
59
62
|
"publishConfig": {
|
|
60
63
|
"access": "public"
|
|
61
64
|
},
|
|
62
|
-
"gitHead": "
|
|
65
|
+
"gitHead": "b862d8c84121a47bbeff882f6c87e61681ce2e0d"
|
|
63
66
|
}
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* WordPress dependencies
|
|
3
3
|
*/
|
|
4
|
+
import { Button, Dropdown } from '@wordpress/components';
|
|
4
5
|
import { pipe } from '@wordpress/compose';
|
|
5
6
|
import { __ } from '@wordpress/i18n';
|
|
6
|
-
import { Dropdown, Button } from '@wordpress/components';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Internal dependencies
|
|
10
10
|
*/
|
|
11
|
+
import type { ImportDropdownProps } from '../../utils/types';
|
|
11
12
|
import ImportForm from '../import-form';
|
|
12
13
|
|
|
13
|
-
function ImportDropdown( { onUpload } ) {
|
|
14
|
+
function ImportDropdown( { onUpload }: ImportDropdownProps ) {
|
|
14
15
|
return (
|
|
15
16
|
<Dropdown
|
|
16
17
|
popoverProps={ { placement: 'bottom-start' } }
|
|
@@ -1,50 +1,51 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* WordPress dependencies
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import { Button, Notice } from '@wordpress/components';
|
|
5
5
|
import { withInstanceId } from '@wordpress/compose';
|
|
6
|
+
import { useRef, useState } from '@wordpress/element';
|
|
6
7
|
import { __, _x } from '@wordpress/i18n';
|
|
7
|
-
import { Button, Notice } from '@wordpress/components';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* Internal dependencies
|
|
11
11
|
*/
|
|
12
12
|
import importReusableBlock from '../../utils/import';
|
|
13
|
+
import type { ImportFormProps, ReusableBlock } from '../../utils/types';
|
|
13
14
|
|
|
14
|
-
function ImportForm( { instanceId, onUpload } ) {
|
|
15
|
+
function ImportForm( { instanceId, onUpload }: ImportFormProps ) {
|
|
15
16
|
const inputId = 'list-reusable-blocks-import-form-' + instanceId;
|
|
16
17
|
|
|
17
|
-
const formRef = useRef();
|
|
18
|
+
const formRef = useRef< HTMLFormElement >( null );
|
|
18
19
|
const [ isLoading, setIsLoading ] = useState( false );
|
|
19
|
-
const [ error, setError ] = useState( null );
|
|
20
|
-
const [ file, setFile ] = useState( null );
|
|
20
|
+
const [ error, setError ] = useState< string | null >( null );
|
|
21
|
+
const [ file, setFile ] = useState< File | null >( null );
|
|
21
22
|
|
|
22
|
-
const onChangeFile = ( event ) => {
|
|
23
|
-
setFile( event.target.files[ 0 ] );
|
|
23
|
+
const onChangeFile = ( event: React.ChangeEvent< HTMLInputElement > ) => {
|
|
24
|
+
setFile( event.target.files?.[ 0 ] || null );
|
|
24
25
|
setError( null );
|
|
25
26
|
};
|
|
26
27
|
|
|
27
|
-
const onSubmit = ( event ) => {
|
|
28
|
+
const onSubmit = ( event: React.FormEvent< HTMLFormElement > ) => {
|
|
28
29
|
event.preventDefault();
|
|
29
30
|
if ( ! file ) {
|
|
30
31
|
return;
|
|
31
32
|
}
|
|
32
33
|
setIsLoading( true );
|
|
33
34
|
importReusableBlock( file )
|
|
34
|
-
.then( ( reusableBlock ) => {
|
|
35
|
-
if ( ! formRef ) {
|
|
35
|
+
.then( ( reusableBlock: ReusableBlock ) => {
|
|
36
|
+
if ( ! formRef.current ) {
|
|
36
37
|
return;
|
|
37
38
|
}
|
|
38
39
|
|
|
39
40
|
setIsLoading( false );
|
|
40
41
|
onUpload( reusableBlock );
|
|
41
42
|
} )
|
|
42
|
-
.catch( ( errors ) => {
|
|
43
|
-
if ( ! formRef ) {
|
|
43
|
+
.catch( ( errors: Error ) => {
|
|
44
|
+
if ( ! formRef.current ) {
|
|
44
45
|
return;
|
|
45
46
|
}
|
|
46
47
|
|
|
47
|
-
let uiMessage;
|
|
48
|
+
let uiMessage: string;
|
|
48
49
|
switch ( errors.message ) {
|
|
49
50
|
case 'Invalid JSON file':
|
|
50
51
|
uiMessage = __( 'Invalid JSON file' );
|
|
@@ -7,18 +7,23 @@ import { __ } from '@wordpress/i18n';
|
|
|
7
7
|
/**
|
|
8
8
|
* Internal dependencies
|
|
9
9
|
*/
|
|
10
|
-
import exportReusableBlock from './utils/export';
|
|
11
10
|
import ImportDropdown from './components/import-dropdown';
|
|
11
|
+
import exportReusableBlock from './utils/export';
|
|
12
12
|
|
|
13
13
|
// Setup Export Links.
|
|
14
|
-
document.body.addEventListener( 'click', ( event ) => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
) {
|
|
14
|
+
document.body.addEventListener( 'click', ( event: MouseEvent ) => {
|
|
15
|
+
const target = event.target as HTMLElement;
|
|
16
|
+
|
|
17
|
+
if ( ! target.classList.contains( 'wp-list-reusable-blocks__export' ) ) {
|
|
18
18
|
return;
|
|
19
19
|
}
|
|
20
|
+
|
|
20
21
|
event.preventDefault();
|
|
21
|
-
|
|
22
|
+
|
|
23
|
+
const blockId = target.dataset.id;
|
|
24
|
+
if ( blockId ) {
|
|
25
|
+
exportReusableBlock( Number( blockId ) );
|
|
26
|
+
}
|
|
22
27
|
} );
|
|
23
28
|
|
|
24
29
|
// Setup Import Form.
|
|
@@ -34,7 +39,7 @@ document.addEventListener( 'DOMContentLoaded', () => {
|
|
|
34
39
|
notice.innerHTML = `<p>${ __( 'Pattern imported successfully!' ) }</p>`;
|
|
35
40
|
|
|
36
41
|
const headerEnd = document.querySelector( '.wp-header-end' );
|
|
37
|
-
if ( ! headerEnd ) {
|
|
42
|
+
if ( ! headerEnd || ! headerEnd.parentNode ) {
|
|
38
43
|
return;
|
|
39
44
|
}
|
|
40
45
|
headerEnd.parentNode.insertBefore( notice, headerEnd );
|
|
@@ -42,6 +47,11 @@ document.addEventListener( 'DOMContentLoaded', () => {
|
|
|
42
47
|
|
|
43
48
|
const container = document.createElement( 'div' );
|
|
44
49
|
container.className = 'list-reusable-blocks__container';
|
|
50
|
+
|
|
51
|
+
if ( ! button.parentNode ) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
|
|
45
55
|
button.parentNode.insertBefore( container, button );
|
|
46
56
|
createRoot( container ).render(
|
|
47
57
|
<StrictMode>
|
|
@@ -12,20 +12,26 @@ import apiFetch from '@wordpress/api-fetch';
|
|
|
12
12
|
* Internal dependencies
|
|
13
13
|
*/
|
|
14
14
|
import { downloadBlob } from '@wordpress/blob';
|
|
15
|
+
import type { Post, PostType } from './types';
|
|
15
16
|
|
|
16
17
|
/**
|
|
17
18
|
* Export a reusable block as a JSON file.
|
|
18
19
|
*
|
|
19
|
-
* @param
|
|
20
|
+
* @param id - The ID of the reusable block to export
|
|
20
21
|
*/
|
|
21
|
-
async function exportReusableBlock( id ) {
|
|
22
|
-
const postType = await apiFetch( {
|
|
23
|
-
|
|
22
|
+
async function exportReusableBlock( id: number ): Promise< void > {
|
|
23
|
+
const postType = await apiFetch< PostType >( {
|
|
24
|
+
path: `/wp/v2/types/wp_block`,
|
|
25
|
+
} );
|
|
26
|
+
|
|
27
|
+
const post = await apiFetch< Post >( {
|
|
24
28
|
path: `/wp/v2/${ postType.rest_base }/${ id }?context=edit`,
|
|
25
29
|
} );
|
|
30
|
+
|
|
26
31
|
const title = post.title.raw;
|
|
27
32
|
const content = post.content.raw;
|
|
28
33
|
const syncStatus = post.wp_pattern_sync_status;
|
|
34
|
+
|
|
29
35
|
const fileContent = JSON.stringify(
|
|
30
36
|
{
|
|
31
37
|
__file: 'wp_block',
|
|
@@ -36,6 +42,7 @@ async function exportReusableBlock( id ) {
|
|
|
36
42
|
null,
|
|
37
43
|
2
|
|
38
44
|
);
|
|
45
|
+
|
|
39
46
|
const fileName = kebabCase( title ) + '.json';
|
|
40
47
|
|
|
41
48
|
downloadBlob( fileName, fileContent, 'application/json' );
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Reads the textual content of the given file.
|
|
3
3
|
*
|
|
4
|
-
* @param
|
|
5
|
-
* @return
|
|
4
|
+
* @param file - File to read
|
|
5
|
+
* @return Promise that resolves to the content of the file
|
|
6
6
|
*/
|
|
7
|
-
export function readTextFile( file ) {
|
|
7
|
+
export function readTextFile( file: File ): Promise< string > {
|
|
8
8
|
const reader = new window.FileReader();
|
|
9
9
|
return new Promise( ( resolve ) => {
|
|
10
10
|
reader.onload = () => {
|
|
11
|
-
resolve( reader.result );
|
|
11
|
+
resolve( reader.result as string );
|
|
12
12
|
};
|
|
13
13
|
reader.readAsText( file );
|
|
14
14
|
} );
|
|
@@ -7,21 +7,29 @@ import apiFetch from '@wordpress/api-fetch';
|
|
|
7
7
|
* Internal dependencies
|
|
8
8
|
*/
|
|
9
9
|
import { readTextFile } from './file';
|
|
10
|
+
import type {
|
|
11
|
+
ParsedContent,
|
|
12
|
+
PostType,
|
|
13
|
+
ReusableBlock,
|
|
14
|
+
ReusableBlockData,
|
|
15
|
+
} from './types';
|
|
10
16
|
|
|
11
17
|
/**
|
|
12
18
|
* Import a reusable block from a JSON file.
|
|
13
19
|
*
|
|
14
|
-
* @param
|
|
15
|
-
* @return
|
|
20
|
+
* @param file - File to import
|
|
21
|
+
* @return Promise returning the imported reusable block
|
|
16
22
|
*/
|
|
17
|
-
async function importReusableBlock( file ) {
|
|
23
|
+
async function importReusableBlock( file: File ): Promise< ReusableBlock > {
|
|
18
24
|
const fileContent = await readTextFile( file );
|
|
19
|
-
let parsedContent;
|
|
25
|
+
let parsedContent: ParsedContent;
|
|
26
|
+
|
|
20
27
|
try {
|
|
21
28
|
parsedContent = JSON.parse( fileContent );
|
|
22
|
-
} catch
|
|
29
|
+
} catch {
|
|
23
30
|
throw new Error( 'Invalid JSON file' );
|
|
24
31
|
}
|
|
32
|
+
|
|
25
33
|
if (
|
|
26
34
|
parsedContent.__file !== 'wp_block' ||
|
|
27
35
|
! parsedContent.title ||
|
|
@@ -33,8 +41,12 @@ async function importReusableBlock( file ) {
|
|
|
33
41
|
) {
|
|
34
42
|
throw new Error( 'Invalid pattern JSON file' );
|
|
35
43
|
}
|
|
36
|
-
|
|
37
|
-
const
|
|
44
|
+
|
|
45
|
+
const postType = await apiFetch< PostType >( {
|
|
46
|
+
path: `/wp/v2/types/wp_block`,
|
|
47
|
+
} );
|
|
48
|
+
|
|
49
|
+
const reusableBlock = await apiFetch< ReusableBlock >( {
|
|
38
50
|
path: `/wp/v2/${ postType.rest_base }`,
|
|
39
51
|
data: {
|
|
40
52
|
title: parsedContent.title,
|
|
@@ -44,7 +56,7 @@ async function importReusableBlock( file ) {
|
|
|
44
56
|
parsedContent.syncStatus === 'unsynced'
|
|
45
57
|
? { wp_pattern_sync_status: parsedContent.syncStatus }
|
|
46
58
|
: undefined,
|
|
47
|
-
},
|
|
59
|
+
} satisfies ReusableBlockData,
|
|
48
60
|
method: 'POST',
|
|
49
61
|
} );
|
|
50
62
|
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WordPress dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { Post as CorePost, Type } from '@wordpress/core-data';
|
|
5
|
+
|
|
6
|
+
export interface ImportDropdownProps {
|
|
7
|
+
onUpload: ( data: ReusableBlock ) => void;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface ImportFormProps {
|
|
11
|
+
instanceId: string | number;
|
|
12
|
+
onUpload: ( reusableBlock: ReusableBlock ) => void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type PostType = Type;
|
|
16
|
+
|
|
17
|
+
export type Post = CorePost & { wp_pattern_sync_status?: string };
|
|
18
|
+
|
|
19
|
+
export interface ParsedContent {
|
|
20
|
+
__file: string;
|
|
21
|
+
title: string;
|
|
22
|
+
content: string;
|
|
23
|
+
syncStatus?: string;
|
|
24
|
+
[ key: string ]: unknown;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface ReusableBlockMeta {
|
|
28
|
+
wp_pattern_sync_status?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface ReusableBlockData {
|
|
32
|
+
title: string;
|
|
33
|
+
content: string;
|
|
34
|
+
status: string;
|
|
35
|
+
meta?: ReusableBlockMeta;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface ReusableBlock {
|
|
39
|
+
id: number;
|
|
40
|
+
title: {
|
|
41
|
+
raw: string;
|
|
42
|
+
rendered: string;
|
|
43
|
+
};
|
|
44
|
+
content: {
|
|
45
|
+
raw: string;
|
|
46
|
+
rendered: string;
|
|
47
|
+
};
|
|
48
|
+
status: string;
|
|
49
|
+
[ key: string ]: unknown;
|
|
50
|
+
}
|