Skip to content

Commit

Permalink
[structure] Resolve intents based on documentListItem IDs if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored and bjoerge committed Feb 19, 2020
1 parent 133f67f commit bd34ce1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/@sanity/structure/src/DocumentListItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ export class DocumentListItemBuilder extends ListItemBuilder {
return builder
}
}

export function isDocumentListItem(item: any): item is DocumentListItem {
const listItem = item as DocumentListItem
return typeof listItem.schemaType !== 'undefined' && typeof listItem._id === 'string'
}
2 changes: 1 addition & 1 deletion packages/@sanity/structure/src/GenericList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function noChildResolver() {
return undefined
}

const shallowIntentChecker: IntentChecker = (intentName, params, {pane, index}): boolean => {
export const shallowIntentChecker: IntentChecker = (intentName, params, {pane, index}): boolean => {
return index <= 1 && defaultIntentChecker(intentName, params, {pane, index})
}

Expand Down
17 changes: 16 additions & 1 deletion packages/@sanity/structure/src/List.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import {SerializePath, SerializeOptions, Divider, Collection} from './StructureN
import {ChildResolverOptions, ChildResolver} from './ChildResolver'
import {SerializeError, HELP_URL} from './SerializeError'
import {ListItem, ListItemBuilder} from './ListItem'
import {IntentChecker} from './Intent'
import {isDocumentListItem} from './DocumentListItem'
import {
GenericListBuilder,
BuildableGenericList,
GenericList,
GenericListInput
GenericListInput,
shallowIntentChecker
} from './GenericList'

const getArgType = (thing: ListItem) => {
Expand All @@ -26,6 +29,17 @@ const isListItem = (item: ListItem | Divider): item is ListItem => {
return item.type === 'listItem'
}

const defaultCanHandleIntent: IntentChecker = (intentName: string, params, context) => {
const pane = context.pane as List
const items = pane.items || []
return (
items
.filter(isDocumentListItem)
.some(item => item.schemaType.name === params.type && item._id === params.id) ||
shallowIntentChecker(intentName, params, context)
)
}

const resolveChildForItem: ChildResolver = (itemId: string, options: ChildResolverOptions) => {
const parentItem = options.parent as List
const items = parentItem.items.filter(isListItem)
Expand Down Expand Up @@ -138,6 +152,7 @@ export class ListBuilder extends GenericListBuilder<BuildableList, ListBuilder>
return {
...super.serialize(options),
type: 'list',
canHandleIntent: this.spec.canHandleIntent || defaultCanHandleIntent,
child: this.spec.child || resolveChildForItem,
items: serializedItems
}
Expand Down

1 comment on commit bd34ce1

@vercel
Copy link

@vercel vercel bot commented on bd34ce1 Feb 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.