docxkeun 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +48 -0
- package/README.md +137 -16
- package/dist/api/editor.d.ts +172 -5
- package/dist/api/editor.js +814 -24
- package/dist/api/headers.d.ts +51 -0
- package/dist/api/headers.js +133 -0
- package/dist/api/index.d.ts +5 -1
- package/dist/api/index.js +4 -1
- package/dist/api/lists.d.ts +37 -0
- package/dist/api/lists.js +132 -0
- package/dist/api/parts.js +1 -0
- package/dist/api/positions.js +1 -0
- package/dist/api/revisions.d.ts +61 -0
- package/dist/api/revisions.js +390 -0
- package/dist/api/selection.d.ts +57 -0
- package/dist/api/selection.js +91 -0
- package/dist/charts/color.d.ts +45 -0
- package/dist/charts/color.js +322 -0
- package/dist/charts/format.d.ts +12 -0
- package/dist/charts/format.js +459 -0
- package/dist/charts/index.d.ts +11 -0
- package/dist/charts/index.js +8 -0
- package/dist/charts/model.d.ts +225 -0
- package/dist/charts/model.js +758 -0
- package/dist/charts/outline.d.ts +17 -0
- package/dist/charts/outline.js +214 -0
- package/dist/charts/render.d.ts +20 -0
- package/dist/charts/render.js +1772 -0
- package/dist/drawing/arrows.d.ts +37 -0
- package/dist/drawing/arrows.js +99 -0
- package/dist/drawing/formula.d.ts +40 -0
- package/dist/drawing/formula.js +159 -0
- package/dist/drawing/geometry.d.ts +61 -0
- package/dist/drawing/geometry.js +286 -0
- package/dist/drawing/index.d.ts +12 -0
- package/dist/drawing/index.js +8 -0
- package/dist/drawing/presets.d.ts +72 -0
- package/dist/drawing/presets.js +2874 -0
- package/dist/fonts/shaping/apply.d.ts +94 -0
- package/dist/fonts/shaping/apply.js +349 -0
- package/dist/fonts/shaping/arabic.d.ts +28 -0
- package/dist/fonts/shaping/arabic.js +235 -0
- package/dist/fonts/shaping/buffer.d.ts +53 -0
- package/dist/fonts/shaping/buffer.js +95 -0
- package/dist/fonts/shaping/gdef.d.ts +30 -0
- package/dist/fonts/shaping/gdef.js +66 -0
- package/dist/fonts/shaping/gpos.d.ts +86 -0
- package/dist/fonts/shaping/gpos.js +428 -0
- package/dist/fonts/shaping/gsub.d.ts +65 -0
- package/dist/fonts/shaping/gsub.js +313 -0
- package/dist/fonts/shaping/index.d.ts +32 -0
- package/dist/fonts/shaping/index.js +402 -0
- package/dist/fonts/shaping/indic.d.ts +6 -0
- package/dist/fonts/shaping/indic.js +805 -0
- package/dist/fonts/shaping/layout-common.d.ts +124 -0
- package/dist/fonts/shaping/layout-common.js +349 -0
- package/dist/fonts/shaping/plan.d.ts +87 -0
- package/dist/fonts/shaping/plan.js +152 -0
- package/dist/fonts/shaping/scripts.d.ts +26 -0
- package/dist/fonts/shaping/scripts.js +160 -0
- package/dist/fonts/shaping/shaper.d.ts +23 -0
- package/dist/fonts/shaping/shaper.js +5 -0
- package/dist/fonts/shaping/unicode.d.ts +34 -0
- package/dist/fonts/shaping/unicode.js +138 -0
- package/dist/images/bmp.d.ts +9 -0
- package/dist/images/bmp.js +234 -0
- package/dist/images/gif.d.ts +9 -0
- package/dist/images/gif.js +215 -0
- package/dist/images/index.d.ts +18 -0
- package/dist/images/index.js +63 -0
- package/dist/images/png-encode.d.ts +4 -0
- package/dist/images/png-encode.js +73 -0
- package/dist/images/size.d.ts +10 -0
- package/dist/images/size.js +83 -0
- package/dist/images/tiff.d.ts +14 -0
- package/dist/images/tiff.js +407 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.js +5 -0
- package/dist/layout/bidi.d.ts +18 -0
- package/dist/layout/bidi.js +71 -0
- package/dist/layout/blocks.d.ts +20 -3
- package/dist/layout/blocks.js +88 -58
- package/dist/layout/cache.d.ts +31 -0
- package/dist/layout/cache.js +38 -0
- package/dist/layout/drawing.d.ts +30 -0
- package/dist/layout/drawing.js +120 -0
- package/dist/layout/engine.d.ts +3 -0
- package/dist/layout/engine.js +218 -11
- package/dist/layout/index.d.ts +1 -0
- package/dist/layout/index.js +1 -0
- package/dist/layout/lines.d.ts +63 -5
- package/dist/layout/lines.js +245 -20
- package/dist/layout/paint.d.ts +25 -3
- package/dist/layout/paint.js +26 -10
- package/dist/layout/shapes.d.ts +7 -0
- package/dist/layout/shapes.js +338 -0
- package/dist/layout/style.d.ts +4 -0
- package/dist/layout/style.js +2 -0
- package/dist/layout/types.d.ts +89 -2
- package/dist/math/boxes.d.ts +88 -0
- package/dist/math/boxes.js +166 -0
- package/dist/math/index.d.ts +29 -0
- package/dist/math/index.js +44 -0
- package/dist/math/layout.d.ts +13 -0
- package/dist/math/layout.js +771 -0
- package/dist/math/linear.d.ts +3 -0
- package/dist/math/linear.js +152 -0
- package/dist/math/omml.d.ts +15 -0
- package/dist/math/omml.js +57 -0
- package/dist/math/paths.d.ts +61 -0
- package/dist/math/paths.js +450 -0
- package/dist/math/symbols.d.ts +49 -0
- package/dist/math/symbols.js +269 -0
- package/dist/math/text.d.ts +53 -0
- package/dist/math/text.js +316 -0
- package/dist/metafile/canvas.d.ts +80 -0
- package/dist/metafile/canvas.js +515 -0
- package/dist/metafile/dib.d.ts +27 -0
- package/dist/metafile/dib.js +300 -0
- package/dist/metafile/emf.d.ts +31 -0
- package/dist/metafile/emf.js +591 -0
- package/dist/metafile/geometry.d.ts +49 -0
- package/dist/metafile/geometry.js +85 -0
- package/dist/metafile/index.d.ts +27 -0
- package/dist/metafile/index.js +34 -0
- package/dist/metafile/reader.d.ts +28 -0
- package/dist/metafile/reader.js +59 -0
- package/dist/metafile/state.d.ts +118 -0
- package/dist/metafile/state.js +172 -0
- package/dist/metafile/text.d.ts +17 -0
- package/dist/metafile/text.js +130 -0
- package/dist/metafile/wmf.d.ts +33 -0
- package/dist/metafile/wmf.js +395 -0
- package/dist/model/content.d.ts +92 -7
- package/dist/model/content.js +251 -17
- package/dist/model/document.d.ts +34 -1
- package/dist/model/document.js +80 -1
- package/dist/model/fields.d.ts +4 -0
- package/dist/model/fields.js +10 -0
- package/dist/model/numbering.d.ts +2 -0
- package/dist/model/numbering.js +22 -0
- package/dist/model/props.d.ts +2 -0
- package/dist/model/props.js +3 -0
- package/dist/opc/index.d.ts +1 -1
- package/dist/opc/package.d.ts +34 -0
- package/dist/opc/package.js +58 -4
- package/dist/pdf/export.js +97 -6
- package/dist/pdf/images.d.ts +7 -0
- package/dist/pdf/images.js +39 -2
- package/dist/render/activity.d.ts +138 -0
- package/dist/render/activity.js +794 -0
- package/dist/render/clipboard.d.ts +29 -0
- package/dist/render/clipboard.js +401 -0
- package/dist/render/comments.d.ts +166 -0
- package/dist/render/comments.js +779 -0
- package/dist/render/context-menu.d.ts +124 -0
- package/dist/render/context-menu.js +525 -0
- package/dist/render/editing.d.ts +146 -5
- package/dist/render/editing.js +678 -53
- package/dist/render/index.d.ts +12 -0
- package/dist/render/index.js +11 -0
- package/dist/render/navigation-panel.d.ts +76 -0
- package/dist/render/navigation-panel.js +431 -0
- package/dist/render/selection.js +5 -0
- package/dist/render/svg.d.ts +4 -6
- package/dist/render/svg.js +69 -7
- package/dist/render/toolbar.d.ts +83 -0
- package/dist/render/toolbar.js +505 -0
- package/dist/render/ui/comment-layout.d.ts +39 -0
- package/dist/render/ui/comment-layout.js +79 -0
- package/dist/render/ui/icons.d.ts +6 -0
- package/dist/render/ui/icons.js +38 -0
- package/dist/render/ui/menu.d.ts +38 -0
- package/dist/render/ui/menu.js +274 -0
- package/dist/render/ui/nav-model.d.ts +11 -0
- package/dist/render/ui/nav-model.js +34 -0
- package/dist/render/ui/overflow.d.ts +5 -0
- package/dist/render/ui/overflow.js +18 -0
- package/dist/render/ui/popover.d.ts +18 -0
- package/dist/render/ui/popover.js +109 -0
- package/dist/render/ui/theme.d.ts +2 -0
- package/dist/render/ui/theme.js +29 -0
- package/dist/render/undo.d.ts +76 -0
- package/dist/render/undo.js +144 -0
- package/dist/render/viewer.d.ts +100 -7
- package/dist/render/viewer.js +302 -92
- package/dist/security/policy.d.ts +3 -0
- package/dist/security/policy.js +6 -2
- package/dist/security/protection.d.ts +1 -0
- package/dist/security/protection.js +1 -1
- package/dist/text/bidi-data.d.ts +53 -0
- package/dist/text/bidi-data.js +318 -0
- package/dist/text/bidi.d.ts +37 -0
- package/dist/text/bidi.js +508 -0
- package/dist/xml/ns.d.ts +5 -0
- package/dist/xml/ns.js +7 -0
- package/package.json +9 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,54 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to docxkeun and docxkeun-fonts. Versions follow [semantic versioning](https://semver.org); while the version is 0.x, minor versions may contain breaking changes.
|
|
4
4
|
|
|
5
|
+
## 0.2.0 (2026-11-22)
|
|
6
|
+
|
|
7
|
+
### docxkeun
|
|
8
|
+
|
|
9
|
+
- List editing: `DocxEditor.listItems()` returns each list paragraph's label as Word shows it (`"1."`, `"b)"`, `"•"`), and `DocxEditor.setList()` switches paragraphs between bullets, numbers, letters and roman numerals, restarts numbering, changes the list level, joins an existing list or removes a list. A changed list keeps the indentation and label formatting of the list it came from. `numbering.xml` is created when a document has none.
|
|
10
|
+
- **Tracked changes:** `new DocxEditor(doc, guard, { trackChanges: true, author })` records edits as Word revisions (insertions, deletions, formatting and paragraph changes, paragraph marks), signed and dated. Typing inside your own insertion extends it; deleting it removes it. `revisions()`, `acceptRevisions()` and `rejectRevisions()` (all, by id or by author) review them, including moves made in Word, and `setTrackChangesSetting()` turns Word's own Track Changes on or off. Edits follow the document's setting by default. Documents protected for tracked changes now accept edits (recorded) and refuse review. With `showDeletions`, insertions are underlined and deletions struck through in an author colour.
|
|
11
|
+
- **Drawings:** DrawingML shapes (153 preset shapes and custom geometry, theme fills and outlines, arrowheads, rotation and flips), text boxes (laid out as Word paragraphs, old VML ones too), groups and drawing canvases, charts (column, bar, line, area, pie, doughnut, scatter, bubble, radar, combinations, with axes, legends, data labels and number formats) and SmartArt (from Word's saved drawing) are drawn on screen and in PDFs.
|
|
12
|
+
- **Pictures:** GIF, BMP and TIFF are decoded (TIFF is shown in the browser as PNG; all three are embedded in PDFs), and EMF/WMF pictures are drawn as vectors. Pictures can be rotated and flipped. `DocxEditor.insertImage()` adds a picture at its natural size or a given one.
|
|
13
|
+
- **Equations:** Office Math is typeset: fractions, scripts, radicals, big operators with limits, delimiters that grow, matrices, accents, equation arrays and more, inline or on their own line. Text extraction gives Office's linear format (`x=(-b±√(b^2-4ac))/(2a)`).
|
|
14
|
+
- **Text wrapping:** text flows around floating pictures, shapes and text boxes (square, tight, through, top and bottom), on the paragraphs they meet.
|
|
15
|
+
- **Columns:** a multi-column section that ends with a continuous section break is balanced, as in Word.
|
|
16
|
+
- **Right-to-left text:** the Unicode bidi algorithm orders mixed Arabic, Hebrew and Latin text; right-to-left paragraphs (`w:bidi`) are laid out from the right margin with their indents and alignment mirrored. `DocxEditor.formatParagraph()` sets alignment, direction, indents, spacing and keep rules.
|
|
17
|
+
- **Headers, footers and notes:** `DocxDocument.storyParts()` lists header, footer and note parts, and `new DocxEditor(doc, guard, { part })` edits one. `DocxEditor.addFootnote()` adds a footnote (and the footnotes part when there is none).
|
|
18
|
+
- **In-page editing:** double-click a header or footer, or click a footnote or text box, to type in it. A formatting toolbar (`toolbar: true`), rich copy and paste (HTML from Word, Google Docs and web pages), Cmd/Ctrl+E/L/R/J alignment, Track Changes while typing, and a caret that steps over pictures. Layout re-uses the lines of unchanged paragraphs (`LayoutCache`, used by the viewer), so typing in long documents is several times faster.
|
|
19
|
+
- **AI assistants:** `viewer.selectionInfo()` and the `selectioninfo` event describe what the user selected (or where the caret is) with read_document's paragraph numbers, offsets, pages and text; `describeSelection()` turns it into a prompt line. `viewer.activity` shows an AI at work: a shimmer over the paragraphs it is changing and its own named caret, a scan line while it reads, then the change inked in with a fading glow, removed paragraphs floating away, change bars with ✦ in the margin and a change map on the right edge (`work`, `snapshot`, `reveal`, `idle`, `clearMarks`; follows `prefers-reduced-motion`). Optional: `aiActivity: false`, or `{ animate: false }` for highlights without motion and `{ marks: false }` without change bars; `viewer.activity.enabled` / `.animate` / `.marksShown` at run time. `{ name, color }` (and `viewer.activity.name` / `.color`) set the name on the caret and the colours, one CSS colour or two for a gradient; from CSS, `--docxkeun-ai-color-1` / `-2`. The caret's label turns dark on light colours.
|
|
20
|
+
- **Adding headers and footers:** `DocxEditor.setHeader()` / `setFooter()` create or replace them, for every section or one, on every page, the first page (w:titlePg) or even pages (Word's even/odd setting). Tabs split a line into left, centre and right parts, and `{PAGE}`, `{NUMPAGES}`, `{SECTIONPAGES}` become page number fields. A part shared with other sections is not overwritten, Track Changes records the text changes, and `removeHeader()` / `removeFooter()` delete parts nothing uses any more. `headersAndFooters()` lists them with their sections and text.
|
|
21
|
+
- New exports: `describeChart`, `readChart`, `renderChart`, `mathToText`, `layoutMath`, `renderMetafile`, `sniffImage`, `decodeRaster`, `imageInfo`, `LayoutCache`, `FormatToolbar`, `htmlToParagraphs`.
|
|
22
|
+
- **Viewer UI:** one shared look and frame for four optional parts. `toolbar` (fold to a "Show toolbar" strip, overflow menu), `navigation` (240px panel: back/forward, Outline, Bookmarks and Links tabs, search, per-tab previous/next, folds to a 36px rail), `comments` (one column of compact cards, margin bubbles on narrow viewers with `mode: 'auto'`, Reply/Resolve/Delete, "Show resolved") and `contextMenu` (right-click menu that follows what was clicked; Shift+right-click shows the browser's). Each has a controller on the viewer (`viewer.toolbar`, `.navigation`, `.comments`, `.contextMenu`), the `chromechange` event reports changes and nothing is stored. Theme with `--docxkeun-ui-bg`, `-fg`, `-muted`, `-line`, `-hover`, `-accent`, `-accent-soft`, `-radius`, `-shadow`, `-font` (the `--docxkeun-toolbar-*` variables still work). `scrollToComment(id)` now opens the thread, and `viewer.comments.width` is what fit-to-window apps subtract.
|
|
23
|
+
- **Undo for everything:** `viewer.editing.transaction(label, fn)` makes an action that touches several parts (comments, relationships, headers, numbering, styles) one undo step; a failure restores the document and emits `editerror`. Inside `fn`, changes go through the `editor` it receives; edits requested meanwhile wait their turn. `DocxDocument.checkpoint()` / `restore()` back it. Fixes ⌘Z after adding a comment or link leaving markers half-removed. Undo keeps 200 steps.
|
|
24
|
+
- **Editor:** `setImageAlt()`, `setHyperlinkTarget()` (URL or bookmark, same scheme rules as `addHyperlink`) and `clearFormatting()`.
|
|
25
|
+
- **Breaking (0.x):** `DocxEditor.removeHyperlink()` is now `async`; call it with `await`. It also drops the link's relationship when nothing else uses it, and a guard refusal rejects the promise instead of throwing.
|
|
26
|
+
- New exports (`docxkeun/viewer`): `NavigationController`, `NavTab`, `NavigationOptions`, `CommentsController`, `CommentThread`, `CommentsMode`, `CommentsOptions`, `relativeTime`, `filterTargets`, `currentHeading`, `ToolbarController`, `ContextMenuController`, `hitTarget`, `defaultItems`, `parseLinkTarget`, `MenuItem`, `MenuHandle`, `MenuContext`, `MenuTarget`, `MenuTargetKind`, `MenuEnv`, `MenuActions`, `ContextMenuOptions`, `ToolbarOptions`, `FormatChoice`, `ParagraphChoice`, `openMenu`, `promptPopover`.
|
|
27
|
+
- `viewer.navigation` is now a class instance (`NavigationController`). `const { headings, bookmarks, links } = viewer.navigation` works as before, but its lists are getters, so `{ ...viewer.navigation }` and `JSON.stringify(viewer.navigation)` no longer include them.
|
|
28
|
+
|
|
29
|
+
### docxkeun chat (new app, not published)
|
|
30
|
+
|
|
31
|
+
- A web app in `packages/chat`: open a `.docx`, chat with Claude about it with your own API key, and watch the changes appear live in the docxkeun viewer. Undo per reply, selection as context, download and PDF export.
|
|
32
|
+
- The viewer shimmers the paragraphs Claude is about to change while it is still writing the tool call (from the streamed tool input), then animates the change. The selection (or the caret's paragraph) goes with the next message, with paragraph numbers and page.
|
|
33
|
+
- The document has the viewer's navigation rail and comment cards, and the right-click menu has "Ask Claude about this", which puts the selection into the message box.
|
|
34
|
+
- "Appearance" sets the name on Claude's caret and its colours, remembered in the browser.
|
|
35
|
+
- The Claude calls go through a small relay on your computer (`pnpm dev:chat`, or `pnpm start:chat` for the build), which keeps the key in memory: it works for organizations that block browser calls to the API, and the browser never stores the key. The relay answers only its own page and forwards only Messages and Models calls.
|
|
36
|
+
|
|
37
|
+
### docxkeun-mcp 0.1.0 (new package)
|
|
38
|
+
|
|
39
|
+
- An MCP server for Word documents, so Claude Code, Claude Desktop and other MCP clients can work with `.docx` files through docxkeun.
|
|
40
|
+
- Tools to list, read, search and inspect documents; find and replace; fill templates; edit, insert and delete paragraphs; format text; change lists (bullets, numbers, letters, levels); add links and bookmarks; add, reply to, resolve and delete comments; create documents; lock regions; set protection and open passwords; and export PDFs.
|
|
41
|
+
- Only files inside the folders given on the command line can be read or written (symlinks and `..` cannot escape them). Writes go through a temporary file.
|
|
42
|
+
- `--preview` runs a live preview on 127.0.0.1: docxkeun's viewer redraws a document whenever a tool or anything else changes it, with a note of what changed. Read-only, local only, and it checks the Host header against DNS rebinding. `--preview-only` runs just the preview, to watch a folder.
|
|
43
|
+
- `--role viewer|commenter|editor|owner` decides which tools the AI gets, and docxkeun's `AccessPolicy` enforces it together with the document's protection and locked regions.
|
|
44
|
+
- The document operations behind the tools are exported (`OPERATIONS`, `operationsFor`, `contextFor`) for building other front ends, such as docxkeun chat.
|
|
45
|
+
- Tools for tracked changes (`list_revisions`, `accept_revisions`, `reject_revisions`, `set_track_changes`), headers, footers, notes and text boxes (`read_headers_footers`, `replace_story_text`), pictures (`insert_image`, `list_objects`), footnotes (`add_footnote`) and paragraph formatting (`format_paragraph`, including right-to-left). `read_document` shows pictures, charts (with their data), shapes, equations, text direction and tracked changes. Edits are signed with `--author` when Track Changes is on, and documents protected for tracked changes can now be edited.
|
|
46
|
+
- `set_header_footer` and `remove_header_footer`: headers and footers with page numbers, left / centre / right parts, first-page and even-page versions, per section. `read_headers_footers` shows page fields as `{PAGE}` and which pages and sections each one is for.
|
|
47
|
+
- The live preview's "AI effects" menu: animated, still or off. The AI's caret carries the `--author` name, and `--ai-color` sets its colours (one CSS colour, or two for a gradient).
|
|
48
|
+
- `get_selection` (and the resource `docxkeun://selection`): what the user selected in the live preview, so "this" and "here" mean something. The preview's header shows what is shared. Pages report positions only, with a per-server token; the text comes from the file.
|
|
49
|
+
- The live preview shows where the AI works while a tool runs (shimmer and caret, or a scan line for reads) and animates each change when it lands.
|
|
50
|
+
- The live preview has the navigation rail and comment cards (margin bubbles in a narrow pane), and a read-only right-click menu with Copy, Copy link address and Open link.
|
|
51
|
+
- It needs docxkeun 0.2.0.
|
|
52
|
+
|
|
5
53
|
## 0.1.3 (2026-11-02)
|
|
6
54
|
|
|
7
55
|
Documentation only. No code changes.
|
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
A DOCX viewer, editor and DOCX-to-PDF converter for JavaScript and TypeScript. Read, render, edit and convert `.docx` files in the browser (and Node), with **zero runtime dependencies**. Everything is implemented in this package: zip, XML, the document model, a page layout engine, an SVG viewer, a PDF writer, and the cryptography for password-protected files.
|
|
8
8
|
|
|
9
|
-
**[Live demo](https://kevinkjs.github.io/docxkeun/)**: open a `.docx`, type on the page,
|
|
9
|
+
**[Live demo](https://kevinkjs.github.io/docxkeun/)**: open a `.docx`, type on the page, right-click to comment or link, track changes, undo anything, export a PDF.
|
|
10
10
|
|
|
11
11
|
[](https://kevinkjs.github.io/docxkeun/)
|
|
12
12
|
|
|
@@ -14,11 +14,12 @@ A DOCX viewer, editor and DOCX-to-PDF converter for JavaScript and TypeScript. R
|
|
|
14
14
|
|
|
15
15
|
| Area | What you get |
|
|
16
16
|
|---|---|
|
|
17
|
-
| Read / write | Open `.docx` (also `.dotx`/`.docm`), edit through a code API, save. Parts you did not touch are written back byte-for-byte; unknown markup is preserved. |
|
|
18
|
-
| Render | Paginated, Word-like layout: styles, lists, tabs, tables, sections, columns, headers/footers, footnotes, page numbers,
|
|
17
|
+
| Read / write | Open `.docx` (also `.dotx`/`.docm`), edit through a code API, save. Parts you did not touch are written back byte-for-byte; unknown markup is preserved. Record, accept and reject tracked changes. |
|
|
18
|
+
| Render | Paginated, Word-like layout: styles, lists, tabs, tables, sections, balanced columns, headers/footers, footnotes, page numbers. Pictures (PNG, JPEG, GIF, BMP, TIFF, EMF, WMF), shapes and text boxes, charts, SmartArt and equations, with text wrapping around floating objects. Right-to-left text (Arabic, Hebrew) and complex scripts. One SVG per page with real, selectable text. |
|
|
19
19
|
| PDF | Native PDF from the same layout: embedded font subsets, searchable text, links, bookmarks as named destinations, an outline from headings, comments as annotations. |
|
|
20
20
|
| Permissions | Word protection (read-only / comments / tracked changes / forms) with editable ranges, an app-level `AccessPolicy`, password-to-open `.docx` encryption, PDF passwords and permission flags. |
|
|
21
21
|
| Comments, anchors, links | Threaded comments (all five Word comment parts), bookmarks, internal and external hyperlinks, field-code links. |
|
|
22
|
+
| AI assistants | [`docxkeun-mcp`](packages/mcp#readme): an MCP server that lets Claude and other AI assistants read, edit, comment on, protect and export your `.docx` files. |
|
|
22
23
|
|
|
23
24
|
## Install
|
|
24
25
|
|
|
@@ -73,18 +74,89 @@ viewer.on('navigate', ({ target, location }) => highlightInMyPanel(target));
|
|
|
73
74
|
- Bookmarks whose names start with `_` (`_Toc…`, `_Hlk…`, `_GoBack`) are marked `hidden`, as Word hides them. `next`/`previous` skip them unless you pass `{ includeHidden: true }`.
|
|
74
75
|
- `collectNavTargets(layout)` from `docxkeun/viewer` gives the same lists without a viewer (e.g. in Node).
|
|
75
76
|
|
|
76
|
-
The
|
|
77
|
+
The `navigation` option adds a ready-made panel for these lists (see [Viewer UI](#viewer-ui)); the playground uses it with Alt+←/→ for back and forward.
|
|
77
78
|
|
|
78
79
|
### Typing on the page
|
|
79
80
|
|
|
80
81
|
```ts
|
|
81
|
-
const viewer = new DocxViewer(el, { fonts, editable: true });
|
|
82
|
+
const viewer = new DocxViewer(el, { fonts, editable: true, toolbar: true }); // or { editable: { guard: policy, trackChanges: true, author: 'Rina' } }
|
|
82
83
|
viewer.on('editerror', ({ error }) => toast(error.message)); // e.g. PermissionError under read-only protection
|
|
84
|
+
viewer.on('storychange', ({ story }) => console.log(story.kind)); // 'body', 'header', 'footer', 'footnote', 'endnote' or 'textbox'
|
|
83
85
|
viewer.editing!.undo();
|
|
84
86
|
viewer.setEditable(false); // back to view-only
|
|
85
87
|
```
|
|
86
88
|
|
|
87
|
-
Click to place the caret and type. Supported: text and IME input, Backspace/Delete (Alt/Ctrl for words), Enter (new paragraph, following the style's "next" style), Shift+Enter (line break), Tab, arrow keys, Home/End, Shift to select, double-click a word, drag to select, Cmd/Ctrl+A,
|
|
89
|
+
Click to place the caret and type. Supported: text and IME input, Backspace/Delete (Alt/Ctrl for words), Enter (new paragraph, following the style's "next" style), Shift+Enter (line break), Tab, arrow keys, Home/End, Shift to select, double-click a word, drag to select, Cmd/Ctrl+A, Cmd/Ctrl+B/I/U, Cmd/Ctrl+E/L/R/J for alignment, Cmd/Ctrl+Z / Shift+Z / Y for undo and redo, and Cmd/Ctrl-click to follow a link. Every change goes through `DocxEditor`, so the same access guard applies and everything the editor does not understand stays in the file.
|
|
90
|
+
|
|
91
|
+
- **Headers, footers, notes and text boxes:** double-click a header or footer to type in it (as in Word), click a footnote, endnote or text box to edit it, and click the body to go back.
|
|
92
|
+
- **Toolbar:** `toolbar: true` adds a formatting bar above the pages (paragraph style, size, bold/italic/underline/strikethrough, colour, highlight, alignment, lists, indents, text direction, undo/redo, Track Changes). It follows the formatting at the caret through the `caretchange` event, so you can also build your own: `viewer.editing.format({ bold: true })`, `formatParagraphs({ align: 'center' })`, `setStyle('Heading1')`, `setList('bullet')`, `indent(1)`, `trackChanges = true`.
|
|
93
|
+
- **Copy and paste keep formatting:** HTML from Word, Google Docs and web pages comes in with bold, italic, underline, colours, sizes, links, headings and lists; copying puts HTML and plain text on the clipboard.
|
|
94
|
+
- **Pictures** count as one character: the caret steps over them, and Backspace/Delete removes them.
|
|
95
|
+
- **Track Changes:** with Word's Track Changes on (or `trackChanges: true`), typing is recorded as tracked insertions and deletions.
|
|
96
|
+
- **Speed:** layout keeps the measured lines of unchanged paragraphs, so a keystroke re-measures only the paragraph you type in (a 90-page document re-lays out in about 45 ms).
|
|
97
|
+
|
|
98
|
+
### AI assistants on the page
|
|
99
|
+
|
|
100
|
+
Two pieces for apps that let an AI edit the document (the [chat app](packages/chat#readme) and the MCP [live preview](packages/mcp#live-preview) use both):
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
import { describeSelection } from 'docxkeun/editor';
|
|
104
|
+
|
|
105
|
+
// What the user selected, like the "selected lines" an editor shares with a coding agent.
|
|
106
|
+
viewer.on('selectioninfo', ({ selection }) => {
|
|
107
|
+
// { story: 'body', start: { paragraph: 4, offset: 22 }, end: …, text: '30 days', pages: [2], paragraphs: [...] }
|
|
108
|
+
if (selection) prompt.context = describeSelection(selection, { document: 'contract.docx' });
|
|
109
|
+
// "Selected in contract.docx: paragraph 4, page 2, 7 characters.
|
|
110
|
+
// [4] Payment is due within «30 days» of the invoice date."
|
|
111
|
+
});
|
|
112
|
+
viewer.selectionInfo(); // the same, on demand (the caret when typing is on, else the text selection)
|
|
113
|
+
|
|
114
|
+
// Show the AI at work, then what it changed.
|
|
115
|
+
viewer.activity.work([4, 5], { label: 'editing' }); // shimmer over paragraphs 4–5, and "Claude is editing…" on its own caret
|
|
116
|
+
const before = viewer.activity.snapshot();
|
|
117
|
+
editor.replaceRange(editor.find('30 days')[0], '45 days');
|
|
118
|
+
await viewer.activity.reveal(before); // new text inks in, removed text floats away, change bars and a change map stay
|
|
119
|
+
viewer.activity.clearMarks();
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Paragraph numbers are the ones docxkeun-mcp's `read_document` uses (table cells included), so a selection can go straight into a tool call.
|
|
123
|
+
|
|
124
|
+
The effects are optional: `new DocxViewer(el, { aiActivity: false })` turns them off (calls become no-ops; `reveal` still returns the diff), or `aiActivity: { animate: false }` keeps the highlights without motion and `marks: false` leaves out the change bars. At run time: `viewer.activity.enabled`, `.animate` and `.marksShown`. They also follow `prefers-reduced-motion`.
|
|
125
|
+
|
|
126
|
+
Name and colours: `aiActivity: { name: 'Aria', color: ['#0b57d0', '#1e8e3e'] }` puts "Aria is editing…" on the caret and draws the caret, shimmer, glow and change marks from blue to green; one colour (`color: '#0b57d0'`) is used for both ends. Change them at run time with `viewer.activity.name` and `viewer.activity.color` (`undefined` goes back to the default purple to orange), or from CSS with `--docxkeun-ai-color-1` and `--docxkeun-ai-color-2` (any CSS colour). The caret's label turns dark when white text would be hard to read on the colours you pick.
|
|
127
|
+
|
|
128
|
+
### Viewer UI
|
|
129
|
+
|
|
130
|
+
Four optional parts share one look and one frame: a toolbar, a navigation panel, a comment column and a right-click menu. With none enabled the viewer's DOM is unchanged.
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
const viewer = new DocxViewer(el, {
|
|
134
|
+
fonts, editable: true,
|
|
135
|
+
toolbar: { collapsed: false }, // true | HTMLElement | { container?, collapsed?, styles? }
|
|
136
|
+
navigation: { tab: 'outline' }, // true | { tab?: 'outline' | 'bookmarks' | 'links', collapsed? }
|
|
137
|
+
comments: { mode: 'auto', showResolved: false }, // 'auto' | 'cards' | 'bubbles'
|
|
138
|
+
contextMenu: { // true | { items?(at, defaults) }
|
|
139
|
+
items: (at, defaults) => [...defaults, '-', { label: 'Ask Claude', run: () => ask(at.selection) }],
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
viewer.toolbar.hide(); viewer.toolbar.collapsed = true; // show(), hide(), visible, collapsed
|
|
143
|
+
viewer.navigation.show('links'); viewer.navigation.refresh(); // show(tab) unfolds onto that tab; hide(), collapsed, tab; headings, bookmarks, links as before
|
|
144
|
+
viewer.comments.open(id); viewer.comments.compose(); // close(), mode, effectiveMode, showResolved, width
|
|
145
|
+
viewer.contextMenu.open(x, y); // enable(), disable(), close(), enabled
|
|
146
|
+
viewer.on('chromechange', ({ part, visible, collapsed }) => remember(part, visible, collapsed));
|
|
147
|
+
|
|
148
|
+
// One undo step for an action that touches several parts (comment parts, relationships, headers, numbering, styles).
|
|
149
|
+
await viewer.editing!.transaction('Add note', async (editor) => { /* several editor calls */ });
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
- **Toolbar:** a fold button hides the bar to a thin "Show toolbar" strip; groups that do not fit move into a "⋯" menu. Arrow keys move between buttons.
|
|
153
|
+
- **Navigation panel:** 240px beside the pages, with back/forward, Outline, Bookmarks and Links tabs, search, previous/next per tab and "Show hidden bookmarks". It folds to a 36px rail (with a Comments button when the document has comments) and starts folded when the frame is narrower than 720px, unless `collapsed` is given.
|
|
154
|
+
- **Comments:** one column for the whole document with compact cards. Opening a thread lines the card up with its text; when editing is allowed it has Reply, Resolve and Delete, and resolved threads sit behind "Show resolved (N)". `auto` switches to margin bubbles when the viewer is narrower than 0.75 × the widest page + 300px; `showComments: false` still hides everything. An app that fits the page to the window should subtract `viewer.comments.width`. `commentclick` fires when a thread opens, and `scrollToComment(id)` opens it too.
|
|
155
|
+
- **Right-click menu:** off by default. Items change with what was clicked (text, link, comment, picture, header or footer), and ones the access guard refuses are greyed with the reason as a tooltip. Shift+right-click shows the browser's own menu, and right-clicks outside the pages are left alone. A read-only viewer drops Cut and Paste, and Paste shows "Paste (⌘V)" where the async clipboard is missing.
|
|
156
|
+
- **Theming:** set `--docxkeun-ui-bg`, `-fg`, `-muted`, `-line`, `-hover`, `-accent`, `-accent-soft`, `-radius`, `-shadow` and `-font` on the container or `:root` (light by default; set them for a dark theme). The older `--docxkeun-toolbar-*` variables still work. The library stores nothing: use `chromechange` to remember what the user folded.
|
|
157
|
+
- **Undo:** `transaction()` makes everything an action changed one undo step, and a failure restores the document, emits `editerror` and rethrows. Inside the function, change the document through the `editor` it receives; edits requested elsewhere meanwhile wait their turn and are undo steps of their own. Typing waits for a pending transaction: keys pressed while one runs are ignored, as are undo and redo. Undo keeps 200 steps. Adding a comment or link now undoes completely.
|
|
158
|
+
|
|
159
|
+
The playground uses all four; `docxkeun/viewer` exports the controllers (`NavigationController`, `CommentsController`, `ContextMenuController`, `ToolbarController`) and the pieces for custom menus (`openMenu`, `promptPopover`).
|
|
88
160
|
|
|
89
161
|
### Editing from code
|
|
90
162
|
|
|
@@ -109,7 +181,44 @@ await editor.resolveComment(id);
|
|
|
109
181
|
const bytes = await viewer.document!.save();
|
|
110
182
|
```
|
|
111
183
|
|
|
112
|
-
Also available: `insertText`, `appendText`, `replaceRange`, `replaceAll`, `deleteRange` (across paragraphs), `insertParagraph`, `deleteParagraph`, `setParagraphStyle`, `removeHyperlink`, `hyperlinks()`, `bookmarks()`, `removeBookmark`, `editComment`, `deleteComment`, `addEditableRange`, `removeEditableRange`.
|
|
184
|
+
Also available: `insertText`, `appendText`, `replaceRange`, `replaceAll`, `deleteRange` (across paragraphs), `insertParagraph`, `deleteParagraph`, `setParagraphStyle`, `formatParagraph` (alignment, right-to-left, indents, spacing, keep rules), `insertImage`, `addFootnote`, `setHeader` / `setFooter` / `removeHeader` / `removeFooter` / `headersAndFooters()`, `await removeHyperlink` (async since it also drops the link's relationship when unused), `setHyperlinkTarget`, `setImageAlt`, `clearFormatting`, `hyperlinks()`, `bookmarks()`, `removeBookmark`, `editComment`, `deleteComment`, `addEditableRange`, `removeEditableRange`.
|
|
185
|
+
|
|
186
|
+
```ts
|
|
187
|
+
// Tracked changes: record edits as Word revisions, then review them.
|
|
188
|
+
const tracked = new DocxEditor(doc, policy, { trackChanges: true, author: 'Rina Pratama' });
|
|
189
|
+
tracked.replaceRange(tracked.find('30 days')[0], '45 days'); // a w:del and a w:ins, signed and dated
|
|
190
|
+
tracked.revisions(); // [{ id, kind: 'delete', author, date, text: '30 days' }, { kind: 'insert', … }]
|
|
191
|
+
tracked.acceptRevisions({ author: 'Rina Pratama' }); // or rejectRevisions({ ids: ['3'] }); no filter = all
|
|
192
|
+
await tracked.setTrackChangesSetting(true); // turn on Word's own Track Changes for the file
|
|
193
|
+
|
|
194
|
+
// Headers, footers and notes: an editor per part.
|
|
195
|
+
for (const story of doc.storyParts()) console.log(story.kind, story.part); // 'header' word/header1.xml …
|
|
196
|
+
const header = new DocxEditor(doc, policy, { part: 'word/header1.xml' });
|
|
197
|
+
header.replaceAll('Draft', 'Final');
|
|
198
|
+
|
|
199
|
+
// Headers and footers: tabs split left / centre / right, {PAGE} {NUMPAGES} {SECTIONPAGES} are page numbers.
|
|
200
|
+
await editor.setHeader('ACME Ltd.\tService Agreement\tConfidential');
|
|
201
|
+
await editor.setFooter('Page {PAGE} of {NUMPAGES}', { align: 'center' });
|
|
202
|
+
await editor.setFooter('', { type: 'first' }); // a blank footer on the first page; also 'even', and { section: 2 }
|
|
203
|
+
await editor.removeHeader({ section: 2 });
|
|
204
|
+
|
|
205
|
+
// Pictures and footnotes.
|
|
206
|
+
await editor.insertImage(editor.find('Site plan')[0].end, pngBytes, { width: 300, alt: 'Site plan' });
|
|
207
|
+
await editor.addFootnote(editor.find('royalty')[0].end, 'Calculated on gross revenue.');
|
|
208
|
+
editor.formatParagraph(editor.paragraphs()[3].el, { direction: 'rtl', align: 'right', spaceAfter: 6 });
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
Lists: `listItems()` gives every list paragraph's label as Word shows it (`"1."`, `"b)"`, `"•"`), and `setList` changes list formatting. A changed list keeps the indentation of the list it was in.
|
|
212
|
+
|
|
213
|
+
```ts
|
|
214
|
+
const items = [...editor.listItems().keys()]; // w:p elements, in document order
|
|
215
|
+
await editor.setList(items, { format: 'decimal' }); // a. b. → 1. 2. (also 'bullet', 'decimalParen', 'lowerLetter', 'upperLetter', 'lowerRoman', 'upperRoman')
|
|
216
|
+
await editor.setList(items, { start: 5 }); // restart the numbering, same format
|
|
217
|
+
await editor.setList([items[1]!], { level: 1 }); // indent one level
|
|
218
|
+
const extra = editor.insertParagraph({ after: items[1]! }, 'Admin fee');
|
|
219
|
+
await editor.setList([extra], { sameListAs: items[1]! }); // continue an existing list
|
|
220
|
+
await editor.setList(items, { format: 'none' }); // no longer a list
|
|
221
|
+
```
|
|
113
222
|
|
|
114
223
|
### PDF
|
|
115
224
|
|
|
@@ -189,6 +298,18 @@ const DocxView = createDocxViewerComponent(React); // no React dependency in do
|
|
|
189
298
|
<DocxView file={bytes} fonts={fonts} onReady={(viewer) => setViewer(viewer)} />
|
|
190
299
|
```
|
|
191
300
|
|
|
301
|
+
## AI assistants (MCP)
|
|
302
|
+
|
|
303
|
+
[`docxkeun-mcp`](packages/mcp#readme) is an [MCP](https://modelcontextprotocol.io) server built on docxkeun. It gives Claude Code, Claude Desktop and other MCP clients tools to read documents (including pictures, charts, equations, headers, footers and notes), find and replace text, fill templates, format paragraphs, insert pictures and footnotes, comment, record and review tracked changes, lock regions, set protection and passwords, and export PDFs, limited to the folders you choose and to a role (viewer, commenter, editor or owner).
|
|
304
|
+
|
|
305
|
+
```bash
|
|
306
|
+
claude mcp add docxkeun -- npx -y docxkeun-mcp ~/Documents/contracts
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
It is a separate package so that docxkeun itself keeps zero dependencies.
|
|
310
|
+
|
|
311
|
+
[`docxkeun chat`](packages/chat#readme) is a small web app built on the same tools: open a `.docx`, chat with Claude about it using your own API key, and watch the edits appear in the docxkeun viewer as they happen. Run it with `pnpm dev:chat`; a small local server makes the API calls and keeps the key in memory.
|
|
312
|
+
|
|
192
313
|
## Fonts
|
|
193
314
|
|
|
194
315
|
Word documents name fonts (Calibri, Times New Roman...) but rarely contain them, and browsers cannot read the system's font files. For exact layout and for PDF export, layout needs font files in a `FontRegistry`.
|
|
@@ -225,21 +346,21 @@ TrueType, OpenType (CFF) and `.ttc` collections are supported. PDF embedding res
|
|
|
225
346
|
|
|
226
347
|
## Status and limitations
|
|
227
348
|
|
|
228
|
-
Working and tested (
|
|
349
|
+
Working and tested (over 500 tests): zip/XML/OPC round-trips, styles and numbering, layout and pagination (text wrapping around floating objects, balanced columns, right-to-left text), shapes, charts, SmartArt, equations, GIF/BMP/TIFF/EMF/WMF pictures, OpenType shaping, the viewer and in-page editing, tracked changes, comments, links, bookmarks, PDF export, document protection, access policy, `.docx` encryption, PDF encryption.
|
|
229
350
|
|
|
230
351
|
Verified against independent implementations so far:
|
|
231
352
|
- Crypto primitives against FIPS/RFC test vectors and Node's crypto.
|
|
232
353
|
- PDF output and AES-256 PDF encryption opened by Chrome's PDF viewer (with and without an open password).
|
|
233
354
|
|
|
234
|
-
**Still to verify against files made by Microsoft Word:** layout fidelity (line and page breaks), the Word protection password hash (the Excel-compatible low half is confirmed),
|
|
355
|
+
**Still to verify against files made by Microsoft Word:** layout fidelity (line and page breaks), the Word protection password hash (the Excel-compatible low half is confirmed), opening docxkeun-encrypted files in Word / decrypting Word-encrypted files, and how closely shapes, charts and equations match Word's own drawing.
|
|
235
356
|
|
|
236
|
-
|
|
237
|
-
-
|
|
238
|
-
-
|
|
239
|
-
-
|
|
240
|
-
-
|
|
241
|
-
-
|
|
242
|
-
-
|
|
357
|
+
Known limits:
|
|
358
|
+
- **Drawings:** text in a rotated shape or text box stays upright, and vertical text is drawn horizontally. Gradient and pattern fills are drawn in one colour; picture fills, shadows, glow and 3-D effects are not drawn. SmartArt is drawn from the layout Word saves with the file; a diagram saved without it shows its text in a box.
|
|
359
|
+
- **Charts:** surface charts, trendlines, error bars, data tables and up/down bars are not drawn; 3-D charts are drawn flat; date axes are spaced like categories.
|
|
360
|
+
- **Pictures:** only the first frame of a GIF and the first page of a TIFF; JPEG-compressed and CMYK TIFFs are not decoded. EMF+-only pictures (some Office 2007+ charts pasted as pictures) and clipping inside metafiles are not supported.
|
|
361
|
+
- **Equations:** no line breaks inside an equation and no equation numbers. Math symbols missing from your fonts fall back to other fonts (the bundled fonts have no math font such as Cambria Math).
|
|
362
|
+
- **Right-to-left and complex scripts:** the bundled fonts have no Arabic or Indic letters; register a font that has them (for example Arial, Times New Roman or Noto) for PDF export. The viewer leaves shaping these scripts to the browser.
|
|
363
|
+
- **Editing:** creating or changing shapes, charts and equations, moving or resizing pictures, and editing table structure (rows, columns, merging) are not supported. A keystroke still paginates the whole document, re-using the measured lines of unchanged paragraphs.
|
|
243
364
|
|
|
244
365
|
## Development
|
|
245
366
|
|
package/dist/api/editor.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
import { type HeaderFooterInfo, type HeaderFooterKind, type HeaderFooterOptions, type HeaderFooterType } from './headers.js';
|
|
1
2
|
import type { DocParagraph, Block } from '../model/content.js';
|
|
2
3
|
import type { DocxDocument } from '../model/document.js';
|
|
3
4
|
import { type XmlElement } from '../xml/index.js';
|
|
5
|
+
import { type ListFormat, type ListItem } from './lists.js';
|
|
6
|
+
import { type Revision, type RevisionFilter } from './revisions.js';
|
|
4
7
|
import { EditError, type DocPosition, type DocRange } from './positions.js';
|
|
5
|
-
export type EditAction = 'edit' | 'format' | 'comment' | 'reply' | 'resolveComment' | 'editComment' | 'deleteComment' | 'link' | 'bookmark' | 'fillForm' | 'protect';
|
|
8
|
+
export type EditAction = 'edit' | 'format' | 'comment' | 'reply' | 'resolveComment' | 'editComment' | 'deleteComment' | 'link' | 'bookmark' | 'fillForm' | 'protect' | 'review';
|
|
6
9
|
/**
|
|
7
10
|
* How a locked region is locked (Word's content-control locking, w:sdtPr/w:lock):
|
|
8
11
|
* 'content' = the text cannot be edited; 'contentAndControl' = and the region cannot be deleted;
|
|
@@ -32,10 +35,25 @@ export declare class LockedRegionError extends EditError {
|
|
|
32
35
|
export declare function lockModeOf(sdt: XmlElement): LockMode | undefined;
|
|
33
36
|
/** Checks every edit (see docxkeun/security's AccessPolicy). Throw to refuse. */
|
|
34
37
|
export interface EditGuard {
|
|
38
|
+
/** `detail.tracked` is set when the edit is recorded as a tracked change. */
|
|
35
39
|
assert(action: EditAction, range?: DocRange, detail?: {
|
|
36
40
|
commentAuthor?: string;
|
|
41
|
+
tracked?: boolean;
|
|
37
42
|
}): void;
|
|
38
43
|
}
|
|
44
|
+
export interface EditorOptions {
|
|
45
|
+
/** Record edits as tracked changes. Default: the document's own Track Changes setting. */
|
|
46
|
+
trackChanges?: boolean;
|
|
47
|
+
/** Author of tracked changes (default: the guard's user name, else "docxkeun"). */
|
|
48
|
+
author?: string;
|
|
49
|
+
/**
|
|
50
|
+
* Edit another story: a header or footer part, or the footnotes or endnotes part (see
|
|
51
|
+
* `DocxDocument.storyParts()`). Default: the main document (body and text boxes).
|
|
52
|
+
*/
|
|
53
|
+
part?: string;
|
|
54
|
+
}
|
|
55
|
+
/** Whether Word would record changes in this document: Track Changes on, or protected for tracked changes. */
|
|
56
|
+
export declare function documentTracksChanges(doc: DocxDocument): boolean;
|
|
39
57
|
export interface TextFormat {
|
|
40
58
|
bold?: boolean;
|
|
41
59
|
italic?: boolean;
|
|
@@ -52,6 +70,33 @@ export interface TextFormat {
|
|
|
52
70
|
smallCaps?: boolean;
|
|
53
71
|
hidden?: boolean;
|
|
54
72
|
}
|
|
73
|
+
/** Paragraph formatting for `formatParagraph` (lengths in points; undefined leaves a property alone). */
|
|
74
|
+
export interface ParagraphFormat {
|
|
75
|
+
align?: 'left' | 'center' | 'right' | 'justify';
|
|
76
|
+
/** Right-to-left paragraph (Arabic, Hebrew): w:bidi. */
|
|
77
|
+
direction?: 'ltr' | 'rtl';
|
|
78
|
+
indentLeft?: number;
|
|
79
|
+
indentRight?: number;
|
|
80
|
+
/** Positive: first-line indent; negative: hanging indent. */
|
|
81
|
+
indentFirstLine?: number;
|
|
82
|
+
spaceBefore?: number;
|
|
83
|
+
spaceAfter?: number;
|
|
84
|
+
/** Line spacing as a multiple of single spacing (1, 1.15, 1.5, 2). */
|
|
85
|
+
lineSpacing?: number;
|
|
86
|
+
keepWithNext?: boolean;
|
|
87
|
+
keepLinesTogether?: boolean;
|
|
88
|
+
pageBreakBefore?: boolean;
|
|
89
|
+
}
|
|
90
|
+
export interface SetListOptions {
|
|
91
|
+
/** A list format, or 'none' to take the paragraphs out of their list. */
|
|
92
|
+
format?: ListFormat | 'none';
|
|
93
|
+
/** List level, 0–8. */
|
|
94
|
+
level?: number;
|
|
95
|
+
/** First number of the list. */
|
|
96
|
+
start?: number;
|
|
97
|
+
/** Join this paragraph's list instead of starting a new one. */
|
|
98
|
+
sameListAs?: XmlElement;
|
|
99
|
+
}
|
|
55
100
|
export interface CommentInput {
|
|
56
101
|
author: string;
|
|
57
102
|
text: string;
|
|
@@ -78,7 +123,16 @@ export interface ParagraphText {
|
|
|
78
123
|
}
|
|
79
124
|
/** Every paragraph of a block list (including table cells), in document order. */
|
|
80
125
|
export declare function paragraphsOf(blocks: Block[], out?: DocParagraph[]): DocParagraph[];
|
|
81
|
-
export declare function paragraphText(para: DocParagraph
|
|
126
|
+
export declare function paragraphText(para: DocParagraph, options?: {
|
|
127
|
+
objects?: boolean;
|
|
128
|
+
}): ParagraphText;
|
|
129
|
+
/** Stands for an inline picture, shape or chart in `paragraphText(p, { objects: true })`. */
|
|
130
|
+
export declare const OBJECT_CHAR = "\uFFFC";
|
|
131
|
+
/** The run child (w:drawing, w:pict, mc:AlternateContent) holding an inline object. */
|
|
132
|
+
export declare function objectNode(inline: {
|
|
133
|
+
run: XmlElement;
|
|
134
|
+
drawing: XmlElement;
|
|
135
|
+
}): XmlElement | undefined;
|
|
82
136
|
/**
|
|
83
137
|
* XML position of character offset `k` in a paragraph. With side 'start' the position is at the
|
|
84
138
|
* start of the segment holding character k; with 'end' it is at the end of the segment holding
|
|
@@ -92,7 +146,39 @@ export declare function applyFormat(run: XmlElement, format: TextFormat): void;
|
|
|
92
146
|
export declare class DocxEditor {
|
|
93
147
|
readonly doc: DocxDocument;
|
|
94
148
|
readonly guard?: EditGuard | undefined;
|
|
95
|
-
|
|
149
|
+
private trackOverride;
|
|
150
|
+
private readonly authorName;
|
|
151
|
+
/** The part being edited. */
|
|
152
|
+
readonly part: string;
|
|
153
|
+
constructor(doc: DocxDocument, guard?: EditGuard | undefined, options?: EditorOptions);
|
|
154
|
+
/** Whether this editor works on a header, footer or notes part rather than the main document. */
|
|
155
|
+
private get isStory();
|
|
156
|
+
/** Whether edits are recorded as tracked changes (default: the document's Track Changes setting). */
|
|
157
|
+
get trackChanges(): boolean;
|
|
158
|
+
set trackChanges(on: boolean);
|
|
159
|
+
/** The author tracked changes are recorded under. */
|
|
160
|
+
get author(): string;
|
|
161
|
+
/** Revision stamp and id source for one edit, or undefined when not tracking. */
|
|
162
|
+
private tracking;
|
|
163
|
+
/** Turn Word's own Track Changes setting (settings.xml w:trackRevisions) on or off. */
|
|
164
|
+
setTrackChangesSetting(on: boolean): Promise<void>;
|
|
165
|
+
/** Tracked changes in the main document, in document order. */
|
|
166
|
+
revisions(): Revision[];
|
|
167
|
+
/** Accept tracked changes (all, or those matching ids / author). Returns how many. */
|
|
168
|
+
acceptRevisions(filter?: RevisionFilter): number;
|
|
169
|
+
/** Reject tracked changes (all, or those matching ids / author). Returns how many. */
|
|
170
|
+
rejectRevisions(filter?: RevisionFilter): number;
|
|
171
|
+
/** The new run as it goes into the document: wrapped in a w:ins when tracking. */
|
|
172
|
+
private placed;
|
|
173
|
+
/** Whether a run sits in an insertion by the current author (edits to it need no new revision). */
|
|
174
|
+
private inOwnInsertion;
|
|
175
|
+
/**
|
|
176
|
+
* A sentinel inside someone else's w:ins cannot take a nested w:ins: split that insertion at the
|
|
177
|
+
* sentinel so the new one goes between its two halves.
|
|
178
|
+
*/
|
|
179
|
+
private liftOutOfInsertion;
|
|
180
|
+
/** Mark the runs of a range as deleted (and the paragraph marks inside it), keeping the text. */
|
|
181
|
+
private trackedCut;
|
|
96
182
|
private get root();
|
|
97
183
|
private get body();
|
|
98
184
|
private index;
|
|
@@ -121,7 +207,7 @@ export declare class DocxEditor {
|
|
|
121
207
|
offset: number;
|
|
122
208
|
};
|
|
123
209
|
}): DocRange;
|
|
124
|
-
/** Every paragraph of the body, including those in tables, in document order. */
|
|
210
|
+
/** Every paragraph of the story (the body by default), including those in tables, in document order. */
|
|
125
211
|
paragraphs(): DocParagraph[];
|
|
126
212
|
/** Range covering a paragraph's text (undefined for paragraphs without text). */
|
|
127
213
|
paragraphRange(paragraph: XmlElement | DocParagraph): DocRange | undefined;
|
|
@@ -135,6 +221,57 @@ export declare class DocxEditor {
|
|
|
135
221
|
* "inside this (empty) paragraph".
|
|
136
222
|
*/
|
|
137
223
|
insertText(at: DocPosition, text: string, format?: TextFormat): DocPosition;
|
|
224
|
+
/** Insert a run holding `content` (a drawing, a note reference) at a position; returns the run. */
|
|
225
|
+
private insertRunContent;
|
|
226
|
+
/**
|
|
227
|
+
* Insert a picture (PNG, JPEG, GIF, BMP, TIFF, EMF or WMF) inline at a position. Without a size it
|
|
228
|
+
* gets its natural size, shrunk to `maxWidth` (default 468pt, a Letter page's text width).
|
|
229
|
+
*/
|
|
230
|
+
insertImage(at: DocPosition, bytes: Uint8Array, options?: {
|
|
231
|
+
width?: number;
|
|
232
|
+
height?: number;
|
|
233
|
+
alt?: string;
|
|
234
|
+
name?: string;
|
|
235
|
+
maxWidth?: number;
|
|
236
|
+
}): Promise<XmlElement>;
|
|
237
|
+
/** Add a footnote whose reference mark goes at a position. Returns the note's id. */
|
|
238
|
+
addFootnote(at: DocPosition, text: string): Promise<string>;
|
|
239
|
+
/** Headers and footers by section and page type, with their text ({PAGE} for page fields). */
|
|
240
|
+
headersAndFooters(): HeaderFooterInfo[];
|
|
241
|
+
/** Give the document's sections a header (see `setHeaderFooter`). Returns the part name. */
|
|
242
|
+
setHeader(text: string, options?: HeaderFooterOptions): Promise<string>;
|
|
243
|
+
/** Give the document's sections a footer (see `setHeaderFooter`). Returns the part name. */
|
|
244
|
+
setFooter(text: string, options?: HeaderFooterOptions): Promise<string>;
|
|
245
|
+
/**
|
|
246
|
+
* Create or replace a header or footer. `text`: lines become paragraphs, tabs split it into
|
|
247
|
+
* left / centre / right parts, and {PAGE}, {NUMPAGES}, {SECTIONPAGES} become page number fields
|
|
248
|
+
* ("Page {PAGE} of {NUMPAGES}"). Every section by default, or one (`section`, from 1); all pages,
|
|
249
|
+
* the first page or even pages (`type`). A part used only by these sections is rewritten in place,
|
|
250
|
+
* otherwise a new part is made, so other sections keep theirs. With Track Changes on, the text
|
|
251
|
+
* changes are recorded.
|
|
252
|
+
*/
|
|
253
|
+
setHeaderFooter(kind: HeaderFooterKind, text: string, options?: HeaderFooterOptions): Promise<string>;
|
|
254
|
+
/** Remove headers (see `removeHeaderFooter`). Returns how many references were removed. */
|
|
255
|
+
removeHeader(options?: Omit<HeaderFooterOptions, 'type' | 'align'> & {
|
|
256
|
+
type?: HeaderFooterType | 'all';
|
|
257
|
+
}): Promise<number>;
|
|
258
|
+
/** Remove footers (see `removeHeaderFooter`). Returns how many references were removed. */
|
|
259
|
+
removeFooter(options?: Omit<HeaderFooterOptions, 'type' | 'align'> & {
|
|
260
|
+
type?: HeaderFooterType | 'all';
|
|
261
|
+
}): Promise<number>;
|
|
262
|
+
/**
|
|
263
|
+
* Remove headers or footers: of every type unless `type` says which, from every section unless
|
|
264
|
+
* `section` says which (a later section without its own then shows the previous section's). Parts
|
|
265
|
+
* nothing uses any more are deleted. Not recorded as a tracked change.
|
|
266
|
+
*/
|
|
267
|
+
removeHeaderFooter(kind: HeaderFooterKind, options?: Omit<HeaderFooterOptions, 'type' | 'align'> & {
|
|
268
|
+
type?: HeaderFooterType | 'all';
|
|
269
|
+
}): Promise<number>;
|
|
270
|
+
private mainBody;
|
|
271
|
+
private sectionTargets;
|
|
272
|
+
/** Delete header/footer parts (and their relationships) that no section refers to any more. */
|
|
273
|
+
private dropUnusedParts;
|
|
274
|
+
private setEvenAndOdd;
|
|
138
275
|
/** Join a paragraph with the one right after it (Backspace at the start of the second). */
|
|
139
276
|
mergeParagraphs(first: XmlElement, second: XmlElement): void;
|
|
140
277
|
/** Split a paragraph at a position (Enter). Returns the new, second paragraph. */
|
|
@@ -161,6 +298,8 @@ export declare class DocxEditor {
|
|
|
161
298
|
}): string[];
|
|
162
299
|
/** Apply character formatting to a range. */
|
|
163
300
|
formatRange(range: DocRange, format: TextFormat): void;
|
|
301
|
+
/** Remove direct character formatting (and character styles, except Hyperlink) from a range. */
|
|
302
|
+
clearFormatting(range: DocRange): void;
|
|
164
303
|
/** Remove the content of a range, returning the sentinels at its (now adjacent) ends. */
|
|
165
304
|
private cut;
|
|
166
305
|
private removeRange;
|
|
@@ -174,14 +313,42 @@ export declare class DocxEditor {
|
|
|
174
313
|
}): XmlElement;
|
|
175
314
|
deleteParagraph(paragraph: XmlElement): void;
|
|
176
315
|
setParagraphStyle(paragraph: XmlElement, styleId: string): void;
|
|
316
|
+
/** Change paragraph formatting: alignment, direction, indents, spacing and keep rules. */
|
|
317
|
+
formatParagraph(paragraph: XmlElement, format: ParagraphFormat): void;
|
|
318
|
+
/** The list paragraphs of the body with their labels ("1.", "a)", "•"), keyed by w:p. */
|
|
319
|
+
listItems(): Map<XmlElement, ListItem>;
|
|
320
|
+
/**
|
|
321
|
+
* Change list formatting of paragraphs:
|
|
322
|
+
* - `format`: make them one list in that format ('bullet', 'decimal' 1., 'decimalParen' 1),
|
|
323
|
+
* 'lowerLetter' a., 'upperLetter' A., 'lowerRoman' i., 'upperRoman' I.), numbered from `start`
|
|
324
|
+
* (default 1). Paragraphs already in a list keep its indentation and their level. 'none' takes
|
|
325
|
+
* them out of their list.
|
|
326
|
+
* - `start` alone: restart the numbering, keeping the format.
|
|
327
|
+
* - `level` (0–8): the list level (indent); alone, only the level changes.
|
|
328
|
+
* - `sameListAs`: join the list of that paragraph, continuing its numbering.
|
|
329
|
+
* Returns the numId the paragraphs now use (undefined after 'none').
|
|
330
|
+
*/
|
|
331
|
+
setList(paragraphs: XmlElement[], options: SetListOptions): Promise<string | undefined>;
|
|
332
|
+
/** Set or clear a paragraph's w:numPr (clearing style-given numbering with numId 0). */
|
|
333
|
+
private writeNumPr;
|
|
177
334
|
/** Turn a range (within one paragraph) into a hyperlink to a URL or a bookmark. */
|
|
178
335
|
addHyperlink(range: DocRange, target: {
|
|
179
336
|
url: string;
|
|
180
337
|
} | {
|
|
181
338
|
bookmark: string;
|
|
182
339
|
}, tooltip?: string): Promise<XmlElement>;
|
|
340
|
+
/** Point an existing w:hyperlink at another URL or a bookmark (same scheme rules as `addHyperlink`). */
|
|
341
|
+
setHyperlinkTarget(hyperlink: XmlElement, target: {
|
|
342
|
+
url: string;
|
|
343
|
+
} | {
|
|
344
|
+
bookmark: string;
|
|
345
|
+
}): Promise<void>;
|
|
346
|
+
/** Remove a hyperlink relationship that nothing in this part refers to any more. */
|
|
347
|
+
private dropHyperlinkRel;
|
|
348
|
+
/** Set the alt text (wp:docPr/@descr) of a picture or drawing. */
|
|
349
|
+
setImageAlt(drawing: XmlElement, text: string): void;
|
|
183
350
|
/** Unwrap a w:hyperlink, keeping its text. */
|
|
184
|
-
removeHyperlink(link: XmlElement): void
|
|
351
|
+
removeHyperlink(link: XmlElement): Promise<void>;
|
|
185
352
|
/** Every w:hyperlink in the body with its target. */
|
|
186
353
|
hyperlinks(): Array<{
|
|
187
354
|
el: XmlElement;
|