@wordpress/patterns 1.2.0 → 1.3.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.
Files changed (55) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/build/components/category-selector.js +105 -0
  3. package/build/components/category-selector.js.map +1 -0
  4. package/build/components/create-pattern-modal.js +26 -19
  5. package/build/components/create-pattern-modal.js.map +1 -1
  6. package/build/components/pattern-convert-button.js +20 -1
  7. package/build/components/pattern-convert-button.js.map +1 -1
  8. package/build/components/patterns-manage-button.js +7 -3
  9. package/build/components/patterns-manage-button.js.map +1 -1
  10. package/build/constants.js +21 -0
  11. package/build/constants.js.map +1 -0
  12. package/build/index.js +11 -1
  13. package/build/index.js.map +1 -1
  14. package/build/private-apis.js +6 -1
  15. package/build/private-apis.js.map +1 -1
  16. package/build/store/actions.js +45 -23
  17. package/build/store/actions.js.map +1 -1
  18. package/build/store/index.js +4 -3
  19. package/build/store/index.js.map +1 -1
  20. package/build/store/selectors.js +2 -2
  21. package/build/store/selectors.js.map +1 -1
  22. package/build-module/components/category-selector.js +99 -0
  23. package/build-module/components/category-selector.js.map +1 -0
  24. package/build-module/components/create-pattern-modal.js +27 -18
  25. package/build-module/components/create-pattern-modal.js.map +1 -1
  26. package/build-module/components/pattern-convert-button.js +22 -3
  27. package/build-module/components/pattern-convert-button.js.map +1 -1
  28. package/build-module/components/patterns-manage-button.js +8 -4
  29. package/build-module/components/patterns-manage-button.js.map +1 -1
  30. package/build-module/constants.js +11 -0
  31. package/build-module/constants.js.map +1 -0
  32. package/build-module/index.js +1 -1
  33. package/build-module/index.js.map +1 -1
  34. package/build-module/private-apis.js +6 -1
  35. package/build-module/private-apis.js.map +1 -1
  36. package/build-module/store/actions.js +41 -20
  37. package/build-module/store/actions.js.map +1 -1
  38. package/build-module/store/index.js +4 -3
  39. package/build-module/store/index.js.map +1 -1
  40. package/build-module/store/selectors.js +1 -1
  41. package/build-module/store/selectors.js.map +1 -1
  42. package/build-style/style-rtl.css +7 -0
  43. package/build-style/style.css +7 -0
  44. package/package.json +16 -14
  45. package/src/components/category-selector.js +118 -0
  46. package/src/components/create-pattern-modal.js +45 -38
  47. package/src/components/pattern-convert-button.js +26 -3
  48. package/src/components/patterns-manage-button.js +10 -4
  49. package/src/components/style.scss +7 -0
  50. package/src/constants.js +16 -0
  51. package/src/index.js +1 -2
  52. package/src/private-apis.js +10 -0
  53. package/src/store/actions.js +55 -27
  54. package/src/store/index.js +3 -2
  55. package/src/store/selectors.js +1 -1
@@ -3,54 +3,76 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.__experimentalCreatePattern = exports.__experimentalConvertSyncedPatternToStatic = void 0;
7
- exports.__experimentalSetEditingPattern = __experimentalSetEditingPattern;
6
+ exports.createPatternFromFile = exports.createPattern = exports.convertSyncedPatternToStatic = void 0;
7
+ exports.setEditingPattern = setEditingPattern;
8
8
  var _blocks = require("@wordpress/blocks");
9
9
  var _coreData = require("@wordpress/core-data");
10
10
  var _blockEditor = require("@wordpress/block-editor");
11
+ var _constants = require("../constants");
11
12
  /**
12
13
  * WordPress dependencies
13
14
  */
14
15
 
16
+ /**
17
+ * Internal dependencies
18
+ */
19
+
15
20
  /**
16
21
  * Returns a generator converting one or more static blocks into a pattern, or creating a new empty pattern.
17
22
  *
18
- * @param {string} title Pattern title.
19
- * @param {'full'|'unsynced'} syncType They way block is synced, 'full' or 'unsynced'.
20
- * @param {string[]|undefined} clientIds Optional client IDs of blocks to convert to pattern.
23
+ * @param {string} title Pattern title.
24
+ * @param {'full'|'unsynced'} syncType They way block is synced, 'full' or 'unsynced'.
25
+ * @param {string|undefined} [content] Optional serialized content of blocks to convert to pattern.
26
+ * @param {number[]|undefined} [categories] Ids of any selected categories.
21
27
  */
22
- const __experimentalCreatePattern = (title, syncType, clientIds) => async ({
23
- registry,
24
- dispatch
28
+ const createPattern = (title, syncType, content, categories) => async ({
29
+ registry
25
30
  }) => {
26
- const meta = syncType === 'unsynced' ? {
31
+ const meta = syncType === _constants.PATTERN_SYNC_TYPES.unsynced ? {
27
32
  wp_pattern_sync_status: syncType
28
33
  } : undefined;
29
34
  const reusableBlock = {
30
35
  title,
31
- content: clientIds ? (0, _blocks.serialize)(registry.select(_blockEditor.store).getBlocksByClientId(clientIds)) : undefined,
36
+ content,
32
37
  status: 'publish',
33
- meta
38
+ meta,
39
+ wp_pattern_category: categories
34
40
  };
35
41
  const updatedRecord = await registry.dispatch(_coreData.store).saveEntityRecord('postType', 'wp_block', reusableBlock);
36
- if (syncType === 'unsynced' || !clientIds) {
37
- return updatedRecord;
38
- }
39
- const newBlock = (0, _blocks.createBlock)('core/block', {
40
- ref: updatedRecord.id
41
- });
42
- registry.dispatch(_blockEditor.store).replaceBlocks(clientIds, newBlock);
43
- dispatch.__experimentalSetEditingPattern(newBlock.clientId, true);
44
42
  return updatedRecord;
45
43
  };
46
44
 
45
+ /**
46
+ * Create a pattern from a JSON file.
47
+ * @param {File} file The JSON file instance of the pattern.
48
+ * @param {number[]|undefined} [categories] Ids of any selected categories.
49
+ */
50
+ exports.createPattern = createPattern;
51
+ const createPatternFromFile = (file, categories) => async ({
52
+ dispatch
53
+ }) => {
54
+ const fileContent = await file.text();
55
+ /** @type {import('./types').PatternJSON} */
56
+ let parsedContent;
57
+ try {
58
+ parsedContent = JSON.parse(fileContent);
59
+ } catch (e) {
60
+ throw new Error('Invalid JSON file');
61
+ }
62
+ if (parsedContent.__file !== 'wp_block' || !parsedContent.title || !parsedContent.content || typeof parsedContent.title !== 'string' || typeof parsedContent.content !== 'string' || parsedContent.syncStatus && typeof parsedContent.syncStatus !== 'string') {
63
+ throw new Error('Invalid Pattern JSON file');
64
+ }
65
+ const pattern = await dispatch.createPattern(parsedContent.title, parsedContent.syncStatus, parsedContent.content, categories);
66
+ return pattern;
67
+ };
68
+
47
69
  /**
48
70
  * Returns a generator converting a synced pattern block into a static block.
49
71
  *
50
72
  * @param {string} clientId The client ID of the block to attach.
51
73
  */
52
- exports.__experimentalCreatePattern = __experimentalCreatePattern;
53
- const __experimentalConvertSyncedPatternToStatic = clientId => ({
74
+ exports.createPatternFromFile = createPatternFromFile;
75
+ const convertSyncedPatternToStatic = clientId => ({
54
76
  registry
55
77
  }) => {
56
78
  const oldBlock = registry.select(_blockEditor.store).getBlock(clientId);
@@ -66,8 +88,8 @@ const __experimentalConvertSyncedPatternToStatic = clientId => ({
66
88
  * @param {boolean} isEditing Whether the block should be in editing state.
67
89
  * @return {Object} Action descriptor.
68
90
  */
69
- exports.__experimentalConvertSyncedPatternToStatic = __experimentalConvertSyncedPatternToStatic;
70
- function __experimentalSetEditingPattern(clientId, isEditing) {
91
+ exports.convertSyncedPatternToStatic = convertSyncedPatternToStatic;
92
+ function setEditingPattern(clientId, isEditing) {
71
93
  return {
72
94
  type: 'SET_EDITING_PATTERN',
73
95
  clientId,
@@ -1 +1 @@
1
- {"version":3,"names":["_blocks","require","_coreData","_blockEditor","__experimentalCreatePattern","title","syncType","clientIds","registry","dispatch","meta","wp_pattern_sync_status","undefined","reusableBlock","content","serialize","select","blockEditorStore","getBlocksByClientId","status","updatedRecord","coreStore","saveEntityRecord","newBlock","createBlock","ref","id","replaceBlocks","__experimentalSetEditingPattern","clientId","exports","__experimentalConvertSyncedPatternToStatic","oldBlock","getBlock","pattern","getEditedEntityRecord","attributes","newBlocks","parse","isEditing","type"],"sources":["@wordpress/patterns/src/store/actions.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\n\nimport { parse, serialize, createBlock } from '@wordpress/blocks';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\n\n/**\n * Returns a generator converting one or more static blocks into a pattern, or creating a new empty pattern.\n *\n * @param {string} title Pattern title.\n * @param {'full'|'unsynced'} syncType They way block is synced, 'full' or 'unsynced'.\n * @param {string[]|undefined} clientIds Optional client IDs of blocks to convert to pattern.\n */\nexport const __experimentalCreatePattern =\n\t( title, syncType, clientIds ) =>\n\tasync ( { registry, dispatch } ) => {\n\t\tconst meta =\n\t\t\tsyncType === 'unsynced'\n\t\t\t\t? {\n\t\t\t\t\t\twp_pattern_sync_status: syncType,\n\t\t\t\t }\n\t\t\t\t: undefined;\n\n\t\tconst reusableBlock = {\n\t\t\ttitle,\n\t\t\tcontent: clientIds\n\t\t\t\t? serialize(\n\t\t\t\t\t\tregistry\n\t\t\t\t\t\t\t.select( blockEditorStore )\n\t\t\t\t\t\t\t.getBlocksByClientId( clientIds )\n\t\t\t\t )\n\t\t\t\t: undefined,\n\t\t\tstatus: 'publish',\n\t\t\tmeta,\n\t\t};\n\n\t\tconst updatedRecord = await registry\n\t\t\t.dispatch( coreStore )\n\t\t\t.saveEntityRecord( 'postType', 'wp_block', reusableBlock );\n\n\t\tif ( syncType === 'unsynced' || ! clientIds ) {\n\t\t\treturn updatedRecord;\n\t\t}\n\n\t\tconst newBlock = createBlock( 'core/block', {\n\t\t\tref: updatedRecord.id,\n\t\t} );\n\t\tregistry\n\t\t\t.dispatch( blockEditorStore )\n\t\t\t.replaceBlocks( clientIds, newBlock );\n\t\tdispatch.__experimentalSetEditingPattern( newBlock.clientId, true );\n\t\treturn updatedRecord;\n\t};\n\n/**\n * Returns a generator converting a synced pattern block into a static block.\n *\n * @param {string} clientId The client ID of the block to attach.\n */\nexport const __experimentalConvertSyncedPatternToStatic =\n\t( clientId ) =>\n\t( { registry } ) => {\n\t\tconst oldBlock = registry\n\t\t\t.select( blockEditorStore )\n\t\t\t.getBlock( clientId );\n\t\tconst pattern = registry\n\t\t\t.select( 'core' )\n\t\t\t.getEditedEntityRecord(\n\t\t\t\t'postType',\n\t\t\t\t'wp_block',\n\t\t\t\toldBlock.attributes.ref\n\t\t\t);\n\n\t\tconst newBlocks = parse(\n\t\t\ttypeof pattern.content === 'function'\n\t\t\t\t? pattern.content( pattern )\n\t\t\t\t: pattern.content\n\t\t);\n\t\tregistry\n\t\t\t.dispatch( blockEditorStore )\n\t\t\t.replaceBlocks( oldBlock.clientId, newBlocks );\n\t};\n\n/**\n * Returns an action descriptor for SET_EDITING_PATTERN action.\n *\n * @param {string} clientId The clientID of the pattern to target.\n * @param {boolean} isEditing Whether the block should be in editing state.\n * @return {Object} Action descriptor.\n */\nexport function __experimentalSetEditingPattern( clientId, isEditing ) {\n\treturn {\n\t\ttype: 'SET_EDITING_PATTERN',\n\t\tclientId,\n\t\tisEditing,\n\t};\n}\n"],"mappings":";;;;;;;AAIA,IAAAA,OAAA,GAAAC,OAAA;AACA,IAAAC,SAAA,GAAAD,OAAA;AACA,IAAAE,YAAA,GAAAF,OAAA;AANA;AACA;AACA;;AAMA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMG,2BAA2B,GACvCA,CAAEC,KAAK,EAAEC,QAAQ,EAAEC,SAAS,KAC5B,OAAQ;EAAEC,QAAQ;EAAEC;AAAS,CAAC,KAAM;EACnC,MAAMC,IAAI,GACTJ,QAAQ,KAAK,UAAU,GACpB;IACAK,sBAAsB,EAAEL;EACxB,CAAC,GACDM,SAAS;EAEb,MAAMC,aAAa,GAAG;IACrBR,KAAK;IACLS,OAAO,EAAEP,SAAS,GACf,IAAAQ,iBAAS,EACTP,QAAQ,CACNQ,MAAM,CAAEC,kBAAiB,CAAC,CAC1BC,mBAAmB,CAAEX,SAAU,CACjC,CAAC,GACDK,SAAS;IACZO,MAAM,EAAE,SAAS;IACjBT;EACD,CAAC;EAED,MAAMU,aAAa,GAAG,MAAMZ,QAAQ,CAClCC,QAAQ,CAAEY,eAAU,CAAC,CACrBC,gBAAgB,CAAE,UAAU,EAAE,UAAU,EAAET,aAAc,CAAC;EAE3D,IAAKP,QAAQ,KAAK,UAAU,IAAI,CAAEC,SAAS,EAAG;IAC7C,OAAOa,aAAa;EACrB;EAEA,MAAMG,QAAQ,GAAG,IAAAC,mBAAW,EAAE,YAAY,EAAE;IAC3CC,GAAG,EAAEL,aAAa,CAACM;EACpB,CAAE,CAAC;EACHlB,QAAQ,CACNC,QAAQ,CAAEQ,kBAAiB,CAAC,CAC5BU,aAAa,CAAEpB,SAAS,EAAEgB,QAAS,CAAC;EACtCd,QAAQ,CAACmB,+BAA+B,CAAEL,QAAQ,CAACM,QAAQ,EAAE,IAAK,CAAC;EACnE,OAAOT,aAAa;AACrB,CAAC;;AAEF;AACA;AACA;AACA;AACA;AAJAU,OAAA,CAAA1B,2BAAA,GAAAA,2BAAA;AAKO,MAAM2B,0CAA0C,GACpDF,QAAQ,IACV,CAAE;EAAErB;AAAS,CAAC,KAAM;EACnB,MAAMwB,QAAQ,GAAGxB,QAAQ,CACvBQ,MAAM,CAAEC,kBAAiB,CAAC,CAC1BgB,QAAQ,CAAEJ,QAAS,CAAC;EACtB,MAAMK,OAAO,GAAG1B,QAAQ,CACtBQ,MAAM,CAAE,MAAO,CAAC,CAChBmB,qBAAqB,CACrB,UAAU,EACV,UAAU,EACVH,QAAQ,CAACI,UAAU,CAACX,GACrB,CAAC;EAEF,MAAMY,SAAS,GAAG,IAAAC,aAAK,EACtB,OAAOJ,OAAO,CAACpB,OAAO,KAAK,UAAU,GAClCoB,OAAO,CAACpB,OAAO,CAAEoB,OAAQ,CAAC,GAC1BA,OAAO,CAACpB,OACZ,CAAC;EACDN,QAAQ,CACNC,QAAQ,CAAEQ,kBAAiB,CAAC,CAC5BU,aAAa,CAAEK,QAAQ,CAACH,QAAQ,EAAEQ,SAAU,CAAC;AAChD,CAAC;;AAEF;AACA;AACA;AACA;AACA;AACA;AACA;AANAP,OAAA,CAAAC,0CAAA,GAAAA,0CAAA;AAOO,SAASH,+BAA+BA,CAAEC,QAAQ,EAAEU,SAAS,EAAG;EACtE,OAAO;IACNC,IAAI,EAAE,qBAAqB;IAC3BX,QAAQ;IACRU;EACD,CAAC;AACF"}
1
+ {"version":3,"names":["_blocks","require","_coreData","_blockEditor","_constants","createPattern","title","syncType","content","categories","registry","meta","PATTERN_SYNC_TYPES","unsynced","wp_pattern_sync_status","undefined","reusableBlock","status","wp_pattern_category","updatedRecord","dispatch","coreStore","saveEntityRecord","exports","createPatternFromFile","file","fileContent","text","parsedContent","JSON","parse","e","Error","__file","syncStatus","pattern","convertSyncedPatternToStatic","clientId","oldBlock","select","blockEditorStore","getBlock","getEditedEntityRecord","attributes","ref","newBlocks","replaceBlocks","setEditingPattern","isEditing","type"],"sources":["@wordpress/patterns/src/store/actions.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\n\nimport { parse } from '@wordpress/blocks';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\n\n/**\n * Internal dependencies\n */\nimport { PATTERN_SYNC_TYPES } from '../constants';\n\n/**\n * Returns a generator converting one or more static blocks into a pattern, or creating a new empty pattern.\n *\n * @param {string} title Pattern title.\n * @param {'full'|'unsynced'} syncType They way block is synced, 'full' or 'unsynced'.\n * @param {string|undefined} [content] Optional serialized content of blocks to convert to pattern.\n * @param {number[]|undefined} [categories] Ids of any selected categories.\n */\nexport const createPattern =\n\t( title, syncType, content, categories ) =>\n\tasync ( { registry } ) => {\n\t\tconst meta =\n\t\t\tsyncType === PATTERN_SYNC_TYPES.unsynced\n\t\t\t\t? {\n\t\t\t\t\t\twp_pattern_sync_status: syncType,\n\t\t\t\t }\n\t\t\t\t: undefined;\n\n\t\tconst reusableBlock = {\n\t\t\ttitle,\n\t\t\tcontent,\n\t\t\tstatus: 'publish',\n\t\t\tmeta,\n\t\t\twp_pattern_category: categories,\n\t\t};\n\n\t\tconst updatedRecord = await registry\n\t\t\t.dispatch( coreStore )\n\t\t\t.saveEntityRecord( 'postType', 'wp_block', reusableBlock );\n\n\t\treturn updatedRecord;\n\t};\n\n/**\n * Create a pattern from a JSON file.\n * @param {File} file The JSON file instance of the pattern.\n * @param {number[]|undefined} [categories] Ids of any selected categories.\n */\nexport const createPatternFromFile =\n\t( file, categories ) =>\n\tasync ( { dispatch } ) => {\n\t\tconst fileContent = await file.text();\n\t\t/** @type {import('./types').PatternJSON} */\n\t\tlet parsedContent;\n\t\ttry {\n\t\t\tparsedContent = JSON.parse( fileContent );\n\t\t} catch ( e ) {\n\t\t\tthrow new Error( 'Invalid JSON file' );\n\t\t}\n\t\tif (\n\t\t\tparsedContent.__file !== 'wp_block' ||\n\t\t\t! parsedContent.title ||\n\t\t\t! parsedContent.content ||\n\t\t\ttypeof parsedContent.title !== 'string' ||\n\t\t\ttypeof parsedContent.content !== 'string' ||\n\t\t\t( parsedContent.syncStatus &&\n\t\t\t\ttypeof parsedContent.syncStatus !== 'string' )\n\t\t) {\n\t\t\tthrow new Error( 'Invalid Pattern JSON file' );\n\t\t}\n\n\t\tconst pattern = await dispatch.createPattern(\n\t\t\tparsedContent.title,\n\t\t\tparsedContent.syncStatus,\n\t\t\tparsedContent.content,\n\t\t\tcategories\n\t\t);\n\n\t\treturn pattern;\n\t};\n\n/**\n * Returns a generator converting a synced pattern block into a static block.\n *\n * @param {string} clientId The client ID of the block to attach.\n */\nexport const convertSyncedPatternToStatic =\n\t( clientId ) =>\n\t( { registry } ) => {\n\t\tconst oldBlock = registry\n\t\t\t.select( blockEditorStore )\n\t\t\t.getBlock( clientId );\n\t\tconst pattern = registry\n\t\t\t.select( 'core' )\n\t\t\t.getEditedEntityRecord(\n\t\t\t\t'postType',\n\t\t\t\t'wp_block',\n\t\t\t\toldBlock.attributes.ref\n\t\t\t);\n\n\t\tconst newBlocks = parse(\n\t\t\ttypeof pattern.content === 'function'\n\t\t\t\t? pattern.content( pattern )\n\t\t\t\t: pattern.content\n\t\t);\n\t\tregistry\n\t\t\t.dispatch( blockEditorStore )\n\t\t\t.replaceBlocks( oldBlock.clientId, newBlocks );\n\t};\n\n/**\n * Returns an action descriptor for SET_EDITING_PATTERN action.\n *\n * @param {string} clientId The clientID of the pattern to target.\n * @param {boolean} isEditing Whether the block should be in editing state.\n * @return {Object} Action descriptor.\n */\nexport function setEditingPattern( clientId, isEditing ) {\n\treturn {\n\t\ttype: 'SET_EDITING_PATTERN',\n\t\tclientId,\n\t\tisEditing,\n\t};\n}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Q,EAAEW,SAAS,EAAG;EACxD,OAAO;IACNC,IAAI,EAAE,qBAAqB;IAC3BZ,QAAQ;IACRW;EACD,CAAC;AACF"}
@@ -10,6 +10,7 @@ var _reducer = _interopRequireDefault(require("./reducer"));
10
10
  var actions = _interopRequireWildcard(require("./actions"));
11
11
  var _constants = require("./constants");
12
12
  var selectors = _interopRequireWildcard(require("./selectors"));
13
+ var _lockUnlock = require("../lock-unlock");
13
14
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
14
15
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
15
16
  /**
@@ -28,9 +29,7 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
28
29
  * @type {Object}
29
30
  */
30
31
  const storeConfig = {
31
- reducer: _reducer.default,
32
- selectors,
33
- actions
32
+ reducer: _reducer.default
34
33
  };
35
34
 
36
35
  /**
@@ -46,4 +45,6 @@ const store = (0, _data.createReduxStore)(_constants.STORE_NAME, {
46
45
  });
47
46
  exports.store = store;
48
47
  (0, _data.register)(store);
48
+ (0, _lockUnlock.unlock)(store).registerPrivateActions(actions);
49
+ (0, _lockUnlock.unlock)(store).registerPrivateSelectors(selectors);
49
50
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["_data","require","_reducer","_interopRequireDefault","actions","_interopRequireWildcard","_constants","selectors","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","obj","__esModule","default","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","storeConfig","reducer","exports","store","createReduxStore","STORE_NAME","register"],"sources":["@wordpress/patterns/src/store/index.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { createReduxStore, register } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport reducer from './reducer';\nimport * as actions from './actions';\nimport { STORE_NAME } from './constants';\nimport * as selectors from './selectors';\n\n/**\n * Post editor data store configuration.\n *\n * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#registerStore\n *\n * @type {Object}\n */\nexport const storeConfig = {\n\treducer,\n\tselectors,\n\tactions,\n};\n\n/**\n * Store definition for the editor namespace.\n *\n * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore\n *\n * @type {Object}\n */\nexport const store = createReduxStore( STORE_NAME, {\n\t...storeConfig,\n} );\n\nregister( store );\n"],"mappings":";;;;;;;AAGA,IAAAA,KAAA,GAAAC,OAAA;AAKA,IAAAC,QAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,OAAA,GAAAC,uBAAA,CAAAJ,OAAA;AACA,IAAAK,UAAA,GAAAL,OAAA;AACA,IAAAM,SAAA,GAAAF,uBAAA,CAAAJ,OAAA;AAAyC,SAAAO,yBAAAC,WAAA,eAAAC,OAAA,kCAAAC,iBAAA,OAAAD,OAAA,QAAAE,gBAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,WAAA,WAAAA,WAAA,GAAAG,gBAAA,GAAAD,iBAAA,KAAAF,WAAA;AAAA,SAAA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C,KAAA,GAAAA,KAAA;AAEJ,IAAAG,cAAQ,EAAEH,KAAM,CAAC"}
1
+ {"version":3,"names":["_data","require","_reducer","_interopRequireDefault","actions","_interopRequireWildcard","_constants","selectors","_lockUnlock","_getRequireWildcardCache","nodeInterop","WeakMap","cacheBabelInterop","cacheNodeInterop","obj","__esModule","default","cache","has","get","newObj","hasPropertyDescriptor","Object","defineProperty","getOwnPropertyDescriptor","key","prototype","hasOwnProperty","call","desc","set","storeConfig","reducer","exports","store","createReduxStore","STORE_NAME","register","unlock","registerPrivateActions","registerPrivateSelectors"],"sources":["@wordpress/patterns/src/store/index.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { createReduxStore, register } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\nimport reducer from './reducer';\nimport * as actions from './actions';\nimport { STORE_NAME } from './constants';\nimport * as selectors from './selectors';\nimport { unlock } from '../lock-unlock';\n\n/**\n * Post editor data store configuration.\n *\n * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#registerStore\n *\n * @type {Object}\n */\nexport const storeConfig = {\n\treducer,\n};\n\n/**\n * Store definition for the editor namespace.\n *\n * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore\n *\n * @type {Object}\n */\nexport const store = createReduxStore( STORE_NAME, {\n\t...storeConfig,\n} );\n\nregister( store );\nunlock( store ).registerPrivateActions( actions );\nunlock( store ).registerPrivateSelectors( selectors );\n"],"mappings":";;;;;;;AAGA,IAAAA,KAAA,GAAAC,OAAA;AAKA,IAAAC,QAAA,GAAAC,sBAAA,CAAAF,OAAA;AACA,IAAAG,OAAA,GAAAC,uBAAA,CAAAJ,OAAA;AACA,IAAAK,UAAA,GAAAL,OAAA;AACA,IAAAM,SAAA,GAAAF,uBAAA,CAAAJ,OAAA;AACA,IAAAO,WAAA,GAAAP,OAAA;AAAwC,SAAAQ,yBAAAC,WAAA,eAAAC,OAAA,kCAAAC,iBAAA,OAAAD,OAAA,QAAAE,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,GAAG,IAAAC,sBAAgB,EAAEC,qBAAU,EAAE;EAClD,GAAGL;AACJ,CAAE,CAAC;AAACE,OAAA,CAAAC,KAAA,GAAAA,KAAA;AAEJ,IAAAG,cAAQ,EAAEH,KAAM,CAAC;AACjB,IAAAI,kBAAM,EAAEJ,KAAM,CAAC,CAACK,sBAAsB,CAAEnC,OAAQ,CAAC;AACjD,IAAAkC,kBAAM,EAAEJ,KAAM,CAAC,CAACM,wBAAwB,CAAEjC,SAAU,CAAC"}
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.__experimentalIsEditingPattern = __experimentalIsEditingPattern;
6
+ exports.isEditingPattern = isEditingPattern;
7
7
  /**
8
8
  * Returns true if pattern is in the editing state.
9
9
  *
@@ -11,7 +11,7 @@ exports.__experimentalIsEditingPattern = __experimentalIsEditingPattern;
11
11
  * @param {number} clientId the clientID of the block.
12
12
  * @return {boolean} Whether the pattern is in the editing state.
13
13
  */
14
- function __experimentalIsEditingPattern(state, clientId) {
14
+ function isEditingPattern(state, clientId) {
15
15
  return state.isEditingPattern[clientId];
16
16
  }
17
17
  //# sourceMappingURL=selectors.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["__experimentalIsEditingPattern","state","clientId","isEditingPattern"],"sources":["@wordpress/patterns/src/store/selectors.js"],"sourcesContent":["/**\n * Returns true if pattern is in the editing state.\n *\n * @param {Object} state Global application state.\n * @param {number} clientId the clientID of the block.\n * @return {boolean} Whether the pattern is in the editing state.\n */\nexport function __experimentalIsEditingPattern( state, clientId ) {\n\treturn state.isEditingPattern[ clientId ];\n}\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,8BAA8BA,CAAEC,KAAK,EAAEC,QAAQ,EAAG;EACjE,OAAOD,KAAK,CAACE,gBAAgB,CAAED,QAAQ,CAAE;AAC1C"}
1
+ {"version":3,"names":["isEditingPattern","state","clientId"],"sources":["@wordpress/patterns/src/store/selectors.js"],"sourcesContent":["/**\n * Returns true if pattern is in the editing state.\n *\n * @param {Object} state Global application state.\n * @param {number} clientId the clientID of the block.\n * @return {boolean} Whether the pattern is in the editing state.\n */\nexport function isEditingPattern( state, clientId ) {\n\treturn state.isEditingPattern[ clientId ];\n}\n"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,gBAAgBA,CAAEC,KAAK,EAAEC,QAAQ,EAAG;EACnD,OAAOD,KAAK,CAACD,gBAAgB,CAAEE,QAAQ,CAAE;AAC1C"}
@@ -0,0 +1,99 @@
1
+ import { createElement, Fragment } from "@wordpress/element";
2
+ /**
3
+ * WordPress dependencies
4
+ */
5
+ import { __ } from '@wordpress/i18n';
6
+ import { useMemo, useState } from '@wordpress/element';
7
+ import { FormTokenField } from '@wordpress/components';
8
+ import { useSelect, useDispatch } from '@wordpress/data';
9
+ import { store as coreStore } from '@wordpress/core-data';
10
+ import { useDebounce } from '@wordpress/compose';
11
+ import { decodeEntities } from '@wordpress/html-entities';
12
+ const unescapeString = arg => {
13
+ return decodeEntities(arg);
14
+ };
15
+ const unescapeTerm = term => {
16
+ return {
17
+ ...term,
18
+ name: unescapeString(term.name)
19
+ };
20
+ };
21
+ const EMPTY_ARRAY = [];
22
+ const MAX_TERMS_SUGGESTIONS = 20;
23
+ const DEFAULT_QUERY = {
24
+ per_page: MAX_TERMS_SUGGESTIONS,
25
+ _fields: 'id,name',
26
+ context: 'view'
27
+ };
28
+ const slug = 'wp_pattern_category';
29
+ export default function CategorySelector({
30
+ onCategorySelection
31
+ }) {
32
+ const [values, setValues] = useState([]);
33
+ const [search, setSearch] = useState('');
34
+ const debouncedSearch = useDebounce(setSearch, 500);
35
+ const {
36
+ invalidateResolution
37
+ } = useDispatch(coreStore);
38
+ const {
39
+ searchResults
40
+ } = useSelect(select => {
41
+ const {
42
+ getEntityRecords
43
+ } = select(coreStore);
44
+ return {
45
+ searchResults: !!search ? getEntityRecords('taxonomy', slug, {
46
+ ...DEFAULT_QUERY,
47
+ search
48
+ }) : EMPTY_ARRAY
49
+ };
50
+ }, [search]);
51
+ const suggestions = useMemo(() => {
52
+ return (searchResults !== null && searchResults !== void 0 ? searchResults : []).map(term => unescapeString(term.name));
53
+ }, [searchResults]);
54
+ const {
55
+ saveEntityRecord
56
+ } = useDispatch(coreStore);
57
+ async function findOrCreateTerm(term) {
58
+ try {
59
+ const newTerm = await saveEntityRecord('taxonomy', slug, term, {
60
+ throwOnError: true
61
+ });
62
+ invalidateResolution('getUserPatternCategories');
63
+ return unescapeTerm(newTerm);
64
+ } catch (error) {
65
+ if (error.code !== 'term_exists') {
66
+ throw error;
67
+ }
68
+ return {
69
+ id: error.data.term_id,
70
+ name: term.name
71
+ };
72
+ }
73
+ }
74
+ function onChange(termNames) {
75
+ const uniqueTerms = termNames.reduce((terms, newTerm) => {
76
+ if (!terms.some(term => term.toLowerCase() === newTerm.toLowerCase())) {
77
+ terms.push(newTerm);
78
+ }
79
+ return terms;
80
+ }, []);
81
+ setValues(uniqueTerms);
82
+ Promise.all(uniqueTerms.map(termName => findOrCreateTerm({
83
+ name: termName
84
+ }))).then(newTerms => {
85
+ onCategorySelection(newTerms);
86
+ });
87
+ }
88
+ return createElement(Fragment, null, createElement(FormTokenField, {
89
+ className: "patterns-menu-items__convert-modal-categories",
90
+ value: values,
91
+ suggestions: suggestions,
92
+ onChange: onChange,
93
+ onInputChange: debouncedSearch,
94
+ maxSuggestions: MAX_TERMS_SUGGESTIONS,
95
+ label: __('Categories'),
96
+ tokenizeOnBlur: true
97
+ }));
98
+ }
99
+ //# sourceMappingURL=category-selector.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["__","useMemo","useState","FormTokenField","useSelect","useDispatch","store","coreStore","useDebounce","decodeEntities","unescapeString","arg","unescapeTerm","term","name","EMPTY_ARRAY","MAX_TERMS_SUGGESTIONS","DEFAULT_QUERY","per_page","_fields","context","slug","CategorySelector","onCategorySelection","values","setValues","search","setSearch","debouncedSearch","invalidateResolution","searchResults","select","getEntityRecords","suggestions","map","saveEntityRecord","findOrCreateTerm","newTerm","throwOnError","error","code","id","data","term_id","onChange","termNames","uniqueTerms","reduce","terms","some","toLowerCase","push","Promise","all","termName","then","newTerms","createElement","Fragment","className","value","onInputChange","maxSuggestions","label","tokenizeOnBlur"],"sources":["@wordpress/patterns/src/components/category-selector.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { __ } from '@wordpress/i18n';\nimport { useMemo, useState } from '@wordpress/element';\nimport { FormTokenField } from '@wordpress/components';\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { useDebounce } from '@wordpress/compose';\nimport { decodeEntities } from '@wordpress/html-entities';\n\nconst unescapeString = ( arg ) => {\n\treturn decodeEntities( arg );\n};\n\nconst unescapeTerm = ( term ) => {\n\treturn {\n\t\t...term,\n\t\tname: unescapeString( term.name ),\n\t};\n};\n\nconst EMPTY_ARRAY = [];\nconst MAX_TERMS_SUGGESTIONS = 20;\nconst DEFAULT_QUERY = {\n\tper_page: MAX_TERMS_SUGGESTIONS,\n\t_fields: 'id,name',\n\tcontext: 'view',\n};\nconst slug = 'wp_pattern_category';\n\nexport default function CategorySelector( { onCategorySelection } ) {\n\tconst [ values, setValues ] = useState( [] );\n\tconst [ search, setSearch ] = useState( '' );\n\tconst debouncedSearch = useDebounce( setSearch, 500 );\n\tconst { invalidateResolution } = useDispatch( coreStore );\n\n\tconst { searchResults } = useSelect(\n\t\t( select ) => {\n\t\t\tconst { getEntityRecords } = select( coreStore );\n\n\t\t\treturn {\n\t\t\t\tsearchResults: !! search\n\t\t\t\t\t? getEntityRecords( 'taxonomy', slug, {\n\t\t\t\t\t\t\t...DEFAULT_QUERY,\n\t\t\t\t\t\t\tsearch,\n\t\t\t\t\t } )\n\t\t\t\t\t: EMPTY_ARRAY,\n\t\t\t};\n\t\t},\n\t\t[ search ]\n\t);\n\n\tconst suggestions = useMemo( () => {\n\t\treturn ( searchResults ?? [] ).map( ( term ) =>\n\t\t\tunescapeString( term.name )\n\t\t);\n\t}, [ searchResults ] );\n\n\tconst { saveEntityRecord } = useDispatch( coreStore );\n\n\tasync function findOrCreateTerm( term ) {\n\t\ttry {\n\t\t\tconst newTerm = await saveEntityRecord( 'taxonomy', slug, term, {\n\t\t\t\tthrowOnError: true,\n\t\t\t} );\n\t\t\tinvalidateResolution( 'getUserPatternCategories' );\n\t\t\treturn unescapeTerm( newTerm );\n\t\t} catch ( error ) {\n\t\t\tif ( error.code !== 'term_exists' ) {\n\t\t\t\tthrow error;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tid: error.data.term_id,\n\t\t\t\tname: term.name,\n\t\t\t};\n\t\t}\n\t}\n\n\tfunction onChange( termNames ) {\n\t\tconst uniqueTerms = termNames.reduce( ( terms, newTerm ) => {\n\t\t\tif (\n\t\t\t\t! terms.some(\n\t\t\t\t\t( term ) => term.toLowerCase() === newTerm.toLowerCase()\n\t\t\t\t)\n\t\t\t) {\n\t\t\t\tterms.push( newTerm );\n\t\t\t}\n\t\t\treturn terms;\n\t\t}, [] );\n\n\t\tsetValues( uniqueTerms );\n\n\t\tPromise.all(\n\t\t\tuniqueTerms.map( ( termName ) =>\n\t\t\t\tfindOrCreateTerm( { name: termName } )\n\t\t\t)\n\t\t).then( ( newTerms ) => {\n\t\t\tonCategorySelection( newTerms );\n\t\t} );\n\t}\n\n\treturn (\n\t\t<>\n\t\t\t<FormTokenField\n\t\t\t\tclassName=\"patterns-menu-items__convert-modal-categories\"\n\t\t\t\tvalue={ values }\n\t\t\t\tsuggestions={ suggestions }\n\t\t\t\tonChange={ onChange }\n\t\t\t\tonInputChange={ debouncedSearch }\n\t\t\t\tmaxSuggestions={ MAX_TERMS_SUGGESTIONS }\n\t\t\t\tlabel={ __( 'Categories' ) }\n\t\t\t\ttokenizeOnBlur={ true }\n\t\t\t/>\n\t\t</>\n\t);\n}\n"],"mappings":";AAAA;AACA;AACA;AACA,SAASA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I,CAAEd,OAAQ,CAAC;MACtB;MACA,OAAOW,KAAK;IACb,CAAC,EAAE,EAAG,CAAC;IAEPvB,SAAS,CAAEqB,WAAY,CAAC;IAExBM,OAAO,CAACC,GAAG,CACVP,WAAW,CAACZ,GAAG,CAAIoB,QAAQ,IAC1BlB,gBAAgB,CAAE;MAAEtB,IAAI,EAAEwC;IAAS,CAAE,CACtC,CACD,CAAC,CAACC,IAAI,CAAIC,QAAQ,IAAM;MACvBjC,mBAAmB,CAAEiC,QAAS,CAAC;IAChC,CAAE,CAAC;EACJ;EAEA,OACCC,aAAA,CAAAC,QAAA,QACCD,aAAA,CAACtD,cAAc;IACdwD,SAAS,EAAC,+CAA+C;IACzDC,KAAK,EAAGpC,MAAQ;IAChBS,WAAW,EAAGA,WAAa;IAC3BW,QAAQ,EAAGA,QAAU;IACrBiB,aAAa,EAAGjC,eAAiB;IACjCkC,cAAc,EAAG9C,qBAAuB;IACxC+C,KAAK,EAAG/D,EAAE,CAAE,YAAa,CAAG;IAC5BgE,cAAc,EAAG;EAAM,CACvB,CACA,CAAC;AAEL"}
@@ -4,40 +4,43 @@ import { createElement } from "@wordpress/element";
4
4
  */
5
5
  import { Modal, Button, TextControl, __experimentalHStack as HStack, __experimentalVStack as VStack, ToggleControl } from '@wordpress/components';
6
6
  import { __ } from '@wordpress/i18n';
7
- import { useState, useCallback } from '@wordpress/element';
7
+ import { useState } from '@wordpress/element';
8
8
  import { useDispatch } from '@wordpress/data';
9
9
  import { store as noticesStore } from '@wordpress/notices';
10
- export const USER_PATTERN_CATEGORY = 'my-patterns';
11
- export const SYNC_TYPES = {
12
- full: undefined,
13
- unsynced: 'unsynced'
14
- };
15
10
 
16
11
  /**
17
12
  * Internal dependencies
18
13
  */
19
- import { store } from '../store';
14
+ import { PATTERN_DEFAULT_CATEGORY, PATTERN_SYNC_TYPES } from '../constants';
15
+
16
+ /**
17
+ * Internal dependencies
18
+ */
19
+ import { store as patternsStore } from '../store';
20
+ import CategorySelector from './category-selector';
21
+ import { unlock } from '../lock-unlock';
20
22
  export default function CreatePatternModal({
21
23
  onSuccess,
22
24
  onError,
23
- clientIds,
25
+ content,
24
26
  onClose,
25
27
  className = 'patterns-menu-items__convert-modal'
26
28
  }) {
27
- const [syncType, setSyncType] = useState(SYNC_TYPES.full);
29
+ const [syncType, setSyncType] = useState(PATTERN_SYNC_TYPES.full);
30
+ const [categories, setCategories] = useState([]);
28
31
  const [title, setTitle] = useState('');
29
32
  const {
30
- __experimentalCreatePattern: createPattern
31
- } = useDispatch(store);
33
+ createPattern
34
+ } = unlock(useDispatch(patternsStore));
32
35
  const {
33
36
  createErrorNotice
34
37
  } = useDispatch(noticesStore);
35
- const onCreate = useCallback(async function (patternTitle, sync) {
38
+ async function onCreate(patternTitle, sync) {
36
39
  try {
37
- const newPattern = await createPattern(patternTitle, sync, clientIds);
40
+ const newPattern = await createPattern(patternTitle, sync, typeof content === 'function' ? content() : content, categories);
38
41
  onSuccess({
39
42
  pattern: newPattern,
40
- categoryId: USER_PATTERN_CATEGORY
43
+ categoryId: PATTERN_DEFAULT_CATEGORY
41
44
  });
42
45
  } catch (error) {
43
46
  createErrorNotice(error.message, {
@@ -46,7 +49,10 @@ export default function CreatePatternModal({
46
49
  });
47
50
  onError();
48
51
  }
49
- }, [createPattern, clientIds, onSuccess, createErrorNotice, onError]);
52
+ }
53
+ const handleCategorySelection = selectedCategories => {
54
+ setCategories(selectedCategories.map(cat => cat.id));
55
+ };
50
56
  return createElement(Modal, {
51
57
  title: __('Create pattern'),
52
58
  onRequestClose: () => {
@@ -67,13 +73,16 @@ export default function CreatePatternModal({
67
73
  label: __('Name'),
68
74
  value: title,
69
75
  onChange: setTitle,
70
- placeholder: __('My pattern')
76
+ placeholder: __('My pattern'),
77
+ className: "patterns-create-modal__name-input"
78
+ }), createElement(CategorySelector, {
79
+ onCategorySelection: handleCategorySelection
71
80
  }), createElement(ToggleControl, {
72
81
  label: __('Synced'),
73
82
  help: __('Editing the pattern will update it anywhere it is used.'),
74
- checked: !syncType,
83
+ checked: syncType === PATTERN_SYNC_TYPES.full,
75
84
  onChange: () => {
76
- setSyncType(syncType === SYNC_TYPES.full ? SYNC_TYPES.unsynced : SYNC_TYPES.full);
85
+ setSyncType(syncType === PATTERN_SYNC_TYPES.full ? PATTERN_SYNC_TYPES.unsynced : PATTERN_SYNC_TYPES.full);
77
86
  }
78
87
  }), createElement(HStack, {
79
88
  justify: "right"
@@ -1 +1 @@
1
- {"version":3,"names":["Modal","Button","TextControl","__experimentalHStack","HStack","__experimentalVStack","VStack","ToggleControl","__","useState","useCallback","useDispatch","store","noticesStore","USER_PATTERN_CATEGORY","SYNC_TYPES","full","undefined","unsynced","CreatePatternModal","onSuccess","onError","clientIds","onClose","className","syncType","setSyncType","title","setTitle","__experimentalCreatePattern","createPattern","createErrorNotice","onCreate","patternTitle","sync","newPattern","pattern","categoryId","error","message","type","id","createElement","onRequestClose","overlayClassName","onSubmit","event","preventDefault","spacing","__nextHasNoMarginBottom","label","value","onChange","placeholder","help","checked","justify","variant","onClick"],"sources":["@wordpress/patterns/src/components/create-pattern-modal.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport {\n\tModal,\n\tButton,\n\tTextControl,\n\t__experimentalHStack as HStack,\n\t__experimentalVStack as VStack,\n\tToggleControl,\n} from '@wordpress/components';\nimport { __ } from '@wordpress/i18n';\nimport { useState, useCallback } from '@wordpress/element';\nimport { useDispatch } from '@wordpress/data';\nimport { store as noticesStore } from '@wordpress/notices';\n\nexport const USER_PATTERN_CATEGORY = 'my-patterns';\n\nexport const SYNC_TYPES = {\n\tfull: undefined,\n\tunsynced: 'unsynced',\n};\n\n/**\n * Internal dependencies\n */\nimport { store } from '../store';\n\nexport default function CreatePatternModal( {\n\tonSuccess,\n\tonError,\n\tclientIds,\n\tonClose,\n\tclassName = 'patterns-menu-items__convert-modal',\n} ) {\n\tconst [ syncType, setSyncType ] = useState( SYNC_TYPES.full );\n\tconst [ title, setTitle ] = useState( '' );\n\tconst { __experimentalCreatePattern: createPattern } = useDispatch( store );\n\n\tconst { createErrorNotice } = useDispatch( noticesStore );\n\tconst onCreate = useCallback(\n\t\tasync function ( patternTitle, sync ) {\n\t\t\ttry {\n\t\t\t\tconst newPattern = await createPattern(\n\t\t\t\t\tpatternTitle,\n\t\t\t\t\tsync,\n\t\t\t\t\tclientIds\n\t\t\t\t);\n\t\t\t\tonSuccess( {\n\t\t\t\t\tpattern: newPattern,\n\t\t\t\t\tcategoryId: USER_PATTERN_CATEGORY,\n\t\t\t\t} );\n\t\t\t} catch ( error ) {\n\t\t\t\tcreateErrorNotice( error.message, {\n\t\t\t\t\ttype: 'snackbar',\n\t\t\t\t\tid: 'convert-to-pattern-error',\n\t\t\t\t} );\n\t\t\t\tonError();\n\t\t\t}\n\t\t},\n\t\t[ createPattern, clientIds, onSuccess, createErrorNotice, onError ]\n\t);\n\treturn (\n\t\t<Modal\n\t\t\ttitle={ __( 'Create pattern' ) }\n\t\t\tonRequestClose={ () => {\n\t\t\t\tonClose();\n\t\t\t\tsetTitle( '' );\n\t\t\t} }\n\t\t\toverlayClassName={ className }\n\t\t>\n\t\t\t<form\n\t\t\t\tonSubmit={ ( event ) => {\n\t\t\t\t\tevent.preventDefault();\n\t\t\t\t\tonCreate( title, syncType );\n\t\t\t\t\tsetTitle( '' );\n\t\t\t\t} }\n\t\t\t>\n\t\t\t\t<VStack spacing=\"5\">\n\t\t\t\t\t<TextControl\n\t\t\t\t\t\t__nextHasNoMarginBottom\n\t\t\t\t\t\tlabel={ __( 'Name' ) }\n\t\t\t\t\t\tvalue={ title }\n\t\t\t\t\t\tonChange={ setTitle }\n\t\t\t\t\t\tplaceholder={ __( 'My pattern' ) }\n\t\t\t\t\t/>\n\n\t\t\t\t\t<ToggleControl\n\t\t\t\t\t\tlabel={ __( 'Synced' ) }\n\t\t\t\t\t\thelp={ __(\n\t\t\t\t\t\t\t'Editing the pattern will update it anywhere it is used.'\n\t\t\t\t\t\t) }\n\t\t\t\t\t\tchecked={ ! syncType }\n\t\t\t\t\t\tonChange={ () => {\n\t\t\t\t\t\t\tsetSyncType(\n\t\t\t\t\t\t\t\tsyncType === SYNC_TYPES.full\n\t\t\t\t\t\t\t\t\t? SYNC_TYPES.unsynced\n\t\t\t\t\t\t\t\t\t: SYNC_TYPES.full\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t} }\n\t\t\t\t\t/>\n\t\t\t\t\t<HStack justify=\"right\">\n\t\t\t\t\t\t<Button\n\t\t\t\t\t\t\tvariant=\"tertiary\"\n\t\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\t\tonClose();\n\t\t\t\t\t\t\t\tsetTitle( '' );\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{ __( 'Cancel' ) }\n\t\t\t\t\t\t</Button>\n\n\t\t\t\t\t\t<Button variant=\"primary\" type=\"submit\">\n\t\t\t\t\t\t\t{ __( 'Create' ) }\n\t\t\t\t\t\t</Button>\n\t\t\t\t\t</HStack>\n\t\t\t\t</VStack>\n\t\t\t</form>\n\t\t</Modal>\n\t);\n}\n"],"mappings":";AAAA;AACA;AACA;AACA,SACCA,KAAK,EACLC,MAAM,EACNC,WAAW,EACXC,oBAAoB,IAAIC,MAAM,EAC9BC,oBAAoB,IAAIC,MAAM,EAC9BC,aAAa,QACP,uBAAuB;AAC9B,SAASC,EAAE,QAAQ,iBAAiB;AACpC,SAASC,QAAQ,EAAEC,WAAW,QAAQ,oBAAoB;AAC1D,SAASC,WAAW,QAAQ,iBAAiB;AAC7C,SAASC,KAAK,IAAIC,YAAY,QAAQ,oBAAoB;AAE1D,OAAO,MAAMC,qBAAqB,GAAG,aAAa;AAElD,OAAO,MAAMC,UAAU,GAAG;EACzBC,IAAI,EAAEC,SAAS;EACfC,QAAQ,EAAE;AACX,CAAC;;AAED;AACA;AACA;AACA,SAASN,KAAK,QAAQ,UAAU;AAEhC,eAAe,SAASO,kBAAkBA,CAAE;EAC3CC,SAAS;EAC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EL,KAAK,EAAEF,QAAS,CAAC;MAC3BG,QAAQ,CAAE,EAAG,CAAC;IACf;EAAG,GAEHc,aAAA,CAACpC,MAAM;IAAC0C,OAAO,EAAC;EAAG,GAClBN,aAAA,CAACxC,WAAW;IACX+C,uBAAuB;IACvBC,KAAK,EAAG1C,EAAE,CAAE,MAAO,CAAG;IACtB2C,KAAK,EAAGxB,KAAO;IACfyB,QAAQ,EAAGxB,QAAU;IACrByB,WAAW,EAAG7C,EAAE,CAAE,YAAa;EAAG,CAClC,CAAC,EAEFkC,aAAA,CAACnC,aAAa;IACb2C,KAAK,EAAG1C,EAAE,CAAE,QAAS,CAAG;IACxB8C,IAAI,EAAG9C,EAAE,CACR,yDACD,CAAG;IACH+C,OAAO,EAAG,CAAE9B,QAAU;IACtB2B,QAAQ,EAAGA,CAAA,KAAM;MAChB1B,WAAW,CACVD,QAAQ,KAAKV,UAAU,CAACC,IAAI,GACzBD,UAAU,CAACG,QAAQ,GACnBH,UAAU,CAACC,IACf,CAAC;IACF;EAAG,CACH,CAAC,EACF0B,aAAA,CAACtC,MAAM;IAACoD,OAAO,EAAC;EAAO,GACtBd,aAAA,CAACzC,MAAM;IACNwD,OAAO,EAAC,UAAU;IAClBC,OAAO,EAAGA,CAAA,KAAM;MACfnC,OAAO,CAAC,CAAC;MACTK,QAAQ,CAAE,EAAG,CAAC;IACf;EAAG,GAEDpB,EAAE,CAAE,QAAS,CACR,CAAC,EAETkC,aAAA,CAACzC,MAAM;IAACwD,OAAO,EAAC,SAAS;IAACjB,IAAI,EAAC;EAAQ,GACpChC,EAAE,CAAE,QAAS,CACR,CACD,CACD,CACH,CACA,CAAC;AAEV"}
1
+ {"version":3,"names":["Modal","Button","TextControl","__experimentalHStack","HStack","__experimentalVStack","VStack","ToggleControl","__","useState","useDispatch","store","noticesStore","PATTERN_DEFAULT_CATEGORY","PATTERN_SYNC_TYPES","patternsStore","CategorySelector","unlock","CreatePatternModal","onSuccess","onError","content","onClose","className","syncType","setSyncType","full","categories","setCategories","title","setTitle","createPattern","createErrorNotice","onCreate","patternTitle","sync","newPattern","pattern","categoryId","error","message","type","id","handleCategorySelection","selectedCategories","map","cat","createElement","onRequestClose","overlayClassName","onSubmit","event","preventDefault","spacing","__nextHasNoMarginBottom","label","value","onChange","placeholder","onCategorySelection","help","checked","unsynced","justify","variant","onClick"],"sources":["@wordpress/patterns/src/components/create-pattern-modal.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport {\n\tModal,\n\tButton,\n\tTextControl,\n\t__experimentalHStack as HStack,\n\t__experimentalVStack as VStack,\n\tToggleControl,\n} from '@wordpress/components';\nimport { __ } from '@wordpress/i18n';\nimport { useState } from '@wordpress/element';\nimport { useDispatch } from '@wordpress/data';\nimport { store as noticesStore } from '@wordpress/notices';\n\n/**\n * Internal dependencies\n */\nimport { PATTERN_DEFAULT_CATEGORY, PATTERN_SYNC_TYPES } from '../constants';\n\n/**\n * Internal dependencies\n */\nimport { store as patternsStore } from '../store';\nimport CategorySelector from './category-selector';\nimport { unlock } from '../lock-unlock';\n\nexport default function CreatePatternModal( {\n\tonSuccess,\n\tonError,\n\tcontent,\n\tonClose,\n\tclassName = 'patterns-menu-items__convert-modal',\n} ) {\n\tconst [ syncType, setSyncType ] = useState( PATTERN_SYNC_TYPES.full );\n\tconst [ categories, setCategories ] = useState( [] );\n\tconst [ title, setTitle ] = useState( '' );\n\tconst { createPattern } = unlock( useDispatch( patternsStore ) );\n\n\tconst { createErrorNotice } = useDispatch( noticesStore );\n\tasync function onCreate( patternTitle, sync ) {\n\t\ttry {\n\t\t\tconst newPattern = await createPattern(\n\t\t\t\tpatternTitle,\n\t\t\t\tsync,\n\t\t\t\ttypeof content === 'function' ? content() : content,\n\t\t\t\tcategories\n\t\t\t);\n\t\t\tonSuccess( {\n\t\t\t\tpattern: newPattern,\n\t\t\t\tcategoryId: PATTERN_DEFAULT_CATEGORY,\n\t\t\t} );\n\t\t} catch ( error ) {\n\t\t\tcreateErrorNotice( error.message, {\n\t\t\t\ttype: 'snackbar',\n\t\t\t\tid: 'convert-to-pattern-error',\n\t\t\t} );\n\t\t\tonError();\n\t\t}\n\t}\n\n\tconst handleCategorySelection = ( selectedCategories ) => {\n\t\tsetCategories( selectedCategories.map( ( cat ) => cat.id ) );\n\t};\n\n\treturn (\n\t\t<Modal\n\t\t\ttitle={ __( 'Create pattern' ) }\n\t\t\tonRequestClose={ () => {\n\t\t\t\tonClose();\n\t\t\t\tsetTitle( '' );\n\t\t\t} }\n\t\t\toverlayClassName={ className }\n\t\t>\n\t\t\t<form\n\t\t\t\tonSubmit={ ( event ) => {\n\t\t\t\t\tevent.preventDefault();\n\t\t\t\t\tonCreate( title, syncType );\n\t\t\t\t\tsetTitle( '' );\n\t\t\t\t} }\n\t\t\t>\n\t\t\t\t<VStack spacing=\"5\">\n\t\t\t\t\t<TextControl\n\t\t\t\t\t\t__nextHasNoMarginBottom\n\t\t\t\t\t\tlabel={ __( 'Name' ) }\n\t\t\t\t\t\tvalue={ title }\n\t\t\t\t\t\tonChange={ setTitle }\n\t\t\t\t\t\tplaceholder={ __( 'My pattern' ) }\n\t\t\t\t\t\tclassName=\"patterns-create-modal__name-input\"\n\t\t\t\t\t/>\n\t\t\t\t\t<CategorySelector\n\t\t\t\t\t\tonCategorySelection={ handleCategorySelection }\n\t\t\t\t\t/>\n\t\t\t\t\t<ToggleControl\n\t\t\t\t\t\tlabel={ __( 'Synced' ) }\n\t\t\t\t\t\thelp={ __(\n\t\t\t\t\t\t\t'Editing the pattern will update it anywhere it is used.'\n\t\t\t\t\t\t) }\n\t\t\t\t\t\tchecked={ syncType === PATTERN_SYNC_TYPES.full }\n\t\t\t\t\t\tonChange={ () => {\n\t\t\t\t\t\t\tsetSyncType(\n\t\t\t\t\t\t\t\tsyncType === PATTERN_SYNC_TYPES.full\n\t\t\t\t\t\t\t\t\t? PATTERN_SYNC_TYPES.unsynced\n\t\t\t\t\t\t\t\t\t: PATTERN_SYNC_TYPES.full\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t} }\n\t\t\t\t\t/>\n\t\t\t\t\t<HStack justify=\"right\">\n\t\t\t\t\t\t<Button\n\t\t\t\t\t\t\tvariant=\"tertiary\"\n\t\t\t\t\t\t\tonClick={ () => {\n\t\t\t\t\t\t\t\tonClose();\n\t\t\t\t\t\t\t\tsetTitle( '' );\n\t\t\t\t\t\t\t} }\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{ __( 'Cancel' ) }\n\t\t\t\t\t\t</Button>\n\n\t\t\t\t\t\t<Button variant=\"primary\" type=\"submit\">\n\t\t\t\t\t\t\t{ __( 'Create' ) }\n\t\t\t\t\t\t</Button>\n\t\t\t\t\t</HStack>\n\t\t\t\t</VStack>\n\t\t\t</form>\n\t\t</Modal>\n\t);\n}\n"],"mappings":";AAAA;AACA;AACA;AACA,SACCA,KAAK,EACLC,MAAM,EACNC,WAAW,EACXC,oBAAoB,IAAIC,MAAM,EAC9BC,oBAAoB,IAAIC,MAAM,EAC9BC,aAAa,QACP,uBAAuB;AAC9B,SAASC,EAAE,QAAQ,iBAAiB;AACpC,SAASC,QAAQ,QAAQ,oBAAoB;AAC7C,SAASC,WAAW,QAAQ,iBAAiB;AAC7C,SAASC,KAAK,IAAIC,YAAY,QAAQ,oBAAoB;;AAE1D;AACA;AACA;AACA,SAASC,wBAAwB,EAAEC,kBAAkB,QAAQ,cAAc;;AAE3E;AACA;AACA;AACA,SAASH,KAAK,IAAII,aAAa,QAAQ,UAAU;AACjD,OAAOC,gBAAgB,MAAM,qBAAqB;AAClD,SAASC,MAAM,QAAQ,gBAAgB;AAEvC,eAAe,SAASC,kBAAkBA,CAAE;EAC3CC,SAAS;EACTC,OAAO;EACPC,OAAO;EACPC,OAAO;EACPC,SAAS,GAAG;AACb,CAAC,EAAG;EACH,MAAM,CAAEC,QAAQ,EAAEC,WAAW,CAAE,GAAGhB,QAAQ,CAAEK,kBAAkB,CAACY,IAAK,CAAC;EACrE,MAAM,CAAEC,UAAU,EAAEC,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
  /**
3
3
  * WordPress dependencies
4
4
  */
5
- import { hasBlockSupport, isReusableBlock } from '@wordpress/blocks';
5
+ import { hasBlockSupport, isReusableBlock, createBlock, serialize } from '@wordpress/blocks';
6
6
  import { store as blockEditorStore } from '@wordpress/block-editor';
7
- import { useState } from '@wordpress/element';
7
+ import { useState, useCallback } from '@wordpress/element';
8
8
  import { MenuItem } from '@wordpress/components';
9
9
  import { symbol } from '@wordpress/icons';
10
10
  import { useSelect, useDispatch } from '@wordpress/data';
@@ -14,7 +14,9 @@ import { store as noticesStore } from '@wordpress/notices';
14
14
  /**
15
15
  * Internal dependencies
16
16
  */
17
+ import { store as patternsStore } from '../store';
17
18
  import CreatePatternModal from './create-pattern-modal';
19
+ import { unlock } from '../lock-unlock';
18
20
 
19
21
  /**
20
22
  * Menu control to convert block(s) to a pattern block.
@@ -31,6 +33,14 @@ export default function PatternConvertButton({
31
33
  const {
32
34
  createSuccessNotice
33
35
  } = useDispatch(noticesStore);
36
+ const {
37
+ replaceBlocks
38
+ } = useDispatch(blockEditorStore);
39
+ // Ignore reason: false positive of the lint rule.
40
+ // eslint-disable-next-line @wordpress/no-unused-vars-before-return
41
+ const {
42
+ setEditingPattern
43
+ } = unlock(useDispatch(patternsStore));
34
44
  const [isModalOpen, setIsModalOpen] = useState(false);
35
45
  const canConvert = useSelect(select => {
36
46
  var _getBlocksByClientId;
@@ -60,12 +70,21 @@ export default function PatternConvertButton({
60
70
  !!canUser('create', 'blocks');
61
71
  return _canConvert;
62
72
  }, [clientIds, rootClientId]);
73
+ const {
74
+ getBlocksByClientId
75
+ } = useSelect(blockEditorStore);
76
+ const getContent = useCallback(() => serialize(getBlocksByClientId(clientIds)), [getBlocksByClientId, clientIds]);
63
77
  if (!canConvert) {
64
78
  return null;
65
79
  }
66
80
  const handleSuccess = ({
67
81
  pattern
68
82
  }) => {
83
+ const newBlock = createBlock('core/block', {
84
+ ref: pattern.id
85
+ });
86
+ replaceBlocks(clientIds, newBlock);
87
+ setEditingPattern(newBlock.clientId, true);
69
88
  createSuccessNotice(pattern.wp_pattern_sync_status === 'unsynced' ? sprintf(
70
89
  // translators: %s: the name the user has given to the pattern.
71
90
  __('Unsynced Pattern created: %s'), pattern.title.raw) : sprintf(
@@ -82,7 +101,7 @@ export default function PatternConvertButton({
82
101
  "aria-expanded": isModalOpen,
83
102
  "aria-haspopup": "dialog"
84
103
  }, __('Create pattern')), isModalOpen && createElement(CreatePatternModal, {
85
- clientIds: clientIds,
104
+ content: getContent,
86
105
  onSuccess: pattern => {
87
106
  handleSuccess(pattern);
88
107
  },
@@ -1 +1 @@
1
- {"version":3,"names":["hasBlockSupport","isReusableBlock","store","blockEditorStore","useState","MenuItem","symbol","useSelect","useDispatch","coreStore","__","sprintf","noticesStore","CreatePatternModal","PatternConvertButton","clientIds","rootClientId","createSuccessNotice","isModalOpen","setIsModalOpen","canConvert","select","_getBlocksByClientId","canUser","getBlocksByClientId","canInsertBlockType","getBlockRootClientId","rootId","length","undefined","blocks","isReusable","getEntityRecord","attributes","ref","_canConvert","every","block","isValid","name","handleSuccess","pattern","wp_pattern_sync_status","title","raw","type","id","createElement","Fragment","icon","onClick","onSuccess","onError","onClose"],"sources":["@wordpress/patterns/src/components/pattern-convert-button.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport { hasBlockSupport, isReusableBlock } from '@wordpress/blocks';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\nimport { useState } from '@wordpress/element';\nimport { MenuItem } from '@wordpress/components';\nimport { symbol } from '@wordpress/icons';\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { __, sprintf } from '@wordpress/i18n';\nimport { store as noticesStore } from '@wordpress/notices';\n/**\n * Internal dependencies\n */\nimport CreatePatternModal from './create-pattern-modal';\n\n/**\n * Menu control to convert block(s) to a pattern block.\n *\n * @param {Object} props Component props.\n * @param {string[]} props.clientIds Client ids of selected blocks.\n * @param {string} props.rootClientId ID of the currently selected top-level block.\n * @return {import('@wordpress/element').WPComponent} The menu control or null.\n */\nexport default function PatternConvertButton( { clientIds, rootClientId } ) {\n\tconst { createSuccessNotice } = useDispatch( noticesStore );\n\tconst [ isModalOpen, setIsModalOpen ] = useState( false );\n\tconst canConvert = useSelect(\n\t\t( select ) => {\n\t\t\tconst { canUser } = select( coreStore );\n\t\t\tconst {\n\t\t\t\tgetBlocksByClientId,\n\t\t\t\tcanInsertBlockType,\n\t\t\t\tgetBlockRootClientId,\n\t\t\t} = select( blockEditorStore );\n\n\t\t\tconst rootId =\n\t\t\t\trootClientId ||\n\t\t\t\t( clientIds.length > 0\n\t\t\t\t\t? getBlockRootClientId( clientIds[ 0 ] )\n\t\t\t\t\t: undefined );\n\n\t\t\tconst blocks = getBlocksByClientId( clientIds ) ?? [];\n\n\t\t\tconst isReusable =\n\t\t\t\tblocks.length === 1 &&\n\t\t\t\tblocks[ 0 ] &&\n\t\t\t\tisReusableBlock( blocks[ 0 ] ) &&\n\t\t\t\t!! select( coreStore ).getEntityRecord(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_block',\n\t\t\t\t\tblocks[ 0 ].attributes.ref\n\t\t\t\t);\n\n\t\t\tconst _canConvert =\n\t\t\t\t// Hide when this is already a synced pattern.\n\t\t\t\t! isReusable &&\n\t\t\t\t// Hide when patterns are disabled.\n\t\t\t\tcanInsertBlockType( 'core/block', rootId ) &&\n\t\t\t\tblocks.every(\n\t\t\t\t\t( block ) =>\n\t\t\t\t\t\t// Guard against the case where a regular block has *just* been converted.\n\t\t\t\t\t\t!! block &&\n\t\t\t\t\t\t// Hide on invalid blocks.\n\t\t\t\t\t\tblock.isValid &&\n\t\t\t\t\t\t// Hide when block doesn't support being made into a pattern.\n\t\t\t\t\t\thasBlockSupport( block.name, 'reusable', true )\n\t\t\t\t) &&\n\t\t\t\t// Hide when current doesn't have permission to do that.\n\t\t\t\t!! canUser( 'create', 'blocks' );\n\n\t\t\treturn _canConvert;\n\t\t},\n\t\t[ clientIds, rootClientId ]\n\t);\n\n\tif ( ! canConvert ) {\n\t\treturn null;\n\t}\n\n\tconst handleSuccess = ( { pattern } ) => {\n\t\tcreateSuccessNotice(\n\t\t\tpattern.wp_pattern_sync_status === 'unsynced'\n\t\t\t\t? sprintf(\n\t\t\t\t\t\t// translators: %s: the name the user has given to the pattern.\n\t\t\t\t\t\t__( 'Unsynced Pattern created: %s' ),\n\t\t\t\t\t\tpattern.title.raw\n\t\t\t\t )\n\t\t\t\t: sprintf(\n\t\t\t\t\t\t// translators: %s: the name the user has given to the pattern.\n\t\t\t\t\t\t__( 'Synced Pattern created: %s' ),\n\t\t\t\t\t\tpattern.title.raw\n\t\t\t\t ),\n\t\t\t{\n\t\t\t\ttype: 'snackbar',\n\t\t\t\tid: 'convert-to-pattern-success',\n\t\t\t}\n\t\t);\n\t\tsetIsModalOpen( false );\n\t};\n\treturn (\n\t\t<>\n\t\t\t<MenuItem\n\t\t\t\ticon={ symbol }\n\t\t\t\tonClick={ () => setIsModalOpen( true ) }\n\t\t\t\taria-expanded={ isModalOpen }\n\t\t\t\taria-haspopup=\"dialog\"\n\t\t\t>\n\t\t\t\t{ __( 'Create pattern' ) }\n\t\t\t</MenuItem>\n\t\t\t{ isModalOpen && (\n\t\t\t\t<CreatePatternModal\n\t\t\t\t\tclientIds={ clientIds }\n\t\t\t\t\tonSuccess={ ( pattern ) => {\n\t\t\t\t\t\thandleSuccess( pattern );\n\t\t\t\t\t} }\n\t\t\t\t\tonError={ () => {\n\t\t\t\t\t\tsetIsModalOpen( false );\n\t\t\t\t\t} }\n\t\t\t\t\tonClose={ () => {\n\t\t\t\t\t\tsetIsModalOpen( false );\n\t\t\t\t\t} }\n\t\t\t\t/>\n\t\t\t) }\n\t\t</>\n\t);\n}\n"],"mappings":";AAAA;AACA;AACA;AACA,SAASA,eAAe,EAAEC,eAAe,QAAQ,mBAAmB;AACpE,SAASC,KAAK,IAAIC,gBAAgB,QAAQ,yBAAyB;AACnE,SAASC,QAAQ,QAAQ,oB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IACZ6B,aAAA,CAAClC,kBAAkB;IAClBE,SAAS,EAAGA,SAAW;IACvBoC,SAAS,EAAKV,OAAO,IAAM;MAC1BD,aAAa,CAAEC,OAAQ,CAAC;IACzB,CAAG;IACHW,OAAO,EAAGA,CAAA,KAAM;MACfjC,cAAc,CAAE,KAAM,CAAC;IACxB,CAAG;IACHkC,OAAO,EAAGA,CAAA,KAAM;MACflC,cAAc,CAAE,KAAM,CAAC;IACxB;EAAG,CACH,CAED,CAAC;AAEL"}
1
+ {"version":3,"names":["hasBlockSupport","isReusableBlock","createBlock","serialize","store","blockEditorStore","useState","useCallback","MenuItem","symbol","useSelect","useDispatch","coreStore","__","sprintf","noticesStore","patternsStore","CreatePatternModal","unlock","PatternConvertButton","clientIds","rootClientId","createSuccessNotice","replaceBlocks","setEditingPattern","isModalOpen","setIsModalOpen","canConvert","select","_getBlocksByClientId","canUser","getBlocksByClientId","canInsertBlockType","getBlockRootClientId","rootId","length","undefined","blocks","isReusable","getEntityRecord","attributes","ref","_canConvert","every","block","isValid","name","getContent","handleSuccess","pattern","newBlock","id","clientId","wp_pattern_sync_status","title","raw","type","createElement","Fragment","icon","onClick","content","onSuccess","onError","onClose"],"sources":["@wordpress/patterns/src/components/pattern-convert-button.js"],"sourcesContent":["/**\n * WordPress dependencies\n */\nimport {\n\thasBlockSupport,\n\tisReusableBlock,\n\tcreateBlock,\n\tserialize,\n} from '@wordpress/blocks';\nimport { store as blockEditorStore } from '@wordpress/block-editor';\nimport { useState, useCallback } from '@wordpress/element';\nimport { MenuItem } from '@wordpress/components';\nimport { symbol } from '@wordpress/icons';\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { store as coreStore } from '@wordpress/core-data';\nimport { __, sprintf } from '@wordpress/i18n';\nimport { store as noticesStore } from '@wordpress/notices';\n/**\n * Internal dependencies\n */\nimport { store as patternsStore } from '../store';\nimport CreatePatternModal from './create-pattern-modal';\nimport { unlock } from '../lock-unlock';\n\n/**\n * Menu control to convert block(s) to a pattern block.\n *\n * @param {Object} props Component props.\n * @param {string[]} props.clientIds Client ids of selected blocks.\n * @param {string} props.rootClientId ID of the currently selected top-level block.\n * @return {import('@wordpress/element').WPComponent} The menu control or null.\n */\nexport default function PatternConvertButton( { clientIds, rootClientId } ) {\n\tconst { createSuccessNotice } = useDispatch( noticesStore );\n\tconst { replaceBlocks } = useDispatch( blockEditorStore );\n\t// Ignore reason: false positive of the lint rule.\n\t// eslint-disable-next-line @wordpress/no-unused-vars-before-return\n\tconst { setEditingPattern } = unlock( useDispatch( patternsStore ) );\n\tconst [ isModalOpen, setIsModalOpen ] = useState( false );\n\tconst canConvert = useSelect(\n\t\t( select ) => {\n\t\t\tconst { canUser } = select( coreStore );\n\t\t\tconst {\n\t\t\t\tgetBlocksByClientId,\n\t\t\t\tcanInsertBlockType,\n\t\t\t\tgetBlockRootClientId,\n\t\t\t} = select( blockEditorStore );\n\n\t\t\tconst rootId =\n\t\t\t\trootClientId ||\n\t\t\t\t( clientIds.length > 0\n\t\t\t\t\t? getBlockRootClientId( clientIds[ 0 ] )\n\t\t\t\t\t: undefined );\n\n\t\t\tconst blocks = getBlocksByClientId( clientIds ) ?? [];\n\n\t\t\tconst isReusable =\n\t\t\t\tblocks.length === 1 &&\n\t\t\t\tblocks[ 0 ] &&\n\t\t\t\tisReusableBlock( blocks[ 0 ] ) &&\n\t\t\t\t!! select( coreStore ).getEntityRecord(\n\t\t\t\t\t'postType',\n\t\t\t\t\t'wp_block',\n\t\t\t\t\tblocks[ 0 ].attributes.ref\n\t\t\t\t);\n\n\t\t\tconst _canConvert =\n\t\t\t\t// Hide when this is already a synced pattern.\n\t\t\t\t! isReusable &&\n\t\t\t\t// Hide when patterns are disabled.\n\t\t\t\tcanInsertBlockType( 'core/block', rootId ) &&\n\t\t\t\tblocks.every(\n\t\t\t\t\t( block ) =>\n\t\t\t\t\t\t// Guard against the case where a regular block has *just* been converted.\n\t\t\t\t\t\t!! block &&\n\t\t\t\t\t\t// Hide on invalid blocks.\n\t\t\t\t\t\tblock.isValid &&\n\t\t\t\t\t\t// Hide when block doesn't support being made into a pattern.\n\t\t\t\t\t\thasBlockSupport( block.name, 'reusable', true )\n\t\t\t\t) &&\n\t\t\t\t// Hide when current doesn't have permission to do that.\n\t\t\t\t!! canUser( 'create', 'blocks' );\n\n\t\t\treturn _canConvert;\n\t\t},\n\t\t[ clientIds, rootClientId ]\n\t);\n\tconst { getBlocksByClientId } = useSelect( blockEditorStore );\n\tconst getContent = useCallback(\n\t\t() => serialize( getBlocksByClientId( clientIds ) ),\n\t\t[ getBlocksByClientId, clientIds ]\n\t);\n\n\tif ( ! canConvert ) {\n\t\treturn null;\n\t}\n\n\tconst handleSuccess = ( { pattern } ) => {\n\t\tconst newBlock = createBlock( 'core/block', {\n\t\t\tref: pattern.id,\n\t\t} );\n\n\t\treplaceBlocks( clientIds, newBlock );\n\t\tsetEditingPattern( newBlock.clientId, true );\n\n\t\tcreateSuccessNotice(\n\t\t\tpattern.wp_pattern_sync_status === 'unsynced'\n\t\t\t\t? sprintf(\n\t\t\t\t\t\t// translators: %s: the name the user has given to the pattern.\n\t\t\t\t\t\t__( 'Unsynced Pattern created: %s' ),\n\t\t\t\t\t\tpattern.title.raw\n\t\t\t\t )\n\t\t\t\t: sprintf(\n\t\t\t\t\t\t// translators: %s: the name the user has given to the pattern.\n\t\t\t\t\t\t__( 'Synced Pattern created: %s' ),\n\t\t\t\t\t\tpattern.title.raw\n\t\t\t\t ),\n\t\t\t{\n\t\t\t\ttype: 'snackbar',\n\t\t\t\tid: 'convert-to-pattern-success',\n\t\t\t}\n\t\t);\n\t\tsetIsModalOpen( false );\n\t};\n\treturn (\n\t\t<>\n\t\t\t<MenuItem\n\t\t\t\ticon={ symbol }\n\t\t\t\tonClick={ () => setIsModalOpen( true ) }\n\t\t\t\taria-expanded={ isModalOpen }\n\t\t\t\taria-haspopup=\"dialog\"\n\t\t\t>\n\t\t\t\t{ __( 'Create pattern' ) }\n\t\t\t</MenuItem>\n\t\t\t{ isModalOpen && (\n\t\t\t\t<CreatePatternModal\n\t\t\t\t\tcontent={ getContent }\n\t\t\t\t\tonSuccess={ ( pattern ) => {\n\t\t\t\t\t\thandleSuccess( pattern );\n\t\t\t\t\t} }\n\t\t\t\t\tonError={ () => {\n\t\t\t\t\t\tsetIsModalOpen( false );\n\t\t\t\t\t} }\n\t\t\t\t\tonClose={ () => {\n\t\t\t\t\t\tsetIsModalOpen( false );\n\t\t\t\t\t} }\n\t\t\t\t/>\n\t\t\t) }\n\t\t</>\n\t);\n}\n"],"mappings":";AAAA;AACA;AACA;AACA,SACCA,eAAe,EACfC,eAAe,EACfC,WAAW,EACXC,SAAS,QACH,mBAAmB;AAC1B,SAASC,KAAK,IAAIC,gBAAgB,QAAQ,yBAAyB;AACnE,SAASC,QAAQ,EAAEC,WAAW,QAAQ,oBAAoB;AAC1D,SAASC,QAAQ,QAAQ,uBAAuB;AAChD,SAASC,MAAM,QAAQ,kBAAkB;AACzC,SAASC,SAAS,EAAEC,WAAW,QAAQ,iBAAiB;AACxD,SAASP,KAAK,IAAIQ,SAAS,QAAQ,sBAAsB;AACzD,SAASC,EAAE,EAAEC,OAAO,QAAQ,iBAAiB;AAC7C,SAASV,KAAK,IAAIW,YAAY,QAAQ,oBAAoB;AAC1D;AACA;AACA;AACA,SAASX,KAAK,IAAIY,aAAa,QAAQ,UAAU;AACjD,OAAOC,kBAAkB,MAAM,wBAAwB;AACvD,SAASC,MAAM,QAAQ,gBAAgB;;AAEvC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,oBAAoBA,CAAE;EAAEC,SAAS;EAAEC;AAAa,CAAC,EAAG;EAC3E,MAAM;IAAEC;EAAoB,CAAC,GAAGX,WAAW,CAAEI,YAAa,CAAC;EAC3D,MAAM;IAAEQ;EAAc,CAAC,GAAGZ,WAAW,CAAEN,gBAAiB,CAAC;EACzD;EACA;EACA,MAAM;IAAEmB;EAAkB,CAAC,GAAGN,MAAM,CAAEP,WAAW,C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B4C,OAAO,EAAGd,UAAY;IACtBe,SAAS,EAAKb,OAAO,IAAM;MAC1BD,aAAa,CAAEC,OAAQ,CAAC;IACzB,CAAG;IACHc,OAAO,EAAGA,CAAA,KAAM;MACfrC,cAAc,CAAE,KAAM,CAAC;IACxB,CAAG;IACHsC,OAAO,EAAGA,CAAA,KAAM;MACftC,cAAc,CAAE,KAAM,CAAC;IACxB;EAAG,CACH,CAED,CAAC;AAEL"}
@@ -13,7 +13,8 @@ import { store as coreStore } from '@wordpress/core-data';
13
13
  /**
14
14
  * Internal dependencies
15
15
  */
16
- import { store as editorStore } from '../store';
16
+ import { store as patternsStore } from '../store';
17
+ import { unlock } from '../lock-unlock';
17
18
  function PatternsManageButton({
18
19
  clientId
19
20
  }) {
@@ -48,16 +49,19 @@ function PatternsManageButton({
48
49
  })
49
50
  };
50
51
  }, [clientId]);
52
+
53
+ // Ignore reason: false positive of the lint rule.
54
+ // eslint-disable-next-line @wordpress/no-unused-vars-before-return
51
55
  const {
52
- __experimentalConvertSyncedPatternToStatic: convertBlockToStatic
53
- } = useDispatch(editorStore);
56
+ convertSyncedPatternToStatic
57
+ } = unlock(useDispatch(patternsStore));
54
58
  if (!isVisible) {
55
59
  return null;
56
60
  }
57
61
  return createElement(Fragment, null, createElement(MenuItem, {
58
62
  href: managePatternsUrl
59
63
  }, __('Manage patterns')), canRemove && createElement(MenuItem, {
60
- onClick: () => convertBlockToStatic(clientId)
64
+ onClick: () => convertSyncedPatternToStatic(clientId)
61
65
  }, innerBlockCount > 1 ? __('Detach patterns') : __('Detach pattern')));
62
66
  }
63
67
  export default PatternsManageButton;