# Open and save

Provide an initial file, store each version in your own service and handle unsaved changes.

The editor does not decide where files are stored. The host provides the initial file and how to save; when the user saves, the editor hands the host the complete `.pptx` bytes.

## Open an initial file

```ts
import { Editor } from '@quirekit/pptx-editor'

const editor = new Editor({
  initialDocument: {
    name: 'Quarterly report.pptx',
    read: async (signal) => {
      const response = await fetch('/api/decks/42', { signal })
      return response.ok ? new Uint8Array(await response.arrayBuffer()) : undefined
    },
  },
}).mount('#app')
```

When `read` returns `undefined` the editor opens a new presentation. `signal` aborts the request when the user opens another file first or the editor is destroyed.

## Save to your own service

```ts
const editor = new Editor({
  save: async ({ name, bytes }) => {
    const response = await fetch(`/api/decks/42?name=${encodeURIComponent(name)}`, { method: 'PUT', body: bytes.slice() })
    return response.ok
  },
  autoSave: true,
}).mount('#app')
```

- `save` runs when the user clicks Save or presses Ctrl/⌘+S. Returning `true` means the version is stored and clears the unsaved mark; `false` keeps the mark so the user can retry.
- `autoSave: true` calls `save` shortly after each edit.
- Without `save`, saving downloads the file and counts as saved.

## Session

`onSessionChange` hands the host a session once the deck is open, and `undefined` when the editor is destroyed. File methods live on the session:

```ts
let session: PptxEditorSession | undefined

new Editor({ onSessionChange: (next) => { session = next } }).mount('#app')

await session?.ready                              // the first file is open and shown
await session?.open('Another deck.pptx', bytes)   // replaces the current file
const current = await session?.snapshot()         // current .pptx bytes; does not save or clear the unsaved mark
session?.rename('New name')                       // .pptx is added when missing
await session?.save()                             // the same flow as the Save button
```

Import `PptxEditorSession` as a type from `@quirekit/pptx-editor`.

## Unsaved changes

```ts
session.subscribe((event) => {
  if (event.type === 'dirty') saveIndicator.textContent = event.dirty ? 'Unsaved changes' : 'Saved'
})

window.addEventListener('beforeunload', (event) => {
  if (session?.isDirty()) event.preventDefault()
})
```

`change` events say the content changed and where from: `user` (editing in the interface), `command` (a script calling commands), `agent` (the AI assistant), `history` (undo or redo) and `file` (another file was opened).

## Open dropped files

```ts
window.addEventListener('dragover', (event) => event.preventDefault())
window.addEventListener('drop', (event) => {
  const file = [...(event.dataTransfer?.files ?? [])].find((item) => /\.pptx$/i.test(item.name))
  if (!file || !session) return
  event.preventDefault()
  void file.arrayBuffer().then((buffer) => session!.open(file.name, new Uint8Array(buffer)))
})
```

The editor's File menu also offers New, Open a local file (Ctrl/⌘+O), Save as template or show file, and export to PDF, pictures, video, GIF, Word handouts and Print. Without `save`, Save appears as Download a copy.
