| 1 | function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|---|
| 2 | function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|---|
| 3 | function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|---|
| 4 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|---|
| 5 | function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|---|
| 6 | /**
|
|---|
| 7 | * This slice contains a registry of z-index values for various components.
|
|---|
| 8 | * The state is a map from z-index numbers to element references.
|
|---|
| 9 | */
|
|---|
| 10 | import { createSlice, prepareAutoBatched } from '@reduxjs/toolkit';
|
|---|
| 11 | import { castDraft } from 'immer';
|
|---|
| 12 | import { DefaultZIndexes } from '../zIndex/DefaultZIndexes';
|
|---|
| 13 | var seed = {};
|
|---|
| 14 | var initialState = {
|
|---|
| 15 | zIndexMap: Object.values(DefaultZIndexes).reduce((acc, current) => _objectSpread(_objectSpread({}, acc), {}, {
|
|---|
| 16 | [current]: {
|
|---|
| 17 | element: undefined,
|
|---|
| 18 | panoramaElement: undefined,
|
|---|
| 19 | consumers: 0
|
|---|
| 20 | }
|
|---|
| 21 | }), seed)
|
|---|
| 22 | };
|
|---|
| 23 | var defaultZIndexSet = new Set(Object.values(DefaultZIndexes));
|
|---|
| 24 | function isDefaultZIndex(zIndex) {
|
|---|
| 25 | return defaultZIndexSet.has(zIndex);
|
|---|
| 26 | }
|
|---|
| 27 | var zIndexSlice = createSlice({
|
|---|
| 28 | name: 'zIndex',
|
|---|
| 29 | initialState,
|
|---|
| 30 | reducers: {
|
|---|
| 31 | registerZIndexPortal: {
|
|---|
| 32 | reducer: (state, action) => {
|
|---|
| 33 | var {
|
|---|
| 34 | zIndex
|
|---|
| 35 | } = action.payload;
|
|---|
| 36 | if (state.zIndexMap[zIndex]) {
|
|---|
| 37 | state.zIndexMap[zIndex].consumers += 1;
|
|---|
| 38 | } else {
|
|---|
| 39 | state.zIndexMap[zIndex] = {
|
|---|
| 40 | consumers: 1,
|
|---|
| 41 | element: undefined,
|
|---|
| 42 | panoramaElement: undefined
|
|---|
| 43 | };
|
|---|
| 44 | }
|
|---|
| 45 | },
|
|---|
| 46 | prepare: prepareAutoBatched()
|
|---|
| 47 | },
|
|---|
| 48 | unregisterZIndexPortal: {
|
|---|
| 49 | reducer: (state, action) => {
|
|---|
| 50 | var {
|
|---|
| 51 | zIndex
|
|---|
| 52 | } = action.payload;
|
|---|
| 53 | if (state.zIndexMap[zIndex]) {
|
|---|
| 54 | state.zIndexMap[zIndex].consumers -= 1;
|
|---|
| 55 | /*
|
|---|
| 56 | * Garbage collect unused z-index entries, except for default z-indexes.
|
|---|
| 57 | * Default z-indexes are always rendered, regardless of whether there are consumers or not.
|
|---|
| 58 | * And because of that, even if we delete this entry, the ZIndexPortal provider will still be rendered
|
|---|
| 59 | * and React is not going to re-create it, and it won't re-register the element ID.
|
|---|
| 60 | * So let's not delete default z-index entries.
|
|---|
| 61 | */
|
|---|
| 62 | if (state.zIndexMap[zIndex].consumers <= 0 && !isDefaultZIndex(zIndex)) {
|
|---|
| 63 | delete state.zIndexMap[zIndex];
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 | },
|
|---|
| 67 | prepare: prepareAutoBatched()
|
|---|
| 68 | },
|
|---|
| 69 | registerZIndexPortalElement: {
|
|---|
| 70 | reducer: (state, action) => {
|
|---|
| 71 | var {
|
|---|
| 72 | zIndex,
|
|---|
| 73 | element,
|
|---|
| 74 | isPanorama
|
|---|
| 75 | } = action.payload;
|
|---|
| 76 | if (state.zIndexMap[zIndex]) {
|
|---|
| 77 | if (isPanorama) {
|
|---|
| 78 | state.zIndexMap[zIndex].panoramaElement = castDraft(element);
|
|---|
| 79 | } else {
|
|---|
| 80 | state.zIndexMap[zIndex].element = castDraft(element);
|
|---|
| 81 | }
|
|---|
| 82 | } else {
|
|---|
| 83 | state.zIndexMap[zIndex] = {
|
|---|
| 84 | consumers: 0,
|
|---|
| 85 | element: isPanorama ? undefined : castDraft(element),
|
|---|
| 86 | panoramaElement: isPanorama ? castDraft(element) : undefined
|
|---|
| 87 | };
|
|---|
| 88 | }
|
|---|
| 89 | },
|
|---|
| 90 | prepare: prepareAutoBatched()
|
|---|
| 91 | },
|
|---|
| 92 | unregisterZIndexPortalElement: {
|
|---|
| 93 | reducer: (state, action) => {
|
|---|
| 94 | var {
|
|---|
| 95 | zIndex
|
|---|
| 96 | } = action.payload;
|
|---|
| 97 | if (state.zIndexMap[zIndex]) {
|
|---|
| 98 | if (action.payload.isPanorama) {
|
|---|
| 99 | state.zIndexMap[zIndex].panoramaElement = undefined;
|
|---|
| 100 | } else {
|
|---|
| 101 | state.zIndexMap[zIndex].element = undefined;
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 | },
|
|---|
| 105 | prepare: prepareAutoBatched()
|
|---|
| 106 | }
|
|---|
| 107 | }
|
|---|
| 108 | });
|
|---|
| 109 | export var {
|
|---|
| 110 | registerZIndexPortal,
|
|---|
| 111 | unregisterZIndexPortal,
|
|---|
| 112 | registerZIndexPortalElement,
|
|---|
| 113 | unregisterZIndexPortalElement
|
|---|
| 114 | } = zIndexSlice.actions;
|
|---|
| 115 | export var zIndexReducer = zIndexSlice.reducer; |
|---|