Skip to content

Commit

Permalink
[default-layout] Use templates for global action modal
Browse files Browse the repository at this point in the history
[structure] Fix signature of hasIcon
[dashboard] Don't implicitly depend on @sanity/client being installed
[base] Fix incorrect require after refactoring
[structure] Fix Boolean/boolean mismatch
[structure] Remove template tests from structure
  • Loading branch information
rexxars committed Oct 24, 2019
1 parent d63181f commit ece7f38
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 244 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion 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
@@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/@sanity/default-layout/package.json
Expand Up @@ -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"
Expand Down
14 changes: 6 additions & 8 deletions packages/@sanity/default-layout/src/components/ActionModal.js
Expand Up @@ -15,14 +15,12 @@ function ActionModal(props) {
<h1 className={styles.title}>{title}</h1>
<div className={styles.listContainer}>
<CreateDocumentList
items={actions.map((action, i) => {
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
}))}
/>
</div>
</DialogContent>
Expand Down
37 changes: 16 additions & 21 deletions 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(
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions packages/@sanity/desk-tool/src/DeskTool.js
Expand Up @@ -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)
Expand All @@ -121,7 +122,7 @@ export default withRouterHOC(
const editor = {
id: 'editor',
type: 'document',
options: {id: editDocumentId, type}
options: {id: editDocumentId, type, template}
}

if (type !== '*') {
Expand Down
21 changes: 19 additions & 2 deletions packages/@sanity/desk-tool/src/index.js
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/desk-tool/src/utils/withInitialValue.js
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/@sanity/structure/src/index.ts
Expand Up @@ -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 {
Expand Down
95 changes: 0 additions & 95 deletions packages/@sanity/structure/test/templates/Template.test.ts

This file was deleted.

0 comments on commit ece7f38

Please sign in to comment.