@witchcraft/editor
    Preparing search index...

    Type Alias DocumentApiInterface<T>

    type DocumentApiInterface<T extends Record<string, any> = Record<string, any>> = {
        connectedEditors: Record<string, Editor[]>;
        connectEditor: (docId: string, editor: Editor) => void;
        disconnectEditor: (docId: string, editor: Editor) => void;
        getEmbeddedContent: (embedId: EmbedId) => Content | undefined;
        getEmbedTitle: (embedId: EmbedId) => string;
        getFromCache: (
            docId: DocId,
            options?: { errorIfNotFound?: boolean },
        ) => EditorState | undefined;
        getFullState: (docId: DocId) => EditorState;
        getSuggestions: (
            searchString: string,
        ) => Promise<{ docId: string; title: string }[]>;
        load: (docId: DocId) => Promise<{ data?: T; state: EditorState }>;
        postEditorInit: (docId: string, editor: Editor) => void;
        preEditorInit: (
            docId: string,
            options: Partial<EditorOptions>,
            state: EditorState,
        ) => Partial<EditorOptions>;
        save: (docId: string) => void;
        unload: (docId: DocId) => void;
        updateDocument: (
            embedId: DocId,
            tr: Transaction,
            selfSymbol?: symbol,
        ) => void;
        updateFilter?: (tr: Transaction) => boolean | undefined;
        addEventListener(
            type: "saving" | "saved",
            cb: OnSaveDocumentCallback,
        ): void;
        addEventListener(type: "update", cb: OnUpdateDocumentCallback): void;
        removeEventListener(
            type: "saving" | "saved",
            cb: OnSaveDocumentCallback,
        ): void;
        removeEventListener(type: "update", cb: OnUpdateDocumentCallback): void;
    }

    Type Parameters

    • T extends Record<string, any> = Record<string, any>

    Implemented by

    Index

    Properties

    connectedEditors: Record<string, Editor[]>
    connectEditor: (docId: string, editor: Editor) => void
    disconnectEditor: (docId: string, editor: Editor) => void
    getEmbeddedContent: (embedId: EmbedId) => Content | undefined
    getEmbedTitle: (embedId: EmbedId) => string

    How to format the title of the embedded document. Defaults to docId#blockId

    getFromCache: (
        docId: DocId,
        options?: { errorIfNotFound?: boolean },
    ) => EditorState | undefined
    getFullState: (docId: DocId) => EditorState

    Load should be called the first time, before attempting to load the state.

    getSuggestions: (
        searchString: string,
    ) => Promise<{ docId: string; title: string }[]>

    For the embedded document picker, should return suggestions for the search string.

    load: (docId: DocId) => Promise<{ data?: T; state: EditorState }>

    Tells the document api how to load an unloaded document and any additional data. Whatever this function returns will be passed to the refCounter.load option in the default DocumentApi implementation.

    	 load: async ( docId: string, schema: Schema, plugins: Plugin[], getConnectedEditors: () => Editor[]) => {
    const dbDoc = getFromYourDb(docId)

    const state = EditorState.create({
    doc: yjs.doc,
    schema,
    plugins
    })
    // return the state and any additional data we want to cache
    return { state, data: { dbDoc } }
    },

    See DocumentApi.preEditorInit for how to set this up with sync (e.g. yjs).

    postEditorInit: (docId: string, editor: Editor) => void

    For replacing DocumentApi.preEditorInit which runs after initializing and loading the document but before the transaction listeners are added.

    Can be used to add the Collaboration extension for example (see useTestDocumentApi for an example).

    The default implementation just sets the content:

    preEditorInit: (_docId, options, state) => {
    options.content = state.doc.toJSON()
    return options
    }
    preEditorInit: (
        docId: string,
        options: Partial<EditorOptions>,
        state: EditorState,
    ) => Partial<EditorOptions>

    Sets options before initializing the editor. By default just does options.content = state.doc.toJSON(), but can be useful for using per editor component plugins.

    This is normally a bit tricky to do since the editor component initializes the editor before the document is loaded and is re-used (the wrapper Editor component, not the editor) when the document changes.

    So this hook can be used to add these additional per-editor instances of extensions. Be sure to clone the properties you are modifying. They are only shallow cloned before being passed to the function.

    If you need per doc plugins use load instead. See useTestDocumentApi for an example.

    preEditorInit(docId, options: Partial<EditorOptions>, state: EditorState) {
    // we do not need to set options.content when using collab
    // so no options.content = state.doc.toJSON()
    const ydoc = cache.value[docId].ydoc
    // it's suggested you add the collab extension only here
    // otherwise you would have to initially configure it with a dummy document
    options.extensions = [
    ...(options.extensions ?? []),
    // per editor extensions
    ]
    return options
    },
    save: (docId: string) => void

    Debounced save (to storage) function. Use the event listeners to get notified when saving finishes.

    unload: (docId: DocId) => void

    Notifies the document api that an editor has unloaded the document.

    updateDocument: (embedId: DocId, tr: Transaction, selfSymbol?: symbol) => void
    updateFilter?: (tr: Transaction) => boolean | undefined

    Return false to prevent applying the transaction to the state in the cache.

    This used to be needed to ignore yjs transactions, but that's no longer the case. Even with multiple editors loaded to use the same ydoc, everything should work. Leaving the option in case it's needed for some other rare use case.

    Methods