Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function resolveInitialValueWithParameters(templateName, parameters) {
if (!templateExists(templateName)) {
// eslint-disable-next-line no-console
console.warn('Template "%s" not defined, using empty initial value', templateName)
return of({isResolving: false, initialValue: undefined})
}
return from(resolveInitialValue(getTemplateById(templateName), parameters)).pipe(
map(initialValue => ({isResolving: false, initialValue}))
)
}
}
const payload = paneContext.payload || {}
const urlTemplate = (paneContext.params || {}).template
const definedTemplate = props.options.template
if (urlTemplate && definedTemplate && definedTemplate !== urlTemplate) {
// eslint-disable-next-line no-console
console.warn(
`Conflicting templates: URL says "${urlTemplate}", structure node says "${definedTemplate}". Using "${definedTemplate}".`
)
}
const {options = {}} = props
const template = definedTemplate || urlTemplate
const typeTemplates = getTemplatesBySchemaType(options.type)
const parameters = {...options.templateParameters, ...payload}
let templateName = template
// If we have not specified a specific template, and we only have a single
// template available for a schema type, use it
if (!template && typeTemplates.length === 1) {
templateName = typeTemplates[0].id
}
return {templateName, parameters}
}
function resolveInitialValueWithParameters(templateName, parameters) {
if (!templateExists(templateName)) {
// eslint-disable-next-line no-console
console.warn('Template "%s" not defined, using empty initial value', templateName)
return of({isResolving: false, initialValue: undefined})
}
return from(resolveInitialValue(getTemplateById(templateName), parameters)).pipe(
map(initialValue => ({isResolving: false, initialValue}))
)
}
`Invalid "new document" configuration: "part:@sanity/base/new-document-structure" should return an array of items. Falling back to default structure.`
)
structure = S.defaultInitialValueTemplateItems()
} else if (structure) {
try {
validateNewDocumentStructure(structure)
} catch (err) {
// eslint-disable-next-line no-console
console.error(
`Invalid "new document" configuration: ${err.message}. Falling back to default structure.`
)
structure = S.defaultInitialValueTemplateItems()
}
} else {
// No structure defined, use default
structure = S.defaultInitialValueTemplateItems()
}
return createModalActions(structure)
}
function createModalAction(templateItem) {
// Make sure we're working with serialized definitions
let item = templateItem
if (item && typeof item.serialize === 'function') {
item = item.serialize()
}
// We currently only allow initial value template items in the "new document" dialog
if (item.type !== 'initialValueTemplateItem') {
throw new Error(
'Only initial value template items are currently allowed in the new document structure'
)
}
// Make sure the template actually exists
const tpl = getTemplateById(item.templateId)
if (!tpl) {
throw new Error(`Template "${item.templateId}" not declared`)
}
// Build up an item suited for the "action modal" dialog
const type = schema.get(tpl.schemaType)
const title = item.title || tpl.title
const description = item.description || tpl.description
return {
...tpl,
title,
description,
// Don't show the type name as subtitle if it's the same as the template name
subtitle: type.title === title ? undefined : type.title,
key: item.id,
// Prioritize icon from initial value template item
function resolvePaneOptions(props, templateName, document) {
const {options} = props
const hasDefinedType = options.type && options.type !== '*'
const typeFromOptions = hasDefinedType ? options.type : undefined
let documentType = typeFromOptions || (document && document._type)
if (!documentType && templateName) {
const template = getTemplateById(templateName)
documentType = template && template.schemaType
}
// If we were not passed a schema type, use the resolved value if available
return hasDefinedType ? options : {...options, type: documentType}
}
import T from '@sanity/base/initial-value-template-builder'
export default [
...T.defaults(),
T.template({
id: 'author-developer',
title: 'Developer',
description: `Selects the role "Developer" for you, so you don't have to`,
schemaType: 'author',
value: params => ({role: 'developer'})
}),
T.template({
id: 'book-by-author',
title: 'Book by author',
description: 'Book by a specific author',
schemaType: 'book',
parameters: [{name: 'authorId', type: 'string'}],
value: params => ({
import T from '@sanity/base/initial-value-template-builder'
export default [
...T.defaults(),
T.template({
id: 'author-developer',
title: 'Developer',
description: `Selects the role "Developer" for you, so you don't have to`,
schemaType: 'author',
value: params => ({role: 'developer'})
}),
T.template({
id: 'book-by-author',
title: 'Book by author',
description: 'Book by a specific author',
schemaType: 'book',
parameters: [{name: 'authorId', type: 'string'}],
value: params => ({
author: {_type: 'reference', _ref: params.authorId}
})
})
]
function getNewRouterState({structure, documentType, params, payload, documentId, paneSegments}) {
const lastChild = structure[structure.length - 1] || {}
const lastGroup = paneSegments[paneSegments.length - 1]
const lastSibling = lastGroup[lastGroup.length - 1]
const terminatesInDocument = lastChild.type === 'document' && lastChild.options.id === documentId
const isTemplateCreate = params.template
const template = isTemplateCreate && getTemplateById(params.template)
const type = (template && template.schemaType) || documentType
const fallbackParameters = {type, template: params.template}
const newDocumentId = documentId === FALLBACK_ID ? UUID() : documentId
return terminatesInDocument
? paneSegments
.slice(0, -1)
.concat([lastGroup.slice(0, -1).concat({...lastSibling, id: newDocumentId})])
: [[{id: `__edit__${newDocumentId}`, params: fallbackParameters, payload}]]
}
maybeHandleOldUrl() {
const {navigate} = this.props.router
const {
action,
legacyEditDocumentId,
type: schemaType,
editDocumentId,
params = {}
} = this.props.router.state
const {template: templateName, ...payloadParams} = params
const template = getTemplateById(templateName)
const type = (template && template.schemaType) || schemaType
const shouldRewrite = (action === 'edit' && legacyEditDocumentId) || (type && editDocumentId)
if (!shouldRewrite) {
return
}
navigate(
getIntentRouteParams({
id: editDocumentId || legacyEditDocumentId,
type,
payloadParams,
templateName
}),
{replace: true}
)
}