@wordpress/preferences 1.0.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 (46) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE.md +788 -0
  3. package/README.md +174 -0
  4. package/build/components/index.js +16 -0
  5. package/build/components/index.js.map +1 -0
  6. package/build/components/preference-toggle-menu-item/index.js +70 -0
  7. package/build/components/preference-toggle-menu-item/index.js.map +1 -0
  8. package/build/index.js +31 -0
  9. package/build/index.js.map +1 -0
  10. package/build/store/actions.js +65 -0
  11. package/build/store/actions.js.map +1 -0
  12. package/build/store/constants.js +15 -0
  13. package/build/store/constants.js.map +1 -0
  14. package/build/store/index.js +58 -0
  15. package/build/store/index.js.map +1 -0
  16. package/build/store/reducer.js +81 -0
  17. package/build/store/reducer.js.map +1 -0
  18. package/build/store/selectors.js +24 -0
  19. package/build/store/selectors.js.map +1 -0
  20. package/build-module/components/index.js +2 -0
  21. package/build-module/components/index.js.map +1 -0
  22. package/build-module/components/preference-toggle-menu-item/index.js +57 -0
  23. package/build-module/components/preference-toggle-menu-item/index.js.map +1 -0
  24. package/build-module/index.js +3 -0
  25. package/build-module/index.js.map +1 -0
  26. package/build-module/store/actions.js +54 -0
  27. package/build-module/store/actions.js.map +1 -0
  28. package/build-module/store/constants.js +7 -0
  29. package/build-module/store/constants.js.map +1 -0
  30. package/build-module/store/index.js +39 -0
  31. package/build-module/store/index.js.map +1 -0
  32. package/build-module/store/reducer.js +67 -0
  33. package/build-module/store/reducer.js.map +1 -0
  34. package/build-module/store/selectors.js +17 -0
  35. package/build-module/store/selectors.js.map +1 -0
  36. package/package.json +44 -0
  37. package/src/components/index.js +1 -0
  38. package/src/components/preference-toggle-menu-item/README.md +58 -0
  39. package/src/components/preference-toggle-menu-item/index.js +66 -0
  40. package/src/index.js +2 -0
  41. package/src/store/actions.js +49 -0
  42. package/src/store/constants.js +6 -0
  43. package/src/store/index.js +38 -0
  44. package/src/store/reducer.js +58 -0
  45. package/src/store/selectors.js +14 -0
  46. package/src/store/test/selectors.js +68 -0
package/README.md ADDED
@@ -0,0 +1,174 @@
1
+ # Preferences
2
+
3
+ Utilities for storing WordPress preferences.
4
+
5
+ ## Installation
6
+
7
+ Install the module
8
+
9
+ ```bash
10
+ npm install @wordpress/preferences --save
11
+ ```
12
+
13
+ _This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has limited or no support for such language features and APIs, you should include [the polyfill shipped in `@wordpress/babel-preset-default`](https://github.com/WordPress/gutenberg/tree/HEAD/packages/babel-preset-default#polyfill) in your code._
14
+
15
+ ## Examples
16
+
17
+ ### Data store
18
+
19
+ Preferences are persisted values of any kind.
20
+
21
+ Set the default preferences for any features on initialization by dispatching an action:
22
+
23
+ ```js
24
+ import { dispatch } from '@wordpress/data';
25
+ import { store as preferencesStore } from '@wordpress/preferences';
26
+
27
+ function initialize() {
28
+ // ...
29
+
30
+ dispatch( preferencesStore ).setDefaults(
31
+ 'namespace/editor-or-plugin-name',
32
+ {
33
+ myBooleanFeature: true,
34
+ }
35
+ );
36
+
37
+ // ...
38
+ }
39
+ ```
40
+
41
+ Or the `get` selector to get a preference value, and the `set` action to update a preference to any value:
42
+
43
+ ```js
44
+ wp.data
45
+ .select( 'core/preferences' )
46
+ .get( 'namespace/editor-or-plugin-name', 'myPreferenceName' ); // 1
47
+ wp.data
48
+ .dispatch( 'core/preferences' )
49
+ .set( 'namespace/editor-or-plugin-name', 'myPreferenceName', 2 );
50
+ wp.data
51
+ .select( 'core/preferences' )
52
+ .get( 'namespace/editor-or-plugin-name', 'myPreferenceName' ); // 2
53
+ ```
54
+
55
+ Use the `toggle` action to flip a boolean preference between `true` and `false`:
56
+
57
+ ```js
58
+ wp.data
59
+ .select( 'core/preferences' )
60
+ .get( 'namespace/editor-or-plugin-name', 'myPreferenceName' ); // true
61
+ wp.data
62
+ .dispatch( 'core/preferences' )
63
+ .toggle( 'namespace/editor-or-plugin-name', 'myPreferenceName' );
64
+ wp.data
65
+ .select( 'core/preferences' )
66
+ .get( 'namespace/editor-or-plugin-name', 'myPreferenceName' ); // false
67
+ ```
68
+
69
+ ### Components
70
+
71
+ The `PreferenceToggleMenuItem` components can be used with a `DropdownMenu` to implement a menu for changing preferences.
72
+
73
+ Also see the `MoreMenuDropdown` component from the `@wordpress/interface` package for implementing a more menu.
74
+
75
+ ```jsx
76
+ function MyEditorMenu() {
77
+ return (
78
+ <MoreMenuDropdown>
79
+ { () => (
80
+ <MenuGroup label={ __( 'Features' ) }>
81
+ <PreferenceToggleMenuItem
82
+ scope="namespace/editor-or-plugin-name"
83
+ name="myPreferenceName"
84
+ label={ __( 'My feature' ) }
85
+ info={ __( 'A really awesome feature' ) }
86
+ messageActivated={ __( 'My feature activated' ) }
87
+ messageDeactivated={ __( 'My feature deactivated' ) }
88
+ />
89
+ </MenuGroup>
90
+ ) }
91
+ </MoreMenuDropdown>
92
+ );
93
+ }
94
+ ```
95
+
96
+ ## API Reference
97
+
98
+ ### Actions
99
+
100
+ The following set of dispatching action creators are available on the object returned by `wp.data.dispatch( 'core/preferences' )`:
101
+
102
+ <!-- START TOKEN(Autogenerated actions|src/store/actions.js) -->
103
+
104
+ #### set
105
+
106
+ Returns an action object used in signalling that a preference should be set
107
+ to a value
108
+
109
+ _Parameters_
110
+
111
+ - _scope_ `string`: The preference scope (e.g. core/edit-post).
112
+ - _name_ `string`: The preference name.
113
+ - _value_ `*`: The value to set.
114
+
115
+ _Returns_
116
+
117
+ - `Object`: Action object.
118
+
119
+ #### setDefaults
120
+
121
+ Returns an action object used in signalling that preference defaults should
122
+ be set.
123
+
124
+ _Parameters_
125
+
126
+ - _scope_ `string`: The preference scope (e.g. core/edit-post).
127
+ - _defaults_ `Object<string, *>`: A key/value map of preference names to values.
128
+
129
+ _Returns_
130
+
131
+ - `Object`: Action object.
132
+
133
+ #### toggle
134
+
135
+ Returns an action object used in signalling that a preference should be
136
+ toggled.
137
+
138
+ _Parameters_
139
+
140
+ - _scope_ `string`: The preference scope (e.g. core/edit-post).
141
+ - _name_ `string`: The preference name.
142
+
143
+ <!-- END TOKEN(Autogenerated actions|src/store/actions.js) -->
144
+
145
+ ### Selectors
146
+
147
+ The following selectors are available on the object returned by `wp.data.select( 'core/preferences' )`:
148
+
149
+ <!-- START TOKEN(Autogenerated selectors|src/store/selectors.js) -->
150
+
151
+ #### get
152
+
153
+ Returns a boolean indicating whether a prefer is active for a particular
154
+ scope.
155
+
156
+ _Parameters_
157
+
158
+ - _state_ `Object`: The store state.
159
+ - _scope_ `string`: The scope of the feature (e.g. core/edit-post).
160
+ - _name_ `string`: The name of the feature.
161
+
162
+ _Returns_
163
+
164
+ - `*`: Is the feature enabled?
165
+
166
+ <!-- END TOKEN(Autogenerated selectors|src/store/selectors.js) -->
167
+
168
+ ## Contributing to this package
169
+
170
+ This is an individual package that's part of the Gutenberg project. The project is organized as a monorepo. It's made up of multiple self-contained software packages, each with a specific purpose. The packages in this monorepo are published to [npm](https://www.npmjs.com/) and used by [WordPress](https://make.wordpress.org/core/) as well as other software projects.
171
+
172
+ To find out more about contributing to this package or Gutenberg as a whole, please read the project's main [contributor guide](https://github.com/WordPress/gutenberg/tree/HEAD/CONTRIBUTING.md).
173
+
174
+ <br /><br /><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ Object.defineProperty(exports, "PreferenceToggleMenuItem", {
9
+ enumerable: true,
10
+ get: function () {
11
+ return _preferenceToggleMenuItem.default;
12
+ }
13
+ });
14
+
15
+ var _preferenceToggleMenuItem = _interopRequireDefault(require("./preference-toggle-menu-item"));
16
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences/src/components/index.js"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA","sourcesContent":["export { default as PreferenceToggleMenuItem } from './preference-toggle-menu-item';\n"]}
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = PreferenceToggleMenuItem;
7
+
8
+ var _element = require("@wordpress/element");
9
+
10
+ var _data = require("@wordpress/data");
11
+
12
+ var _components = require("@wordpress/components");
13
+
14
+ var _i18n = require("@wordpress/i18n");
15
+
16
+ var _icons = require("@wordpress/icons");
17
+
18
+ var _a11y = require("@wordpress/a11y");
19
+
20
+ var _store = require("../../store");
21
+
22
+ /**
23
+ * WordPress dependencies
24
+ */
25
+
26
+ /**
27
+ * Internal dependencies
28
+ */
29
+ function PreferenceToggleMenuItem(_ref) {
30
+ let {
31
+ scope,
32
+ name,
33
+ label,
34
+ info,
35
+ messageActivated,
36
+ messageDeactivated,
37
+ shortcut
38
+ } = _ref;
39
+ const isActive = (0, _data.useSelect)(select => !!select(_store.store).get(scope, name), [name]);
40
+ const {
41
+ toggle
42
+ } = (0, _data.useDispatch)(_store.store);
43
+
44
+ const speakMessage = () => {
45
+ if (isActive) {
46
+ const message = messageDeactivated || (0, _i18n.sprintf)(
47
+ /* translators: %s: preference name, e.g. 'Fullscreen mode' */
48
+ (0, _i18n.__)('Preference deactivated - %s'), label);
49
+ (0, _a11y.speak)(message);
50
+ } else {
51
+ const message = messageActivated || (0, _i18n.sprintf)(
52
+ /* translators: %s: preference name, e.g. 'Fullscreen mode' */
53
+ (0, _i18n.__)('Preference activated - %s'), label);
54
+ (0, _a11y.speak)(message);
55
+ }
56
+ };
57
+
58
+ return (0, _element.createElement)(_components.MenuItem, {
59
+ icon: isActive && _icons.check,
60
+ isSelected: isActive,
61
+ onClick: () => {
62
+ toggle(scope, name);
63
+ speakMessage();
64
+ },
65
+ role: "menuitemcheckbox",
66
+ info: info,
67
+ shortcut: shortcut
68
+ }, label);
69
+ }
70
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences/src/components/preference-toggle-menu-item/index.js"],"names":["PreferenceToggleMenuItem","scope","name","label","info","messageActivated","messageDeactivated","shortcut","isActive","select","preferencesStore","get","toggle","speakMessage","message","check"],"mappings":";;;;;;;;;AAGA;;AACA;;AACA;;AACA;;AACA;;AAKA;;AAZA;AACA;AACA;;AAOA;AACA;AACA;AAGe,SAASA,wBAAT,OAQX;AAAA,MAR8C;AACjDC,IAAAA,KADiD;AAEjDC,IAAAA,IAFiD;AAGjDC,IAAAA,KAHiD;AAIjDC,IAAAA,IAJiD;AAKjDC,IAAAA,gBALiD;AAMjDC,IAAAA,kBANiD;AAOjDC,IAAAA;AAPiD,GAQ9C;AACH,QAAMC,QAAQ,GAAG,qBACdC,MAAF,IAAc,CAAC,CAAEA,MAAM,CAAEC,YAAF,CAAN,CAA2BC,GAA3B,CAAgCV,KAAhC,EAAuCC,IAAvC,CADD,EAEhB,CAAEA,IAAF,CAFgB,CAAjB;AAIA,QAAM;AAAEU,IAAAA;AAAF,MAAa,uBAAaF,YAAb,CAAnB;;AACA,QAAMG,YAAY,GAAG,MAAM;AAC1B,QAAKL,QAAL,EAAgB;AACf,YAAMM,OAAO,GACZR,kBAAkB,IAClB;AACC;AACA,oBAAI,6BAAJ,CAFD,EAGCH,KAHD,CAFD;AAOA,uBAAOW,OAAP;AACA,KATD,MASO;AACN,YAAMA,OAAO,GACZT,gBAAgB,IAChB;AACC;AACA,oBAAI,2BAAJ,CAFD,EAGCF,KAHD,CAFD;AAOA,uBAAOW,OAAP;AACA;AACD,GApBD;;AAsBA,SACC,4BAAC,oBAAD;AACC,IAAA,IAAI,EAAGN,QAAQ,IAAIO,YADpB;AAEC,IAAA,UAAU,EAAGP,QAFd;AAGC,IAAA,OAAO,EAAG,MAAM;AACfI,MAAAA,MAAM,CAAEX,KAAF,EAASC,IAAT,CAAN;AACAW,MAAAA,YAAY;AACZ,KANF;AAOC,IAAA,IAAI,EAAC,kBAPN;AAQC,IAAA,IAAI,EAAGT,IARR;AASC,IAAA,QAAQ,EAAGG;AATZ,KAWGJ,KAXH,CADD;AAeA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { MenuItem } from '@wordpress/components';\nimport { __, sprintf } from '@wordpress/i18n';\nimport { check } from '@wordpress/icons';\nimport { speak } from '@wordpress/a11y';\n\n/**\n * Internal dependencies\n */\nimport { store as preferencesStore } from '../../store';\n\nexport default function PreferenceToggleMenuItem( {\n\tscope,\n\tname,\n\tlabel,\n\tinfo,\n\tmessageActivated,\n\tmessageDeactivated,\n\tshortcut,\n} ) {\n\tconst isActive = useSelect(\n\t\t( select ) => !! select( preferencesStore ).get( scope, name ),\n\t\t[ name ]\n\t);\n\tconst { toggle } = useDispatch( preferencesStore );\n\tconst speakMessage = () => {\n\t\tif ( isActive ) {\n\t\t\tconst message =\n\t\t\t\tmessageDeactivated ||\n\t\t\t\tsprintf(\n\t\t\t\t\t/* translators: %s: preference name, e.g. 'Fullscreen mode' */\n\t\t\t\t\t__( 'Preference deactivated - %s' ),\n\t\t\t\t\tlabel\n\t\t\t\t);\n\t\t\tspeak( message );\n\t\t} else {\n\t\t\tconst message =\n\t\t\t\tmessageActivated ||\n\t\t\t\tsprintf(\n\t\t\t\t\t/* translators: %s: preference name, e.g. 'Fullscreen mode' */\n\t\t\t\t\t__( 'Preference activated - %s' ),\n\t\t\t\t\tlabel\n\t\t\t\t);\n\t\t\tspeak( message );\n\t\t}\n\t};\n\n\treturn (\n\t\t<MenuItem\n\t\t\ticon={ isActive && check }\n\t\t\tisSelected={ isActive }\n\t\t\tonClick={ () => {\n\t\t\t\ttoggle( scope, name );\n\t\t\t\tspeakMessage();\n\t\t\t} }\n\t\t\trole=\"menuitemcheckbox\"\n\t\t\tinfo={ info }\n\t\t\tshortcut={ shortcut }\n\t\t>\n\t\t\t{ label }\n\t\t</MenuItem>\n\t);\n}\n"]}
package/build/index.js ADDED
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ var _exportNames = {
7
+ store: true
8
+ };
9
+ Object.defineProperty(exports, "store", {
10
+ enumerable: true,
11
+ get: function () {
12
+ return _store.store;
13
+ }
14
+ });
15
+
16
+ var _components = require("./components");
17
+
18
+ Object.keys(_components).forEach(function (key) {
19
+ if (key === "default" || key === "__esModule") return;
20
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
21
+ if (key in exports && exports[key] === _components[key]) return;
22
+ Object.defineProperty(exports, key, {
23
+ enumerable: true,
24
+ get: function () {
25
+ return _components[key];
26
+ }
27
+ });
28
+ });
29
+
30
+ var _store = require("./store");
31
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences/src/index.js"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;AACA","sourcesContent":["export * from './components';\nexport { store } from './store';\n"]}
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.set = set;
7
+ exports.setDefaults = setDefaults;
8
+ exports.toggle = toggle;
9
+
10
+ /**
11
+ * Returns an action object used in signalling that a preference should be
12
+ * toggled.
13
+ *
14
+ * @param {string} scope The preference scope (e.g. core/edit-post).
15
+ * @param {string} name The preference name.
16
+ */
17
+ function toggle(scope, name) {
18
+ return function (_ref) {
19
+ let {
20
+ select,
21
+ dispatch
22
+ } = _ref;
23
+ const currentValue = select.get(scope, name);
24
+ dispatch.set(scope, name, !currentValue);
25
+ };
26
+ }
27
+ /**
28
+ * Returns an action object used in signalling that a preference should be set
29
+ * to a value
30
+ *
31
+ * @param {string} scope The preference scope (e.g. core/edit-post).
32
+ * @param {string} name The preference name.
33
+ * @param {*} value The value to set.
34
+ *
35
+ * @return {Object} Action object.
36
+ */
37
+
38
+
39
+ function set(scope, name, value) {
40
+ return {
41
+ type: 'SET_PREFERENCE_VALUE',
42
+ scope,
43
+ name,
44
+ value
45
+ };
46
+ }
47
+ /**
48
+ * Returns an action object used in signalling that preference defaults should
49
+ * be set.
50
+ *
51
+ * @param {string} scope The preference scope (e.g. core/edit-post).
52
+ * @param {Object<string, *>} defaults A key/value map of preference names to values.
53
+ *
54
+ * @return {Object} Action object.
55
+ */
56
+
57
+
58
+ function setDefaults(scope, defaults) {
59
+ return {
60
+ type: 'SET_PREFERENCE_DEFAULTS',
61
+ scope,
62
+ defaults
63
+ };
64
+ }
65
+ //# sourceMappingURL=actions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences/src/store/actions.js"],"names":["toggle","scope","name","select","dispatch","currentValue","get","set","value","type","setDefaults","defaults"],"mappings":";;;;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,MAAT,CAAiBC,KAAjB,EAAwBC,IAAxB,EAA+B;AACrC,SAAO,gBAAkC;AAAA,QAAvB;AAAEC,MAAAA,MAAF;AAAUC,MAAAA;AAAV,KAAuB;AACxC,UAAMC,YAAY,GAAGF,MAAM,CAACG,GAAP,CAAYL,KAAZ,EAAmBC,IAAnB,CAArB;AACAE,IAAAA,QAAQ,CAACG,GAAT,CAAcN,KAAd,EAAqBC,IAArB,EAA2B,CAAEG,YAA7B;AACA,GAHD;AAIA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASE,GAAT,CAAcN,KAAd,EAAqBC,IAArB,EAA2BM,KAA3B,EAAmC;AACzC,SAAO;AACNC,IAAAA,IAAI,EAAE,sBADA;AAENR,IAAAA,KAFM;AAGNC,IAAAA,IAHM;AAINM,IAAAA;AAJM,GAAP;AAMA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASE,WAAT,CAAsBT,KAAtB,EAA6BU,QAA7B,EAAwC;AAC9C,SAAO;AACNF,IAAAA,IAAI,EAAE,yBADA;AAENR,IAAAA,KAFM;AAGNU,IAAAA;AAHM,GAAP;AAKA","sourcesContent":["/**\n * Returns an action object used in signalling that a preference should be\n * toggled.\n *\n * @param {string} scope The preference scope (e.g. core/edit-post).\n * @param {string} name The preference name.\n */\nexport function toggle( scope, name ) {\n\treturn function ( { select, dispatch } ) {\n\t\tconst currentValue = select.get( scope, name );\n\t\tdispatch.set( scope, name, ! currentValue );\n\t};\n}\n\n/**\n * Returns an action object used in signalling that a preference should be set\n * to a value\n *\n * @param {string} scope The preference scope (e.g. core/edit-post).\n * @param {string} name The preference name.\n * @param {*} value The value to set.\n *\n * @return {Object} Action object.\n */\nexport function set( scope, name, value ) {\n\treturn {\n\t\ttype: 'SET_PREFERENCE_VALUE',\n\t\tscope,\n\t\tname,\n\t\tvalue,\n\t};\n}\n\n/**\n * Returns an action object used in signalling that preference defaults should\n * be set.\n *\n * @param {string} scope The preference scope (e.g. core/edit-post).\n * @param {Object<string, *>} defaults A key/value map of preference names to values.\n *\n * @return {Object} Action object.\n */\nexport function setDefaults( scope, defaults ) {\n\treturn {\n\t\ttype: 'SET_PREFERENCE_DEFAULTS',\n\t\tscope,\n\t\tdefaults,\n\t};\n}\n"]}
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.STORE_NAME = void 0;
7
+
8
+ /**
9
+ * The identifier for the data store.
10
+ *
11
+ * @type {string}
12
+ */
13
+ const STORE_NAME = 'core/preferences';
14
+ exports.STORE_NAME = STORE_NAME;
15
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences/src/store/constants.js"],"names":["STORE_NAME"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA;AACA;AACO,MAAMA,UAAU,GAAG,kBAAnB","sourcesContent":["/**\n * The identifier for the data store.\n *\n * @type {string}\n */\nexport const STORE_NAME = 'core/preferences';\n"]}
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.store = void 0;
9
+
10
+ var _data = require("@wordpress/data");
11
+
12
+ var _reducer = _interopRequireDefault(require("./reducer"));
13
+
14
+ var actions = _interopRequireWildcard(require("./actions"));
15
+
16
+ var selectors = _interopRequireWildcard(require("./selectors"));
17
+
18
+ var _constants = require("./constants");
19
+
20
+ 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); }
21
+
22
+ 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; }
23
+
24
+ /**
25
+ * WordPress dependencies
26
+ */
27
+
28
+ /**
29
+ * Internal dependencies
30
+ */
31
+
32
+ /**
33
+ * Internal dependencies
34
+ */
35
+
36
+ /**
37
+ * Store definition for the interface namespace.
38
+ *
39
+ * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
40
+ *
41
+ * @type {Object}
42
+ */
43
+ const store = (0, _data.createReduxStore)(_constants.STORE_NAME, {
44
+ reducer: _reducer.default,
45
+ actions,
46
+ selectors,
47
+ persist: ['preferences']
48
+ }); // Once we build a more generic persistence plugin that works across types of stores
49
+ // we'd be able to replace this with a register call.
50
+
51
+ exports.store = store;
52
+ (0, _data.registerStore)(_constants.STORE_NAME, {
53
+ reducer: _reducer.default,
54
+ actions,
55
+ selectors,
56
+ persist: ['preferences']
57
+ });
58
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences/src/store/index.js"],"names":["store","STORE_NAME","reducer","actions","selectors","persist"],"mappings":";;;;;;;;;AAGA;;AAQA;;AACA;;AACA;;AACA;;;;;;AAdA;AACA;AACA;;AAGA;AACA;AACA;;AACA;AACA;AACA;;AAMA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMA,KAAK,GAAG,4BAAkBC,qBAAlB,EAA8B;AAClDC,EAAAA,OAAO,EAAPA,gBADkD;AAElDC,EAAAA,OAFkD;AAGlDC,EAAAA,SAHkD;AAIlDC,EAAAA,OAAO,EAAE,CAAE,aAAF;AAJyC,CAA9B,CAAd,C,CAOP;AACA;;;AACA,yBAAeJ,qBAAf,EAA2B;AAC1BC,EAAAA,OAAO,EAAPA,gBAD0B;AAE1BC,EAAAA,OAF0B;AAG1BC,EAAAA,SAH0B;AAI1BC,EAAAA,OAAO,EAAE,CAAE,aAAF;AAJiB,CAA3B","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { createReduxStore, registerStore } from '@wordpress/data';\n\n/**\n * Internal dependencies\n */\n/**\n * Internal dependencies\n */\nimport reducer from './reducer';\nimport * as actions from './actions';\nimport * as selectors from './selectors';\nimport { STORE_NAME } from './constants';\n\n/**\n * Store definition for the interface 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\treducer,\n\tactions,\n\tselectors,\n\tpersist: [ 'preferences' ],\n} );\n\n// Once we build a more generic persistence plugin that works across types of stores\n// we'd be able to replace this with a register call.\nregisterStore( STORE_NAME, {\n\treducer,\n\tactions,\n\tselectors,\n\tpersist: [ 'preferences' ],\n} );\n"]}
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ exports.defaults = defaults;
8
+ exports.preferences = preferences;
9
+
10
+ var _data = require("@wordpress/data");
11
+
12
+ /**
13
+ * WordPress dependencies
14
+ */
15
+
16
+ /**
17
+ * Reducer returning the defaults for user preferences.
18
+ *
19
+ * This is kept intentionally separate from the preferences
20
+ * themselves so that defaults are not persisted.
21
+ *
22
+ * @param {Object} state Current state.
23
+ * @param {Object} action Dispatched action.
24
+ *
25
+ * @return {Object} Updated state.
26
+ */
27
+ function defaults() {
28
+ let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
29
+ let action = arguments.length > 1 ? arguments[1] : undefined;
30
+
31
+ if (action.type === 'SET_PREFERENCE_DEFAULTS') {
32
+ const {
33
+ scope,
34
+ defaults: values
35
+ } = action;
36
+ return { ...state,
37
+ [scope]: { ...state[scope],
38
+ ...values
39
+ }
40
+ };
41
+ }
42
+
43
+ return state;
44
+ }
45
+ /**
46
+ * Reducer returning the user preferences.
47
+ *
48
+ * @param {Object} state Current state.
49
+ * @param {Object} action Dispatched action.
50
+ *
51
+ * @return {Object} Updated state.
52
+ */
53
+
54
+
55
+ function preferences() {
56
+ let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
57
+ let action = arguments.length > 1 ? arguments[1] : undefined;
58
+
59
+ if (action.type === 'SET_PREFERENCE_VALUE') {
60
+ const {
61
+ scope,
62
+ name,
63
+ value
64
+ } = action;
65
+ return { ...state,
66
+ [scope]: { ...state[scope],
67
+ [name]: value
68
+ }
69
+ };
70
+ }
71
+
72
+ return state;
73
+ }
74
+
75
+ var _default = (0, _data.combineReducers)({
76
+ defaults,
77
+ preferences
78
+ });
79
+
80
+ exports.default = _default;
81
+ //# sourceMappingURL=reducer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences/src/store/reducer.js"],"names":["defaults","state","action","type","scope","values","preferences","name","value"],"mappings":";;;;;;;;;AAGA;;AAHA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,QAAT,GAAwC;AAAA,MAArBC,KAAqB,uEAAb,EAAa;AAAA,MAATC,MAAS;;AAC9C,MAAKA,MAAM,CAACC,IAAP,KAAgB,yBAArB,EAAiD;AAChD,UAAM;AAAEC,MAAAA,KAAF;AAASJ,MAAAA,QAAQ,EAAEK;AAAnB,QAA8BH,MAApC;AACA,WAAO,EACN,GAAGD,KADG;AAEN,OAAEG,KAAF,GAAW,EACV,GAAGH,KAAK,CAAEG,KAAF,CADE;AAEV,WAAGC;AAFO;AAFL,KAAP;AAOA;;AAED,SAAOJ,KAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASK,WAAT,GAA2C;AAAA,MAArBL,KAAqB,uEAAb,EAAa;AAAA,MAATC,MAAS;;AACjD,MAAKA,MAAM,CAACC,IAAP,KAAgB,sBAArB,EAA8C;AAC7C,UAAM;AAAEC,MAAAA,KAAF;AAASG,MAAAA,IAAT;AAAeC,MAAAA;AAAf,QAAyBN,MAA/B;AACA,WAAO,EACN,GAAGD,KADG;AAEN,OAAEG,KAAF,GAAW,EACV,GAAGH,KAAK,CAAEG,KAAF,CADE;AAEV,SAAEG,IAAF,GAAUC;AAFA;AAFL,KAAP;AAOA;;AAED,SAAOP,KAAP;AACA;;eAEc,2BAAiB;AAC/BD,EAAAA,QAD+B;AAE/BM,EAAAA;AAF+B,CAAjB,C","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { combineReducers } from '@wordpress/data';\n\n/**\n * Reducer returning the defaults for user preferences.\n *\n * This is kept intentionally separate from the preferences\n * themselves so that defaults are not persisted.\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Updated state.\n */\nexport function defaults( state = {}, action ) {\n\tif ( action.type === 'SET_PREFERENCE_DEFAULTS' ) {\n\t\tconst { scope, defaults: values } = action;\n\t\treturn {\n\t\t\t...state,\n\t\t\t[ scope ]: {\n\t\t\t\t...state[ scope ],\n\t\t\t\t...values,\n\t\t\t},\n\t\t};\n\t}\n\n\treturn state;\n}\n\n/**\n * Reducer returning the user preferences.\n *\n * @param {Object} state Current state.\n * @param {Object} action Dispatched action.\n *\n * @return {Object} Updated state.\n */\nexport function preferences( state = {}, action ) {\n\tif ( action.type === 'SET_PREFERENCE_VALUE' ) {\n\t\tconst { scope, name, value } = action;\n\t\treturn {\n\t\t\t...state,\n\t\t\t[ scope ]: {\n\t\t\t\t...state[ scope ],\n\t\t\t\t[ name ]: value,\n\t\t\t},\n\t\t};\n\t}\n\n\treturn state;\n}\n\nexport default combineReducers( {\n\tdefaults,\n\tpreferences,\n} );\n"]}
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.get = get;
7
+
8
+ /**
9
+ * Returns a boolean indicating whether a prefer is active for a particular
10
+ * scope.
11
+ *
12
+ * @param {Object} state The store state.
13
+ * @param {string} scope The scope of the feature (e.g. core/edit-post).
14
+ * @param {string} name The name of the feature.
15
+ *
16
+ * @return {*} Is the feature enabled?
17
+ */
18
+ function get(state, scope, name) {
19
+ var _state$preferences$sc, _state$defaults$scope;
20
+
21
+ const value = (_state$preferences$sc = state.preferences[scope]) === null || _state$preferences$sc === void 0 ? void 0 : _state$preferences$sc[name];
22
+ return value !== null && value !== void 0 ? value : (_state$defaults$scope = state.defaults[scope]) === null || _state$defaults$scope === void 0 ? void 0 : _state$defaults$scope[name];
23
+ }
24
+ //# sourceMappingURL=selectors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences/src/store/selectors.js"],"names":["get","state","scope","name","value","preferences","defaults"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,GAAT,CAAcC,KAAd,EAAqBC,KAArB,EAA4BC,IAA5B,EAAmC;AAAA;;AACzC,QAAMC,KAAK,4BAAGH,KAAK,CAACI,WAAN,CAAmBH,KAAnB,CAAH,0DAAG,sBAA8BC,IAA9B,CAAd;AACA,SAAOC,KAAP,aAAOA,KAAP,cAAOA,KAAP,4BAAgBH,KAAK,CAACK,QAAN,CAAgBJ,KAAhB,CAAhB,0DAAgB,sBAA2BC,IAA3B,CAAhB;AACA","sourcesContent":["/**\n * Returns a boolean indicating whether a prefer is active for a particular\n * scope.\n *\n * @param {Object} state The store state.\n * @param {string} scope The scope of the feature (e.g. core/edit-post).\n * @param {string} name The name of the feature.\n *\n * @return {*} Is the feature enabled?\n */\nexport function get( state, scope, name ) {\n\tconst value = state.preferences[ scope ]?.[ name ];\n\treturn value ?? state.defaults[ scope ]?.[ name ];\n}\n"]}
@@ -0,0 +1,2 @@
1
+ export { default as PreferenceToggleMenuItem } from './preference-toggle-menu-item';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences/src/components/index.js"],"names":["default","PreferenceToggleMenuItem"],"mappings":"AAAA,SAASA,OAAO,IAAIC,wBAApB,QAAoD,+BAApD","sourcesContent":["export { default as PreferenceToggleMenuItem } from './preference-toggle-menu-item';\n"]}
@@ -0,0 +1,57 @@
1
+ import { createElement } from "@wordpress/element";
2
+
3
+ /**
4
+ * WordPress dependencies
5
+ */
6
+ import { useSelect, useDispatch } from '@wordpress/data';
7
+ import { MenuItem } from '@wordpress/components';
8
+ import { __, sprintf } from '@wordpress/i18n';
9
+ import { check } from '@wordpress/icons';
10
+ import { speak } from '@wordpress/a11y';
11
+ /**
12
+ * Internal dependencies
13
+ */
14
+
15
+ import { store as preferencesStore } from '../../store';
16
+ export default function PreferenceToggleMenuItem(_ref) {
17
+ let {
18
+ scope,
19
+ name,
20
+ label,
21
+ info,
22
+ messageActivated,
23
+ messageDeactivated,
24
+ shortcut
25
+ } = _ref;
26
+ const isActive = useSelect(select => !!select(preferencesStore).get(scope, name), [name]);
27
+ const {
28
+ toggle
29
+ } = useDispatch(preferencesStore);
30
+
31
+ const speakMessage = () => {
32
+ if (isActive) {
33
+ const message = messageDeactivated || sprintf(
34
+ /* translators: %s: preference name, e.g. 'Fullscreen mode' */
35
+ __('Preference deactivated - %s'), label);
36
+ speak(message);
37
+ } else {
38
+ const message = messageActivated || sprintf(
39
+ /* translators: %s: preference name, e.g. 'Fullscreen mode' */
40
+ __('Preference activated - %s'), label);
41
+ speak(message);
42
+ }
43
+ };
44
+
45
+ return createElement(MenuItem, {
46
+ icon: isActive && check,
47
+ isSelected: isActive,
48
+ onClick: () => {
49
+ toggle(scope, name);
50
+ speakMessage();
51
+ },
52
+ role: "menuitemcheckbox",
53
+ info: info,
54
+ shortcut: shortcut
55
+ }, label);
56
+ }
57
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences/src/components/preference-toggle-menu-item/index.js"],"names":["useSelect","useDispatch","MenuItem","__","sprintf","check","speak","store","preferencesStore","PreferenceToggleMenuItem","scope","name","label","info","messageActivated","messageDeactivated","shortcut","isActive","select","get","toggle","speakMessage","message"],"mappings":";;AAAA;AACA;AACA;AACA,SAASA,SAAT,EAAoBC,WAApB,QAAuC,iBAAvC;AACA,SAASC,QAAT,QAAyB,uBAAzB;AACA,SAASC,EAAT,EAAaC,OAAb,QAA4B,iBAA5B;AACA,SAASC,KAAT,QAAsB,kBAAtB;AACA,SAASC,KAAT,QAAsB,iBAAtB;AAEA;AACA;AACA;;AACA,SAASC,KAAK,IAAIC,gBAAlB,QAA0C,aAA1C;AAEA,eAAe,SAASC,wBAAT,OAQX;AAAA,MAR8C;AACjDC,IAAAA,KADiD;AAEjDC,IAAAA,IAFiD;AAGjDC,IAAAA,KAHiD;AAIjDC,IAAAA,IAJiD;AAKjDC,IAAAA,gBALiD;AAMjDC,IAAAA,kBANiD;AAOjDC,IAAAA;AAPiD,GAQ9C;AACH,QAAMC,QAAQ,GAAGjB,SAAS,CACvBkB,MAAF,IAAc,CAAC,CAAEA,MAAM,CAAEV,gBAAF,CAAN,CAA2BW,GAA3B,CAAgCT,KAAhC,EAAuCC,IAAvC,CADQ,EAEzB,CAAEA,IAAF,CAFyB,CAA1B;AAIA,QAAM;AAAES,IAAAA;AAAF,MAAanB,WAAW,CAAEO,gBAAF,CAA9B;;AACA,QAAMa,YAAY,GAAG,MAAM;AAC1B,QAAKJ,QAAL,EAAgB;AACf,YAAMK,OAAO,GACZP,kBAAkB,IAClBX,OAAO;AACN;AACAD,MAAAA,EAAE,CAAE,6BAAF,CAFI,EAGNS,KAHM,CAFR;AAOAN,MAAAA,KAAK,CAAEgB,OAAF,CAAL;AACA,KATD,MASO;AACN,YAAMA,OAAO,GACZR,gBAAgB,IAChBV,OAAO;AACN;AACAD,MAAAA,EAAE,CAAE,2BAAF,CAFI,EAGNS,KAHM,CAFR;AAOAN,MAAAA,KAAK,CAAEgB,OAAF,CAAL;AACA;AACD,GApBD;;AAsBA,SACC,cAAC,QAAD;AACC,IAAA,IAAI,EAAGL,QAAQ,IAAIZ,KADpB;AAEC,IAAA,UAAU,EAAGY,QAFd;AAGC,IAAA,OAAO,EAAG,MAAM;AACfG,MAAAA,MAAM,CAAEV,KAAF,EAASC,IAAT,CAAN;AACAU,MAAAA,YAAY;AACZ,KANF;AAOC,IAAA,IAAI,EAAC,kBAPN;AAQC,IAAA,IAAI,EAAGR,IARR;AASC,IAAA,QAAQ,EAAGG;AATZ,KAWGJ,KAXH,CADD;AAeA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useSelect, useDispatch } from '@wordpress/data';\nimport { MenuItem } from '@wordpress/components';\nimport { __, sprintf } from '@wordpress/i18n';\nimport { check } from '@wordpress/icons';\nimport { speak } from '@wordpress/a11y';\n\n/**\n * Internal dependencies\n */\nimport { store as preferencesStore } from '../../store';\n\nexport default function PreferenceToggleMenuItem( {\n\tscope,\n\tname,\n\tlabel,\n\tinfo,\n\tmessageActivated,\n\tmessageDeactivated,\n\tshortcut,\n} ) {\n\tconst isActive = useSelect(\n\t\t( select ) => !! select( preferencesStore ).get( scope, name ),\n\t\t[ name ]\n\t);\n\tconst { toggle } = useDispatch( preferencesStore );\n\tconst speakMessage = () => {\n\t\tif ( isActive ) {\n\t\t\tconst message =\n\t\t\t\tmessageDeactivated ||\n\t\t\t\tsprintf(\n\t\t\t\t\t/* translators: %s: preference name, e.g. 'Fullscreen mode' */\n\t\t\t\t\t__( 'Preference deactivated - %s' ),\n\t\t\t\t\tlabel\n\t\t\t\t);\n\t\t\tspeak( message );\n\t\t} else {\n\t\t\tconst message =\n\t\t\t\tmessageActivated ||\n\t\t\t\tsprintf(\n\t\t\t\t\t/* translators: %s: preference name, e.g. 'Fullscreen mode' */\n\t\t\t\t\t__( 'Preference activated - %s' ),\n\t\t\t\t\tlabel\n\t\t\t\t);\n\t\t\tspeak( message );\n\t\t}\n\t};\n\n\treturn (\n\t\t<MenuItem\n\t\t\ticon={ isActive && check }\n\t\t\tisSelected={ isActive }\n\t\t\tonClick={ () => {\n\t\t\t\ttoggle( scope, name );\n\t\t\t\tspeakMessage();\n\t\t\t} }\n\t\t\trole=\"menuitemcheckbox\"\n\t\t\tinfo={ info }\n\t\t\tshortcut={ shortcut }\n\t\t>\n\t\t\t{ label }\n\t\t</MenuItem>\n\t);\n}\n"]}
@@ -0,0 +1,3 @@
1
+ export * from './components';
2
+ export { store } from './store';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["@wordpress/preferences/src/index.js"],"names":["store"],"mappings":"AAAA,cAAc,cAAd;AACA,SAASA,KAAT,QAAsB,SAAtB","sourcesContent":["export * from './components';\nexport { store } from './store';\n"]}