How to use @sanity/initial-value-templates - 9 common examples

To help you get started, we’ve selected a few @sanity/initial-value-templates examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sanity-io / sanity / packages / @sanity / structure / src / GenericList.ts View on Github external
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)
github sanity-io / sanity / packages / @sanity / base / initial-value-template-builder.js View on Github external
// eslint-disable-next-line import/no-commonjs
module.exports = require('@sanity/initial-value-templates').TemplateBuilder
github sanity-io / sanity / packages / @sanity / structure / src / InitialValueTemplateItem.ts View on Github external
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))
}
github sanity-io / sanity / packages / @sanity / structure / src / GenericList.ts View on Github external
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
  }
}
github sanity-io / sanity / packages / @sanity / structure / src / InitialValueTemplateItem.ts View on Github external
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()
  })
}
github sanity-io / sanity / packages / @sanity / structure / src / Document.ts View on Github external
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
  )
}
github sanity-io / sanity / packages / @sanity / structure / src / GenericList.ts View on Github external
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()
    )
  })
github sanity-io / sanity / packages / @sanity / structure / src / Editor.ts View on Github external
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)
}
github sanity-io / sanity / packages / @sanity / structure / src / DocumentList.ts View on Github external
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)
}

@sanity/initial-value-templates

Build templates for initial values

MIT
Latest version published 1 year ago

Package Health Score

79 / 100
Full package analysis