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)
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)
}