@altano/textfit 0.0.1
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/LICENSE +22 -0
- package/README.md +22 -0
- package/dist/textFit.d.ts +46 -0
- package/dist/textFit.js +101 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014 Samuel Reed <samuel.trace.reed@gmail.com> (http://strml.net/)
|
|
4
|
+
Copyright (c) 2022 Alan Norbauer <altano@gmail.com> (https://alan.norbauer.com/)
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# `textFit` - Hassle-Free Text Fitting
|
|
2
|
+
|
|
3
|
+
A fork of [STRML/textFit](https://github.com/STRML/textFit) (@7a1eed6db54a97798556eed3c57b2ce1f87dbab4). Has the following changes:
|
|
4
|
+
|
|
5
|
+
- Convert to ESM module
|
|
6
|
+
- Remove .min.js file
|
|
7
|
+
- Convert to TypeScript (with small code changes to accommodate)
|
|
8
|
+
- Make inputs stricter
|
|
9
|
+
- Adds the following settings to [the defaults](https://github.com/STRML/textFit#default-settings):
|
|
10
|
+
```js
|
|
11
|
+
settings = {
|
|
12
|
+
...
|
|
13
|
+
/**
|
|
14
|
+
* Sets the final size to the max multiple of this. Useful when a font
|
|
15
|
+
* requires the font size to be a multiple of a number for optimal rendering.
|
|
16
|
+
*/
|
|
17
|
+
sizeMultipleOf: 1,
|
|
18
|
+
};
|
|
19
|
+
```
|
|
20
|
+
- Remove the following settings (to simplify the code):
|
|
21
|
+
- `alignHoriz`
|
|
22
|
+
- `alignVert`
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* textFit v2.3.1
|
|
3
|
+
* Previously known as jQuery.textFit
|
|
4
|
+
* 11/2014 by STRML (strml.github.com)
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* To use: textFit(document.getElementById('target-div'), options);
|
|
8
|
+
*
|
|
9
|
+
* Will make the *text* content inside a container scale to fit the container
|
|
10
|
+
* The container is required to have a set width and height
|
|
11
|
+
* Uses binary search to fit text with minimal layout calls.
|
|
12
|
+
* Version 2.0 does not use jQuery.
|
|
13
|
+
*/
|
|
14
|
+
interface Options {
|
|
15
|
+
/**
|
|
16
|
+
* if true, textFit will not set white-space: no-wrap
|
|
17
|
+
*/
|
|
18
|
+
multiLine?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* disable to turn off automatic multi-line sensing
|
|
21
|
+
*/
|
|
22
|
+
detectMultiLine?: boolean;
|
|
23
|
+
minFontSize?: number;
|
|
24
|
+
maxFontSize?: number;
|
|
25
|
+
/**
|
|
26
|
+
* if true, textFit will re-process already-fit nodes. Set to 'false' for
|
|
27
|
+
* better performance
|
|
28
|
+
*/
|
|
29
|
+
reProcess?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* if true, textFit will fit text to element width, regardless of text height
|
|
32
|
+
*/
|
|
33
|
+
widthOnly?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* if true, textFit will use flexbox for vertical alignment
|
|
36
|
+
*/
|
|
37
|
+
alignVertWithFlexbox?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Sets the final size to the max multiple of this. Useful when a font
|
|
40
|
+
* requires the font size to be a multiple of a number for optimal rendering.
|
|
41
|
+
*/
|
|
42
|
+
sizeMultipleOf?: number;
|
|
43
|
+
}
|
|
44
|
+
declare function textFit(elements: HTMLElement | HTMLElement[], options?: Options): void;
|
|
45
|
+
|
|
46
|
+
export { Options, textFit };
|
package/dist/textFit.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// src/textFit.ts
|
|
2
|
+
var defaultSettings = {
|
|
3
|
+
multiLine: false,
|
|
4
|
+
detectMultiLine: true,
|
|
5
|
+
minFontSize: 6,
|
|
6
|
+
maxFontSize: 80,
|
|
7
|
+
reProcess: true,
|
|
8
|
+
widthOnly: false,
|
|
9
|
+
alignVertWithFlexbox: false,
|
|
10
|
+
sizeMultipleOf: 1
|
|
11
|
+
};
|
|
12
|
+
function textFit(elements, options = {}) {
|
|
13
|
+
const settings = Object.assign({}, defaultSettings, options);
|
|
14
|
+
const els = Array.isArray(elements) ? elements : [elements];
|
|
15
|
+
for (var i = 0; i < els.length; i++) {
|
|
16
|
+
processItem(els[i], settings);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function processItem(el, settings) {
|
|
20
|
+
if (!isElement(el) || !settings.reProcess && el.getAttribute("textFitted")) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (!settings.reProcess) {
|
|
24
|
+
el.setAttribute("textFitted", "1");
|
|
25
|
+
}
|
|
26
|
+
let innerSpan, originalHeight, originalHTML, originalWidth;
|
|
27
|
+
let low, mid, high;
|
|
28
|
+
originalHTML = el.innerHTML;
|
|
29
|
+
originalWidth = innerWidth(el);
|
|
30
|
+
originalHeight = innerHeight(el);
|
|
31
|
+
if (!originalWidth || !settings.widthOnly && !originalHeight) {
|
|
32
|
+
if (!settings.widthOnly)
|
|
33
|
+
throw new Error("Set a static height and width on the target element " + el.outerHTML + " before using textFit!");
|
|
34
|
+
else
|
|
35
|
+
throw new Error("Set a static width on the target element " + el.outerHTML + " before using textFit!");
|
|
36
|
+
}
|
|
37
|
+
if (originalHTML.indexOf("textFitted") === -1) {
|
|
38
|
+
innerSpan = document.createElement("span");
|
|
39
|
+
innerSpan.className = "textFitted";
|
|
40
|
+
innerSpan.style["display"] = "inline-block";
|
|
41
|
+
innerSpan.innerHTML = originalHTML;
|
|
42
|
+
el.innerHTML = "";
|
|
43
|
+
el.appendChild(innerSpan);
|
|
44
|
+
} else {
|
|
45
|
+
const innerSpanNode = el.querySelector("span.textFitted");
|
|
46
|
+
if (innerSpanNode == null) {
|
|
47
|
+
throw new Error("Could not find `span.textFitted`");
|
|
48
|
+
} else if (!(innerSpanNode instanceof HTMLElement)) {
|
|
49
|
+
throw new Error("Element matching `span.textFitted` not a span");
|
|
50
|
+
}
|
|
51
|
+
innerSpan = innerSpanNode;
|
|
52
|
+
if (hasClass(innerSpan, "textFitAlignVert")) {
|
|
53
|
+
innerSpan.className = innerSpan.className.replace("textFitAlignVert", "");
|
|
54
|
+
innerSpan.style["height"] = "";
|
|
55
|
+
el.className.replace("textFitAlignVertFlex", "");
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
var multiLine = settings.multiLine;
|
|
59
|
+
if (settings.detectMultiLine && !multiLine && innerSpan.getBoundingClientRect().height >= parseInt(window.getComputedStyle(innerSpan).fontSize, 10) * 2) {
|
|
60
|
+
multiLine = true;
|
|
61
|
+
}
|
|
62
|
+
if (!multiLine) {
|
|
63
|
+
el.style.whiteSpace = "nowrap";
|
|
64
|
+
}
|
|
65
|
+
low = settings.minFontSize;
|
|
66
|
+
high = settings.maxFontSize;
|
|
67
|
+
var size = low;
|
|
68
|
+
while (low <= high) {
|
|
69
|
+
mid = high + low >> 1;
|
|
70
|
+
innerSpan.style.fontSize = mid + "px";
|
|
71
|
+
var innerSpanBoundingClientRect = innerSpan.getBoundingClientRect();
|
|
72
|
+
if (innerSpanBoundingClientRect.width <= originalWidth && (settings.widthOnly || innerSpanBoundingClientRect.height <= originalHeight)) {
|
|
73
|
+
size = mid;
|
|
74
|
+
low = mid + 1;
|
|
75
|
+
} else {
|
|
76
|
+
high = mid - 1;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (settings.sizeMultipleOf > 1) {
|
|
80
|
+
size = size % settings.sizeMultipleOf;
|
|
81
|
+
}
|
|
82
|
+
if (innerSpan.style.fontSize != size + "px")
|
|
83
|
+
innerSpan.style.fontSize = size + "px";
|
|
84
|
+
}
|
|
85
|
+
function innerHeight(el) {
|
|
86
|
+
var style = window.getComputedStyle(el, null);
|
|
87
|
+
return el.getBoundingClientRect().height - parseInt(style.getPropertyValue("padding-top"), 10) - parseInt(style.getPropertyValue("padding-bottom"), 10);
|
|
88
|
+
}
|
|
89
|
+
function innerWidth(el) {
|
|
90
|
+
var style = window.getComputedStyle(el, null);
|
|
91
|
+
return el.getBoundingClientRect().width - parseInt(style.getPropertyValue("padding-left"), 10) - parseInt(style.getPropertyValue("padding-right"), 10);
|
|
92
|
+
}
|
|
93
|
+
function isElement(o) {
|
|
94
|
+
return typeof HTMLElement === "object" ? o instanceof HTMLElement : o && typeof o === "object" && o !== null && o.nodeType === 1 && typeof o.nodeName === "string";
|
|
95
|
+
}
|
|
96
|
+
function hasClass(element, cls) {
|
|
97
|
+
return (" " + element.className + " ").indexOf(" " + cls + " ") > -1;
|
|
98
|
+
}
|
|
99
|
+
export {
|
|
100
|
+
textFit
|
|
101
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@altano/textfit",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A fast, jQuery-free text sizing component that quickly fits single and multi-line text to the width (and optionally height) of its container.",
|
|
5
|
+
"directories": {
|
|
6
|
+
"example": "examples"
|
|
7
|
+
},
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"type": "module",
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"exports": "./dist/textFit.js",
|
|
14
|
+
"types": "./dist/textFit.d.ts",
|
|
15
|
+
"scripts": {
|
|
16
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
17
|
+
"build": "tsup src/textFit.ts --format esm --dts",
|
|
18
|
+
"dev": "tsup src/textFit.ts --watch --format esm --dts",
|
|
19
|
+
"format": "prettier --write src",
|
|
20
|
+
"build-example": "sh examples/mk-examples.sh",
|
|
21
|
+
"prepublish": "npm run build"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/altano/npm-packages",
|
|
26
|
+
"directory": "packages/textFit"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"textfit",
|
|
30
|
+
"fit",
|
|
31
|
+
"text"
|
|
32
|
+
],
|
|
33
|
+
"author": "Samuel Reed <samuel.trace.reed@gmail.com> (http://strml.net/)",
|
|
34
|
+
"contributors": [
|
|
35
|
+
"Alan Norbauer <altano@gmail.com> (https://alan.norbauer.com/)"
|
|
36
|
+
],
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@altano/tsconfig": "*"
|
|
43
|
+
}
|
|
44
|
+
}
|