-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(visual-editing): add svelte optimistic state support (#2307)
* feat(visual-editing): add optimistic export path * feat(visual-editing): add svelte optimistic state support * chore(apps): use svelte optimistic state * chore: migrate useDatasetMutator hook * chore: fix types and merge conflict regressions * chore: handle deprecated exports --------- Co-authored-by: Cody Olsen <stipsan@gmail.com>
- Loading branch information
Showing
25 changed files
with
432 additions
and
139 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
2 changes: 1 addition & 1 deletion
2
...diting/src/ui/optimistic-state/context.ts → .../visual-editing/src/optimistic/context.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
export type {VisualEditingNode} from '../types' | ||
export type { | ||
DocumentSchema, | ||
HistoryRefresh, | ||
HistoryUpdate, | ||
PreviewSnapshot, | ||
ResolvedSchemaTypeMap, | ||
SanityNode, | ||
SanityStegaNode, | ||
SchemaArrayItem, | ||
SchemaArrayNode, | ||
SchemaBooleanNode, | ||
SchemaInlineNode, | ||
SchemaNode, | ||
SchemaNullNode, | ||
SchemaNumberNode, | ||
SchemaObjectField, | ||
SchemaObjectNode, | ||
SchemaStringNode, | ||
SchemaType, | ||
SchemaUnionNode, | ||
SchemaUnionNodeOptions, | ||
SchemaUnionOption, | ||
SchemaUnknownNode, | ||
Serializable, | ||
SerializableArray, | ||
SerializableObject, | ||
SerializablePrimitive, | ||
TypeSchema, | ||
UnresolvedPath, | ||
VisualEditingControllerMsg, | ||
VisualEditingNodeMsg, | ||
} from '@repo/visual-editing-helpers' | ||
export type { | ||
DocumentsGet, | ||
DocumentsMutate, | ||
OptimisticDocument, | ||
OptimisticDocumentPatches, | ||
OptimisticReducerAction, | ||
Path, | ||
PathValue, | ||
OptimisticReducer, | ||
} from './types' | ||
export { | ||
type MutatorActor, | ||
type EmptyActor, | ||
emptyActor, | ||
actor, | ||
listeners, | ||
isEmptyActor, | ||
setActor, | ||
} from './context' | ||
export {createDatasetMutator, type DatasetMutatorMachineInput} from './state/datasetMutator' | ||
export {createDocumentMutator} from './state/documentMutator' |
2 changes: 1 addition & 1 deletion
2
...ic-state/machines/createSharedListener.ts → .../optimistic/state/createSharedListener.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
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,77 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import type {SanityDocument} from '@sanity/client' | ||
import {type Mutation, type NodePatchList} from '@sanity/mutate' | ||
|
||
export type Path<T, K extends keyof T> = K extends string | ||
? T[K] extends Record<string, any> | ||
? `${K}.${Path<T[K], keyof T[K]>}` | K | ||
: K | ||
: never | ||
|
||
export type PathValue<T, P extends string> = P extends `${infer K}.${infer Rest}` | ||
? K extends keyof T | ||
? PathValue<T[K], Rest> | ||
: never | ||
: P extends keyof T | ||
? T[P] | ||
: never | ||
|
||
export type DocumentsMutate = ( | ||
documentId: string, | ||
mutations: Mutation[], | ||
options?: {commit?: boolean | {debounce: number}}, | ||
) => void | ||
|
||
export type DocumentsGet = <T extends Record<string, any>>( | ||
documentId: string, | ||
) => OptimisticDocument<T> | ||
|
||
export type OptimisticDocumentPatches<T extends Record<string, any> = Record<string, any>> = | ||
| ((context: { | ||
draftId: string | ||
publishedId: string | ||
/** | ||
* @deprecated - use `getSnapshot` instead | ||
*/ | ||
snapshot: SanityDocument<T> | undefined | ||
getSnapshot: () => Promise<SanityDocument<T> | null> | ||
}) => Promise<NodePatchList> | NodePatchList) | ||
| NodePatchList | ||
|
||
export type OptimisticDocument<T extends Record<string, any> = Record<string, any>> = { | ||
/** | ||
* The document ID | ||
*/ | ||
id: string | ||
/** | ||
* Commits any locally applied mutations to the remote document | ||
*/ | ||
commit: () => void | ||
/** | ||
* @deprecated - use `getSnapshot` instead | ||
*/ | ||
get: { | ||
(): SanityDocument<T> | undefined | ||
<P extends Path<T, keyof T>>(path: P): PathValue<T, P> | undefined | ||
} | ||
/** | ||
* Returns a promise that resolves to the current document snapshot | ||
*/ | ||
getSnapshot: () => Promise<SanityDocument<T> | null> | ||
/** | ||
* Applies the given patches to the document | ||
*/ | ||
patch: ( | ||
patches: OptimisticDocumentPatches<T>, | ||
options?: {commit?: boolean | {debounce: number}}, | ||
) => void | ||
} | ||
|
||
export type OptimisticReducerAction<T> = { | ||
document: T | ||
id: string | ||
originalId: string | ||
type: 'appear' | 'mutate' | 'disappear' | ||
} | ||
|
||
export type OptimisticReducer<T, U> = (state: T, action: OptimisticReducerAction<U>) => T |
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
Oops, something went wrong.