# 嵌入工作台

在自己的页面打开 OFD，把文件生命周期、模板库和业务草稿接到现有系统。

## 运行仓库示例

在已获得 QuireKit OFD 编辑器的工作区，用仓库指定的 Node 22 和 pnpm 运行以下命令。工作台位于 `http://127.0.0.1:8998/`。该应用入口目前通过商业工作区提供。

```sh
pnpm install
pnpm --filter @quirekit/ofd-editor-demo dev
```

## 挂载到页面

在宿主中准备一个有明确高度的空元素。工作台的浏览器入口依赖 Vue、Vite 与 UnoCSS，复用预览应用的 QuireKit preset 和样式扫描配置。在 DOM 可用后调用 `mountFile(element, file)`，切换路由或移除容器时调用它返回的清理函数。对于 SSR，在客户端挂载阶段加载此模块。

### recipes/mount.ts

```ts
import { Editor, type OFDEditorOptions } from "@quirekit/ofd-editor";

/**
 * Opens an authorized file in a host element and gives its owner a teardown function.
 * @param target - Empty element sized by the host.
 * @param file - File chosen by the user.
 * @param options - Host services and session observer.
 * @returns Cleanup to run when leaving the document or removing the element.
 */
export function mountFile(target: HTMLElement, file: File, options: OFDEditorOptions = {}): () => void {
  const editor = new Editor({
    ...options,
    initialDocument: {
      name: file.name,
      /** @param signal - Cancelled when the host leaves the file. @returns The selected file bytes. */
      async read(signal) {
        signal.throwIfAborted();
        const bytes = new Uint8Array(await file.arrayBuffer());
        signal.throwIfAborted();
        return bytes;
      },
    },
  }).mount(target);
  return () => editor.destroy();
}

```

`initialDocument.read(signal)` 也可以通过宿主原有的认证请求读取 OFD 字节，切换文档或销毁页面时要遵守取消信号。省略 `initialDocument` 会打开空白文档。`onSessionChange` 发布当前的 `editor` 和 `view`，宿主据此更新保存、打印等操作；销毁后回调收到 `undefined`。

## 选择需要接入的服务

| 选项 | 由宿主提供 |
| --- | --- |
| `documentTemplates` / `templatePresets` | 可写版本库和可选的只读模板目录 |
| `drafts` / `batch` / `sheetMappings` | 业务草稿、批量生成和共享表格映射 |
| `printService` | 已授权的打印队列与回执查询 |
| `loadSigning` / `licenseKey` | 签章服务和印章来源，以及部署使用的授权码 |

模板操作及 `printDocument()` 可直接在当前会话调用。`printDocument()` 会先提交正在输入的文字和表单值。要让保存能写回业务系统，需要配置 `drafts`，并处理 `openBusinessDraft` 返回的 `pending` 状态，让用户先处理未保存的内容。[会话 API](/api) · [工作站 HTTP](/integration/http)
