Skip to content

refactor: resolve vscode type errors in wakatime card render and remove redundant css #3232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
113 changes: 112 additions & 1 deletion src/cards/stats-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
kFormatter,
measureText,
} from "../common/utils.js";
import { getStyles } from "../getStyles.js";
import { statCardLocales } from "../translations.js";

const CARD_MIN_WIDTH = 287;
Expand Down Expand Up @@ -76,6 +75,118 @@ const createTextNode = ({
`;
};

/**
* Calculates progress along the boundary of the circle i.e it's circumference.
*
* @param {number} value The rank value to calculate progress for.
* @returns {number} Progress value.
*/
const calculateCircleProgress = (value) => {
const radius = 40;
const c = Math.PI * (radius * 2);

if (value < 0) {
value = 0;
}
if (value > 100) {
value = 100;
}

return ((100 - value) / 100) * c;
};

/**
* Retrieves the animation to display progress along the circumference of circle
* from the beginning to the given value in a clockwise direction.
*
* @param {{progress: number}} progress The progress value to animate to.
* @returns {string} Progress animation css.
*/
const getProgressAnimation = ({ progress }) => {
return `
@keyframes rankAnimation {
from {
stroke-dashoffset: ${calculateCircleProgress(0)};
}
to {
stroke-dashoffset: ${calculateCircleProgress(progress)};
}
}
`;
};

/**
* Retrieves CSS styles for a card.
*
* @param {Object} colors The colors to use for the card.
* @param {string} colors.titleColor The title color.
* @param {string} colors.textColor The text color.
* @param {string} colors.iconColor The icon color.
* @param {string} colors.ringColor The ring color.
* @param {boolean} colors.show_icons Whether to show icons.
* @param {number} colors.progress The progress value to animate to.
* @returns {string} Card CSS styles.
*/
const getStyles = ({
// eslint-disable-next-line no-unused-vars
titleColor,
textColor,
iconColor,
ringColor,
show_icons,
progress,
}) => {
return `
.stat {
font: 600 14px 'Segoe UI', Ubuntu, "Helvetica Neue", Sans-Serif; fill: ${textColor};
}
@supports(-moz-appearance: auto) {
/* Selector detects Firefox */
.stat { font-size:12px; }
}
.stagger {
opacity: 0;
animation: fadeInAnimation 0.3s ease-in-out forwards;
}
.rank-text {
font: 800 24px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${textColor};
animation: scaleInAnimation 0.3s ease-in-out forwards;
}
.rank-percentile-header {
font-size: 14px;
}
.rank-percentile-text {
font-size: 16px;
}

.not_bold { font-weight: 400 }
.bold { font-weight: 700 }
.icon {
fill: ${iconColor};
display: ${!!show_icons ? "block" : "none"};
}

.rank-circle-rim {
stroke: ${ringColor};
fill: none;
stroke-width: 6;
opacity: 0.2;
}
.rank-circle {
stroke: ${ringColor};
stroke-dasharray: 250;
fill: none;
stroke-width: 6;
stroke-linecap: round;
opacity: 0.8;
transform-origin: -10px 8px;
transform: rotate(-90deg);
animation: rankAnimation 1s forwards ease-in-out;
}
${process.env.NODE_ENV === "test" ? "" : getProgressAnimation({ progress })}
`;
};

/**
* @typedef {import('../fetchers/types').StatsData} StatsData
* @typedef {import('./types').StatCardOptions} StatCardOptions
Expand Down
32 changes: 30 additions & 2 deletions src/cards/wakatime-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
getCardColors,
lowercaseTrim,
} from "../common/utils.js";
import { getStyles } from "../getStyles.js";
import { wakatimeCardLocales } from "../translations.js";

/** Import language colors.
Expand Down Expand Up @@ -158,6 +157,36 @@ const recalculatePercentages = (languages) => {
});
};

/**
* Retrieves CSS styles for a card.
*
* @param {Object} colors The colors to use for the card.
* @param {string} colors.titleColor The title color.
* @param {string} colors.textColor The text color.
* @returns {string} Card CSS styles.
*/
const getStyles = ({
// eslint-disable-next-line no-unused-vars
titleColor,
textColor,
}) => {
return `
.stat {
font: 600 14px 'Segoe UI', Ubuntu, "Helvetica Neue", Sans-Serif; fill: ${textColor};
}
@supports(-moz-appearance: auto) {
/* Selector detects Firefox */
.stat { font-size:12px; }
}
.stagger {
opacity: 0;
animation: fadeInAnimation 0.3s ease-in-out forwards;
}
.not_bold { font-weight: 400 }
.bold { font-weight: 700 }
`;
};

/**
* @typedef {import('../fetchers/types').WakaTimeData} WakaTimeData
* @typedef {import('./types').WakaTimeOptions} WakaTimeOptions
Expand Down Expand Up @@ -235,7 +264,6 @@ const renderWakatimeCard = (stats = {}, options = { hide: [] }) => {
const cssStyles = getStyles({
titleColor,
textColor,
iconColor,
});

let finalLayout = "";
Expand Down
30 changes: 28 additions & 2 deletions src/common/Card.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getAnimations } from "../getStyles.js";
import { encodeHTML, flexLayout } from "./utils.js";

class Card {
Expand Down Expand Up @@ -173,6 +172,33 @@ class Card {
: "";
}

/**
* Retrieves css animations for a card.
*
* @returns {string} Animation css.
*/
getAnimations = () => {
return `
/* Animations */
@keyframes scaleInAnimation {
from {
transform: translate(-5px, 5px) scale(0);
}
to {
transform: translate(-5px, 5px) scale(1);
}
}
@keyframes fadeInAnimation {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
`;
};

/**
* @param {string} body The inner body of the card.
* @returns {string} The rendered card.
Expand Down Expand Up @@ -202,7 +228,7 @@ class Card {
}
${this.css}

${process.env.NODE_ENV === "test" ? "" : getAnimations()}
${process.env.NODE_ENV === "test" ? "" : this.getAnimations()}
${
this.animations === false
? `* { animation-duration: 0s !important; animation-delay: 0s !important; }`
Expand Down
142 changes: 0 additions & 142 deletions src/getStyles.js

This file was deleted.

1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from "./common/index.js";
export * from "./cards/index.js";
export { getStyles, getAnimations } from "./getStyles.js";
Loading