preferences.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /******/ (function() { // webpackBootstrap
  2. /******/ "use strict";
  3. /******/ // The require scope
  4. /******/ var __webpack_require__ = {};
  5. /******/
  6. /************************************************************************/
  7. /******/ /* webpack/runtime/define property getters */
  8. /******/ !function() {
  9. /******/ // define getter functions for harmony exports
  10. /******/ __webpack_require__.d = function(exports, definition) {
  11. /******/ for(var key in definition) {
  12. /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
  13. /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
  14. /******/ }
  15. /******/ }
  16. /******/ };
  17. /******/ }();
  18. /******/
  19. /******/ /* webpack/runtime/hasOwnProperty shorthand */
  20. /******/ !function() {
  21. /******/ __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
  22. /******/ }();
  23. /******/
  24. /******/ /* webpack/runtime/make namespace object */
  25. /******/ !function() {
  26. /******/ // define __esModule on exports
  27. /******/ __webpack_require__.r = function(exports) {
  28. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  29. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  30. /******/ }
  31. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  32. /******/ };
  33. /******/ }();
  34. /******/
  35. /************************************************************************/
  36. var __webpack_exports__ = {};
  37. // ESM COMPAT FLAG
  38. __webpack_require__.r(__webpack_exports__);
  39. // EXPORTS
  40. __webpack_require__.d(__webpack_exports__, {
  41. "PreferenceToggleMenuItem": function() { return /* reexport */ PreferenceToggleMenuItem; },
  42. "store": function() { return /* reexport */ store; }
  43. });
  44. // NAMESPACE OBJECT: ./node_modules/@wordpress/preferences/build-module/store/actions.js
  45. var actions_namespaceObject = {};
  46. __webpack_require__.r(actions_namespaceObject);
  47. __webpack_require__.d(actions_namespaceObject, {
  48. "set": function() { return set; },
  49. "setDefaults": function() { return setDefaults; },
  50. "setPersistenceLayer": function() { return setPersistenceLayer; },
  51. "toggle": function() { return toggle; }
  52. });
  53. // NAMESPACE OBJECT: ./node_modules/@wordpress/preferences/build-module/store/selectors.js
  54. var selectors_namespaceObject = {};
  55. __webpack_require__.r(selectors_namespaceObject);
  56. __webpack_require__.d(selectors_namespaceObject, {
  57. "get": function() { return get; }
  58. });
  59. ;// CONCATENATED MODULE: external ["wp","element"]
  60. var external_wp_element_namespaceObject = window["wp"]["element"];
  61. ;// CONCATENATED MODULE: external ["wp","data"]
  62. var external_wp_data_namespaceObject = window["wp"]["data"];
  63. ;// CONCATENATED MODULE: external ["wp","components"]
  64. var external_wp_components_namespaceObject = window["wp"]["components"];
  65. ;// CONCATENATED MODULE: external ["wp","i18n"]
  66. var external_wp_i18n_namespaceObject = window["wp"]["i18n"];
  67. ;// CONCATENATED MODULE: external ["wp","primitives"]
  68. var external_wp_primitives_namespaceObject = window["wp"]["primitives"];
  69. ;// CONCATENATED MODULE: ./node_modules/@wordpress/icons/build-module/library/check.js
  70. /**
  71. * WordPress dependencies
  72. */
  73. const check = (0,external_wp_element_namespaceObject.createElement)(external_wp_primitives_namespaceObject.SVG, {
  74. xmlns: "http://www.w3.org/2000/svg",
  75. viewBox: "0 0 24 24"
  76. }, (0,external_wp_element_namespaceObject.createElement)(external_wp_primitives_namespaceObject.Path, {
  77. d: "M16.7 7.1l-6.3 8.5-3.3-2.5-.9 1.2 4.5 3.4L17.9 8z"
  78. }));
  79. /* harmony default export */ var library_check = (check);
  80. ;// CONCATENATED MODULE: external ["wp","a11y"]
  81. var external_wp_a11y_namespaceObject = window["wp"]["a11y"];
  82. ;// CONCATENATED MODULE: ./node_modules/@wordpress/preferences/build-module/store/reducer.js
  83. /**
  84. * WordPress dependencies
  85. */
  86. /**
  87. * Reducer returning the defaults for user preferences.
  88. *
  89. * This is kept intentionally separate from the preferences
  90. * themselves so that defaults are not persisted.
  91. *
  92. * @param {Object} state Current state.
  93. * @param {Object} action Dispatched action.
  94. *
  95. * @return {Object} Updated state.
  96. */
  97. function defaults() {
  98. let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  99. let action = arguments.length > 1 ? arguments[1] : undefined;
  100. if (action.type === 'SET_PREFERENCE_DEFAULTS') {
  101. const {
  102. scope,
  103. defaults: values
  104. } = action;
  105. return { ...state,
  106. [scope]: { ...state[scope],
  107. ...values
  108. }
  109. };
  110. }
  111. return state;
  112. }
  113. /**
  114. * Higher order reducer that does the following:
  115. * - Merges any data from the persistence layer into the state when the
  116. * `SET_PERSISTENCE_LAYER` action is received.
  117. * - Passes any preferences changes to the persistence layer.
  118. *
  119. * @param {Function} reducer The preferences reducer.
  120. *
  121. * @return {Function} The enhanced reducer.
  122. */
  123. function withPersistenceLayer(reducer) {
  124. let persistenceLayer;
  125. return (state, action) => {
  126. // Setup the persistence layer, and return the persisted data
  127. // as the state.
  128. if (action.type === 'SET_PERSISTENCE_LAYER') {
  129. const {
  130. persistenceLayer: persistence,
  131. persistedData
  132. } = action;
  133. persistenceLayer = persistence;
  134. return persistedData;
  135. }
  136. const nextState = reducer(state, action);
  137. if (action.type === 'SET_PREFERENCE_VALUE') {
  138. var _persistenceLayer;
  139. (_persistenceLayer = persistenceLayer) === null || _persistenceLayer === void 0 ? void 0 : _persistenceLayer.set(nextState);
  140. }
  141. return nextState;
  142. };
  143. }
  144. /**
  145. * Reducer returning the user preferences.
  146. *
  147. * @param {Object} state Current state.
  148. * @param {Object} action Dispatched action.
  149. *
  150. * @return {Object} Updated state.
  151. */
  152. const preferences = withPersistenceLayer(function () {
  153. let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  154. let action = arguments.length > 1 ? arguments[1] : undefined;
  155. if (action.type === 'SET_PREFERENCE_VALUE') {
  156. const {
  157. scope,
  158. name,
  159. value
  160. } = action;
  161. return { ...state,
  162. [scope]: { ...state[scope],
  163. [name]: value
  164. }
  165. };
  166. }
  167. return state;
  168. });
  169. /* harmony default export */ var reducer = ((0,external_wp_data_namespaceObject.combineReducers)({
  170. defaults,
  171. preferences
  172. }));
  173. ;// CONCATENATED MODULE: ./node_modules/@wordpress/preferences/build-module/store/actions.js
  174. /**
  175. * Returns an action object used in signalling that a preference should be
  176. * toggled.
  177. *
  178. * @param {string} scope The preference scope (e.g. core/edit-post).
  179. * @param {string} name The preference name.
  180. */
  181. function toggle(scope, name) {
  182. return function (_ref) {
  183. let {
  184. select,
  185. dispatch
  186. } = _ref;
  187. const currentValue = select.get(scope, name);
  188. dispatch.set(scope, name, !currentValue);
  189. };
  190. }
  191. /**
  192. * Returns an action object used in signalling that a preference should be set
  193. * to a value
  194. *
  195. * @param {string} scope The preference scope (e.g. core/edit-post).
  196. * @param {string} name The preference name.
  197. * @param {*} value The value to set.
  198. *
  199. * @return {Object} Action object.
  200. */
  201. function set(scope, name, value) {
  202. return {
  203. type: 'SET_PREFERENCE_VALUE',
  204. scope,
  205. name,
  206. value
  207. };
  208. }
  209. /**
  210. * Returns an action object used in signalling that preference defaults should
  211. * be set.
  212. *
  213. * @param {string} scope The preference scope (e.g. core/edit-post).
  214. * @param {Object<string, *>} defaults A key/value map of preference names to values.
  215. *
  216. * @return {Object} Action object.
  217. */
  218. function setDefaults(scope, defaults) {
  219. return {
  220. type: 'SET_PREFERENCE_DEFAULTS',
  221. scope,
  222. defaults
  223. };
  224. }
  225. /** @typedef {() => Promise<Object>} WPPreferencesPersistenceLayerGet */
  226. /** @typedef {(*) => void} WPPreferencesPersistenceLayerSet */
  227. /**
  228. * @typedef WPPreferencesPersistenceLayer
  229. *
  230. * @property {WPPreferencesPersistenceLayerGet} get An async function that gets data from the persistence layer.
  231. * @property {WPPreferencesPersistenceLayerSet} set A function that sets data in the persistence layer.
  232. */
  233. /**
  234. * Sets the persistence layer.
  235. *
  236. * When a persistence layer is set, the preferences store will:
  237. * - call `get` immediately and update the store state to the value returned.
  238. * - call `set` with all preferences whenever a preference changes value.
  239. *
  240. * `setPersistenceLayer` should ideally be dispatched at the start of an
  241. * application's lifecycle, before any other actions have been dispatched to
  242. * the preferences store.
  243. *
  244. * @param {WPPreferencesPersistenceLayer} persistenceLayer The persistence layer.
  245. *
  246. * @return {Object} Action object.
  247. */
  248. async function setPersistenceLayer(persistenceLayer) {
  249. const persistedData = await persistenceLayer.get();
  250. return {
  251. type: 'SET_PERSISTENCE_LAYER',
  252. persistenceLayer,
  253. persistedData
  254. };
  255. }
  256. ;// CONCATENATED MODULE: ./node_modules/@wordpress/preferences/build-module/store/selectors.js
  257. /**
  258. * Returns a boolean indicating whether a prefer is active for a particular
  259. * scope.
  260. *
  261. * @param {Object} state The store state.
  262. * @param {string} scope The scope of the feature (e.g. core/edit-post).
  263. * @param {string} name The name of the feature.
  264. *
  265. * @return {*} Is the feature enabled?
  266. */
  267. function get(state, scope, name) {
  268. var _state$preferences$sc, _state$defaults$scope;
  269. const value = (_state$preferences$sc = state.preferences[scope]) === null || _state$preferences$sc === void 0 ? void 0 : _state$preferences$sc[name];
  270. return value !== undefined ? value : (_state$defaults$scope = state.defaults[scope]) === null || _state$defaults$scope === void 0 ? void 0 : _state$defaults$scope[name];
  271. }
  272. ;// CONCATENATED MODULE: ./node_modules/@wordpress/preferences/build-module/store/constants.js
  273. /**
  274. * The identifier for the data store.
  275. *
  276. * @type {string}
  277. */
  278. const STORE_NAME = 'core/preferences';
  279. ;// CONCATENATED MODULE: ./node_modules/@wordpress/preferences/build-module/store/index.js
  280. /**
  281. * WordPress dependencies
  282. */
  283. /**
  284. * Internal dependencies
  285. */
  286. /**
  287. * Store definition for the interface namespace.
  288. *
  289. * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
  290. *
  291. * @type {Object}
  292. */
  293. const store = (0,external_wp_data_namespaceObject.createReduxStore)(STORE_NAME, {
  294. reducer: reducer,
  295. actions: actions_namespaceObject,
  296. selectors: selectors_namespaceObject
  297. });
  298. (0,external_wp_data_namespaceObject.register)(store);
  299. ;// CONCATENATED MODULE: ./node_modules/@wordpress/preferences/build-module/components/preference-toggle-menu-item/index.js
  300. /**
  301. * WordPress dependencies
  302. */
  303. /**
  304. * Internal dependencies
  305. */
  306. function PreferenceToggleMenuItem(_ref) {
  307. let {
  308. scope,
  309. name,
  310. label,
  311. info,
  312. messageActivated,
  313. messageDeactivated,
  314. shortcut
  315. } = _ref;
  316. const isActive = (0,external_wp_data_namespaceObject.useSelect)(select => !!select(store).get(scope, name), [name]);
  317. const {
  318. toggle
  319. } = (0,external_wp_data_namespaceObject.useDispatch)(store);
  320. const speakMessage = () => {
  321. if (isActive) {
  322. const message = messageDeactivated || (0,external_wp_i18n_namespaceObject.sprintf)(
  323. /* translators: %s: preference name, e.g. 'Fullscreen mode' */
  324. (0,external_wp_i18n_namespaceObject.__)('Preference deactivated - %s'), label);
  325. (0,external_wp_a11y_namespaceObject.speak)(message);
  326. } else {
  327. const message = messageActivated || (0,external_wp_i18n_namespaceObject.sprintf)(
  328. /* translators: %s: preference name, e.g. 'Fullscreen mode' */
  329. (0,external_wp_i18n_namespaceObject.__)('Preference activated - %s'), label);
  330. (0,external_wp_a11y_namespaceObject.speak)(message);
  331. }
  332. };
  333. return (0,external_wp_element_namespaceObject.createElement)(external_wp_components_namespaceObject.MenuItem, {
  334. icon: isActive && library_check,
  335. isSelected: isActive,
  336. onClick: () => {
  337. toggle(scope, name);
  338. speakMessage();
  339. },
  340. role: "menuitemcheckbox",
  341. info: info,
  342. shortcut: shortcut
  343. }, label);
  344. }
  345. ;// CONCATENATED MODULE: ./node_modules/@wordpress/preferences/build-module/components/index.js
  346. ;// CONCATENATED MODULE: ./node_modules/@wordpress/preferences/build-module/index.js
  347. (window.wp = window.wp || {}).preferences = __webpack_exports__;
  348. /******/ })()
  349. ;