@wordpress/patterns 1.3.0 → 1.3.2
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/build/components/category-selector.js +9 -41
- package/build/components/category-selector.js.map +1 -1
- package/build/components/create-pattern-modal.js +45 -9
- package/build/components/create-pattern-modal.js.map +1 -1
- package/build/components/pattern-convert-button.js +11 -8
- package/build/components/pattern-convert-button.js.map +1 -1
- package/build/constants.js +3 -1
- package/build/constants.js.map +1 -1
- package/build/private-apis.js +1 -0
- package/build/private-apis.js.map +1 -1
- package/build/store/actions.js +1 -1
- package/build/store/actions.js.map +1 -1
- package/build-module/components/category-selector.js +8 -42
- package/build-module/components/category-selector.js.map +1 -1
- package/build-module/components/create-pattern-modal.js +43 -8
- package/build-module/components/create-pattern-modal.js.map +1 -1
- package/build-module/components/pattern-convert-button.js +11 -8
- package/build-module/components/pattern-convert-button.js.map +1 -1
- package/build-module/constants.js +1 -0
- package/build-module/constants.js.map +1 -1
- package/build-module/private-apis.js +2 -1
- package/build-module/private-apis.js.map +1 -1
- package/build-module/store/actions.js +1 -1
- package/build-module/store/actions.js.map +1 -1
- package/package.json +15 -15
- package/src/components/category-selector.js +7 -45
- package/src/components/create-pattern-modal.js +51 -9
- package/src/components/pattern-convert-button.js +11 -8
- package/src/constants.js +1 -0
- package/src/private-apis.js +2 -0
- package/src/store/actions.js +1 -1
|
@@ -7,6 +7,7 @@ import { __ } from '@wordpress/i18n';
|
|
|
7
7
|
import { useState } from '@wordpress/element';
|
|
8
8
|
import { useDispatch } from '@wordpress/data';
|
|
9
9
|
import { store as noticesStore } from '@wordpress/notices';
|
|
10
|
+
import { store as coreStore } from '@wordpress/core-data';
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* Internal dependencies
|
|
@@ -17,7 +18,7 @@ import { PATTERN_DEFAULT_CATEGORY, PATTERN_SYNC_TYPES } from '../constants';
|
|
|
17
18
|
* Internal dependencies
|
|
18
19
|
*/
|
|
19
20
|
import { store as patternsStore } from '../store';
|
|
20
|
-
import CategorySelector from './category-selector';
|
|
21
|
+
import CategorySelector, { CATEGORY_SLUG } from './category-selector';
|
|
21
22
|
import { unlock } from '../lock-unlock';
|
|
22
23
|
export default function CreatePatternModal({
|
|
23
24
|
onSuccess,
|
|
@@ -27,16 +28,26 @@ export default function CreatePatternModal({
|
|
|
27
28
|
className = 'patterns-menu-items__convert-modal'
|
|
28
29
|
}) {
|
|
29
30
|
const [syncType, setSyncType] = useState(PATTERN_SYNC_TYPES.full);
|
|
30
|
-
const [
|
|
31
|
+
const [categoryTerms, setCategoryTerms] = useState([]);
|
|
31
32
|
const [title, setTitle] = useState('');
|
|
33
|
+
const [isSaving, setIsSaving] = useState(false);
|
|
32
34
|
const {
|
|
33
35
|
createPattern
|
|
34
36
|
} = unlock(useDispatch(patternsStore));
|
|
37
|
+
const {
|
|
38
|
+
saveEntityRecord,
|
|
39
|
+
invalidateResolution
|
|
40
|
+
} = useDispatch(coreStore);
|
|
35
41
|
const {
|
|
36
42
|
createErrorNotice
|
|
37
43
|
} = useDispatch(noticesStore);
|
|
38
44
|
async function onCreate(patternTitle, sync) {
|
|
45
|
+
if (!title || isSaving) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
39
48
|
try {
|
|
49
|
+
setIsSaving(true);
|
|
50
|
+
const categories = await Promise.all(categoryTerms.map(termName => findOrCreateTerm(termName)));
|
|
40
51
|
const newPattern = await createPattern(patternTitle, sync, typeof content === 'function' ? content() : content, categories);
|
|
41
52
|
onSuccess({
|
|
42
53
|
pattern: newPattern,
|
|
@@ -48,11 +59,33 @@ export default function CreatePatternModal({
|
|
|
48
59
|
id: 'convert-to-pattern-error'
|
|
49
60
|
});
|
|
50
61
|
onError();
|
|
62
|
+
} finally {
|
|
63
|
+
setIsSaving(false);
|
|
64
|
+
setCategoryTerms([]);
|
|
65
|
+
setTitle('');
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param {string} term
|
|
71
|
+
* @return {Promise<number>} The pattern category id.
|
|
72
|
+
*/
|
|
73
|
+
async function findOrCreateTerm(term) {
|
|
74
|
+
try {
|
|
75
|
+
const newTerm = await saveEntityRecord('taxonomy', CATEGORY_SLUG, {
|
|
76
|
+
name: term
|
|
77
|
+
}, {
|
|
78
|
+
throwOnError: true
|
|
79
|
+
});
|
|
80
|
+
invalidateResolution('getUserPatternCategories');
|
|
81
|
+
return newTerm.id;
|
|
82
|
+
} catch (error) {
|
|
83
|
+
if (error.code !== 'term_exists') {
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
return error.data.term_id;
|
|
51
87
|
}
|
|
52
88
|
}
|
|
53
|
-
const handleCategorySelection = selectedCategories => {
|
|
54
|
-
setCategories(selectedCategories.map(cat => cat.id));
|
|
55
|
-
};
|
|
56
89
|
return createElement(Modal, {
|
|
57
90
|
title: __('Create pattern'),
|
|
58
91
|
onRequestClose: () => {
|
|
@@ -64,7 +97,6 @@ export default function CreatePatternModal({
|
|
|
64
97
|
onSubmit: event => {
|
|
65
98
|
event.preventDefault();
|
|
66
99
|
onCreate(title, syncType);
|
|
67
|
-
setTitle('');
|
|
68
100
|
}
|
|
69
101
|
}, createElement(VStack, {
|
|
70
102
|
spacing: "5"
|
|
@@ -76,7 +108,8 @@ export default function CreatePatternModal({
|
|
|
76
108
|
placeholder: __('My pattern'),
|
|
77
109
|
className: "patterns-create-modal__name-input"
|
|
78
110
|
}), createElement(CategorySelector, {
|
|
79
|
-
|
|
111
|
+
values: categoryTerms,
|
|
112
|
+
onChange: setCategoryTerms
|
|
80
113
|
}), createElement(ToggleControl, {
|
|
81
114
|
label: __('Synced'),
|
|
82
115
|
help: __('Editing the pattern will update it anywhere it is used.'),
|
|
@@ -94,7 +127,9 @@ export default function CreatePatternModal({
|
|
|
94
127
|
}
|
|
95
128
|
}, __('Cancel')), createElement(Button, {
|
|
96
129
|
variant: "primary",
|
|
97
|
-
type: "submit"
|
|
130
|
+
type: "submit",
|
|
131
|
+
"aria-disabled": !title || isSaving,
|
|
132
|
+
isBusy: isSaving
|
|
98
133
|
}, __('Create'))))));
|
|
99
134
|
}
|
|
100
135
|
//# sourceMappingURL=create-pattern-modal.js.map
|
|
@@ -1 +1 @@
|
|
|
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","
|
|
1
|
+
{"version":3,"names":["Modal","Button","TextControl","__experimentalHStack","HStack","__experimentalVStack","VStack","ToggleControl","__","useState","useDispatch","store","noticesStore","coreStore","PATTERN_DEFAULT_CATEGORY","PATTERN_SYNC_TYPES","patternsStore","CategorySelector","CATEGORY_SLUG","unlock","CreatePatternModal","onSuccess","onError","content","onClose","className","syncType","setSyncType","full","categoryTerms","setCategoryTerms","title","setTitle","isSaving","setIsSaving","createPattern","saveEntityRecord","invalidateResolution","createErrorNotice","onCreate","patternTitle","sync","categories","Promise","all","map","termName","findOrCreateTerm","newPattern","pattern","categoryId","error","message","type","id","term","newTerm","name","throwOnError","code","data","term_id","createElement","onRequestClose","overlayClassName","onSubmit","event","preventDefault","spacing","__nextHasNoMarginBottom","label","value","onChange","placeholder","values","help","checked","unsynced","justify","variant","onClick","isBusy"],"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';\nimport { store as coreStore } from '@wordpress/core-data';\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, { CATEGORY_SLUG } 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 [ categoryTerms, setCategoryTerms ] = useState( [] );\n\tconst [ title, setTitle ] = useState( '' );\n\tconst [ isSaving, setIsSaving ] = useState( false );\n\tconst { createPattern } = unlock( useDispatch( patternsStore ) );\n\tconst { saveEntityRecord, invalidateResolution } = useDispatch( coreStore );\n\tconst { createErrorNotice } = useDispatch( noticesStore );\n\n\tasync function onCreate( patternTitle, sync ) {\n\t\tif ( ! title || isSaving ) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tsetIsSaving( true );\n\t\t\tconst categories = await Promise.all(\n\t\t\t\tcategoryTerms.map( ( termName ) =>\n\t\t\t\t\tfindOrCreateTerm( termName )\n\t\t\t\t)\n\t\t\t);\n\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} finally {\n\t\t\tsetIsSaving( false );\n\t\t\tsetCategoryTerms( [] );\n\t\t\tsetTitle( '' );\n\t\t}\n\t}\n\n\t/**\n\t * @param {string} term\n\t * @return {Promise<number>} The pattern category id.\n\t */\n\tasync function findOrCreateTerm( term ) {\n\t\ttry {\n\t\t\tconst newTerm = await saveEntityRecord(\n\t\t\t\t'taxonomy',\n\t\t\t\tCATEGORY_SLUG,\n\t\t\t\t{ name: term },\n\t\t\t\t{ throwOnError: true }\n\t\t\t);\n\t\t\tinvalidateResolution( 'getUserPatternCategories' );\n\t\t\treturn newTerm.id;\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 error.data.term_id;\n\t\t}\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} }\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\tvalues={ categoryTerms }\n\t\t\t\t\t\tonChange={ setCategoryTerms }\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\n\t\t\t\t\t\t\tvariant=\"primary\"\n\t\t\t\t\t\t\ttype=\"submit\"\n\t\t\t\t\t\t\taria-disabled={ ! title || isSaving }\n\t\t\t\t\t\t\tisBusy={ isSaving }\n\t\t\t\t\t\t>\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;AAC1D,SAASD,KAAK,IAAIE,SAAS,QAAQ,sBAAsB;;AAEzD;AACA;AACA;AACA,SAASC,wBAAwB,EAAEC,kBAAkB,QAAQ,cAAc;;AAE3E;AACA;AACA;AACA,SAASJ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Store } from '@wordpress/notices';
|
|
|
17
17
|
import { store as patternsStore } from '../store';
|
|
18
18
|
import CreatePatternModal from './create-pattern-modal';
|
|
19
19
|
import { unlock } from '../lock-unlock';
|
|
20
|
+
import { PATTERN_SYNC_TYPES } from '../constants';
|
|
20
21
|
|
|
21
22
|
/**
|
|
22
23
|
* Menu control to convert block(s) to a pattern block.
|
|
@@ -80,16 +81,18 @@ export default function PatternConvertButton({
|
|
|
80
81
|
const handleSuccess = ({
|
|
81
82
|
pattern
|
|
82
83
|
}) => {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
84
|
+
if (pattern.wp_pattern_sync_status !== PATTERN_SYNC_TYPES.unsynced) {
|
|
85
|
+
const newBlock = createBlock('core/block', {
|
|
86
|
+
ref: pattern.id
|
|
87
|
+
});
|
|
88
|
+
replaceBlocks(clientIds, newBlock);
|
|
89
|
+
setEditingPattern(newBlock.clientId, true);
|
|
90
|
+
}
|
|
91
|
+
createSuccessNotice(pattern.wp_pattern_sync_status === PATTERN_SYNC_TYPES.unsynced ? sprintf(
|
|
89
92
|
// translators: %s: the name the user has given to the pattern.
|
|
90
|
-
__('Unsynced
|
|
93
|
+
__('Unsynced pattern created: %s'), pattern.title.raw) : sprintf(
|
|
91
94
|
// translators: %s: the name the user has given to the pattern.
|
|
92
|
-
__('Synced
|
|
95
|
+
__('Synced pattern created: %s'), pattern.title.raw), {
|
|
93
96
|
type: 'snackbar',
|
|
94
97
|
id: 'convert-to-pattern-success'
|
|
95
98
|
});
|
|
@@ -1 +1 @@
|
|
|
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","
|
|
1
|
+
{"version":3,"names":["hasBlockSupport","isReusableBlock","createBlock","serialize","store","blockEditorStore","useState","useCallback","MenuItem","symbol","useSelect","useDispatch","coreStore","__","sprintf","noticesStore","patternsStore","CreatePatternModal","unlock","PATTERN_SYNC_TYPES","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","wp_pattern_sync_status","unsynced","newBlock","id","clientId","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';\nimport { PATTERN_SYNC_TYPES } from '../constants';\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\tif ( pattern.wp_pattern_sync_status !== PATTERN_SYNC_TYPES.unsynced ) {\n\t\t\tconst newBlock = createBlock( 'core/block', {\n\t\t\t\tref: pattern.id,\n\t\t\t} );\n\n\t\t\treplaceBlocks( clientIds, newBlock );\n\t\t\tsetEditingPattern( newBlock.clientId, true );\n\t\t}\n\n\t\tcreateSuccessNotice(\n\t\t\tpattern.wp_pattern_sync_status === PATTERN_SYNC_TYPES.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;AACvC,SAASC,kBAAkB,QAAQ,cAAc;;AAEjD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,oBAAoBA,CAAE;EAAEC,SAAS;EAAEC;AAAa,CAAC,EAAG;EAC3E,MAAM;IAAEC;EAAoB,CAAC,GAAGZ,WAAW,CAAEI,YAAa,CAAC;EAC3D,MAAM;IAAES;EAAc,CAAC,GAAGb,WAAW,CAAEN,gBAAiB,CAAC;EACzD;EACA;EACA,MAAM;IAAEoB;EAAkB,CAAC,GAAGP,MAAM,C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Aa;IAC7B,iBAAc;EAAQ,GAEpBb,EAAE,CAAE,gBAAiB,CACd,CAAC,EACTa,WAAW,IACZiC,aAAA,CAAC1C,kBAAkB;IAClB8C,OAAO,EAAGf,UAAY;IACtBgB,SAAS,EAAKd,OAAO,IAAM;MAC1BD,aAAa,CAAEC,OAAQ,CAAC;IACzB,CAAG;IACHe,OAAO,EAAGA,CAAA,KAAM;MACftC,cAAc,CAAE,KAAM,CAAC;IACxB,CAAG;IACHuC,OAAO,EAAGA,CAAA,KAAM;MACfvC,cAAc,CAAE,KAAM,CAAC;IACxB;EAAG,CACH,CAED,CAAC;AAEL"}
|
|
@@ -3,6 +3,7 @@ export const PATTERN_TYPES = {
|
|
|
3
3
|
user: 'wp_block'
|
|
4
4
|
};
|
|
5
5
|
export const PATTERN_DEFAULT_CATEGORY = 'all-patterns';
|
|
6
|
+
export const PATTERN_USER_CATEGORY = 'my-patterns';
|
|
6
7
|
export const PATTERN_CORE_SOURCES = ['core', 'pattern-directory/core', 'pattern-directory/featured', 'pattern-directory/theme'];
|
|
7
8
|
export const PATTERN_SYNC_TYPES = {
|
|
8
9
|
full: 'fully',
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["PATTERN_TYPES","theme","user","PATTERN_DEFAULT_CATEGORY","PATTERN_CORE_SOURCES","PATTERN_SYNC_TYPES","full","unsynced"],"sources":["@wordpress/patterns/src/constants.js"],"sourcesContent":["export const PATTERN_TYPES = {\n\ttheme: 'pattern',\n\tuser: 'wp_block',\n};\n\nexport const PATTERN_DEFAULT_CATEGORY = 'all-patterns';\nexport const PATTERN_CORE_SOURCES = [\n\t'core',\n\t'pattern-directory/core',\n\t'pattern-directory/featured',\n\t'pattern-directory/theme',\n];\nexport const PATTERN_SYNC_TYPES = {\n\tfull: 'fully',\n\tunsynced: 'unsynced',\n};\n"],"mappings":"AAAA,OAAO,MAAMA,aAAa,GAAG;EAC5BC,KAAK,EAAE,SAAS;EAChBC,IAAI,EAAE;AACP,CAAC;AAED,OAAO,MAAMC,wBAAwB,GAAG,cAAc;AACtD,OAAO,MAAMC,oBAAoB,GAAG,CACnC,MAAM,EACN,wBAAwB,EACxB,4BAA4B,EAC5B,yBAAyB,CACzB;AACD,OAAO,MAAMC,kBAAkB,GAAG;EACjCC,IAAI,EAAE,OAAO;EACbC,QAAQ,EAAE;AACX,CAAC"}
|
|
1
|
+
{"version":3,"names":["PATTERN_TYPES","theme","user","PATTERN_DEFAULT_CATEGORY","PATTERN_USER_CATEGORY","PATTERN_CORE_SOURCES","PATTERN_SYNC_TYPES","full","unsynced"],"sources":["@wordpress/patterns/src/constants.js"],"sourcesContent":["export const PATTERN_TYPES = {\n\ttheme: 'pattern',\n\tuser: 'wp_block',\n};\n\nexport const PATTERN_DEFAULT_CATEGORY = 'all-patterns';\nexport const PATTERN_USER_CATEGORY = 'my-patterns';\nexport const PATTERN_CORE_SOURCES = [\n\t'core',\n\t'pattern-directory/core',\n\t'pattern-directory/featured',\n\t'pattern-directory/theme',\n];\nexport const PATTERN_SYNC_TYPES = {\n\tfull: 'fully',\n\tunsynced: 'unsynced',\n};\n"],"mappings":"AAAA,OAAO,MAAMA,aAAa,GAAG;EAC5BC,KAAK,EAAE,SAAS;EAChBC,IAAI,EAAE;AACP,CAAC;AAED,OAAO,MAAMC,wBAAwB,GAAG,cAAc;AACtD,OAAO,MAAMC,qBAAqB,GAAG,aAAa;AAClD,OAAO,MAAMC,oBAAoB,GAAG,CACnC,MAAM,EACN,wBAAwB,EACxB,4BAA4B,EAC5B,yBAAyB,CACzB;AACD,OAAO,MAAMC,kBAAkB,GAAG;EACjCC,IAAI,EAAE,OAAO;EACbC,QAAQ,EAAE;AACX,CAAC"}
|
|
@@ -4,13 +4,14 @@
|
|
|
4
4
|
import { lock } from './lock-unlock';
|
|
5
5
|
import CreatePatternModal from './components/create-pattern-modal';
|
|
6
6
|
import PatternsMenuItems from './components';
|
|
7
|
-
import { PATTERN_TYPES, PATTERN_DEFAULT_CATEGORY, PATTERN_CORE_SOURCES, PATTERN_SYNC_TYPES } from './constants';
|
|
7
|
+
import { PATTERN_TYPES, PATTERN_DEFAULT_CATEGORY, PATTERN_USER_CATEGORY, PATTERN_CORE_SOURCES, PATTERN_SYNC_TYPES } from './constants';
|
|
8
8
|
export const privateApis = {};
|
|
9
9
|
lock(privateApis, {
|
|
10
10
|
CreatePatternModal,
|
|
11
11
|
PatternsMenuItems,
|
|
12
12
|
PATTERN_TYPES,
|
|
13
13
|
PATTERN_DEFAULT_CATEGORY,
|
|
14
|
+
PATTERN_USER_CATEGORY,
|
|
14
15
|
PATTERN_CORE_SOURCES,
|
|
15
16
|
PATTERN_SYNC_TYPES
|
|
16
17
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["lock","CreatePatternModal","PatternsMenuItems","PATTERN_TYPES","PATTERN_DEFAULT_CATEGORY","PATTERN_CORE_SOURCES","PATTERN_SYNC_TYPES","privateApis"],"sources":["@wordpress/patterns/src/private-apis.js"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport { lock } from './lock-unlock';\nimport CreatePatternModal from './components/create-pattern-modal';\nimport PatternsMenuItems from './components';\nimport {\n\tPATTERN_TYPES,\n\tPATTERN_DEFAULT_CATEGORY,\n\tPATTERN_CORE_SOURCES,\n\tPATTERN_SYNC_TYPES,\n} from './constants';\n\nexport const privateApis = {};\nlock( privateApis, {\n\tCreatePatternModal,\n\tPatternsMenuItems,\n\tPATTERN_TYPES,\n\tPATTERN_DEFAULT_CATEGORY,\n\tPATTERN_CORE_SOURCES,\n\tPATTERN_SYNC_TYPES,\n} );\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,IAAI,QAAQ,eAAe;AACpC,OAAOC,kBAAkB,MAAM,mCAAmC;AAClE,OAAOC,iBAAiB,MAAM,cAAc;AAC5C,SACCC,aAAa,EACbC,wBAAwB,EACxBC,oBAAoB,EACpBC,kBAAkB,QACZ,aAAa;AAEpB,OAAO,MAAMC,WAAW,GAAG,CAAC,CAAC;
|
|
1
|
+
{"version":3,"names":["lock","CreatePatternModal","PatternsMenuItems","PATTERN_TYPES","PATTERN_DEFAULT_CATEGORY","PATTERN_USER_CATEGORY","PATTERN_CORE_SOURCES","PATTERN_SYNC_TYPES","privateApis"],"sources":["@wordpress/patterns/src/private-apis.js"],"sourcesContent":["/**\n * Internal dependencies\n */\nimport { lock } from './lock-unlock';\nimport CreatePatternModal from './components/create-pattern-modal';\nimport PatternsMenuItems from './components';\nimport {\n\tPATTERN_TYPES,\n\tPATTERN_DEFAULT_CATEGORY,\n\tPATTERN_USER_CATEGORY,\n\tPATTERN_CORE_SOURCES,\n\tPATTERN_SYNC_TYPES,\n} from './constants';\n\nexport const privateApis = {};\nlock( privateApis, {\n\tCreatePatternModal,\n\tPatternsMenuItems,\n\tPATTERN_TYPES,\n\tPATTERN_DEFAULT_CATEGORY,\n\tPATTERN_USER_CATEGORY,\n\tPATTERN_CORE_SOURCES,\n\tPATTERN_SYNC_TYPES,\n} );\n"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,IAAI,QAAQ,eAAe;AACpC,OAAOC,kBAAkB,MAAM,mCAAmC;AAClE,OAAOC,iBAAiB,MAAM,cAAc;AAC5C,SACCC,aAAa,EACbC,wBAAwB,EACxBC,qBAAqB,EACrBC,oBAAoB,EACpBC,kBAAkB,QACZ,aAAa;AAEpB,OAAO,MAAMC,WAAW,GAAG,CAAC,CAAC;AAC7BR,IAAI,CAAEQ,WAAW,EAAE;EAClBP,kBAAkB;EAClBC,iBAAiB;EACjBC,aAAa;EACbC,wBAAwB;EACxBC,qBAAqB;EACrBC,oBAAoB;EACpBC;AACD,CAAE,CAAC"}
|
|
@@ -53,7 +53,7 @@ export const createPatternFromFile = (file, categories) => async ({
|
|
|
53
53
|
throw new Error('Invalid JSON file');
|
|
54
54
|
}
|
|
55
55
|
if (parsedContent.__file !== 'wp_block' || !parsedContent.title || !parsedContent.content || typeof parsedContent.title !== 'string' || typeof parsedContent.content !== 'string' || parsedContent.syncStatus && typeof parsedContent.syncStatus !== 'string') {
|
|
56
|
-
throw new Error('Invalid
|
|
56
|
+
throw new Error('Invalid pattern JSON file');
|
|
57
57
|
}
|
|
58
58
|
const pattern = await dispatch.createPattern(parsedContent.title, parsedContent.syncStatus, parsedContent.content, categories);
|
|
59
59
|
return pattern;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["parse","store","coreStore","blockEditorStore","PATTERN_SYNC_TYPES","createPattern","title","syncType","content","categories","registry","meta","unsynced","wp_pattern_sync_status","undefined","reusableBlock","status","wp_pattern_category","updatedRecord","dispatch","saveEntityRecord","createPatternFromFile","file","fileContent","text","parsedContent","JSON","e","Error","__file","syncStatus","pattern","convertSyncedPatternToStatic","clientId","oldBlock","select","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
|
|
1
|
+
{"version":3,"names":["parse","store","coreStore","blockEditorStore","PATTERN_SYNC_TYPES","createPattern","title","syncType","content","categories","registry","meta","unsynced","wp_pattern_sync_status","undefined","reusableBlock","status","wp_pattern_category","updatedRecord","dispatch","saveEntityRecord","createPatternFromFile","file","fileContent","text","parsedContent","JSON","e","Error","__file","syncStatus","pattern","convertSyncedPatternToStatic","clientId","oldBlock","select","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":"AAAA;AACA;AACA;;AAEA,SAASA,KAAK,QAAQ,mBAAmB;AACzC,SAASC,KAAK,IAAIC,SAAS,QAAQ,sBAAsB;AACzD,SAASD,KAAK,IAAIE,gBAAgB,QAAQ,yBAAyB;;AAEnE;AACA;AACA;AACA,SAASC,kBAAkB,QAAQ,cAAc;;AAEjD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,aAAa,GACzBA,CAAEC,KAAK,EAAEC,QAAQ,EAAEC,OAAO,EAAEC,UAAU,KACtC,OAAQ;EAAEC;AAAS,CAAC,KAAM;EACzB,MAAMC,IAAI,GACTJ,QAAQ,KAAKH,kBA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O;AACf,CAAC;;AAEF;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,4BAA4B,GACtCC,QAAQ,IACV,CAAE;EAAEvB;AAAS,CAAC,KAAM;EACnB,MAAMwB,QAAQ,GAAGxB,QAAQ,CACvByB,MAAM,CAAEhC,gBAAiB,CAAC,CAC1BiC,QAAQ,CAAEH,QAAS,CAAC;EACtB,MAAMF,OAAO,GAAGrB,QAAQ,CACtByB,MAAM,CAAE,MAAO,CAAC,CAChBE,qBAAqB,CACrB,UAAU,EACV,UAAU,EACVH,QAAQ,CAACI,UAAU,CAACC,GACrB,CAAC;EAEF,MAAMC,SAAS,GAAGxC,KAAK,CACtB,OAAO+B,OAAO,CAACvB,OAAO,KAAK,UAAU,GAClCuB,OAAO,CAACvB,OAAO,CAAEuB,OAAQ,CAAC,GAC1BA,OAAO,CAACvB,OACZ,CAAC;EACDE,QAAQ,CACNS,QAAQ,CAAEhB,gBAAiB,CAAC,CAC5BsC,aAAa,CAAEP,QAAQ,CAACD,QAAQ,EAAEO,SAAU,CAAC;AAChD,CAAC;;AAEF;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASE,iBAAiBA,CAAET,QAAQ,EAAEU,SAAS,EAAG;EACxD,OAAO;IACNC,IAAI,EAAE,qBAAqB;IAC3BX,QAAQ;IACRU;EACD,CAAC;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/patterns",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.2",
|
|
4
4
|
"description": "Management of user pattern editing.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -32,19 +32,19 @@
|
|
|
32
32
|
],
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@babel/runtime": "^7.16.0",
|
|
35
|
-
"@wordpress/block-editor": "^12.10.
|
|
36
|
-
"@wordpress/blocks": "^12.19.
|
|
37
|
-
"@wordpress/components": "^25.8.
|
|
38
|
-
"@wordpress/compose": "^6.19.
|
|
39
|
-
"@wordpress/core-data": "^6.19.
|
|
40
|
-
"@wordpress/data": "^9.12.
|
|
41
|
-
"@wordpress/element": "^5.19.
|
|
42
|
-
"@wordpress/html-entities": "^3.42.
|
|
43
|
-
"@wordpress/i18n": "^4.42.
|
|
44
|
-
"@wordpress/icons": "^9.33.
|
|
45
|
-
"@wordpress/notices": "^4.10.
|
|
46
|
-
"@wordpress/private-apis": "^0.24.
|
|
47
|
-
"@wordpress/url": "^3.43.
|
|
35
|
+
"@wordpress/block-editor": "^12.10.2",
|
|
36
|
+
"@wordpress/blocks": "^12.19.2",
|
|
37
|
+
"@wordpress/components": "^25.8.2",
|
|
38
|
+
"@wordpress/compose": "^6.19.2",
|
|
39
|
+
"@wordpress/core-data": "^6.19.2",
|
|
40
|
+
"@wordpress/data": "^9.12.2",
|
|
41
|
+
"@wordpress/element": "^5.19.2",
|
|
42
|
+
"@wordpress/html-entities": "^3.42.2",
|
|
43
|
+
"@wordpress/i18n": "^4.42.2",
|
|
44
|
+
"@wordpress/icons": "^9.33.2",
|
|
45
|
+
"@wordpress/notices": "^4.10.2",
|
|
46
|
+
"@wordpress/private-apis": "^0.24.2",
|
|
47
|
+
"@wordpress/url": "^3.43.2"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
50
|
"react": "^18.0.0",
|
|
@@ -53,5 +53,5 @@
|
|
|
53
53
|
"publishConfig": {
|
|
54
54
|
"access": "public"
|
|
55
55
|
},
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "5d2e3d07cc97af8090fc32c1e5d5013a2967e752"
|
|
57
57
|
}
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import { __ } from '@wordpress/i18n';
|
|
5
5
|
import { useMemo, useState } from '@wordpress/element';
|
|
6
6
|
import { FormTokenField } from '@wordpress/components';
|
|
7
|
-
import { useSelect
|
|
7
|
+
import { useSelect } from '@wordpress/data';
|
|
8
8
|
import { store as coreStore } from '@wordpress/core-data';
|
|
9
9
|
import { useDebounce } from '@wordpress/compose';
|
|
10
10
|
import { decodeEntities } from '@wordpress/html-entities';
|
|
@@ -13,13 +13,6 @@ const unescapeString = ( arg ) => {
|
|
|
13
13
|
return decodeEntities( arg );
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
-
const unescapeTerm = ( term ) => {
|
|
17
|
-
return {
|
|
18
|
-
...term,
|
|
19
|
-
name: unescapeString( term.name ),
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
|
|
23
16
|
const EMPTY_ARRAY = [];
|
|
24
17
|
const MAX_TERMS_SUGGESTIONS = 20;
|
|
25
18
|
const DEFAULT_QUERY = {
|
|
@@ -27,13 +20,11 @@ const DEFAULT_QUERY = {
|
|
|
27
20
|
_fields: 'id,name',
|
|
28
21
|
context: 'view',
|
|
29
22
|
};
|
|
30
|
-
const
|
|
23
|
+
export const CATEGORY_SLUG = 'wp_pattern_category';
|
|
31
24
|
|
|
32
|
-
export default function CategorySelector( {
|
|
33
|
-
const [ values, setValues ] = useState( [] );
|
|
25
|
+
export default function CategorySelector( { values, onChange } ) {
|
|
34
26
|
const [ search, setSearch ] = useState( '' );
|
|
35
27
|
const debouncedSearch = useDebounce( setSearch, 500 );
|
|
36
|
-
const { invalidateResolution } = useDispatch( coreStore );
|
|
37
28
|
|
|
38
29
|
const { searchResults } = useSelect(
|
|
39
30
|
( select ) => {
|
|
@@ -41,7 +32,7 @@ export default function CategorySelector( { onCategorySelection } ) {
|
|
|
41
32
|
|
|
42
33
|
return {
|
|
43
34
|
searchResults: !! search
|
|
44
|
-
? getEntityRecords( 'taxonomy',
|
|
35
|
+
? getEntityRecords( 'taxonomy', CATEGORY_SLUG, {
|
|
45
36
|
...DEFAULT_QUERY,
|
|
46
37
|
search,
|
|
47
38
|
} )
|
|
@@ -57,28 +48,7 @@ export default function CategorySelector( { onCategorySelection } ) {
|
|
|
57
48
|
);
|
|
58
49
|
}, [ searchResults ] );
|
|
59
50
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
async function findOrCreateTerm( term ) {
|
|
63
|
-
try {
|
|
64
|
-
const newTerm = await saveEntityRecord( 'taxonomy', slug, term, {
|
|
65
|
-
throwOnError: true,
|
|
66
|
-
} );
|
|
67
|
-
invalidateResolution( 'getUserPatternCategories' );
|
|
68
|
-
return unescapeTerm( newTerm );
|
|
69
|
-
} catch ( error ) {
|
|
70
|
-
if ( error.code !== 'term_exists' ) {
|
|
71
|
-
throw error;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return {
|
|
75
|
-
id: error.data.term_id,
|
|
76
|
-
name: term.name,
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function onChange( termNames ) {
|
|
51
|
+
function handleChange( termNames ) {
|
|
82
52
|
const uniqueTerms = termNames.reduce( ( terms, newTerm ) => {
|
|
83
53
|
if (
|
|
84
54
|
! terms.some(
|
|
@@ -90,15 +60,7 @@ export default function CategorySelector( { onCategorySelection } ) {
|
|
|
90
60
|
return terms;
|
|
91
61
|
}, [] );
|
|
92
62
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
Promise.all(
|
|
96
|
-
uniqueTerms.map( ( termName ) =>
|
|
97
|
-
findOrCreateTerm( { name: termName } )
|
|
98
|
-
)
|
|
99
|
-
).then( ( newTerms ) => {
|
|
100
|
-
onCategorySelection( newTerms );
|
|
101
|
-
} );
|
|
63
|
+
onChange( uniqueTerms );
|
|
102
64
|
}
|
|
103
65
|
|
|
104
66
|
return (
|
|
@@ -107,7 +69,7 @@ export default function CategorySelector( { onCategorySelection } ) {
|
|
|
107
69
|
className="patterns-menu-items__convert-modal-categories"
|
|
108
70
|
value={ values }
|
|
109
71
|
suggestions={ suggestions }
|
|
110
|
-
onChange={
|
|
72
|
+
onChange={ handleChange }
|
|
111
73
|
onInputChange={ debouncedSearch }
|
|
112
74
|
maxSuggestions={ MAX_TERMS_SUGGESTIONS }
|
|
113
75
|
label={ __( 'Categories' ) }
|
|
@@ -13,6 +13,7 @@ import { __ } from '@wordpress/i18n';
|
|
|
13
13
|
import { useState } from '@wordpress/element';
|
|
14
14
|
import { useDispatch } from '@wordpress/data';
|
|
15
15
|
import { store as noticesStore } from '@wordpress/notices';
|
|
16
|
+
import { store as coreStore } from '@wordpress/core-data';
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
19
|
* Internal dependencies
|
|
@@ -23,7 +24,7 @@ import { PATTERN_DEFAULT_CATEGORY, PATTERN_SYNC_TYPES } from '../constants';
|
|
|
23
24
|
* Internal dependencies
|
|
24
25
|
*/
|
|
25
26
|
import { store as patternsStore } from '../store';
|
|
26
|
-
import CategorySelector from './category-selector';
|
|
27
|
+
import CategorySelector, { CATEGORY_SLUG } from './category-selector';
|
|
27
28
|
import { unlock } from '../lock-unlock';
|
|
28
29
|
|
|
29
30
|
export default function CreatePatternModal( {
|
|
@@ -34,13 +35,26 @@ export default function CreatePatternModal( {
|
|
|
34
35
|
className = 'patterns-menu-items__convert-modal',
|
|
35
36
|
} ) {
|
|
36
37
|
const [ syncType, setSyncType ] = useState( PATTERN_SYNC_TYPES.full );
|
|
37
|
-
const [
|
|
38
|
+
const [ categoryTerms, setCategoryTerms ] = useState( [] );
|
|
38
39
|
const [ title, setTitle ] = useState( '' );
|
|
40
|
+
const [ isSaving, setIsSaving ] = useState( false );
|
|
39
41
|
const { createPattern } = unlock( useDispatch( patternsStore ) );
|
|
40
|
-
|
|
42
|
+
const { saveEntityRecord, invalidateResolution } = useDispatch( coreStore );
|
|
41
43
|
const { createErrorNotice } = useDispatch( noticesStore );
|
|
44
|
+
|
|
42
45
|
async function onCreate( patternTitle, sync ) {
|
|
46
|
+
if ( ! title || isSaving ) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
43
50
|
try {
|
|
51
|
+
setIsSaving( true );
|
|
52
|
+
const categories = await Promise.all(
|
|
53
|
+
categoryTerms.map( ( termName ) =>
|
|
54
|
+
findOrCreateTerm( termName )
|
|
55
|
+
)
|
|
56
|
+
);
|
|
57
|
+
|
|
44
58
|
const newPattern = await createPattern(
|
|
45
59
|
patternTitle,
|
|
46
60
|
sync,
|
|
@@ -57,12 +71,35 @@ export default function CreatePatternModal( {
|
|
|
57
71
|
id: 'convert-to-pattern-error',
|
|
58
72
|
} );
|
|
59
73
|
onError();
|
|
74
|
+
} finally {
|
|
75
|
+
setIsSaving( false );
|
|
76
|
+
setCategoryTerms( [] );
|
|
77
|
+
setTitle( '' );
|
|
60
78
|
}
|
|
61
79
|
}
|
|
62
80
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
81
|
+
/**
|
|
82
|
+
* @param {string} term
|
|
83
|
+
* @return {Promise<number>} The pattern category id.
|
|
84
|
+
*/
|
|
85
|
+
async function findOrCreateTerm( term ) {
|
|
86
|
+
try {
|
|
87
|
+
const newTerm = await saveEntityRecord(
|
|
88
|
+
'taxonomy',
|
|
89
|
+
CATEGORY_SLUG,
|
|
90
|
+
{ name: term },
|
|
91
|
+
{ throwOnError: true }
|
|
92
|
+
);
|
|
93
|
+
invalidateResolution( 'getUserPatternCategories' );
|
|
94
|
+
return newTerm.id;
|
|
95
|
+
} catch ( error ) {
|
|
96
|
+
if ( error.code !== 'term_exists' ) {
|
|
97
|
+
throw error;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return error.data.term_id;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
66
103
|
|
|
67
104
|
return (
|
|
68
105
|
<Modal
|
|
@@ -77,7 +114,6 @@ export default function CreatePatternModal( {
|
|
|
77
114
|
onSubmit={ ( event ) => {
|
|
78
115
|
event.preventDefault();
|
|
79
116
|
onCreate( title, syncType );
|
|
80
|
-
setTitle( '' );
|
|
81
117
|
} }
|
|
82
118
|
>
|
|
83
119
|
<VStack spacing="5">
|
|
@@ -90,7 +126,8 @@ export default function CreatePatternModal( {
|
|
|
90
126
|
className="patterns-create-modal__name-input"
|
|
91
127
|
/>
|
|
92
128
|
<CategorySelector
|
|
93
|
-
|
|
129
|
+
values={ categoryTerms }
|
|
130
|
+
onChange={ setCategoryTerms }
|
|
94
131
|
/>
|
|
95
132
|
<ToggleControl
|
|
96
133
|
label={ __( 'Synced' ) }
|
|
@@ -117,7 +154,12 @@ export default function CreatePatternModal( {
|
|
|
117
154
|
{ __( 'Cancel' ) }
|
|
118
155
|
</Button>
|
|
119
156
|
|
|
120
|
-
<Button
|
|
157
|
+
<Button
|
|
158
|
+
variant="primary"
|
|
159
|
+
type="submit"
|
|
160
|
+
aria-disabled={ ! title || isSaving }
|
|
161
|
+
isBusy={ isSaving }
|
|
162
|
+
>
|
|
121
163
|
{ __( 'Create' ) }
|
|
122
164
|
</Button>
|
|
123
165
|
</HStack>
|