@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.
- package/CHANGELOG.md +2 -0
- package/build/components/category-selector.js +105 -0
- package/build/components/category-selector.js.map +1 -0
- package/build/components/create-pattern-modal.js +26 -19
- package/build/components/create-pattern-modal.js.map +1 -1
- package/build/components/pattern-convert-button.js +20 -1
- package/build/components/pattern-convert-button.js.map +1 -1
- package/build/components/patterns-manage-button.js +7 -3
- package/build/components/patterns-manage-button.js.map +1 -1
- package/build/constants.js +21 -0
- package/build/constants.js.map +1 -0
- package/build/index.js +11 -1
- package/build/index.js.map +1 -1
- package/build/private-apis.js +6 -1
- package/build/private-apis.js.map +1 -1
- package/build/store/actions.js +45 -23
- package/build/store/actions.js.map +1 -1
- package/build/store/index.js +4 -3
- package/build/store/index.js.map +1 -1
- package/build/store/selectors.js +2 -2
- package/build/store/selectors.js.map +1 -1
- package/build-module/components/category-selector.js +99 -0
- package/build-module/components/category-selector.js.map +1 -0
- package/build-module/components/create-pattern-modal.js +27 -18
- package/build-module/components/create-pattern-modal.js.map +1 -1
- package/build-module/components/pattern-convert-button.js +22 -3
- package/build-module/components/pattern-convert-button.js.map +1 -1
- package/build-module/components/patterns-manage-button.js +8 -4
- package/build-module/components/patterns-manage-button.js.map +1 -1
- package/build-module/constants.js +11 -0
- package/build-module/constants.js.map +1 -0
- package/build-module/index.js +1 -1
- package/build-module/index.js.map +1 -1
- package/build-module/private-apis.js +6 -1
- package/build-module/private-apis.js.map +1 -1
- package/build-module/store/actions.js +41 -20
- package/build-module/store/actions.js.map +1 -1
- package/build-module/store/index.js +4 -3
- package/build-module/store/index.js.map +1 -1
- package/build-module/store/selectors.js +1 -1
- package/build-module/store/selectors.js.map +1 -1
- package/build-style/style-rtl.css +7 -0
- package/build-style/style.css +7 -0
- package/package.json +16 -14
- package/src/components/category-selector.js +118 -0
- package/src/components/create-pattern-modal.js +45 -38
- package/src/components/pattern-convert-button.js +26 -3
- package/src/components/patterns-manage-button.js +10 -4
- package/src/components/style.scss +7 -0
- package/src/constants.js +16 -0
- package/src/index.js +1 -2
- package/src/private-apis.js +10 -0
- package/src/store/actions.js +55 -27
- package/src/store/index.js +3 -2
- package/src/store/selectors.js +1 -1
package/build/store/actions.js
CHANGED
|
@@ -3,54 +3,76 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.
|
|
7
|
-
exports.
|
|
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
|
|
19
|
-
* @param {'full'|'unsynced'} syncType
|
|
20
|
-
* @param {string
|
|
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
|
|
23
|
-
registry
|
|
24
|
-
dispatch
|
|
28
|
+
const createPattern = (title, syncType, content, categories) => async ({
|
|
29
|
+
registry
|
|
25
30
|
}) => {
|
|
26
|
-
const meta = syncType ===
|
|
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
|
|
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.
|
|
53
|
-
const
|
|
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.
|
|
70
|
-
function
|
|
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","
|
|
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}\n"],"mappings":";;;;;;;AAIA,IAAAA,OAAA,GAAAC,OAAA;AACA,IAAAC,SAAA,GAAAD,OAAA;AACA,IAAAE,YAAA,GAAAF,OAAA;AAKA,IAAAG,UAAA,GAAAH,OAAA;AAXA;AACA;AACA;;AAMA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;A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IF,KAAK,CAAE,2BAA4B,CAAC;EAC/C;EAEA,MAAMG,OAAO,GAAG,MAAMf,QAAQ,CAACf,aAAa,CAC3CuB,aAAa,CAACtB,KAAK,EACnBsB,aAAa,CAACM,UAAU,EACxBN,aAAa,CAACpB,OAAO,EACrBC,UACD,CAAC;EAED,OAAO0B,OAAO;AACf,CAAC;;AAEF;AACA;AACA;AACA;AACA;AAJAZ,OAAA,CAAAC,qBAAA,GAAAA,qBAAA;AAKO,MAAMY,4BAA4B,GACtCC,QAAQ,IACV,CAAE;EAAE3B;AAAS,CAAC,KAAM;EACnB,MAAM4B,QAAQ,GAAG5B,QAAQ,CACvB6B,MAAM,CAAEC,kBAAiB,CAAC,CAC1BC,QAAQ,CAAEJ,QAAS,CAAC;EACtB,MAAMF,OAAO,GAAGzB,QAAQ,CACtB6B,MAAM,CAAE,MAAO,CAAC,CAChBG,qBAAqB,CACrB,UAAU,EACV,UAAU,EACVJ,QAAQ,CAACK,UAAU,CAACC,GACrB,CAAC;EAEF,MAAMC,SAAS,GAAG,IAAAf,aAAK,EACtB,OAAOK,OAAO,CAAC3B,OAAO,KAAK,UAAU,GAClC2B,OAAO,CAAC3B,OAAO,CAAE2B,OAAQ,CAAC,GAC1BA,OAAO,CAAC3B,OACZ,CAAC;EACDE,QAAQ,CACNU,QAAQ,CAAEoB,kBAAiB,CAAC,CAC5BM,aAAa,CAAER,QAAQ,CAACD,QAAQ,EAAEQ,SAAU,CAAC;AAChD,CAAC;;AAEF;AACA;AACA;AACA;AACA;AACA;AACA;AANAtB,OAAA,CAAAa,4BAAA,GAAAA,4BAAA;AAOO,SAASW,iBAAiBA,CAAEV,QAAQ,EAAEW,SAAS,EAAG;EACxD,OAAO;IACNC,IAAI,EAAE,qBAAqB;IAC3BZ,QAAQ;IACRW;EACD,CAAC;AACF"}
|
package/build/store/index.js
CHANGED
|
@@ -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
|
package/build/store/index.js.map
CHANGED
|
@@ -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
|
|
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,gBAAA,OAAAF,OAAA,YAAAF,wBAAA,YAAAA,CAAAC,WAAA,WAAAA,WAAA,GAAAG,gBAAA,GAAAD,iBAAA,KAAAF,WAAA;AAAA,SAAAL,wBAAAS,GAAA,EAAAJ,WAAA,SAAAA,WAAA,IAAAI,GAAA,IAAAA,GAAA,CAAAC,UAAA,WAAAD,GAAA,QAAAA,GAAA,oBAAAA,GAAA,wBAAAA,GAAA,4BAAAE,OAAA,EAAAF,GAAA,UAAAG,KAAA,GAAAR,wBAAA,CAAAC,WAAA,OAAAO,KAAA,IAAAA,KAAA,CAAAC,GAAA,CAAAJ,GAAA,YAAAG,KAAA,CAAAE,GAAA,CAAAL,GAAA,SAAAM,MAAA,WAAAC,qBAAA,GAAAC,MAAA,CAAAC,cAAA,IAAAD,MAAA,CAAAE,wBAAA,WAAAC,GAAA,IAAAX,GAAA,QAAAW,GAAA,kBAAAH,MAAA,CAAAI,SAAA,CAAAC,cAAA,CAAAC,IAAA,CAAAd,GAAA,EAAAW,GAAA,SAAAI,IAAA,GAAAR,qBAAA,GAAAC,MAAA,CAAAE,wBAAA,CAAAV,GAAA,EAAAW,GAAA,cAAAI,IAAA,KAAAA,IAAA,CAAAV,GAAA,IAAAU,IAAA,CAAAC,GAAA,KAAAR,MAAA,CAAAC,cAAA,CAAAH,MAAA,EAAAK,GAAA,EAAAI,IAAA,YAAAT,MAAA,CAAAK,GAAA,IAAAX,GAAA,CAAAW,GAAA,SAAAL,MAAA,CAAAJ,OAAA,GAAAF,GAAA,MAAAG,KAAA,IAAAA,KAAA,CAAAa,GAAA,CAAAhB,GAAA,EAAAM,MAAA,YAAAA,MAAA;AAZxC;AACA;AACA;;AAGA;AACA;AACA;;AAOA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMW,WAAW,GAAG;EAC1BC,OAAO,EAAPA;AACD,CAAC;;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AANAC,OAAA,CAAAF,WAAA,GAAAA,WAAA;AAOO,MAAMG,KAAK,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"}
|
package/build/store/selectors.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.
|
|
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
|
|
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":["
|
|
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,EAAE,QAAQ,iBAAiB;AACpC,SAASC,OAAO,EAAEC,QAAQ,QAAQ,oBAAoB;AACtD,SAASC,cAAc,QAAQ,uBAAuB;AACtD,SAASC,SAAS,EAAEC,WAAW,QAAQ,iBAAiB;AACxD,SAASC,KAAK,IAAIC,SAAS,QAAQ,sBAAsB;AACzD,S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tB,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
|
|
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 {
|
|
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
|
-
|
|
25
|
+
content,
|
|
24
26
|
onClose,
|
|
25
27
|
className = 'patterns-menu-items__convert-modal'
|
|
26
28
|
}) {
|
|
27
|
-
const [syncType, setSyncType] = useState(
|
|
29
|
+
const [syncType, setSyncType] = useState(PATTERN_SYNC_TYPES.full);
|
|
30
|
+
const [categories, setCategories] = useState([]);
|
|
28
31
|
const [title, setTitle] = useState('');
|
|
29
32
|
const {
|
|
30
|
-
|
|
31
|
-
} = useDispatch(
|
|
33
|
+
createPattern
|
|
34
|
+
} = unlock(useDispatch(patternsStore));
|
|
32
35
|
const {
|
|
33
36
|
createErrorNotice
|
|
34
37
|
} = useDispatch(noticesStore);
|
|
35
|
-
|
|
38
|
+
async function onCreate(patternTitle, sync) {
|
|
36
39
|
try {
|
|
37
|
-
const newPattern = await createPattern(patternTitle, sync,
|
|
40
|
+
const newPattern = await createPattern(patternTitle, sync, typeof content === 'function' ? content() : content, categories);
|
|
38
41
|
onSuccess({
|
|
39
42
|
pattern: newPattern,
|
|
40
|
-
categoryId:
|
|
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
|
-
}
|
|
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:
|
|
83
|
+
checked: syncType === PATTERN_SYNC_TYPES.full,
|
|
75
84
|
onChange: () => {
|
|
76
|
-
setSyncType(syncType ===
|
|
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","
|
|
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>\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A,CAACxC,aAAa;IACbgD,KAAK,EAAG/C,EAAE,CAAE,QAAS,CAAG;IACxBoD,IAAI,EAAGpD,EAAE,CACR,yDACD,CAAG;IACHqD,OAAO,EAAGrC,QAAQ,KAAKV,kBAAkB,CAACY,IAAM;IAChD+B,QAAQ,EAAGA,CAAA,KAAM;MAChBhC,WAAW,CACVD,QAAQ,KAAKV,kBAAkB,CAACY,IAAI,GACjCZ,kBAAkB,CAACgD,QAAQ,GAC3BhD,kBAAkB,CAACY,IACvB,CAAC;IACF;EAAG,CACH,CAAC,EACFqB,aAAA,CAAC3C,MAAM;IAAC2D,OAAO,EAAC;EAAO,GACtBhB,aAAA,CAAC9C,MAAM;IACN+D,OAAO,EAAC,UAAU;IAClBC,OAAO,EAAGA,CAAA,KAAM;MACf3C,OAAO,CAAC,CAAC;MACTQ,QAAQ,CAAE,EAAG,CAAC;IACf;EAAG,GAEDtB,EAAE,CAAE,QAAS,CACR,CAAC,EAETuC,aAAA,CAAC9C,MAAM;IAAC+D,OAAO,EAAC,SAAS;IAACvB,IAAI,EAAC;EAAQ,GACpCjC,EAAE,CAAE,QAAS,CACR,CACD,CACD,CACH,CACA,CAAC;AAEV"}
|
|
@@ -2,9 +2,9 @@ import { createElement, Fragment } from "@wordpress/element";
|
|
|
2
2
|
/**
|
|
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
|
-
|
|
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","
|
|
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,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C,GACd,CAAC,EACJ;MACCC,IAAI,EAAE,UAAU;MAChBL,EAAE,EAAE;IACL,CACD,CAAC;IACDzB,cAAc,CAAE,KAAM,CAAC;EACxB,CAAC;EACD,OACC+B,aAAA,CAAAC,QAAA,QACCD,aAAA,CAACjD,QAAQ;IACRmD,IAAI,EAAGlD,MAAQ;IACfmD,OAAO,EAAGA,CAAA,KAAMlC,cAAc,CAAE,IAAK,CAAG;IACxC,iBAAgBD,WAAa;IAC7B,iBAAc;EAAQ,GAEpBZ,EAAE,CAAE,gBAAiB,CACd,CAAC,EACTY,WAAW,IACZgC,aAAA,CAACxC,kBAAkB;IAClB4C,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
|
|
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
|
-
|
|
53
|
-
} = useDispatch(
|
|
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: () =>
|
|
64
|
+
onClick: () => convertSyncedPatternToStatic(clientId)
|
|
61
65
|
}, innerBlockCount > 1 ? __('Detach patterns') : __('Detach pattern')));
|
|
62
66
|
}
|
|
63
67
|
export default PatternsManageButton;
|