Skip to content

Commit 80458cc

Browse files
authored
Merge pull request #518 from Harbour-Enterprises/har-9668_html-annotation-export
HAR-9668 Fix issue with inability to open the document after export
2 parents 65342fd + 22e1d8c commit 80458cc

1 file changed

Lines changed: 43 additions & 8 deletions

File tree

  • packages/super-editor/src/core/super-converter

packages/super-editor/src/core/super-converter/exporter.js

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import {
99
linesToTwips,
1010
pixelsToEightPoints,
1111
pixelsToEmu,
12-
pixelsToTwips,
12+
pixelsToTwips,
1313
ptToTwips,
1414
rgbToHex,
1515
} from './helpers.js';
1616
import { generateDocxRandomId } from '@helpers/generateDocxRandomId.js';
1717
import { DEFAULT_DOCX_DEFS } from './exporter-docx-defs.js';
18-
import { TrackDeleteMarkName, TrackInsertMarkName, TrackFormatMarkName } from '@extensions/track-changes/constants.js';
18+
import { TrackDeleteMarkName, TrackFormatMarkName, TrackInsertMarkName } from '@extensions/track-changes/constants.js';
1919
import { carbonCopy } from '../utilities/carbonCopy.js';
2020
import { baseBulletList, baseOrderedListDef } from './v2/exporter/helpers/base-list.definitions.js';
2121
import { translateCommentNode } from './v2/exporter/commentsExporter.js';
@@ -121,7 +121,7 @@ function translateBodyNode(params) {
121121
attributes[`w:${key}`] = convertedValue;
122122
});
123123
sectPrMargins.attributes = attributes;
124-
};
124+
}
125125

126126
const elements = translateChildNodes(params);
127127
return {
@@ -141,7 +141,7 @@ export function translateParagraphNode(params) {
141141

142142
// Replace current paragraph with content of html annotation
143143
const htmlAnnotationChild = elements.find((element) => element.name === 'htmlAnnotation');
144-
if (elements.length === 1 && htmlAnnotationChild) {
144+
if (htmlAnnotationChild) {
145145
return htmlAnnotationChild.elements;
146146
}
147147

@@ -240,7 +240,7 @@ function generateParagraphProperties(node) {
240240
if (textAlign) {
241241
const textAlignElement = {
242242
name: 'w:jc',
243-
attributes: { 'w:val': textAlign },
243+
attributes: { 'w:val': textAlign === 'justify' ? 'both' : textAlign },
244244
};
245245
pPrElements.push(textAlignElement);
246246
}
@@ -347,7 +347,9 @@ function translateChildNodes(params) {
347347

348348
const translatedNodes = [];
349349
nodes.forEach((node) => {
350-
const translatedNode = exportSchemaToJson({ ...params, node });
350+
let translatedNode = exportSchemaToJson({ ...params, node });
351+
translatedNode = isolateAnnotations(translatedNode);
352+
351353
if (translatedNode instanceof Array) translatedNodes.push(...translatedNode);
352354
else translatedNodes.push(translatedNode);
353355
});
@@ -356,6 +358,37 @@ function translateChildNodes(params) {
356358
return translatedNodes.filter((n) => n);
357359
}
358360

361+
/**
362+
* Process nodes to isolate sdt annotations from simple text nodes
363+
* since having sdt annotation with text run in one paragraph inside table cell
364+
* can lead to export issues
365+
*/
366+
const isolateAnnotations = (node) => {
367+
if (!node) return node;
368+
const hasTextRun = node.elements?.some(item => item.name === 'w:r');
369+
const hasSdtContent = node.elements?.some(item => item.name === 'w:sdt');
370+
let result = node;
371+
if (hasTextRun && hasSdtContent) {
372+
const sdtNodes = node.elements.filter(item => item.name === 'w:sdt');
373+
const otherNodes = node.elements.filter(item => item.name !== 'w:sdt');
374+
const hasText = otherNodes.filter(item => item.name === 'w:r').every(item => {
375+
const textElements = item.elements.filter(item => item.name === 'w:t');
376+
return textElements?.every(item => item.elements[0].text.trim().length > 0);
377+
});
378+
result = [
379+
...(hasText ? [{
380+
...node,
381+
elements: otherNodes,
382+
}] : []),
383+
{
384+
name: 'w:p',
385+
elements: sdtNodes,
386+
}
387+
];
388+
}
389+
return result;
390+
}
391+
359392
/**
360393
* Helper function to be used for text node translation
361394
* Also used for transforming text annotations for the final submit
@@ -1131,6 +1164,7 @@ function translateTableCell(params) {
11311164
...params,
11321165
tableCell: params.node,
11331166
});
1167+
11341168
const cellProps = generateTableCellProperties(params.node);
11351169

11361170
elements.unshift(cellProps);
@@ -1318,7 +1352,8 @@ function translateMark(mark) {
13181352
case 'fontFamily':
13191353
value = attrs.fontFamily;
13201354
['w:ascii', 'w:eastAsia', 'w:hAnsi', 'w:cs'].forEach((attr) => {
1321-
markElement.attributes[attr] = value;
1355+
const parsedValue = value.split(', ');
1356+
markElement.attributes[attr] = parsedValue[0] ? parsedValue[0] : value;
13221357
});
13231358
break;
13241359

@@ -1749,7 +1784,7 @@ function prepareHtmlAnnotation(params) {
17491784
}
17501785

17511786
const htmlAnnotationNode = state.doc.toJSON();
1752-
1787+
17531788
return {
17541789
name: 'htmlAnnotation',
17551790
elements: translateChildNodes({

0 commit comments

Comments
 (0)