Skip to content

Commit

Permalink
Margin: Check Zero or Truth Values for Margin (#29)
Browse files Browse the repository at this point in the history
* refactor: remove unused import

Signed-off-by: K-Kumar-01 <[email protected]>

* feat: add util to check zero or truthy value

Signed-off-by: K-Kumar-01 <[email protected]>

* fix: margin attributes building

Signed-off-by: K-Kumar-01 <[email protected]>

* docs: update example for lists

Signed-off-by: K-Kumar-01 <[email protected]>

---------

Signed-off-by: K-Kumar-01 <[email protected]>
  • Loading branch information
K-Kumar-01 authored Jun 7, 2024
1 parent 08be315 commit 7c133d8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
32 changes: 29 additions & 3 deletions example/example-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,34 @@ const htmlString = `<!DOCTYPE html>
</ul>
</li>
<li style="font-family: arial, helvetica, sans-serif; font-size: 12pt; line-height: 1.15;"><span style="font-family: arial, helvetica, sans-serif; font-size: 12pt;">Heading 3</span></li>
</ul>
</ul>
<div>
<p>One More test case</p>
<p style="margin: 0in; line-height: normal; font-size: 11pt; font-family: Calibri, sans-serif;"><strong><span style="font-size: 10.0pt; font-family: Arial, sans-serif;">PHASE ONE</span></strong><br /><strong><span style="font-size: 10.0pt; font-family: Arial, sans-serif; color: red;">CONFIDENTIAL <br />HOURS: 1 – 2</span></strong></p>
<p style="margin: 0in; line-height: normal; font-size: 11pt; font-family: Calibri, sans-serif;"><strong><span style="font-size: 10.0pt; font-family: Arial, sans-serif; color: red;">TIER: 1 </span></strong></p>
<p style="line-height: normal; margin: 0in 0in 10pt; font-size: 11pt; font-family: Calibri, sans-serif;"><strong><span style="font-size: 10.0pt; font-family: Arial, sans-serif;">Scheduled Work</span></strong><span style="font-size: 10.0pt; font-family: Arial, sans-serif;">: Business Hours </span><br /><strong><span style="font-size: 10.0pt; font-family: Arial, sans-serif;">Location:</span></strong>
<span style="font-size: 10.0pt; font-family: Arial, sans-serif;"> Remote</span>
</p>
<ul style="margin-bottom: 0in; margin-top: 0px;">
<li style="line-height: normal; margin: 0in 0in 0in 0px; font-size: 11pt; font-family: Calibri, sans-serif;"><span style="font-size: 9.0pt; font-family: Arial, sans-serif;">Item 1k</span>
<ul style="list-style-type: circle; margin-bottom: 0in; margin-top: 0px;">
<li style="line-height: normal; margin: 0in 0in 0in 0px; font-size: 11pt; font-family: Calibri, sans-serif;"><span style="font-size: 9.0pt; font-family: Arial, sans-serif;">Sub item 1</span></li>
<li style="line-height: normal; margin: 0in 0in 0in 0px; font-size: 11pt; font-family: Calibri, sans-serif;"><span style="font-size: 9.0pt; font-family: Arial, sans-serif;">Sub item 2</span></li>
<li style="line-height: normal; margin: 0in 0in 0in 0px; font-size: 11pt; font-family: Calibri, sans-serif;"><span style="font-size: 9.0pt; font-family: Arial, sans-serif;">Sub item 3</span></li>
<li style="line-height: normal; margin: 0in 0in 0in 0px; font-size: 11pt; font-family: Calibri, sans-serif;"><span style="font-size: 9.0pt; font-family: Arial, sans-serif;">Sub item 4</span></li>
<li style="line-height: normal; margin: 0in 0in 0in 0px; font-size: 11pt; font-family: Calibri, sans-serif;"><span style="font-size: 9.0pt; font-family: Arial, sans-serif;">Sub item 5</span></li>
<li style="line-height: normal; margin: 0in 0in 0in 0px; font-size: 11pt; font-family: Calibri, sans-serif;"><span style="font-size: 9.0pt; font-family: Arial, sans-serif;">Sub item 6</span></li>
</ul>
</li>
<li style="line-height: normal; margin: 0in 0in 0in 0px; font-size: 11pt; font-family: Calibri, sans-serif;"><span style="font-size: 9.0pt; font-family: Arial, sans-serif;">Item 2</span>
<ul style="list-style-type: circle; margin-bottom: 0in; margin-top: 0px;">
<li style="line-height: normal; margin: 0in 0in 0in 0px; font-size: 11pt; font-family: Calibri, sans-serif;"><span style="font-size: 9.0pt; font-family: Arial, sans-serif;">Sub item 1</span></li>
<li style="line-height: normal; margin: 0in 0in 0in 0px; font-size: 11pt; font-family: Calibri, sans-serif;"><span style="font-size: 9.0pt; font-family: Arial, sans-serif;">Sub item 2</span></li>
<li style="line-height: normal; margin: 0in 0in 10pt 0px; font-size: 11pt; font-family: Calibri, sans-serif;"><span style="font-size: 9.0pt; font-family: Arial, sans-serif;">Sub item 1</span></li>
</ul>
</li>
</ul>
</div>
</body>
</html>`;

Expand All @@ -1632,5 +1659,4 @@ const htmlString = `<!DOCTYPE html>
}
console.log('Docx file created successfully');
});
})();

})();
19 changes: 9 additions & 10 deletions src/helpers/xml-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ import {
verticalAlignValues,
imageType,
internalRelationship,
defaultPercentageMarginValue,
defaultTableBorderOptions,
defaultTableBorderAttributeOptions
} from '../constants';
import { vNodeHasChildren } from '../utils/vnode';
import { isValidUrl } from '../utils/url';
import { isZeroOrTruthy } from '../utils/truthy-check';

const setUpDirectionalBorderStroke = (borderStrike = 'nil') => ({
top: borderStrike,
Expand Down Expand Up @@ -335,7 +335,7 @@ const fixupLineHeight = (lineHeight, fontSize) => {
if (!isNaN(lineHeight)) {
if (fontSize) {
const actualLineHeight = +lineHeight * fontSize;

return HIPToTWIP(actualLineHeight);
} else {
// 240 TWIP or 12 point is default line height
Expand All @@ -356,7 +356,7 @@ const fixupLineHeight = (lineHeight, fontSize) => {
} else if (percentageRegex.test(lineHeight)) {
const matchedParts = lineHeight.match(percentageRegex);
return HIPToTWIP((matchedParts[1] * fontSize) / 100);
}
}
else {
// 240 TWIP or 12 point is default line height
return 240;
Expand Down Expand Up @@ -522,7 +522,6 @@ const cssBorderParser = (borderString, defaultBorderOptions = { ...defaultTableB
return [size, stroke, color];
};


const modifiedStyleAttributesBuilder = (docxDocumentInstance, vNode, attributes, options) => {
const modifiedAttributes = { ...attributes };

Expand Down Expand Up @@ -602,23 +601,23 @@ const modifiedStyleAttributesBuilder = (docxDocumentInstance, vNode, attributes,

const { left, right, bottom } = margins
const indentation = { left, right }
if (left || right) {
if (isZeroOrTruthy(left) || isZeroOrTruthy(right)) {
modifiedAttributes.indentation = indentation;
}
if (bottom) {
if (isZeroOrTruthy(bottom)) {
modifiedAttributes.afterSpacing = bottom;
}
} else if (vNodeStyleKey === 'margin-left' || vNodeStyleKey === 'margin-right') {
const leftMargin = fixupMargin(vNodeStyle['margin-left']);
const rightMargin = fixupMargin(vNodeStyle['margin-right']);
const indentation = {};
if (leftMargin) {
if (isZeroOrTruthy(leftMargin)) {
indentation.left = leftMargin;
}
if (rightMargin) {
if (isZeroOrTruthy(rightMargin)) {
indentation.right = rightMargin;
}
if (leftMargin || rightMargin) {
if (isZeroOrTruthy(leftMargin) || isZeroOrTruthy(rightMargin)) {
modifiedAttributes.indentation = indentation;
}
} else if (vNodeStyleKey === 'margin-bottom') {
Expand Down Expand Up @@ -759,7 +758,7 @@ const buildRunProperties = (attributes) => {
if (key === 'textDecoration') {
options.textDecoration = attributes[key];
}

if (key === 'textShadow') {
options.textShadow = attributes[key];
}
Expand Down
3 changes: 3 additions & 0 deletions src/utils/truthy-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const isZeroOrTruthy = (value) => {
return value === 0 || !!value;
}

0 comments on commit 7c133d8

Please sign in to comment.