Skip to content

Commit de957c3

Browse files
davidfan168Aeolun
authored andcommitted
refactoring attempt
1 parent 71d1374 commit de957c3

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

src/index.tsx

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -589,28 +589,7 @@ class DiffViewer extends React.Component<
589589
};
590590
};
591591

592-
public render = (): ReactElement => {
593-
const {
594-
oldValue,
595-
newValue,
596-
useDarkTheme,
597-
leftTitle,
598-
rightTitle,
599-
splitView,
600-
compareMethod,
601-
hideLineNumbers,
602-
nonce,
603-
} = this.props;
604-
605-
if (typeof(compareMethod) === 'string' && compareMethod !== DiffMethod.JSON) {
606-
if (typeof oldValue !== 'string' || typeof newValue !== 'string') {
607-
throw Error('"oldValue" and "newValue" should be strings');
608-
}
609-
}
610-
611-
this.styles = this.computeStyles(this.props.styles, useDarkTheme, nonce);
612-
const nodes = this.renderDiff();
613-
592+
private calculateColSpans = (hideLineNumbers : boolean) => {
614593
let colSpanOnSplitView = 3;
615594
let colSpanOnInlineView = 4;
616595

@@ -624,6 +603,10 @@ class DiffViewer extends React.Component<
624603
colSpanOnInlineView += 1;
625604
}
626605

606+
return [colSpanOnSplitView, colSpanOnInlineView]
607+
}
608+
609+
private calculateChanges = (nodes) => {
627610
let deletions = 0, additions = 0
628611
nodes.lineInformation.forEach((l) => {
629612
if (l.left.type === DiffType.ADDED) {
@@ -639,6 +622,34 @@ class DiffViewer extends React.Component<
639622
deletions++
640623
}
641624
})
625+
return [deletions, additions]
626+
}
627+
628+
public render = (): ReactElement => {
629+
const {
630+
oldValue,
631+
newValue,
632+
useDarkTheme,
633+
leftTitle,
634+
rightTitle,
635+
splitView,
636+
compareMethod,
637+
hideLineNumbers,
638+
nonce,
639+
} = this.props;
640+
641+
if (typeof(compareMethod) === 'string' && compareMethod !== DiffMethod.JSON) {
642+
if (typeof oldValue !== 'string' || typeof newValue !== 'string') {
643+
throw Error('"oldValue" and "newValue" should be strings');
644+
}
645+
}
646+
647+
this.styles = this.computeStyles(this.props.styles, useDarkTheme, nonce);
648+
const nodes = this.renderDiff();
649+
650+
const [colSpanOnSplitView, colSpanOnInlineView] = this.calculateColSpans(hideLineNumbers)
651+
652+
const [deletions, additions] = this.calculateChanges(nodes)
642653
const totalChanges = deletions + additions
643654

644655
const percentageAddition = Math.round((additions / totalChanges) * 100)

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"experimentalDecorators": true,
44
"jsx": "react-jsx",
55
"module": "commonjs",
6+
"strict": true,
67
"moduleResolution": "node",
78
"noImplicitAny": true,
89
"esModuleInterop": true,

0 commit comments

Comments
 (0)