npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@tdh-editor/moxi

v1.2.0

Published

Browser-based Office document editing SDK with local document conversion.

Readme

@tdh-editor/moxi 组件使用文档

说明

@tdh-editor/moxi 提供浏览器内的 Office 文档预览与编辑能力。组件接入时主要使用两个核心类:

  • EditorServer:负责文档加载、资源代理、插件配置和请求拦截
  • Editor:负责渲染编辑器 UI,并挂载到页面容器中

安装

在接入项目中安装 SDK:

npm install @tdh-editor/moxi

包内包含 ESM、UMD、Web Worker 和用于类型解析的源码,类型入口为 src/index.ts,不单独生成 .d.ts。TypeScript 工程需要 5.7 或更新版本。Office 内核、WASM、字体和插件单独部署,不包含在 npm 包中,使用前需要按下文说明手动拷贝。

这是浏览器 SDK,在 Next.js 等 SSR 工程中应于客户端挂载后创建实例。

引入方式

1. 模块化工程

适用于 Vite、Webpack、Next.js 等项目:

import { Editor, EditorServer } from '@tdh-editor/moxi'

2. 原生 HTML / ESM

将 npm 包内的整个 dist/(包括 assets/)手动拷贝到站点静态目录。以下示例假设部署在 /moxi-sdk/,此路径可按需调整:

<script type="module">
  import { Editor, EditorServer } from '/moxi-sdk/moxi-editor.mjs'
</script>

3. UMD

<script src="/moxi-sdk/moxi-editor.umd.js"></script>
<script>
  const { Editor, EditorServer } = window.MoxiEditor
</script>

assets/ 必须与入口文件保持相对位置,转换 Worker 才能加载。

快速开始

1. 准备容器

先准备一个有明确宽高的 DOM 容器:

<div id="editor-container" style="width: 100%; height: 100%;"></div>

如果这里使用 100%,请确保父级节点也有明确高度,否则容器不会被正确撑开。

2. 初始化 EditorServer

import { EditorServer } from '@tdh-editor/moxi'

const server = new EditorServer()

如果需要控制插件加载策略:

const server = new EditorServer({
  getState: () => ({
    plugins: 'featured'
  })
})

可选值:

  • 'featured'
  • 'all'
  • 'none'

3. 打开文档

从本地文件打开

const file = fileInput.files[0]
const result = await server.open(file)
const fileId = result.id

带文件名和类型:

const result = await server.open(file, {
  fileType: 'docx',
  fileName: 'example.docx'
})

从 URL 打开

const result = await server.openUrl('/moxi-office/files/sample.pdf')
const fileId = result.id

带文件名和类型:

const result = await server.openUrl('https://example.com/demo.docx', {
  fileType: 'docx',
  fileName: 'demo.docx'
})

创建空白文档

const result = server.openNew('docx')
const fileId = result.id

支持的常用新建类型:

  • docx
  • xlsx
  • pptx
  • pdf

4. 挂载编辑器

打开已有文档

import { Editor, EditorServer } from '@tdh-editor/moxi'

const container = document.getElementById('editor-container')
const server = new EditorServer()
const result = await server.openUrl('/moxi-office/files/sample.pdf')

const editor = new Editor({
  container,
  server,
  language: 'zh-CN',
  theme: 'theme-white',
  fileId: result.id,
  newDoc: null
})

创建空白文档

const editor = new Editor({
  container,
  server,
  language: 'zh-CN',
  theme: 'theme-white',
  fileId: null,
  newDoc: 'docx'
})

5. 获取编辑后的文件

调用无参数的 getFile(),获取当前编辑结果。返回原生 File,不会触发浏览器下载:

const file = await editor.getFile()

const formData = new FormData()
formData.append('file', file)

await fetch('/api/upload', {
  method: 'POST',
  body: formData
})

文件名沿用当前文档标题,后缀与实际输出格式保持一致。Word、Excel、PowerPoint 分别返回 docx、xlsx、pptx;只读 PDF 返回原文件。新建文档使用 New Document 作为默认名称。

getFile() 会等待文档就绪,每次调用都会重新导出,包含当前已经提交的修改;表格中正在编辑的单元格会先提交再导出。获取文件不会清除未保存提示。

请在编辑器完成当前操作后调用,同一实例同时执行一个获取任务。默认超时为 60 秒,失败时通过 Promise rejection 返回 Error。

6. 接管保存

通过 onSave(file) 接管编辑器内的保存操作,包括 Ctrl+S / Cmd+S、工具栏保存和「文件 → 保存」。配置后,这些操作不再触发浏览器下载,回调会收到转换完成的原生 File:

const editor = new Editor({
  container,
  server,
  language: 'zh-CN',
  theme: 'theme-white',
  fileId: result.id,
  newDoc: null,
  onSave: async (file) => {
    const formData = new FormData()
    formData.append('file', file)

    const response = await fetch('/api/save', {
      method: 'POST',
      body: formData
    })
    if (!response.ok) throw new Error('保存失败')
  }
})

同步回调返回或异步回调完成后,本次保存才视为成功;如果保存期间没有新修改,会清除未保存提示。回调抛错或 Promise rejection 时,保留未保存状态。业务侧可在回调中展示失败原因,再抛出错误。请等待实际写入或上传完成后再返回,fetch() 遇到 HTTP 错误不会自动抛错,需要像示例中一样检查 response.ok。

未配置 onSave 时,保存仍会下载文件。「下载为/另存为」继续下载,getFile() 仍只返回文件,不触发 onSave。

7. 销毁实例

页面卸载或组件销毁时建议主动销毁:

editor.destroy()

原生 HTML 示例

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Editor Demo</title>
    <style>
      html,
      body,
      #app {
        margin: 0;
        width: 100%;
        height: 100%;
        overflow: hidden;
      }
    </style>
  </head>
  <body>
    <div id="app"></div>

    <script type="module">
      import { Editor, EditorServer } from '/moxi-sdk/moxi-editor.mjs'

      const container = document.getElementById('app')
      const server = new EditorServer({
        getState: () => ({ plugins: 'featured' })
      })

      const urlParams = new URLSearchParams(window.location.search)
      const fileUrl = urlParams.get('url')

      let fileId = null
      let newDoc = 'docx'

      if (fileUrl) {
        const result = await server.openUrl(fileUrl)
        fileId = result.id
        newDoc = null
      }

      const editor = new Editor({
        container,
        server,
        language: 'zh-CN',
        theme: 'theme-white',
        fileId,
        newDoc,
        edit: true
      })
    </script>
  </body>
</html>

Vue 使用示例

Vue 3

下面示例适用于 Vue 3 + script setup:

<template>
  <div class="editor-page">
    <div ref="containerRef" class="editor-container"></div>
  </div>
</template>

<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from 'vue'
import { Editor, EditorServer } from '@tdh-editor/moxi'

const containerRef = ref<HTMLElement | null>(null)

let editor: Editor | null = null

onMounted(async () => {
  if (!containerRef.value) return

  const server = new EditorServer({
    getState: () => ({
      plugins: 'featured'
    })
  })

  const result = await server.openUrl('/moxi-office/files/sample.pdf')

  editor = new Editor({
    container: containerRef.value,
    server,
    language: 'zh-CN',
    theme: 'theme-white',
    fileId: result.id,
    newDoc: null,
    edit: true
  })
})

onBeforeUnmount(() => {
  editor?.destroy()
  editor = null
})
</script>

<style scoped>
.editor-page {
  width: 100%;
  height: 100vh;
  overflow: hidden;
}

.editor-container {
  width: 100%;
  height: 100%;
}
</style>

如果你想在 Vue 3 页面中创建空白文档,可以把打开远程文件替换成:

editor = new Editor({
  container: containerRef.value,
  server,
  language: 'zh-CN',
  theme: 'theme-white',
  fileId: null,
  newDoc: 'docx'
})

Vue 2

下面示例适用于 Vue 2 的 Options API:

<template>
  <div class="editor-page">
    <div ref="editorContainer" class="editor-container"></div>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { Editor, EditorServer } from '@tdh-editor/moxi'

export default Vue.extend({
  name: 'OfficeEditor',
  data() {
    return {
      editor: null as Editor | null
    }
  },
  async mounted() {
    const container = this.$refs.editorContainer as HTMLElement | undefined
    if (!container) return

    const server = new EditorServer({
      getState: () => ({
        plugins: 'featured'
      })
    })

    const result = await server.openUrl('/moxi-office/files/sample.pdf')

    this.editor = new Editor({
      container,
      server,
      language: 'zh-CN',
      theme: 'theme-white',
      fileId: result.id,
      newDoc: null,
      edit: true
    })
  },
  beforeDestroy() {
    this.editor?.destroy()
    this.editor = null
  }
})
</script>

<style scoped>
.editor-page {
  width: 100%;
  height: 100vh;
  overflow: hidden;
}

.editor-container {
  width: 100%;
  height: 100%;
}
</style>

如果你想在 Vue 2 页面中创建空白文档,可以把打开远程文件替换成:

this.editor = new Editor({
  container,
  server,
  language: 'zh-CN',
  theme: 'theme-white',
  fileId: null,
  newDoc: 'docx'
})

注意:

  • Vue 2 和 Vue 3 都要在组件挂载完成后再创建 Editor
  • 组件销毁时要调用 destroy(),避免事件监听和实例残留
  • 容器及其父级节点需要有明确高度,否则编辑器无法正常显示

API 参考

Editor

EditorOptions

| 属性名 | 类型 | 必填 | 说明 | | --- | --- | --- | --- | | container | HTMLElement | 是 | 挂载编辑器的 DOM 容器 | | server | EditorServer | 是 | 文档加载与请求代理实例 | | language | string | 是 | 界面语言,如 zh-CN、en | | theme | string | 是 | 界面主题,如 theme-white | | fileId | string \| null | 是 | 已打开文档的 ID,新建时传 null | | newDoc | string \| null | 是 | 新建文档类型,如 docx、xlsx、pptx、pdf;打开已有文档时传 null | | edit | boolean | 否 | 是否允许编辑,传 false 时为只读 | | zoom | number | 否 | 初始缩放值 | | onSave | (file: File) => void \| Promise<void> | 否 | 接管快捷键、工具栏及菜单保存;成功后清除本次修改的未保存提示,失败时保留;未配置则下载 |

方法

| 方法名 | 说明 | | --- | --- | | new Editor(options) | 创建并挂载编辑器 | | getFile(): Promise<File> | 获取编辑后的文件,自动使用当前标题和标准格式,不触发下载 | | destroy() | 销毁编辑器实例并移除事件监听 |

EditorServer

构造参数

new EditorServer(options?)

当前支持:

| 属性名 | 类型 | 说明 | | --- | --- | --- | | getState | () => { plugins: 'featured' \| 'all' \| 'none' } | 动态返回插件策略 |

方法

| 方法名 | 参数 | 返回值 | 说明 | | --- | --- | --- | --- | | open(file, options?) | File | Promise<{ id: string, documentType: string }> | 打开本地文件 | | openUrl(url, options?) | string | Promise<{ id: string, documentType: string }> | 打开远程或本地 URL | | openNew(fileType?) | string | { id: string, documentType: string } | 创建空白文档 | | getDocument() | 无 | { fileType: string, key: string, title: string, url: string } | 获取当前文档信息 | | getUser() | 无 | { id: string, name: string } | 获取当前用户信息 |

支持的文档类型

当前主要支持以下类型:

  • 文本文档:docx、doc、odt、rtf、txt、html、mht、epub、fb2、mobi
  • 表格文档:xlsx、xls、ods、csv、xlsm、xltx、xltm、xlsb
  • 演示文档:pptx、ppt、odp、ppsx、pptm、ppsm、potx、potm
  • 其他:pdf
  • 绘图相关:vsdx、vssx、vstx、vsdm、vssm、vstm

底层会映射为以下五类编辑器能力:

  • word
  • cell
  • slide
  • draw
  • pdf

主题与语言

当前类型中暴露的主题值包括:

  • theme-light
  • theme-classic-light
  • theme-white
  • theme-dark
  • theme-night
  • theme-contrast-dark

语言值可直接传标准 locale,例如:

  • zh-CN
  • zh-TW
  • en
  • ja
  • fr

繁体中文

初始化 Editor 时设置 language: 'zh-TW' 即可启用繁体中文界面。演示应用也可在 Settings → Language 中选择 zh-TW,再重新打开文档。

仓库已包含 Word、表格、演示和 PDF 编辑器的官方繁体语言包,来源为 ONLYOFFICE web-apps v9.3.0.24,与当前内核版本对应。官方尚未翻译的条目仍可能显示英文。

部署时请一并拷贝以下静态资源;内核会将 zh-TW 规范化为小写的 zh-tw,文件名需保持小写:

app/public/moxi-office/v9.3.0.24-1/web-apps/apps/
├── documenteditor/main/locale/zh-tw.json
├── spreadsheeteditor/main/locale/zh-tw.json
├── presentationeditor/main/locale/zh-tw.json
└── pdfeditor/main/locale/zh-tw.json

语言包属于独立部署的 Office 运行资源,不包含在 SDK npm 包中。已有编辑器实例需重新创建后应用新的语言设置。

静态资源拷贝与部署

组件运行时会直接请求一组静态资源。当前代码里的默认绝对路径约定如下:

  • Office 内核资源根路径:/moxi-office/v9.3.0.24-1
  • 插件资源根路径:/moxi-office/v9/sdkjs-plugins
  • 文档转换相关资源:/moxi-office/x2t
  • 示例文件路径:/moxi-office/files,仅在使用本文中的示例文件时需要

如果你使用纯 HTML 或 UMD 方式接入,还需要额外提供 SDK 构建产物:

  • ESM:/moxi-sdk/moxi-editor.mjs
  • UMD:/moxi-sdk/moxi-editor.umd.js
  • Worker:/moxi-sdk/assets/ 下的构建文件

1. 需要拷贝哪些目录

手动将本仓库的 app/public/moxi-office/ 完整拷贝到接入项目的 public/moxi-office/(或对应的静态资源目录),保留其中的许可证文件。npm 包不包含该资源目录,需要从仓库取得。

运行资源单独分发,不包含在 npm 包内;其内部包含 /moxi-office 绝对路径,需要保持同源部署路径。

如果你的页面使用脚本标签或原生 ESM 引入 SDK,还需要将 npm 包内的整个 dist/ 拷贝到 public/moxi-sdk/。从本仓库构建时,对应目录为 editor/dist/。

2. 路径要求

运行资源必须以应用同源的 /moxi-office 路径访问;资源内部包含绝对路径,需要保持目录结构。原生 ESM 和 UMD 示例使用的 /moxi-sdk 是 SDK 构建产物的部署路径,可按实际站点调整。

3. Nginx 部署示例

root /path/to/site/public;

gzip_static on;
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml+rss text/javascript application/wasm;

types {
    application/javascript mjs;
    application/wasm wasm;
}

location /moxi-office {
    try_files $uri =404;
}

# 仅原生 ESM / UMD 接入时需要
location /moxi-sdk {
    try_files $uri =404;
}

说明:

  • gzip_types 建议包含 application/wasm
  • 将 types 中的映射合并到站点现有的 MIME 配置,确保 .mjs 返回 application/javascript、.wasm 返回 application/wasm
  • 缺失资源返回 404,避免用应用页面替代 JS 或 WASM 文件

4. gzip_static on 说明

仓库当前的 app/public/moxi-office/ 中默认没有预生成的 .gz 文件,所以:

  • gzip on; 可以直接启用动态压缩
  • gzip_static on; 只有在你额外生成了同名 .gz 文件时才会生效

5. 部署自检

部署完成后,建议至少确认下面几个地址都能直接访问:

  • /moxi-office/v9.3.0.24-1/web-apps/apps/api/documents/api.js
  • /moxi-office/v9.3.0.24-1/web-apps/apps/api/documents/preload.html
  • /moxi-office/x2t/x2t.js
  • /moxi-office/x2t/x2t.wasm

原生 ESM / UMD 接入时,还需确认 /moxi-sdk/moxi-editor.mjs 或 /moxi-sdk/moxi-editor.umd.js 以及 Worker 文件均可访问。

注意事项

  • 组件必须通过 HTTP 服务访问,不能直接使用 file://
  • openUrl() 读取跨域文件时,目标地址需要允许浏览器访问
  • PDF 默认不支持编辑,会自动按只读能力处理
  • 页面卸载时如果仍有未保存修改,编辑器会注册 beforeunload 提示

许可证

SDK 自有代码使用 MIT。单独部署的 Office 内核、转换器、字体及插件遵循各自许可证。