notices.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. "store": function() { return /* reexport */ store; }
  42. });
  43. // NAMESPACE OBJECT: ./node_modules/@wordpress/notices/build-module/store/actions.js
  44. var actions_namespaceObject = {};
  45. __webpack_require__.r(actions_namespaceObject);
  46. __webpack_require__.d(actions_namespaceObject, {
  47. "createErrorNotice": function() { return createErrorNotice; },
  48. "createInfoNotice": function() { return createInfoNotice; },
  49. "createNotice": function() { return createNotice; },
  50. "createSuccessNotice": function() { return createSuccessNotice; },
  51. "createWarningNotice": function() { return createWarningNotice; },
  52. "removeNotice": function() { return removeNotice; }
  53. });
  54. // NAMESPACE OBJECT: ./node_modules/@wordpress/notices/build-module/store/selectors.js
  55. var selectors_namespaceObject = {};
  56. __webpack_require__.r(selectors_namespaceObject);
  57. __webpack_require__.d(selectors_namespaceObject, {
  58. "getNotices": function() { return getNotices; }
  59. });
  60. ;// CONCATENATED MODULE: external ["wp","data"]
  61. var external_wp_data_namespaceObject = window["wp"]["data"];
  62. ;// CONCATENATED MODULE: ./node_modules/@wordpress/notices/build-module/store/utils/on-sub-key.js
  63. /**
  64. * Higher-order reducer creator which creates a combined reducer object, keyed
  65. * by a property on the action object.
  66. *
  67. * @param {string} actionProperty Action property by which to key object.
  68. *
  69. * @return {Function} Higher-order reducer.
  70. */
  71. const onSubKey = actionProperty => reducer => function () {
  72. let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  73. let action = arguments.length > 1 ? arguments[1] : undefined;
  74. // Retrieve subkey from action. Do not track if undefined; useful for cases
  75. // where reducer is scoped by action shape.
  76. const key = action[actionProperty];
  77. if (key === undefined) {
  78. return state;
  79. } // Avoid updating state if unchanged. Note that this also accounts for a
  80. // reducer which returns undefined on a key which is not yet tracked.
  81. const nextKeyState = reducer(state[key], action);
  82. if (nextKeyState === state[key]) {
  83. return state;
  84. }
  85. return { ...state,
  86. [key]: nextKeyState
  87. };
  88. };
  89. /* harmony default export */ var on_sub_key = (onSubKey);
  90. ;// CONCATENATED MODULE: ./node_modules/@wordpress/notices/build-module/store/reducer.js
  91. /**
  92. * Internal dependencies
  93. */
  94. /**
  95. * Reducer returning the next notices state. The notices state is an object
  96. * where each key is a context, its value an array of notice objects.
  97. *
  98. * @param {Object} state Current state.
  99. * @param {Object} action Dispatched action.
  100. *
  101. * @return {Object} Updated state.
  102. */
  103. const notices = on_sub_key('context')(function () {
  104. let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  105. let action = arguments.length > 1 ? arguments[1] : undefined;
  106. switch (action.type) {
  107. case 'CREATE_NOTICE':
  108. // Avoid duplicates on ID.
  109. return [...state.filter(_ref => {
  110. let {
  111. id
  112. } = _ref;
  113. return id !== action.notice.id;
  114. }), action.notice];
  115. case 'REMOVE_NOTICE':
  116. return state.filter(_ref2 => {
  117. let {
  118. id
  119. } = _ref2;
  120. return id !== action.id;
  121. });
  122. }
  123. return state;
  124. });
  125. /* harmony default export */ var reducer = (notices);
  126. ;// CONCATENATED MODULE: ./node_modules/@wordpress/notices/build-module/store/constants.js
  127. /**
  128. * Default context to use for notice grouping when not otherwise specified. Its
  129. * specific value doesn't hold much meaning, but it must be reasonably unique
  130. * and, more importantly, referenced consistently in the store implementation.
  131. *
  132. * @type {string}
  133. */
  134. const DEFAULT_CONTEXT = 'global';
  135. /**
  136. * Default notice status.
  137. *
  138. * @type {string}
  139. */
  140. const DEFAULT_STATUS = 'info';
  141. ;// CONCATENATED MODULE: ./node_modules/@wordpress/notices/build-module/store/actions.js
  142. /**
  143. * Internal dependencies
  144. */
  145. /**
  146. * @typedef {Object} WPNoticeAction Object describing a user action option associated with a notice.
  147. *
  148. * @property {string} label Message to use as action label.
  149. * @property {?string} url Optional URL of resource if action incurs
  150. * browser navigation.
  151. * @property {?Function} onClick Optional function to invoke when action is
  152. * triggered by user.
  153. *
  154. */
  155. let uniqueId = 0;
  156. /**
  157. * Returns an action object used in signalling that a notice is to be created.
  158. *
  159. * @param {string} [status='info'] Notice status.
  160. * @param {string} content Notice message.
  161. * @param {Object} [options] Notice options.
  162. * @param {string} [options.context='global'] Context under which to
  163. * group notice.
  164. * @param {string} [options.id] Identifier for notice.
  165. * Automatically assigned
  166. * if not specified.
  167. * @param {boolean} [options.isDismissible=true] Whether the notice can
  168. * be dismissed by user.
  169. * @param {string} [options.type='default'] Type of notice, one of
  170. * `default`, or `snackbar`.
  171. * @param {boolean} [options.speak=true] Whether the notice
  172. * content should be
  173. * announced to screen
  174. * readers.
  175. * @param {Array<WPNoticeAction>} [options.actions] User actions to be
  176. * presented with notice.
  177. * @param {string} [options.icon] An icon displayed with the notice.
  178. * Only used when type is set to `snackbar`.
  179. * @param {boolean} [options.explicitDismiss] Whether the notice includes
  180. * an explicit dismiss button and
  181. * can't be dismissed by clicking
  182. * the body of the notice. Only applies
  183. * when type is set to `snackbar`.
  184. * @param {Function} [options.onDismiss] Called when the notice is dismissed.
  185. *
  186. * @example
  187. * ```js
  188. * import { __ } from '@wordpress/i18n';
  189. * import { useDispatch } from '@wordpress/data';
  190. * import { store as noticesStore } from '@wordpress/notices';
  191. * import { Button } from '@wordpress/components';
  192. *
  193. * const ExampleComponent = () => {
  194. * const { createNotice } = useDispatch( noticesStore );
  195. * return (
  196. * <Button
  197. * onClick={ () => createNotice( 'success', __( 'Notice message' ) ) }
  198. * >
  199. * { __( 'Generate a success notice!' ) }
  200. * </Button>
  201. * );
  202. * };
  203. * ```
  204. *
  205. * @return {Object} Action object.
  206. */
  207. function createNotice() {
  208. let status = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : DEFAULT_STATUS;
  209. let content = arguments.length > 1 ? arguments[1] : undefined;
  210. let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  211. const {
  212. speak = true,
  213. isDismissible = true,
  214. context = DEFAULT_CONTEXT,
  215. id = `${context}${++uniqueId}`,
  216. actions = [],
  217. type = 'default',
  218. __unstableHTML,
  219. icon = null,
  220. explicitDismiss = false,
  221. onDismiss
  222. } = options; // The supported value shape of content is currently limited to plain text
  223. // strings. To avoid setting expectation that e.g. a WPElement could be
  224. // supported, cast to a string.
  225. content = String(content);
  226. return {
  227. type: 'CREATE_NOTICE',
  228. context,
  229. notice: {
  230. id,
  231. status,
  232. content,
  233. spokenMessage: speak ? content : null,
  234. __unstableHTML,
  235. isDismissible,
  236. actions,
  237. type,
  238. icon,
  239. explicitDismiss,
  240. onDismiss
  241. }
  242. };
  243. }
  244. /**
  245. * Returns an action object used in signalling that a success notice is to be
  246. * created. Refer to `createNotice` for options documentation.
  247. *
  248. * @see createNotice
  249. *
  250. * @param {string} content Notice message.
  251. * @param {Object} [options] Optional notice options.
  252. *
  253. * @example
  254. * ```js
  255. * import { __ } from '@wordpress/i18n';
  256. * import { useDispatch } from '@wordpress/data';
  257. * import { store as noticesStore } from '@wordpress/notices';
  258. * import { Button } from '@wordpress/components';
  259. *
  260. * const ExampleComponent = () => {
  261. * const { createSuccessNotice } = useDispatch( noticesStore );
  262. * return (
  263. * <Button
  264. * onClick={ () =>
  265. * createSuccessNotice( __( 'Success!' ), {
  266. * type: 'snackbar',
  267. * icon: '🔥',
  268. * } )
  269. * }
  270. * >
  271. * { __( 'Generate a snackbar success notice!' ) }
  272. * </Button>
  273. * );
  274. * };
  275. * ```
  276. *
  277. * @return {Object} Action object.
  278. */
  279. function createSuccessNotice(content, options) {
  280. return createNotice('success', content, options);
  281. }
  282. /**
  283. * Returns an action object used in signalling that an info notice is to be
  284. * created. Refer to `createNotice` for options documentation.
  285. *
  286. * @see createNotice
  287. *
  288. * @param {string} content Notice message.
  289. * @param {Object} [options] Optional notice options.
  290. *
  291. * @example
  292. * ```js
  293. * import { __ } from '@wordpress/i18n';
  294. * import { useDispatch } from '@wordpress/data';
  295. * import { store as noticesStore } from '@wordpress/notices';
  296. * import { Button } from '@wordpress/components';
  297. *
  298. * const ExampleComponent = () => {
  299. * const { createInfoNotice } = useDispatch( noticesStore );
  300. * return (
  301. * <Button
  302. * onClick={ () =>
  303. * createInfoNotice( __( 'Something happened!' ), {
  304. * isDismissible: false,
  305. * } )
  306. * }
  307. * >
  308. * { __( 'Generate a notice that cannot be dismissed.' ) }
  309. * </Button>
  310. * );
  311. * };
  312. *```
  313. *
  314. * @return {Object} Action object.
  315. */
  316. function createInfoNotice(content, options) {
  317. return createNotice('info', content, options);
  318. }
  319. /**
  320. * Returns an action object used in signalling that an error notice is to be
  321. * created. Refer to `createNotice` for options documentation.
  322. *
  323. * @see createNotice
  324. *
  325. * @param {string} content Notice message.
  326. * @param {Object} [options] Optional notice options.
  327. *
  328. * @example
  329. * ```js
  330. * import { __ } from '@wordpress/i18n';
  331. * import { useDispatch } from '@wordpress/data';
  332. * import { store as noticesStore } from '@wordpress/notices';
  333. * import { Button } from '@wordpress/components';
  334. *
  335. * const ExampleComponent = () => {
  336. * const { createErrorNotice } = useDispatch( noticesStore );
  337. * return (
  338. * <Button
  339. * onClick={ () =>
  340. * createErrorNotice( __( 'An error occurred!' ), {
  341. * type: 'snackbar',
  342. * explicitDismiss: true,
  343. * } )
  344. * }
  345. * >
  346. * { __(
  347. * 'Generate an snackbar error notice with explicit dismiss button.'
  348. * ) }
  349. * </Button>
  350. * );
  351. * };
  352. * ```
  353. *
  354. * @return {Object} Action object.
  355. */
  356. function createErrorNotice(content, options) {
  357. return createNotice('error', content, options);
  358. }
  359. /**
  360. * Returns an action object used in signalling that a warning notice is to be
  361. * created. Refer to `createNotice` for options documentation.
  362. *
  363. * @see createNotice
  364. *
  365. * @param {string} content Notice message.
  366. * @param {Object} [options] Optional notice options.
  367. *
  368. * @example
  369. * ```js
  370. * import { __ } from '@wordpress/i18n';
  371. * import { useDispatch } from '@wordpress/data';
  372. * import { store as noticesStore } from '@wordpress/notices';
  373. * import { Button } from '@wordpress/components';
  374. *
  375. * const ExampleComponent = () => {
  376. * const { createWarningNotice, createInfoNotice } = useDispatch( noticesStore );
  377. * return (
  378. * <Button
  379. * onClick={ () =>
  380. * createWarningNotice( __( 'Warning!' ), {
  381. * onDismiss: () => {
  382. * createInfoNotice(
  383. * __( 'The warning has been dismissed!' )
  384. * );
  385. * },
  386. * } )
  387. * }
  388. * >
  389. * { __( 'Generates a warning notice with onDismiss callback' ) }
  390. * </Button>
  391. * );
  392. * };
  393. * ```
  394. *
  395. * @return {Object} Action object.
  396. */
  397. function createWarningNotice(content, options) {
  398. return createNotice('warning', content, options);
  399. }
  400. /**
  401. * Returns an action object used in signalling that a notice is to be removed.
  402. *
  403. * @param {string} id Notice unique identifier.
  404. * @param {string} [context='global'] Optional context (grouping) in which the notice is
  405. * intended to appear. Defaults to default context.
  406. *
  407. * @example
  408. * ```js
  409. * import { __ } from '@wordpress/i18n';
  410. * import { useDispatch } from '@wordpress/data';
  411. * import { store as noticesStore } from '@wordpress/notices';
  412. * import { Button } from '@wordpress/components';
  413. *
  414. * const ExampleComponent = () => {
  415. * const notices = useSelect( ( select ) => select( noticesStore ).getNotices() );
  416. * const { createWarningNotice, removeNotice } = useDispatch( noticesStore );
  417. *
  418. * return (
  419. * <>
  420. * <Button
  421. * onClick={ () =>
  422. * createWarningNotice( __( 'Warning!' ), {
  423. * isDismissible: false,
  424. * } )
  425. * }
  426. * >
  427. * { __( 'Generate a notice' ) }
  428. * </Button>
  429. * { notices.length > 0 && (
  430. * <Button onClick={ () => removeNotice( notices[ 0 ].id ) }>
  431. * { __( 'Remove the notice' ) }
  432. * </Button>
  433. * ) }
  434. * </>
  435. * );
  436. *};
  437. * ```
  438. *
  439. * @return {Object} Action object.
  440. */
  441. function removeNotice(id) {
  442. let context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_CONTEXT;
  443. return {
  444. type: 'REMOVE_NOTICE',
  445. id,
  446. context
  447. };
  448. }
  449. ;// CONCATENATED MODULE: ./node_modules/@wordpress/notices/build-module/store/selectors.js
  450. /**
  451. * Internal dependencies
  452. */
  453. /** @typedef {import('./actions').WPNoticeAction} WPNoticeAction */
  454. /**
  455. * The default empty set of notices to return when there are no notices
  456. * assigned for a given notices context. This can occur if the getNotices
  457. * selector is called without a notice ever having been created for the
  458. * context. A shared value is used to ensure referential equality between
  459. * sequential selector calls, since otherwise `[] !== []`.
  460. *
  461. * @type {Array}
  462. */
  463. const DEFAULT_NOTICES = [];
  464. /**
  465. * @typedef {Object} WPNotice Notice object.
  466. *
  467. * @property {string} id Unique identifier of notice.
  468. * @property {string} status Status of notice, one of `success`,
  469. * `info`, `error`, or `warning`. Defaults
  470. * to `info`.
  471. * @property {string} content Notice message.
  472. * @property {string} spokenMessage Audibly announced message text used by
  473. * assistive technologies.
  474. * @property {string} __unstableHTML Notice message as raw HTML. Intended to
  475. * serve primarily for compatibility of
  476. * server-rendered notices, and SHOULD NOT
  477. * be used for notices. It is subject to
  478. * removal without notice.
  479. * @property {boolean} isDismissible Whether the notice can be dismissed by
  480. * user. Defaults to `true`.
  481. * @property {string} type Type of notice, one of `default`,
  482. * or `snackbar`. Defaults to `default`.
  483. * @property {boolean} speak Whether the notice content should be
  484. * announced to screen readers. Defaults to
  485. * `true`.
  486. * @property {WPNoticeAction[]} actions User actions to present with notice.
  487. *
  488. */
  489. /**
  490. * Returns all notices as an array, optionally for a given context. Defaults to
  491. * the global context.
  492. *
  493. * @param {Object} state Notices state.
  494. * @param {?string} context Optional grouping context.
  495. *
  496. * @example
  497. *
  498. *```js
  499. * import { useSelect } from '@wordpress/data';
  500. * import { store as noticesStore } from '@wordpress/notices';
  501. *
  502. * const ExampleComponent = () => {
  503. * const notices = useSelect( ( select ) => select( noticesStore ).getNotices() );
  504. * return (
  505. * <ul>
  506. * { notices.map( ( notice ) => (
  507. * <li key={ notice.ID }>{ notice.content }</li>
  508. * ) ) }
  509. * </ul>
  510. * )
  511. * };
  512. *```
  513. *
  514. * @return {WPNotice[]} Array of notices.
  515. */
  516. function getNotices(state) {
  517. let context = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_CONTEXT;
  518. return state[context] || DEFAULT_NOTICES;
  519. }
  520. ;// CONCATENATED MODULE: ./node_modules/@wordpress/notices/build-module/store/index.js
  521. /**
  522. * WordPress dependencies
  523. */
  524. /**
  525. * Internal dependencies
  526. */
  527. /**
  528. * Store definition for the notices namespace.
  529. *
  530. * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/data/README.md#createReduxStore
  531. *
  532. * @type {Object}
  533. */
  534. const store = (0,external_wp_data_namespaceObject.createReduxStore)('core/notices', {
  535. reducer: reducer,
  536. actions: actions_namespaceObject,
  537. selectors: selectors_namespaceObject
  538. });
  539. (0,external_wp_data_namespaceObject.register)(store);
  540. ;// CONCATENATED MODULE: ./node_modules/@wordpress/notices/build-module/index.js
  541. (window.wp = window.wp || {}).notices = __webpack_exports__;
  542. /******/ })()
  543. ;