@a3s-lab/office 0.10.0 → 0.12.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 (47) hide show
  1. package/COLLABORATION_ROADMAP.md +22 -12
  2. package/README.md +40 -15
  3. package/dist/{0~4705.js → 0~5093.js} +959 -10
  4. package/dist/0~document-editor.js +1 -0
  5. package/dist/0~spreadsheet-editor.js +189 -104
  6. package/dist/0~work-docx-export.js +225 -6
  7. package/dist/0~work-docx-import.js +26 -755
  8. package/dist/0~work-office-diagnostics.js +12 -8
  9. package/dist/0~work-pptx-import.js +2 -2
  10. package/dist/4104.js +330 -122
  11. package/dist/4476.js +82 -65
  12. package/dist/8715.js +41 -3
  13. package/dist/8928.js +448 -137
  14. package/dist/9356.js +126 -8
  15. package/dist/core.d.ts +1 -0
  16. package/dist/internal/features/work/editors/spreadsheet-editor-support.d.ts +1 -0
  17. package/dist/internal/features/work/editors/use-spreadsheet-collaboration.d.ts +2 -1
  18. package/dist/internal/features/work/spreadsheet-sparse.d.ts +5 -0
  19. package/dist/internal/features/work/work-document-file-io.d.ts +2 -1
  20. package/dist/internal/features/work/work-document-format-change-tracking.d.ts +1 -1
  21. package/dist/internal/features/work/work-document-paragraph-format-change-tracking.d.ts +7 -0
  22. package/dist/internal/features/work/work-document-paragraph-format-changes.d.ts +8 -0
  23. package/dist/internal/features/work/work-docx-import.d.ts +5 -2
  24. package/dist/internal/features/work/work-docx-paragraph-alignment-import.d.ts +1 -0
  25. package/dist/internal/features/work/work-docx-paragraph-borders-import.d.ts +1 -0
  26. package/dist/internal/features/work/work-docx-paragraph-direction-import.d.ts +1 -0
  27. package/dist/internal/features/work/work-docx-paragraph-format-change-export.d.ts +13 -0
  28. package/dist/internal/features/work/work-docx-paragraph-format-change-import.d.ts +17 -0
  29. package/dist/internal/features/work/work-docx-paragraph-indent-import.d.ts +1 -0
  30. package/dist/internal/features/work/work-docx-paragraph-pagination-import.d.ts +1 -0
  31. package/dist/internal/features/work/work-docx-paragraph-shading-import.d.ts +1 -0
  32. package/dist/internal/features/work/work-docx-paragraph-spacing-import.d.ts +1 -0
  33. package/dist/internal/features/work/work-docx-tab-stop-import.d.ts +1 -0
  34. package/dist/internal/features/work/work-file-data.d.ts +7 -1
  35. package/dist/internal/features/work/work-file-import.d.ts +28 -0
  36. package/dist/internal/features/work/work-file-io.d.ts +2 -1
  37. package/dist/internal/features/work/work-markdown-file-io.d.ts +2 -1
  38. package/dist/internal/features/work/work-office-diagnostics.d.ts +3 -2
  39. package/dist/internal/features/work/work-pptx-import.d.ts +1 -1
  40. package/dist/internal/features/work/work-presentation-file-io.d.ts +2 -1
  41. package/dist/internal/features/work/work-spreadsheet-protection.d.ts +2 -0
  42. package/dist/internal/features/work/work-types.d.ts +23 -1
  43. package/dist/internal/features/work/work-xlsx-interop.d.ts +4 -14
  44. package/dist/office-kernel.wasm +0 -0
  45. package/dist/styles.css +8 -0
  46. package/docs/latest/en/browser-editor-architecture.md +18 -0
  47. package/package.json +10 -4
@@ -1,7 +1,7 @@
1
1
  import jszip from "jszip";
2
2
  import { contentTypeForPart, directChildren, bytesToDataUrl, OoxmlPackage, firstDescendant, descendants, directChild, attribute } from "./5184.js";
3
- import { normalizeDocumentBookmarkName, renderDocumentTableCellMarginOverrides, applyDocumentImageIdentityToElement, normalizeDocumentTabStops, createDocumentImageIdentityRegistry, normalizeDocumentColumns, normalizeDocumentImageWrapContour, DOCUMENT_TABLE_ROW_ID_ATTRIBUTE, normalizeDocumentBookmarksHtml, applyDocumentTableRowIdentityToElement, normalizeDocumentCaptionsHtml, normalizeDocumentImageAlignment, normalizeDocumentParagraphId, applyDocumentImageLayerToElement, normalizeDocumentNotesHtml, normalizeDocumentBookmarkReferencesHtml, documentCitationTagsFromInstruction, applyDocumentImageWrapContourToElement, renderDocumentAutoLineHeight, applyDocumentPageGeometry, normalizeDocumentPageChrome, normalizeDocumentImageLayoutOptions, applyDocumentImageCropToElement, documentPageMarginsForLayout, createDocumentNoteElement, serializeDocumentTabStops, normalizeDocumentImageLayer, DOCUMENT_TABLE_ROW_TEXT_ID_ATTRIBUTE, documentBookmarkReferenceInstruction, documentInitialSectionLayout, normalizeDocumentFieldsHtml, documentCaptionLabel, documentTableBordersFromElement, isContourImageLayout, normalizeDocumentCitationsHtml, normalizeDocumentImageIdentity, normalizeDocumentImageWrapSide, applyDocumentParagraphIdentityToElement, documentAutoLineHeight, docxDocumentFieldKind, serializeDocxThemeReference, renderDocumentTableBorders, docxBookmarkReferenceTarget, normalizeDocumentImagePosition, wrapsBesideImage, documentPageChromeLegacyFields, documentParagraphShadingDomAttributes, DEFAULT_DOCUMENT_TABLE_GEOMETRY, documentParagraphBordersDomAttributes, documentSectionDomAttributes, DEFAULT_DOCUMENT_TABLE_CELL_MARGINS, normalizeDocumentParagraphIdentity, documentPageMarginBody, normalizeDocumentParagraphIndent, normalizeDocumentTableRowIdentity, normalizeDocumentBookmarkNativeId, uniqueDocumentImageIdentity, applyDocumentTableGeometryToElement, normalizeDocumentImageCrop, documentContentLayoutProperties } from "./8928.js";
4
- import { parseDocxPageGeometry, parseDocxPageBorders, docxCaptionReferenceTarget, applyImportedDocxEquationMarkers, createDocxTableStyleResolver, hasImportedDocxParagraphShadingMarkers, isDocxEquationLikeRoot, inspectDocxPageMarginSettings, hasImportedDocxRunFormattingMarkers, docxParagraphPropertySources, docxCaptionSequenceKind, resolveDocxParagraphBordersForParagraph, applyImportedDocxRunFormattingMarkers, applyImportedDocxParagraphShadingMarkers, hasImportedDocxParagraphBorderMarkers, docxCaptionBookmark, docxFieldResultText, resolveDocxParagraphStyleResolver, createDocxParagraphStyleResolver, docxEquationHtml, docxFieldOccurrences, docxTableParagraphPropertySources, resolveDocxTableStyleResolver, docxTableCellStyleLayers, markDocxParagraphBorders, markDocxRunFormatting, docxFieldOccurrenceIsInlineEditable, applyImportedDocxParagraphBorderMarkers, parseDocxPaperSource, docxTablePropertySources, parseDirectDocxParagraphShading, parseDocxPageMargins, markDocxParagraphShading, markDocxPackageEquations } from "./0~4705.js";
3
+ import { normalizeDocumentBookmarkName, documentInitialSectionLayout, normalizeDocumentFieldsHtml, applyDocumentImageIdentityToElement, renderDocumentTableCellMarginOverrides, documentCaptionLabel, documentTableBordersFromElement, isContourImageLayout, normalizeDocumentCitationsHtml, createDocumentImageIdentityRegistry, normalizeDocumentColumns, normalizeDocumentImageIdentity, normalizeDocumentImageWrapSide, applyDocumentParagraphIdentityToElement, docxDocumentFieldKind, serializeDocxThemeReference, renderDocumentTableBorders, docxBookmarkReferenceTarget, normalizeDocumentImagePosition, wrapsBesideImage, documentPageChromeLegacyFields, normalizeDocumentImageWrapContour, documentParagraphShadingDomAttributes, normalizeDocumentBookmarksHtml, documentParagraphBordersDomAttributes, DOCUMENT_TABLE_ROW_ID_ATTRIBUTE, applyDocumentTableRowIdentityToElement, normalizeDocumentCaptionsHtml, normalizeDocumentParagraphIdentity, DEFAULT_DOCUMENT_TABLE_CELL_MARGINS, DEFAULT_DOCUMENT_TABLE_GEOMETRY, documentSectionDomAttributes, documentPageMarginBody, normalizeDocumentImageAlignment, normalizeDocumentParagraphId, applyDocumentImageLayerToElement, normalizeDocumentBookmarkNativeId, normalizeDocumentTableRowIdentity, uniqueDocumentImageIdentity, normalizeDocumentBookmarkReferencesHtml, documentCitationTagsFromInstruction, applyDocumentImageWrapContourToElement, normalizeDocumentNotesHtml, applyDocumentTableGeometryToElement, normalizeDocumentImageCrop, normalizeDocumentPageChrome, normalizeDocumentImageLayoutOptions, applyDocumentImageCropToElement, documentPageMarginsForLayout, applyDocumentPageGeometry, documentContentLayoutProperties, createDocumentNoteElement, normalizeDocumentImageLayer, DOCUMENT_TABLE_ROW_TEXT_ID_ATTRIBUTE, documentBookmarkReferenceInstruction } from "./8928.js";
4
+ import { markDocxParagraphFormattingChanges, markDocxParagraphIndents, parseDocxPageGeometry, parseDocxPageBorders, docxCaptionReferenceTarget, applyImportedDocxEquationMarkers, hasImportedDocxParagraphPaginationMarkers, createDocxTableStyleResolver, applyImportedDocxParagraphSpacingMarkers, hasImportedDocxParagraphShadingMarkers, hasImportedDocxParagraphDirectionMarkers, isDocxEquationLikeRoot, inspectDocxPageMarginSettings, applyImportedDocxParagraphFormattingChangeMarkers, hasImportedDocxParagraphTabStopMarkers, hasImportedDocxRunFormattingMarkers, hasImportedDocxParagraphIndentMarkers, docxCaptionSequenceKind, resolveDocxParagraphBordersForParagraph, applyImportedDocxRunFormattingMarkers, markDocxParagraphAlignments, markDocxParagraphDirections, applyImportedDocxParagraphShadingMarkers, markDocxParagraphSpacing, hasImportedDocxParagraphBorderMarkers, hasImportedDocxParagraphFormattingChangeMarkers, docxCaptionBookmark, docxFieldResultText, createDocxParagraphStyleResolver, docxEquationHtml, docxFieldOccurrences, hasImportedDocxParagraphAlignmentMarkers, markDocxParagraphTabStops, applyImportedDocxParagraphIndentMarkers, resolveDocxTableStyleResolver, applyImportedDocxParagraphDirectionMarkers, docxTableCellStyleLayers, applyImportedDocxParagraphAlignmentMarkers, markDocxParagraphPagination, markDocxParagraphBorders, markDocxRunFormatting, applyImportedDocxParagraphTabStopMarkers, docxFieldOccurrenceIsInlineEditable, applyImportedDocxParagraphBorderMarkers, applyImportedDocxParagraphPaginationMarkers, parseDocxPaperSource, hasImportedDocxParagraphSpacingMarkers, docxTablePropertySources, parseDirectDocxParagraphShading, parseDocxPageMargins, markDocxParagraphShading, markDocxPackageEquations } from "./0~5093.js";
5
5
  import { xmlAttributeLocalName, readDocxBibliography, createDocxThemeResolver, docxThemeColor, parseDocxParagraphDefaultCollapsed, resolveDocxThemeResolver, DOCX_WORDPROCESSING_NAMESPACES, xmlAttributeNamespace } from "./0~2805.js";
6
6
  import { importDocxPageColor, importedDocumentNoteId } from "./0~4980.js";
7
7
  const WORD_NAMESPACE = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
@@ -1881,216 +1881,6 @@ function updateFieldState(field, fieldType) {
1881
1881
  function fieldContentVisible(field) {
1882
1882
  return !field.active || field.separated && !field.skipResult;
1883
1883
  }
1884
- const work_docx_paragraph_alignment_import_WORD_NAMESPACE = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
1885
- const work_docx_paragraph_alignment_import_XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace';
1886
- const PARAGRAPH_ALIGNMENT_MARKER_PATTERN = /__A3S_WORK_PARAGRAPH_ALIGNMENT_\d+__/g;
1887
- function markDocxParagraphAlignments(document, styleSource, tableStyleSource) {
1888
- const paragraphs = [];
1889
- const styles = resolveDocxParagraphStyleResolver(styleSource);
1890
- const tableStyles = resolveDocxTableStyleResolver(tableStyleSource);
1891
- for (const paragraph of descendants(document, 'p')){
1892
- let properties = directChild(paragraph, 'pPr');
1893
- const sources = docxParagraphPropertySources(properties, styles, docxTableParagraphPropertySources(paragraph, tableStyles));
1894
- const alignment = paragraphAlignment(sources);
1895
- if (!alignment) continue;
1896
- properties ??= insertParagraphProperties(document, paragraph);
1897
- const marker = `__A3S_WORK_PARAGRAPH_ALIGNMENT_${paragraphs.length + 1}__`;
1898
- work_docx_paragraph_alignment_import_insertParagraphMarker(document, paragraph, properties, marker);
1899
- paragraphs.push({
1900
- marker,
1901
- alignment
1902
- });
1903
- }
1904
- return {
1905
- paragraphs
1906
- };
1907
- }
1908
- function applyImportedDocxParagraphAlignmentMarkers(document, markers) {
1909
- const alignmentByMarker = new Map(markers.paragraphs.map(({ marker, alignment })=>[
1910
- marker,
1911
- alignment
1912
- ]));
1913
- for (const node of work_docx_paragraph_alignment_import_textNodes(document.body))if (node.data.includes('__A3S_WORK_PARAGRAPH_ALIGNMENT_')) node.data = node.data.replace(PARAGRAPH_ALIGNMENT_MARKER_PATTERN, (marker)=>{
1914
- const alignment = alignmentByMarker.get(marker);
1915
- const block = alignment ? closestParagraphBlock(node.parentElement, node) : null;
1916
- if (block && alignment) block.style.textAlign = alignment;
1917
- return '';
1918
- });
1919
- document.body.normalize();
1920
- }
1921
- function hasImportedDocxParagraphAlignmentMarkers(markers) {
1922
- return markers.paragraphs.length > 0;
1923
- }
1924
- function paragraphAlignment(propertySources) {
1925
- let direction = 'ltr';
1926
- let justification = null;
1927
- let hasJustification = false;
1928
- for (const properties of propertySources){
1929
- const bidi = directChild(properties, 'bidi');
1930
- if (bidi) direction = onOffValue(bidi) ? 'rtl' : 'ltr';
1931
- const source = directChild(properties, 'jc');
1932
- if (source) {
1933
- hasJustification = true;
1934
- justification = wordAttribute(source, 'val')?.trim() ?? null;
1935
- }
1936
- }
1937
- if (!hasJustification || !justification) return null;
1938
- if ('center' === justification) return 'center';
1939
- if ('left' === justification) return 'left';
1940
- if ('right' === justification) return 'right';
1941
- if ('start' === justification) return 'rtl' === direction ? 'right' : 'left';
1942
- if ('end' === justification) return 'rtl' === direction ? 'left' : 'right';
1943
- if ('both' === justification || 'distribute' === justification || 'highKashida' === justification || 'lowKashida' === justification || 'mediumKashida' === justification || 'thaiDistribute' === justification) return 'justify';
1944
- return null;
1945
- }
1946
- function onOffValue(element) {
1947
- const value = wordAttribute(element, 'val')?.trim().toLowerCase();
1948
- return '0' !== value && 'false' !== value && 'off' !== value;
1949
- }
1950
- function wordAttribute(element, name) {
1951
- return attribute(element, name) ?? attribute(element, `w:${name}`);
1952
- }
1953
- function work_docx_paragraph_alignment_import_insertParagraphMarker(document, paragraph, properties, marker) {
1954
- const run = document.createElementNS(work_docx_paragraph_alignment_import_WORD_NAMESPACE, 'w:r');
1955
- const text = document.createElementNS(work_docx_paragraph_alignment_import_WORD_NAMESPACE, 'w:t');
1956
- text.setAttributeNS(work_docx_paragraph_alignment_import_XML_NAMESPACE, 'xml:space', 'preserve');
1957
- text.textContent = marker;
1958
- run.append(text);
1959
- paragraph.insertBefore(run, properties.nextSibling);
1960
- }
1961
- function insertParagraphProperties(document, paragraph) {
1962
- const properties = document.createElementNS(work_docx_paragraph_alignment_import_WORD_NAMESPACE, 'w:pPr');
1963
- paragraph.insertBefore(properties, paragraph.firstChild);
1964
- return properties;
1965
- }
1966
- function closestParagraphBlock(element, markerNode) {
1967
- const block = element?.closest('p, h1, h2, h3, h4, h5, h6, blockquote, li');
1968
- if (!(block instanceof HTMLElement)) return null;
1969
- return 'li' === block.tagName.toLowerCase() ? wrapListItemParagraph(block, markerNode) : block;
1970
- }
1971
- function wrapListItemParagraph(item, markerNode) {
1972
- let anchor = markerNode;
1973
- while(anchor.parentNode && anchor.parentNode !== item)anchor = anchor.parentNode;
1974
- const children = Array.from(item.childNodes);
1975
- const anchorIndex = children.indexOf(anchor);
1976
- if (anchorIndex < 0) return item;
1977
- let start = anchorIndex;
1978
- let end = anchorIndex;
1979
- while(start > 0 && !isListItemBlock(children[start - 1]))start -= 1;
1980
- while(end + 1 < children.length && !isListItemBlock(children[end + 1]))end += 1;
1981
- const paragraph = item.ownerDocument.createElement('p');
1982
- const grouped = children.slice(start, end + 1);
1983
- item.insertBefore(paragraph, grouped[0] ?? null);
1984
- paragraph.append(...grouped);
1985
- return paragraph;
1986
- }
1987
- function isListItemBlock(node) {
1988
- if (!(node instanceof HTMLElement)) return false;
1989
- return [
1990
- 'blockquote',
1991
- 'div',
1992
- 'h1',
1993
- 'h2',
1994
- 'h3',
1995
- 'h4',
1996
- 'h5',
1997
- 'h6',
1998
- 'hr',
1999
- 'ol',
2000
- 'p',
2001
- 'pre',
2002
- 'table',
2003
- 'ul'
2004
- ].includes(node.tagName.toLowerCase());
2005
- }
2006
- function work_docx_paragraph_alignment_import_textNodes(root) {
2007
- const document = root.ownerDocument;
2008
- const walker = document?.createTreeWalker(root, NodeFilter.SHOW_TEXT);
2009
- const nodes = [];
2010
- if (!walker) return nodes;
2011
- while(walker.nextNode())nodes.push(walker.currentNode);
2012
- return nodes;
2013
- }
2014
- const work_docx_paragraph_direction_import_WORD_NAMESPACE = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
2015
- const work_docx_paragraph_direction_import_XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace';
2016
- const PARAGRAPH_DIRECTION_MARKER_PATTERN = /__A3S_WORK_PARAGRAPH_DIRECTION_\d+__/g;
2017
- function markDocxParagraphDirections(document, styleSource, tableStyleSource) {
2018
- const paragraphs = [];
2019
- const styles = resolveDocxParagraphStyleResolver(styleSource);
2020
- const tableStyles = resolveDocxTableStyleResolver(tableStyleSource);
2021
- for (const paragraph of descendants(document, 'p')){
2022
- let properties = directChild(paragraph, 'pPr');
2023
- const sources = docxParagraphPropertySources(properties, styles, docxTableParagraphPropertySources(paragraph, tableStyles));
2024
- const direction = paragraphDirection(sources);
2025
- if (!direction) continue;
2026
- properties ??= work_docx_paragraph_direction_import_insertParagraphProperties(document, paragraph);
2027
- const marker = `__A3S_WORK_PARAGRAPH_DIRECTION_${paragraphs.length + 1}__`;
2028
- work_docx_paragraph_direction_import_insertParagraphMarker(document, paragraph, properties, marker);
2029
- paragraphs.push({
2030
- marker,
2031
- direction
2032
- });
2033
- }
2034
- return {
2035
- paragraphs
2036
- };
2037
- }
2038
- function applyImportedDocxParagraphDirectionMarkers(document, markers) {
2039
- const directionByMarker = new Map(markers.paragraphs.map(({ marker, direction })=>[
2040
- marker,
2041
- direction
2042
- ]));
2043
- for (const node of work_docx_paragraph_direction_import_textNodes(document.body)){
2044
- if (!node.data.includes('__A3S_WORK_PARAGRAPH_DIRECTION_')) continue;
2045
- const block = work_docx_paragraph_direction_import_closestParagraphBlock(node.parentElement);
2046
- node.data = node.data.replace(PARAGRAPH_DIRECTION_MARKER_PATTERN, (marker)=>{
2047
- const direction = directionByMarker.get(marker);
2048
- if (block && direction) block.setAttribute('dir', direction);
2049
- return '';
2050
- });
2051
- }
2052
- document.body.normalize();
2053
- }
2054
- function hasImportedDocxParagraphDirectionMarkers(markers) {
2055
- return markers.paragraphs.length > 0;
2056
- }
2057
- function paragraphDirection(propertySources) {
2058
- let direction = null;
2059
- for (const properties of propertySources){
2060
- const bidi = directChild(properties, 'bidi');
2061
- if (bidi) direction = work_docx_paragraph_direction_import_onOffValue(bidi) ? 'rtl' : 'ltr';
2062
- }
2063
- return direction;
2064
- }
2065
- function work_docx_paragraph_direction_import_onOffValue(element) {
2066
- const value = (attribute(element, 'val') ?? attribute(element, 'w:val'))?.trim().toLowerCase();
2067
- return '0' !== value && 'false' !== value && 'off' !== value;
2068
- }
2069
- function work_docx_paragraph_direction_import_insertParagraphMarker(document, paragraph, properties, marker) {
2070
- const run = document.createElementNS(work_docx_paragraph_direction_import_WORD_NAMESPACE, 'w:r');
2071
- const text = document.createElementNS(work_docx_paragraph_direction_import_WORD_NAMESPACE, 'w:t');
2072
- text.setAttributeNS(work_docx_paragraph_direction_import_XML_NAMESPACE, 'xml:space', 'preserve');
2073
- text.textContent = marker;
2074
- run.append(text);
2075
- paragraph.insertBefore(run, properties.nextSibling);
2076
- }
2077
- function work_docx_paragraph_direction_import_insertParagraphProperties(document, paragraph) {
2078
- const properties = document.createElementNS(work_docx_paragraph_direction_import_WORD_NAMESPACE, 'w:pPr');
2079
- paragraph.insertBefore(properties, paragraph.firstChild);
2080
- return properties;
2081
- }
2082
- function work_docx_paragraph_direction_import_closestParagraphBlock(element) {
2083
- const block = element?.closest('p, h1, h2, h3, h4, h5, h6, blockquote, li');
2084
- return block instanceof HTMLElement ? block : null;
2085
- }
2086
- function work_docx_paragraph_direction_import_textNodes(root) {
2087
- const document = root.ownerDocument;
2088
- const walker = document?.createTreeWalker(root, NodeFilter.SHOW_TEXT);
2089
- const nodes = [];
2090
- if (!walker) return nodes;
2091
- while(walker.nextNode())nodes.push(walker.currentNode);
2092
- return nodes;
2093
- }
2094
1884
  const work_docx_paragraph_identity_import_WORD_2010_NAMESPACE = 'http://schemas.microsoft.com/office/word/2010/wordml';
2095
1885
  const work_docx_paragraph_identity_import_XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace';
2096
1886
  const PARAGRAPH_IDENTITY_MARKER_PATTERN = /__A3S_WORK_PARAGRAPH_IDENTITY_\d+__/g;
@@ -2144,7 +1934,7 @@ function applyImportedDocxParagraphIdentityMarkers(document, markers) {
2144
1934
  ]));
2145
1935
  for (const node of work_docx_paragraph_identity_import_textNodes(document.body))if (node.data.includes('__A3S_WORK_PARAGRAPH_IDENTITY_')) node.data = node.data.replace(PARAGRAPH_IDENTITY_MARKER_PATTERN, (marker)=>{
2146
1936
  const identity = identities.get(marker);
2147
- const block = identity ? work_docx_paragraph_identity_import_closestParagraphBlock(node.parentElement, node) : null;
1937
+ const block = identity ? closestParagraphBlock(node.parentElement, node) : null;
2148
1938
  if (identity && block) applyDocumentParagraphIdentityToElement(block, identity);
2149
1939
  return '';
2150
1940
  });
@@ -2174,7 +1964,7 @@ function work_docx_paragraph_identity_import_insertParagraphMarker(paragraph, ma
2174
1964
  const properties = directChildren(paragraph, 'pPr').find((element)=>DOCX_WORDPROCESSING_NAMESPACES.has(element.namespaceURI ?? ''));
2175
1965
  paragraph.insertBefore(run, properties?.nextSibling ?? paragraph.firstChild);
2176
1966
  }
2177
- function work_docx_paragraph_identity_import_closestParagraphBlock(element, markerNode) {
1967
+ function closestParagraphBlock(element, markerNode) {
2178
1968
  const explicit = element?.closest('p, h1, h2, h3, h4, h5, h6, figcaption');
2179
1969
  if (explicit instanceof HTMLElement) return explicit;
2180
1970
  const container = element?.closest('li, blockquote, td, th, div');
@@ -2223,534 +2013,6 @@ function work_docx_paragraph_identity_import_textNodes(root) {
2223
2013
  while(walker.nextNode())nodes.push(walker.currentNode);
2224
2014
  return nodes;
2225
2015
  }
2226
- const work_docx_paragraph_indent_import_WORD_NAMESPACE = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
2227
- const work_docx_paragraph_indent_import_XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace';
2228
- const TWIPS_PER_CSS_PIXEL = 15;
2229
- const PARAGRAPH_INDENT_MARKER_PATTERN = /__A3S_WORK_PARAGRAPH_INDENT_\d+__/g;
2230
- function markDocxParagraphIndents(document, styleSource, tableStyleSource) {
2231
- const paragraphs = [];
2232
- const styles = resolveDocxParagraphStyleResolver(styleSource);
2233
- const tableStyles = resolveDocxTableStyleResolver(tableStyleSource);
2234
- for (const paragraph of descendants(document, 'p')){
2235
- let properties = directChild(paragraph, 'pPr');
2236
- const sources = docxParagraphPropertySources(properties, styles, docxTableParagraphPropertySources(paragraph, tableStyles));
2237
- if (sources.some((source)=>directChild(source, 'numPr'))) continue;
2238
- const indent = paragraphIndent(sources);
2239
- if (!indent.left && !indent.right && !indent.firstLine) continue;
2240
- properties ??= work_docx_paragraph_indent_import_insertParagraphProperties(document, paragraph);
2241
- const marker = `__A3S_WORK_PARAGRAPH_INDENT_${paragraphs.length + 1}__`;
2242
- work_docx_paragraph_indent_import_insertParagraphMarker(document, paragraph, properties, marker);
2243
- paragraphs.push({
2244
- marker,
2245
- indent
2246
- });
2247
- }
2248
- return {
2249
- paragraphs
2250
- };
2251
- }
2252
- function applyImportedDocxParagraphIndentMarkers(document, markers) {
2253
- const indentByMarker = new Map(markers.paragraphs.map(({ marker, indent })=>[
2254
- marker,
2255
- indent
2256
- ]));
2257
- for (const node of work_docx_paragraph_indent_import_textNodes(document.body)){
2258
- if (!node.data.includes('__A3S_WORK_PARAGRAPH_INDENT_')) continue;
2259
- const block = work_docx_paragraph_indent_import_closestParagraphBlock(node.parentElement);
2260
- node.data = node.data.replace(PARAGRAPH_INDENT_MARKER_PATTERN, (marker)=>{
2261
- const indent = indentByMarker.get(marker);
2262
- if (block && indent) applyParagraphIndent(block, indent);
2263
- return '';
2264
- });
2265
- }
2266
- document.body.normalize();
2267
- }
2268
- function hasImportedDocxParagraphIndentMarkers(markers) {
2269
- return markers.paragraphs.length > 0;
2270
- }
2271
- function paragraphIndent(propertySources) {
2272
- let left;
2273
- let right;
2274
- let firstLine;
2275
- for (const properties of propertySources){
2276
- const indent = directChild(properties, 'ind');
2277
- if (!indent) continue;
2278
- const sourceLeft = work_docx_paragraph_indent_import_numericAttribute(indent, 'start') ?? work_docx_paragraph_indent_import_numericAttribute(indent, 'left');
2279
- const sourceRight = work_docx_paragraph_indent_import_numericAttribute(indent, 'end') ?? work_docx_paragraph_indent_import_numericAttribute(indent, 'right');
2280
- const sourceFirstLine = work_docx_paragraph_indent_import_numericAttribute(indent, 'firstLine');
2281
- const sourceHanging = work_docx_paragraph_indent_import_numericAttribute(indent, 'hanging');
2282
- if (void 0 !== sourceLeft) left = sourceLeft;
2283
- if (void 0 !== sourceRight) right = sourceRight;
2284
- if (void 0 !== sourceFirstLine) firstLine = sourceFirstLine;
2285
- else if (void 0 !== sourceHanging) firstLine = -sourceHanging;
2286
- }
2287
- return normalizeDocumentParagraphIndent({
2288
- left: twipsToPixels(left),
2289
- right: twipsToPixels(right),
2290
- firstLine: twipsToPixels(firstLine)
2291
- });
2292
- }
2293
- function work_docx_paragraph_indent_import_insertParagraphMarker(document, paragraph, properties, marker) {
2294
- const run = document.createElementNS(work_docx_paragraph_indent_import_WORD_NAMESPACE, 'w:r');
2295
- const text = document.createElementNS(work_docx_paragraph_indent_import_WORD_NAMESPACE, 'w:t');
2296
- text.setAttributeNS(work_docx_paragraph_indent_import_XML_NAMESPACE, 'xml:space', 'preserve');
2297
- text.textContent = marker;
2298
- run.append(text);
2299
- paragraph.insertBefore(run, properties.nextSibling);
2300
- }
2301
- function work_docx_paragraph_indent_import_insertParagraphProperties(document, paragraph) {
2302
- const properties = document.createElementNS(work_docx_paragraph_indent_import_WORD_NAMESPACE, 'w:pPr');
2303
- paragraph.insertBefore(properties, paragraph.firstChild);
2304
- return properties;
2305
- }
2306
- function work_docx_paragraph_indent_import_closestParagraphBlock(element) {
2307
- const block = element?.closest('p, h1, h2, h3, h4, h5, h6, blockquote');
2308
- return block instanceof HTMLElement ? block : null;
2309
- }
2310
- function applyParagraphIndent(element, value) {
2311
- const indent = normalizeDocumentParagraphIndent(value);
2312
- if (indent.left) {
2313
- element.dataset.officeIndentLevel = work_docx_paragraph_indent_import_formatNumber(indent.left / 24);
2314
- element.style.marginLeft = `${work_docx_paragraph_indent_import_formatNumber(indent.left)}px`;
2315
- }
2316
- if (indent.right) {
2317
- element.dataset.officeIndentRight = work_docx_paragraph_indent_import_formatNumber(indent.right);
2318
- element.style.marginRight = `${work_docx_paragraph_indent_import_formatNumber(indent.right)}px`;
2319
- }
2320
- if (indent.firstLine) {
2321
- element.dataset.officeIndentFirstLine = work_docx_paragraph_indent_import_formatNumber(indent.firstLine);
2322
- element.style.textIndent = `${work_docx_paragraph_indent_import_formatNumber(indent.firstLine)}px`;
2323
- }
2324
- }
2325
- function work_docx_paragraph_indent_import_numericAttribute(element, name) {
2326
- const raw = attribute(element, name) ?? attribute(element, `w:${name}`);
2327
- if (null === raw || !raw.trim()) return;
2328
- const value = Number(raw);
2329
- return Number.isFinite(value) ? value : void 0;
2330
- }
2331
- function twipsToPixels(value) {
2332
- if (void 0 === value) return 0;
2333
- return Math.round(value / TWIPS_PER_CSS_PIXEL);
2334
- }
2335
- function work_docx_paragraph_indent_import_formatNumber(value) {
2336
- return Number(value.toFixed(2)).toString();
2337
- }
2338
- function work_docx_paragraph_indent_import_textNodes(root) {
2339
- const document = root.ownerDocument;
2340
- const walker = document?.createTreeWalker(root, NodeFilter.SHOW_TEXT);
2341
- const nodes = [];
2342
- if (!walker) return nodes;
2343
- while(walker.nextNode())nodes.push(walker.currentNode);
2344
- return nodes;
2345
- }
2346
- const work_docx_paragraph_pagination_import_WORD_NAMESPACE = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
2347
- const work_docx_paragraph_pagination_import_XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace';
2348
- const PARAGRAPH_PAGINATION_MARKER_PATTERN = /__A3S_WORK_PARAGRAPH_PAGINATION_\d+__/g;
2349
- function markDocxParagraphPagination(document, styleSource, tableStyleSource) {
2350
- const paragraphs = [];
2351
- const styles = resolveDocxParagraphStyleResolver(styleSource);
2352
- const tableStyles = resolveDocxTableStyleResolver(tableStyleSource);
2353
- for (const paragraph of descendants(document, 'p')){
2354
- let properties = directChild(paragraph, 'pPr');
2355
- if (properties && directChild(properties, 'numPr')) continue;
2356
- const pagination = paragraphPagination(docxParagraphPropertySources(properties, styles, docxTableParagraphPropertySources(paragraph, tableStyles)));
2357
- if (!Object.keys(pagination).length) continue;
2358
- properties ??= work_docx_paragraph_pagination_import_insertParagraphProperties(document, paragraph);
2359
- const marker = `__A3S_WORK_PARAGRAPH_PAGINATION_${paragraphs.length + 1}__`;
2360
- work_docx_paragraph_pagination_import_insertParagraphMarker(document, paragraph, properties, marker);
2361
- paragraphs.push({
2362
- marker,
2363
- pagination
2364
- });
2365
- }
2366
- return {
2367
- paragraphs
2368
- };
2369
- }
2370
- function applyImportedDocxParagraphPaginationMarkers(document, markers) {
2371
- const paginationByMarker = new Map(markers.paragraphs.map(({ marker, pagination })=>[
2372
- marker,
2373
- pagination
2374
- ]));
2375
- for (const node of work_docx_paragraph_pagination_import_textNodes(document.body)){
2376
- if (!node.data.includes('__A3S_WORK_PARAGRAPH_PAGINATION_')) continue;
2377
- const block = work_docx_paragraph_pagination_import_closestParagraphBlock(node.parentElement);
2378
- node.data = node.data.replace(PARAGRAPH_PAGINATION_MARKER_PATTERN, (marker)=>{
2379
- const pagination = paginationByMarker.get(marker);
2380
- if (block && pagination) applyParagraphPagination(block, pagination);
2381
- return '';
2382
- });
2383
- }
2384
- document.body.normalize();
2385
- }
2386
- function hasImportedDocxParagraphPaginationMarkers(markers) {
2387
- return markers.paragraphs.length > 0;
2388
- }
2389
- function paragraphPagination(sources) {
2390
- const pagination = {};
2391
- for (const properties of sources){
2392
- const keepLines = directChild(properties, 'keepLines');
2393
- const keepWithNext = directChild(properties, 'keepNext');
2394
- const pageBreakBefore = directChild(properties, 'pageBreakBefore');
2395
- const widowControl = directChild(properties, 'widowControl');
2396
- const contextualSpacing = directChild(properties, 'contextualSpacing');
2397
- const outlineLevel = directChild(properties, 'outlineLvl');
2398
- const defaultCollapsed = parseDocxParagraphDefaultCollapsed(properties);
2399
- if (keepLines) pagination.keepLines = work_docx_paragraph_pagination_import_onOffValue(keepLines);
2400
- if (keepWithNext) pagination.keepWithNext = work_docx_paragraph_pagination_import_onOffValue(keepWithNext);
2401
- if (pageBreakBefore) pagination.pageBreakBefore = work_docx_paragraph_pagination_import_onOffValue(pageBreakBefore);
2402
- if (widowControl) pagination.widowControl = work_docx_paragraph_pagination_import_onOffValue(widowControl);
2403
- if (contextualSpacing) pagination.contextualSpacing = work_docx_paragraph_pagination_import_onOffValue(contextualSpacing);
2404
- const outline = integerValue(outlineLevel);
2405
- if (null !== outline && outline <= 9) pagination.outlineLevel = outline;
2406
- if ('valid' === defaultCollapsed.status) pagination.defaultCollapsed = defaultCollapsed.value;
2407
- else if ('invalid' === defaultCollapsed.status) delete pagination.defaultCollapsed;
2408
- }
2409
- return pagination;
2410
- }
2411
- function work_docx_paragraph_pagination_import_onOffValue(element) {
2412
- const value = (attribute(element, 'val') ?? attribute(element, 'w:val'))?.trim().toLowerCase();
2413
- return '0' !== value && 'false' !== value && 'off' !== value;
2414
- }
2415
- function work_docx_paragraph_pagination_import_insertParagraphMarker(document, paragraph, properties, marker) {
2416
- const run = document.createElementNS(work_docx_paragraph_pagination_import_WORD_NAMESPACE, 'w:r');
2417
- const text = document.createElementNS(work_docx_paragraph_pagination_import_WORD_NAMESPACE, 'w:t');
2418
- text.setAttributeNS(work_docx_paragraph_pagination_import_XML_NAMESPACE, 'xml:space', 'preserve');
2419
- text.textContent = marker;
2420
- run.append(text);
2421
- paragraph.insertBefore(run, properties.nextSibling);
2422
- }
2423
- function work_docx_paragraph_pagination_import_insertParagraphProperties(document, paragraph) {
2424
- const properties = document.createElementNS(work_docx_paragraph_pagination_import_WORD_NAMESPACE, 'w:pPr');
2425
- paragraph.insertBefore(properties, paragraph.firstChild);
2426
- return properties;
2427
- }
2428
- function work_docx_paragraph_pagination_import_closestParagraphBlock(element) {
2429
- const block = element?.closest('p, h1, h2, h3, h4, h5, h6, blockquote');
2430
- return block instanceof HTMLElement ? block : null;
2431
- }
2432
- function applyParagraphPagination(element, pagination) {
2433
- setBooleanAttribute(element, 'data-office-keep-lines', pagination.keepLines);
2434
- setBooleanAttribute(element, 'data-office-keep-with-next', pagination.keepWithNext);
2435
- setBooleanAttribute(element, 'data-office-page-break-before', pagination.pageBreakBefore);
2436
- setBooleanAttribute(element, 'data-office-widow-control', pagination.widowControl);
2437
- setBooleanAttribute(element, 'data-office-contextual-spacing', pagination.contextualSpacing);
2438
- setBooleanAttribute(element, 'data-office-default-collapsed', pagination.defaultCollapsed);
2439
- if (void 0 !== pagination.outlineLevel) element.dataset.officeOutlineLevel = String(pagination.outlineLevel);
2440
- }
2441
- function integerValue(element) {
2442
- if (!element) return null;
2443
- const value = Number(attribute(element, 'val') ?? attribute(element, 'w:val'));
2444
- return Number.isSafeInteger(value) && value >= 0 ? value : null;
2445
- }
2446
- function setBooleanAttribute(element, name, value) {
2447
- if (void 0 !== value) element.setAttribute(name, String(value));
2448
- }
2449
- function work_docx_paragraph_pagination_import_textNodes(root) {
2450
- const document = root.ownerDocument;
2451
- const walker = document?.createTreeWalker(root, NodeFilter.SHOW_TEXT);
2452
- const nodes = [];
2453
- if (!walker) return nodes;
2454
- while(walker.nextNode())nodes.push(walker.currentNode);
2455
- return nodes;
2456
- }
2457
- const work_docx_paragraph_spacing_import_WORD_NAMESPACE = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
2458
- const work_docx_paragraph_spacing_import_XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace';
2459
- const PARAGRAPH_SPACING_MARKER_PATTERN = /__A3S_WORK_PARAGRAPH_SPACING_\d+__/g;
2460
- function markDocxParagraphSpacing(document, styleSource, tableStyleSource) {
2461
- const paragraphs = [];
2462
- const styles = resolveDocxParagraphStyleResolver(styleSource);
2463
- const tableStyles = resolveDocxTableStyleResolver(tableStyleSource);
2464
- for (const paragraph of descendants(document, 'p')){
2465
- let properties = directChild(paragraph, 'pPr');
2466
- const sources = docxParagraphPropertySources(properties, styles, docxTableParagraphPropertySources(paragraph, tableStyles));
2467
- if (sources.some((source)=>directChild(source, 'numPr'))) continue;
2468
- const spacing = paragraphSpacing(sources);
2469
- if (!Object.keys(spacing).length) continue;
2470
- properties ??= work_docx_paragraph_spacing_import_insertParagraphProperties(document, paragraph);
2471
- const marker = `__A3S_WORK_PARAGRAPH_SPACING_${paragraphs.length + 1}__`;
2472
- work_docx_paragraph_spacing_import_insertParagraphMarker(document, paragraph, properties, marker);
2473
- paragraphs.push({
2474
- marker,
2475
- spacing
2476
- });
2477
- }
2478
- return {
2479
- paragraphs
2480
- };
2481
- }
2482
- function applyImportedDocxParagraphSpacingMarkers(document, markers) {
2483
- const spacingByMarker = new Map(markers.paragraphs.map(({ marker, spacing })=>[
2484
- marker,
2485
- spacing
2486
- ]));
2487
- for (const node of work_docx_paragraph_spacing_import_textNodes(document.body)){
2488
- if (!node.data.includes('__A3S_WORK_PARAGRAPH_SPACING_')) continue;
2489
- const block = work_docx_paragraph_spacing_import_closestParagraphBlock(node.parentElement);
2490
- node.data = node.data.replace(PARAGRAPH_SPACING_MARKER_PATTERN, (marker)=>{
2491
- const spacing = spacingByMarker.get(marker);
2492
- if (block && spacing) applyParagraphSpacing(block, spacing);
2493
- return '';
2494
- });
2495
- }
2496
- document.body.normalize();
2497
- }
2498
- function hasImportedDocxParagraphSpacingMarkers(markers) {
2499
- return markers.paragraphs.length > 0;
2500
- }
2501
- function paragraphSpacing(propertySources) {
2502
- let before;
2503
- let after;
2504
- let line;
2505
- let lineRule;
2506
- for (const properties of propertySources){
2507
- const spacing = directChild(properties, 'spacing');
2508
- if (!spacing) continue;
2509
- const sourceBefore = work_docx_paragraph_spacing_import_numericAttribute(spacing, 'before');
2510
- const sourceAfter = work_docx_paragraph_spacing_import_numericAttribute(spacing, 'after');
2511
- const sourceLine = work_docx_paragraph_spacing_import_numericAttribute(spacing, 'line');
2512
- const sourceLineRule = normalizedLineRule(work_docx_paragraph_spacing_import_wordAttribute(spacing, 'lineRule'));
2513
- if (void 0 !== sourceBefore) before = sourceBefore;
2514
- if (void 0 !== sourceAfter) after = sourceAfter;
2515
- if (void 0 !== sourceLine) {
2516
- line = sourceLine;
2517
- lineRule = sourceLineRule ?? 'auto';
2518
- } else if (sourceLineRule) lineRule = sourceLineRule;
2519
- }
2520
- const result = {};
2521
- if (void 0 !== before) result.before = twipsToPoints(before);
2522
- if (void 0 !== after) result.after = twipsToPoints(after);
2523
- if (void 0 !== line) {
2524
- const rule = lineRule ?? 'auto';
2525
- result.lineHeight = 'auto' === rule ? work_docx_paragraph_spacing_import_formatNumber(line / 240) : `${work_docx_paragraph_spacing_import_formatNumber(line / 20)}pt`;
2526
- result.lineRule = rule;
2527
- }
2528
- return result;
2529
- }
2530
- function applyParagraphSpacing(element, spacing) {
2531
- if (void 0 !== spacing.before && null !== spacing.before) {
2532
- const value = work_docx_paragraph_spacing_import_formatNumber(spacing.before);
2533
- element.dataset.officeSpaceBefore = value;
2534
- element.style.marginTop = `${value}pt`;
2535
- }
2536
- if (void 0 !== spacing.after && null !== spacing.after) {
2537
- const value = work_docx_paragraph_spacing_import_formatNumber(spacing.after);
2538
- element.dataset.officeSpaceAfter = value;
2539
- element.style.marginBottom = `${value}pt`;
2540
- }
2541
- if (spacing.lineHeight) element.style.lineHeight = spacing.lineHeight;
2542
- if (spacing.lineRule) element.dataset.officeLineRule = spacing.lineRule;
2543
- if ('auto' === spacing.lineRule) {
2544
- const autoLineHeight = documentAutoLineHeight(spacing.lineHeight);
2545
- for (const [name, value] of Object.entries(renderDocumentAutoLineHeight(spacing.lineHeight, autoLineHeight)))if ('style' === name) for (const declaration of value.split(';')){
2546
- const separator = declaration.indexOf(':');
2547
- if (!(separator < 0)) element.style.setProperty(declaration.slice(0, separator).trim(), declaration.slice(separator + 1).trim());
2548
- }
2549
- else element.setAttribute(name, value);
2550
- }
2551
- }
2552
- function work_docx_paragraph_spacing_import_insertParagraphMarker(document, paragraph, properties, marker) {
2553
- const run = document.createElementNS(work_docx_paragraph_spacing_import_WORD_NAMESPACE, 'w:r');
2554
- const text = document.createElementNS(work_docx_paragraph_spacing_import_WORD_NAMESPACE, 'w:t');
2555
- text.setAttributeNS(work_docx_paragraph_spacing_import_XML_NAMESPACE, 'xml:space', 'preserve');
2556
- text.textContent = marker;
2557
- run.append(text);
2558
- paragraph.insertBefore(run, properties.nextSibling);
2559
- }
2560
- function work_docx_paragraph_spacing_import_insertParagraphProperties(document, paragraph) {
2561
- const properties = document.createElementNS(work_docx_paragraph_spacing_import_WORD_NAMESPACE, 'w:pPr');
2562
- paragraph.insertBefore(properties, paragraph.firstChild);
2563
- return properties;
2564
- }
2565
- function work_docx_paragraph_spacing_import_closestParagraphBlock(element) {
2566
- const block = element?.closest('p, h1, h2, h3, h4, h5, h6, blockquote');
2567
- return block instanceof HTMLElement ? block : null;
2568
- }
2569
- function work_docx_paragraph_spacing_import_numericAttribute(element, name) {
2570
- const raw = work_docx_paragraph_spacing_import_wordAttribute(element, name);
2571
- if (null === raw || !raw.trim()) return;
2572
- const value = Number(raw);
2573
- return Number.isFinite(value) ? value : void 0;
2574
- }
2575
- function work_docx_paragraph_spacing_import_wordAttribute(element, name) {
2576
- return attribute(element, name) ?? attribute(element, `w:${name}`);
2577
- }
2578
- function normalizedLineRule(value) {
2579
- if ('auto' === value || 'atLeast' === value) return value;
2580
- if ('exact' === value || 'exactly' === value) return 'exact';
2581
- }
2582
- function twipsToPoints(value) {
2583
- return Number((value / 20).toFixed(2));
2584
- }
2585
- function work_docx_paragraph_spacing_import_formatNumber(value) {
2586
- return Number(value.toFixed(3)).toString();
2587
- }
2588
- function work_docx_paragraph_spacing_import_textNodes(root) {
2589
- const document = root.ownerDocument;
2590
- const walker = document?.createTreeWalker(root, NodeFilter.SHOW_TEXT);
2591
- const nodes = [];
2592
- if (!walker) return nodes;
2593
- while(walker.nextNode())nodes.push(walker.currentNode);
2594
- return nodes;
2595
- }
2596
- const work_docx_tab_stop_import_WORD_NAMESPACE = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
2597
- const work_docx_tab_stop_import_XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace';
2598
- const work_docx_tab_stop_import_TWIPS_PER_CSS_PIXEL = 15;
2599
- const PARAGRAPH_TAB_STOP_MARKER_PATTERN = /__A3S_WORK_PARAGRAPH_TAB_STOPS_\d+__/g;
2600
- const INLINE_TAB_MARKER_PATTERN = /__A3S_WORK_INLINE_TAB_\d+__/g;
2601
- function markDocxParagraphTabStops(document, styleSource, tableStyleSource) {
2602
- const paragraphs = [];
2603
- const inlineTabs = [];
2604
- const styles = resolveDocxParagraphStyleResolver(styleSource);
2605
- const tableStyles = resolveDocxTableStyleResolver(tableStyleSource);
2606
- for (const paragraph of descendants(document, 'p')){
2607
- let properties = directChild(paragraph, 'pPr');
2608
- const tabStops = resolvedParagraphTabStops(docxParagraphPropertySources(properties, styles, docxTableParagraphPropertySources(paragraph, tableStyles)));
2609
- if (tabStops.length) {
2610
- properties ??= work_docx_tab_stop_import_insertParagraphProperties(document, paragraph);
2611
- const marker = `__A3S_WORK_PARAGRAPH_TAB_STOPS_${paragraphs.length + 1}__`;
2612
- work_docx_tab_stop_import_insertParagraphMarker(document, paragraph, properties, marker);
2613
- paragraphs.push({
2614
- marker,
2615
- tabStops
2616
- });
2617
- }
2618
- for (const tab of inlineRunTabs(paragraph)){
2619
- const marker = `__A3S_WORK_INLINE_TAB_${inlineTabs.length + 1}__`;
2620
- const text = document.createElementNS(work_docx_tab_stop_import_WORD_NAMESPACE, 'w:t');
2621
- text.setAttributeNS(work_docx_tab_stop_import_XML_NAMESPACE, 'xml:space', 'preserve');
2622
- text.textContent = marker;
2623
- tab.replaceWith(text);
2624
- inlineTabs.push(marker);
2625
- }
2626
- }
2627
- return {
2628
- paragraphs,
2629
- inlineTabs
2630
- };
2631
- }
2632
- function applyImportedDocxParagraphTabStopMarkers(document, markers) {
2633
- const tabStopsByMarker = new Map(markers.paragraphs.map(({ marker, tabStops })=>[
2634
- marker,
2635
- tabStops
2636
- ]));
2637
- for (const node of work_docx_tab_stop_import_textNodes(document.body)){
2638
- if (!node.data.includes('__A3S_WORK_PARAGRAPH_TAB_STOPS_')) continue;
2639
- const block = work_docx_tab_stop_import_closestParagraphBlock(node.parentElement);
2640
- node.data = node.data.replace(PARAGRAPH_TAB_STOP_MARKER_PATTERN, (marker)=>{
2641
- const tabStops = tabStopsByMarker.get(marker);
2642
- if (block && tabStops?.length) block.dataset.officeTabStops = serializeDocumentTabStops(tabStops);
2643
- return '';
2644
- });
2645
- }
2646
- const inlineMarkers = new Set(markers.inlineTabs);
2647
- for (const node of work_docx_tab_stop_import_textNodes(document.body))if (node.data.includes('__A3S_WORK_INLINE_TAB_')) replaceInlineTabMarkers(node, inlineMarkers);
2648
- document.body.normalize();
2649
- }
2650
- function hasImportedDocxParagraphTabStopMarkers(markers) {
2651
- return markers.paragraphs.length > 0 || markers.inlineTabs.length > 0;
2652
- }
2653
- function resolvedParagraphTabStops(sources) {
2654
- const byPosition = new Map();
2655
- for (const properties of sources){
2656
- const tabs = directChild(properties, 'tabs');
2657
- if (tabs) for (const tab of directChildren(tabs, 'tab')){
2658
- const positionTwips = work_docx_tab_stop_import_numericAttribute(tab, 'pos');
2659
- if (void 0 === positionTwips || positionTwips <= 0) continue;
2660
- const position = positionTwips / work_docx_tab_stop_import_TWIPS_PER_CSS_PIXEL;
2661
- const value = work_docx_tab_stop_import_wordAttribute(tab, 'val')?.trim();
2662
- if ('clear' === value || 'bar' === value) {
2663
- byPosition.delete(position);
2664
- continue;
2665
- }
2666
- byPosition.set(position, {
2667
- position,
2668
- alignment: docxTabAlignment(value),
2669
- leader: docxTabLeader(work_docx_tab_stop_import_wordAttribute(tab, 'leader'))
2670
- });
2671
- }
2672
- }
2673
- return normalizeDocumentTabStops(Array.from(byPosition.values()));
2674
- }
2675
- function inlineRunTabs(paragraph) {
2676
- return descendants(paragraph, 'tab').filter((tab)=>{
2677
- let parent = tab.parentElement;
2678
- while(parent && parent !== paragraph){
2679
- if ('tabs' === parent.localName) return false;
2680
- parent = parent.parentElement;
2681
- }
2682
- return parent === paragraph;
2683
- });
2684
- }
2685
- function replaceInlineTabMarkers(node, markers) {
2686
- const matches = Array.from(node.data.matchAll(INLINE_TAB_MARKER_PATTERN));
2687
- if (!matches.length) return;
2688
- const fragment = node.ownerDocument.createDocumentFragment();
2689
- let offset = 0;
2690
- for (const match of matches){
2691
- const marker = match[0];
2692
- const index = match.index ?? 0;
2693
- if (index > offset) fragment.append(node.data.slice(offset, index));
2694
- if (markers.has(marker)) {
2695
- const tab = node.ownerDocument.createElement('span');
2696
- tab.dataset.documentTab = 'true';
2697
- tab.className = 'work-document-tab';
2698
- tab.setAttribute('role', 'separator');
2699
- tab.setAttribute('aria-label', '制表符');
2700
- fragment.append(tab);
2701
- } else fragment.append(marker);
2702
- offset = index + marker.length;
2703
- }
2704
- if (offset < node.data.length) fragment.append(node.data.slice(offset));
2705
- node.replaceWith(fragment);
2706
- }
2707
- function work_docx_tab_stop_import_insertParagraphMarker(document, paragraph, properties, marker) {
2708
- const run = document.createElementNS(work_docx_tab_stop_import_WORD_NAMESPACE, 'w:r');
2709
- const text = document.createElementNS(work_docx_tab_stop_import_WORD_NAMESPACE, 'w:t');
2710
- text.setAttributeNS(work_docx_tab_stop_import_XML_NAMESPACE, 'xml:space', 'preserve');
2711
- text.textContent = marker;
2712
- run.append(text);
2713
- paragraph.insertBefore(run, properties.nextSibling);
2714
- }
2715
- function work_docx_tab_stop_import_insertParagraphProperties(document, paragraph) {
2716
- const properties = document.createElementNS(work_docx_tab_stop_import_WORD_NAMESPACE, 'w:pPr');
2717
- paragraph.insertBefore(properties, paragraph.firstChild);
2718
- return properties;
2719
- }
2720
- function work_docx_tab_stop_import_closestParagraphBlock(element) {
2721
- const block = element?.closest('p, h1, h2, h3, h4, h5, h6, blockquote');
2722
- return block instanceof HTMLElement ? block : null;
2723
- }
2724
- function docxTabAlignment(value) {
2725
- if ('center' === value) return 'center';
2726
- if ('right' === value || 'end' === value) return 'right';
2727
- if ('decimal' === value || 'num' === value) return 'decimal';
2728
- return 'left';
2729
- }
2730
- function docxTabLeader(value) {
2731
- if ('dot' === value) return 'dot';
2732
- if ('hyphen' === value) return 'hyphen';
2733
- if ('underscore' === value || 'heavy' === value) return 'underscore';
2734
- if ('middleDot' === value) return 'middleDot';
2735
- return 'none';
2736
- }
2737
- function work_docx_tab_stop_import_numericAttribute(element, name) {
2738
- const raw = work_docx_tab_stop_import_wordAttribute(element, name);
2739
- if (null === raw || !raw.trim()) return;
2740
- const value = Number(raw);
2741
- return Number.isFinite(value) ? value : void 0;
2742
- }
2743
- function work_docx_tab_stop_import_wordAttribute(element, name) {
2744
- return attribute(element, name) ?? attribute(element, `w:${name}`);
2745
- }
2746
- function work_docx_tab_stop_import_textNodes(root) {
2747
- const document = root.ownerDocument;
2748
- const walker = document?.createTreeWalker(root, NodeFilter.SHOW_TEXT);
2749
- const nodes = [];
2750
- if (!walker) return nodes;
2751
- while(walker.nextNode())nodes.push(walker.currentNode);
2752
- return nodes;
2753
- }
2754
2016
  const TABLE_BORDER_EDGES = [
2755
2017
  'top',
2756
2018
  'right',
@@ -3118,7 +2380,7 @@ function markDocxTableRows(document) {
3118
2380
  const cantSplit = properties ? directChild(properties, 'cantSplit') : void 0;
3119
2381
  const repeatHeader = properties ? directChild(properties, 'tblHeader') : void 0;
3120
2382
  const height = properties ? directChild(properties, 'trHeight') : void 0;
3121
- const rowHeight = height ? work_docx_table_row_import_twipsToPixels(Number(attribute(height, 'val'))) : null;
2383
+ const rowHeight = height ? twipsToPixels(Number(attribute(height, 'val'))) : null;
3122
2384
  const rowHeightRule = tableRowHeightRule(attribute(height ?? row, 'hRule'));
3123
2385
  const rowIdentity = identities.get(row);
3124
2386
  const uniqueIdentity = rowIdentity && 1 === identityCounts.get(rowIdentity.rowId) ? rowIdentity : null;
@@ -3130,10 +2392,10 @@ function markDocxTableRows(document) {
3130
2392
  rows.push({
3131
2393
  marker,
3132
2394
  ...cantSplit ? {
3133
- cantSplit: work_docx_table_row_import_onOffValue(cantSplit)
2395
+ cantSplit: onOffValue(cantSplit)
3134
2396
  } : {},
3135
2397
  ...repeatHeader ? {
3136
- repeatHeader: work_docx_table_row_import_onOffValue(repeatHeader)
2398
+ repeatHeader: onOffValue(repeatHeader)
3137
2399
  } : {},
3138
2400
  ...uniqueIdentity ?? {},
3139
2401
  ...null !== rowHeight ? {
@@ -3172,8 +2434,8 @@ function applyImportedDocxTableRowMarkers(document, markers) {
3172
2434
  const properties = rowByMarker.get(marker);
3173
2435
  if (row instanceof HTMLTableRowElement && properties) {
3174
2436
  applyDocumentTableRowIdentityToElement(row, properties);
3175
- work_docx_table_row_import_setBooleanAttribute(row, 'data-office-cant-split', properties.cantSplit);
3176
- work_docx_table_row_import_setBooleanAttribute(row, 'data-office-repeat-header', properties.repeatHeader);
2437
+ setBooleanAttribute(row, 'data-office-cant-split', properties.cantSplit);
2438
+ setBooleanAttribute(row, 'data-office-repeat-header', properties.repeatHeader);
3177
2439
  if (void 0 !== properties.rowHeight) {
3178
2440
  row.dataset.officeRowHeight = String(properties.rowHeight);
3179
2441
  row.style.height = `${properties.rowHeight}px`;
@@ -3219,18 +2481,18 @@ function wordTableRowIdentity(row) {
3219
2481
  function work_docx_table_row_import_word2010Attribute(element, localName) {
3220
2482
  return Array.from(element.attributes).find((item)=>xmlAttributeLocalName(item) === localName && xmlAttributeNamespace(element, item) === work_docx_table_row_import_WORD_2010_NAMESPACE)?.value ?? null;
3221
2483
  }
3222
- function work_docx_table_row_import_onOffValue(element) {
2484
+ function onOffValue(element) {
3223
2485
  const value = (attribute(element, 'val') ?? attribute(element, 'w:val'))?.trim().toLowerCase();
3224
2486
  return '0' !== value && 'false' !== value && 'off' !== value;
3225
2487
  }
3226
- function work_docx_table_row_import_setBooleanAttribute(element, name, value) {
2488
+ function setBooleanAttribute(element, name, value) {
3227
2489
  if (void 0 !== value) element.setAttribute(name, String(value));
3228
2490
  }
3229
2491
  function tableRowHeightRule(value) {
3230
2492
  if ('exact' === value) return 'exact';
3231
2493
  return 'atLeast' === value ? 'atLeast' : null;
3232
2494
  }
3233
- function work_docx_table_row_import_twipsToPixels(value) {
2495
+ function twipsToPixels(value) {
3234
2496
  if (!Number.isFinite(value) || value <= 0) return null;
3235
2497
  return Math.round(value * work_docx_table_row_import_PIXELS_PER_TWIP * 100) / 100;
3236
2498
  }
@@ -3515,8 +2777,8 @@ function work_docx_table_sizing_import_textNodes(root) {
3515
2777
  }
3516
2778
  const work_docx_import_WORD_NAMESPACE = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
3517
2779
  const work_docx_import_XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace';
3518
- async function prepareDocxImport(buffer) {
3519
- const archive = await OoxmlPackage.load(buffer);
2780
+ async function prepareDocxImport(buffer, sourcePackage) {
2781
+ const archive = sourcePackage ?? await OoxmlPackage.load(buffer);
3520
2782
  const bibliography = (await readDocxBibliography(archive)).bibliography;
3521
2783
  const fallback = documentInitialSectionLayout({
3522
2784
  type: 'document',
@@ -3565,6 +2827,9 @@ async function prepareDocxImport(buffer) {
3565
2827
  paragraphIdentityMarkers: {
3566
2828
  paragraphs: []
3567
2829
  },
2830
+ paragraphFormattingChangeMarkers: {
2831
+ paragraphs: []
2832
+ },
3568
2833
  paragraphAlignmentMarkers: {
3569
2834
  paragraphs: []
3570
2835
  },
@@ -3641,14 +2906,15 @@ async function prepareDocxImport(buffer) {
3641
2906
  const paragraphBorderMarkers = markDocxParagraphBorders(document, paragraphStyles, theme, tableStyles);
3642
2907
  const paragraphShadingMarkers = markDocxParagraphShading(document, paragraphStyles, theme, tableStyles);
3643
2908
  const runFormattingMarkers = markDocxRunFormatting(document, paragraphStyles, theme, tableStyles);
2909
+ const paragraphFormattingChangeMarkers = markDocxParagraphFormattingChanges(document, paragraphStyles, theme, tableStyles);
3644
2910
  const tabStopMarkers = markDocxParagraphTabStops(document, paragraphStyles, tableStyles);
3645
2911
  const tableCellMarkers = markDocxTableCells(document, theme, tableStyles);
3646
2912
  const tableRowMarkers = markDocxTableRows(document);
3647
2913
  const tableSizingMarkers = markDocxTableSizing(document, tableStyles);
3648
- const trackChanges = Boolean(settings && firstDescendant(settings, 'trackRevisions')) || changeMarkers.changes.length > 0;
2914
+ const trackChanges = Boolean(settings && firstDescendant(settings, 'trackRevisions')) || changeMarkers.changes.length > 0 || paragraphFormattingChangeMarkers.paragraphs.length > 0 || runFormattingMarkers.runs.some((run)=>Boolean(run.change));
3649
2915
  const sectionElements = effectiveSectionProperties(document);
3650
2916
  if (!sectionElements.length) return {
3651
- conversionBuffer: hasImportedDocxCaptionMarkers(captionMarkers) || hasImportedDocxBookmarkMarkers(bookmarkMarkers) || hasImportedDocxChangeMarkers(changeMarkers) || hasImportedDocxCommentMarkers(commentMarkers) || hasImportedDocxCitationMarkers(citationMarkers) || hasImportedDocxFieldMarkers(fieldMarkers) || equationMarkers.changed || hasImportedDocxListMarkers(listMarkers) || hasImportedDocxImageLayoutMarkers(imageLayoutMarkers) || hasImportedDocxParagraphIdentityMarkers(paragraphIdentityMarkers) || hasImportedDocxParagraphAlignmentMarkers(paragraphAlignmentMarkers) || hasImportedDocxParagraphDirectionMarkers(paragraphDirectionMarkers) || hasImportedDocxParagraphIndentMarkers(paragraphIndentMarkers) || hasImportedDocxParagraphSpacingMarkers(paragraphSpacingMarkers) || hasImportedDocxParagraphBorderMarkers(paragraphBorderMarkers) || hasImportedDocxParagraphShadingMarkers(paragraphShadingMarkers) || hasImportedDocxParagraphPaginationMarkers(paragraphPaginationMarkers) || hasImportedDocxRunFormattingMarkers(runFormattingMarkers) || hasImportedDocxParagraphTabStopMarkers(tabStopMarkers) || hasImportedDocxTableCellMarkers(tableCellMarkers) || hasImportedDocxTableRowMarkers(tableRowMarkers) || hasImportedDocxTableSizingMarkers(tableSizingMarkers) ? await writeDocumentXml(buffer, document, equationMarkers.parts) : buffer,
2917
+ conversionBuffer: hasImportedDocxCaptionMarkers(captionMarkers) || hasImportedDocxBookmarkMarkers(bookmarkMarkers) || hasImportedDocxChangeMarkers(changeMarkers) || hasImportedDocxCommentMarkers(commentMarkers) || hasImportedDocxCitationMarkers(citationMarkers) || hasImportedDocxFieldMarkers(fieldMarkers) || equationMarkers.changed || hasImportedDocxListMarkers(listMarkers) || hasImportedDocxImageLayoutMarkers(imageLayoutMarkers) || hasImportedDocxParagraphIdentityMarkers(paragraphIdentityMarkers) || hasImportedDocxParagraphFormattingChangeMarkers(paragraphFormattingChangeMarkers) || hasImportedDocxParagraphAlignmentMarkers(paragraphAlignmentMarkers) || hasImportedDocxParagraphDirectionMarkers(paragraphDirectionMarkers) || hasImportedDocxParagraphIndentMarkers(paragraphIndentMarkers) || hasImportedDocxParagraphSpacingMarkers(paragraphSpacingMarkers) || hasImportedDocxParagraphBorderMarkers(paragraphBorderMarkers) || hasImportedDocxParagraphShadingMarkers(paragraphShadingMarkers) || hasImportedDocxParagraphPaginationMarkers(paragraphPaginationMarkers) || hasImportedDocxRunFormattingMarkers(runFormattingMarkers) || hasImportedDocxParagraphTabStopMarkers(tabStopMarkers) || hasImportedDocxTableCellMarkers(tableCellMarkers) || hasImportedDocxTableRowMarkers(tableRowMarkers) || hasImportedDocxTableSizingMarkers(tableSizingMarkers) ? await writeDocumentXml(buffer, document, equationMarkers.parts) : buffer,
3652
2918
  sections: [
3653
2919
  {
3654
2920
  id: 'document-section-1',
@@ -3666,6 +2932,7 @@ async function prepareDocxImport(buffer) {
3666
2932
  listMarkers,
3667
2933
  imageLayoutMarkers,
3668
2934
  paragraphIdentityMarkers,
2935
+ paragraphFormattingChangeMarkers,
3669
2936
  paragraphAlignmentMarkers,
3670
2937
  paragraphDirectionMarkers,
3671
2938
  paragraphIndentMarkers,
@@ -3695,7 +2962,7 @@ async function prepareDocxImport(buffer) {
3695
2962
  }
3696
2963
  if (sections.length > 1) addSectionMarkers(document, sectionElements);
3697
2964
  return {
3698
- conversionBuffer: sections.length > 1 || hasImportedDocxCaptionMarkers(captionMarkers) || hasImportedDocxBookmarkMarkers(bookmarkMarkers) || hasImportedDocxChangeMarkers(changeMarkers) || hasImportedDocxCommentMarkers(commentMarkers) || hasImportedDocxCitationMarkers(citationMarkers) || hasImportedDocxFieldMarkers(fieldMarkers) || equationMarkers.changed || hasImportedDocxListMarkers(listMarkers) || hasImportedDocxImageLayoutMarkers(imageLayoutMarkers) || hasImportedDocxParagraphIdentityMarkers(paragraphIdentityMarkers) || hasImportedDocxParagraphAlignmentMarkers(paragraphAlignmentMarkers) || hasImportedDocxParagraphDirectionMarkers(paragraphDirectionMarkers) || hasImportedDocxParagraphIndentMarkers(paragraphIndentMarkers) || hasImportedDocxParagraphSpacingMarkers(paragraphSpacingMarkers) || hasImportedDocxParagraphBorderMarkers(paragraphBorderMarkers) || hasImportedDocxParagraphShadingMarkers(paragraphShadingMarkers) || hasImportedDocxParagraphPaginationMarkers(paragraphPaginationMarkers) || hasImportedDocxRunFormattingMarkers(runFormattingMarkers) || hasImportedDocxParagraphTabStopMarkers(tabStopMarkers) || hasImportedDocxTableCellMarkers(tableCellMarkers) || hasImportedDocxTableRowMarkers(tableRowMarkers) || hasImportedDocxTableSizingMarkers(tableSizingMarkers) ? await writeDocumentXml(buffer, document, equationMarkers.parts) : buffer,
2965
+ conversionBuffer: sections.length > 1 || hasImportedDocxCaptionMarkers(captionMarkers) || hasImportedDocxBookmarkMarkers(bookmarkMarkers) || hasImportedDocxChangeMarkers(changeMarkers) || hasImportedDocxCommentMarkers(commentMarkers) || hasImportedDocxCitationMarkers(citationMarkers) || hasImportedDocxFieldMarkers(fieldMarkers) || equationMarkers.changed || hasImportedDocxListMarkers(listMarkers) || hasImportedDocxImageLayoutMarkers(imageLayoutMarkers) || hasImportedDocxParagraphIdentityMarkers(paragraphIdentityMarkers) || hasImportedDocxParagraphFormattingChangeMarkers(paragraphFormattingChangeMarkers) || hasImportedDocxParagraphAlignmentMarkers(paragraphAlignmentMarkers) || hasImportedDocxParagraphDirectionMarkers(paragraphDirectionMarkers) || hasImportedDocxParagraphIndentMarkers(paragraphIndentMarkers) || hasImportedDocxParagraphSpacingMarkers(paragraphSpacingMarkers) || hasImportedDocxParagraphBorderMarkers(paragraphBorderMarkers) || hasImportedDocxParagraphShadingMarkers(paragraphShadingMarkers) || hasImportedDocxParagraphPaginationMarkers(paragraphPaginationMarkers) || hasImportedDocxRunFormattingMarkers(runFormattingMarkers) || hasImportedDocxParagraphTabStopMarkers(tabStopMarkers) || hasImportedDocxTableCellMarkers(tableCellMarkers) || hasImportedDocxTableRowMarkers(tableRowMarkers) || hasImportedDocxTableSizingMarkers(tableSizingMarkers) ? await writeDocumentXml(buffer, document, equationMarkers.parts) : buffer,
3699
2966
  sections,
3700
2967
  pageColor,
3701
2968
  captionMarkers,
@@ -3708,6 +2975,7 @@ async function prepareDocxImport(buffer) {
3708
2975
  listMarkers,
3709
2976
  imageLayoutMarkers,
3710
2977
  paragraphIdentityMarkers,
2978
+ paragraphFormattingChangeMarkers,
3711
2979
  paragraphAlignmentMarkers,
3712
2980
  paragraphDirectionMarkers,
3713
2981
  paragraphIndentMarkers,
@@ -3748,6 +3016,8 @@ function applyDocxSectionsToHtml(html, sections, captionMarkers = {
3748
3016
  images: []
3749
3017
  }, paragraphIdentityMarkers = {
3750
3018
  paragraphs: []
3019
+ }, paragraphFormattingChangeMarkers = {
3020
+ paragraphs: []
3751
3021
  }, paragraphAlignmentMarkers = {
3752
3022
  paragraphs: []
3753
3023
  }, runFormattingMarkers = {
@@ -3795,6 +3065,7 @@ function applyDocxSectionsToHtml(html, sections, captionMarkers = {
3795
3065
  applyImportedDocxParagraphPaginationMarkers(document, paragraphPaginationMarkers);
3796
3066
  applyImportedDocxParagraphTabStopMarkers(document, tabStopMarkers);
3797
3067
  applyImportedDocxParagraphAlignmentMarkers(document, paragraphAlignmentMarkers);
3068
+ applyImportedDocxParagraphFormattingChangeMarkers(document, paragraphFormattingChangeMarkers);
3798
3069
  applyImportedDocxTableCellMarkers(document, tableCellMarkers);
3799
3070
  applyImportedDocxTableSizingMarkers(document, tableSizingMarkers);
3800
3071
  applyImportedDocxTableRowMarkers(document, tableRowMarkers);