ketcher-react
Version:
Web-based molecule sketcher
1,356 lines (1,335 loc) • 3.24 MB
JavaScript
/****************************************************************************
* Copyright 2021 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { jsx, jsxs, Fragment } from '@emotion/react/jsx-runtime';
import { Provider, useDispatch, useSelector } from 'react-redux';
import * as React from 'react';
import React__default, { memo, createContext, useRef, useEffect, useState, useCallback, useMemo, useLayoutEffect, useContext } from 'react';
import { ThemeProvider, Global, css, useTheme } from '@emotion/react';
import { createTheme } from '@mui/material/styles';
import { merge, debounce, flatten, isNumber, get as get$2 } from 'lodash';
import { SettingsManager, CoreEditor, MonomerGroups as MonomerGroups$1, KetMonomerClass, MONOMER_CONST, KetcherLogger, ChemicalMimeType as ChemicalMimeType$1, KetSerializer, generateMenuShortcuts, hotkeysConfiguration, ToolName, SequenceMode, SetEditorLineLengthAction, isAmbiguousMonomerLibraryItem, setAmbiguousMonomerTemplatePrefix, setMonomerTemplatePrefix, macromoleculesFilesInputFormats, EditorHistory, SnakeMode, FlexMode, ModeTypes, ketcherProvider, KetcherAsyncEvents, isTwoStrandedNodeRestrictedForHydrogenBondCreation, UsageInMacromolecule, ZoomTool, SequenceType, HydrogenBond, BackBoneSequenceNode, AmbiguousMonomer, LinkerSequenceNode, Nucleotide, Nucleoside, isLibraryItemRnaPreset, monomerFactory, Entities, Sugar, getRnaBaseFromSugar, RNABase, isRnaBaseOrAmbiguousRnaBase, getSugarFromRnaBase, Peptide, canModifyAminoAcid, getAminoAcidsToModify, compareByTitleWithNaturalFirst, SequenceRenderer, EmptySequenceNode, notifyRequestCompleted, Struct, ChainsCollection, getAllConnectedMonomersRecursively, SnakeLayoutCellWidth, AmbiguousMonomerRenderer, DEFAULT_LAYOUT_MODE, HAS_CONTENT_LAYOUT_MODE, libraryItemHasR1AttachmentPoint, Phosphate, KetAmbiguousMonomerTemplateSubType, RNA_DNA_NON_MODIFIED_PART, MonomerToAtomBond, peptideNaturalAnalogues, rnaDnaNaturalAnalogues, isHelmCompatible, getSvgFromDrawnStructures, isClipboardAPIAvailable, legacyCopy, Vec2, Coordinates } from 'ketcher-core';
import { Icon, Input as Input$1, Button, Accordion, IndigoProvider, StructRender, IconButton, preview, EditorClassName, KETCHER_MACROMOLECULES_ROOT_NODE_SELECTOR, AmbiguousMonomerPreview, calculateBondPreviewPosition, ArrowScroll, getFullscreenElement, calculateAmbiguousMonomerPreviewTop, calculateMonomerPreviewTop, calculateNucleoElementPreviewTop, usePortalStyle } from 'ketcher-react';
import { Tabs as Tabs$2, Tab, Dialog, DialogTitle, DialogContent, DialogActions, FormControl, Select, MenuItem as MenuItem$1, Button as Button$1, Collapse, Popover, Snackbar, IconButton as IconButton$1, ListItemText, Box, ButtonGroup, ClickAwayListener } from '@mui/material';
import _styled from '@emotion/styled/base';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { isPlainObject as isPlainObject$1, combineReducers, applyMiddleware, createStore, compose, isAction } from 'redux';
import { createSelector } from 'reselect';
import { thunk, withExtraArgument } from 'redux-thunk';
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
import _createClass from '@babel/runtime/helpers/createClass';
import omit from 'lodash/omit';
import { Menu as Menu$1, useContextMenu, Submenu, Item, Separator } from 'react-contexify';
import { createPortal } from 'react-dom';
import clsx from 'clsx';
import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
import _regeneratorRuntime from '@babel/runtime/regenerator';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import { useInView } from 'react-intersection-observer';
import MuiButton from '@mui/material/ButtonBase';
import { useDropzone } from 'react-dropzone';
import { saveAs } from 'file-saver';
import _typeof from '@babel/runtime/helpers/typeof';
import Tooltip, { tooltipClasses } from '@mui/material/Tooltip';
import Box$1 from '@mui/material/Box';
import _round from 'lodash/round';
import _map from 'lodash/map';
var __defProp$1 = Object.defineProperty;
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues$1 = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp$1.call(b, prop))
__defNormalProp$1(a, prop, b[prop]);
if (__getOwnPropSymbols$1)
for (var prop of __getOwnPropSymbols$1(b)) {
if (__propIsEnum$1.call(b, prop))
__defNormalProp$1(a, prop, b[prop]);
}
return a;
};
// src/utils/env.ts
var NOTHING = Symbol.for("immer-nothing");
var DRAFTABLE = Symbol.for("immer-draftable");
var DRAFT_STATE = Symbol.for("immer-state");
// src/utils/errors.ts
var errors = process.env.NODE_ENV !== "production" ? [
// All error codes, starting by 0:
function(plugin) {
return `The plugin for '${plugin}' has not been loaded into Immer. To enable the plugin, import and call \`enable${plugin}()\` when initializing your application.`;
},
function(thing) {
return `produce can only be called on things that are draftable: plain objects, arrays, Map, Set or classes that are marked with '[immerable]: true'. Got '${thing}'`;
},
"This object has been frozen and should not be mutated",
function(data) {
return "Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? " + data;
},
"An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.",
"Immer forbids circular references",
"The first or second argument to `produce` must be a function",
"The third argument to `produce` must be a function or undefined",
"First argument to `createDraft` must be a plain object, an array, or an immerable object",
"First argument to `finishDraft` must be a draft returned by `createDraft`",
function(thing) {
return `'current' expects a draft, got: ${thing}`;
},
"Object.defineProperty() cannot be used on an Immer draft",
"Object.setPrototypeOf() cannot be used on an Immer draft",
"Immer only supports deleting array indices",
"Immer only supports setting array indices and the 'length' property",
function(thing) {
return `'original' expects a draft, got: ${thing}`;
}
// Note: if more errors are added, the errorOffset in Patches.ts should be increased
// See Patches.ts for additional errors
] : [];
function die(error, ...args) {
if (process.env.NODE_ENV !== "production") {
const e = errors[error];
const msg = typeof e === "function" ? e.apply(null, args) : e;
throw new Error(`[Immer] ${msg}`);
}
throw new Error(
`[Immer] minified error nr: ${error}. Full error at: https://bit.ly/3cXEKWf`
);
}
// src/utils/common.ts
var getPrototypeOf = Object.getPrototypeOf;
function isDraft(value) {
return !!value && !!value[DRAFT_STATE];
}
function isDraftable(value) {
var _a;
if (!value)
return false;
return isPlainObject(value) || Array.isArray(value) || !!value[DRAFTABLE] || !!((_a = value.constructor) == null ? void 0 : _a[DRAFTABLE]) || isMap(value) || isSet(value);
}
var objectCtorString = Object.prototype.constructor.toString();
function isPlainObject(value) {
if (!value || typeof value !== "object")
return false;
const proto = getPrototypeOf(value);
if (proto === null) {
return true;
}
const Ctor = Object.hasOwnProperty.call(proto, "constructor") && proto.constructor;
if (Ctor === Object)
return true;
return typeof Ctor == "function" && Function.toString.call(Ctor) === objectCtorString;
}
function each(obj, iter) {
if (getArchtype(obj) === 0 /* Object */) {
Reflect.ownKeys(obj).forEach((key) => {
iter(key, obj[key], obj);
});
} else {
obj.forEach((entry, index) => iter(index, entry, obj));
}
}
function getArchtype(thing) {
const state = thing[DRAFT_STATE];
return state ? state.type_ : Array.isArray(thing) ? 1 /* Array */ : isMap(thing) ? 2 /* Map */ : isSet(thing) ? 3 /* Set */ : 0 /* Object */;
}
function has(thing, prop) {
return getArchtype(thing) === 2 /* Map */ ? thing.has(prop) : Object.prototype.hasOwnProperty.call(thing, prop);
}
function set$2(thing, propOrOldValue, value) {
const t = getArchtype(thing);
if (t === 2 /* Map */)
thing.set(propOrOldValue, value);
else if (t === 3 /* Set */) {
thing.add(value);
} else
thing[propOrOldValue] = value;
}
function is(x, y) {
if (x === y) {
return x !== 0 || 1 / x === 1 / y;
} else {
return x !== x && y !== y;
}
}
function isMap(target) {
return target instanceof Map;
}
function isSet(target) {
return target instanceof Set;
}
function latest(state) {
return state.copy_ || state.base_;
}
function shallowCopy(base, strict) {
if (isMap(base)) {
return new Map(base);
}
if (isSet(base)) {
return new Set(base);
}
if (Array.isArray(base))
return Array.prototype.slice.call(base);
const isPlain = isPlainObject(base);
if (strict === true || strict === "class_only" && !isPlain) {
const descriptors = Object.getOwnPropertyDescriptors(base);
delete descriptors[DRAFT_STATE];
let keys = Reflect.ownKeys(descriptors);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const desc = descriptors[key];
if (desc.writable === false) {
desc.writable = true;
desc.configurable = true;
}
if (desc.get || desc.set)
descriptors[key] = {
configurable: true,
writable: true,
// could live with !!desc.set as well here...
enumerable: desc.enumerable,
value: base[key]
};
}
return Object.create(getPrototypeOf(base), descriptors);
} else {
const proto = getPrototypeOf(base);
if (proto !== null && isPlain) {
return __spreadValues$1({}, base);
}
const obj = Object.create(proto);
return Object.assign(obj, base);
}
}
function freeze(obj, deep = false) {
if (isFrozen(obj) || isDraft(obj) || !isDraftable(obj))
return obj;
if (getArchtype(obj) > 1) {
obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections;
}
Object.freeze(obj);
if (deep)
Object.entries(obj).forEach(([key, value]) => freeze(value, true));
return obj;
}
function dontMutateFrozenCollections() {
die(2);
}
function isFrozen(obj) {
return Object.isFrozen(obj);
}
// src/utils/plugins.ts
var plugins = {};
function getPlugin(pluginKey) {
const plugin = plugins[pluginKey];
if (!plugin) {
die(0, pluginKey);
}
return plugin;
}
// src/core/scope.ts
var currentScope;
function getCurrentScope() {
return currentScope;
}
function createScope(parent_, immer_) {
return {
drafts_: [],
parent_,
immer_,
// Whenever the modified draft contains a draft from another scope, we
// need to prevent auto-freezing so the unowned draft can be finalized.
canAutoFreeze_: true,
unfinalizedDrafts_: 0
};
}
function usePatchesInScope(scope, patchListener) {
if (patchListener) {
getPlugin("Patches");
scope.patches_ = [];
scope.inversePatches_ = [];
scope.patchListener_ = patchListener;
}
}
function revokeScope(scope) {
leaveScope(scope);
scope.drafts_.forEach(revokeDraft);
scope.drafts_ = null;
}
function leaveScope(scope) {
if (scope === currentScope) {
currentScope = scope.parent_;
}
}
function enterScope(immer2) {
return currentScope = createScope(currentScope, immer2);
}
function revokeDraft(draft) {
const state = draft[DRAFT_STATE];
if (state.type_ === 0 /* Object */ || state.type_ === 1 /* Array */)
state.revoke_();
else
state.revoked_ = true;
}
// src/core/finalize.ts
function processResult(result, scope) {
scope.unfinalizedDrafts_ = scope.drafts_.length;
const baseDraft = scope.drafts_[0];
const isReplaced = result !== void 0 && result !== baseDraft;
if (isReplaced) {
if (baseDraft[DRAFT_STATE].modified_) {
revokeScope(scope);
die(4);
}
if (isDraftable(result)) {
result = finalize(scope, result);
if (!scope.parent_)
maybeFreeze(scope, result);
}
if (scope.patches_) {
getPlugin("Patches").generateReplacementPatches_(
baseDraft[DRAFT_STATE].base_,
result,
scope.patches_,
scope.inversePatches_
);
}
} else {
result = finalize(scope, baseDraft, []);
}
revokeScope(scope);
if (scope.patches_) {
scope.patchListener_(scope.patches_, scope.inversePatches_);
}
return result !== NOTHING ? result : void 0;
}
function finalize(rootScope, value, path) {
if (isFrozen(value))
return value;
const state = value[DRAFT_STATE];
if (!state) {
each(
value,
(key, childValue) => finalizeProperty(rootScope, state, value, key, childValue, path)
);
return value;
}
if (state.scope_ !== rootScope)
return value;
if (!state.modified_) {
maybeFreeze(rootScope, state.base_, true);
return state.base_;
}
if (!state.finalized_) {
state.finalized_ = true;
state.scope_.unfinalizedDrafts_--;
const result = state.copy_;
let resultEach = result;
let isSet2 = false;
if (state.type_ === 3 /* Set */) {
resultEach = new Set(result);
result.clear();
isSet2 = true;
}
each(
resultEach,
(key, childValue) => finalizeProperty(rootScope, state, result, key, childValue, path, isSet2)
);
maybeFreeze(rootScope, result, false);
if (path && rootScope.patches_) {
getPlugin("Patches").generatePatches_(
state,
path,
rootScope.patches_,
rootScope.inversePatches_
);
}
}
return state.copy_;
}
function finalizeProperty(rootScope, parentState, targetObject, prop, childValue, rootPath, targetIsSet) {
if (process.env.NODE_ENV !== "production" && childValue === targetObject)
die(5);
if (isDraft(childValue)) {
const path = rootPath && parentState && parentState.type_ !== 3 /* Set */ && // Set objects are atomic since they have no keys.
!has(parentState.assigned_, prop) ? rootPath.concat(prop) : void 0;
const res = finalize(rootScope, childValue, path);
set$2(targetObject, prop, res);
if (isDraft(res)) {
rootScope.canAutoFreeze_ = false;
} else
return;
} else if (targetIsSet) {
targetObject.add(childValue);
}
if (isDraftable(childValue) && !isFrozen(childValue)) {
if (!rootScope.immer_.autoFreeze_ && rootScope.unfinalizedDrafts_ < 1) {
return;
}
finalize(rootScope, childValue);
if ((!parentState || !parentState.scope_.parent_) && typeof prop !== "symbol" && Object.prototype.propertyIsEnumerable.call(targetObject, prop))
maybeFreeze(rootScope, childValue);
}
}
function maybeFreeze(scope, value, deep = false) {
if (!scope.parent_ && scope.immer_.autoFreeze_ && scope.canAutoFreeze_) {
freeze(value, deep);
}
}
// src/core/proxy.ts
function createProxyProxy(base, parent) {
const isArray = Array.isArray(base);
const state = {
type_: isArray ? 1 /* Array */ : 0 /* Object */,
// Track which produce call this is associated with.
scope_: parent ? parent.scope_ : getCurrentScope(),
// True for both shallow and deep changes.
modified_: false,
// Used during finalization.
finalized_: false,
// Track which properties have been assigned (true) or deleted (false).
assigned_: {},
// The parent draft state.
parent_: parent,
// The base state.
base_: base,
// The base proxy.
draft_: null,
// set below
// The base copy with any updated values.
copy_: null,
// Called by the `produce` function.
revoke_: null,
isManual_: false
};
let target = state;
let traps = objectTraps;
if (isArray) {
target = [state];
traps = arrayTraps;
}
const { revoke, proxy } = Proxy.revocable(target, traps);
state.draft_ = proxy;
state.revoke_ = revoke;
return proxy;
}
var objectTraps = {
get(state, prop) {
if (prop === DRAFT_STATE)
return state;
const source = latest(state);
if (!has(source, prop)) {
return readPropFromProto(state, source, prop);
}
const value = source[prop];
if (state.finalized_ || !isDraftable(value)) {
return value;
}
if (value === peek(state.base_, prop)) {
prepareCopy(state);
return state.copy_[prop] = createProxy(value, state);
}
return value;
},
has(state, prop) {
return prop in latest(state);
},
ownKeys(state) {
return Reflect.ownKeys(latest(state));
},
set(state, prop, value) {
const desc = getDescriptorFromProto(latest(state), prop);
if (desc == null ? void 0 : desc.set) {
desc.set.call(state.draft_, value);
return true;
}
if (!state.modified_) {
const current2 = peek(latest(state), prop);
const currentState = current2 == null ? void 0 : current2[DRAFT_STATE];
if (currentState && currentState.base_ === value) {
state.copy_[prop] = value;
state.assigned_[prop] = false;
return true;
}
if (is(value, current2) && (value !== void 0 || has(state.base_, prop)))
return true;
prepareCopy(state);
markChanged(state);
}
if (state.copy_[prop] === value && // special case: handle new props with value 'undefined'
(value !== void 0 || prop in state.copy_) || // special case: NaN
Number.isNaN(value) && Number.isNaN(state.copy_[prop]))
return true;
state.copy_[prop] = value;
state.assigned_[prop] = true;
return true;
},
deleteProperty(state, prop) {
if (peek(state.base_, prop) !== void 0 || prop in state.base_) {
state.assigned_[prop] = false;
prepareCopy(state);
markChanged(state);
} else {
delete state.assigned_[prop];
}
if (state.copy_) {
delete state.copy_[prop];
}
return true;
},
// Note: We never coerce `desc.value` into an Immer draft, because we can't make
// the same guarantee in ES5 mode.
getOwnPropertyDescriptor(state, prop) {
const owner = latest(state);
const desc = Reflect.getOwnPropertyDescriptor(owner, prop);
if (!desc)
return desc;
return {
writable: true,
configurable: state.type_ !== 1 /* Array */ || prop !== "length",
enumerable: desc.enumerable,
value: owner[prop]
};
},
defineProperty() {
die(11);
},
getPrototypeOf(state) {
return getPrototypeOf(state.base_);
},
setPrototypeOf() {
die(12);
}
};
var arrayTraps = {};
each(objectTraps, (key, fn) => {
arrayTraps[key] = function() {
arguments[0] = arguments[0][0];
return fn.apply(this, arguments);
};
});
arrayTraps.deleteProperty = function(state, prop) {
if (process.env.NODE_ENV !== "production" && isNaN(parseInt(prop)))
die(13);
return arrayTraps.set.call(this, state, prop, void 0);
};
arrayTraps.set = function(state, prop, value) {
if (process.env.NODE_ENV !== "production" && prop !== "length" && isNaN(parseInt(prop)))
die(14);
return objectTraps.set.call(this, state[0], prop, value, state[0]);
};
function peek(draft, prop) {
const state = draft[DRAFT_STATE];
const source = state ? latest(state) : draft;
return source[prop];
}
function readPropFromProto(state, source, prop) {
var _a;
const desc = getDescriptorFromProto(source, prop);
return desc ? `value` in desc ? desc.value : (
// This is a very special case, if the prop is a getter defined by the
// prototype, we should invoke it with the draft as context!
(_a = desc.get) == null ? void 0 : _a.call(state.draft_)
) : void 0;
}
function getDescriptorFromProto(source, prop) {
if (!(prop in source))
return void 0;
let proto = getPrototypeOf(source);
while (proto) {
const desc = Object.getOwnPropertyDescriptor(proto, prop);
if (desc)
return desc;
proto = getPrototypeOf(proto);
}
return void 0;
}
function markChanged(state) {
if (!state.modified_) {
state.modified_ = true;
if (state.parent_) {
markChanged(state.parent_);
}
}
}
function prepareCopy(state) {
if (!state.copy_) {
state.copy_ = shallowCopy(
state.base_,
state.scope_.immer_.useStrictShallowCopy_
);
}
}
// src/core/immerClass.ts
var Immer2 = class {
constructor(config) {
this.autoFreeze_ = true;
this.useStrictShallowCopy_ = false;
/**
* The `produce` function takes a value and a "recipe function" (whose
* return value often depends on the base state). The recipe function is
* free to mutate its first argument however it wants. All mutations are
* only ever applied to a __copy__ of the base state.
*
* Pass only a function to create a "curried producer" which relieves you
* from passing the recipe function every time.
*
* Only plain objects and arrays are made mutable. All other objects are
* considered uncopyable.
*
* Note: This function is __bound__ to its `Immer` instance.
*
* @param {any} base - the initial state
* @param {Function} recipe - function that receives a proxy of the base state as first argument and which can be freely modified
* @param {Function} patchListener - optional function that will be called with all the patches produced here
* @returns {any} a new state, or the initial state if nothing was modified
*/
this.produce = (base, recipe, patchListener) => {
if (typeof base === "function" && typeof recipe !== "function") {
const defaultBase = recipe;
recipe = base;
const self = this;
return function curriedProduce(base2 = defaultBase, ...args) {
return self.produce(base2, (draft) => recipe.call(this, draft, ...args));
};
}
if (typeof recipe !== "function")
die(6);
if (patchListener !== void 0 && typeof patchListener !== "function")
die(7);
let result;
if (isDraftable(base)) {
const scope = enterScope(this);
const proxy = createProxy(base, void 0);
let hasError = true;
try {
result = recipe(proxy);
hasError = false;
} finally {
if (hasError)
revokeScope(scope);
else
leaveScope(scope);
}
usePatchesInScope(scope, patchListener);
return processResult(result, scope);
} else if (!base || typeof base !== "object") {
result = recipe(base);
if (result === void 0)
result = base;
if (result === NOTHING)
result = void 0;
if (this.autoFreeze_)
freeze(result, true);
if (patchListener) {
const p = [];
const ip = [];
getPlugin("Patches").generateReplacementPatches_(base, result, p, ip);
patchListener(p, ip);
}
return result;
} else
die(1, base);
};
this.produceWithPatches = (base, recipe) => {
if (typeof base === "function") {
return (state, ...args) => this.produceWithPatches(state, (draft) => base(draft, ...args));
}
let patches, inversePatches;
const result = this.produce(base, recipe, (p, ip) => {
patches = p;
inversePatches = ip;
});
return [result, patches, inversePatches];
};
if (typeof (config == null ? void 0 : config.autoFreeze) === "boolean")
this.setAutoFreeze(config.autoFreeze);
if (typeof (config == null ? void 0 : config.useStrictShallowCopy) === "boolean")
this.setUseStrictShallowCopy(config.useStrictShallowCopy);
}
createDraft(base) {
if (!isDraftable(base))
die(8);
if (isDraft(base))
base = current(base);
const scope = enterScope(this);
const proxy = createProxy(base, void 0);
proxy[DRAFT_STATE].isManual_ = true;
leaveScope(scope);
return proxy;
}
finishDraft(draft, patchListener) {
const state = draft && draft[DRAFT_STATE];
if (!state || !state.isManual_)
die(9);
const { scope_: scope } = state;
usePatchesInScope(scope, patchListener);
return processResult(void 0, scope);
}
/**
* Pass true to automatically freeze all copies created by Immer.
*
* By default, auto-freezing is enabled.
*/
setAutoFreeze(value) {
this.autoFreeze_ = value;
}
/**
* Pass true to enable strict shallow copy.
*
* By default, immer does not copy the object descriptors such as getter, setter and non-enumrable properties.
*/
setUseStrictShallowCopy(value) {
this.useStrictShallowCopy_ = value;
}
applyPatches(base, patches) {
let i;
for (i = patches.length - 1; i >= 0; i--) {
const patch = patches[i];
if (patch.path.length === 0 && patch.op === "replace") {
base = patch.value;
break;
}
}
if (i > -1) {
patches = patches.slice(i + 1);
}
const applyPatchesImpl = getPlugin("Patches").applyPatches_;
if (isDraft(base)) {
return applyPatchesImpl(base, patches);
}
return this.produce(
base,
(draft) => applyPatchesImpl(draft, patches)
);
}
};
function createProxy(value, parent) {
const draft = isMap(value) ? getPlugin("MapSet").proxyMap_(value, parent) : isSet(value) ? getPlugin("MapSet").proxySet_(value, parent) : createProxyProxy(value, parent);
const scope = parent ? parent.scope_ : getCurrentScope();
scope.drafts_.push(draft);
return draft;
}
// src/core/current.ts
function current(value) {
if (!isDraft(value))
die(10, value);
return currentImpl(value);
}
function currentImpl(value) {
if (!isDraftable(value) || isFrozen(value))
return value;
const state = value[DRAFT_STATE];
let copy;
if (state) {
if (!state.modified_)
return state.base_;
state.finalized_ = true;
copy = shallowCopy(value, state.scope_.immer_.useStrictShallowCopy_);
} else {
copy = shallowCopy(value, true);
}
each(copy, (key, childValue) => {
set$2(copy, key, currentImpl(childValue));
});
if (state) {
state.finalized_ = false;
}
return copy;
}
// src/immer.ts
var immer = new Immer2();
var produce = immer.produce;
immer.produceWithPatches.bind(
immer
);
immer.setAutoFreeze.bind(immer);
immer.setUseStrictShallowCopy.bind(immer);
immer.applyPatches.bind(immer);
immer.createDraft.bind(immer);
immer.finishDraft.bind(immer);
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
var target = {};
for (var prop in source)
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
target[prop] = source[prop];
if (source != null && __getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(source)) {
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
target[prop] = source[prop];
}
return target;
};
var composeWithDevTools = typeof window !== "undefined" && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ : function() {
if (arguments.length === 0) return void 0;
if (typeof arguments[0] === "object") return compose;
return compose.apply(null, arguments);
};
// src/tsHelpers.ts
var hasMatchFunction = (v) => {
return v && typeof v.match === "function";
};
// src/createAction.ts
function createAction(type, prepareAction) {
function actionCreator(...args) {
if (prepareAction) {
let prepared = prepareAction(...args);
if (!prepared) {
throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(0) : "prepareAction did not return an object");
}
return __spreadValues(__spreadValues({
type,
payload: prepared.payload
}, "meta" in prepared && {
meta: prepared.meta
}), "error" in prepared && {
error: prepared.error
});
}
return {
type,
payload: args[0]
};
}
actionCreator.toString = () => `${type}`;
actionCreator.type = type;
actionCreator.match = (action) => isAction(action) && action.type === type;
return actionCreator;
}
function isActionCreator(action) {
return typeof action === "function" && "type" in action && // hasMatchFunction only wants Matchers but I don't see the point in rewriting it
hasMatchFunction(action);
}
// src/actionCreatorInvariantMiddleware.ts
function getMessage(type) {
const splitType = type ? `${type}`.split("/") : [];
const actionName = splitType[splitType.length - 1] || "actionCreator";
return `Detected an action creator with type "${type || "unknown"}" being dispatched.
Make sure you're calling the action creator before dispatching, i.e. \`dispatch(${actionName}())\` instead of \`dispatch(${actionName})\`. This is necessary even if the action has no payload.`;
}
function createActionCreatorInvariantMiddleware(options = {}) {
if (process.env.NODE_ENV === "production") {
return () => (next) => (action) => next(action);
}
const {
isActionCreator: isActionCreator2 = isActionCreator
} = options;
return () => (next) => (action) => {
if (isActionCreator2(action)) {
console.warn(getMessage(action.type));
}
return next(action);
};
}
function getTimeMeasureUtils(maxDelay, fnName) {
let elapsed = 0;
return {
measureTime(fn) {
const started = Date.now();
try {
return fn();
} finally {
const finished = Date.now();
elapsed += finished - started;
}
},
warnIfExceeded() {
if (elapsed > maxDelay) {
console.warn(`${fnName} took ${elapsed}ms, which is more than the warning threshold of ${maxDelay}ms.
If your state or actions are very large, you may want to disable the middleware as it might cause too much of a slowdown in development mode. See https://redux-toolkit.js.org/api/getDefaultMiddleware for instructions.
It is disabled in production builds, so you don't need to worry about that.`);
}
}
};
}
var Tuple = class _Tuple extends Array {
constructor(...items) {
super(...items);
Object.setPrototypeOf(this, _Tuple.prototype);
}
static get [Symbol.species]() {
return _Tuple;
}
concat(...arr) {
return super.concat.apply(this, arr);
}
prepend(...arr) {
if (arr.length === 1 && Array.isArray(arr[0])) {
return new _Tuple(...arr[0].concat(this));
}
return new _Tuple(...arr.concat(this));
}
};
function freezeDraftable(val) {
return isDraftable(val) ? produce(val, () => {
}) : val;
}
function getOrInsertComputed(map, key, compute) {
if (map.has(key)) return map.get(key);
return map.set(key, compute(key)).get(key);
}
// src/immutableStateInvariantMiddleware.ts
function isImmutableDefault(value) {
return typeof value !== "object" || value == null || Object.isFrozen(value);
}
function trackForMutations(isImmutable, ignorePaths, obj) {
const trackedProperties = trackProperties(isImmutable, ignorePaths, obj);
return {
detectMutations() {
return detectMutations(isImmutable, ignorePaths, trackedProperties, obj);
}
};
}
function trackProperties(isImmutable, ignorePaths = [], obj, path = "", checkedObjects = /* @__PURE__ */ new Set()) {
const tracked = {
value: obj
};
if (!isImmutable(obj) && !checkedObjects.has(obj)) {
checkedObjects.add(obj);
tracked.children = {};
for (const key in obj) {
const childPath = path ? path + "." + key : key;
if (ignorePaths.length && ignorePaths.indexOf(childPath) !== -1) {
continue;
}
tracked.children[key] = trackProperties(isImmutable, ignorePaths, obj[key], childPath);
}
}
return tracked;
}
function detectMutations(isImmutable, ignoredPaths = [], trackedProperty, obj, sameParentRef = false, path = "") {
const prevObj = trackedProperty ? trackedProperty.value : void 0;
const sameRef = prevObj === obj;
if (sameParentRef && !sameRef && !Number.isNaN(obj)) {
return {
wasMutated: true,
path
};
}
if (isImmutable(prevObj) || isImmutable(obj)) {
return {
wasMutated: false
};
}
const keysToDetect = {};
for (let key in trackedProperty.children) {
keysToDetect[key] = true;
}
for (let key in obj) {
keysToDetect[key] = true;
}
const hasIgnoredPaths = ignoredPaths.length > 0;
for (let key in keysToDetect) {
const nestedPath = path ? path + "." + key : key;
if (hasIgnoredPaths) {
const hasMatches = ignoredPaths.some((ignored) => {
if (ignored instanceof RegExp) {
return ignored.test(nestedPath);
}
return nestedPath === ignored;
});
if (hasMatches) {
continue;
}
}
const result = detectMutations(isImmutable, ignoredPaths, trackedProperty.children[key], obj[key], sameRef, nestedPath);
if (result.wasMutated) {
return result;
}
}
return {
wasMutated: false
};
}
function createImmutableStateInvariantMiddleware(options = {}) {
if (process.env.NODE_ENV === "production") {
return () => (next) => (action) => next(action);
} else {
let stringify2 = function(obj, serializer, indent, decycler) {
return JSON.stringify(obj, getSerialize2(serializer, decycler), indent);
}, getSerialize2 = function(serializer, decycler) {
let stack = [], keys = [];
if (!decycler) decycler = function(_, value) {
if (stack[0] === value) return "[Circular ~]";
return "[Circular ~." + keys.slice(0, stack.indexOf(value)).join(".") + "]";
};
return function(key, value) {
if (stack.length > 0) {
var thisPos = stack.indexOf(this);
~thisPos ? stack.splice(thisPos + 1) : stack.push(this);
~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key);
if (~stack.indexOf(value)) value = decycler.call(this, key, value);
} else stack.push(value);
return serializer == null ? value : serializer.call(this, key, value);
};
};
let {
isImmutable = isImmutableDefault,
ignoredPaths,
warnAfter = 32
} = options;
const track = trackForMutations.bind(null, isImmutable, ignoredPaths);
return ({
getState
}) => {
let state = getState();
let tracker = track(state);
let result;
return (next) => (action) => {
const measureUtils = getTimeMeasureUtils(warnAfter, "ImmutableStateInvariantMiddleware");
measureUtils.measureTime(() => {
state = getState();
result = tracker.detectMutations();
tracker = track(state);
if (result.wasMutated) {
throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(19) : `A state mutation was detected between dispatches, in the path '${result.path || ""}'. This may cause incorrect behavior. (https://redux.js.org/style-guide/style-guide#do-not-mutate-state)`);
}
});
const dispatchedAction = next(action);
measureUtils.measureTime(() => {
state = getState();
result = tracker.detectMutations();
tracker = track(state);
if (result.wasMutated) {
throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(20) : `A state mutation was detected inside a dispatch, in the path: ${result.path || ""}. Take a look at the reducer(s) handling the action ${stringify2(action)}. (https://redux.js.org/style-guide/style-guide#do-not-mutate-state)`);
}
});
measureUtils.warnIfExceeded();
return dispatchedAction;
};
};
}
}
function isPlain(val) {
const type = typeof val;
return val == null || type === "string" || type === "boolean" || type === "number" || Array.isArray(val) || isPlainObject$1(val);
}
function findNonSerializableValue(value, path = "", isSerializable = isPlain, getEntries, ignoredPaths = [], cache) {
let foundNestedSerializable;
if (!isSerializable(value)) {
return {
keyPath: path || "<root>",
value
};
}
if (typeof value !== "object" || value === null) {
return false;
}
if (cache == null ? void 0 : cache.has(value)) return false;
const entries = getEntries != null ? getEntries(value) : Object.entries(value);
const hasIgnoredPaths = ignoredPaths.length > 0;
for (const [key, nestedValue] of entries) {
const nestedPath = path ? path + "." + key : key;
if (hasIgnoredPaths) {
const hasMatches = ignoredPaths.some((ignored) => {
if (ignored instanceof RegExp) {
return ignored.test(nestedPath);
}
return nestedPath === ignored;
});
if (hasMatches) {
continue;
}
}
if (!isSerializable(nestedValue)) {
return {
keyPath: nestedPath,
value: nestedValue
};
}
if (typeof nestedValue === "object") {
foundNestedSerializable = findNonSerializableValue(nestedValue, nestedPath, isSerializable, getEntries, ignoredPaths, cache);
if (foundNestedSerializable) {
return foundNestedSerializable;
}
}
}
if (cache && isNestedFrozen(value)) cache.add(value);
return false;
}
function isNestedFrozen(value) {
if (!Object.isFrozen(value)) return false;
for (const nestedValue of Object.values(value)) {
if (typeof nestedValue !== "object" || nestedValue === null) continue;
if (!isNestedFrozen(nestedValue)) return false;
}
return true;
}
function createSerializableStateInvariantMiddleware(options = {}) {
if (process.env.NODE_ENV === "production") {
return () => (next) => (action) => next(action);
} else {
const {
isSerializable = isPlain,
getEntries,
ignoredActions = [],
ignoredActionPaths = ["meta.arg", "meta.baseQueryMeta"],
ignoredPaths = [],
warnAfter = 32,
ignoreState = false,
ignoreActions = false,
disableCache = false
} = options;
const cache = !disableCache && WeakSet ? /* @__PURE__ */ new WeakSet() : void 0;
return (storeAPI) => (next) => (action) => {
if (!isAction(action)) {
return next(action);
}
const result = next(action);
const measureUtils = getTimeMeasureUtils(warnAfter, "SerializableStateInvariantMiddleware");
if (!ignoreActions && !(ignoredActions.length && ignoredActions.indexOf(action.type) !== -1)) {
measureUtils.measureTime(() => {
const foundActionNonSerializableValue = findNonSerializableValue(action, "", isSerializable, getEntries, ignoredActionPaths, cache);
if (foundActionNonSerializableValue) {
const {
keyPath,
value
} = foundActionNonSerializableValue;
console.error(`A non-serializable value was detected in an action, in the path: \`${keyPath}\`. Value:`, value, "\nTake a look at the logic that dispatched this action: ", action, "\n(See https://redux.js.org/faq/actions#why-should-type-be-a-string-or-at-least-serializable-why-should-my-action-types-be-constants)", "\n(To allow non-serializable values see: https://redux-toolkit.js.org/usage/usage-guide#working-with-non-serializable-data)");
}
});
}
if (!ignoreState) {
measureUtils.measureTime(() => {
const state = storeAPI.getState();
const foundStateNonSerializableValue = findNonSerializableValue(state, "", isSerializable, getEntries, ignoredPaths, cache);
if (foundStateNonSerializableValue) {
const {
keyPath,
value
} = foundStateNonSerializableValue;
console.error(`A non-serializable value was detected in the state, in the path: \`${keyPath}\`. Value:`, value, `
Take a look at the reducer(s) handling this action type: ${action.type}.
(See https://redux.js.org/faq/organizing-state#can-i-put-functions-promises-or-other-non-serializable-items-in-my-store-state)`);
}
});
measureUtils.warnIfExceeded();
}
return result;
};
}
}
// src/getDefaultMiddleware.ts
function isBoolean(x) {
return typeof x === "boolean";
}
var buildGetDefaultMiddleware = () => function getDefaultMiddleware(options) {
const {
thunk: thunk$1 = true,
immutableCheck = true,
serializableCheck = true,
actionCreatorCheck = true
} = options != null ? options : {};
let middlewareArray = new Tuple();
if (thunk$1) {
if (isBoolean(thunk$1)) {
middlewareArray.push(thunk);
} else {
middlewareArray.push(withExtraArgument(thunk$1.extraArgument));
}
}
if (process.env.NODE_ENV !== "production") {
if (immutableCheck) {
let immutableOptions = {};
if (!isBoolean(immutableCheck)) {
immutableOptions = immutableCheck;
}
middlewareArray.unshift(createImmutableStateInvariantMiddleware(immutableOptions));
}
if (serializableCheck) {
let serializableOptions = {};
if (!isBoolean(serializableCheck)) {
serializableOptions = serializableCheck;
}
middlewareArray.push(createSerializableStateInvariantMiddleware(serializableOptions));
}
if (actionCreatorCheck) {
let actionCreatorOptions = {};
if (!isBoolean(actionCreatorCheck)) {
actionCreatorOptions = actionCreatorCheck;
}
middlewareArray.unshift(createActionCreatorInvariantMiddleware(actionCreatorOptions));
}
}
return middlewareArray;
};
// src/autoBatchEnhancer.ts
var SHOULD_AUTOBATCH = "RTK_autoBatch";
var createQueueWithTimer = (timeout) => {
return (notify) => {
setTimeout(notify, timeout);
};
};
var autoBatchEnhancer = (options = {
type: "raf"
}) => (next) => (...args) => {
const store = next(...args);
let notifying = true;
let shouldNotifyAtEndOfTick = false;
let notificationQueued = false;
const listeners = /* @__PURE__ */ new Set();
const queueCallback = options.type === "tick" ? queueMicrotask : options.type === "raf" ? (
// requestAnimationFrame won't exist in SSR environments. Fall back to a vague approximation just to keep from erroring.
typeof window !== "undefined" && window.requestAnimationFrame ? window.requestAnimationFrame : createQueueWithTimer(10)
) : options.type === "callback" ? options.queueNotification : createQueueWithTimer(options.timeout);
const notifyListeners = () => {
notificationQueued = false;
if (shouldNotifyAtEndOfTick) {
shouldNotifyAtEndOfTick = false;
listeners.forEach((l) => l());
}
};
return Object.assign({}, store, {
// Override the base `store.subscribe` method to keep original listeners
// from running if we're delaying notifications
subscribe(listener2) {
const wrappedListener = () => notifying && listener2();
const unsubscribe = store.subscribe(wrappedListener);
listeners.add(listener2);
return () => {
unsubscribe();
listeners.delete(listener2);
};
},
// Override the base `store.dispatch` method so that we can check actions
// for the `shouldAutoBatch` flag and determine if batching is active
dispatch(action) {
var _a;
try {
notifying = !((_a = action == null ? void 0 : action.meta) == null ? void 0 : _a[SHOULD_AUTOBATCH]);
shouldNotifyAtEndOfTick = !notifying;
if (shouldNotifyAtEndOfTick) {
if (!notificationQueued) {
notificationQueued = true;
queueCallback(notifyListeners);
}
}
return store.dispatch(action);
} finally {
notifying = true;
}
}
});
};
// src/getDefaultEnhancers.ts
var buildGetDefaultEnhancers = (middlewareEnhancer) => function getDefaultEnhancers(options) {
const {
autoBatch = true
} = options != null ? options : {};
let enhancerArray = new Tuple(middlewareEnhancer);
if (autoBatch) {
enhancerArray.push(autoBatchEnhancer(typeof autoBatch === "object" ? autoBatch : void 0));
}
return enhancerArray;
};
// src/configureStore.ts
function configureStore(options) {
const getDefaultMiddleware = buildGetDefaultMiddleware();
const {
reducer = void 0,
middleware,
devTools = true,
duplicateMiddlewareCheck = true,
preloadedState = void 0,
enhancers = void 0
} = options || {};
let rootReducer;
if (typeof reducer === "function") {
rootReducer = reducer;
} else if (isPlainObject$1(reducer)) {
rootReducer = combineReducers(reducer);
} else {
throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(1) : "`reducer` is a required argument, and must be a function or an object of functions that can be passed to combineReducers");
}
if (process.env.NODE_ENV !== "production" && middleware && typeof middleware !== "function") {
throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(2) : "`middleware` field must be a callback");
}
let finalMiddleware;
if (typeof middleware === "function") {
finalMiddleware = middleware(getDefaultMiddleware);
if (process.env.NODE_ENV !== "production" && !Array.isArray(finalMiddleware)) {
throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(3) : "when using a middleware builder function, an array of middleware must be returned");
}
} else {
finalMiddleware = getDefaultMiddleware();
}
if (process.env.NODE_ENV !== "production" && finalMiddleware.some((item) => typeof item !== "function")) {
throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(4) : "each middleware provided to configureStore must be a function");
}
if (process.env.NODE_ENV !== "production" && duplicateMiddlewareCheck) {
let middlewareReferences = /* @__PURE__ */ new Set();
finalMiddleware.forEach((middleware2) => {
if (middlewareReferences.has(middleware2)) {
throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(42) : "Duplicate middleware references found when creating the store. Ensure that each middleware is only included once.");
}
middlewareReferences.add(middleware2);
});
}
let finalCompose = compose;
if (devTools) {
finalCompose = composeWithDevTools(__spreadValues({
// Enable capture of stack traces for dispatched Redux actions
trace: process.env.NODE_ENV !== "production"
}, typeof devTools === "object" && devTools));
}
const middlewareEnhancer = applyMiddleware(...finalMiddleware);
const getDefaultEnhancers = buildGetDefaultEnhancers(middlewareEnhancer);
if (process.env.NODE_ENV !== "production" && enhancers && typeof enhancers !== "function") {
throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(5) : "`enhancers` field must be a callback");
}
let storeEnhancers = typeof enhancers === "function" ? enhancers(getDefaultEnhancers) : getDefaultEnhancers();
if (process.env.NODE_ENV !== "production" && !Array.isArray(storeEnhancers)) {
throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(6) : "`enhancers` callback must return an array");
}
if (process.env.NODE_ENV !== "production" && storeEnhancers.some((item) => typeof item !== "function")) {
throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(7) : "each enhancer provided to configureStore must be a function");
}
if (process.env.NODE_ENV !== "production" && finalMiddleware.length && !storeEnhancers.includes(middlewareEnhancer)) {
console.error("middlewares were provided, but middleware enhancer was not included in final enhancers - make sure to call `getDefaultEnhancers`");
}
const composedEnhancer = finalCompose(...storeEnhancers);
return createStore(rootReducer, preloadedState, composedEnhancer);
}
// src/mapBuilders.ts
function executeReducerBuilderCallback(builderCallback) {
const actionsMap = {};
const actionMatchers = [];
let defaultCaseReducer;
const builder = {
addCase(typeOrActionCreator, reducer) {
if (process.env.NODE_ENV !== "production") {
if (actionMatchers.length > 0) {
throw new Error(process.env.NODE_ENV === "production" ? formatProdErrorMessage(26) : "`builder.addCase` should only be called before calling `builder.addMatcher`"