@altano/textfit 0.0.2 → 0.0.4

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 CHANGED
@@ -1,4 +1,4 @@
1
- # `textFit` - Hassle-Free Text Fitting
1
+ # `@altano/textfit` - Hassle-Free Text Fitting
2
2
 
3
3
  A fork of [STRML/textFit](https://github.com/STRML/textFit) (@7a1eed6db54a97798556eed3c57b2ce1f87dbab4). Has the following changes:
4
4
 
@@ -11,7 +11,7 @@
11
11
  * Uses binary search to fit text with minimal layout calls.
12
12
  * Version 2.0 does not use jQuery.
13
13
  */
14
- interface Options {
14
+ export interface Options {
15
15
  /**
16
16
  * if true, textFit will not set white-space: no-wrap
17
17
  */
@@ -41,6 +41,5 @@ interface Options {
41
41
  */
42
42
  sizeMultipleOf?: number;
43
43
  }
44
- declare function textFit(elements: HTMLElement | HTMLElement[], options?: Options): void;
45
-
46
- export { Options, textFit };
44
+ export declare function textFit(elements: HTMLElement | HTMLElement[], options?: Options): void;
45
+ //# sourceMappingURL=index.d.ts.map
@@ -1,4 +1,4 @@
1
- // src/textFit.ts
1
+ // src/index.ts
2
2
  var defaultSettings = {
3
3
  multiLine: false,
4
4
  detectMultiLine: true,
@@ -12,9 +12,9 @@ var defaultSettings = {
12
12
  function textFit(elements, options = {}) {
13
13
  const settings = Object.assign({}, defaultSettings, options);
14
14
  const els = Array.isArray(elements) ? elements : [elements];
15
- for (var i = 0; i < els.length; i++) {
16
- processItem(els[i], settings);
17
- }
15
+ els.forEach((el) => {
16
+ processItem(el, settings);
17
+ });
18
18
  }
19
19
  function processItem(el, settings) {
20
20
  if (!isElement(el) || !settings.reProcess && el.getAttribute("textFitted")) {
@@ -23,16 +23,20 @@ function processItem(el, settings) {
23
23
  if (!settings.reProcess) {
24
24
  el.setAttribute("textFitted", "1");
25
25
  }
26
- let innerSpan, originalHeight, originalHTML, originalWidth;
26
+ let innerSpan;
27
27
  let low, mid, high;
28
- originalHTML = el.innerHTML;
29
- originalWidth = innerWidth(el);
30
- originalHeight = innerHeight(el);
28
+ const originalHTML = el.innerHTML;
29
+ const originalWidth = innerWidth(el);
30
+ const originalHeight = innerHeight(el);
31
31
  if (!originalWidth || !settings.widthOnly && !originalHeight) {
32
32
  if (!settings.widthOnly)
33
- throw new Error("Set a static height and width on the target element " + el.outerHTML + " before using textFit!");
33
+ throw new Error(
34
+ "Set a static height and width on the target element " + el.outerHTML + " before using textFit!"
35
+ );
34
36
  else
35
- throw new Error("Set a static width on the target element " + el.outerHTML + " before using textFit!");
37
+ throw new Error(
38
+ "Set a static width on the target element " + el.outerHTML + " before using textFit!"
39
+ );
36
40
  }
37
41
  if (originalHTML.indexOf("textFitted") === -1) {
38
42
  innerSpan = document.createElement("span");
@@ -55,7 +59,7 @@ function processItem(el, settings) {
55
59
  el.className.replace("textFitAlignVertFlex", "");
56
60
  }
57
61
  }
58
- var multiLine = settings.multiLine;
62
+ let multiLine = settings.multiLine;
59
63
  if (settings.detectMultiLine && !multiLine && innerSpan.getBoundingClientRect().height >= parseInt(window.getComputedStyle(innerSpan).fontSize, 10) * 2) {
60
64
  multiLine = true;
61
65
  }
@@ -64,11 +68,11 @@ function processItem(el, settings) {
64
68
  }
65
69
  low = settings.minFontSize;
66
70
  high = settings.maxFontSize;
67
- var size = low;
71
+ let size = low;
68
72
  while (low <= high) {
69
73
  mid = high + low >> 1;
70
74
  innerSpan.style.fontSize = mid + "px";
71
- var innerSpanBoundingClientRect = innerSpan.getBoundingClientRect();
75
+ const innerSpanBoundingClientRect = innerSpan.getBoundingClientRect();
72
76
  if (innerSpanBoundingClientRect.width <= originalWidth && (settings.widthOnly || innerSpanBoundingClientRect.height <= originalHeight)) {
73
77
  size = mid;
74
78
  low = mid + 1;
@@ -83,15 +87,15 @@ function processItem(el, settings) {
83
87
  innerSpan.style.fontSize = size + "px";
84
88
  }
85
89
  function innerHeight(el) {
86
- var style = window.getComputedStyle(el, null);
90
+ const style = window.getComputedStyle(el, null);
87
91
  return el.getBoundingClientRect().height - parseInt(style.getPropertyValue("padding-top"), 10) - parseInt(style.getPropertyValue("padding-bottom"), 10);
88
92
  }
89
93
  function innerWidth(el) {
90
- var style = window.getComputedStyle(el, null);
94
+ const style = window.getComputedStyle(el, null);
91
95
  return el.getBoundingClientRect().width - parseInt(style.getPropertyValue("padding-left"), 10) - parseInt(style.getPropertyValue("padding-right"), 10);
92
96
  }
93
97
  function isElement(o) {
94
- return typeof HTMLElement === "object" ? o instanceof HTMLElement : o && typeof o === "object" && o !== null && o.nodeType === 1 && typeof o.nodeName === "string";
98
+ return typeof HTMLElement === "object" ? o instanceof HTMLElement : o != null && typeof o === "object" && o !== null && "nodeType" in o && o.nodeType === 1 && "nodeName" in o && typeof o.nodeName === "string";
95
99
  }
96
100
  function hasClass(element, cls) {
97
101
  return (" " + element.className + " ").indexOf(" " + cls + " ") > -1;
package/package.json CHANGED
@@ -1,29 +1,21 @@
1
1
  {
2
2
  "name": "@altano/textfit",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
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
5
  "files": [
9
- "dist"
6
+ "dist/**/*.js",
7
+ "dist/**/*.d.ts"
10
8
  ],
11
9
  "type": "module",
12
10
  "sideEffects": false,
13
- "exports": "./dist/textFit.js",
14
- "types": "./dist/textFit.d.ts",
15
- "scripts": {
16
- "build": "tsup src/textFit.ts --format esm --dts",
17
- "dev": "tsup src/textFit.ts --watch --format esm --dts",
18
- "format": "prettier --write src",
19
- "build-example": "sh examples/mk-examples.sh",
20
- "prepublish": "npm run build"
21
- },
11
+ "exports": "./dist/index.js",
12
+ "types": "./dist/index.d.ts",
22
13
  "repository": {
23
14
  "type": "git",
24
15
  "url": "https://github.com/altano/npm-packages",
25
- "directory": "packages/textFit"
16
+ "directory": "packages/textfit"
26
17
  },
18
+ "homepage": "https://github.com/altano/npm-packages/tree/main/packages/textfit",
27
19
  "keywords": [
28
20
  "textfit",
29
21
  "fit",
@@ -34,10 +26,28 @@
34
26
  "Alan Norbauer <altano@gmail.com> (https://alan.norbauer.com/)"
35
27
  ],
36
28
  "license": "MIT",
29
+ "devDependencies": {
30
+ "@playwright/test": "1.40.0",
31
+ "typescript": "^5.4.2",
32
+ "vite": "^5.2.4",
33
+ "@altano/tsconfig": "0.0.0"
34
+ },
37
35
  "publishConfig": {
38
36
  "access": "public"
39
37
  },
40
- "devDependencies": {
41
- "@altano/tsconfig": "*"
38
+ "scripts": {
39
+ "build": "tsup --config build-config/tsup.config.browser.ts",
40
+ "build:types": "tsc --project tsconfig.declarations.json",
41
+ "dev": "pnpm run build --watch",
42
+ "format": "prettier --check src",
43
+ "format:fix": "prettier --write src",
44
+ "lint": "eslint package.json src/**/*.ts*",
45
+ "lint:fix": "TIMING=1 pnpm lint --fix",
46
+ "lint:timing": "TIMING=1 pnpm lint",
47
+ "test:e2e": "playwright test",
48
+ "test:e2e:server": "vite --strictPort --port 8173 ./tests/e2e",
49
+ "test:e2e:ui": "playwright test --ui",
50
+ "typecheck:src": "tsc --noEmit",
51
+ "typecheck:test": "tsc --noEmit --project ./tests/tsconfig.json"
42
52
  }
43
- }
53
+ }