Nessuna descrizione

list-reusable-blocks.js 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. this["wp"] = this["wp"] || {}; this["wp"]["listReusableBlocks"] =
  2. /******/ (function(modules) { // webpackBootstrap
  3. /******/ // The module cache
  4. /******/ var installedModules = {};
  5. /******/
  6. /******/ // The require function
  7. /******/ function __webpack_require__(moduleId) {
  8. /******/
  9. /******/ // Check if module is in cache
  10. /******/ if(installedModules[moduleId]) {
  11. /******/ return installedModules[moduleId].exports;
  12. /******/ }
  13. /******/ // Create a new module (and put it into the cache)
  14. /******/ var module = installedModules[moduleId] = {
  15. /******/ i: moduleId,
  16. /******/ l: false,
  17. /******/ exports: {}
  18. /******/ };
  19. /******/
  20. /******/ // Execute the module function
  21. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  22. /******/
  23. /******/ // Flag the module as loaded
  24. /******/ module.l = true;
  25. /******/
  26. /******/ // Return the exports of the module
  27. /******/ return module.exports;
  28. /******/ }
  29. /******/
  30. /******/
  31. /******/ // expose the modules object (__webpack_modules__)
  32. /******/ __webpack_require__.m = modules;
  33. /******/
  34. /******/ // expose the module cache
  35. /******/ __webpack_require__.c = installedModules;
  36. /******/
  37. /******/ // define getter function for harmony exports
  38. /******/ __webpack_require__.d = function(exports, name, getter) {
  39. /******/ if(!__webpack_require__.o(exports, name)) {
  40. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  41. /******/ }
  42. /******/ };
  43. /******/
  44. /******/ // define __esModule on exports
  45. /******/ __webpack_require__.r = function(exports) {
  46. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  47. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  48. /******/ }
  49. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  50. /******/ };
  51. /******/
  52. /******/ // create a fake namespace object
  53. /******/ // mode & 1: value is a module id, require it
  54. /******/ // mode & 2: merge all properties of value into the ns
  55. /******/ // mode & 4: return value when already ns object
  56. /******/ // mode & 8|1: behave like require
  57. /******/ __webpack_require__.t = function(value, mode) {
  58. /******/ if(mode & 1) value = __webpack_require__(value);
  59. /******/ if(mode & 8) return value;
  60. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  61. /******/ var ns = Object.create(null);
  62. /******/ __webpack_require__.r(ns);
  63. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  64. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  65. /******/ return ns;
  66. /******/ };
  67. /******/
  68. /******/ // getDefaultExport function for compatibility with non-harmony modules
  69. /******/ __webpack_require__.n = function(module) {
  70. /******/ var getter = module && module.__esModule ?
  71. /******/ function getDefault() { return module['default']; } :
  72. /******/ function getModuleExports() { return module; };
  73. /******/ __webpack_require__.d(getter, 'a', getter);
  74. /******/ return getter;
  75. /******/ };
  76. /******/
  77. /******/ // Object.prototype.hasOwnProperty.call
  78. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  79. /******/
  80. /******/ // __webpack_public_path__
  81. /******/ __webpack_require__.p = "";
  82. /******/
  83. /******/
  84. /******/ // Load entry module and return exports
  85. /******/ return __webpack_require__(__webpack_require__.s = "SdGz");
  86. /******/ })
  87. /************************************************************************/
  88. /******/ ({
  89. /***/ "GRId":
  90. /***/ (function(module, exports) {
  91. (function() { module.exports = window["wp"]["element"]; }());
  92. /***/ }),
  93. /***/ "K9lf":
  94. /***/ (function(module, exports) {
  95. (function() { module.exports = window["wp"]["compose"]; }());
  96. /***/ }),
  97. /***/ "SdGz":
  98. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  99. "use strict";
  100. // ESM COMPAT FLAG
  101. __webpack_require__.r(__webpack_exports__);
  102. // EXTERNAL MODULE: external ["wp","element"]
  103. var external_wp_element_ = __webpack_require__("GRId");
  104. // EXTERNAL MODULE: external ["wp","i18n"]
  105. var external_wp_i18n_ = __webpack_require__("l3Sj");
  106. // EXTERNAL MODULE: external "lodash"
  107. var external_lodash_ = __webpack_require__("YLtl");
  108. // EXTERNAL MODULE: external ["wp","apiFetch"]
  109. var external_wp_apiFetch_ = __webpack_require__("ywyh");
  110. var external_wp_apiFetch_default = /*#__PURE__*/__webpack_require__.n(external_wp_apiFetch_);
  111. // CONCATENATED MODULE: ./node_modules/@wordpress/list-reusable-blocks/build-module/utils/file.js
  112. /**
  113. * Downloads a file.
  114. *
  115. * @param {string} fileName File Name.
  116. * @param {string} content File Content.
  117. * @param {string} contentType File mime type.
  118. */
  119. function download(fileName, content, contentType) {
  120. const file = new window.Blob([content], {
  121. type: contentType
  122. }); // IE11 can't use the click to download technique
  123. // we use a specific IE11 technique instead.
  124. if (window.navigator.msSaveOrOpenBlob) {
  125. window.navigator.msSaveOrOpenBlob(file, fileName);
  126. } else {
  127. const a = document.createElement('a');
  128. a.href = URL.createObjectURL(file);
  129. a.download = fileName;
  130. a.style.display = 'none';
  131. document.body.appendChild(a);
  132. a.click();
  133. document.body.removeChild(a);
  134. }
  135. }
  136. /**
  137. * Reads the textual content of the given file.
  138. *
  139. * @param {File} file File.
  140. * @return {Promise<string>} Content of the file.
  141. */
  142. function readTextFile(file) {
  143. const reader = new window.FileReader();
  144. return new Promise(resolve => {
  145. reader.onload = () => {
  146. resolve(reader.result);
  147. };
  148. reader.readAsText(file);
  149. });
  150. }
  151. // CONCATENATED MODULE: ./node_modules/@wordpress/list-reusable-blocks/build-module/utils/export.js
  152. /**
  153. * External dependencies
  154. */
  155. /**
  156. * WordPress dependencies
  157. */
  158. /**
  159. * Internal dependencies
  160. */
  161. /**
  162. * Export a reusable block as a JSON file.
  163. *
  164. * @param {number} id
  165. */
  166. async function exportReusableBlock(id) {
  167. const postType = await external_wp_apiFetch_default()({
  168. path: `/wp/v2/types/wp_block`
  169. });
  170. const post = await external_wp_apiFetch_default()({
  171. path: `/wp/v2/${postType.rest_base}/${id}?context=edit`
  172. });
  173. const title = post.title.raw;
  174. const content = post.content.raw;
  175. const fileContent = JSON.stringify({
  176. __file: 'wp_block',
  177. title,
  178. content
  179. }, null, 2);
  180. const fileName = Object(external_lodash_["kebabCase"])(title) + '.json';
  181. download(fileName, fileContent, 'application/json');
  182. }
  183. /* harmony default export */ var utils_export = (exportReusableBlock);
  184. // EXTERNAL MODULE: external ["wp","components"]
  185. var external_wp_components_ = __webpack_require__("tI+e");
  186. // EXTERNAL MODULE: external ["wp","compose"]
  187. var external_wp_compose_ = __webpack_require__("K9lf");
  188. // CONCATENATED MODULE: ./node_modules/@wordpress/list-reusable-blocks/build-module/utils/import.js
  189. /**
  190. * External dependencies
  191. */
  192. /**
  193. * WordPress dependencies
  194. */
  195. /**
  196. * Internal dependencies
  197. */
  198. /**
  199. * Import a reusable block from a JSON file.
  200. *
  201. * @param {File} file File.
  202. * @return {Promise} Promise returning the imported reusable block.
  203. */
  204. async function importReusableBlock(file) {
  205. const fileContent = await readTextFile(file);
  206. let parsedContent;
  207. try {
  208. parsedContent = JSON.parse(fileContent);
  209. } catch (e) {
  210. throw new Error('Invalid JSON file');
  211. }
  212. if (parsedContent.__file !== 'wp_block' || !parsedContent.title || !parsedContent.content || !Object(external_lodash_["isString"])(parsedContent.title) || !Object(external_lodash_["isString"])(parsedContent.content)) {
  213. throw new Error('Invalid Reusable block JSON file');
  214. }
  215. const postType = await external_wp_apiFetch_default()({
  216. path: `/wp/v2/types/wp_block`
  217. });
  218. const reusableBlock = await external_wp_apiFetch_default()({
  219. path: `/wp/v2/${postType.rest_base}`,
  220. data: {
  221. title: parsedContent.title,
  222. content: parsedContent.content,
  223. status: 'publish'
  224. },
  225. method: 'POST'
  226. });
  227. return reusableBlock;
  228. }
  229. /* harmony default export */ var utils_import = (importReusableBlock);
  230. // CONCATENATED MODULE: ./node_modules/@wordpress/list-reusable-blocks/build-module/components/import-form/index.js
  231. /**
  232. * WordPress dependencies
  233. */
  234. /**
  235. * Internal dependencies
  236. */
  237. class import_form_ImportForm extends external_wp_element_["Component"] {
  238. constructor() {
  239. super(...arguments);
  240. this.state = {
  241. isLoading: false,
  242. error: null,
  243. file: null
  244. };
  245. this.isStillMounted = true;
  246. this.onChangeFile = this.onChangeFile.bind(this);
  247. this.onSubmit = this.onSubmit.bind(this);
  248. }
  249. componentWillUnmount() {
  250. this.isStillMounted = false;
  251. }
  252. onChangeFile(event) {
  253. this.setState({
  254. file: event.target.files[0],
  255. error: null
  256. });
  257. }
  258. onSubmit(event) {
  259. event.preventDefault();
  260. const {
  261. file
  262. } = this.state;
  263. const {
  264. onUpload
  265. } = this.props;
  266. if (!file) {
  267. return;
  268. }
  269. this.setState({
  270. isLoading: true
  271. });
  272. utils_import(file).then(reusableBlock => {
  273. if (!this.isStillMounted) {
  274. return;
  275. }
  276. this.setState({
  277. isLoading: false
  278. });
  279. onUpload(reusableBlock);
  280. }).catch(error => {
  281. if (!this.isStillMounted) {
  282. return;
  283. }
  284. let uiMessage;
  285. switch (error.message) {
  286. case 'Invalid JSON file':
  287. uiMessage = Object(external_wp_i18n_["__"])('Invalid JSON file');
  288. break;
  289. case 'Invalid Reusable block JSON file':
  290. uiMessage = Object(external_wp_i18n_["__"])('Invalid Reusable block JSON file');
  291. break;
  292. default:
  293. uiMessage = Object(external_wp_i18n_["__"])('Unknown error');
  294. }
  295. this.setState({
  296. isLoading: false,
  297. error: uiMessage
  298. });
  299. });
  300. }
  301. onDismissError() {
  302. this.setState({
  303. error: null
  304. });
  305. }
  306. render() {
  307. const {
  308. instanceId
  309. } = this.props;
  310. const {
  311. file,
  312. isLoading,
  313. error
  314. } = this.state;
  315. const inputId = 'list-reusable-blocks-import-form-' + instanceId;
  316. return Object(external_wp_element_["createElement"])("form", {
  317. className: "list-reusable-blocks-import-form",
  318. onSubmit: this.onSubmit
  319. }, error && Object(external_wp_element_["createElement"])(external_wp_components_["Notice"], {
  320. status: "error",
  321. onRemove: () => this.onDismissError()
  322. }, error), Object(external_wp_element_["createElement"])("label", {
  323. htmlFor: inputId,
  324. className: "list-reusable-blocks-import-form__label"
  325. }, Object(external_wp_i18n_["__"])('File')), Object(external_wp_element_["createElement"])("input", {
  326. id: inputId,
  327. type: "file",
  328. onChange: this.onChangeFile
  329. }), Object(external_wp_element_["createElement"])(external_wp_components_["Button"], {
  330. type: "submit",
  331. isBusy: isLoading,
  332. disabled: !file || isLoading,
  333. isSecondary: true,
  334. className: "list-reusable-blocks-import-form__button"
  335. }, Object(external_wp_i18n_["_x"])('Import', 'button label')));
  336. }
  337. }
  338. /* harmony default export */ var import_form = (Object(external_wp_compose_["withInstanceId"])(import_form_ImportForm));
  339. // CONCATENATED MODULE: ./node_modules/@wordpress/list-reusable-blocks/build-module/components/import-dropdown/index.js
  340. /**
  341. * External dependencies
  342. */
  343. /**
  344. * WordPress dependencies
  345. */
  346. /**
  347. * Internal dependencies
  348. */
  349. function ImportDropdown({
  350. onUpload
  351. }) {
  352. return Object(external_wp_element_["createElement"])(external_wp_components_["Dropdown"], {
  353. position: "bottom right",
  354. contentClassName: "list-reusable-blocks-import-dropdown__content",
  355. renderToggle: ({
  356. isOpen,
  357. onToggle
  358. }) => Object(external_wp_element_["createElement"])(external_wp_components_["Button"], {
  359. "aria-expanded": isOpen,
  360. onClick: onToggle,
  361. isPrimary: true
  362. }, Object(external_wp_i18n_["__"])('Import from JSON')),
  363. renderContent: ({
  364. onClose
  365. }) => Object(external_wp_element_["createElement"])(import_form, {
  366. onUpload: Object(external_lodash_["flow"])(onClose, onUpload)
  367. })
  368. });
  369. }
  370. /* harmony default export */ var import_dropdown = (ImportDropdown);
  371. // CONCATENATED MODULE: ./node_modules/@wordpress/list-reusable-blocks/build-module/index.js
  372. /**
  373. * WordPress dependencies
  374. */
  375. /**
  376. * Internal dependencies
  377. */
  378. // Setup Export Links
  379. document.body.addEventListener('click', event => {
  380. if (!event.target.classList.contains('wp-list-reusable-blocks__export')) {
  381. return;
  382. }
  383. event.preventDefault();
  384. utils_export(event.target.dataset.id);
  385. }); // Setup Import Form
  386. document.addEventListener('DOMContentLoaded', () => {
  387. const button = document.querySelector('.page-title-action');
  388. if (!button) {
  389. return;
  390. }
  391. const showNotice = () => {
  392. const notice = document.createElement('div');
  393. notice.className = 'notice notice-success is-dismissible';
  394. notice.innerHTML = `<p>${Object(external_wp_i18n_["__"])('Reusable block imported successfully!')}</p>`;
  395. const headerEnd = document.querySelector('.wp-header-end');
  396. if (!headerEnd) {
  397. return;
  398. }
  399. headerEnd.parentNode.insertBefore(notice, headerEnd);
  400. };
  401. const container = document.createElement('div');
  402. container.className = 'list-reusable-blocks__container';
  403. button.parentNode.insertBefore(container, button);
  404. Object(external_wp_element_["render"])(Object(external_wp_element_["createElement"])(import_dropdown, {
  405. onUpload: showNotice
  406. }), container);
  407. });
  408. /***/ }),
  409. /***/ "YLtl":
  410. /***/ (function(module, exports) {
  411. (function() { module.exports = window["lodash"]; }());
  412. /***/ }),
  413. /***/ "l3Sj":
  414. /***/ (function(module, exports) {
  415. (function() { module.exports = window["wp"]["i18n"]; }());
  416. /***/ }),
  417. /***/ "tI+e":
  418. /***/ (function(module, exports) {
  419. (function() { module.exports = window["wp"]["components"]; }());
  420. /***/ }),
  421. /***/ "ywyh":
  422. /***/ (function(module, exports) {
  423. (function() { module.exports = window["wp"]["apiFetch"]; }());
  424. /***/ })
  425. /******/ });