@portone/docx-editor 0.1.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 +7 -0
- package/CONTRIBUTING.md +63 -0
- package/LICENSE +203 -0
- package/README.md +48 -0
- package/assets/editor.png +0 -0
- package/dist/DocxEditor.d.ts +123 -0
- package/dist/DocxEditor.js +348 -0
- package/dist/core.d.ts +36 -0
- package/dist/core.js +38 -0
- package/dist/docx/comments/constants.d.ts +8 -0
- package/dist/docx/comments/constants.js +20 -0
- package/dist/docx/comments/model.d.ts +37 -0
- package/dist/docx/comments/model.js +104 -0
- package/dist/docx/comments/reading.d.ts +36 -0
- package/dist/docx/comments/reading.js +149 -0
- package/dist/docx/comments/writing.d.ts +11 -0
- package/dist/docx/comments/writing.js +327 -0
- package/dist/docx/comments.d.ts +3 -0
- package/dist/docx/comments.js +4 -0
- package/dist/docx/container.d.ts +12 -0
- package/dist/docx/container.js +130 -0
- package/dist/docx/documentSettings.d.ts +4 -0
- package/dist/docx/documentSettings.js +14 -0
- package/dist/docx/exportDocx.d.ts +15 -0
- package/dist/docx/exportDocx.js +163 -0
- package/dist/docx/exportRefs.d.ts +16 -0
- package/dist/docx/exportRefs.js +10 -0
- package/dist/docx/formatting/direct.d.ts +30 -0
- package/dist/docx/formatting/direct.js +201 -0
- package/dist/docx/formatting/effectiveParagraph.d.ts +15 -0
- package/dist/docx/formatting/effectiveParagraph.js +81 -0
- package/dist/docx/formatting/styles.d.ts +67 -0
- package/dist/docx/formatting/styles.js +165 -0
- package/dist/docx/formatting/tabStops.d.ts +8 -0
- package/dist/docx/formatting/tabStops.js +65 -0
- package/dist/docx/formatting.d.ts +4 -0
- package/dist/docx/formatting.js +4 -0
- package/dist/docx/headersFooters.d.ts +36 -0
- package/dist/docx/headersFooters.js +218 -0
- package/dist/docx/hyperlink.d.ts +75 -0
- package/dist/docx/hyperlink.js +71 -0
- package/dist/docx/importDocx.d.ts +21 -0
- package/dist/docx/importDocx.js +263 -0
- package/dist/docx/importParagraph.d.ts +32 -0
- package/dist/docx/importParagraph.js +308 -0
- package/dist/docx/importTable.d.ts +9 -0
- package/dist/docx/importTable.js +275 -0
- package/dist/docx/media.d.ts +60 -0
- package/dist/docx/media.js +213 -0
- package/dist/docx/notes.d.ts +22 -0
- package/dist/docx/notes.js +71 -0
- package/dist/docx/pageGeometry.d.ts +75 -0
- package/dist/docx/pageGeometry.js +88 -0
- package/dist/docx/paraProps.d.ts +74 -0
- package/dist/docx/paraProps.js +167 -0
- package/dist/docx/propsXml.d.ts +49 -0
- package/dist/docx/propsXml.js +302 -0
- package/dist/docx/relationships.d.ts +42 -0
- package/dist/docx/relationships.js +94 -0
- package/dist/docx/runProps.d.ts +51 -0
- package/dist/docx/runProps.js +207 -0
- package/dist/docx/scan.d.ts +20 -0
- package/dist/docx/scan.js +110 -0
- package/dist/docx/sdt.d.ts +56 -0
- package/dist/docx/sdt.js +104 -0
- package/dist/docx/sdtProps.d.ts +18 -0
- package/dist/docx/sdtProps.js +11 -0
- package/dist/docx/serializeBlock.d.ts +4 -0
- package/dist/docx/serializeBlock.js +30 -0
- package/dist/docx/serializeParagraph.d.ts +15 -0
- package/dist/docx/serializeParagraph.js +208 -0
- package/dist/docx/serializeTable.d.ts +12 -0
- package/dist/docx/serializeTable.js +134 -0
- package/dist/docx/session.d.ts +81 -0
- package/dist/docx/session.js +79 -0
- package/dist/docx/tableFormatting/editing.d.ts +64 -0
- package/dist/docx/tableFormatting/editing.js +318 -0
- package/dist/docx/tableFormatting/reading.d.ts +96 -0
- package/dist/docx/tableFormatting/reading.js +221 -0
- package/dist/docx/tableFormatting.d.ts +2 -0
- package/dist/docx/tableFormatting.js +3 -0
- package/dist/docx/tableTemplate.d.ts +22 -0
- package/dist/docx/tableTemplate.js +111 -0
- package/dist/docx/theme.d.ts +40 -0
- package/dist/docx/theme.js +61 -0
- package/dist/docx/uniqueControls.d.ts +14 -0
- package/dist/docx/uniqueControls.js +62 -0
- package/dist/download.d.ts +57 -0
- package/dist/download.js +44 -0
- package/dist/editor/clipboard/images.d.ts +14 -0
- package/dist/editor/clipboard/images.js +158 -0
- package/dist/editor/clipboard/inlineFormatting.d.ts +21 -0
- package/dist/editor/clipboard/inlineFormatting.js +232 -0
- package/dist/editor/commands/breakCommands.d.ts +8 -0
- package/dist/editor/commands/breakCommands.js +19 -0
- package/dist/editor/commands/canRunCommand.d.ts +6 -0
- package/dist/editor/commands/canRunCommand.js +10 -0
- package/dist/editor/commands/commentCommands.d.ts +3 -0
- package/dist/editor/commands/commentCommands.js +4 -0
- package/dist/editor/commands/comments/editing.d.ts +21 -0
- package/dist/editor/commands/comments/editing.js +305 -0
- package/dist/editor/commands/comments/model.d.ts +31 -0
- package/dist/editor/commands/comments/model.js +0 -0
- package/dist/editor/commands/comments/reading.d.ts +8 -0
- package/dist/editor/commands/comments/reading.js +58 -0
- package/dist/editor/commands/formatting/editing.d.ts +32 -0
- package/dist/editor/commands/formatting/editing.js +138 -0
- package/dist/editor/commands/formatting/reading.d.ts +57 -0
- package/dist/editor/commands/formatting/reading.js +82 -0
- package/dist/editor/commands/formatting/shared.d.ts +32 -0
- package/dist/editor/commands/formatting/shared.js +53 -0
- package/dist/editor/commands/formattingCommands.d.ts +2 -0
- package/dist/editor/commands/formattingCommands.js +3 -0
- package/dist/editor/commands/historyCommands.d.ts +11 -0
- package/dist/editor/commands/historyCommands.js +16 -0
- package/dist/editor/commands/indentCommands.d.ts +15 -0
- package/dist/editor/commands/indentCommands.js +44 -0
- package/dist/editor/commands/index.d.ts +82 -0
- package/dist/editor/commands/index.js +171 -0
- package/dist/editor/commands/linkCommands.d.ts +56 -0
- package/dist/editor/commands/linkCommands.js +146 -0
- package/dist/editor/commands/listCommands.d.ts +58 -0
- package/dist/editor/commands/listCommands.js +140 -0
- package/dist/editor/commands/lockCommands.d.ts +47 -0
- package/dist/editor/commands/lockCommands.js +198 -0
- package/dist/editor/commands/noteQueries.d.ts +12 -0
- package/dist/editor/commands/noteQueries.js +29 -0
- package/dist/editor/commands/paragraphCommands.d.ts +59 -0
- package/dist/editor/commands/paragraphCommands.js +113 -0
- package/dist/editor/commands/spacingCommands.d.ts +23 -0
- package/dist/editor/commands/spacingCommands.js +48 -0
- package/dist/editor/commands/tabCommands.d.ts +5 -0
- package/dist/editor/commands/tabCommands.js +65 -0
- package/dist/editor/createEditor.d.ts +59 -0
- package/dist/editor/createEditor.js +160 -0
- package/dist/editor/documentStyles.d.ts +60 -0
- package/dist/editor/documentStyles.js +80 -0
- package/dist/editor/externalClipboard.d.ts +11 -0
- package/dist/editor/externalClipboard.js +323 -0
- package/dist/editor/imageFiles.d.ts +74 -0
- package/dist/editor/imageFiles.js +173 -0
- package/dist/editor/insertImage.d.ts +31 -0
- package/dist/editor/insertImage.js +35 -0
- package/dist/editor/insertTable.d.ts +18 -0
- package/dist/editor/insertTable.js +44 -0
- package/dist/editor/paragraphEdits.d.ts +42 -0
- package/dist/editor/paragraphEdits.js +59 -0
- package/dist/editor/plainText.d.ts +3 -0
- package/dist/editor/plainText.js +50 -0
- package/dist/editor/plugins/bookmarkProtection.d.ts +3 -0
- package/dist/editor/plugins/bookmarkProtection.js +28 -0
- package/dist/editor/plugins/columnResize.d.ts +27 -0
- package/dist/editor/plugins/columnResize.js +192 -0
- package/dist/editor/plugins/commentDecorations.d.ts +3 -0
- package/dist/editor/plugins/commentDecorations.js +23 -0
- package/dist/editor/plugins/commentReservations.d.ts +5 -0
- package/dist/editor/plugins/commentReservations.js +26 -0
- package/dist/editor/plugins/dropCaret.d.ts +10 -0
- package/dist/editor/plugins/dropCaret.js +11 -0
- package/dist/editor/plugins/imagePaste.d.ts +8 -0
- package/dist/editor/plugins/imagePaste.js +265 -0
- package/dist/editor/plugins/keymap.d.ts +5 -0
- package/dist/editor/plugins/keymap.js +94 -0
- package/dist/editor/plugins/linkPanel.d.ts +23 -0
- package/dist/editor/plugins/linkPanel.js +39 -0
- package/dist/editor/plugins/listInputRules.d.ts +14 -0
- package/dist/editor/plugins/listInputRules.js +36 -0
- package/dist/editor/plugins/lockedContent.d.ts +6 -0
- package/dist/editor/plugins/lockedContent.js +41 -0
- package/dist/editor/plugins/noteProtection.d.ts +3 -0
- package/dist/editor/plugins/noteProtection.js +25 -0
- package/dist/editor/plugins/numberingDecorations.d.ts +42 -0
- package/dist/editor/plugins/numberingDecorations.js +110 -0
- package/dist/editor/plugins/rowResize.d.ts +15 -0
- package/dist/editor/plugins/rowResize.js +196 -0
- package/dist/editor/plugins/styledParagraphs.d.ts +15 -0
- package/dist/editor/plugins/styledParagraphs.js +65 -0
- package/dist/editor/plugins/tabCaret.d.ts +3 -0
- package/dist/editor/plugins/tabCaret.js +126 -0
- package/dist/editor/plugins/tabDecorations.d.ts +11 -0
- package/dist/editor/plugins/tabDecorations.js +87 -0
- package/dist/editor/plugins/tabLayout.d.ts +16 -0
- package/dist/editor/plugins/tabLayout.js +328 -0
- package/dist/editor/plugins/tabPointer.d.ts +7 -0
- package/dist/editor/plugins/tabPointer.js +95 -0
- package/dist/editor/plugins/tableContextMenu.d.ts +21 -0
- package/dist/editor/plugins/tableContextMenu.js +88 -0
- package/dist/editor/plugins/textContextMenu.d.ts +15 -0
- package/dist/editor/plugins/textContextMenu.js +90 -0
- package/dist/editor/views/imageResize.d.ts +53 -0
- package/dist/editor/views/imageResize.js +167 -0
- package/dist/editor/views/runMarkView.d.ts +15 -0
- package/dist/editor/views/runMarkView.js +9 -0
- package/dist/editor/views/tableView.d.ts +24 -0
- package/dist/editor/views/tableView.js +32 -0
- package/dist/index.d.ts +44 -0
- package/dist/index.js +38 -0
- package/dist/model/format.d.ts +240 -0
- package/dist/model/format.js +305 -0
- package/dist/model/tabStops.d.ts +17 -0
- package/dist/model/tabStops.js +22 -0
- package/dist/numbering/listTemplate.d.ts +29 -0
- package/dist/numbering/listTemplate.js +83 -0
- package/dist/numbering/markers.d.ts +24 -0
- package/dist/numbering/markers.js +93 -0
- package/dist/numbering/parseNumbering.d.ts +65 -0
- package/dist/numbering/parseNumbering.js +116 -0
- package/dist/numbering/writeNumbering.d.ts +13 -0
- package/dist/numbering/writeNumbering.js +49 -0
- package/dist/ooxml/errors.d.ts +39 -0
- package/dist/ooxml/errors.js +21 -0
- package/dist/ooxml/image.d.ts +88 -0
- package/dist/ooxml/image.js +106 -0
- package/dist/ooxml/tabStops.d.ts +3 -0
- package/dist/ooxml/tabStops.js +51 -0
- package/dist/ooxml/units.d.ts +37 -0
- package/dist/ooxml/units.js +116 -0
- package/dist/ooxml/xml.d.ts +36 -0
- package/dist/ooxml/xml.js +89 -0
- package/dist/page/PageGuides.d.ts +16 -0
- package/dist/page/PageGuides.js +114 -0
- package/dist/page/measureBlocks.d.ts +25 -0
- package/dist/page/measureBlocks.js +80 -0
- package/dist/page/pageDecorations.d.ts +36 -0
- package/dist/page/pageDecorations.js +211 -0
- package/dist/page/pageLayout.d.ts +176 -0
- package/dist/page/pageLayout.js +157 -0
- package/dist/page/tableMeasurements.d.ts +10 -0
- package/dist/page/tableMeasurements.js +115 -0
- package/dist/page/usePageLayout.d.ts +57 -0
- package/dist/page/usePageLayout.js +141 -0
- package/dist/schema/docxSchema.d.ts +9 -0
- package/dist/schema/docxSchema.js +863 -0
- package/dist/schema/index.d.ts +2 -0
- package/dist/schema/index.js +9 -0
- package/dist/schema/locks.d.ts +112 -0
- package/dist/schema/locks.js +211 -0
- package/dist/schema/rendering.d.ts +6 -0
- package/dist/schema/rendering.js +46 -0
- package/dist/styles/classNames.d.ts +214 -0
- package/dist/styles/classNames.js +208 -0
- package/dist/styles/colors.d.ts +11 -0
- package/dist/styles/colors.js +14 -0
- package/dist/styles/fontSizes.d.ts +6 -0
- package/dist/styles/fontSizes.js +20 -0
- package/dist/styles/fontStack.d.ts +55 -0
- package/dist/styles/fontStack.js +262 -0
- package/dist/styles/fonts.d.ts +6 -0
- package/dist/styles/fonts.js +11 -0
- package/dist/styles/inlineStyle.d.ts +67 -0
- package/dist/styles/inlineStyle.js +212 -0
- package/dist/styles/lineSpacings.d.ts +11 -0
- package/dist/styles/lineSpacings.js +10 -0
- package/dist/styles.css +1650 -0
- package/dist/table/cellBorders.d.ts +12 -0
- package/dist/table/cellBorders.js +9 -0
- package/dist/table/cellFormatting.d.ts +57 -0
- package/dist/table/cellFormatting.js +173 -0
- package/dist/table/columns.d.ts +25 -0
- package/dist/table/columns.js +155 -0
- package/dist/table/commands.d.ts +19 -0
- package/dist/table/commands.js +63 -0
- package/dist/table/format.d.ts +35 -0
- package/dist/table/format.js +26 -0
- package/dist/table/gridBorders.d.ts +42 -0
- package/dist/table/gridBorders.js +192 -0
- package/dist/table/index.d.ts +16 -0
- package/dist/table/index.js +52 -0
- package/dist/table/merge.d.ts +36 -0
- package/dist/table/merge.js +125 -0
- package/dist/table/resize.d.ts +55 -0
- package/dist/table/resize.js +66 -0
- package/dist/table/rowResize.d.ts +14 -0
- package/dist/table/rowResize.js +44 -0
- package/dist/table/rows.d.ts +32 -0
- package/dist/table/rows.js +89 -0
- package/dist/table/widths.d.ts +69 -0
- package/dist/table/widths.js +97 -0
- package/dist/ui/AlignMenu.d.ts +15 -0
- package/dist/ui/AlignMenu.js +64 -0
- package/dist/ui/CellStyleGroup.d.ts +15 -0
- package/dist/ui/CellStyleGroup.js +232 -0
- package/dist/ui/ColorPicker.d.ts +13 -0
- package/dist/ui/ColorPicker.js +165 -0
- package/dist/ui/CommentsPanel.d.ts +15 -0
- package/dist/ui/CommentsPanel.js +348 -0
- package/dist/ui/FontFamilySelect.d.ts +14 -0
- package/dist/ui/FontFamilySelect.js +59 -0
- package/dist/ui/FontSizeSelect.d.ts +11 -0
- package/dist/ui/FontSizeSelect.js +49 -0
- package/dist/ui/InsertImageButton.d.ts +9 -0
- package/dist/ui/InsertImageButton.js +47 -0
- package/dist/ui/LinkCard.d.ts +12 -0
- package/dist/ui/LinkCard.js +127 -0
- package/dist/ui/LinkPanel.d.ts +9 -0
- package/dist/ui/LinkPanel.js +121 -0
- package/dist/ui/MenuChoice.d.ts +11 -0
- package/dist/ui/MenuChoice.js +32 -0
- package/dist/ui/NotesPanel.d.ts +6 -0
- package/dist/ui/NotesPanel.js +33 -0
- package/dist/ui/ParagraphStyleSelect.d.ts +11 -0
- package/dist/ui/ParagraphStyleSelect.js +62 -0
- package/dist/ui/Popover.d.ts +22 -0
- package/dist/ui/Popover.js +74 -0
- package/dist/ui/SpacingMenu.d.ts +12 -0
- package/dist/ui/SpacingMenu.js +54 -0
- package/dist/ui/TableMenu.d.ts +12 -0
- package/dist/ui/TableMenu.js +112 -0
- package/dist/ui/TableSizePicker.d.ts +9 -0
- package/dist/ui/TableSizePicker.js +53 -0
- package/dist/ui/TextMenu.d.ts +15 -0
- package/dist/ui/TextMenu.js +194 -0
- package/dist/ui/Toolbar.d.ts +19 -0
- package/dist/ui/Toolbar.js +448 -0
- package/dist/ui/ToolbarButton.d.ts +23 -0
- package/dist/ui/ToolbarButton.js +56 -0
- package/dist/ui/Tooltip.d.ts +19 -0
- package/dist/ui/Tooltip.js +47 -0
- package/dist/ui/ZoomSelect.d.ts +7 -0
- package/dist/ui/ZoomSelect.js +44 -0
- package/dist/ui/comments/CommentComposer.d.ts +17 -0
- package/dist/ui/comments/CommentComposer.js +59 -0
- package/dist/ui/comments/useCommentRailLayout.d.ts +18 -0
- package/dist/ui/comments/useCommentRailLayout.js +183 -0
- package/dist/ui/keyboardWalk.d.ts +22 -0
- package/dist/ui/keyboardWalk.js +70 -0
- package/dist/ui/openAddress.d.ts +11 -0
- package/dist/ui/openAddress.js +16 -0
- package/dist/ui/panelPlacement.d.ts +51 -0
- package/dist/ui/panelPlacement.js +71 -0
- package/dist/ui/presets.d.ts +11 -0
- package/dist/ui/presets.js +0 -0
- package/dist/ui/rovingFocus.d.ts +20 -0
- package/dist/ui/rovingFocus.js +61 -0
- package/dist/ui/runCommand.d.ts +5 -0
- package/dist/ui/runCommand.js +13 -0
- package/dist/ui/useDismiss.d.ts +6 -0
- package/dist/ui/useDismiss.js +31 -0
- package/dist/ui/useFitWidthZoom.d.ts +2 -0
- package/dist/ui/useFitWidthZoom.js +26 -0
- package/dist/ui/useGridKeyboard.d.ts +12 -0
- package/dist/ui/useGridKeyboard.js +90 -0
- package/dist/ui/useMenuKeyboard.d.ts +12 -0
- package/dist/ui/useMenuKeyboard.js +56 -0
- package/dist/ui/zoom.d.ts +4 -0
- package/dist/ui/zoom.js +21 -0
- package/package.json +120 -0
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
// src/DocxEditor.tsx
|
|
2
|
+
import {
|
|
3
|
+
forwardRef,
|
|
4
|
+
useEffect,
|
|
5
|
+
useImperativeHandle,
|
|
6
|
+
useLayoutEffect,
|
|
7
|
+
useMemo,
|
|
8
|
+
useRef,
|
|
9
|
+
useState
|
|
10
|
+
} from "react";
|
|
11
|
+
import { exportDocx } from "./docx/exportDocx.js";
|
|
12
|
+
import { importDocx } from "./docx/importDocx.js";
|
|
13
|
+
import {
|
|
14
|
+
documentComments
|
|
15
|
+
} from "./editor/commands/commentCommands.js";
|
|
16
|
+
import { activeLinkSpan } from "./editor/commands/linkCommands.js";
|
|
17
|
+
import { createEditorState, createEditorView } from "./editor/createEditor.js";
|
|
18
|
+
import { isLinkPanelOpen } from "./editor/plugins/linkPanel.js";
|
|
19
|
+
import { tableMenuAnchor } from "./editor/plugins/tableContextMenu.js";
|
|
20
|
+
import { textMenuAnchor } from "./editor/plugins/textContextMenu.js";
|
|
21
|
+
import { parseNumbering } from "./numbering/parseNumbering.js";
|
|
22
|
+
import { DocxImportError } from "./ooxml/errors.js";
|
|
23
|
+
import { PageGuides } from "./page/PageGuides.js";
|
|
24
|
+
import { A4_PAGE_PIXELS, pagePixels } from "./page/pageLayout.js";
|
|
25
|
+
import { usePageLayout } from "./page/usePageLayout.js";
|
|
26
|
+
import { editorClassNames } from "./styles/classNames.js";
|
|
27
|
+
import { CommentsPanel } from "./ui/CommentsPanel.js";
|
|
28
|
+
import { LinkCard } from "./ui/LinkCard.js";
|
|
29
|
+
import { LinkPanel } from "./ui/LinkPanel.js";
|
|
30
|
+
import { NotesPanel } from "./ui/NotesPanel.js";
|
|
31
|
+
import { TableMenu } from "./ui/TableMenu.js";
|
|
32
|
+
import { TextMenu } from "./ui/TextMenu.js";
|
|
33
|
+
import { Toolbar } from "./ui/Toolbar.js";
|
|
34
|
+
import { useFitWidthZoom } from "./ui/useFitWidthZoom.js";
|
|
35
|
+
import { normalizeZoom } from "./ui/zoom.js";
|
|
36
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
37
|
+
function openDocument(bytes) {
|
|
38
|
+
try {
|
|
39
|
+
const { doc, session } = importDocx(bytes);
|
|
40
|
+
return {
|
|
41
|
+
status: "opened",
|
|
42
|
+
doc,
|
|
43
|
+
session,
|
|
44
|
+
numbering: parseNumbering(session.numberingXml)
|
|
45
|
+
};
|
|
46
|
+
} catch (error) {
|
|
47
|
+
if (error instanceof DocxImportError) return { status: "rejected", error };
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function useLatest(value) {
|
|
52
|
+
const box = useRef(value);
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
box.current = value;
|
|
55
|
+
});
|
|
56
|
+
return box;
|
|
57
|
+
}
|
|
58
|
+
function isBlob(source) {
|
|
59
|
+
return typeof Blob !== "undefined" && source instanceof Blob;
|
|
60
|
+
}
|
|
61
|
+
function useDocumentBytes(source) {
|
|
62
|
+
const [read, setRead] = useState(null);
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
if (!isBlob(source)) return;
|
|
65
|
+
let current = true;
|
|
66
|
+
source.arrayBuffer().then((bytes) => {
|
|
67
|
+
if (current) setRead({ of: source, status: "read", bytes });
|
|
68
|
+
}).catch((error) => {
|
|
69
|
+
if (current) setRead({ of: source, status: "failed", error });
|
|
70
|
+
});
|
|
71
|
+
return () => {
|
|
72
|
+
current = false;
|
|
73
|
+
};
|
|
74
|
+
}, [source]);
|
|
75
|
+
if (!isBlob(source)) return source;
|
|
76
|
+
if (read?.of !== source) return null;
|
|
77
|
+
if (read.status === "failed") throw read.error;
|
|
78
|
+
return read.bytes;
|
|
79
|
+
}
|
|
80
|
+
var IMPORT_REJECTION_REASON = {
|
|
81
|
+
"not-a-docx": "This file is not a docx document, or it is damaged.",
|
|
82
|
+
"too-large": "This document is too large to open.",
|
|
83
|
+
"missing-part": "This document is missing the part that holds its body.",
|
|
84
|
+
"missing-body": "This document has no body.",
|
|
85
|
+
"malformed-xml": "The XML inside this document cannot be read.",
|
|
86
|
+
"unsupported-content": "This document holds content that could not be kept as it is."
|
|
87
|
+
};
|
|
88
|
+
function ImportRejection({ error }) {
|
|
89
|
+
return /* @__PURE__ */ jsxs("div", { className: editorClassNames.rejection, role: "alert", children: [
|
|
90
|
+
/* @__PURE__ */ jsx("p", { className: editorClassNames.rejectionTitle, children: "This document was not opened" }),
|
|
91
|
+
/* @__PURE__ */ jsx("p", { children: IMPORT_REJECTION_REASON[error.code] })
|
|
92
|
+
] });
|
|
93
|
+
}
|
|
94
|
+
function zoomVariable(factor) {
|
|
95
|
+
return { "--docx-editor-zoom": factor };
|
|
96
|
+
}
|
|
97
|
+
function DocxEditorSurface({
|
|
98
|
+
document: source,
|
|
99
|
+
renderImportError,
|
|
100
|
+
mode = { kind: "edit" },
|
|
101
|
+
showPageGuides = true,
|
|
102
|
+
zoom,
|
|
103
|
+
defaultZoom = "fit-width",
|
|
104
|
+
onZoomChange,
|
|
105
|
+
fontFallbacks,
|
|
106
|
+
presets,
|
|
107
|
+
commentAuthor = { name: "Anonymous" },
|
|
108
|
+
plugins,
|
|
109
|
+
className,
|
|
110
|
+
style,
|
|
111
|
+
onReady,
|
|
112
|
+
onChange
|
|
113
|
+
}, ref) {
|
|
114
|
+
const readOnly = mode.kind === "readOnly";
|
|
115
|
+
const showToolbar = mode.kind === "edit" && (mode.toolbar ?? true);
|
|
116
|
+
const allowLocking = mode.kind === "edit" && (mode.locking ?? false);
|
|
117
|
+
const bytes = useDocumentBytes(source);
|
|
118
|
+
const opened = useMemo(
|
|
119
|
+
() => bytes === null ? null : openDocument(bytes),
|
|
120
|
+
[bytes]
|
|
121
|
+
);
|
|
122
|
+
const mountedPlugins = useRef(plugins).current;
|
|
123
|
+
const mountedContextMenus = useRef(
|
|
124
|
+
mode.kind !== "edit" || (mode.contextMenus ?? true)
|
|
125
|
+
).current;
|
|
126
|
+
const mountedFontFallbacks = useRef(fontFallbacks).current;
|
|
127
|
+
const layerRef = useRef(null);
|
|
128
|
+
const rootRef = useRef(null);
|
|
129
|
+
const mountRef = useRef(null);
|
|
130
|
+
const keptState = useRef(
|
|
131
|
+
null
|
|
132
|
+
);
|
|
133
|
+
const viewRef = useRef(null);
|
|
134
|
+
const [live, setLive] = useState(null);
|
|
135
|
+
const [commentComposerOpen, setCommentComposerOpen] = useState(false);
|
|
136
|
+
const [commentsOpen, setCommentsOpen] = useState(false);
|
|
137
|
+
const [uncontrolledZoom, setUncontrolledZoom] = useState(
|
|
138
|
+
() => normalizeZoom(defaultZoom)
|
|
139
|
+
);
|
|
140
|
+
const latestOnReady = useLatest(onReady);
|
|
141
|
+
const latestOnChange = useLatest(onChange);
|
|
142
|
+
const selectedZoom = normalizeZoom(zoom ?? uncontrolledZoom);
|
|
143
|
+
const page = opened?.status === "opened" ? pagePixels(opened.session.geometry) : A4_PAGE_PIXELS;
|
|
144
|
+
const fitWidth = useFitWidthZoom(rootRef, page.pageWidth);
|
|
145
|
+
const effectiveZoom = selectedZoom === "fit-width" ? fitWidth : selectedZoom;
|
|
146
|
+
const changeZoom = (next) => {
|
|
147
|
+
const normalized = normalizeZoom(next);
|
|
148
|
+
if (zoom === void 0) setUncontrolledZoom(normalized);
|
|
149
|
+
onZoomChange?.(normalized);
|
|
150
|
+
};
|
|
151
|
+
useLayoutEffect(() => {
|
|
152
|
+
const mount = mountRef.current;
|
|
153
|
+
if (!mount || opened?.status !== "opened") return;
|
|
154
|
+
const kept = keptState.current;
|
|
155
|
+
const view = createEditorView({
|
|
156
|
+
mount,
|
|
157
|
+
state: kept?.of === opened ? kept.state : createEditorState(opened.doc, {
|
|
158
|
+
numbering: opened.numbering,
|
|
159
|
+
styles: opened.session.styles,
|
|
160
|
+
defaults: opened.session.defaults,
|
|
161
|
+
paragraphDefaults: opened.session.paragraphDefaults,
|
|
162
|
+
canStartNewList: opened.session.numberingPartPath !== null,
|
|
163
|
+
consumerPlugins: mountedPlugins,
|
|
164
|
+
paragraphStyles: opened.session.paragraphStyles,
|
|
165
|
+
contextMenus: mountedContextMenus,
|
|
166
|
+
geometry: opened.session.geometry,
|
|
167
|
+
defaultTabStopPt: opened.session.defaultTabStopPt,
|
|
168
|
+
reservedCommentIds: opened.session.comments.byId.keys(),
|
|
169
|
+
reservedCommentParaIds: [
|
|
170
|
+
...opened.session.comments.ordered.flatMap(
|
|
171
|
+
(comment) => comment.paraId === null ? [] : [comment.paraId]
|
|
172
|
+
),
|
|
173
|
+
...opened.session.comments.extendedOrdered.map(
|
|
174
|
+
(extension) => extension.paraId
|
|
175
|
+
)
|
|
176
|
+
]
|
|
177
|
+
}),
|
|
178
|
+
defaults: opened.session.defaults,
|
|
179
|
+
geometry: opened.session.geometry,
|
|
180
|
+
readOnly,
|
|
181
|
+
fontFallbacks: mountedFontFallbacks,
|
|
182
|
+
onStateChange: (state) => {
|
|
183
|
+
keptState.current = { of: opened, state };
|
|
184
|
+
setLive({ view, state });
|
|
185
|
+
latestOnChange.current?.();
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
viewRef.current = view;
|
|
189
|
+
keptState.current = { of: opened, state: view.state };
|
|
190
|
+
setLive({ view, state: view.state });
|
|
191
|
+
latestOnReady.current?.(view);
|
|
192
|
+
return () => {
|
|
193
|
+
view.destroy();
|
|
194
|
+
viewRef.current = null;
|
|
195
|
+
setLive(null);
|
|
196
|
+
};
|
|
197
|
+
}, [
|
|
198
|
+
opened,
|
|
199
|
+
readOnly,
|
|
200
|
+
mountedContextMenus,
|
|
201
|
+
mountedFontFallbacks,
|
|
202
|
+
mountedPlugins,
|
|
203
|
+
latestOnReady,
|
|
204
|
+
latestOnChange
|
|
205
|
+
]);
|
|
206
|
+
useImperativeHandle(ref, () => {
|
|
207
|
+
const view = viewRef.current;
|
|
208
|
+
if (!view || opened?.status !== "opened") return null;
|
|
209
|
+
const session = opened.session;
|
|
210
|
+
return { view, exportBytes: () => exportDocx(view.state.doc, session) };
|
|
211
|
+
}, [opened, readOnly]);
|
|
212
|
+
const overlay = usePageLayout({
|
|
213
|
+
view: live?.view ?? null,
|
|
214
|
+
layer: layerRef,
|
|
215
|
+
enabled: showPageGuides,
|
|
216
|
+
revision: live?.state.doc,
|
|
217
|
+
geometry: opened?.status === "opened" ? opened.session.geometry : void 0
|
|
218
|
+
});
|
|
219
|
+
if (opened?.status === "rejected") {
|
|
220
|
+
return renderImportError ? renderImportError(opened.error) : /* @__PURE__ */ jsx(ImportRejection, { error: opened.error });
|
|
221
|
+
}
|
|
222
|
+
const textAnchor = live && !readOnly ? textMenuAnchor(live.state) : null;
|
|
223
|
+
const tableAnchor = live && !readOnly && !textAnchor ? tableMenuAnchor(live.state) : null;
|
|
224
|
+
const linkAtCursor = live && !live.view.composing && !isLinkPanelOpen(live.state) ? activeLinkSpan(live.state) : null;
|
|
225
|
+
const comments = live?.state === void 0 ? [] : documentComments(live.state);
|
|
226
|
+
const hasUnresolvedComments = comments.some((comment) => !comment.resolved);
|
|
227
|
+
const effectiveComposerOpen = commentComposerOpen && !readOnly;
|
|
228
|
+
const showComments = live !== null && (commentsOpen || effectiveComposerOpen || hasUnresolvedComments);
|
|
229
|
+
const commentsPanel = live && showComments && /* @__PURE__ */ jsx(
|
|
230
|
+
CommentsPanel,
|
|
231
|
+
{
|
|
232
|
+
view: live.view,
|
|
233
|
+
state: live.state,
|
|
234
|
+
readOnly,
|
|
235
|
+
composerOpen: effectiveComposerOpen,
|
|
236
|
+
author: commentAuthor,
|
|
237
|
+
closeComposer: () => setCommentComposerOpen(false),
|
|
238
|
+
scrollContainer: rootRef.current,
|
|
239
|
+
allCommentsOpen: commentsOpen
|
|
240
|
+
}
|
|
241
|
+
);
|
|
242
|
+
return /* @__PURE__ */ jsxs(
|
|
243
|
+
"div",
|
|
244
|
+
{
|
|
245
|
+
className: [editorClassNames.frame, className].filter(Boolean).join(" "),
|
|
246
|
+
style,
|
|
247
|
+
children: [
|
|
248
|
+
live && showToolbar && /* @__PURE__ */ jsx(
|
|
249
|
+
Toolbar,
|
|
250
|
+
{
|
|
251
|
+
view: live.view,
|
|
252
|
+
state: live.state,
|
|
253
|
+
fontFallbacks: mountedFontFallbacks,
|
|
254
|
+
presets,
|
|
255
|
+
commentsOpen,
|
|
256
|
+
onToggleComments: () => {
|
|
257
|
+
setCommentsOpen((open) => {
|
|
258
|
+
if (open) setCommentComposerOpen(false);
|
|
259
|
+
return !open;
|
|
260
|
+
});
|
|
261
|
+
},
|
|
262
|
+
zoom: selectedZoom,
|
|
263
|
+
onZoomChange: changeZoom
|
|
264
|
+
}
|
|
265
|
+
),
|
|
266
|
+
/* @__PURE__ */ jsxs(
|
|
267
|
+
"div",
|
|
268
|
+
{
|
|
269
|
+
className: editorClassNames.workspace,
|
|
270
|
+
"data-comments": showComments ? "visible" : void 0,
|
|
271
|
+
style: zoomVariable(effectiveZoom),
|
|
272
|
+
children: [
|
|
273
|
+
live && readOnly && comments.length > 0 && /* @__PURE__ */ jsx(
|
|
274
|
+
"button",
|
|
275
|
+
{
|
|
276
|
+
type: "button",
|
|
277
|
+
className: editorClassNames.commentsToggle,
|
|
278
|
+
"aria-expanded": commentsOpen,
|
|
279
|
+
onClick: () => setCommentsOpen((open) => !open),
|
|
280
|
+
children: commentsOpen ? "Hide comments" : "Show comments"
|
|
281
|
+
}
|
|
282
|
+
),
|
|
283
|
+
/* @__PURE__ */ jsxs("div", { ref: rootRef, className: editorClassNames.root, children: [
|
|
284
|
+
/* @__PURE__ */ jsxs(
|
|
285
|
+
"div",
|
|
286
|
+
{
|
|
287
|
+
ref: layerRef,
|
|
288
|
+
className: editorClassNames.pageLayer,
|
|
289
|
+
style: { zoom: effectiveZoom },
|
|
290
|
+
children: [
|
|
291
|
+
/* @__PURE__ */ jsx("div", { ref: mountRef }),
|
|
292
|
+
overlay && /* @__PURE__ */ jsx(
|
|
293
|
+
PageGuides,
|
|
294
|
+
{
|
|
295
|
+
overlay,
|
|
296
|
+
headersFooters: opened?.status === "opened" ? opened.session.headersFooters : void 0
|
|
297
|
+
}
|
|
298
|
+
),
|
|
299
|
+
live && opened?.status === "opened" && /* @__PURE__ */ jsx(NotesPanel, { state: live.state, pageWidth: page.pageWidth })
|
|
300
|
+
]
|
|
301
|
+
}
|
|
302
|
+
),
|
|
303
|
+
!commentsOpen && commentsPanel
|
|
304
|
+
] }),
|
|
305
|
+
commentsOpen && commentsPanel
|
|
306
|
+
]
|
|
307
|
+
}
|
|
308
|
+
),
|
|
309
|
+
live && !readOnly && isLinkPanelOpen(live.state) && /* @__PURE__ */ jsx(LinkPanel, { view: live.view, state: live.state }),
|
|
310
|
+
live && linkAtCursor && /* @__PURE__ */ jsx(
|
|
311
|
+
LinkCard,
|
|
312
|
+
{
|
|
313
|
+
view: live.view,
|
|
314
|
+
state: live.state,
|
|
315
|
+
link: linkAtCursor,
|
|
316
|
+
readOnly
|
|
317
|
+
},
|
|
318
|
+
linkAtCursor.from
|
|
319
|
+
),
|
|
320
|
+
live && textAnchor && /* @__PURE__ */ jsx(
|
|
321
|
+
TextMenu,
|
|
322
|
+
{
|
|
323
|
+
view: live.view,
|
|
324
|
+
state: live.state,
|
|
325
|
+
anchor: textAnchor,
|
|
326
|
+
allowLocking,
|
|
327
|
+
onAddComment: () => {
|
|
328
|
+
setCommentComposerOpen(true);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
),
|
|
332
|
+
live && tableAnchor && /* @__PURE__ */ jsx(
|
|
333
|
+
TableMenu,
|
|
334
|
+
{
|
|
335
|
+
view: live.view,
|
|
336
|
+
state: live.state,
|
|
337
|
+
anchor: tableAnchor,
|
|
338
|
+
allowLocking
|
|
339
|
+
}
|
|
340
|
+
)
|
|
341
|
+
]
|
|
342
|
+
}
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
var DocxEditor = forwardRef(DocxEditorSurface);
|
|
346
|
+
export {
|
|
347
|
+
DocxEditor
|
|
348
|
+
};
|
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-free DOCX import and export API. The opaque session preserves package parts between
|
|
3
|
+
* calls; server runtimes must provide global `DOMParser` and `Node` implementations.
|
|
4
|
+
*/
|
|
5
|
+
import type { Node as PMNode } from "prosemirror-model";
|
|
6
|
+
import { type DocxBytes } from "./docx/importDocx";
|
|
7
|
+
import type { DocxSession } from "./docx/session";
|
|
8
|
+
export { exportDocx } from "./docx/exportDocx";
|
|
9
|
+
export type { ParagraphStyleOption } from "./docx/formatting";
|
|
10
|
+
export type { DocxBytes } from "./docx/importDocx";
|
|
11
|
+
export type { DocxSession } from "./docx/session";
|
|
12
|
+
export { documentNumbering, documentPartPath } from "./docx/session";
|
|
13
|
+
export type { CellFormat, CellVerticalAlign, DocumentDefaults, HighlightName, LineSpacing, NumberingRef, ParagraphAlign, ParagraphFormat, RowFormat, RowHeight, RunFormat, TableFormat, TableWidth, TableWidthType, UnderlineKind, VerticalAlign, } from "./model/format";
|
|
14
|
+
export { toCellFormat, toParagraphFormat, toRowFormat, toRunFormat, toTableFormat, toTableWidth, } from "./model/format";
|
|
15
|
+
export type { LevelIndent, NumberFormat, Numbering, NumberingLevel, NumberingList, } from "./numbering/parseNumbering";
|
|
16
|
+
export { parseNumbering } from "./numbering/parseNumbering";
|
|
17
|
+
export type { DocxExportErrorCode, DocxImportErrorCode, } from "./ooxml/errors";
|
|
18
|
+
export { DocxExportError, DocxImportError } from "./ooxml/errors";
|
|
19
|
+
/**
|
|
20
|
+
* An image node's size is written in EMU, the unit the document itself uses. A builder
|
|
21
|
+
* that places an image is working in pixels, so the conversion ships with the type.
|
|
22
|
+
*/
|
|
23
|
+
export type { ImageExtent } from "./ooxml/image";
|
|
24
|
+
export { emuToPx, pxToEmu, toImageExtent } from "./ooxml/image";
|
|
25
|
+
export { docxSchema } from "./schema";
|
|
26
|
+
/**
|
|
27
|
+
* Opens docx bytes into the document to work on and the session that remembers the file they came
|
|
28
|
+
* from.
|
|
29
|
+
*
|
|
30
|
+
* The engine hands out the store it fills in; this is where it narrows to the opaque session, so
|
|
31
|
+
* that the original XML behind it stays the exporter's business.
|
|
32
|
+
*/
|
|
33
|
+
export declare function importDocx(input: DocxBytes): {
|
|
34
|
+
doc: PMNode;
|
|
35
|
+
session: DocxSession;
|
|
36
|
+
};
|
package/dist/core.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// src/core.ts
|
|
2
|
+
import { importDocx as openDocx } from "./docx/importDocx.js";
|
|
3
|
+
import { exportDocx } from "./docx/exportDocx.js";
|
|
4
|
+
import { documentNumbering, documentPartPath } from "./docx/session.js";
|
|
5
|
+
import {
|
|
6
|
+
toCellFormat,
|
|
7
|
+
toParagraphFormat,
|
|
8
|
+
toRowFormat,
|
|
9
|
+
toRunFormat,
|
|
10
|
+
toTableFormat,
|
|
11
|
+
toTableWidth
|
|
12
|
+
} from "./model/format.js";
|
|
13
|
+
import { parseNumbering } from "./numbering/parseNumbering.js";
|
|
14
|
+
import { DocxExportError, DocxImportError } from "./ooxml/errors.js";
|
|
15
|
+
import { emuToPx, pxToEmu, toImageExtent } from "./ooxml/image.js";
|
|
16
|
+
import { docxSchema } from "./schema/index.js";
|
|
17
|
+
function importDocx(input) {
|
|
18
|
+
return openDocx(input);
|
|
19
|
+
}
|
|
20
|
+
export {
|
|
21
|
+
DocxExportError,
|
|
22
|
+
DocxImportError,
|
|
23
|
+
documentNumbering,
|
|
24
|
+
documentPartPath,
|
|
25
|
+
docxSchema,
|
|
26
|
+
emuToPx,
|
|
27
|
+
exportDocx,
|
|
28
|
+
importDocx,
|
|
29
|
+
parseNumbering,
|
|
30
|
+
pxToEmu,
|
|
31
|
+
toCellFormat,
|
|
32
|
+
toImageExtent,
|
|
33
|
+
toParagraphFormat,
|
|
34
|
+
toRowFormat,
|
|
35
|
+
toRunFormat,
|
|
36
|
+
toTableFormat,
|
|
37
|
+
toTableWidth
|
|
38
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const COMMENTS_REL_TYPE = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments";
|
|
2
|
+
export declare const COMMENTS_EXTENDED_REL_TYPE = "http://schemas.microsoft.com/office/2011/relationships/commentsExtended";
|
|
3
|
+
export declare const COMMENTS_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml";
|
|
4
|
+
export declare const COMMENTS_EXTENDED_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.commentsExtended+xml";
|
|
5
|
+
export declare const CONTENT_TYPES_PATH = "[Content_Types].xml";
|
|
6
|
+
export declare const W14_NS = "http://schemas.microsoft.com/office/word/2010/wordml";
|
|
7
|
+
export declare const W15_NS = "http://schemas.microsoft.com/office/word/2012/wordml";
|
|
8
|
+
export declare const MC_NS = "http://schemas.openxmlformats.org/markup-compatibility/2006";
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/docx/comments/constants.ts
|
|
2
|
+
import { R_NS } from "../../ooxml/xml.js";
|
|
3
|
+
var COMMENTS_REL_TYPE = `${R_NS}/comments`;
|
|
4
|
+
var COMMENTS_EXTENDED_REL_TYPE = "http://schemas.microsoft.com/office/2011/relationships/commentsExtended";
|
|
5
|
+
var COMMENTS_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml";
|
|
6
|
+
var COMMENTS_EXTENDED_CONTENT_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.commentsExtended+xml";
|
|
7
|
+
var CONTENT_TYPES_PATH = "[Content_Types].xml";
|
|
8
|
+
var W14_NS = "http://schemas.microsoft.com/office/word/2010/wordml";
|
|
9
|
+
var W15_NS = "http://schemas.microsoft.com/office/word/2012/wordml";
|
|
10
|
+
var MC_NS = "http://schemas.openxmlformats.org/markup-compatibility/2006";
|
|
11
|
+
export {
|
|
12
|
+
COMMENTS_CONTENT_TYPE,
|
|
13
|
+
COMMENTS_EXTENDED_CONTENT_TYPE,
|
|
14
|
+
COMMENTS_EXTENDED_REL_TYPE,
|
|
15
|
+
COMMENTS_REL_TYPE,
|
|
16
|
+
CONTENT_TYPES_PATH,
|
|
17
|
+
MC_NS,
|
|
18
|
+
W14_NS,
|
|
19
|
+
W15_NS
|
|
20
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps imported comment threads to and from ProseMirror reference data.
|
|
3
|
+
*/
|
|
4
|
+
import type { Node as PMNode } from "prosemirror-model";
|
|
5
|
+
import type { ImportedComments } from "./reading";
|
|
6
|
+
export interface CommentReferenceData {
|
|
7
|
+
id: string;
|
|
8
|
+
author: string | null;
|
|
9
|
+
initials: string | null;
|
|
10
|
+
date: string | null;
|
|
11
|
+
text: string;
|
|
12
|
+
commentXml: string | null;
|
|
13
|
+
imported: boolean;
|
|
14
|
+
paraId: string;
|
|
15
|
+
resolved: boolean;
|
|
16
|
+
extensionXml: string | null;
|
|
17
|
+
threadImported: boolean;
|
|
18
|
+
replies: readonly CommentReplyData[];
|
|
19
|
+
}
|
|
20
|
+
export interface CommentReplyData {
|
|
21
|
+
id: string;
|
|
22
|
+
author: string | null;
|
|
23
|
+
initials: string | null;
|
|
24
|
+
date: string | null;
|
|
25
|
+
text: string;
|
|
26
|
+
commentXml: string | null;
|
|
27
|
+
imported: boolean;
|
|
28
|
+
paraId: string;
|
|
29
|
+
parentParaId: string;
|
|
30
|
+
extensionXml: string | null;
|
|
31
|
+
}
|
|
32
|
+
/** A stable Word paragraph id for a comment created or upgraded by the editor. */
|
|
33
|
+
export declare function commentParaId(seed: string): string;
|
|
34
|
+
/** Replies below one referenced comment, in the order of the Comments part. */
|
|
35
|
+
export declare function importedCommentReplies(comments: ImportedComments, rootId: string): readonly CommentReplyData[];
|
|
36
|
+
/** The first reference for each comment id, in document order. */
|
|
37
|
+
export declare function commentReferencesIn(doc: PMNode): ReadonlyMap<string, CommentReferenceData>;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
// src/docx/comments/model.ts
|
|
2
|
+
function commentParaId(seed) {
|
|
3
|
+
let hash = 2166136261;
|
|
4
|
+
for (const char of seed) {
|
|
5
|
+
hash ^= char.charCodeAt(0);
|
|
6
|
+
hash = Math.imul(hash, 16777619);
|
|
7
|
+
}
|
|
8
|
+
const value = hash >>> 0 & 2147483647;
|
|
9
|
+
return (value === 0 ? 1 : value).toString(16).toUpperCase().padStart(8, "0");
|
|
10
|
+
}
|
|
11
|
+
function importedCommentReplies(comments, rootId) {
|
|
12
|
+
const replies = [];
|
|
13
|
+
const visited = /* @__PURE__ */ new Set([rootId]);
|
|
14
|
+
const pending = [...comments.repliesByParentId.get(rootId) ?? []].reverse().map((reply) => ({ parentId: rootId, reply }));
|
|
15
|
+
while (pending.length > 0) {
|
|
16
|
+
const item = pending.pop();
|
|
17
|
+
if (item === void 0 || visited.has(item.reply.id)) continue;
|
|
18
|
+
const { parentId, reply } = item;
|
|
19
|
+
visited.add(reply.id);
|
|
20
|
+
const parent = comments.byId.get(parentId);
|
|
21
|
+
const paraId = reply.paraId ?? commentParaId(`comment-${reply.id}`);
|
|
22
|
+
const parentParaId = reply.parentParaId ?? parent?.paraId ?? commentParaId(`comment-${parentId}`);
|
|
23
|
+
replies.push({
|
|
24
|
+
id: reply.id,
|
|
25
|
+
author: reply.author,
|
|
26
|
+
initials: reply.initials,
|
|
27
|
+
date: reply.date,
|
|
28
|
+
text: reply.text,
|
|
29
|
+
commentXml: reply.xml,
|
|
30
|
+
imported: true,
|
|
31
|
+
paraId,
|
|
32
|
+
parentParaId,
|
|
33
|
+
extensionXml: reply.extensionXml
|
|
34
|
+
});
|
|
35
|
+
const children = comments.repliesByParentId.get(reply.id) ?? [];
|
|
36
|
+
for (let index = children.length - 1; index >= 0; index -= 1) {
|
|
37
|
+
pending.push({ parentId: reply.id, reply: children[index] });
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return replies;
|
|
41
|
+
}
|
|
42
|
+
function nullableString(value) {
|
|
43
|
+
return typeof value === "string" ? value : null;
|
|
44
|
+
}
|
|
45
|
+
function replyData(value) {
|
|
46
|
+
if (!Array.isArray(value)) return [];
|
|
47
|
+
return value.flatMap((candidate) => {
|
|
48
|
+
if (typeof candidate !== "object" || candidate === null) return [];
|
|
49
|
+
const entry = candidate;
|
|
50
|
+
const id = nullableString(entry.id);
|
|
51
|
+
const paraId = nullableString(entry.paraId);
|
|
52
|
+
const parentParaId = nullableString(entry.parentParaId);
|
|
53
|
+
if (id === null || paraId === null || parentParaId === null) return [];
|
|
54
|
+
return [
|
|
55
|
+
{
|
|
56
|
+
id,
|
|
57
|
+
author: nullableString(entry.author),
|
|
58
|
+
initials: nullableString(entry.initials),
|
|
59
|
+
date: nullableString(entry.date),
|
|
60
|
+
text: nullableString(entry.text) ?? "",
|
|
61
|
+
commentXml: nullableString(entry.commentXml),
|
|
62
|
+
imported: entry.imported === true,
|
|
63
|
+
paraId,
|
|
64
|
+
parentParaId,
|
|
65
|
+
extensionXml: nullableString(entry.extensionXml)
|
|
66
|
+
}
|
|
67
|
+
];
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
function referenceData(node) {
|
|
71
|
+
if (node.type.name !== "commentReference") return null;
|
|
72
|
+
const id = nullableString(node.attrs.id);
|
|
73
|
+
if (id === null) return null;
|
|
74
|
+
return {
|
|
75
|
+
id,
|
|
76
|
+
author: nullableString(node.attrs.author),
|
|
77
|
+
initials: nullableString(node.attrs.initials),
|
|
78
|
+
date: nullableString(node.attrs.date),
|
|
79
|
+
text: nullableString(node.attrs.text) ?? "",
|
|
80
|
+
commentXml: nullableString(node.attrs.commentXml),
|
|
81
|
+
imported: node.attrs.imported === true,
|
|
82
|
+
paraId: nullableString(node.attrs.paraId) ?? "00000001",
|
|
83
|
+
resolved: node.attrs.resolved === true,
|
|
84
|
+
extensionXml: nullableString(node.attrs.extensionXml),
|
|
85
|
+
threadImported: node.attrs.threadImported === true,
|
|
86
|
+
replies: replyData(node.attrs.replies)
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function commentReferencesIn(doc) {
|
|
90
|
+
const references = /* @__PURE__ */ new Map();
|
|
91
|
+
doc.descendants((node) => {
|
|
92
|
+
const comment = referenceData(node);
|
|
93
|
+
if (comment && !references.has(comment.id)) {
|
|
94
|
+
references.set(comment.id, comment);
|
|
95
|
+
}
|
|
96
|
+
return true;
|
|
97
|
+
});
|
|
98
|
+
return references;
|
|
99
|
+
}
|
|
100
|
+
export {
|
|
101
|
+
commentParaId,
|
|
102
|
+
commentReferencesIn,
|
|
103
|
+
importedCommentReplies
|
|
104
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads comment and comment-extension package parts.
|
|
3
|
+
*/
|
|
4
|
+
export interface ImportedComment {
|
|
5
|
+
id: string;
|
|
6
|
+
author: string | null;
|
|
7
|
+
initials: string | null;
|
|
8
|
+
date: string | null;
|
|
9
|
+
text: string;
|
|
10
|
+
xml: string;
|
|
11
|
+
paraId: string | null;
|
|
12
|
+
parentParaId: string | null;
|
|
13
|
+
resolved: boolean;
|
|
14
|
+
extensionXml: string | null;
|
|
15
|
+
}
|
|
16
|
+
export interface ImportedComments {
|
|
17
|
+
partPath: string | null;
|
|
18
|
+
xml: string | null;
|
|
19
|
+
hadBom: boolean;
|
|
20
|
+
ordered: readonly ImportedComment[];
|
|
21
|
+
byId: ReadonlyMap<string, ImportedComment>;
|
|
22
|
+
repliesByParentId: ReadonlyMap<string, readonly ImportedComment[]>;
|
|
23
|
+
extendedPartPath: string | null;
|
|
24
|
+
extendedXml: string | null;
|
|
25
|
+
extendedHadBom: boolean;
|
|
26
|
+
extendedOrdered: readonly ImportedCommentExtension[];
|
|
27
|
+
}
|
|
28
|
+
export declare const NO_COMMENTS: ImportedComments;
|
|
29
|
+
export interface ImportedCommentExtension {
|
|
30
|
+
paraId: string;
|
|
31
|
+
parentParaId: string | null;
|
|
32
|
+
resolved: boolean;
|
|
33
|
+
xml: string;
|
|
34
|
+
}
|
|
35
|
+
/** Reads the Comments part related from the main document story. */
|
|
36
|
+
export declare function readComments(parts: Map<string, Uint8Array>, mainPartPath: string): ImportedComments;
|