Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chore] Move preview into base (#2212)
- Loading branch information
Showing
54 changed files
with
103 additions
and
352 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
2 changes: 1 addition & 1 deletion
2
packages/@sanity/base/src/datastores/document/useDocumentValues.ts
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
62 changes: 62 additions & 0 deletions
62
packages/@sanity/base/src/preview/createPreviewObserver.ts
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,62 @@ | ||
import {of as observableOf, Observable} from 'rxjs' | ||
import {map, switchMap} from 'rxjs/operators' | ||
import {isReferenceSchemaType, ReferenceSchemaType, SchemaType} from '@sanity/types' | ||
import prepareForPreview, {invokePrepare, PrepareInvocationResult} from './prepareForPreview' | ||
import {FieldName, Path, Reference, PrepareViewOptions} from './types' | ||
|
||
export interface PreviewValue { | ||
type?: SchemaType | ||
snapshot: null | PrepareInvocationResult | ||
} | ||
|
||
// Takes a value and its type and prepares a snapshot for it that can be passed to a preview component | ||
export function createPreviewObserver( | ||
observePaths: (value: any, paths: Path[]) => any, | ||
resolveRefType: ( | ||
value: Reference, | ||
ownerType: ReferenceSchemaType | ||
) => Observable<SchemaType | undefined> | ||
) { | ||
return function observeForPreview( | ||
value: any, | ||
type: SchemaType, | ||
fields: FieldName[], | ||
viewOptions?: PrepareViewOptions | ||
): Observable<PreviewValue> { | ||
if (isReferenceSchemaType(type)) { | ||
// if the value is of type reference, but has no _ref property, we cannot prepare any value for the preview | ||
// and the most sane thing to do is to return `null` for snapshot | ||
if (!value._ref) { | ||
return observableOf({snapshot: null}) | ||
} | ||
// Previewing references actually means getting the referenced value, | ||
// and preview using the preview config of its type | ||
// todo: We need a way of knowing the type of the referenced value by looking at the reference record alone | ||
return resolveRefType(value, type).pipe( | ||
switchMap((refType) => | ||
refType | ||
? observeForPreview(value, refType, fields) | ||
: observableOf<PreviewValue>({ | ||
type: type, | ||
snapshot: null, | ||
}) | ||
) | ||
) | ||
} | ||
|
||
const selection = type.preview?.select | ||
if (selection) { | ||
const paths = Object.keys(selection).map((key) => selection[key].split('.')) | ||
return observePaths(value, paths).pipe( | ||
map((snapshot) => ({ | ||
type: type, | ||
snapshot: snapshot && prepareForPreview(snapshot, type, viewOptions), | ||
})) | ||
) | ||
} | ||
return observableOf({ | ||
type: type, | ||
snapshot: invokePrepare(type, value, viewOptions), | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,14 @@ | ||
import observeFields from './observeFields' | ||
import resolveRefType from './utils/resolveRefType' | ||
import {createPathObserver} from './createPathObserver' | ||
import {createPreviewObserver} from './createPreviewObserver' | ||
|
||
export {default} from './components/SanityDefaultPreview' | ||
export {default as SanityDefaultPreview} from './components/SanityDefaultPreview' | ||
export {default as PreviewFields} from './components/PreviewFields' | ||
export {default as SanityPreview} from './components/SanityPreview' | ||
export {default as PreviewSubscriber} from './components/PreviewSubscriber' | ||
export {default as WithVisibility} from './components/WithVisibility' | ||
|
||
export const observePaths = createPathObserver(observeFields) | ||
export const observeForPreview = createPreviewObserver(observePaths, resolveRefType) |
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,22 @@ | ||
import client from 'part:@sanity/base/client' | ||
import {from as observableFrom, Observable} from 'rxjs' | ||
import {map} from 'rxjs/operators' | ||
import {Reference, ReferenceSchemaType, SchemaType} from '@sanity/types' | ||
|
||
const CACHE: Record<string, Promise<string>> = Object.create(null) // todo: use a LRU cache instead (e.g. hashlru or quick-lru) | ||
|
||
function resolveRefTypeName(reference: Reference): Observable<string> { | ||
if (!(reference._ref in CACHE)) { | ||
CACHE[reference._ref] = client.fetch('*[_id == $id][0]._type', {id: reference._ref}) | ||
} | ||
return observableFrom(CACHE[reference._ref]) | ||
} | ||
|
||
export default function resolveRefType( | ||
value: Reference, | ||
type: ReferenceSchemaType | ||
): Observable<SchemaType | undefined> { | ||
return resolveRefTypeName(value).pipe( | ||
map((refTypeName) => type.to.find((toType) => toType.name === refTypeName)) | ||
) | ||
} |
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 |
---|---|---|
|
@@ -21,9 +21,6 @@ | |
] | ||
}, | ||
"references": [ | ||
{ | ||
"path": "../preview" | ||
}, | ||
{ | ||
"path": "../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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
8a65d25
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: