Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const loneTemplate =
initialValueTemplates.length === 1 &&
maybeSerializeInitialValueTemplateItem(initialValueTemplates[0], 0, path)
const actionButton = new MenuItemBuilder()
.title('Create new')
.icon(PlusIcon)
.showAsAction({whenCollapsed: true})
if (loneTemplate) {
// If we have a single create item, link to that template directly.
// Otherwise we'll want to select from a menu
// Action button
const template = getTemplateById(loneTemplate.templateId)
const templateTitle = template && template.title
items.unshift(
actionButton
.title(`Create new ${loneTemplate.title || templateTitle || ''}`)
.intent(getCreateIntent(loneTemplate))
.serialize()
)
} else {
// More than one item, so we'll want that dropdown of choices
items.unshift(actionButton.action('toggleTemplateSelectionMenu').serialize())
}
// Menu buttons
initialValueTemplates.forEach(tpl => {
const template = getTemplateById(tpl.templateId)
const templateTitle = tpl.title || (template && template.title)
// eslint-disable-next-line import/no-commonjs
module.exports = require('@sanity/initial-value-templates').TemplateBuilder
export function defaultInitialValueTemplateItems(
schema: Schema = getDefaultSchema()
): InitialValueTemplateItemBuilder[] {
const templates = getTemplates()
// Don't list templates that require parameters
.filter(tpl => !tpl.parameters || tpl.parameters.length === 0)
// Don't list templates that we can't create
.filter(tpl => isActionEnabled(schema.get(tpl.schemaType), 'create'))
// Sort templates by their schema type, in order or definition
const typeNames = schema.getTypeNames()
const ordered = templates.sort(
(a, b) => typeNames.indexOf(a.schemaType) - typeNames.indexOf(b.schemaType)
)
// Create actual template items out of the templates
return ordered.map(tpl => StructureBuilder.initialValueTemplateItem(tpl.id))
}
function getCreateIntent(templateItem: InitialValueTemplateItem): Intent {
const tpl = getTemplateById(templateItem.templateId)
const params = pickBy({type: tpl && tpl.schemaType, template: templateItem.templateId}, Boolean)
const intentParams: IntentParams = templateItem.parameters
? [params, templateItem.parameters]
: params
return {
type: 'create',
params: intentParams
}
}
return templateItems.map(item => {
const tpl = getTemplateById(item.templateId)
const title = item.title || (tpl && tpl.title) || 'Create new'
const params = pickBy({type: tpl && tpl.schemaType, template: item.templateId}, Boolean)
const intentParams: IntentParams = item.parameters ? [params, item.parameters] : params
const schema = tpl && getDefaultSchema().get(tpl.schemaType)
return new MenuItemBuilder()
.title(title)
.icon((tpl && tpl.icon) || (schema && schema.icon) || getPlusIcon())
.intent({
type: 'create',
params: intentParams
})
.serialize()
})
}
export function documentFromEditorWithInitialValue(
templateId: string,
parameters?: {[key: string]: any}
) {
const template = getTemplateById(templateId)
if (!template) {
throw new Error(`Template with ID "${templateId}" not defined`)
}
return getDefaultDocumentNode({schemaType: template.schemaType}).initialValueTemplate(
templateId,
parameters
)
}
initialValueTemplates.forEach(tpl => {
const template = getTemplateById(tpl.templateId)
const templateTitle = tpl.title || (template && template.title)
items.push(
new MenuItemBuilder()
.title(`Create new ${templateTitle}`)
.icon(PlusIcon)
.intent(getCreateIntent(tpl))
.group('create-new')
.serialize()
)
})
export function editorWithInitialValueTemplate(
templateId: string,
parameters?: {[key: string]: any}
) {
const template = getTemplateById(templateId)
if (!template) {
throw new Error(`Template with ID "${templateId}" not defined`)
}
return new EditorBuilder()
.schemaType(template.schemaType)
.initialValueTemplate(templateId, parameters)
}
return typeNames.reduce((items, typeName) => {
const schemaType = schema.get(typeName)
if (!isActionEnabled(schemaType, 'create')) {
return items
}
return items.concat(
getParameterlessTemplatesBySchemaType(typeName).map(
(tpl): InitialValueTemplateItem => ({
type: 'initialValueTemplateItem',
id: tpl.id,
templateId: tpl.id
})
)
)
}, templateItems)
}