Skip to content

Get State API (api/editor/getState.ts)

Endpoint Overview

Retrieves the current state of the editor for a specific document, including content, themes, and selection state. Supports version retrieval and collaborative editing.

Request Handlers

GET

  • Query Parameters:
    {
      documentId: string;  // Required: Document identifier
      version?: number;    // Optional: Specific version to retrieve
    }
    
  • Response:
    {
      success: boolean;
      state: {
        version: number;
        content: {
          root: {
            children: Array<{
              type: string;
              children: Array<{
                type: string;
                text: string;
              }>;
            }>;
          };
        };
        themes: Array<{
          id: string;
          name: string;
          color: string;
          ranges: Array<[number, number]>;
        }>;
        selection: {
          anchor: { path: number[]; offset: number; };
          focus: { path: number[]; offset: number; };
        };
        timestamp: string;
      };
    }
    
  • Status: 200 OK, 400 Bad Request, 401 Unauthorized, 404 Not Found

Error Handling

try {
  // State retrieval logic
} catch (error) {
  return {
    error: error.message,
    code: error.code
  };
}

Common errors: - DOCUMENT_NOT_FOUND: Document does not exist - UNAUTHORIZED: User not authorized - INVALID_ID: Document ID is invalid - VERSION_NOT_FOUND: Specified version not found - STATE_NOT_FOUND: Editor state not available

Usage Example

const response = await fetch("/api/editor/getState?documentId=doc_123&version=2");
const { success, state } = await response.json();

if (success) {
  console.log(`Document version: ${state.version}`);
  console.log(`Content: ${state.content}`);
  console.log(`Themes: ${state.themes.length}`);
}

Implementation Details

  • Uses Lexical editor state format
  • Supports versioned state retrieval
  • Includes theme information
  • Maintains cursor selection
  • Stores timestamps for sync
  • Handles collaborative editing
  • Validates document access
  • Returns latest version by default
  • Manages state persistence

Pages/Components Referenced By

Notes

  • Requires authentication
  • Supports version history
  • Compatible with Lexical
  • Includes theme data
  • Maintains selections
  • Handles collaboration
  • Uses secure storage
  • Validates permissions
  • Supports sync operations