Front-End Document API

Last modified by Pierre Jeanjean on 2026/03/23 16:18

Reference

The DocumentService provides operations to access information about the current document, to change the current document, and to refresh the current document content.

/**
 * Provide the operation to access a document.
 *
 * @since 18.0.0RC1
 */
interface DocumentService {
  /**
   * @returns the reference to the current document, the current document changes when setCurrentDocument is called
   */
  getCurrentDocument(): Ref<PageData | undefined>;

  /**
   * Returns a reference the document reference for the current document.
   *
   * @since 18.0.0RC1
   */
  getCurrentDocumentReference(): Ref<DocumentReference | undefined>;

  /**
   * Returns a serialized string of {@link getCurrentDocumentReference}.
   *
   * @since 18.0.0RC1
   */
  getCurrentDocumentReferenceString(): Ref<string | undefined>;

  /**
   * @returns the revision of the current document, or undefined if it's the last one
   * @since 18.0.0RC1
   */
  getCurrentDocumentRevision(): Ref<string | undefined>;

  /**
   * @returns the current document action
   * @since 18.0.0RC1
   */
  getCurrentDocumentAction(): Ref<string | undefined>;

  /**
   * @returns a ref to the loading state. true when the page is loading, false otherwise
   */
  isLoading(): Ref<boolean>;

  /**
   * @returns a ref to the error for the loading of the current document. undefined if no error happened
   */
  getError(): Ref<Error | undefined>;

  /**
   * Update the reference of the latest document.
   * @param documentReference - the current document reference
   * @param action - the current document action (default: "view")
   * @param revision - the revision of the document, undefined for latest
   * @since 18.0.0RC1
   */
  setCurrentDocument(
    documentReference: string,
    action?: string,
    revision?: string,
  ): Promise<void>;

  /**
   * Force reloading the content of the document without changing the current document reference
   */
  refreshCurrentDocument(): void;

  /**
   * Register a change listener that will be executed on any document change
   * made on the whole Cristal instance.
   * @param change - the kind of change
   * @param listener - the listener to register
   * @since 18.0.0RC1
   */
  registerDocumentChangeListener(
    change: DocumentChange,
    listener: (page: PageData) => Promise<void>,
  ): void;

  /**
   * Notify that a document change happened. This will execute all registered
   * listeners for the given kind of change.
   * @param change - the kind of change
   * @param page - the document changed
   * @since 18.0.0RC1
   */
  notifyDocumentChange(change: DocumentChange, page: PageData): Promise<void>;

  /**
   * Unregister a document change listener.
   * @param change - the kind of change
   * @param listener - the listener to unregister
   * @since 18.2.0RC1
   */
  removeDocumentChangeListener(
    change: DocumentChange,
    listener: (page: DocumentReference) => Promise<void>,
  ): void;
}

Related

Cristal - Document

Get Connected