index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. "use strict";
  2. "use client";
  3. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  4. var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
  5. Object.defineProperty(exports, "__esModule", {
  6. value: true
  7. });
  8. exports.default = exports.actWrapper = exports.actDestroy = void 0;
  9. var _react = _interopRequireWildcard(require("react"));
  10. var _context = require("../app/context");
  11. var _configProvider = _interopRequireWildcard(require("../config-provider"));
  12. var _UnstableContext = require("../config-provider/UnstableContext");
  13. var _PurePanel = _interopRequireDefault(require("./PurePanel"));
  14. var _useNotification = _interopRequireWildcard(require("./useNotification"));
  15. let notification = null;
  16. let act = callback => callback();
  17. let taskQueue = [];
  18. let defaultGlobalConfig = {};
  19. function getGlobalContext() {
  20. const {
  21. getContainer,
  22. rtl,
  23. maxCount,
  24. top,
  25. bottom,
  26. showProgress,
  27. pauseOnHover
  28. } = defaultGlobalConfig;
  29. const mergedContainer = (getContainer === null || getContainer === void 0 ? void 0 : getContainer()) || document.body;
  30. return {
  31. getContainer: () => mergedContainer,
  32. rtl,
  33. maxCount,
  34. top,
  35. bottom,
  36. showProgress,
  37. pauseOnHover
  38. };
  39. }
  40. const GlobalHolder = /*#__PURE__*/_react.default.forwardRef((props, ref) => {
  41. const {
  42. notificationConfig,
  43. sync
  44. } = props;
  45. const {
  46. getPrefixCls
  47. } = (0, _react.useContext)(_configProvider.ConfigContext);
  48. const prefixCls = defaultGlobalConfig.prefixCls || getPrefixCls('notification');
  49. const appConfig = (0, _react.useContext)(_context.AppConfigContext);
  50. const [api, holder] = (0, _useNotification.useInternalNotification)(Object.assign(Object.assign(Object.assign({}, notificationConfig), {
  51. prefixCls
  52. }), appConfig.notification));
  53. _react.default.useEffect(sync, []);
  54. _react.default.useImperativeHandle(ref, () => {
  55. const instance = Object.assign({}, api);
  56. Object.keys(instance).forEach(method => {
  57. instance[method] = (...args) => {
  58. sync();
  59. return api[method].apply(api, args);
  60. };
  61. });
  62. return {
  63. instance,
  64. sync
  65. };
  66. });
  67. return holder;
  68. });
  69. const GlobalHolderWrapper = /*#__PURE__*/_react.default.forwardRef((_, ref) => {
  70. const [notificationConfig, setNotificationConfig] = _react.default.useState(getGlobalContext);
  71. const sync = () => {
  72. setNotificationConfig(getGlobalContext);
  73. };
  74. _react.default.useEffect(sync, []);
  75. const global = (0, _configProvider.globalConfig)();
  76. const rootPrefixCls = global.getRootPrefixCls();
  77. const rootIconPrefixCls = global.getIconPrefixCls();
  78. const theme = global.getTheme();
  79. const dom = /*#__PURE__*/_react.default.createElement(GlobalHolder, {
  80. ref: ref,
  81. sync: sync,
  82. notificationConfig: notificationConfig
  83. });
  84. return /*#__PURE__*/_react.default.createElement(_configProvider.default, {
  85. prefixCls: rootPrefixCls,
  86. iconPrefixCls: rootIconPrefixCls,
  87. theme: theme
  88. }, global.holderRender ? global.holderRender(dom) : dom);
  89. });
  90. const flushNotificationQueue = () => {
  91. if (!notification) {
  92. const holderFragment = document.createDocumentFragment();
  93. const newNotification = {
  94. fragment: holderFragment
  95. };
  96. notification = newNotification;
  97. // Delay render to avoid sync issue
  98. act(() => {
  99. const reactRender = (0, _UnstableContext.unstableSetRender)();
  100. reactRender(/*#__PURE__*/_react.default.createElement(GlobalHolderWrapper, {
  101. ref: node => {
  102. const {
  103. instance,
  104. sync
  105. } = node || {};
  106. Promise.resolve().then(() => {
  107. if (!newNotification.instance && instance) {
  108. newNotification.instance = instance;
  109. newNotification.sync = sync;
  110. flushNotificationQueue();
  111. }
  112. });
  113. }
  114. }), holderFragment);
  115. });
  116. return;
  117. }
  118. // Notification not ready
  119. if (!notification.instance) {
  120. return;
  121. }
  122. // >>> Execute task
  123. taskQueue.forEach(task => {
  124. switch (task.type) {
  125. case 'open':
  126. {
  127. act(() => {
  128. notification.instance.open(Object.assign(Object.assign({}, defaultGlobalConfig), task.config));
  129. });
  130. break;
  131. }
  132. case 'destroy':
  133. act(() => {
  134. var _a;
  135. (_a = notification === null || notification === void 0 ? void 0 : notification.instance) === null || _a === void 0 ? void 0 : _a.destroy(task.key);
  136. });
  137. break;
  138. }
  139. });
  140. // Clean up
  141. taskQueue = [];
  142. };
  143. // ==============================================================================
  144. // == Export ==
  145. // ==============================================================================
  146. function setNotificationGlobalConfig(config) {
  147. defaultGlobalConfig = Object.assign(Object.assign({}, defaultGlobalConfig), config);
  148. // Trigger sync for it
  149. act(() => {
  150. var _a;
  151. (_a = notification === null || notification === void 0 ? void 0 : notification.sync) === null || _a === void 0 ? void 0 : _a.call(notification);
  152. });
  153. }
  154. function open(config) {
  155. const global = (0, _configProvider.globalConfig)();
  156. if (process.env.NODE_ENV !== 'production' && !global.holderRender) {
  157. (0, _configProvider.warnContext)('notification');
  158. }
  159. taskQueue.push({
  160. type: 'open',
  161. config
  162. });
  163. flushNotificationQueue();
  164. }
  165. const destroy = key => {
  166. taskQueue.push({
  167. type: 'destroy',
  168. key
  169. });
  170. flushNotificationQueue();
  171. };
  172. const methods = ['success', 'info', 'warning', 'error'];
  173. const baseStaticMethods = {
  174. open,
  175. destroy,
  176. config: setNotificationGlobalConfig,
  177. useNotification: _useNotification.default,
  178. _InternalPanelDoNotUseOrYouWillBeFired: _PurePanel.default
  179. };
  180. const staticMethods = baseStaticMethods;
  181. methods.forEach(type => {
  182. staticMethods[type] = config => open(Object.assign(Object.assign({}, config), {
  183. type
  184. }));
  185. });
  186. // ==============================================================================
  187. // == Test ==
  188. // ==============================================================================
  189. const noop = () => {};
  190. let _actWrapper = noop;
  191. if (process.env.NODE_ENV === 'test') {
  192. _actWrapper = wrapper => {
  193. act = wrapper;
  194. };
  195. }
  196. const actWrapper = exports.actWrapper = _actWrapper;
  197. let _actDestroy = noop;
  198. if (process.env.NODE_ENV === 'test') {
  199. _actDestroy = () => {
  200. notification = null;
  201. };
  202. }
  203. const actDestroy = exports.actDestroy = _actDestroy;
  204. var _default = exports.default = staticMethods;