Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 25 additions & 35 deletions packages/super-editor/src/core/super-converter/exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,20 +783,8 @@ function translateList(params) {
// Collapse multiple paragraphs into a single node for this list item
// In docx we need a single paragraph, but can include line breaks in a run
const collapsedParagraphNode = convertMultipleListItemsIntoSingleNode(listItem);
let outputNode = exportSchemaToJson({ ...params, node: collapsedParagraphNode });

if (!outputNode) {
console.warn('translateList: exportSchemaToJson returned null/undefined');
return [
{
name: 'w:p',
elements: [
{ name: 'w:pPr', elements: numPrTag ? [numPrTag] : [] },
{ name: 'w:r', elements: [{ name: 'w:t', elements: [{ text: '', type: 'text' }] }] },
],
},
];
}
let outputNode = exportSchemaToJson({ ...params, node: collapsedParagraphNode });

/**
* MS Word does not allow paragraphs inside lists (which are just paragraphs in OOXML)
Expand All @@ -807,64 +795,66 @@ function translateList(params) {
* 2. Not final doc (keep w:sdt node, process its content)
*/
let nodesToFlatten = [];

if (Array.isArray(outputNode) && params.isFinalDoc) {
const parsedElements = [];
outputNode?.forEach((node, index) => {
if (node?.elements) {
const runs = node.elements?.filter((n) => n.name === 'w:r') || [];
const runs = node.elements?.filter((n) => n.name === 'w:r');
parsedElements.push(...runs);

if (node.name === 'w:p' && index < outputNode.length - 1) {
parsedElements.push({ name: 'w:br' });
parsedElements.push({
name: 'w:br',
});
}
}
});

outputNode = {
name: 'w:p',
elements: [{ name: 'w:pPr', elements: [] }, ...parsedElements],
};
}

/** Case 2: Process w:sdt content */
const sdtNodes = outputNode?.elements?.filter((n) => n.name === 'w:sdt') || [];
if (sdtNodes.length > 0) {
const sdtNodes = outputNode.elements?.filter((n) => n.name === 'w:sdt');
if (sdtNodes && sdtNodes.length > 0) {
nodesToFlatten = sdtNodes;
nodesToFlatten?.forEach((sdtNode) => {
const sdtContent = sdtNode.elements?.find((n) => n.name === 'w:sdtContent');
if (sdtContent?.elements) {
const sdtContent = sdtNode.elements.find((n) => n.name === 'w:sdtContent');
if (sdtContent && sdtContent.elements) {
const parsedElements = [];
sdtContent.elements.forEach((element, index) => {
const runs = element.elements?.filter((n) => n.name === 'w:r') || [];
const runs = element.elements?.filter((n) => n.name === 'w:r');
parsedElements.push(...runs);

if (element.name === 'w:p' && index < sdtContent.elements.length - 1) {
parsedElements.push({ name: 'w:br' });
parsedElements.push({
name: 'w:br',
});
}
});
sdtContent.elements = parsedElements;
}
});
}

const pPr = outputNode?.elements?.find((n) => n.name === 'w:pPr');
if (pPr?.elements && numPrTag) {
const pPr = outputNode.elements?.find((n) => n.name === 'w:pPr');
if (pPr && pPr.elements && numPrTag) {
pPr.elements.unshift(numPrTag);
}

const indentTag = restoreIndent(listItem.attrs.indent);
if (indentTag && pPr?.elements) {
pPr.elements.push(indentTag);
}
indentTag && pPr?.elements?.push(indentTag);

const runNode = outputNode?.elements?.find((n) => n.name === 'w:r');
const runNode = outputNode.elements?.find((n) => n.name === 'w:r');
const rPr = runNode?.elements?.find((n) => n.name === 'w:rPr');
if (rPr && pPr?.elements) {
pPr.elements.push(rPr);
}
if (rPr) pPr.elements.push(rPr);

if (listItem.attrs.numPrType !== 'inline') {
const numPrIndex = pPr?.elements?.findIndex((e) => e?.name === 'w:numPr');
if (numPrIndex !== -1 && pPr?.elements) {
pPr.elements.splice(numPrIndex, 1);
if (numPrIndex !== -1) {
pPr?.elements?.splice(numPrIndex, 1);
}
}

Expand Down Expand Up @@ -1686,7 +1676,7 @@ function translateMark(mark) {

const { attrs } = mark;
let value;

switch (mark.type) {
case 'bold':
if (attrs?.value) {
Expand Down Expand Up @@ -1736,7 +1726,7 @@ function translateMark(mark) {
case 'textIndent':
markElement.attributes['w:firstline'] = inchesToTwips(attrs.textIndent);
break;

case 'textTransform':
if (attrs?.textTransform === 'none') {
markElement.attributes['w:val'] = '0';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ function handleListNodes(params, node) {
if (!Object.keys(numberingDefinition).length) {
const { definition, ilvl } = getNodeNumberingDefinitionByStyle(node, docx);
if (definition) numberingDefinition = definition;
if (Number.isNaN(iLvl)) iLvl = ilvl;
if (Number.isNaN(iLvl)) iLvl = ilvl;
}

const { listType, listOrderingType, listrPrs, listpPrs, start, lvlText, lvlJc, customFormat } = numberingDefinition;

// Fallback if the list definition is not found or is invalid
// See invalid-list-def-fallback.docx for example and
if (!listType) {
Expand Down Expand Up @@ -201,7 +201,7 @@ export function testForList(node, docx) {
let outlinelvl;

const styleId = paragraphStyle?.attributes['w:val'];

const styleTag = getStyleTagFromStyleId(styleId, docx);
if (styleTag && !numId) {
const { numPr: numPrRecursve, type } = getNumPrRecursive({ node, styleId, docx });
Expand Down Expand Up @@ -544,7 +544,7 @@ export function getNodeNumberingDefinitionByStyle(item, docx) {
const styleId = styleTag?.attributes['w:val'];
const styleDef = getStyleTagFromStyleId(styleId, docx);
if (!styleDef) return {};

const pPr = styleDef.elements?.find((el) => el.name === 'w:pPr');
const numPr = pPr?.elements?.find((el) => el.name === 'w:numPr');
const numIdTag = numPr?.elements?.find((el) => el.name === 'w:numId');
Expand Down Expand Up @@ -573,7 +573,10 @@ function getAbstractNumIdByNumId(numId, docx) {
const listData = elements[0];
const numberingElements = listData.elements || [];

const numDef = numberingElements.find((el) => el.name === 'w:num' && el.attributes?.['w:numId'] === numId);
const numDef = numberingElements.find((el) =>
el.name === 'w:num' &&
el.attributes?.['w:numId'] === numId
);

if (!numDef) return null;

Expand All @@ -589,19 +592,20 @@ function getLevelDataFromAbstractNum(abstractNumId, styleId, docx) {
const listData = elements[0];
const numberingElements = listData.elements || [];

const abstractNum = numberingElements.find(
(el) => el.name === 'w:abstractNum' && el.attributes?.['w:abstractNumId'] === abstractNumId,
const abstractNum = numberingElements.find((el) =>
el.name === 'w:abstractNum' &&
el.attributes?.['w:abstractNumId'] === abstractNumId
);

if (!abstractNum) return null;

const levels = abstractNum.elements?.filter((el) => el.name === 'w:lvl') || [];
const levels = abstractNum.elements?.filter(el => el.name === 'w:lvl') || [];
for (const level of levels) {
const pStyle = level.elements?.find((el) => el.name === 'w:pStyle');
if (pStyle?.attributes?.['w:val'] === styleId) {
const found = {
level,
ilvl: Number(level.attributes?.['w:ilvl']) || 0,
ilvl: Number(level.attributes?.['w:ilvl']) || 0
};
return found;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ export function parseMarks(property, unknownMarks = [], docx = null) {
const newMark = { type: m.type };

const exceptionMarks = ['w:b', 'w:caps'];
if ((attributes['w:val'] === '0' || attributes['w:val'] === 'none') && !exceptionMarks.includes(m.name)) {
if (
(attributes['w:val'] === '0' || attributes['w:val'] === 'none')
&& !exceptionMarks.includes(m.name)
) {
return;
}

Expand Down Expand Up @@ -73,7 +76,7 @@ export function parseMarks(property, unknownMarks = [], docx = null) {
newMark.attrs = {};
newMark.attrs[m.property] = value;
}

marks.push(newMark);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,13 @@ const getDefaultParagraphStyle = (docx, styleId = '') => {
const { attributes: pPrNormalIndentAttr } = pPrNormalIndentTag;
const { attributes: pPrByIdIndentAttr } = pPrStyleIdIndentTag;

const spacingRest = isNormalAsDefault
? pPrNormalSpacingAttr || pPrDefaultSpacingAttr
: pPrDefaultSpacingAttr || pPrNormalSpacingAttr;
const spacingRest = isNormalAsDefault
? (pPrNormalSpacingAttr || pPrDefaultSpacingAttr)
: (pPrDefaultSpacingAttr || pPrNormalSpacingAttr);

const indentRest = isNormalAsDefault
? pPrNormalIndentAttr || pPrDefaultIndentAttr
: pPrDefaultIndentAttr || pPrNormalIndentAttr;
? (pPrNormalIndentAttr || pPrDefaultIndentAttr)
: (pPrDefaultIndentAttr || pPrNormalIndentAttr);

return {
spacing: pPrByIdSpacingAttr || spacingRest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ const getStylesFromLinkedStyles = ({ node, pos, editor }) => {
const { state } = editor.view;
const linkedStyles = LinkedStylesPluginKey.getState(state)?.decorations;
const decorationsInPlace = linkedStyles?.find(pos, pos + node.nodeSize);
// We are looking from the end as there may be several decorations
// We are looking from the end as there may be several decorations
// and we need to find the most specific one.
const styleDeco = decorationsInPlace?.findLast((dec) => dec.type.attrs?.style);
const style = styleDeco?.type.attrs?.style;
Expand Down
2 changes: 1 addition & 1 deletion packages/super-editor/src/extensions/table/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export const Table = Node.create({
return { style: `margin: 0 auto` };
}
if (attrs.justification === 'right') {
return { style: `margin-left: auto` };
return { style: `margin-left: auto` };
}

return {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const TextTransform = Extension.create({
default: null,
renderDOM: (attrs) => {
if (!attrs.textCase) return {};
return {
return {
style: `text-transform: ${attrs.textCase}`,
};
},
Expand Down
Loading