@wordpress/patterns 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE.md +788 -0
  3. package/README.md +26 -0
  4. package/build/components/create-pattern-modal.js +100 -0
  5. package/build/components/create-pattern-modal.js.map +1 -0
  6. package/build/components/index.js +32 -0
  7. package/build/components/index.js.map +1 -0
  8. package/build/components/pattern-convert-button.js +102 -0
  9. package/build/components/pattern-convert-button.js.map +1 -0
  10. package/build/components/patterns-manage-button.js +72 -0
  11. package/build/components/patterns-manage-button.js.map +1 -0
  12. package/build/index.js +18 -0
  13. package/build/index.js.map +1 -0
  14. package/build/index.native.js +19 -0
  15. package/build/index.native.js.map +1 -0
  16. package/build/lock-unlock.js +18 -0
  17. package/build/lock-unlock.js.map +1 -0
  18. package/build/private-apis.js +21 -0
  19. package/build/private-apis.js.map +1 -0
  20. package/build/store/actions.js +77 -0
  21. package/build/store/actions.js.map +1 -0
  22. package/build/store/constants.js +12 -0
  23. package/build/store/constants.js.map +1 -0
  24. package/build/store/index.js +49 -0
  25. package/build/store/index.js.map +1 -0
  26. package/build/store/reducer.js +26 -0
  27. package/build/store/reducer.js.map +1 -0
  28. package/build/store/selectors.js +17 -0
  29. package/build/store/selectors.js.map +1 -0
  30. package/build-module/components/create-pattern-modal.js +91 -0
  31. package/build-module/components/create-pattern-modal.js.map +1 -0
  32. package/build-module/components/index.js +24 -0
  33. package/build-module/components/index.js.map +1 -0
  34. package/build-module/components/pattern-convert-button.js +95 -0
  35. package/build-module/components/pattern-convert-button.js.map +1 -0
  36. package/build-module/components/patterns-manage-button.js +64 -0
  37. package/build-module/components/patterns-manage-button.js.map +1 -0
  38. package/build-module/index.js +6 -0
  39. package/build-module/index.js.map +1 -0
  40. package/build-module/index.native.js +11 -0
  41. package/build-module/index.native.js.map +1 -0
  42. package/build-module/lock-unlock.js +9 -0
  43. package/build-module/lock-unlock.js.map +1 -0
  44. package/build-module/private-apis.js +12 -0
  45. package/build-module/private-apis.js.map +1 -0
  46. package/build-module/store/actions.js +69 -0
  47. package/build-module/store/actions.js.map +1 -0
  48. package/build-module/store/constants.js +5 -0
  49. package/build-module/store/constants.js.map +1 -0
  50. package/build-module/store/index.js +38 -0
  51. package/build-module/store/index.js.map +1 -0
  52. package/build-module/store/reducer.js +17 -0
  53. package/build-module/store/reducer.js.map +1 -0
  54. package/build-module/store/selectors.js +11 -0
  55. package/build-module/store/selectors.js.map +1 -0
  56. package/build-style/style-rtl.css +108 -0
  57. package/build-style/style.css +108 -0
  58. package/package.json +55 -0
  59. package/src/components/create-pattern-modal.js +121 -0
  60. package/src/components/index.js +30 -0
  61. package/src/components/pattern-convert-button.js +123 -0
  62. package/src/components/patterns-manage-button.js +77 -0
  63. package/src/components/style.scss +3 -0
  64. package/src/index.js +6 -0
  65. package/src/index.native.js +11 -0
  66. package/src/lock-unlock.js +9 -0
  67. package/src/private-apis.js +12 -0
  68. package/src/store/actions.js +99 -0
  69. package/src/store/constants.js +4 -0
  70. package/src/store/index.js +38 -0
  71. package/src/store/reducer.js +19 -0
  72. package/src/store/selectors.js +10 -0
  73. package/src/style.scss +1 -0
@@ -0,0 +1,9 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { __dangerousOptInToUnstableAPIsOnlyForCoreModules } from '@wordpress/private-apis';
5
+ export const { lock, unlock } =
6
+ __dangerousOptInToUnstableAPIsOnlyForCoreModules(
7
+ 'I know using unstable features means my plugin or theme will inevitably break on the next WordPress release.',
8
+ '@wordpress/patterns'
9
+ );
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import { lock } from './lock-unlock';
5
+ import CreatePatternModal from './components/create-pattern-modal';
6
+ import PatternsMenuItems from './components';
7
+
8
+ export const privateApis = {};
9
+ lock( privateApis, {
10
+ CreatePatternModal,
11
+ PatternsMenuItems,
12
+ } );
@@ -0,0 +1,99 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+
5
+ import { parse, serialize, createBlock } from '@wordpress/blocks';
6
+ import { store as coreStore } from '@wordpress/core-data';
7
+ import { store as blockEditorStore } from '@wordpress/block-editor';
8
+
9
+ /**
10
+ * Returns a generator converting one or more static blocks into a pattern, or creating a new empty pattern.
11
+ *
12
+ * @param {string} title Pattern title.
13
+ * @param {'full'|'unsynced'} syncType They way block is synced, 'full' or 'unsynced'.
14
+ * @param {string[]|undefined} clientIds Optional client IDs of blocks to convert to pattern.
15
+ */
16
+ export const __experimentalCreatePattern =
17
+ ( title, syncType, clientIds ) =>
18
+ async ( { registry, dispatch } ) => {
19
+ const meta =
20
+ syncType === 'unsynced'
21
+ ? {
22
+ wp_pattern_sync_status: syncType,
23
+ }
24
+ : undefined;
25
+
26
+ const reusableBlock = {
27
+ title,
28
+ content: clientIds
29
+ ? serialize(
30
+ registry
31
+ .select( blockEditorStore )
32
+ .getBlocksByClientId( clientIds )
33
+ )
34
+ : undefined,
35
+ status: 'publish',
36
+ meta,
37
+ };
38
+
39
+ const updatedRecord = await registry
40
+ .dispatch( coreStore )
41
+ .saveEntityRecord( 'postType', 'wp_block', reusableBlock );
42
+
43
+ if ( syncType === 'unsynced' || ! clientIds ) {
44
+ return updatedRecord;
45
+ }
46
+
47
+ const newBlock = createBlock( 'core/block', {
48
+ ref: updatedRecord.id,
49
+ } );
50
+ registry
51
+ .dispatch( blockEditorStore )
52
+ .replaceBlocks( clientIds, newBlock );
53
+ dispatch.__experimentalSetEditingPattern( newBlock.clientId, true );
54
+ return updatedRecord;
55
+ };
56
+
57
+ /**
58
+ * Returns a generator converting a synced pattern block into a static block.
59
+ *
60
+ * @param {string} clientId The client ID of the block to attach.
61
+ */
62
+ export const __experimentalConvertSyncedPatternToStatic =
63
+ ( clientId ) =>
64
+ ( { registry } ) => {
65
+ const oldBlock = registry
66
+ .select( blockEditorStore )
67
+ .getBlock( clientId );
68
+ const pattern = registry
69
+ .select( 'core' )
70
+ .getEditedEntityRecord(
71
+ 'postType',
72
+ 'wp_block',
73
+ oldBlock.attributes.ref
74
+ );
75
+
76
+ const newBlocks = parse(
77
+ typeof pattern.content === 'function'
78
+ ? pattern.content( pattern )
79
+ : pattern.content
80
+ );
81
+ registry
82
+ .dispatch( blockEditorStore )
83
+ .replaceBlocks( oldBlock.clientId, newBlocks );
84
+ };
85
+
86
+ /**
87
+ * Returns an action descriptor for SET_EDITING_PATTERN action.
88
+ *
89
+ * @param {string} clientId The clientID of the pattern to target.
90
+ * @param {boolean} isEditing Whether the block should be in editing state.
91
+ * @return {Object} Action descriptor.
92
+ */
93
+ export function __experimentalSetEditingPattern( clientId, isEditing ) {
94
+ return {
95
+ type: 'SET_EDITING_PATTERN',
96
+ clientId,
97
+ isEditing,
98
+ };
99
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Module Constants
3
+ */
4
+ export const STORE_NAME = 'core/patterns';
@@ -0,0 +1,38 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { createReduxStore, register } from '@wordpress/data';
5
+
6
+ /**
7
+ * Internal dependencies
8
+ */
9
+ import reducer from './reducer';
10
+ import * as actions from './actions';
11
+ import { STORE_NAME } from './constants';
12
+ import * as selectors from './selectors';
13
+
14
+ /**
15
+ * Post editor data store configuration.
16
+ *
17
+ * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#registerStore
18
+ *
19
+ * @type {Object}
20
+ */
21
+ export const storeConfig = {
22
+ reducer,
23
+ selectors,
24
+ actions,
25
+ };
26
+
27
+ /**
28
+ * Store definition for the editor namespace.
29
+ *
30
+ * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
31
+ *
32
+ * @type {Object}
33
+ */
34
+ export const store = createReduxStore( STORE_NAME, {
35
+ ...storeConfig,
36
+ } );
37
+
38
+ register( store );
@@ -0,0 +1,19 @@
1
+ /**
2
+ * WordPress dependencies
3
+ */
4
+ import { combineReducers } from '@wordpress/data';
5
+
6
+ export function isEditingPattern( state = {}, action ) {
7
+ if ( action?.type === 'SET_EDITING_PATTERN' ) {
8
+ return {
9
+ ...state,
10
+ [ action.clientId ]: action.isEditing,
11
+ };
12
+ }
13
+
14
+ return state;
15
+ }
16
+
17
+ export default combineReducers( {
18
+ isEditingPattern,
19
+ } );
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Returns true if pattern is in the editing state.
3
+ *
4
+ * @param {Object} state Global application state.
5
+ * @param {number} clientId the clientID of the block.
6
+ * @return {boolean} Whether the pattern is in the editing state.
7
+ */
8
+ export function __experimentalIsEditingPattern( state, clientId ) {
9
+ return state.isEditingPattern[ clientId ];
10
+ }
package/src/style.scss ADDED
@@ -0,0 +1 @@
1
+ @import "./components/style.scss";