展示 HN:JavaScript 优先、开源的所见即所得 DOCX 编辑器
Show HN: JavaScript-first, open-source WYSIWYG DOCX editor

原始链接: https://github.com/eigenpal/docx-js-editor

## @eigenpal/docx-js-editor: 一个 React DOCX 编辑器 `@eigenpal/docx-js-editor` 是一个开源的、功能齐全的所见即所得 DOCX 编辑器,专为 React 构建。它允许用户在浏览器中直接打开、编辑和保存 `.docx` 文件,**无需服务器**。 主要功能包括全面的文本和段落格式设置、对表格、图像和超链接的支持、撤销/重做功能以及打印预览。它通过插件系统具有高度可扩展性,允许进行自定义集成,例如侧边栏和文档叠加层。 该组件接受 `.docx` 文件作为 `ArrayBuffer`,并提供保存更改、访问文档对象、控制缩放以及管理编辑器焦点/滚动的方法。`readOnly` 属性启用只读预览功能。 **重要提示:** 此编辑器需要 DOM,最好与 Next.js 或类似框架集成,使用动态导入或延迟加载以避免服务器端渲染问题。它采用 MIT 许可证发布。

相关文章

原文

DOCX JS Editor

npm version npm downloads license Live Demo

Open-source WYSIWYG DOCX editor for React. Open, edit, and save .docx files entirely in the browser — no server required. Try the live demo.

DOCX JS Editor screenshot

npm install @eigenpal/docx-js-editor
import { useRef } from 'react';
import { DocxEditor, type DocxEditorRef } from '@eigenpal/docx-js-editor';
import '@eigenpal/docx-js-editor/styles.css';

function Editor({ file }: { file: ArrayBuffer }) {
  const editorRef = useRef<DocxEditorRef>(null);

  const handleSave = async () => {
    const buffer = await editorRef.current?.save();
    if (buffer) {
      await fetch('/api/documents/1', { method: 'PUT', body: buffer });
    }
  };

  return (
    <>
      <button onClick={handleSave}>Save</button>
      <DocxEditor ref={editorRef} documentBuffer={file} onChange={() => {}} />
    </>
  );
}

Next.js / SSR: The editor requires the DOM. Use a dynamic import or lazy useEffect load to avoid server-side rendering issues.

Prop Type Default Description
documentBuffer ArrayBuffer .docx file contents to load
document Document Pre-parsed document (alternative to buffer)
readOnly boolean false Read-only preview (no caret/selection)
showToolbar boolean true Show formatting toolbar
showRuler boolean false Show horizontal ruler
showZoomControl boolean true Show zoom controls
showVariablePanel boolean true Show template variable panel
initialZoom number 1.0 Initial zoom level
onChange (doc: Document) => void Called on document change
onSave (buffer: ArrayBuffer) => void Called on save
onError (error: Error) => void Called on error
const ref = useRef<DocxEditorRef>(null);

await ref.current.save(); // Returns ArrayBuffer of the .docx
ref.current.getDocument(); // Current document object
ref.current.setZoom(1.5); // Set zoom to 150%
ref.current.focus(); // Focus the editor
ref.current.scrollToPage(3); // Scroll to page 3
ref.current.print(); // Print the document

Use readOnly for a preview-only viewer. This disables editing, caret, and selection UI.

<DocxEditor documentBuffer={file} readOnly />

Extend the editor with the plugin system. Wrap DocxEditor in a PluginHost and pass plugins that can contribute ProseMirror plugins, side panels, document overlays, and custom CSS:

import { DocxEditor, PluginHost, templatePlugin } from '@eigenpal/docx-js-editor';

function Editor({ file }: { file: ArrayBuffer }) {
  return (
    <PluginHost plugins={[templatePlugin]}>
      <DocxEditor documentBuffer={file} />
    </PluginHost>
  );
}

See docs/PLUGINS.md for the full plugin API, including how to create custom plugins with panels, overlays, and ProseMirror integrations.

  • Full WYSIWYG editing with Microsoft Word fidelity
  • Text and paragraph formatting (bold, italic, fonts, colors, alignment, spacing)
  • Tables, images, hyperlinks
  • Extensible plugin architecture
  • Undo/redo, find & replace, keyboard shortcuts
  • Print preview
  • Zero server dependencies

MIT

联系我们 contact @ memedata.com