@stats-forge/github-stats-forge-core 0.0.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -10
- package/build/api/api-result.d.ts +6 -7
- package/build/api/api-result.d.ts.map +1 -1
- package/build/api/api-result.js +4 -5
- package/build/api/contributed-to.d.ts +23 -0
- package/build/api/contributed-to.d.ts.map +1 -0
- package/build/api/contributed-to.js +43 -0
- package/build/api/gist.d.ts +8 -22
- package/build/api/gist.d.ts.map +1 -1
- package/build/api/gist.js +14 -36
- package/build/api/handler.d.ts +16 -0
- package/build/api/handler.d.ts.map +1 -0
- package/build/api/handler.js +29 -0
- package/build/api/index.d.ts +13 -12
- package/build/api/index.d.ts.map +1 -1
- package/build/api/index.js +1 -0
- package/build/api/params.d.ts +21 -12
- package/build/api/params.d.ts.map +1 -1
- package/build/api/params.js +41 -19
- package/build/api/pin.d.ts +11 -31
- package/build/api/pin.d.ts.map +1 -1
- package/build/api/pin.js +35 -63
- package/build/api/stats.d.ts +15 -16
- package/build/api/stats.d.ts.map +1 -1
- package/build/api/stats.js +60 -99
- package/build/api/top-langs.d.ts +13 -18
- package/build/api/top-langs.d.ts.map +1 -1
- package/build/api/top-langs.js +35 -72
- package/build/api/wakatime.d.ts +11 -17
- package/build/api/wakatime.d.ts.map +1 -1
- package/build/api/wakatime.js +27 -64
- package/build/calculateRank.d.ts +0 -9
- package/build/calculateRank.d.ts.map +1 -1
- package/build/calculateRank.js +12 -17
- package/build/cards/contributed-to.d.ts +19 -0
- package/build/cards/contributed-to.d.ts.map +1 -0
- package/build/cards/contributed-to.js +242 -0
- package/build/cards/gist.d.ts +3 -4
- package/build/cards/gist.d.ts.map +1 -1
- package/build/cards/gist.js +38 -43
- package/build/cards/options.d.ts +1 -1
- package/build/cards/repo.d.ts +14 -5
- package/build/cards/repo.d.ts.map +1 -1
- package/build/cards/repo.js +93 -103
- package/build/cards/stats.d.ts +18 -11
- package/build/cards/stats.d.ts.map +1 -1
- package/build/cards/stats.js +191 -222
- package/build/cards/top-languages.d.ts +18 -33
- package/build/cards/top-languages.d.ts.map +1 -1
- package/build/cards/top-languages.js +196 -387
- package/build/cards/wakatime.d.ts +17 -8
- package/build/cards/wakatime.d.ts.map +1 -1
- package/build/cards/wakatime.js +133 -187
- package/build/common/Card.d.ts +20 -59
- package/build/common/Card.d.ts.map +1 -1
- package/build/common/Card.js +131 -198
- package/build/common/I18n.d.ts +1 -7
- package/build/common/I18n.d.ts.map +1 -1
- package/build/common/I18n.js +7 -8
- package/build/common/brand.d.ts +76 -0
- package/build/common/brand.d.ts.map +1 -0
- package/build/common/brand.js +73 -0
- package/build/common/color.d.ts +4 -17
- package/build/common/color.d.ts.map +1 -1
- package/build/common/color.js +67 -49
- package/build/common/config.d.ts +4 -14
- package/build/common/config.d.ts.map +1 -1
- package/build/common/config.js +3 -16
- package/build/common/constants.d.ts +6 -2
- package/build/common/constants.d.ts.map +1 -1
- package/build/common/constants.js +5 -1
- package/build/common/date.d.ts +50 -5
- package/build/common/date.d.ts.map +1 -1
- package/build/common/date.js +104 -4
- package/build/common/emojiMap.d.ts +8 -0
- package/build/common/emojiMap.d.ts.map +1 -0
- package/build/common/emojiMap.js +10 -0
- package/build/common/emojiMap.json +1915 -0
- package/build/common/error.d.ts +1 -10
- package/build/common/error.d.ts.map +1 -1
- package/build/common/error.js +1 -10
- package/build/common/fmt.d.ts +3 -15
- package/build/common/fmt.d.ts.map +1 -1
- package/build/common/fmt.js +7 -35
- package/build/common/http.d.ts +2 -7
- package/build/common/http.d.ts.map +1 -1
- package/build/common/http.js +8 -17
- package/build/common/icons.d.ts +20 -20
- package/build/common/icons.d.ts.map +1 -1
- package/build/common/icons.js +108 -67
- package/build/common/languageColors.d.ts +0 -1
- package/build/common/languageColors.d.ts.map +1 -1
- package/build/common/languageColors.js +1 -4
- package/build/common/languageColors.json +18 -2
- package/build/common/ops.d.ts +11 -17
- package/build/common/ops.d.ts.map +1 -1
- package/build/common/ops.js +22 -39
- package/build/common/render.d.ts +34 -82
- package/build/common/render.d.ts.map +1 -1
- package/build/common/render.js +225 -259
- package/build/common/retryer.d.ts +2 -7
- package/build/common/retryer.d.ts.map +1 -1
- package/build/common/retryer.js +4 -11
- package/build/fetchers/contributed-to.d.ts +62 -0
- package/build/fetchers/contributed-to.d.ts.map +1 -0
- package/build/fetchers/contributed-to.js +184 -0
- package/build/fetchers/gist.d.ts +2 -5
- package/build/fetchers/gist.d.ts.map +1 -1
- package/build/fetchers/gist.js +1 -5
- package/build/fetchers/graphql-error.d.ts +13 -0
- package/build/fetchers/graphql-error.d.ts.map +1 -0
- package/build/fetchers/graphql-error.js +29 -0
- package/build/fetchers/index.d.ts +13 -12
- package/build/fetchers/index.d.ts.map +1 -1
- package/build/fetchers/index.js +1 -0
- package/build/fetchers/repo.d.ts +2 -11
- package/build/fetchers/repo.d.ts.map +1 -1
- package/build/fetchers/repo.js +20 -33
- package/build/fetchers/stats.d.ts +16 -40
- package/build/fetchers/stats.d.ts.map +1 -1
- package/build/fetchers/stats.js +89 -271
- package/build/fetchers/top-languages.d.ts +2 -9
- package/build/fetchers/top-languages.d.ts.map +1 -1
- package/build/fetchers/top-languages.js +16 -47
- package/build/fetchers/types.d.ts +25 -1
- package/build/fetchers/types.d.ts.map +1 -1
- package/build/fetchers/wakatime.d.ts +2 -6
- package/build/fetchers/wakatime.d.ts.map +1 -1
- package/build/fetchers/wakatime.js +1 -5
- package/build/graphql/contributionsCollection.d.ts +9 -0
- package/build/graphql/contributionsCollection.d.ts.map +1 -0
- package/build/graphql/contributionsCollection.js +12 -0
- package/build/graphql/contributionsDocument.d.ts +6 -6
- package/build/graphql/contributionsDocument.d.ts.map +1 -1
- package/build/graphql/contributionsDocument.js +8 -16
- package/build/graphql/generated/contributed-to.d.ts +22 -0
- package/build/graphql/generated/contributed-to.d.ts.map +1 -0
- package/build/graphql/generated/contributed-to.js +11 -0
- package/build/graphql/generated/gist.d.ts +1 -1
- package/build/graphql/generated/repo.d.ts +1 -1
- package/build/graphql/generated/stats.d.ts +16 -4
- package/build/graphql/generated/stats.d.ts.map +1 -1
- package/build/graphql/generated/stats.js +2 -2
- package/build/graphql/generated/top-languages.d.ts +2 -2
- package/build/graphql/graphqlDocument.d.ts +1 -2
- package/build/graphql/graphqlDocument.d.ts.map +1 -1
- package/build/graphql/graphqlDocument.js +0 -1
- package/build/graphql/reposContributedToDocument.d.ts +4 -5
- package/build/graphql/reposContributedToDocument.d.ts.map +1 -1
- package/build/graphql/reposContributedToDocument.js +11 -7
- package/build/markup/css.d.ts +35 -0
- package/build/markup/css.d.ts.map +1 -0
- package/build/markup/css.js +64 -0
- package/build/markup/element.d.ts +41 -0
- package/build/markup/element.d.ts.map +1 -0
- package/build/markup/element.js +127 -0
- package/build/markup/html.d.ts +13 -0
- package/build/markup/html.d.ts.map +1 -0
- package/build/markup/html.js +27 -0
- package/build/markup/index.d.ts +9 -0
- package/build/markup/index.d.ts.map +1 -0
- package/build/markup/index.js +6 -0
- package/build/markup/serialize.d.ts +16 -0
- package/build/markup/serialize.d.ts.map +1 -0
- package/build/markup/serialize.js +17 -0
- package/build/themes/index.d.ts +332 -333
- package/build/themes/index.d.ts.map +1 -1
- package/build/themes/index.js +1 -1
- package/build/translations.d.ts +66 -7
- package/build/translations.d.ts.map +1 -1
- package/build/translations.js +686 -629
- package/package.json +17 -26
- package/src/api/api-result.ts +10 -9
- package/src/api/contributed-to.ts +84 -0
- package/src/api/gist.ts +22 -46
- package/src/api/handler.ts +42 -0
- package/src/api/index.ts +13 -12
- package/src/api/params.ts +74 -29
- package/src/api/pin.ts +38 -65
- package/src/api/stats.ts +81 -118
- package/src/api/top-langs.ts +38 -80
- package/src/api/wakatime.ts +35 -73
- package/src/calculateRank.ts +12 -23
- package/src/cards/contributed-to.ts +373 -0
- package/src/cards/gist.ts +55 -49
- package/src/cards/options.ts +1 -1
- package/src/cards/repo.ts +134 -120
- package/src/cards/stats.ts +262 -252
- package/src/cards/top-languages.ts +315 -422
- package/src/cards/wakatime.ts +207 -225
- package/src/common/Card.ts +207 -214
- package/src/common/I18n.ts +7 -8
- package/src/common/brand.ts +109 -0
- package/src/common/color.ts +80 -57
- package/src/common/config.ts +5 -18
- package/src/common/constants.ts +8 -2
- package/src/common/date.ts +135 -5
- package/src/common/emojiMap.json +1915 -0
- package/src/common/emojiMap.ts +13 -0
- package/src/common/error.ts +2 -11
- package/src/common/fmt.ts +7 -44
- package/src/common/http.ts +9 -15
- package/src/common/icons.ts +122 -42
- package/src/common/languageColors.json +18 -2
- package/src/common/languageColors.ts +1 -4
- package/src/common/ops.ts +31 -48
- package/src/common/render.ts +334 -284
- package/src/common/retryer.ts +8 -15
- package/src/fetchers/contributed-to.ts +271 -0
- package/src/fetchers/gist.ts +8 -12
- package/src/fetchers/graphql-error.ts +36 -0
- package/src/fetchers/index.ts +15 -12
- package/src/fetchers/repo.ts +27 -40
- package/src/fetchers/stats.ts +157 -366
- package/src/fetchers/top-languages.ts +27 -55
- package/src/fetchers/types.ts +27 -1
- package/src/fetchers/wakatime.ts +5 -9
- package/src/graphql/contributionsCollection.ts +18 -0
- package/src/graphql/contributionsDocument.ts +14 -19
- package/src/graphql/generated/contributed-to.ts +25 -0
- package/src/graphql/generated/gist.ts +1 -1
- package/src/graphql/generated/repo.ts +1 -1
- package/src/graphql/generated/stats.ts +21 -8
- package/src/graphql/generated/top-languages.ts +2 -2
- package/src/graphql/graphqlDocument.ts +1 -2
- package/src/graphql/queries/contributed-to.graphql +8 -0
- package/src/graphql/queries/stats.graphql +14 -2
- package/src/graphql/reposContributedToDocument.ts +16 -15
- package/src/markup/css.ts +118 -0
- package/src/markup/element.ts +246 -0
- package/src/markup/html.ts +34 -0
- package/src/markup/index.ts +9 -0
- package/src/markup/serialize.ts +27 -0
- package/src/themes/index.ts +1 -1
- package/src/translations.ts +699 -629
- package/build/cards/index.d.ts +0 -11
- package/build/cards/index.d.ts.map +0 -1
- package/build/cards/index.js +0 -7
- package/build/common/html.d.ts +0 -8
- package/build/common/html.d.ts.map +0 -1
- package/build/common/html.js +0 -14
- package/build/index.d.ts +0 -15
- package/build/index.d.ts.map +0 -1
- package/build/index.js +0 -10
- package/src/_emoji-name-map.d.ts +0 -10
- package/src/_github-username-regex.d.ts +0 -4
- package/src/cards/index.ts +0 -21
- package/src/common/html.ts +0 -17
- package/src/index.ts +0 -18
package/build/common/render.js
CHANGED
|
@@ -1,66 +1,50 @@
|
|
|
1
|
+
import { el, renderMarkup, rule, style } from '../markup/index.js';
|
|
1
2
|
import { getCardColors, isPrefixedHexColor } from './color.js';
|
|
2
3
|
import { SECONDARY_ERROR_MESSAGES, TRY_AGAIN_LATER } from './error.js';
|
|
3
4
|
import { kFormatter } from './fmt.js';
|
|
4
|
-
import { encodeHTML } from './html.js';
|
|
5
5
|
import { clampValue } from './ops.js';
|
|
6
6
|
/**
|
|
7
7
|
* Auto layout utility, allows us to layout things vertically or horizontally with
|
|
8
8
|
* proper gaping.
|
|
9
9
|
*
|
|
10
|
-
* The
|
|
11
|
-
*
|
|
12
|
-
* @param props Function properties.
|
|
13
|
-
* @param props.items Array of sanitized items to layout.
|
|
14
|
-
* @param props.gap Gap between items.
|
|
15
|
-
* @param props.direction Direction to layout items.
|
|
16
|
-
* @param props.sizes Array of sizes for each item.
|
|
17
|
-
* @returns Array of items with proper layout.
|
|
10
|
+
* @returns The items, each in the group that positions it.
|
|
18
11
|
*/
|
|
19
12
|
const flexLayout = ({ items, gap, direction, sizes = [], }) => {
|
|
20
13
|
if (sizes.some((size) => !Number.isFinite(size))) {
|
|
21
14
|
throw new Error('flexLayout: `sizes` must contain only numbers');
|
|
22
15
|
}
|
|
16
|
+
const laidOut = items.filter(Boolean);
|
|
17
|
+
// A lone item is already where it belongs, so it needs no group to move it.
|
|
18
|
+
if (laidOut.length === 1) {
|
|
19
|
+
return laidOut;
|
|
20
|
+
}
|
|
23
21
|
let lastSize = 0;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
22
|
+
return laidOut.map((item, i) => {
|
|
23
|
+
const offset = lastSize;
|
|
24
|
+
lastSize += (sizes[i] ?? 0) + gap;
|
|
25
|
+
// The first item never moves, so it gets no transform to carry.
|
|
26
|
+
const transform = offset === 0
|
|
27
|
+
? undefined
|
|
28
|
+
: direction === 'column'
|
|
29
|
+
? `translate(0, ${offset})`
|
|
30
|
+
: `translate(${offset}, 0)`;
|
|
31
|
+
return el('g', { transform }, item);
|
|
33
32
|
});
|
|
34
33
|
};
|
|
35
34
|
/**
|
|
36
35
|
* Creates a node to display the primary programming language of the repository/gist.
|
|
37
36
|
*
|
|
38
|
-
* @param langName Language name.
|
|
39
|
-
* @param langColor Language color.
|
|
40
37
|
* @returns Language display SVG object.
|
|
41
38
|
*/
|
|
42
39
|
const createLanguageNode = (langName, langColor) => {
|
|
43
40
|
if (!isPrefixedHexColor(langColor)) {
|
|
44
41
|
throw new Error(`Invalid language color: "${langColor}"`);
|
|
45
42
|
}
|
|
46
|
-
return
|
|
47
|
-
<g data-testid="primary-lang">
|
|
48
|
-
<circle data-testid="lang-color" cx="0" cy="-5" r="6" fill="${langColor}" />
|
|
49
|
-
<text data-testid="lang-name" class="gray" x="15">${encodeHTML(langName)}</text>
|
|
50
|
-
</g>
|
|
51
|
-
`;
|
|
43
|
+
return el('g', { 'data-testid': 'primary-lang' }, el('circle', { 'data-testid': 'lang-color', cx: 0, cy: -5, r: 6, fill: langColor }), el('text', { 'data-testid': 'lang-name', class: 'gray', x: 15 }, langName));
|
|
52
44
|
};
|
|
53
45
|
/**
|
|
54
46
|
* Create a node to indicate progress in percentage along a horizontal line.
|
|
55
47
|
*
|
|
56
|
-
* @param params Object that contains the createProgressNode parameters.
|
|
57
|
-
* @param params.x X-axis position.
|
|
58
|
-
* @param params.y Y-axis position.
|
|
59
|
-
* @param params.width Width of progress bar.
|
|
60
|
-
* @param params.color Optional foreground color as an inline fill fallback.
|
|
61
|
-
* When omitted, the color must be set via a `.lang-progress` CSS rule.
|
|
62
|
-
* @param params.progress Progress value.
|
|
63
|
-
* @param params.delay Delay before animation starts.
|
|
64
48
|
* @returns Progress node.
|
|
65
49
|
*/
|
|
66
50
|
const createProgressNode = ({ x, y, width, color, progress, delay, }) => {
|
|
@@ -68,71 +52,67 @@ const createProgressNode = ({ x, y, width, color, progress, delay, }) => {
|
|
|
68
52
|
throw new Error(`Invalid progress color: "${color}"`);
|
|
69
53
|
}
|
|
70
54
|
if (!Number.isFinite(width)) {
|
|
71
|
-
throw new
|
|
55
|
+
throw new TypeError(`Invalid width: "${width}"`);
|
|
72
56
|
}
|
|
73
57
|
if (!Number.isFinite(x)) {
|
|
74
|
-
throw new
|
|
58
|
+
throw new TypeError(`Invalid x: "${x}"`);
|
|
75
59
|
}
|
|
76
60
|
if (!Number.isFinite(y)) {
|
|
77
|
-
throw new
|
|
61
|
+
throw new TypeError(`Invalid y: "${y}"`);
|
|
78
62
|
}
|
|
79
63
|
if (!Number.isFinite(delay)) {
|
|
80
|
-
throw new
|
|
64
|
+
throw new TypeError(`Invalid delay: "${delay}"`);
|
|
81
65
|
}
|
|
82
66
|
const progressPercentage = clampValue(progress, 2, 100);
|
|
83
|
-
return
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
67
|
+
return el('svg', { width, x, y }, el('rect', {
|
|
68
|
+
'data-testid': 'progress-background',
|
|
69
|
+
class: 'progress-background',
|
|
70
|
+
rx: 5,
|
|
71
|
+
ry: 5,
|
|
72
|
+
x: 0,
|
|
73
|
+
y: 0,
|
|
74
|
+
width,
|
|
75
|
+
height: 8,
|
|
76
|
+
}), el('svg', { 'data-testid': 'lang-progress', width: `${progressPercentage}%` }, el('rect', {
|
|
77
|
+
height: 8,
|
|
78
|
+
fill: color,
|
|
79
|
+
rx: 5,
|
|
80
|
+
ry: 5,
|
|
81
|
+
x: 0,
|
|
82
|
+
y: 0,
|
|
83
|
+
class: 'lang-progress',
|
|
84
|
+
style: `animation-delay: ${delay}ms;`,
|
|
85
|
+
})));
|
|
97
86
|
};
|
|
98
87
|
/**
|
|
99
88
|
* Renders multi-line text via a `foreignObject` so the browser performs
|
|
100
89
|
* native, font-aware wrapping. Content overflowing `lineCount` lines is
|
|
101
90
|
* clipped (with an ellipsis on the last visible line) by CSS line-clamp.
|
|
102
91
|
*
|
|
103
|
-
* @param props Function properties.
|
|
104
|
-
* @param props.text Text to render (will be HTML-encoded).
|
|
105
|
-
* @param props.x X position of the foreignObject.
|
|
106
|
-
* @param props.y Y position of the foreignObject.
|
|
107
|
-
* @param props.width Width of the wrap box.
|
|
108
|
-
* @param props.height Height of the wrap box.
|
|
109
|
-
* @param props.lineCount Maximum number of lines to display.
|
|
110
|
-
* @param props.className CSS class applied to the inner element.
|
|
111
|
-
* @param props.testId Optional test id for the inner element.
|
|
112
92
|
* @returns foreignObject SVG node.
|
|
113
93
|
*/
|
|
114
94
|
const wrappedTextNode = ({ text, x, y, width, height, lineCount, className, testId, }) => {
|
|
115
95
|
if (!Number.isFinite(x)) {
|
|
116
|
-
throw new
|
|
96
|
+
throw new TypeError(`Invalid x: "${x}"`);
|
|
117
97
|
}
|
|
118
98
|
if (!Number.isFinite(y)) {
|
|
119
|
-
throw new
|
|
99
|
+
throw new TypeError(`Invalid y: "${y}"`);
|
|
120
100
|
}
|
|
121
101
|
if (!Number.isFinite(width)) {
|
|
122
|
-
throw new
|
|
102
|
+
throw new TypeError(`Invalid width: "${width}"`);
|
|
123
103
|
}
|
|
124
104
|
if (!Number.isFinite(height)) {
|
|
125
|
-
throw new
|
|
105
|
+
throw new TypeError(`Invalid height: "${height}"`);
|
|
126
106
|
}
|
|
127
107
|
if (!Number.isFinite(lineCount)) {
|
|
128
|
-
throw new
|
|
108
|
+
throw new TypeError(`Invalid lineCount: "${lineCount}"`);
|
|
129
109
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
110
|
+
return el('foreignObject', { x, y, width, height }, el('div', {
|
|
111
|
+
xmlns: 'http://www.w3.org/1999/xhtml',
|
|
112
|
+
class: className,
|
|
113
|
+
style: `--lines: ${lineCount};`,
|
|
114
|
+
'data-testid': testId,
|
|
115
|
+
}, text));
|
|
136
116
|
};
|
|
137
117
|
/**
|
|
138
118
|
* CSS rules used to render multi-line text inside a `foreignObject`. Apply this
|
|
@@ -140,93 +120,82 @@ const wrappedTextNode = ({ text, x, y, width, height, lineCount, className, test
|
|
|
140
120
|
* browser handles wrapping and the line count is taken from the `--lines`
|
|
141
121
|
* custom property set on the element.
|
|
142
122
|
*
|
|
143
|
-
* @
|
|
144
|
-
* @returns CSS rules block (without the surrounding selector).
|
|
123
|
+
* @returns The declarations, to merge into the class's own rule.
|
|
145
124
|
*/
|
|
146
125
|
const wrappedTextStyles = (color) => {
|
|
147
126
|
if (!isPrefixedHexColor(color)) {
|
|
148
127
|
throw new Error(`Invalid text color: "${color}"`);
|
|
149
128
|
}
|
|
150
|
-
return
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
129
|
+
return {
|
|
130
|
+
color,
|
|
131
|
+
margin: 0,
|
|
132
|
+
'line-height': 1.2,
|
|
133
|
+
'overflow-wrap': 'anywhere',
|
|
134
|
+
'word-break': 'break-word',
|
|
135
|
+
display: '-webkit-box',
|
|
136
|
+
'-webkit-box-orient': 'vertical',
|
|
137
|
+
'-webkit-line-clamp': 'var(--lines)',
|
|
138
|
+
'line-clamp': 'var(--lines)',
|
|
139
|
+
overflow: 'hidden',
|
|
140
|
+
'text-overflow': 'ellipsis',
|
|
141
|
+
'padding-bottom': '0.15em',
|
|
142
|
+
};
|
|
164
143
|
};
|
|
165
144
|
/**
|
|
166
145
|
* Creates an icon with label to display repository/gist stats like forks, stars, etc.
|
|
146
|
+
* Draws nothing when the label is not positive.
|
|
167
147
|
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
* @param icon The sanitized icon to display.
|
|
171
|
-
* @param label The label to display.
|
|
172
|
-
* @param testid The testid to assign to the label.
|
|
173
|
-
* @param iconSize The size of the icon.
|
|
174
|
-
* @returns Icon with label SVG object.
|
|
148
|
+
* @returns Icon with label SVG object, or nothing when the label is not positive.
|
|
175
149
|
*/
|
|
176
150
|
const iconWithLabel = (icon, label, testid, iconSize) => {
|
|
177
151
|
if (typeof label === 'number' && label <= 0) {
|
|
178
|
-
return
|
|
152
|
+
return undefined;
|
|
179
153
|
}
|
|
180
154
|
if (!Number.isFinite(iconSize)) {
|
|
181
|
-
throw new
|
|
155
|
+
throw new TypeError(`Invalid iconSize: "${iconSize}"`);
|
|
182
156
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
157
|
+
return flexLayout({
|
|
158
|
+
items: [
|
|
159
|
+
el('svg', {
|
|
160
|
+
class: 'icon',
|
|
161
|
+
y: -12,
|
|
162
|
+
viewBox: '0 0 16 16',
|
|
163
|
+
version: '1.1',
|
|
164
|
+
width: iconSize,
|
|
165
|
+
height: iconSize,
|
|
166
|
+
}, icon),
|
|
167
|
+
el('text', { 'data-testid': testid, class: 'gray' }, String(label)),
|
|
168
|
+
],
|
|
169
|
+
gap: 20,
|
|
170
|
+
});
|
|
197
171
|
};
|
|
172
|
+
/**
|
|
173
|
+
* How a stat value is written.
|
|
174
|
+
* Not validated at the boundary: anything but `long` reads as `short`,
|
|
175
|
+
* so an unknown value falls back rather than failing.
|
|
176
|
+
*/
|
|
177
|
+
const NUMBER_FORMATS = ['short', 'long'];
|
|
198
178
|
/**
|
|
199
179
|
* Create a labelled stat text item
|
|
200
180
|
* (a label and its value, optionally with an icon and link).
|
|
201
181
|
* Shared by the stats and repo cards.
|
|
202
182
|
*
|
|
203
|
-
* The caller must ensure that the passed `
|
|
183
|
+
* The caller must ensure that the passed `link` is properly sanitized!
|
|
204
184
|
*
|
|
205
|
-
* @param params Object that contains the createTextNode parameters.
|
|
206
|
-
* @param params.icon The sanitized icon to display.
|
|
207
|
-
* @param params.label The label to display.
|
|
208
|
-
* @param params.value The value to display.
|
|
209
|
-
* @param params.id The id of the stat.
|
|
210
|
-
* @param params.unitSymbol The unit symbol of the stat.
|
|
211
|
-
* @param params.index The index of the stat.
|
|
212
|
-
* @param params.showIcons Whether to show icons.
|
|
213
|
-
* @param params.shiftValuePos Number of pixels the value has to be shifted to the right.
|
|
214
|
-
* @param params.bold Whether to bold the label.
|
|
215
|
-
* @param params.numberFormat The format of numbers on card.
|
|
216
|
-
* @param params.numberPrecision The precision of numbers on card.
|
|
217
|
-
* @param params.link Sanitized url to link to.
|
|
218
|
-
* @param params.labelXOffset horizontal offset for label.
|
|
219
185
|
* @returns The stats card text item SVG object.
|
|
220
186
|
*/
|
|
221
|
-
const createTextNode = ({ icon, label, value, id, unitSymbol, index, showIcons, shiftValuePos, bold, numberFormat, numberPrecision, link, labelXOffset = 25, }) => {
|
|
187
|
+
const createTextNode = ({ icon, label, value, id, unitSymbol, index, showIcons, shiftValuePos, valueAnchorX, bold, labelBold = bold, numberFormat, numberPrecision, link, labelXOffset = 25, }) => {
|
|
222
188
|
if (!Number.isFinite(labelXOffset)) {
|
|
223
|
-
throw new
|
|
189
|
+
throw new TypeError(`Invalid labelXOffset: "${labelXOffset}"`);
|
|
224
190
|
}
|
|
225
191
|
if (!Number.isFinite(shiftValuePos)) {
|
|
226
|
-
throw new
|
|
192
|
+
throw new TypeError(`Invalid shiftValuePos: "${shiftValuePos}"`);
|
|
227
193
|
}
|
|
228
194
|
if (!Number.isFinite(index)) {
|
|
229
|
-
throw new
|
|
195
|
+
throw new TypeError(`Invalid index: "${index}"`);
|
|
196
|
+
}
|
|
197
|
+
if (valueAnchorX !== undefined && !Number.isFinite(valueAnchorX)) {
|
|
198
|
+
throw new Error(`Invalid valueAnchorX: "${valueAnchorX}"`);
|
|
230
199
|
}
|
|
231
200
|
const precision = numberPrecision !== undefined && Number.isFinite(numberPrecision)
|
|
232
201
|
? clampValue(numberPrecision, 0, 2)
|
|
@@ -235,49 +204,41 @@ const createTextNode = ({ icon, label, value, id, unitSymbol, index, showIcons,
|
|
|
235
204
|
// (always a number here) is abbreviated via kFormatter.
|
|
236
205
|
const rawValue = numberFormat.toLowerCase() === 'long' || id === 'prs_merged_percentage';
|
|
237
206
|
const kValue = rawValue || typeof value !== 'number' ? value : kFormatter(value, precision);
|
|
238
|
-
const staggerDelay =
|
|
239
|
-
const boldClass = bold ? '
|
|
207
|
+
const staggerDelay = 120 + index * 40;
|
|
208
|
+
const boldClass = bold ? 'bold' : 'not_bold';
|
|
209
|
+
const labelBoldClass = labelBold ? 'bold' : 'not_bold';
|
|
240
210
|
const valueX = (showIcons ? 140 : 120) + (bold ? 5 : 0) + shiftValuePos;
|
|
241
211
|
const unit = unitSymbol ? ` ${unitSymbol}` : '';
|
|
242
|
-
const
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
212
|
+
const content = [
|
|
213
|
+
showIcons &&
|
|
214
|
+
el('svg', {
|
|
215
|
+
'data-testid': 'icon',
|
|
216
|
+
class: 'icon',
|
|
217
|
+
viewBox: '0 0 16 16',
|
|
218
|
+
version: '1.1',
|
|
219
|
+
width: 16,
|
|
220
|
+
height: 16,
|
|
221
|
+
}, icon),
|
|
222
|
+
el('text', { class: `stat ${labelBoldClass}`, x: showIcons ? labelXOffset : undefined, y: 12.5 }, `${label}:`),
|
|
223
|
+
el('text', {
|
|
224
|
+
class: `stat ${boldClass}`,
|
|
225
|
+
x: valueAnchorX ?? valueX,
|
|
226
|
+
'text-anchor': valueAnchorX === undefined ? undefined : 'end',
|
|
227
|
+
y: 12.5,
|
|
228
|
+
'data-testid': id,
|
|
229
|
+
}, `${String(kValue)}${unit}`),
|
|
230
|
+
];
|
|
231
|
+
return el('g', {
|
|
232
|
+
class: 'stagger',
|
|
233
|
+
style: `animation-delay: ${staggerDelay}ms`,
|
|
234
|
+
transform: 'translate(25, 0)',
|
|
235
|
+
}, link ? el('a', { href: link }, content) : content);
|
|
264
236
|
};
|
|
265
|
-
// Script parameters.
|
|
266
237
|
const ERROR_CARD_LENGTH = 576.5;
|
|
267
|
-
const UPSTREAM_API_ERRORS = [TRY_AGAIN_LATER, SECONDARY_ERROR_MESSAGES.rate_limited];
|
|
238
|
+
const UPSTREAM_API_ERRORS = new Set([TRY_AGAIN_LATER, SECONDARY_ERROR_MESSAGES.rate_limited]);
|
|
268
239
|
/**
|
|
269
240
|
* Renders error message on the card.
|
|
270
241
|
*
|
|
271
|
-
* @param args Function arguments.
|
|
272
|
-
* @param args.message Main error message.
|
|
273
|
-
* @param args.secondaryMessage The secondary error message.
|
|
274
|
-
* @param args.renderOptions Render options.
|
|
275
|
-
* @param args.renderOptions.title_color Card title color.
|
|
276
|
-
* @param args.renderOptions.text_color Card text color.
|
|
277
|
-
* @param args.renderOptions.bg_color Card background color.
|
|
278
|
-
* @param args.renderOptions.border_color Card border color.
|
|
279
|
-
* @param args.renderOptions.theme Card theme.
|
|
280
|
-
* @param args.renderOptions.show_repo_link Whether to show repo link or not.
|
|
281
242
|
* @returns The SVG markup.
|
|
282
243
|
*/
|
|
283
244
|
const renderError = ({ message, secondaryMessage = '', renderOptions = {}, }) => {
|
|
@@ -291,91 +252,85 @@ const renderError = ({ message, secondaryMessage = '', renderOptions = {}, }) =>
|
|
|
291
252
|
ring_color: '',
|
|
292
253
|
theme,
|
|
293
254
|
});
|
|
294
|
-
|
|
295
|
-
<svg width="${ERROR_CARD_LENGTH}" height="120" viewBox="0 0 ${ERROR_CARD_LENGTH} 120" fill="${String(bgColor)}" xmlns="http://www.w3.org/2000/svg" role="img" aria-labelledby="titleId descId">
|
|
296
|
-
<title id="titleId">${encodeHTML(message)}</title>
|
|
297
|
-
<desc id="descId">${encodeHTML(secondaryMessage)}</desc>
|
|
298
|
-
<style>
|
|
299
|
-
.text { font: 600 16px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${titleColor} }
|
|
300
|
-
.small { font: 600 12px 'Segoe UI', Ubuntu, Sans-Serif; fill: ${textColor} }
|
|
301
|
-
.gray { fill: #858585 }
|
|
302
|
-
</style>
|
|
303
|
-
<rect x="0.5" y="0.5" width="${ERROR_CARD_LENGTH - 1}" height="99%" rx="4.5" fill="${String(bgColor)}" stroke="${borderColor}"/>
|
|
304
|
-
<text x="25" y="45" class="text">Something went wrong!${UPSTREAM_API_ERRORS.includes(secondaryMessage) || !show_repo_link
|
|
255
|
+
const hint = UPSTREAM_API_ERRORS.has(secondaryMessage) || !show_repo_link
|
|
305
256
|
? ''
|
|
306
|
-
: ' file an issue at https://tinyurl.com/github-stats'
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
257
|
+
: ' file an issue at https://tinyurl.com/github-stats';
|
|
258
|
+
return renderMarkup(el('svg', {
|
|
259
|
+
width: ERROR_CARD_LENGTH,
|
|
260
|
+
height: 120,
|
|
261
|
+
viewBox: `0 0 ${ERROR_CARD_LENGTH} 120`,
|
|
262
|
+
fill: String(bgColor),
|
|
263
|
+
xmlns: 'http://www.w3.org/2000/svg',
|
|
264
|
+
role: 'img',
|
|
265
|
+
'aria-labelledby': 'title-id desc-id',
|
|
266
|
+
}, el('title', { id: 'title-id' }, message), el('desc', { id: 'desc-id' }, secondaryMessage), style(rule('.text', { font: "600 16px 'Segoe UI', Ubuntu, Sans-Serif", fill: titleColor }), rule('.small', { font: "600 12px 'Segoe UI', Ubuntu, Sans-Serif", fill: textColor }), rule('.gray', { fill: '#858585' })), el('rect', {
|
|
267
|
+
x: 0.5,
|
|
268
|
+
y: 0.5,
|
|
269
|
+
width: ERROR_CARD_LENGTH - 1,
|
|
270
|
+
height: '99%',
|
|
271
|
+
rx: 8,
|
|
272
|
+
fill: String(bgColor),
|
|
273
|
+
stroke: borderColor,
|
|
274
|
+
}), el('text', { x: 25, y: 45, class: 'text' }, `Something went wrong!${hint}`), el('text', { 'data-testid': 'message', x: 25, y: 55, class: 'text small' }, el('tspan', { x: 25, dy: 18 }, message), el('tspan', { x: 25, dy: 18, class: 'gray' }, secondaryMessage))));
|
|
313
275
|
};
|
|
276
|
+
/** Advance of each ASCII code point, as a fraction of the font size. */
|
|
277
|
+
// prettier-ignore
|
|
278
|
+
const CHAR_WIDTHS = [
|
|
279
|
+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
280
|
+
0, 0, 0, 0, 0, 0, 0, 0.2733333110809326, 0.28499999046325686,
|
|
281
|
+
0.39166667461395266, 0.5900000095367431, 0.5383333206176758,
|
|
282
|
+
0.8183333396911621, 0.8, 0.22999999523162842, 0.30166666507720946,
|
|
283
|
+
0.30166666507720946, 0.41666665077209475, 0.6833333492279052,
|
|
284
|
+
0.21666667461395264, 0.4, 0.21666667461395264, 0.3900000095367432,
|
|
285
|
+
0.5383333206176758, 0.5383333206176758, 0.5383333206176758,
|
|
286
|
+
0.5383333206176758, 0.5383333206176758, 0.5383333206176758,
|
|
287
|
+
0.5383333206176758, 0.5383333206176758, 0.5383333206176758,
|
|
288
|
+
0.5383333206176758, 0.21666667461395264, 0.21666667461395264,
|
|
289
|
+
0.6833333492279052, 0.6833333492279052, 0.6833333492279052,
|
|
290
|
+
0.4483333110809326, 0.9550000190734863, 0.6449999809265137,
|
|
291
|
+
0.5733333110809327, 0.6183333396911621, 0.7016666889190674,
|
|
292
|
+
0.5066666603088379, 0.48833332061767576, 0.6866666793823242,
|
|
293
|
+
0.7099999904632568, 0.26666667461395266, 0.35666666030883787,
|
|
294
|
+
0.5800000190734863, 0.4699999809265137, 0.8983333587646485,
|
|
295
|
+
0.7483333110809326, 0.753333330154419, 0.5599999904632569,
|
|
296
|
+
0.753333330154419, 0.5983333110809326, 0.5316666603088379,
|
|
297
|
+
0.5233333110809326, 0.6866666793823242, 0.621666669845581,
|
|
298
|
+
0.9333333015441895, 0.5900000095367431, 0.553333330154419,
|
|
299
|
+
0.5699999809265137, 0.30166666507720946, 0.37833333015441895,
|
|
300
|
+
0.30166666507720946, 0.6833333492279052, 0.41500000953674315,
|
|
301
|
+
0.26833333969116213, 0.5083333492279053, 0.5883333206176757,
|
|
302
|
+
0.4616666793823242, 0.5883333206176757, 0.5233333110809326,
|
|
303
|
+
0.3133333444595337, 0.5883333206176757, 0.5666666507720948,
|
|
304
|
+
0.24166667461395264, 0.24166667461395264, 0.49666666984558105,
|
|
305
|
+
0.24166667461395264, 0.8616666793823242, 0.5666666507720948,
|
|
306
|
+
0.5866666793823242, 0.5883333206176757, 0.5883333206176757,
|
|
307
|
+
0.3483333349227905, 0.425, 0.33833334445953367, 0.5666666507720948,
|
|
308
|
+
0.4783333301544189, 0.7233333110809326, 0.45833334922790525,
|
|
309
|
+
0.4833333492279053, 0.45166668891906736, 0.30166666507720946,
|
|
310
|
+
0.24000000953674316, 0.30166666507720946, 0.6833333492279052,
|
|
311
|
+
];
|
|
312
|
+
/** Advance of a code point the table does not cover. */
|
|
313
|
+
const AVERAGE_CHAR_WIDTH = 0.5131403493881227;
|
|
314
|
+
/**
|
|
315
|
+
* Full-width glyphs: U+3000–U+9FFF (CJK symbols and punctuation, Hiragana, Katakana,
|
|
316
|
+
* CJK Unified Ideographs incl. Extension A) plus U+FF00–U+FFEF (halfwidth and fullwidth forms).
|
|
317
|
+
*/
|
|
318
|
+
const FULL_WIDTH_PATTERN = /[\u3000-\u9FFF\uFF00-\uFFEF]/;
|
|
314
319
|
/**
|
|
315
320
|
* Retrieve text length based on Segoe UI font.
|
|
316
321
|
*
|
|
317
322
|
* @see https://stackoverflow.com/a/48172630/10629172
|
|
318
|
-
*
|
|
319
|
-
* @param fontSize Font size.
|
|
323
|
+
*
|
|
320
324
|
* @returns Text length.
|
|
321
325
|
*/
|
|
322
|
-
const measureText = (str, fontSize = 10) =>
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
0.21666667461395264, 0.4, 0.21666667461395264, 0.3900000095367432,
|
|
331
|
-
0.5383333206176758, 0.5383333206176758, 0.5383333206176758,
|
|
332
|
-
0.5383333206176758, 0.5383333206176758, 0.5383333206176758,
|
|
333
|
-
0.5383333206176758, 0.5383333206176758, 0.5383333206176758,
|
|
334
|
-
0.5383333206176758, 0.21666667461395264, 0.21666667461395264,
|
|
335
|
-
0.6833333492279052, 0.6833333492279052, 0.6833333492279052,
|
|
336
|
-
0.4483333110809326, 0.9550000190734863, 0.6449999809265137,
|
|
337
|
-
0.5733333110809327, 0.6183333396911621, 0.7016666889190674,
|
|
338
|
-
0.5066666603088379, 0.48833332061767576, 0.6866666793823242,
|
|
339
|
-
0.7099999904632568, 0.26666667461395266, 0.35666666030883787,
|
|
340
|
-
0.5800000190734863, 0.4699999809265137, 0.8983333587646485,
|
|
341
|
-
0.7483333110809326, 0.753333330154419, 0.5599999904632569,
|
|
342
|
-
0.753333330154419, 0.5983333110809326, 0.5316666603088379,
|
|
343
|
-
0.5233333110809326, 0.6866666793823242, 0.621666669845581,
|
|
344
|
-
0.9333333015441895, 0.5900000095367431, 0.553333330154419,
|
|
345
|
-
0.5699999809265137, 0.30166666507720946, 0.37833333015441895,
|
|
346
|
-
0.30166666507720946, 0.6833333492279052, 0.41500000953674315,
|
|
347
|
-
0.26833333969116213, 0.5083333492279053, 0.5883333206176757,
|
|
348
|
-
0.4616666793823242, 0.5883333206176757, 0.5233333110809326,
|
|
349
|
-
0.3133333444595337, 0.5883333206176757, 0.5666666507720948,
|
|
350
|
-
0.24166667461395264, 0.24166667461395264, 0.49666666984558105,
|
|
351
|
-
0.24166667461395264, 0.8616666793823242, 0.5666666507720948,
|
|
352
|
-
0.5866666793823242, 0.5883333206176757, 0.5883333206176757,
|
|
353
|
-
0.3483333349227905, 0.425, 0.33833334445953367, 0.5666666507720948,
|
|
354
|
-
0.4783333301544189, 0.7233333110809326, 0.45833334922790525,
|
|
355
|
-
0.4833333492279053, 0.45166668891906736, 0.30166666507720946,
|
|
356
|
-
0.24000000953674316, 0.30166666507720946, 0.6833333492279052,
|
|
357
|
-
];
|
|
358
|
-
const avg = 0.5131403493881227;
|
|
359
|
-
// CJK character range: U+3000–U+9FFF (CJK Symbols/Punctuation, Hiragana,
|
|
360
|
-
// Katakana, CJK Unified Ideographs incl. Extension A) plus U+FF00–U+FFEF
|
|
361
|
-
// (Halfwidth/Fullwidth Forms — fullwidth ASCII, fullwidth punctuation).
|
|
362
|
-
const cjkRange = /[\u3000-\u9FFF\uFF00-\uFFEF]/;
|
|
363
|
-
return (str
|
|
364
|
-
.split('')
|
|
365
|
-
.map((c) => {
|
|
366
|
-
if (cjkRange.test(c) || c === '\u3000') {
|
|
367
|
-
// CJK glyphs and U+3000 IDEOGRAPHIC SPACE are full-width by default;
|
|
368
|
-
return 1;
|
|
369
|
-
}
|
|
370
|
-
if (c.charCodeAt(0) < widths.length) {
|
|
371
|
-
return widths[c.charCodeAt(0)] ?? avg;
|
|
372
|
-
}
|
|
373
|
-
else {
|
|
374
|
-
return avg;
|
|
375
|
-
}
|
|
376
|
-
})
|
|
377
|
-
.reduce((cur, acc) => acc + cur) * fontSize);
|
|
378
|
-
};
|
|
326
|
+
const measureText = (str, fontSize = 10) => [...str]
|
|
327
|
+
.map((char) => {
|
|
328
|
+
if (FULL_WIDTH_PATTERN.test(char)) {
|
|
329
|
+
return 1;
|
|
330
|
+
}
|
|
331
|
+
return CHAR_WIDTHS[char.codePointAt(0) ?? 0] ?? AVERAGE_CHAR_WIDTH;
|
|
332
|
+
})
|
|
333
|
+
.reduce((total, width) => total + width, 0) * fontSize;
|
|
379
334
|
/**
|
|
380
335
|
* Split text into the lines it would wrap to when laid out greedily at the
|
|
381
336
|
* given font size inside a box of width `maxWidth`. Uses `measureText` so the
|
|
@@ -383,9 +338,6 @@ const measureText = (str, fontSize = 10) => {
|
|
|
383
338
|
* The browser still does the real wrap inside the foreignObject; this is only
|
|
384
339
|
* used to size the SVG.
|
|
385
340
|
*
|
|
386
|
-
* @param text Text to split.
|
|
387
|
-
* @param fontSize Font size in px (matches `measureText`).
|
|
388
|
-
* @param maxWidth Available wrap width in px.
|
|
389
341
|
* @returns Estimated wrapped lines.
|
|
390
342
|
*/
|
|
391
343
|
const splitWrappedText = (text, fontSize, maxWidth) => {
|
|
@@ -402,13 +354,13 @@ const splitWrappedText = (text, fontSize, maxWidth) => {
|
|
|
402
354
|
// Korean Hangul (U+AC00–U+D7AF) is intentionally NOT in the CJK range
|
|
403
355
|
// because Korean wraps at word boundaries by default in HTML.
|
|
404
356
|
// ASCII whitespace is collapsed to a single space per CSS `white-space: normal;`
|
|
405
|
-
const normalizedText = text.
|
|
357
|
+
const normalizedText = text.replaceAll(/[\t\n\r ]+/g, ' ');
|
|
406
358
|
const tokens = normalizedText.match(/\s|[\u3000-\u9FFF\uFF00-\uFFEF]|[^\s\u3000-\u9FFF\uFF00-\uFFEF]+/g);
|
|
407
359
|
if (!tokens) {
|
|
408
360
|
return [];
|
|
409
361
|
}
|
|
410
362
|
const takeFittingSegment = (token, availableWidth) => {
|
|
411
|
-
const characters = token
|
|
363
|
+
const characters = [...token];
|
|
412
364
|
let segment = '';
|
|
413
365
|
let width = 0;
|
|
414
366
|
for (const character of characters) {
|
|
@@ -432,7 +384,7 @@ const splitWrappedText = (text, fontSize, maxWidth) => {
|
|
|
432
384
|
if (currentWidth === 0) {
|
|
433
385
|
continue;
|
|
434
386
|
}
|
|
435
|
-
lines[lines.length - 1] = (lines
|
|
387
|
+
lines[lines.length - 1] = (lines.at(-1) ?? '') + token;
|
|
436
388
|
currentWidth += measureText(token, fontSize);
|
|
437
389
|
continue;
|
|
438
390
|
}
|
|
@@ -440,7 +392,7 @@ const splitWrappedText = (text, fontSize, maxWidth) => {
|
|
|
440
392
|
while (remaining) {
|
|
441
393
|
const w = measureText(remaining, fontSize);
|
|
442
394
|
if (currentWidth + w <= maxWidth) {
|
|
443
|
-
lines[lines.length - 1] = (lines
|
|
395
|
+
lines[lines.length - 1] = (lines.at(-1) ?? '') + remaining;
|
|
444
396
|
currentWidth += w;
|
|
445
397
|
break;
|
|
446
398
|
}
|
|
@@ -451,24 +403,38 @@ const splitWrappedText = (text, fontSize, maxWidth) => {
|
|
|
451
403
|
}
|
|
452
404
|
// An atom wider than the box wraps mid-glyph (overflow-wrap: anywhere).
|
|
453
405
|
const { segment, width } = takeFittingSegment(remaining, maxWidth);
|
|
454
|
-
lines[lines.length - 1] = (lines
|
|
406
|
+
lines[lines.length - 1] = (lines.at(-1) ?? '') + segment;
|
|
455
407
|
currentWidth = width;
|
|
456
408
|
remaining = remaining.slice(segment.length);
|
|
457
409
|
}
|
|
458
410
|
}
|
|
459
411
|
return lines.map((line) => line.replace(/[\t\n\r ]+$/, ''));
|
|
460
412
|
};
|
|
413
|
+
/**
|
|
414
|
+
* Split text over multiple lines based on the card width.
|
|
415
|
+
*
|
|
416
|
+
* @returns Array of lines.
|
|
417
|
+
*/
|
|
418
|
+
const wrapTextMultiline = (text, width, fontSize, maxLines = 3) => {
|
|
419
|
+
const wrapped = splitWrappedText(text, fontSize, width);
|
|
420
|
+
const lines = wrapped.map((line) => line.trim()).slice(0, maxLines); // Only consider maxLines lines
|
|
421
|
+
// Add "..." to the last line if the text exceeds maxLines
|
|
422
|
+
if (wrapped.length > maxLines) {
|
|
423
|
+
const lastIndex = maxLines - 1;
|
|
424
|
+
const lastLine = lines[lastIndex];
|
|
425
|
+
if (lastLine !== undefined) {
|
|
426
|
+
lines[lastIndex] = `${lastLine}...`;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
// Remove empty lines if text fits in less than maxLines lines
|
|
430
|
+
const multiLineText = lines.filter(Boolean);
|
|
431
|
+
return multiLineText;
|
|
432
|
+
};
|
|
461
433
|
/**
|
|
462
434
|
* Estimate how many lines a string will wrap to when laid out greedily at the
|
|
463
435
|
* given font size inside a box of width `maxWidth`, capped at `maxLines`.
|
|
464
436
|
*
|
|
465
|
-
* @param text Text to estimate.
|
|
466
|
-
* @param fontSize Font size in px (matches `measureText`).
|
|
467
|
-
* @param maxWidth Available wrap width in px.
|
|
468
|
-
* @param maxLines Cap on the returned line count.
|
|
469
437
|
* @returns Estimated line count, at least 1, at most `maxLines`.
|
|
470
438
|
*/
|
|
471
|
-
const countWrappedLines = (text, fontSize, maxWidth, maxLines) =>
|
|
472
|
-
|
|
473
|
-
};
|
|
474
|
-
export { renderError, createLanguageNode, createProgressNode, createTextNode, iconWithLabel, flexLayout, measureText, splitWrappedText, countWrappedLines, wrappedTextNode, wrappedTextStyles, };
|
|
439
|
+
const countWrappedLines = (text, fontSize, maxWidth, maxLines) => Math.min(Math.max(1, splitWrappedText(text, fontSize, maxWidth).length), maxLines);
|
|
440
|
+
export { NUMBER_FORMATS, renderError, createLanguageNode, createProgressNode, createTextNode, iconWithLabel, flexLayout, measureText, splitWrappedText, wrapTextMultiline, countWrappedLines, wrappedTextNode, wrappedTextStyles, };
|