@qoretechnologies/reqore 0.79.0-pr.715.g032a17f → 0.79.0-pr.715.g25b5c75
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.
|
@@ -25,14 +25,14 @@ export interface IUseFitTextSizeOptions {
|
|
|
25
25
|
/** Floor in px. Defaults to `max * FIT_TEXT_MIN_RATIO`, never below `FIT_TEXT_FLOOR_PX`. */
|
|
26
26
|
min?: number;
|
|
27
27
|
/**
|
|
28
|
-
* How many lines the text may occupy.
|
|
29
|
-
*
|
|
30
|
-
*
|
|
28
|
+
* How many lines the text may occupy. A label allowed to wrap keeps its natural size while
|
|
29
|
+
* its words fit that many lines of `available` - wrapping is cheaper than shrinking, so it
|
|
30
|
+
* happens first - and shrinks only to make them fit.
|
|
31
31
|
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* the
|
|
32
|
+
* The words are packed into lines the way the browser breaks them (greedily, at spaces), not
|
|
33
|
+
* poured in as one run: "Design changes built" in a 98px box is 156px, under two lines' 196px,
|
|
34
|
+
* but breaks "Design / changes / built" at 16px, so a clamp at two lines cut "built" from a
|
|
35
|
+
* title kept at full size (the admin portal's 200px tile). Shrinking comes before cutting.
|
|
36
36
|
*
|
|
37
37
|
* @default 1
|
|
38
38
|
*/
|
|
@@ -40,13 +40,36 @@ export interface IUseFitTextSizeOptions {
|
|
|
40
40
|
/** Off returns `max`. */
|
|
41
41
|
enabled?: boolean;
|
|
42
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Whether words of these widths (in px at the size being tried) fit `lines` lines of
|
|
45
|
+
* `available` when packed the way a browser breaks them: greedily, a word to the next line when
|
|
46
|
+
* it does not fit beside the last. A word wider than a line on its own does not fit at all.
|
|
47
|
+
*
|
|
48
|
+
* One line keeps `FIT_TOLERANCE_PX` of slack, so a label that fits to the pixel is not shrunk
|
|
49
|
+
* by one for nothing. Several lines get none: the browser breaks a line at its edge, so a word
|
|
50
|
+
* judged to fit a line by a pixel of slack goes to the next one, and on the last line that is
|
|
51
|
+
* the word the clamp cuts ("changes built" at 103px in a 102px line, the portal's tile).
|
|
52
|
+
*/
|
|
53
|
+
export declare const wordsFitLines: (words: number[], space: number, available: number, lines: number) => boolean;
|
|
54
|
+
/**
|
|
55
|
+
* The size to draw a text at: `max` when its words fit `lines` lines of `available` there, else
|
|
56
|
+
* the largest whole px down to `floor` at which they do, else `floor` (the clamp cuts what a
|
|
57
|
+
* floored size still cannot hold). `words` and `space` are widths at `max`.
|
|
58
|
+
*/
|
|
59
|
+
export declare const largestFittingTextSize: ({ words, space, available, max, floor, lines, }: {
|
|
60
|
+
words: number[];
|
|
61
|
+
space: number;
|
|
62
|
+
available: number;
|
|
63
|
+
max: number;
|
|
64
|
+
floor: number;
|
|
65
|
+
lines?: number;
|
|
66
|
+
}) => number;
|
|
43
67
|
/**
|
|
44
68
|
* The largest whole px in `[min, max]` at which `text` fits `available`.
|
|
45
69
|
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* ~10 iterations to reach the same number.
|
|
70
|
+
* Each word is measured once, at the natural size: text width is linear in font size, so a
|
|
71
|
+
* smaller size is arithmetic on those widths, packed into lines the way the browser breaks
|
|
72
|
+
* them (`largestFittingTextSize`), with no further measuring.
|
|
50
73
|
*
|
|
51
74
|
* Measured with `canvas.measureText` against the font of the element that PAINTS the text, so
|
|
52
75
|
* there is no write-then-read cycle at all — the layout thrashing that makes fit-to-width
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useFitTextSize.d.ts","sourceRoot":"","sources":["../../src/hooks/useFitTextSize.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,OAAO,CAAC;AAEvC,gEAAgE;AAChE,eAAO,MAAM,iBAAiB,KAAK,CAAC;AAQpC,MAAM,WAAW,sBAAsB;IACrC,iCAAiC;IACjC,IAAI,EAAE,MAAM,CAAC;IACb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;AACH,eAAO,MAAM,cAAc,GAAI,CAAC,SAAS,WAAW,GAAG,cAAc,EAAE,gDAOpE,sBAAsB,KAAG,CAAC,KAAK,CAAC,gBAAgB,CAAC,CAAC,GAAG,IAAI,CAAC,EAAE,MAAM,CA+CpE,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useFitTextSize = exports.FIT_TEXT_FLOOR_PX = exports.FIT_TEXT_MIN_RATIO = void 0;
|
|
3
|
+
exports.useFitTextSize = exports.largestFittingTextSize = exports.wordsFitLines = exports.FIT_TEXT_FLOOR_PX = exports.FIT_TEXT_MIN_RATIO = void 0;
|
|
4
4
|
var react_1 = require("react");
|
|
5
5
|
/**
|
|
6
6
|
* The smallest a label may shrink to, as a fraction of its natural size.
|
|
@@ -17,13 +17,73 @@ exports.FIT_TEXT_FLOOR_PX = 11;
|
|
|
17
17
|
* without slack a label that fits exactly is shrunk by a pixel for no visible reason.
|
|
18
18
|
*/
|
|
19
19
|
var FIT_TOLERANCE_PX = 2;
|
|
20
|
+
/**
|
|
21
|
+
* Whether words of these widths (in px at the size being tried) fit `lines` lines of
|
|
22
|
+
* `available` when packed the way a browser breaks them: greedily, a word to the next line when
|
|
23
|
+
* it does not fit beside the last. A word wider than a line on its own does not fit at all.
|
|
24
|
+
*
|
|
25
|
+
* One line keeps `FIT_TOLERANCE_PX` of slack, so a label that fits to the pixel is not shrunk
|
|
26
|
+
* by one for nothing. Several lines get none: the browser breaks a line at its edge, so a word
|
|
27
|
+
* judged to fit a line by a pixel of slack goes to the next one, and on the last line that is
|
|
28
|
+
* the word the clamp cuts ("changes built" at 103px in a 102px line, the portal's tile).
|
|
29
|
+
*/
|
|
30
|
+
var wordsFitLines = function (words, space, available, lines) {
|
|
31
|
+
var room = lines > 1 ? available : available + FIT_TOLERANCE_PX;
|
|
32
|
+
var line = 1;
|
|
33
|
+
var used = 0;
|
|
34
|
+
for (var _i = 0, words_1 = words; _i < words_1.length; _i++) {
|
|
35
|
+
var word = words_1[_i];
|
|
36
|
+
if (word > room) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
if (used === 0) {
|
|
40
|
+
used = word;
|
|
41
|
+
}
|
|
42
|
+
else if (used + space + word <= room) {
|
|
43
|
+
used += space + word;
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
line += 1;
|
|
47
|
+
used = word;
|
|
48
|
+
if (line > lines) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return true;
|
|
54
|
+
};
|
|
55
|
+
exports.wordsFitLines = wordsFitLines;
|
|
56
|
+
/**
|
|
57
|
+
* The size to draw a text at: `max` when its words fit `lines` lines of `available` there, else
|
|
58
|
+
* the largest whole px down to `floor` at which they do, else `floor` (the clamp cuts what a
|
|
59
|
+
* floored size still cannot hold). `words` and `space` are widths at `max`.
|
|
60
|
+
*/
|
|
61
|
+
var largestFittingTextSize = function (_a) {
|
|
62
|
+
var words = _a.words, space = _a.space, available = _a.available, max = _a.max, floor = _a.floor, _b = _a.lines, lines = _b === void 0 ? 1 : _b;
|
|
63
|
+
var capacity = Math.max(1, Math.floor(lines) || 1);
|
|
64
|
+
var fitsAt = function (size) {
|
|
65
|
+
var ratio = size / max;
|
|
66
|
+
return (0, exports.wordsFitLines)(words.map(function (word) { return word * ratio; }), space * ratio, available, capacity);
|
|
67
|
+
};
|
|
68
|
+
if (fitsAt(max)) {
|
|
69
|
+
return max;
|
|
70
|
+
}
|
|
71
|
+
// Whole px below the natural size: a size that is itself fractional (a type scale's 14.4)
|
|
72
|
+
// still shrinks to whole px, which keeps snapshots stable across platforms.
|
|
73
|
+
for (var size = Math.ceil(max) - 1; size >= floor; size -= 1) {
|
|
74
|
+
if (fitsAt(size)) {
|
|
75
|
+
return size;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return floor;
|
|
79
|
+
};
|
|
80
|
+
exports.largestFittingTextSize = largestFittingTextSize;
|
|
20
81
|
/**
|
|
21
82
|
* The largest whole px in `[min, max]` at which `text` fits `available`.
|
|
22
83
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* ~10 iterations to reach the same number.
|
|
84
|
+
* Each word is measured once, at the natural size: text width is linear in font size, so a
|
|
85
|
+
* smaller size is arithmetic on those widths, packed into lines the way the browser breaks
|
|
86
|
+
* them (`largestFittingTextSize`), with no further measuring.
|
|
27
87
|
*
|
|
28
88
|
* Measured with `canvas.measureText` against the font of the element that PAINTS the text, so
|
|
29
89
|
* there is no write-then-read cycle at all — the layout thrashing that makes fit-to-width
|
|
@@ -57,17 +117,16 @@ var useFitTextSize = function (_a) {
|
|
|
57
117
|
var style = getComputedStyle(node.firstElementChild || node);
|
|
58
118
|
// Letter-spacing is not part of the canvas font shorthand, so it is added back per
|
|
59
119
|
// character. A spaced heading measured without it fits on paper and clips in the DOM.
|
|
60
|
-
var spacing = (parseFloat(style.letterSpacing) || 0) * text.length;
|
|
61
120
|
context.font = "".concat(style.fontStyle, " ").concat(style.fontWeight, " ").concat(max, "px ").concat(style.fontFamily);
|
|
62
|
-
var
|
|
63
|
-
//
|
|
64
|
-
//
|
|
65
|
-
var
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return;
|
|
69
|
-
|
|
70
|
-
setSize(
|
|
121
|
+
var letterSpacing = parseFloat(style.letterSpacing) || 0;
|
|
122
|
+
// Each word measured once, at the natural size; text width is linear in the font size, so
|
|
123
|
+
// every smaller size is these times its ratio.
|
|
124
|
+
var words = text
|
|
125
|
+
.split(/\s+/)
|
|
126
|
+
.filter(Boolean)
|
|
127
|
+
.map(function (word) { return context.measureText(word).width + letterSpacing * word.length; });
|
|
128
|
+
var space = context.measureText(' ').width + letterSpacing;
|
|
129
|
+
setSize((0, exports.largestFittingTextSize)({ words: words, space: space, available: available, max: max, floor: floor, lines: lines }));
|
|
71
130
|
}, [text, available, max, floor, lines, enabled]);
|
|
72
131
|
return [ref, size];
|
|
73
132
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useFitTextSize.js","sourceRoot":"","sources":["../../src/hooks/useFitTextSize.ts"],"names":[],"mappings":";;;AAAA,+BAAoD;AAEpD;;;;;;GAMG;AACU,QAAA,kBAAkB,GAAG,IAAI,CAAC;AAEvC,gEAAgE;AACnD,QAAA,iBAAiB,GAAG,EAAE,CAAC;AAEpC;;;GAGG;AACH,IAAM,gBAAgB,GAAG,CAAC,CAAC;AAmC3B
|
|
1
|
+
{"version":3,"file":"useFitTextSize.js","sourceRoot":"","sources":["../../src/hooks/useFitTextSize.ts"],"names":[],"mappings":";;;AAAA,+BAAoD;AAEpD;;;;;;GAMG;AACU,QAAA,kBAAkB,GAAG,IAAI,CAAC;AAEvC,gEAAgE;AACnD,QAAA,iBAAiB,GAAG,EAAE,CAAC;AAEpC;;;GAGG;AACH,IAAM,gBAAgB,GAAG,CAAC,CAAC;AAmC3B;;;;;;;;;GASG;AACI,IAAM,aAAa,GAAG,UAC3B,KAAe,EACf,KAAa,EACb,SAAiB,EACjB,KAAa;IAEb,IAAM,IAAI,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,GAAG,gBAAgB,CAAC;IAClE,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,IAAI,IAAI,GAAG,CAAC,CAAC;IAEb,KAAmB,UAAK,EAAL,eAAK,EAAL,mBAAK,EAAL,IAAK,EAAE,CAAC;QAAtB,IAAM,IAAI,cAAA;QACb,IAAI,IAAI,GAAG,IAAI,EAAE,CAAC;YAChB,OAAO,KAAK,CAAC;QACf,CAAC;QAED,IAAI,IAAI,KAAK,CAAC,EAAE,CAAC;YACf,IAAI,GAAG,IAAI,CAAC;QACd,CAAC;aAAM,IAAI,IAAI,GAAG,KAAK,GAAG,IAAI,IAAI,IAAI,EAAE,CAAC;YACvC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