@sanity/desk-tool
Version:
Tool for managing all sorts of content in a structured manner
148 lines (146 loc) • 9.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.PaneRouterProvider = PaneRouterProvider;
var _omit2 = _interopRequireDefault(require("lodash/omit"));
var _router = require("@sanity/base/router");
var _react = _interopRequireWildcard(require("react"));
var _paths = require("@sanity/util/paths");
var _ChildLink = require("./ChildLink");
var _BackLink = require("./BackLink");
var _ReferenceChildLink = require("./ReferenceChildLink");
var _PaneRouterContext = require("./PaneRouterContext");
var _ParameterizedLink = require("./ParameterizedLink");
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
var emptyArray = [];
/**
* @internal
*/
function PaneRouterProvider(props) {
var children = props.children,
flatIndex = props.flatIndex,
index = props.index,
params = props.params,
payload = props.payload,
siblingIndex = props.siblingIndex;
var _useRouter = (0, _router.useRouter)(),
navigate = _useRouter.navigate,
navigateIntent = _useRouter.navigateIntent;
var routerState = (0, _router.useRouterState)();
var routerPaneGroups = (0, _react.useMemo)(() => (routerState === null || routerState === void 0 ? void 0 : routerState.panes) || emptyArray, [routerState === null || routerState === void 0 ? void 0 : routerState.panes]);
var groupIndex = index - 1;
var modifyCurrentGroup = (0, _react.useCallback)(modifier => {
var currentGroup = routerPaneGroups[groupIndex] || [];
var currentItem = currentGroup[siblingIndex];
var nextGroup = modifier(currentGroup, currentItem);
var nextPanes = [...routerPaneGroups.slice(0, groupIndex), nextGroup, ...routerPaneGroups.slice(groupIndex + 1)];
var nextRouterState = _objectSpread(_objectSpread({}, routerState || {}), {}, {
panes: nextPanes
});
setTimeout(() => navigate(nextRouterState), 0);
return nextRouterState;
}, [groupIndex, navigate, routerPaneGroups, routerState, siblingIndex]);
var setPayload = (0, _react.useCallback)(nextPayload => {
modifyCurrentGroup((siblings, item) => [...siblings.slice(0, siblingIndex), _objectSpread(_objectSpread({}, item), {}, {
payload: nextPayload
}), ...siblings.slice(siblingIndex + 1)]);
}, [modifyCurrentGroup, siblingIndex]);
var setParams = (0, _react.useCallback)(nextParams => {
modifyCurrentGroup((siblings, item) => [...siblings.slice(0, siblingIndex), _objectSpread(_objectSpread({}, item), {}, {
params: nextParams
}), ...siblings.slice(siblingIndex + 1)]);
}, [modifyCurrentGroup, siblingIndex]);
var handleEditReference = (0, _react.useCallback)(_ref => {
var id = _ref.id,
parentRefPath = _ref.parentRefPath,
type = _ref.type,
template = _ref.template;
navigate({
panes: [...routerPaneGroups.slice(0, groupIndex + 1), [{
id,
params: {
template: template.id,
parentRefPath: (0, _paths.toString)(parentRefPath),
type
},
payload: template.params
}]]
});
}, [groupIndex, navigate, routerPaneGroups]);
var ctx = (0, _react.useMemo)(() => ({
// Zero-based index (position) of pane, visually
index: flatIndex,
// Zero-based index of pane group (within URL structure)
groupIndex,
// Zero-based index of pane within sibling group
siblingIndex,
// Payload of the current pane
payload,
// Params of the current pane
params,
// Whether or not the pane has any siblings (within the same group)
hasGroupSiblings: routerPaneGroups[groupIndex] ? routerPaneGroups[groupIndex].length > 1 : false,
// The length of the current group
groupLength: routerPaneGroups[groupIndex] ? routerPaneGroups[groupIndex].length : 0,
// Current router state for the "panes" property
routerPanesState: routerPaneGroups,
// Curried StateLink that passes the correct state automatically
ChildLink: _ChildLink.ChildLink,
// Curried StateLink that pops off the last pane group
BackLink: _BackLink.BackLink,
// A specialized `ChildLink` that takes in the needed props to open a
// referenced document to the right
ReferenceChildLink: _ReferenceChildLink.ReferenceChildLink,
// Similar to `ReferenceChildLink` expect without the wrapping component
handleEditReference,
// Curried StateLink that passed the correct state, but merges params/payload
ParameterizedLink: _ParameterizedLink.ParameterizedLink,
// Replaces the current pane with a new one
replaceCurrent: function replaceCurrent() {
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
modifyCurrentGroup(() => [{
id: opts.id || '',
payload: opts.payload,
params: opts.params || {}
}]);
},
// Removes the current pane from the group
closeCurrent: () => {
modifyCurrentGroup((siblings, item) => siblings.length > 1 ? siblings.filter(sibling => sibling !== item) : siblings);
},
// Duplicate the current pane, with optional overrides for payload, parameters
duplicateCurrent: options => {
modifyCurrentGroup((siblings, item) => {
var duplicatedItem = _objectSpread(_objectSpread({}, item), {}, {
payload: (options === null || options === void 0 ? void 0 : options.payload) || item.payload,
params: (options === null || options === void 0 ? void 0 : options.params) || item.params
});
return [...siblings.slice(0, siblingIndex), duplicatedItem, ...siblings.slice(siblingIndex)];
});
},
// Set the view for the current pane
setView: viewId => {
var restParams = (0, _omit2.default)(params, 'view');
return setParams(viewId ? _objectSpread(_objectSpread({}, restParams), {}, {
view: viewId
}) : restParams);
},
// Set the parameters for the current pane
setParams,
// Set the payload for the current pane
setPayload,
// Proxied navigation to a given intent. Consider just exposing `router` instead?
navigateIntent
}), [flatIndex, groupIndex, handleEditReference, modifyCurrentGroup, navigateIntent, params, payload, routerPaneGroups, setParams, setPayload, siblingIndex]);
return /*#__PURE__*/_react.default.createElement(_PaneRouterContext.PaneRouterContext.Provider, {
value: ctx
}, children);
}