-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[structure] Allow specifying a default document node resolver (#1637)
* [structure] Allow specifying a default document fragment resolver * [desk-tool] Warn when structure exports unknown properties * [test-studio] Add example of default document fragment resolver * [structure] Rename to getDefaultDocumentNode
- Loading branch information
Showing
15 changed files
with
150 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {DocumentBuilder} from '../Document' | ||
import {DocumentNode} from '../StructureNodes' | ||
import {DocumentFragmentResolveOptions} from '../userDefinedStructure' | ||
|
||
interface UserDefinedStructure { | ||
getDefaultDocumentNode?: ( | ||
options: DocumentFragmentResolveOptions | ||
) => DocumentNode | DocumentBuilder | null | ||
} | ||
|
||
export function getUserDefinedStructure(): UserDefinedStructure | undefined { | ||
return require('part:@sanity/desk-tool/structure?') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {DocumentBuilder} from './Document' | ||
import {DocumentNode} from './StructureNodes' | ||
import {getUserDefinedStructure} from './parts/userStructure' | ||
|
||
export interface DocumentFragmentResolveOptions { | ||
documentId?: string | ||
schemaType: string | ||
} | ||
|
||
export const getUserDefinedDefaultDocumentBuilder = ( | ||
options: DocumentFragmentResolveOptions | ||
): DocumentBuilder | null => { | ||
const structure = getUserDefinedStructure() | ||
if (!structure || !structure.getDefaultDocumentNode) { | ||
return null | ||
} | ||
|
||
if (typeof structure.getDefaultDocumentNode !== 'function') { | ||
throw new Error('Structure export `getDefaultDocumentNode` must be a function') | ||
} | ||
|
||
const documentNode = structure.getDefaultDocumentNode(options) | ||
|
||
if (!documentNode) { | ||
return null | ||
} | ||
|
||
const isBuilder = typeof (documentNode as DocumentBuilder).serialize === 'function' | ||
if (!isBuilder && (documentNode as DocumentNode).type !== 'document') { | ||
throw new Error('`getDefaultDocumentNode` must return a document or a document builder') | ||
} | ||
|
||
return isBuilder | ||
? (documentNode as DocumentBuilder) | ||
: new DocumentBuilder(documentNode as DocumentNode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters