UNPKG

@sanity/visual-editing

Version:

[![npm stat](https://img.shields.io/npm/dm/@sanity/visual-editing.svg?style=flat-square)](https://npm-stat.com/charts.html?package=@sanity/visual-editing) [![npm version](https://img.shields.io/npm/v/@sanity/visual-editing.svg?style=flat-square)](https://

1,321 lines (1,318 loc) 262 kB
import { MutationEvent as MutationEvent_2, ReconnectEvent, SanityClient, SanityDocument, WelcomeEvent, } from '@sanity/client' import type {Node as Node_2} from '@sanity/comlink' import {Mutation, NodePatchList} from '@sanity/mutate' import { createSharedListener, DocumentMutatorMachineInput, Mutation as Mutation_2, MutationGroup, RawPatch, SanityDocumentBase, Transaction, } from '@sanity/mutate/_unstable_machine' import { 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 '@sanity/presentation-comlink' import {Observable} from 'rxjs' import { ActorRef, ActorRefFrom, ActorRefFromLogic, AnyActorLogic, AnyActorRef, AnyEventObject, ConditionalRequired, EventObject, GetConcreteByKey, InputFrom, IsNotNever, MachineSnapshot, MetaObject, NonReducibleUnknown, ObservableActorLogic, PromiseActorLogic, RequiredActorOptions, RequiredLogicInput, Snapshot, StateMachine, StateValue, Values, } from 'xstate' export declare let actor: MutatorActor | EmptyActor export declare const createDatasetMutator: (comlink: VisualEditingNode) => StateMachine< { client: SanityClient /** A shared listener can be provided, if not it'll be created using `client.listen()` */ sharedListener?: ReturnType<typeof createSharedListener> documents: Record<string, ActorRefFrom<ReturnType<typeof createDocumentMutator>>> }, | { type: 'sync' id: string document: SanityDocumentBase } | { type: 'mutation' id: string effects: { apply: RawPatch } previousRev: string resultRev: string } | { type: 'rebased.local' id: string document: SanityDocumentBase } | { type: 'rebased.remote' id: string document: SanityDocumentBase } | { type: 'pristine' id: string } | { type: 'observe' documentId: string } | { type: 'unobserve' documentId: string } | { type: 'add document actor' documentId: string } | { type: 'stop document actor' documentId: string }, { [x: string]: | ActorRefFromLogic< StateMachine< { client: SanityClient sharedListener?: ReturnType<typeof createSharedListener> id: string cache?: Map<string, SanityDocument<DocumentType> | null> remote: SanityDocument<DocumentType> | null | undefined local: SanityDocument<DocumentType> | null | undefined mutationEvents: MutationEvent_2[] stagedChanges: MutationGroup[] stashedChanges: MutationGroup[] error: unknown fetchRemoteSnapshotAttempts: number submitTransactionsAttempts: number }, | WelcomeEvent | ReconnectEvent | MutationEvent_2 | { type: 'error' } | { type: 'retry' } | { type: 'connect' } | { type: 'reconnect' } | { type: 'welcome' } | { type: 'mutate' mutations: Mutation_2[] } | { type: 'submit' } | { type: 'xstate.done.actor.getDocument' output: SanityDocument<DocumentType> } | { type: 'xstate.done.actor.submitTransactions' output: undefined }, { [x: string]: | ActorRefFromLogic< PromiseActorLogic< void | SanityDocument<Record<string, any>> | undefined, { client: SanityClient id: string }, EventObject > > | ActorRefFromLogic< PromiseActorLogic< void, { client: SanityClient transactions: Transaction[] }, EventObject > > | ActorRefFromLogic< ObservableActorLogic< WelcomeEvent | ReconnectEvent | MutationEvent_2, { listener: ReturnType<typeof createSharedListener> id: string }, EventObject > > | undefined getDocument?: | ActorRefFromLogic< PromiseActorLogic< void | SanityDocument<Record<string, any>> | undefined, { client: SanityClient id: string }, EventObject > > | undefined submitTransactions?: | ActorRefFromLogic< PromiseActorLogic< void, { client: SanityClient transactions: Transaction[] }, EventObject > > | undefined }, Values<{ 'fetch remote snapshot': { src: 'fetch remote snapshot' logic: PromiseActorLogic< void | SanityDocument<Record<string, any>> | undefined, { client: SanityClient id: string }, EventObject > id: 'getDocument' } 'submit mutations as transactions': { src: 'submit mutations as transactions' logic: PromiseActorLogic< void, { client: SanityClient transactions: Transaction[] }, EventObject > id: 'submitTransactions' } 'server-sent events': { src: 'server-sent events' logic: ObservableActorLogic< ReconnectEvent | WelcomeEvent | MutationEvent_2, { listener: ReturnType<typeof createSharedListener> id: string }, EventObject > id: string | undefined } }>, Values<{ 'assign error to context': { type: 'assign error to context' params: NonReducibleUnknown } 'clear error from context': { type: 'clear error from context' params: NonReducibleUnknown } 'connect to server-sent events': { type: 'connect to server-sent events' params: NonReducibleUnknown } 'listen to server-sent events': { type: 'listen to server-sent events' params: NonReducibleUnknown } 'stop listening to server-sent events': { type: 'stop listening to server-sent events' params: NonReducibleUnknown } 'buffer remote mutation events': { type: 'buffer remote mutation events' params: NonReducibleUnknown } 'restore stashed changes': { type: 'restore stashed changes' params: NonReducibleUnknown } 'rebase fetched remote snapshot': { type: 'rebase fetched remote snapshot' params: NonReducibleUnknown } 'apply mendoza patch': { type: 'apply mendoza patch' params: NonReducibleUnknown } 'increment fetch attempts': { type: 'increment fetch attempts' params: NonReducibleUnknown } 'reset fetch attempts': { type: 'reset fetch attempts' params: NonReducibleUnknown } 'increment submit attempts': { type: 'increment submit attempts' params: NonReducibleUnknown } 'reset submit attempts': { type: 'reset submit attempts' params: NonReducibleUnknown } 'stage mutation': { type: 'stage mutation' params: NonReducibleUnknown } 'stash mutation': { type: 'stash mutation' params: NonReducibleUnknown } 'rebase local snapshot': { type: 'rebase local snapshot' params: NonReducibleUnknown } 'send pristine event to parent': { type: 'send pristine event to parent' params: NonReducibleUnknown } 'send sync event to parent': { type: 'send sync event to parent' params: NonReducibleUnknown } 'send mutation event to parent': { type: 'send mutation event to parent' params: NonReducibleUnknown } }>, never, 'fetchRemoteSnapshotTimeout' | 'submitTransactionsTimeout', | 'disconnected' | 'connecting' | 'reconnecting' | 'connectFailure' | { connected: | 'loading' | 'loadFailure' | { loaded: 'pristine' | 'dirty' | 'submitting' | 'submitFailure' } }, 'error' | 'busy' | 'ready', DocumentMutatorMachineInput, NonReducibleUnknown, EventObject, MetaObject, { readonly id: 'document-mutator' readonly context: ({ input, }: { spawn: { < TSrc extends | 'fetch remote snapshot' | 'submit mutations as transactions' | 'server-sent events', >( logic: TSrc, ...[options]: | ({ src: 'fetch remote snapshot' logic: PromiseActorLogic< void | SanityDocument<Record<string, any>> | undefined, { client: SanityClient id: string }, EventObject > id: 'getDocument' } extends infer T ? T extends { src: 'fetch remote snapshot' logic: PromiseActorLogic< void | SanityDocument<Record<string, any>> | undefined, { client: SanityClient id: string }, EventObject > id: 'getDocument' } ? T extends { src: TSrc } ? ConditionalRequired< [ options?: | ({ id?: T['id'] | undefined systemId?: string input?: InputFrom<T['logic']> | undefined syncSnapshot?: boolean } & {[K in RequiredActorOptions<T>]: unknown}) | undefined, ], IsNotNever<RequiredActorOptions<T>> > : never : never : never) | ({ src: 'submit mutations as transactions' logic: PromiseActorLogic< void, { client: SanityClient transactions: Transaction[] }, EventObject > id: 'submitTransactions' } extends infer T_1 ? T_1 extends { src: 'submit mutations as transactions' logic: PromiseActorLogic< void, { client: SanityClient transactions: Transaction[] }, EventObject > id: 'submitTransactions' } ? T_1 extends { src: TSrc } ? ConditionalRequired< [ options?: | ({ id?: T_1['id'] | undefined systemId?: string input?: InputFrom<T_1['logic']> | undefined syncSnapshot?: boolean } & {[K_1 in RequiredActorOptions<T_1>]: unknown}) | undefined, ], IsNotNever<RequiredActorOptions<T_1>> > : never : never : never) | ({ src: 'server-sent events' logic: ObservableActorLogic< ReconnectEvent | WelcomeEvent | MutationEvent_2, { listener: ReturnType<typeof createSharedListener> id: string }, EventObject > id: string | undefined } extends infer T_2 ? T_2 extends { src: 'server-sent events' logic: ObservableActorLogic< ReconnectEvent | WelcomeEvent | MutationEvent_2, { listener: ReturnType<typeof createSharedListener> id: string }, EventObject > id: string | undefined } ? T_2 extends { src: TSrc } ? ConditionalRequired< [ options?: | ({ id?: T_2['id'] | undefined systemId?: string input?: InputFrom<T_2['logic']> | undefined syncSnapshot?: boolean } & {[K_2 in RequiredActorOptions<T_2>]: unknown}) | undefined, ], IsNotNever<RequiredActorOptions<T_2>> > : never : never : never) ): ActorRefFromLogic< GetConcreteByKey< Values<{ 'fetch remote snapshot': { src: 'fetch remote snapshot' logic: PromiseActorLogic< void | SanityDocument<Record<string, any>> | undefined, { client: SanityClient id: string }, EventObject > id: 'getDocument' } 'submit mutations as transactions': { src: 'submit mutations as transactions' logic: PromiseActorLogic< void, { client: SanityClient transactions: Transaction[] }, EventObject > id: 'submitTransactions' } 'server-sent events': { src: 'server-sent events' logic: ObservableActorLogic< ReconnectEvent | WelcomeEvent | MutationEvent_2, { listener: ReturnType<typeof createSharedListener> id: string }, EventObject > id: string | undefined } }>, 'src', TSrc >['logic'] > <TLogic extends AnyActorLogic>( src: TLogic, options?: | { id?: never systemId?: string input?: InputFrom<TLogic> | undefined syncSnapshot?: boolean } | undefined, ): ActorRefFromLogic<TLogic> } input: DocumentMutatorMachineInput self: ActorRef< MachineSnapshot< { client: SanityClient sharedListener?: ReturnType<typeof createSharedListener> id: string cache?: Map<string, SanityDocument<DocumentType> | null> remote: SanityDocument<DocumentType> | null | undefined local: SanityDocument<DocumentType> | null | undefined mutationEvents: MutationEvent_2[] stagedChanges: MutationGroup[] stashedChanges: MutationGroup[] error: unknown fetchRemoteSnapshotAttempts: number submitTransactionsAttempts: number }, | ReconnectEvent | WelcomeEvent | MutationEvent_2 | { type: 'error' } | { type: 'retry' } | { type: 'connect' } | { type: 'reconnect' } | { type: 'welcome' } | { type: 'mutate' mutations: Mutation_2[] } | { type: 'submit' } | { type: 'xstate.done.actor.getDocument' output: SanityDocument<DocumentType> } | { type: 'xstate.done.actor.submitTransactions' output: undefined }, Record<string, AnyActorRef | undefined>, StateValue, string, unknown, any, any >, | ReconnectEvent | WelcomeEvent | MutationEvent_2 | { type: 'error' } | { type: 'retry' } | { type: 'connect' } | { type: 'reconnect' } | { type: 'welcome' } | { type: 'mutate' mutations: Mutation_2[] } | { type: 'submit' } | { type: 'xstate.done.actor.getDocument' output: SanityDocument<DocumentType> } | { type: 'xstate.done.actor.submitTransactions' output: undefined }, AnyEventObject > }) => { client: SanityClient sharedListener: | Observable<ReconnectEvent | WelcomeEvent | MutationEvent_2> | undefined id: string remote: undefined local: undefined mutationEvents: never[] stagedChanges: never[] stashedChanges: never[] error: undefined fetchRemoteSnapshotAttempts: number submitTransactionsAttempts: number cache: Map<string, SanityDocument<DocumentType> | null> | undefined } readonly entry: readonly ['connect to server-sent events'] readonly on: { readonly mutate: { readonly actions: readonly ['rebase local snapshot', 'stage mutation'] } } readonly initial: 'disconnected' readonly states: { readonly disconnected: { readonly on: { readonly connect: { readonly target: 'connecting' readonly actions: readonly ['listen to server-sent events'] } } } readonly connecting: { readonly on: { readonly welcome: 'connected' readonly reconnect: 'reconnecting' readonly error: 'connectFailure' } readonly tags: readonly ['busy'] } readonly connectFailure: { readonly on: { readonly connect: { readonly target: 'connecting' readonly actions: readonly ['listen to server-sent events'] } } readonly entry: readonly [ 'stop listening to server-sent events', 'assign error to context', ] readonly exit: readonly ['clear error from context'] readonly tags: readonly ['error'] } readonly reconnecting: { readonly on: { readonly welcome: { readonly target: 'connected' } readonly error: { readonly target: 'connectFailure' } } readonly tags: readonly ['busy', 'error'] } readonly connected: { readonly on: { readonly mutation: { readonly actions: readonly ['buffer remote mutation events'] } readonly reconnect: 'reconnecting' } readonly entry: readonly ['clear error from context'] readonly initial: 'loading' readonly states: { readonly loading: { readonly invoke: { readonly src: 'fetch remote snapshot' readonly id: 'getDocument' readonly input: ({ context, }: { context: { client: SanityClient sharedListener?: ReturnType<typeof createSharedListener> id: string cache?: Map<string, SanityDocument<DocumentType> | null> remote: SanityDocument<DocumentType> | null | undefined local: SanityDocument<DocumentType> | null | undefined mutationEvents: MutationEvent_2[] stagedChanges: MutationGroup[] stashedChanges: MutationGroup[] error: unknown fetchRemoteSnapshotAttempts: number submitTransactionsAttempts: number } event: | ReconnectEvent | WelcomeEvent | MutationEvent_2 | { type: 'error' } | { type: 'retry' } | { type: 'connect' } | { type: 'reconnect' } | { type: 'welcome' } | { type: 'mutate' mutations: Mutation_2[] } | { type: 'submit' } | { type: 'xstate.done.actor.getDocument' output: SanityDocument<DocumentType> } | { type: 'xstate.done.actor.submitTransactions' output: undefined } self: ActorRef< MachineSnapshot< { client: SanityClient sharedListener?: ReturnType<typeof createSharedListener> id: string cache?: Map<string, SanityDocument<DocumentType> | null> remote: SanityDocument<DocumentType> | null | undefined local: SanityDocument<DocumentType> | null | undefined mutationEvents: MutationEvent_2[] stagedChanges: MutationGroup[] stashedChanges: MutationGroup[] error: unknown fetchRemoteSnapshotAttempts: number submitTransactionsAttempts: number }, | ReconnectEvent | WelcomeEvent | MutationEvent_2 | { type: 'error' } | { type: 'retry' } | { type: 'connect' } | { type: 'reconnect' } | { type: 'welcome' } | { type: 'mutate' mutations: Mutation_2[] } | { type: 'submit' } | { type: 'xstate.done.actor.getDocument' output: SanityDocument<DocumentType> } | { type: 'xstate.done.actor.submitTransactions' output: undefined }, Record<string, AnyActorRef>, StateValue, string, unknown, any, any >, | ReconnectEvent | WelcomeEvent | MutationEvent_2 | { type: 'error' } | { type: 'retry' } | { type: 'connect' } | { type: 'reconnect' } | { type: 'welcome' } | { type: 'mutate' mutations: Mutation_2[] } | { type: 'submit' } | { type: 'xstate.done.actor.getDocument' output: SanityDocument<DocumentType> } | { type: 'xstate.done.actor.submitTransactions' output: undefined }, AnyEventObject > }) => { client: SanityClient id: string } readonly onError: { readonly target: 'loadFailure' } readonly onDone: { readonly target: 'loaded' readonly actions: readonly [ 'rebase fetched remote snapshot', 'reset fetch attempts', ] } } readonly tags: readonly ['busy'] } readonly loaded: { readonly entry: readonly ['send sync event to parent'] readonly on: { readonly mutation: { readonly actions: readonly [ 'apply mendoza patch', 'send mutation event to parent', ] } } readonly initial: 'pristine' readonly states: { readonly pristine: { readonly on: { readonly mutate: { readonly actions: readonly ['rebase local snapshot', 'stage mutation'] readonly target: 'dirty' } } readonly tags: readonly ['ready'] } readonly dirty: { readonly on: { readonly submit: 'submitting' } readonly tags: readonly ['ready'] } readonly submitting: { readonly on: { readonly mutate: { readonly actions: readonly ['rebase local snapshot', 'stash mutation'] } } readonly invoke: { readonly src: 'submit mutations as transactions' readonly id: 'submitTransactions' readonly input: ({ context, }: { context: { client: SanityClient sharedListener?: ReturnType<typeof createSharedListener> id: string cache?: Map<string, SanityDocument<DocumentType> | null> remote: SanityDocument<DocumentType> | null | undefined local: SanityDocument<DocumentType> | null | undefined mutationEvents: MutationEvent_2[] stagedChanges: MutationGroup[] stashedChanges: MutationGroup[] error: unknown fetchRemoteSnapshotAttempts: number submitTransactionsAttempts: number } event: | ReconnectEvent | WelcomeEvent | MutationEvent_2 | { type: 'error' } | { type: 'retry' } | { type: 'connect' } | { type: 'reconnect' } | { type: 'welcome' } | { type: 'mutate' mutations: Mutation_2[] } | { type: 'submit' } | { type: 'xstate.done.actor.getDocument' output: SanityDocument<DocumentType> } | { type: 'xstate.done.actor.submitTransactions' output: undefined } self: ActorRef< MachineSnapshot< { client: SanityClient sharedListener?: ReturnType<typeof createSharedListener> id: string cache?: Map<string, SanityDocument<DocumentType> | null> remote: SanityDocument<DocumentType> | null | undefined local: SanityDocument<DocumentType> | null | undefined mutationEvents: MutationEvent_2[] stagedChanges: MutationGroup[] stashedChanges: MutationGroup[] error: unknown fetchRemoteSnapshotAttempts: number submitTransactionsAttempts: number }, | ReconnectEvent | WelcomeEvent | MutationEvent_2 | { type: 'error' } | { type: 'retry' } | { type: 'connect' } | { type: 'reconnect' } | { type: 'welcome' } | { type: 'mutate' mutations: Mutation_2[] } | { type: 'submit' } | { type: 'xstate.done.actor.getDocument' output: SanityDocument<DocumentType> } | { type: 'xstate.done.actor.submitTransactions' output: undefined }, Record<string, AnyActorRef>, StateValue, string, unknown, any, any >, | ReconnectEvent | WelcomeEvent | MutationEvent_2 | { type: 'error' } | { type: 'retry' } | { type: 'connect' } | { type: 'reconnect' } | { type: 'welcome' } | { type: 'mutate' mutations: Mutation_2[] } | { type: 'submit' } | { type: 'xstate.done.actor.getDocument' output: SanityDocument<DocumentType> } | { type: 'xstate.done.actor.submitTransactions' output: undefined }, AnyEventObject > }) => { client: SanityClient transactions: Transaction[] } readonly onError: { readonly target: 'submitFailure' } readonly onDone: { readonly target: 'pristine' readonly actions: readonly [ 'restore stashed changes', 'reset submit attempts', 'send pristine event to parent', ] } } readonly tags: readonly ['busy', 'ready'] } readonly submitFailure: { readonly exit: readonly ['clear error from context'] readonly after: { readonly submitTransactionsTimeout: { readonly actions: readonly ['increment submit attempts'] readonly target: 'submitting' } } readonly on: { readonly retry: 'submitting' } readonly tags: readonly ['error', 'ready'] } } } readonly loadFailure: { readonly exit: readonly ['clear error from context'] readonly after: { readonly fetchRemoteSnapshotTimeout: { readonly actions: readonly ['increment fetch attempts'] readonly target: 'loading' } } readonly on: { readonly retry: 'loading' } readonly tags: readonly ['error'] } } } } } > > | undefined }, { src: 'documentMutatorMachine' logic: StateMachine< { client: SanityClient sharedListener?: ReturnType<typeof createSharedListener> id: string cache?: Map<string, SanityDocument<DocumentType> | null> remote: SanityDocument<DocumentType> | null | undefined local: SanityDocument<DocumentType> | null | undefined mutationEvents: MutationEvent_2[] stagedChanges: MutationGroup[] stashedChanges: MutationGroup[] error: unknown fetchRemoteSnapshotAttempts: number submitTransactionsAttempts: number }, | WelcomeEvent | ReconnectEvent | MutationEvent_2 | { type: 'error' } | { type: 'retry' } | { type: 'connect' } | { type: 'reconnect' } | { type: 'welcome' } | { type: 'mutate' mutations: Mutation_2[] } | { type: 'submit' } | { type: 'xstate.done.actor.getDocument' output: SanityDocument<DocumentType> } | { type: 'xstate.done.actor.submitTransactions' output: undefined }, { [x: string]: | ActorRefFromLogic< PromiseActorLogic< void | SanityDocument<Record<string, any>> | undefined, { client: SanityClient id: string }, EventObject > > | ActorRefFromLogic< PromiseActorLogic< void, { client: SanityClient transactions: Transaction[] }, EventObject > > | ActorRefFromLogic< ObservableActorLogic< WelcomeEvent | ReconnectEvent | MutationEvent_2, { listener: ReturnType<typeof createSharedListener> id: string }, EventObject > > | undefined getDocument?: | ActorRefFromLogic< PromiseActorLogic< void | SanityDocument<Record<string, any>> | undefined, { client: SanityClient id: string }, EventObject > > | undefined submitTransactions?: | ActorRefFromLogic< PromiseActorLogic< void, { client: SanityClient transactions: Transaction[] }, EventObject > > | undefined }, Values<{ 'fetch remote snapshot': { src: 'fetch remote snapshot' logic: PromiseActorLogic< void | SanityDocument<Record<string, any>> | undefined, { client: SanityClient id: string }, EventObject > id: 'getDocument' } 'submit mutations as transactions': { src: 'submit mutations as transactions' logic: PromiseActorLogic< void, { client: SanityClient transactions: Transaction[] }, EventObject > id: 'submitTransactions' } 'server-sent events': { src: 'server-sent events' logic: ObservableActorLogic< ReconnectEvent | WelcomeEvent | MutationEvent_2, { listener: ReturnType<typeof createSharedListener> id: string }, EventObject > id: string | undefined } }>, Values<{ 'assign error to context': { type: 'assign error to context' params: NonReducibleUnknown } 'clear error from context': { type: 'clear error from context' params: NonReducibleUnknown } 'connect to server-sent events': { type: 'connect to server-sent events' params: NonReducibleUnknown } 'listen to server-sent events': { type: 'listen to server-sent events' params: NonReducibleUnknown } 'stop listening to server-sent events': { type: 'stop listening to server-sent events' params: NonReducibleUnknown } 'buffer remote mutation events': { type: 'buffer remote mutation events' params: NonReducibleUnknown } 'restore stashed changes': { type: 'restore stashed changes' params: NonReducibleUnknown } 'rebase fetched remote snapshot': { type: 'rebase fetched remote snapshot' params: NonReducibleUnknown } 'apply mendoza patch': { type: 'apply mendoza patch' params: NonReducibleUnknown } 'increment fetch attempts': { type: 'increment fetch attempts' params: NonReducibleUnknown