diff --git a/packages/@sanity/base/initial-values.js b/packages/@sanity/base/initial-value-templates.js similarity index 100% rename from packages/@sanity/base/initial-values.js rename to packages/@sanity/base/initial-value-templates.js diff --git a/packages/@sanity/base/initial-values-template-builder.js b/packages/@sanity/base/initial-values-template-builder.js index f7fe8943595..03e777a2080 100644 --- a/packages/@sanity/base/initial-values-template-builder.js +++ b/packages/@sanity/base/initial-values-template-builder.js @@ -1,2 +1,2 @@ // eslint-disable-next-line import/no-commonjs -module.exports = require('@sanity/structure/lib/templates').default +module.exports = require('@sanity/initial-value-templates').TemplateBuilder diff --git a/packages/@sanity/dashboard/src/widgets/sanityTutorials/dataAdapter.js b/packages/@sanity/dashboard/src/widgets/sanityTutorials/dataAdapter.js index 7d45a068dbc..43d3c9ba1e3 100644 --- a/packages/@sanity/dashboard/src/widgets/sanityTutorials/dataAdapter.js +++ b/packages/@sanity/dashboard/src/widgets/sanityTutorials/dataAdapter.js @@ -1,8 +1,7 @@ import client from 'part:@sanity/base/client' -import sanityClient from '@sanity/client' import imageUrlBuilder from '@sanity/image-url' -const configuredClient = sanityClient({ +const configuredClient = client.clone().config({ projectId: '3do82whm', dataset: 'production', useCdn: true diff --git a/packages/@sanity/default-layout/package.json b/packages/@sanity/default-layout/package.json index a73c47acd2e..d39fb490bb3 100644 --- a/packages/@sanity/default-layout/package.json +++ b/packages/@sanity/default-layout/package.json @@ -54,6 +54,7 @@ "rimraf": "^2.6.2" }, "peerDependencies": { + "@sanity/base": "^0.141.6", "prop-types": "^15.6 || ^16", "react": "^16.3", "react-dom": "^16.3" diff --git a/packages/@sanity/default-layout/src/components/ActionModal.js b/packages/@sanity/default-layout/src/components/ActionModal.js index 28ac07857c7..16a594ecb6d 100644 --- a/packages/@sanity/default-layout/src/components/ActionModal.js +++ b/packages/@sanity/default-layout/src/components/ActionModal.js @@ -15,14 +15,12 @@ function ActionModal(props) {

{title}

{ - return { - ...action, - key: `actionModal_${i}`, - icon: action.icon || FileIcon, - onClick: onClose - } - })} + items={actions.map((action, i) => ({ + ...action, + key: `actionModal_${i}`, + icon: action.icon || FileIcon, + onClick: onClose + }))} />
diff --git a/packages/@sanity/default-layout/src/components/DefaultLayout.js b/packages/@sanity/default-layout/src/components/DefaultLayout.js index 373b0dcb67e..1fe1ffdaee4 100644 --- a/packages/@sanity/default-layout/src/components/DefaultLayout.js +++ b/packages/@sanity/default-layout/src/components/DefaultLayout.js @@ -1,32 +1,24 @@ import PropTypes from 'prop-types' import React from 'react' -import {startCase} from 'lodash' import schema from 'part:@sanity/base/schema' -import DataAspectsResolver from 'part:@sanity/data-aspects/resolver?' import AppLoadingScreen from 'part:@sanity/base/app-loading-screen' import {RouteScope, withRouterHOC} from 'part:@sanity/base/router' import absolutes from 'all:part:@sanity/base/absolutes' import {isActionEnabled} from 'part:@sanity/base/util/document-action-utils' import userStore from 'part:@sanity/base/user' import styles from './styles/DefaultLayout.css' +import SideMenu from './SideMenu' import RenderTool from './RenderTool' import ActionModal from './ActionModal' import NavBarContainer from './NavBarContainer' import {SchemaErrorReporter} from './SchemaErrorReporter' -import SideMenu from './SideMenu' - -let dataAspects -if (DataAspectsResolver) { - dataAspects = new DataAspectsResolver(schema) -} +import {getTemplatesBySchemaType} from '@sanity/base/initial-value-templates' function getDocumentTypeNames() { - return dataAspects - ? dataAspects.getInferredTypes() - : schema.getTypeNames().filter(typeName => { - const schemaType = schema.get(typeName) - return schemaType.type && schemaType.type.name === 'document' - }) + return schema.getTypeNames().filter(typeName => { + const schemaType = schema.get(typeName) + return schemaType.type && schemaType.type.name === 'document' + }) } export default withRouterHOC( @@ -147,13 +139,16 @@ export default withRouterHOC( const modalActions = getDocumentTypeNames() .map(typeName => schema.get(typeName)) .filter(type => isActionEnabled(type, 'create')) - .map(type => ({ - title: dataAspects - ? dataAspects.getDisplayName(type.name) - : type.title || startCase(type.name), - icon: dataAspects ? dataAspects.getIcon(type.name) : type.icon, - params: {type: type.name} - })) + .reduce((actions, type) => { + const templates = getTemplatesBySchemaType(type.name) + return actions.concat( + templates.map(tpl => ({ + title: tpl.title, + icon: tpl.icon || type.icon, + params: {template: tpl.id, type: tpl.schemaType} + })) + ) + }, []) const isOverlayVisible = menuIsOpen || searchIsOpen let className = styles.root diff --git a/packages/@sanity/default-layout/src/components/SchemaErrorReporter.js b/packages/@sanity/default-layout/src/components/SchemaErrorReporter.js index 27203e218d1..a2bd2a58870 100644 --- a/packages/@sanity/default-layout/src/components/SchemaErrorReporter.js +++ b/packages/@sanity/default-layout/src/components/SchemaErrorReporter.js @@ -3,7 +3,7 @@ import PropTypes from 'prop-types' import schema from 'part:@sanity/base/schema' import InitialValueTemplateError from './InitialValueTemplateError' import SchemaErrors from './SchemaErrors' -import {getTemplateErrors} from '@sanity/base/initial-values' +import {getTemplateErrors} from '@sanity/base/initial-value-templates' function renderPath(path) { return path diff --git a/packages/@sanity/desk-tool/src/DeskTool.js b/packages/@sanity/desk-tool/src/DeskTool.js index 18014b62523..30951e8b8bb 100644 --- a/packages/@sanity/desk-tool/src/DeskTool.js +++ b/packages/@sanity/desk-tool/src/DeskTool.js @@ -112,7 +112,8 @@ export default withRouterHOC( maybeAddEditorPane = (panes, props) => { const router = props.router - const {editDocumentId, type} = router.state + const {editDocumentId, type, params} = router.state + const template = params && params.template if (!editDocumentId) { return observableOf(panes) @@ -121,7 +122,7 @@ export default withRouterHOC( const editor = { id: 'editor', type: 'document', - options: {id: editDocumentId, type} + options: {id: editDocumentId, type, template} } if (type !== '*') { diff --git a/packages/@sanity/desk-tool/src/index.js b/packages/@sanity/desk-tool/src/index.js index 81fe6f4dd4c..a2bc9d9af8e 100644 --- a/packages/@sanity/desk-tool/src/index.js +++ b/packages/@sanity/desk-tool/src/index.js @@ -4,6 +4,7 @@ import {route} from 'part:@sanity/base/router' import DeskTool from './DeskTool' import {parsePanesSegment} from './utils/parsePanesSegment' import UUID from '@sanity/uuid' +import {getTemplateById} from '@sanity/base/initial-value-templates' function maybeRemapStringSegment(segment) { return typeof segment === 'string' ? {id: segment} : segment @@ -26,6 +27,19 @@ function toPath(panes) { .join(';') } +function paramsToState(params) { + try { + return JSON.parse(decodeURIComponent(params)) + } catch (err) { + console.warn('Failed to parse parameters') + return {} + } +} + +function paramsToPath(params) { + return JSON.stringify(params) +} + const state = {activePanes: []} function setActivePanes(panes) { @@ -57,15 +71,18 @@ function getIntentState(intentName, params, currentState) { function getFallbackIntentState({documentId, intentName, params}) { const editDocumentId = documentId const isTemplateCreate = intentName === 'create' && params.template + const template = isTemplateCreate && getTemplateById(params.template) return isTemplateCreate - ? {editDocumentId, template: params.template} + ? {editDocumentId, type: template.schemaType, params: {template: params.template}} : {editDocumentId, type: params.type || '*'} } export default { router: route('/', [ - route('/edit/:type/:editDocumentId'), + route('/edit/:type/:editDocumentId', [ + route({path: '/:params', transform: {params: {toState: paramsToState, toPath: paramsToPath}}}) + ]), route({ path: '/:panes', // Legacy URLs, used to handle redirects diff --git a/packages/@sanity/desk-tool/src/utils/withInitialValue.js b/packages/@sanity/desk-tool/src/utils/withInitialValue.js index d0f53ecfaa4..dad8906bed2 100644 --- a/packages/@sanity/desk-tool/src/utils/withInitialValue.js +++ b/packages/@sanity/desk-tool/src/utils/withInitialValue.js @@ -11,7 +11,7 @@ import { getTemplateById, getTemplatesBySchemaType, resolveInitialValue -} from '@sanity/base/initial-values' +} from '@sanity/base/initial-value-templates' // Resolves the initial value for a given template, if possible export default function withInitialValue(Pane) { diff --git a/packages/@sanity/structure/src/index.ts b/packages/@sanity/structure/src/index.ts index ac292508be7..5f51a434321 100644 --- a/packages/@sanity/structure/src/index.ts +++ b/packages/@sanity/structure/src/index.ts @@ -50,12 +50,12 @@ const StructureBuilder = { divider: (): Divider => ({id: uniqueId('__divider__'), type: 'divider'}) } -function hasIcon(schemaType?: SchemaType | string) { +function hasIcon(schemaType?: SchemaType | string): boolean { if (!schemaType || typeof schemaType === 'string') { return false } - return schemaType.icon + return Boolean(schemaType.icon) } function getDefaultStructure(): ListBuilder { diff --git a/packages/@sanity/structure/test/templates/Template.test.ts b/packages/@sanity/structure/test/templates/Template.test.ts deleted file mode 100644 index 56d24650b8f..00000000000 --- a/packages/@sanity/structure/test/templates/Template.test.ts +++ /dev/null @@ -1,95 +0,0 @@ -import T from '../../src/templates' - -const icon = () => null - -describe('T.template()', () => { - test('builds template through constructor', () => { - expect( - T.template({ - id: 'foo', - title: 'some title', - schemaType: 'author', - value: {name: 'Default name!'}, - icon - }).serialize() - ).toMatchSnapshot() - }) - - test('throws on missing id', () => { - expect(() => T.template().serialize()).toThrowError(/required "id"/) - }) - - test('throws on missing title', () => { - expect(() => - T.template() - .id('id') - .serialize() - ).toThrowError(/required "title"/) - }) - - test('throws on missing schemaType', () => { - expect(() => - T.template() - .id('id') - .title('Blah') - .serialize() - ).toThrowError(/required "schemaType"/) - }) - - test('throws on missing value', () => { - expect(() => - T.template() - .id('id') - .title('Blah') - .schemaType('author') - .serialize() - ).toThrowError(/required "value"/) - }) - - test('can construct using builder', () => { - expect( - T.template() - .id('yeah') - .title('Yeah') - .schemaType('author') - .icon(icon) - .value({name: 'bar'}) - .serialize() - ).toMatchSnapshot() - }) - - test('builder is immutable', () => { - const original = T.template() - expect(original.id('foo')).not.toEqual(original) - expect(original.title('foo')).not.toEqual(original) - expect(original.schemaType('author')).not.toEqual(original) - expect(original.icon(icon)).not.toEqual(original) - expect(original.value({name: 'foo'})).not.toEqual(original) - }) - - test('getters work', () => { - const original = T.template() - expect(original.id('foo').getId()).toEqual('foo') - expect(original.title('bar').getTitle()).toEqual('bar') - expect(original.schemaType('author').getSchemaType()).toEqual('author') - expect(original.icon(icon).getIcon()).toEqual(icon) - expect(original.value({name: 'bar'}).getValue()).toEqual({name: 'bar'}) - }) -}) - -describe('T.defaultTemplateForType()', () => { - test('generates correct representation of type without initial value', () => { - expect(T.defaultTemplateForType('author').serialize()).toMatchSnapshot() - }) - - test('generates correct representation of type with initial value', () => { - expect(T.defaultTemplateForType('post').serialize()).toMatchSnapshot() - }) -}) - -describe('T.defaults()', () => { - test('generates array of all schema type templates', () => { - expect(T.defaults()).toMatchSnapshot() - expect(T.defaults().map(tpl => tpl.serialize())).toMatchSnapshot() - }) -}) diff --git a/packages/@sanity/structure/test/templates/__snapshots__/Template.test.ts.snap b/packages/@sanity/structure/test/templates/__snapshots__/Template.test.ts.snap deleted file mode 100644 index 38ac56f7d19..00000000000 --- a/packages/@sanity/structure/test/templates/__snapshots__/Template.test.ts.snap +++ /dev/null @@ -1,108 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`T.defaultTemplateForType() generates correct representation of type with initial value 1`] = ` -Object { - "icon": [Function], - "id": "post", - "schemaType": "post", - "title": "Post", - "value": Object { - "slug": Object { - "_type": "slug", - "current": "default-slug", - }, - }, -} -`; - -exports[`T.defaultTemplateForType() generates correct representation of type without initial value 1`] = ` -Object { - "icon": undefined, - "id": "author", - "schemaType": "author", - "title": "Author", - "value": Object { - "_type": "author", - }, -} -`; - -exports[`T.defaults() generates array of all schema type templates 1`] = ` -Array [ - TemplateBuilder { - "spec": Object { - "icon": undefined, - "id": "author", - "schemaType": "author", - "title": "Author", - "value": Object { - "_type": "author", - }, - }, - }, - TemplateBuilder { - "spec": Object { - "icon": [Function], - "id": "post", - "schemaType": "post", - "title": "Post", - "value": Object { - "slug": Object { - "_type": "slug", - "current": "default-slug", - }, - }, - }, - }, -] -`; - -exports[`T.defaults() generates array of all schema type templates 2`] = ` -Array [ - Object { - "icon": undefined, - "id": "author", - "schemaType": "author", - "title": "Author", - "value": Object { - "_type": "author", - }, - }, - Object { - "icon": [Function], - "id": "post", - "schemaType": "post", - "title": "Post", - "value": Object { - "slug": Object { - "_type": "slug", - "current": "default-slug", - }, - }, - }, -] -`; - -exports[`T.template() builds template through constructor 1`] = ` -Object { - "icon": [Function], - "id": "foo", - "schemaType": "author", - "title": "some title", - "value": Object { - "name": "Default name!", - }, -} -`; - -exports[`T.template() can construct using builder 1`] = ` -Object { - "icon": [Function], - "id": "yeah", - "schemaType": "author", - "title": "Yeah", - "value": Object { - "name": "bar", - }, -} -`; diff --git a/packages/test-studio/schemas/author.js b/packages/test-studio/schemas/author.js index f51937e201f..025f2ccd682 100644 --- a/packages/test-studio/schemas/author.js +++ b/packages/test-studio/schemas/author.js @@ -102,7 +102,7 @@ export default { initialValue: () => new Promise(resolve => - setTimeout(resolve, 10000, { + setTimeout(resolve, 2500, { name: 'Foo', bestFriend: {_type: 'reference', _ref: 'foo-bar'}, image: {