# Viewer SDK Guide

The Viewer SDK lets you build custom viewers/editors for documents inside Dome.

## Install

```bash
npm install dome-embedded-app-sdk
```

## Initialize

```JavaScript
import { ViewerSdk } from "dome-embedded-app-sdk";

ViewerSdk.init({
  onInitialData: ({ doc, ui }) => {
    console.debug("Initial data", doc, ui);
  },
  onDataChange: ({ doc }) => {
    console.debug("Doc changed", doc);
  },
  onCloseRequest: () => {
    console.debug("Close requested");
  },
  onSaveRequest: () => {
    console.debug("Save requested");
  }
});
```

## Events

- `onInitialData({ doc, ui, isNewFile, perms, config })`: Initial payload for the viewer.
- `onDataChange({ doc, perms, userConsent })`: Updates when the document changes.
- `onCloseRequest()`: Parent requests close.
- `onSaveRequest()`: Parent requests save.

## Permissions

- `canRead(perms)`: Returns true if read is allowed.
- `canWrite(perms)`: Returns true if write is allowed.

## Viewer actions

- `requestInitialData()`: Ask the host to resend initial data.
- `requestSave(doc, isDataDirty)`: Ask the host to save and return a status promise.
- `setDirty(isDirty)`: Notify the host about dirty state.
- `sendClose(doc, isDataDirty)`: Notify the host to close.
- `sendException(error)`: Report an exception to the host.
