@portone/docx-editor 0.3.0 → 0.5.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.
Files changed (217) hide show
  1. package/CHANGELOG.md +205 -0
  2. package/assets/editor.png +0 -0
  3. package/dist/DocxEditor.d.ts +1 -1
  4. package/dist/DocxEditor.js +45 -25
  5. package/dist/core.d.ts +1 -1
  6. package/dist/docx/cloning.js +4 -9
  7. package/dist/docx/commentOnlyChange.d.ts +5 -3
  8. package/dist/docx/comments/grammar.d.ts +27 -10
  9. package/dist/docx/comments/grammar.js +18 -67
  10. package/dist/docx/comments/model.d.ts +5 -6
  11. package/dist/docx/comments/model.js +0 -9
  12. package/dist/docx/comments/parts.d.ts +16 -10
  13. package/dist/docx/comments/parts.js +65 -13
  14. package/dist/docx/comments/people.d.ts +11 -3
  15. package/dist/docx/comments/people.js +20 -6
  16. package/dist/docx/comments/policy.js +7 -1
  17. package/dist/docx/comments/reading.d.ts +4 -2
  18. package/dist/docx/comments/reading.js +1 -14
  19. package/dist/docx/comments/writing.js +78 -35
  20. package/dist/docx/exportDocx.js +30 -18
  21. package/dist/docx/exportRefs.d.ts +8 -3
  22. package/dist/docx/exportRefs.js +2 -1
  23. package/dist/docx/fidelity.js +24 -10
  24. package/dist/docx/fields.d.ts +42 -0
  25. package/dist/docx/fields.js +76 -0
  26. package/dist/docx/formatting/attrs.d.ts +20 -2
  27. package/dist/docx/formatting/attrs.js +57 -2
  28. package/dist/docx/formatting/context.d.ts +10 -2
  29. package/dist/docx/formatting/context.js +23 -5
  30. package/dist/docx/formatting/resolve.d.ts +10 -4
  31. package/dist/docx/formatting/resolve.js +31 -11
  32. package/dist/docx/formatting/styles.d.ts +25 -2
  33. package/dist/docx/formatting/styles.js +84 -3
  34. package/dist/docx/headersFooters.d.ts +89 -20
  35. package/dist/docx/headersFooters.js +163 -172
  36. package/dist/docx/hyperlink.d.ts +14 -4
  37. package/dist/docx/hyperlink.js +4 -1
  38. package/dist/docx/importDocx.d.ts +1 -1
  39. package/dist/docx/importDocx.js +135 -74
  40. package/dist/docx/importParagraph.d.ts +16 -4
  41. package/dist/docx/importParagraph.js +87 -93
  42. package/dist/docx/importPolicy.d.ts +70 -0
  43. package/dist/docx/importPolicy.js +190 -0
  44. package/dist/docx/importPreserved.d.ts +30 -0
  45. package/dist/docx/importPreserved.js +54 -0
  46. package/dist/docx/importTable.d.ts +2 -2
  47. package/dist/docx/importTable.js +107 -66
  48. package/dist/docx/invariants.js +42 -31
  49. package/dist/docx/newLists.d.ts +30 -0
  50. package/dist/docx/newLists.js +24 -1
  51. package/dist/docx/notes.d.ts +6 -1
  52. package/dist/docx/notes.js +6 -16
  53. package/dist/docx/numberingPlanner.d.ts +6 -3
  54. package/dist/docx/numberingPlanner.js +38 -7
  55. package/dist/docx/pageGeometry.d.ts +5 -14
  56. package/dist/docx/pageGeometry.js +0 -12
  57. package/dist/docx/protectionPolicy.d.ts +13 -5
  58. package/dist/docx/protectionPolicy.js +32 -22
  59. package/dist/docx/scan.d.ts +18 -8
  60. package/dist/docx/scan.js +17 -11
  61. package/dist/docx/sections.d.ts +126 -0
  62. package/dist/docx/sections.js +207 -0
  63. package/dist/docx/serializeBlock.d.ts +13 -4
  64. package/dist/docx/serializeBlock.js +16 -18
  65. package/dist/docx/serializeParagraph.d.ts +4 -0
  66. package/dist/docx/serializeParagraph.js +1 -0
  67. package/dist/docx/serializePreserved.d.ts +14 -0
  68. package/dist/docx/serializePreserved.js +24 -0
  69. package/dist/docx/serializeStory.d.ts +17 -0
  70. package/dist/docx/serializeStory.js +17 -0
  71. package/dist/docx/serializeTable.js +10 -13
  72. package/dist/docx/session.d.ts +24 -7
  73. package/dist/docx/session.js +37 -6
  74. package/dist/docx/story.d.ts +140 -0
  75. package/dist/docx/story.js +237 -0
  76. package/dist/docx/storyProjection.js +6 -1
  77. package/dist/docx/tableFormatting/conditions.d.ts +76 -0
  78. package/dist/docx/tableFormatting/conditions.js +196 -0
  79. package/dist/docx/tableFormatting/editing.d.ts +7 -3
  80. package/dist/docx/tableFormatting/editing.js +4 -5
  81. package/dist/docx/tableFormatting/reading.d.ts +49 -19
  82. package/dist/docx/tableFormatting/reading.js +109 -35
  83. package/dist/docx/tableFormatting.d.ts +1 -0
  84. package/dist/docx/tableFormatting.js +1 -0
  85. package/dist/docx/tableTemplate.js +12 -12
  86. package/dist/editor/clipboard/htmlReader.d.ts +30 -0
  87. package/dist/editor/clipboard/htmlReader.js +302 -0
  88. package/dist/editor/clipboard/internalChannel.d.ts +57 -0
  89. package/dist/editor/clipboard/internalChannel.js +58 -0
  90. package/dist/editor/clipboard/normalizers.d.ts +99 -0
  91. package/dist/editor/clipboard/normalizers.js +199 -0
  92. package/dist/editor/clipboard/parser.d.ts +34 -0
  93. package/dist/editor/clipboard/parser.js +58 -0
  94. package/dist/editor/clipboard/plugin.d.ts +21 -0
  95. package/dist/editor/clipboard/plugin.js +320 -0
  96. package/dist/editor/clipboard/readContext.d.ts +24 -0
  97. package/dist/editor/clipboard/readContext.js +19 -0
  98. package/dist/editor/clipboard/readers.d.ts +33 -0
  99. package/dist/editor/clipboard/readers.js +37 -0
  100. package/dist/editor/commands/comments/editing.d.ts +26 -6
  101. package/dist/editor/commands/comments/editing.js +175 -105
  102. package/dist/editor/commands/comments/model.d.ts +34 -17
  103. package/dist/editor/commands/comments/model.js +11 -0
  104. package/dist/editor/commands/comments/reading.d.ts +5 -3
  105. package/dist/editor/commands/comments/reading.js +8 -58
  106. package/dist/editor/commands/formatting/propertyCommands.js +6 -1
  107. package/dist/editor/commands/index.d.ts +2 -2
  108. package/dist/editor/commands/index.js +2 -0
  109. package/dist/editor/commands/listCommands.d.ts +12 -3
  110. package/dist/editor/commands/listCommands.js +76 -30
  111. package/dist/editor/commands/noteQueries.d.ts +22 -5
  112. package/dist/editor/commands/noteQueries.js +34 -6
  113. package/dist/editor/commands/paragraphCommands.js +6 -1
  114. package/dist/editor/createEditor.js +14 -4
  115. package/dist/editor/documentStyles.d.ts +26 -4
  116. package/dist/editor/documentStyles.js +9 -4
  117. package/dist/editor/editorDocument.d.ts +7 -6
  118. package/dist/editor/editorDocument.js +18 -6
  119. package/dist/editor/imageFiles.d.ts +2 -2
  120. package/dist/editor/imageFiles.js +2 -0
  121. package/dist/editor/insertTable.d.ts +1 -1
  122. package/dist/editor/insertTable.js +2 -2
  123. package/dist/editor/paragraphEdits.d.ts +16 -1
  124. package/dist/editor/paragraphEdits.js +13 -5
  125. package/dist/editor/paragraphPlacement.d.ts +11 -0
  126. package/dist/editor/paragraphPlacement.js +18 -0
  127. package/dist/editor/plainText.d.ts +12 -0
  128. package/dist/editor/plainText.js +2 -1
  129. package/dist/editor/plugins/columnResize.js +6 -3
  130. package/dist/editor/plugins/commentComposer.d.ts +28 -0
  131. package/dist/editor/plugins/commentComposer.js +46 -0
  132. package/dist/editor/plugins/commentDecorations.d.ts +29 -2
  133. package/dist/editor/plugins/commentDecorations.js +89 -15
  134. package/dist/editor/plugins/documentProjection.d.ts +31 -0
  135. package/dist/editor/plugins/documentProjection.js +22 -0
  136. package/dist/editor/plugins/imagePaste.js +20 -10
  137. package/dist/editor/plugins/linkPanel.d.ts +7 -3
  138. package/dist/editor/plugins/linkPanel.js +13 -27
  139. package/dist/editor/plugins/numberingDecorations.d.ts +10 -4
  140. package/dist/editor/plugins/numberingDecorations.js +43 -2
  141. package/dist/editor/plugins/panelState.d.ts +67 -0
  142. package/dist/editor/plugins/panelState.js +54 -0
  143. package/dist/editor/plugins/paragraphDisplay.js +9 -3
  144. package/dist/editor/plugins/tableContextMenu.d.ts +1 -1
  145. package/dist/editor/plugins/tableContextMenu.js +30 -47
  146. package/dist/editor/plugins/tableDisplay.js +19 -5
  147. package/dist/editor/plugins/textContextMenu.d.ts +1 -1
  148. package/dist/editor/plugins/textContextMenu.js +30 -47
  149. package/dist/model/format.d.ts +50 -0
  150. package/dist/model/format.js +65 -0
  151. package/dist/numbering/listRegistry.d.ts +37 -0
  152. package/dist/numbering/listRegistry.js +112 -0
  153. package/dist/numbering/listTemplate.d.ts +26 -17
  154. package/dist/numbering/listTemplate.js +66 -20
  155. package/dist/numbering/markers.d.ts +15 -6
  156. package/dist/numbering/markers.js +26 -59
  157. package/dist/numbering/parseNumbering.d.ts +70 -5
  158. package/dist/numbering/parseNumbering.js +100 -26
  159. package/dist/numbering/spellers.d.ts +15 -0
  160. package/dist/numbering/spellers.js +96 -0
  161. package/dist/numbering/writeNumbering.d.ts +5 -3
  162. package/dist/numbering/writeNumbering.js +15 -15
  163. package/dist/ooxml/conformance.d.ts +28 -0
  164. package/dist/ooxml/conformance.js +20 -0
  165. package/dist/ooxml/errors.d.ts +5 -5
  166. package/dist/ooxml/fragment.d.ts +11 -0
  167. package/dist/ooxml/fragment.js +14 -4
  168. package/dist/ooxml/image.d.ts +3 -4
  169. package/dist/ooxml/image.js +50 -5
  170. package/dist/ooxml/names.d.ts +15 -0
  171. package/dist/ooxml/names.js +10 -1
  172. package/dist/ooxml/partSplice.d.ts +1 -1
  173. package/dist/ooxml/partSplice.js +10 -3
  174. package/dist/ooxml/props.d.ts +8 -0
  175. package/dist/ooxml/props.js +14 -4
  176. package/dist/ooxml/rangeMarkers.d.ts +5 -0
  177. package/dist/ooxml/rangeMarkers.js +24 -0
  178. package/dist/ooxml/xml.d.ts +8 -9
  179. package/dist/ooxml/xml.js +6 -6
  180. package/dist/page/PageGuides.d.ts +8 -4
  181. package/dist/page/PageGuides.js +13 -37
  182. package/dist/page/pageLayout.d.ts +62 -13
  183. package/dist/page/pageLayout.js +91 -37
  184. package/dist/page/usePageLayout.d.ts +14 -11
  185. package/dist/page/usePageLayout.js +21 -29
  186. package/dist/schema/attrRoles.js +44 -19
  187. package/dist/schema/docxSchema.d.ts +9 -1
  188. package/dist/schema/docxSchema.js +220 -139
  189. package/dist/schema/editGuard.d.ts +1 -1
  190. package/dist/schema/guards.js +2 -2
  191. package/dist/schema/index.d.ts +1 -0
  192. package/dist/schema/preservedFragments.d.ts +12 -0
  193. package/dist/schema/preservedFragments.js +38 -0
  194. package/dist/schema/preservedGuards.d.ts +9 -7
  195. package/dist/schema/preservedGuards.js +112 -11
  196. package/dist/schema/protection.d.ts +34 -5
  197. package/dist/schema/protection.js +42 -14
  198. package/dist/schema/rendering.js +3 -1
  199. package/dist/schema/stories.d.ts +49 -0
  200. package/dist/schema/stories.js +78 -0
  201. package/dist/styles/classNames.d.ts +16 -7
  202. package/dist/styles/classNames.js +16 -7
  203. package/dist/styles.css +90 -30
  204. package/dist/table/cellFormatting.js +1 -2
  205. package/dist/table/gridBorders.d.ts +11 -15
  206. package/dist/table/gridBorders.js +32 -18
  207. package/dist/table/resize.d.ts +5 -3
  208. package/dist/ui/CommentsPanel.d.ts +1 -3
  209. package/dist/ui/CommentsPanel.js +24 -15
  210. package/dist/ui/NotesPanel.js +2 -2
  211. package/dist/ui/TextMenu.d.ts +1 -3
  212. package/dist/ui/TextMenu.js +10 -6
  213. package/dist/ui/comments/CommentComposer.d.ts +6 -1
  214. package/dist/ui/comments/CommentComposer.js +26 -3
  215. package/package.json +1 -1
  216. package/dist/editor/externalClipboard.d.ts +0 -11
  217. package/dist/editor/externalClipboard.js +0 -456
@@ -1,456 +0,0 @@
1
- // src/editor/externalClipboard.ts
2
- import {
3
- DOMSerializer,
4
- Fragment,
5
- Slice
6
- } from "prosemirror-model";
7
- import { Plugin } from "prosemirror-state";
8
- import { paragraphAttrsFor, styleIdOf } from "../docx/formatting.js";
9
- import {
10
- withListNumbering,
11
- withParagraphStyle
12
- } from "../docx/paraProps.js";
13
- import { MAX_ILVL, nextNumId } from "../numbering/listTemplate.js";
14
- import { docxSchema, isPageBreak } from "../schema/index.js";
15
- import { editorClassNames } from "../styles/classNames.js";
16
- import { PASTED_IMAGE_ATTRIBUTE } from "./clipboard/images.js";
17
- import {
18
- appendInline,
19
- contextFor,
20
- marksFor,
21
- safeHref,
22
- withInlineStyle
23
- } from "./clipboard/inlineFormatting.js";
24
- import { listRefOf } from "./commands/listCommands.js";
25
- import { documentFormatting, documentParagraphStyles } from "./documentStyles.js";
26
- import { insertPlainText } from "./plainText.js";
27
- import { moveCaretToDrop } from "./plugins/dropCaret.js";
28
- import {
29
- canStartNewList,
30
- documentNumbering
31
- } from "./plugins/numberingDecorations.js";
32
- var BLOCK_TAGS = /* @__PURE__ */ new Set([
33
- "ADDRESS",
34
- "ARTICLE",
35
- "ASIDE",
36
- "BLOCKQUOTE",
37
- "DIV",
38
- "H1",
39
- "H2",
40
- "H3",
41
- "H4",
42
- "H5",
43
- "H6",
44
- "LI",
45
- "P",
46
- "PRE",
47
- "SECTION"
48
- ]);
49
- var HEADING_LEVELS = {
50
- H1: 1,
51
- H2: 2,
52
- H3: 3,
53
- H4: 4,
54
- H5: 5,
55
- H6: 6
56
- };
57
- var HEADING_FALLBACKS = {
58
- 1: { bold: true, fontSizePt: 24 },
59
- 2: { bold: true, fontSizePt: 18 },
60
- 3: { bold: true, fontSizePt: 14 },
61
- 4: { bold: true, fontSizePt: 12 },
62
- 5: { bold: true, fontSizePt: 10 },
63
- 6: { bold: true, fontSizePt: 8 }
64
- };
65
- var PUBLIC_ATTRIBUTES = /* @__PURE__ */ new Set([
66
- "alt",
67
- "aria-label",
68
- "class",
69
- "colspan",
70
- "height",
71
- "href",
72
- "lang",
73
- "rowspan",
74
- "src",
75
- "style",
76
- "width"
77
- ]);
78
- var COPIED_STYLE_ATTRIBUTE = "data-style";
79
- function isAttributes(value) {
80
- return typeof value === "object" && value !== null && !Array.isArray(value) && !(value instanceof Node);
81
- }
82
- function publicAttributes(attrs) {
83
- return Object.fromEntries(
84
- Object.entries(attrs).filter(([name]) => PUBLIC_ATTRIBUTES.has(name))
85
- );
86
- }
87
- function isSpecArray(value) {
88
- return Array.isArray(value) && typeof value[0] === "string";
89
- }
90
- function stripPrivateAttributes(spec) {
91
- if (!isSpecArray(spec)) return spec;
92
- const [tag, ...rest] = spec;
93
- const attrs = isAttributes(rest[0]) ? publicAttributes(rest[0]) : null;
94
- const children = (attrs === null ? rest : rest.slice(1)).map(
95
- (child) => isSpecArray(child) ? stripPrivateAttributes(child) : child
96
- );
97
- return attrs === null ? [tag, ...children] : [tag, attrs, ...children];
98
- }
99
- function withDomAttribute(spec, name, value) {
100
- if (!isSpecArray(spec)) return spec;
101
- const [tag, ...rest] = spec;
102
- return isAttributes(rest[0]) ? [tag, { ...rest[0], [name]: value }, ...rest.slice(1)] : [tag, { [name]: value }, ...rest];
103
- }
104
- function asTag(spec, tag) {
105
- return isSpecArray(spec) ? [tag, ...spec.slice(1)] : spec;
106
- }
107
- function copiedMarkSpec(mark, spec) {
108
- const stripped = stripPrivateAttributes(spec);
109
- if (mark.type !== docxSchema.marks.link) return stripped;
110
- const href = safeHref(
111
- typeof mark.attrs.href === "string" ? mark.attrs.href : null
112
- );
113
- return href === null ? stripped : asTag(withDomAttribute(stripped, "href", href), "a");
114
- }
115
- function copiedNodeSpec(node, spec) {
116
- const stripped = stripPrivateAttributes(spec);
117
- if (node.type !== docxSchema.nodes.paragraph) return stripped;
118
- const styleId = styleIdOf(node.attrs.pPr);
119
- return styleId === null ? stripped : withDomAttribute(stripped, COPIED_STYLE_ATTRIBUTE, styleId);
120
- }
121
- function clipboardSerializer() {
122
- const drawn = DOMSerializer.fromSchema(docxSchema);
123
- const nodes = Object.fromEntries(
124
- Object.entries(drawn.nodes).map(([name, toDOM]) => [
125
- name,
126
- (node) => copiedNodeSpec(node, toDOM(node))
127
- ])
128
- );
129
- const marks = Object.fromEntries(
130
- Object.entries(drawn.marks).map(([name, toDOM]) => [
131
- name,
132
- (mark, inline) => copiedMarkSpec(mark, toDOM(mark, inline))
133
- ])
134
- );
135
- return new DOMSerializer(nodes, marks);
136
- }
137
- var UNREADABLE_BLOCKS = /* @__PURE__ */ new Set([
138
- "bookmarkBlock",
139
- "docxRaw",
140
- "rawBlock"
141
- ]);
142
- function stringAttr(value) {
143
- return typeof value === "string" ? value : "";
144
- }
145
- function inlineText(node) {
146
- if (node.isText) return node.text ?? "";
147
- if (node.type === docxSchema.nodes.hardBreak) {
148
- return isPageBreak(node.attrs.brAttrs) ? "\f" : "\n";
149
- }
150
- if (node.type === docxSchema.nodes.image) return stringAttr(node.attrs.alt);
151
- if (node.type === docxSchema.nodes.noteReference) {
152
- return node.attrs.customMarkFollows === true ? "" : stringAttr(node.attrs.label);
153
- }
154
- return "";
155
- }
156
- function rowsText(table) {
157
- const rows = [];
158
- table.forEach((row) => {
159
- const cells = [];
160
- row.forEach((cell) => {
161
- cells.push(fragmentText(cell.content));
162
- });
163
- rows.push(cells.join(" "));
164
- });
165
- return rows.join("\n");
166
- }
167
- function blockText(node) {
168
- if (node.type === docxSchema.nodes.table) return rowsText(node);
169
- if (UNREADABLE_BLOCKS.has(node.type.name)) return "";
170
- return fragmentText(node.content);
171
- }
172
- function fragmentText(fragment) {
173
- const pieces = [];
174
- fragment.forEach((child) => {
175
- pieces.push(child.isInline ? inlineText(child) : blockText(child));
176
- });
177
- return pieces.join(fragment.firstChild?.isInline === true ? "" : "\n");
178
- }
179
- function bareWrappers(content, openStart, openEnd) {
180
- const wrapper = content.firstChild;
181
- if (openStart <= 1 || openEnd <= 1 || content.childCount !== 1)
182
- return content;
183
- if (wrapper === null || wrapper.childCount !== 1) return content;
184
- return Fragment.from(
185
- wrapper.type.create(
186
- null,
187
- bareWrappers(wrapper.content, openStart - 1, openEnd - 1),
188
- wrapper.marks
189
- )
190
- );
191
- }
192
- function copiedSlice(slice) {
193
- return new Slice(
194
- bareWrappers(slice.content, slice.openStart, slice.openEnd),
195
- slice.openStart,
196
- slice.openEnd
197
- );
198
- }
199
- function clipboardText(slice) {
200
- return fragmentText(slice.content);
201
- }
202
- function normalizedStyleName(value) {
203
- return value.toLowerCase().replace(/[\s_-]+/g, "");
204
- }
205
- function copiedStyleId(element) {
206
- if (!element.classList.contains(editorClassNames.paragraph)) return null;
207
- return element.getAttribute(COPIED_STYLE_ATTRIBUTE) ?? styleIdOf(element.getAttribute("data-ppr"));
208
- }
209
- function headingLevel(element, sourceStyleId) {
210
- const direct = HEADING_LEVELS[element.tagName];
211
- if (direct !== void 0) return direct;
212
- const match = /^heading([1-6])$/.exec(
213
- normalizedStyleName(sourceStyleId ?? "")
214
- );
215
- return match ? Number.parseInt(match[1] ?? "", 10) : null;
216
- }
217
- function destinationStyleId(state, sourceStyleId, level) {
218
- const styles = documentParagraphStyles(state);
219
- if (sourceStyleId && styles.some((style) => style.id === sourceStyleId)) {
220
- return sourceStyleId;
221
- }
222
- if (level === null) return null;
223
- const wanted = `heading${level}`;
224
- return styles.find(
225
- (style) => normalizedStyleName(style.id) === wanted || normalizedStyleName(style.name) === wanted
226
- )?.id ?? null;
227
- }
228
- function blockContext(state, parent, element, preserveParagraphStyle) {
229
- const editorParagraph = element.classList.contains(
230
- editorClassNames.paragraph
231
- );
232
- const sourceStyleId = copiedStyleId(element);
233
- const level = headingLevel(element, sourceStyleId);
234
- if (!preserveParagraphStyle && editorParagraph) {
235
- return {
236
- inline: contextFor(parent, element),
237
- paragraph: null
238
- };
239
- }
240
- const styleId = destinationStyleId(state, sourceStyleId, level);
241
- const paragraph = styleId === null ? null : withParagraphStyle(null, styleId);
242
- const usesDestinationStyle = editorParagraph && (sourceStyleId === null || paragraph !== null);
243
- let inline = parent;
244
- if (paragraph === null && level !== null) {
245
- inline = withInlineStyle(inline, HEADING_FALLBACKS[level] ?? {});
246
- }
247
- return {
248
- inline: contextFor(inline, element, !usesDestinationStyle),
249
- paragraph
250
- };
251
- }
252
- function listParagraphProps(list) {
253
- if (!list || list.numId === null) return null;
254
- return withListNumbering(null, {
255
- numbering: { numId: list.numId, ilvl: Math.min(MAX_ILVL, list.level) },
256
- indent: { kind: "keep" }
257
- });
258
- }
259
- function paragraphAttrs(state, list, paragraph) {
260
- const props = listParagraphProps(list) ?? paragraph;
261
- return props ? {
262
- pPr: props.pPr,
263
- ...paragraphAttrsFor(props.pPr, documentFormatting(state))
264
- } : null;
265
- }
266
- function usedNumIds(state) {
267
- const used = new Set(documentNumbering(state).lists.keys());
268
- state.doc.descendants((node) => {
269
- if (node.type !== docxSchema.nodes.paragraph) return true;
270
- const ref = listRefOf(node);
271
- if (ref) used.add(ref.numId);
272
- return false;
273
- });
274
- return used;
275
- }
276
- var HtmlReader = class {
277
- constructor(state, preserveParagraphStyles, images) {
278
- this.state = state;
279
- this.preserveParagraphStyles = preserveParagraphStyles;
280
- this.images = images;
281
- this.used = usedNumIds(state);
282
- this.canCreateLists = canStartNewList(state);
283
- }
284
- state;
285
- preserveParagraphStyles;
286
- images;
287
- blocks = [];
288
- used;
289
- canCreateLists;
290
- read(root) {
291
- this.readFlow(root, { style: {}, href: null, preserveWhitespace: false });
292
- return this.blocks;
293
- }
294
- appendInline(target, node, context) {
295
- appendInline(target, node, context, (content, element, inline) => {
296
- if (element.tagName !== "IMG") return false;
297
- const token = element.getAttribute(PASTED_IMAGE_ATTRIBUTE);
298
- const image = token === null ? void 0 : this.images.get(token);
299
- if (!image) return true;
300
- content.push(
301
- docxSchema.nodes.image.create(
302
- {
303
- ...image,
304
- // The source drawing may point at a relationship in another package.
305
- xml: null
306
- },
307
- null,
308
- marksFor(inline)
309
- )
310
- );
311
- return true;
312
- });
313
- }
314
- addParagraph(content, list = null, paragraph = null) {
315
- if (list && list.numId === null) {
316
- const marker = list.kind === "bullet" ? "\u2022 " : "1. ";
317
- content.unshift(docxSchema.text(marker));
318
- }
319
- this.blocks.push(
320
- docxSchema.nodes.paragraph.create(
321
- paragraphAttrs(this.state, list, paragraph),
322
- content
323
- )
324
- );
325
- }
326
- readFlow(parent, context, paragraph = null) {
327
- let inline = [];
328
- const flush = () => {
329
- if (inline.length === 0) return;
330
- this.addParagraph(inline, null, paragraph);
331
- inline = [];
332
- };
333
- for (const child of parent.childNodes) {
334
- const element = child.nodeType === child.ELEMENT_NODE ? child : null;
335
- if (element?.tagName === "UL" || element?.tagName === "OL") {
336
- flush();
337
- this.readList(element, context, 0, null);
338
- } else if (element && BLOCK_TAGS.has(element.tagName)) {
339
- flush();
340
- const before = this.blocks.length;
341
- const block = blockContext(
342
- this.state,
343
- context,
344
- element,
345
- this.preserveParagraphStyles
346
- );
347
- this.readFlow(element, block.inline, block.paragraph);
348
- if (this.blocks.length === before) {
349
- this.addParagraph([], null, block.paragraph);
350
- }
351
- } else {
352
- this.appendInline(inline, child, context);
353
- }
354
- }
355
- flush();
356
- }
357
- takeNumId(kind) {
358
- if (!this.canCreateLists) return null;
359
- const id = nextNumId(this.used, kind);
360
- this.used.add(id);
361
- return id;
362
- }
363
- readList(list, context, level, inherited) {
364
- const kind = list.tagName === "UL" ? "bullet" : "numbered";
365
- const listContext = {
366
- kind,
367
- numId: inherited?.kind === kind ? inherited.numId : this.takeNumId(kind),
368
- level
369
- };
370
- const items = Array.from(list.children).filter(
371
- (child) => child.tagName === "LI"
372
- );
373
- for (const item of items) {
374
- const itemContext = contextFor(context, item);
375
- const content = [];
376
- for (const child of item.childNodes) {
377
- const nested = child.nodeType === child.ELEMENT_NODE ? child : null;
378
- if (nested?.tagName === "UL" || nested?.tagName === "OL") continue;
379
- this.appendInline(content, child, itemContext);
380
- }
381
- this.addParagraph(content, listContext);
382
- for (const nested of Array.from(item.children)) {
383
- if (nested.tagName === "UL" || nested.tagName === "OL") {
384
- this.readList(
385
- nested,
386
- itemContext,
387
- level + 1,
388
- listContext
389
- );
390
- }
391
- }
392
- }
393
- }
394
- };
395
- function sliceDepth(root) {
396
- const marker = root.querySelector("[data-pm-slice]");
397
- const openStart = Number.parseInt(
398
- marker?.getAttribute("data-pm-slice")?.split(/\s+/, 1)[0] ?? "",
399
- 10
400
- );
401
- if (Number.isFinite(openStart)) return openStart === 0 ? 0 : 1;
402
- const blocks = [...BLOCK_TAGS, "OL", "UL"].map((tag) => tag.toLowerCase());
403
- return root.querySelector(blocks.join(",")) ? 0 : 1;
404
- }
405
- function richHtmlSlice(state, document, source, images = /* @__PURE__ */ new Map()) {
406
- if (source.trim() === "") return null;
407
- const template = document.createElement("template");
408
- template.innerHTML = source;
409
- const open = sliceDepth(template.content);
410
- const blocks = new HtmlReader(state, open === 0, images).read(
411
- template.content
412
- );
413
- return blocks.length === 0 ? null : new Slice(Fragment.fromArray([...blocks]), open, open);
414
- }
415
- function insertRichHtml(view, source) {
416
- const slice = richHtmlSlice(view.state, view.dom.ownerDocument, source);
417
- if (slice === null) return false;
418
- view.dispatch(view.state.tr.replaceSelection(slice).scrollIntoView());
419
- return true;
420
- }
421
- function insertClipboardData(view, data) {
422
- if (insertRichHtml(view, data.html ?? "")) return;
423
- insertPlainText(view, data.text ?? "");
424
- }
425
- function externalClipboard() {
426
- const serializer = clipboardSerializer();
427
- return new Plugin({
428
- props: {
429
- clipboardSerializer: serializer,
430
- clipboardTextSerializer: clipboardText,
431
- transformCopied: copiedSlice,
432
- handlePaste(view, event) {
433
- insertClipboardData(view, {
434
- html: event.clipboardData?.getData("text/html"),
435
- text: event.clipboardData?.getData("text/plain")
436
- });
437
- return true;
438
- },
439
- handleDrop(view, event) {
440
- if (view.dragging) return false;
441
- const text = event.dataTransfer?.getData("text/plain") ?? "";
442
- if (text) {
443
- moveCaretToDrop(view, event);
444
- insertPlainText(view, text);
445
- }
446
- return true;
447
- }
448
- }
449
- });
450
- }
451
- export {
452
- externalClipboard,
453
- insertClipboardData,
454
- insertRichHtml,
455
- richHtmlSlice
456
- };