Skip to content

Commit

Permalink
[desk-tool] Add document ID to structure node ID for fallback pane
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Nov 19, 2019
1 parent da5b831 commit a3b98df
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
14 changes: 9 additions & 5 deletions packages/@sanity/desk-tool/src/tool.js
Expand Up @@ -51,14 +51,18 @@ function getIntentState(intentName, params, currentState, payload) {
function getFallbackIntentState({documentId, intentName, params, payload}) {
const isTemplateCreate = intentName === 'create' && params.template
const template = isTemplateCreate && getTemplateById(params.template)
const parameters = {
id: documentId,
template: params.template,
type: (template && template.schemaType) || params.type
const type = (template && template.schemaType) || params.type
const parameters = {}
if (type) {
parameters.type = type
}

if (template) {
parameters.template = template
}

return {
panes: [[{id: '__edit__', params: parameters, payload}]]
panes: [[{id: `__edit__${documentId}`, params: parameters, payload}]]
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/desk-tool/src/utils/parsePanesSegment.js
Expand Up @@ -44,7 +44,7 @@ function encodeChunks(pane, i, group) {
return pairs
}

return [...pairs, `${key}=${params[key]}`]
return params[key] ? [...pairs, `${key}=${params[key]}`] : pairs
}, [])

return (
Expand Down
5 changes: 3 additions & 2 deletions packages/@sanity/desk-tool/src/utils/resolvePanes.js
Expand Up @@ -80,7 +80,7 @@ function resolveForStructure(structure, paneGroups, prevStructure, fromIndex) {
const siblings = paneSegments[index]
for (let i = splitIndex; i < siblings.length; i++) {
const {id} = siblings[i]
const isFallbackEditor = index === 1 && id === '__edit__'
const isFallbackEditor = index === 1 && id.startsWith('__edit__')
const child = isFallbackEditor ? resolveFallbackEditor : parent.child
const resolverArgs = getResolverArgumentsForSibling(siblings[i], context, isFallbackEditor)
subscribeForUpdates(child, index, i, context, resolverArgs)
Expand Down Expand Up @@ -177,7 +177,8 @@ function resolveForStructure(structure, paneGroups, prevStructure, fromIndex) {
}

function resolveFallbackEditor(nodeId, context, {params, payload}) {
const {id, template, type} = params
const id = nodeId.replace(/^__edit__/, '')
const {template, type} = params

return {
id: 'editor',
Expand Down
9 changes: 6 additions & 3 deletions packages/@sanity/structure/src/util/validateId.ts
@@ -1,7 +1,6 @@
import {SerializeError} from '../SerializeError'
import {SerializePath} from '../StructureNodes'

const disallowedIds = ['__edit__']
const disallowedPattern = /([^A-Za-z0-9-_.])/

export function validateId(
Expand All @@ -26,8 +25,12 @@ export function validateId(
)
}

if (disallowedIds.includes(id)) {
throw new SerializeError(`Structure node id cannot be "${id}"`, parentPath, pathSegment)
if (id.startsWith('__edit__')) {
throw new SerializeError(
`Structure node id cannot start with __edit__`,
parentPath,
pathSegment
)
}

return id
Expand Down

0 comments on commit a3b98df

Please sign in to comment.