Skip to content

Commit a11169e

Browse files
refactor: move create progress node function into render module (anuraghazra#4594)
Co-authored-by: Alexandr <qwerty541zxc@gmail.com>
1 parent f5de29f commit a11169e

5 files changed

Lines changed: 48 additions & 51 deletions

File tree

src/cards/top-languages.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
import { Card } from "../common/Card.js";
44
import { getCardColors } from "../common/color.js";
5-
import { createProgressNode } from "../common/createProgressNode.js";
65
import { formatBytes } from "../common/fmt.js";
76
import { I18n } from "../common/I18n.js";
87
import { chunkArray, clampValue, lowercaseTrim } from "../common/ops.js";
9-
import { flexLayout, measureText } from "../common/render.js";
8+
import {
9+
createProgressNode,
10+
flexLayout,
11+
measureText,
12+
} from "../common/render.js";
1013
import { langCardLocales } from "../translations.js";
1114

1215
const DEFAULT_CARD_WIDTH = 300;

src/cards/wakatime.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
import { Card } from "../common/Card.js";
44
import { getCardColors } from "../common/color.js";
5-
import { createProgressNode } from "../common/createProgressNode.js";
65
import { I18n } from "../common/I18n.js";
76
import { clampValue, lowercaseTrim } from "../common/ops.js";
8-
import { flexLayout } from "../common/render.js";
7+
import { createProgressNode, flexLayout } from "../common/render.js";
98
import { wakatimeCardLocales } from "../translations.js";
109

1110
/** Import language colors.

src/common/createProgressNode.js

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/common/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
export { blacklist } from "./blacklist.js";
44
export { Card } from "./Card.js";
5-
export { createProgressNode } from "./createProgressNode.js";
65
export { I18n } from "./I18n.js";
76
export { icons } from "./icons.js";
87
export { retryer } from "./retryer.js";

src/common/render.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { SECONDARY_ERROR_MESSAGES, TRY_AGAIN_LATER } from "./error.js";
44
import { getCardColors } from "./color.js";
55
import { encodeHTML } from "./html.js";
6+
import { clampValue } from "./ops.js";
67

78
/**
89
* Auto layout utility, allows us to layout things vertically or horizontally with
@@ -45,6 +46,46 @@ const createLanguageNode = (langName, langColor) => {
4546
`;
4647
};
4748

49+
/**
50+
* Create a node to indicate progress in percentage along a horizontal line.
51+
*
52+
* @param {Object} params Object that contains the createProgressNode parameters.
53+
* @param {number} params.x X-axis position.
54+
* @param {number} params.y Y-axis position.
55+
* @param {number} params.width Width of progress bar.
56+
* @param {string} params.color Progress color.
57+
* @param {number} params.progress Progress value.
58+
* @param {string} params.progressBarBackgroundColor Progress bar bg color.
59+
* @param {number} params.delay Delay before animation starts.
60+
* @returns {string} Progress node.
61+
*/
62+
const createProgressNode = ({
63+
x,
64+
y,
65+
width,
66+
color,
67+
progress,
68+
progressBarBackgroundColor,
69+
delay,
70+
}) => {
71+
const progressPercentage = clampValue(progress, 2, 100);
72+
73+
return `
74+
<svg width="${width}" x="${x}" y="${y}">
75+
<rect rx="5" ry="5" x="0" y="0" width="${width}" height="8" fill="${progressBarBackgroundColor}"></rect>
76+
<svg data-testid="lang-progress" width="${progressPercentage}%">
77+
<rect
78+
height="8"
79+
fill="${color}"
80+
rx="5" ry="5" x="0" y="0"
81+
class="lang-progress"
82+
style="animation-delay: ${delay}ms;"
83+
/>
84+
</svg>
85+
</svg>
86+
`;
87+
};
88+
4889
/**
4990
* Creates an icon with label to display repository/gist stats like forks, stars, etc.
5091
*
@@ -191,6 +232,7 @@ export {
191232
ERROR_CARD_LENGTH,
192233
renderError,
193234
createLanguageNode,
235+
createProgressNode,
194236
iconWithLabel,
195237
flexLayout,
196238
measureText,

0 commit comments

Comments
 (0)