@sanity/desk-tool
Version:
Tool for managing all sorts of content in a structured manner
60 lines (56 loc) • 1.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getIntentState = getIntentState;
exports.setActivePanes = setActivePanes;
var _uuid = require("@sanity/uuid");
var _constants = require("./constants");
var state = {
activePanes: []
};
function setActivePanes(panes) {
state.activePanes = panes;
}
/**
* This function looks at the _active panes_ to resolve an intent. this type of
* intent resolution is faster and does not cause the panes to reset
*
* @internal
*/
function getIntentState(intentName, params, currentState, payload) {
var panes = (currentState === null || currentState === void 0 ? void 0 : currentState.panes) || [];
var activePanes = state.activePanes || [];
var editDocumentId = params.id || (0, _uuid.uuid)();
var isTemplate = intentName === 'create' && params.template;
// Loop through open panes and see if any of them can handle the intent
for (var i = activePanes.length - 1; i >= 0; i--) {
var _pane$canHandleIntent;
var pane = activePanes[i];
if (typeof pane !== 'object') continue;
// NOTE: if you update this logic, please also update the similar handler in
// `resolveIntent.ts`
if ((_pane$canHandleIntent = pane.canHandleIntent) !== null && _pane$canHandleIntent !== void 0 && _pane$canHandleIntent.call(pane, intentName, params, {
pane,
index: i
}) ||
// see `resolveIntent.ts` for more info
pane.type === 'documentList' && pane.schemaTypeName === params.type && pane.options.filter === '_type == $type') {
var paneParams = isTemplate ? {
template: params.template
} : _constants.EMPTY_PARAMS;
return {
panes: panes.slice(0, i).concat([[{
id: editDocumentId,
params: paneParams,
payload
}]])
};
}
}
return {
intent: intentName,
params,
payload
};
}