@auto-skeleton/core 0.0.2
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/dist/index.cjs +166 -0
- package/dist/index.d.mts +16 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +139 -0
- package/package.json +21 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
scanBones: () => scanBones
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
|
|
27
|
+
// src/scanner.ts
|
|
28
|
+
var DEFAULT_MIN_SIZE = 8;
|
|
29
|
+
var DEFAULT_TEXT_LINE_HEIGHT_FACTOR = 0.72;
|
|
30
|
+
var DEFAULT_TEXT_RADIUS = 4;
|
|
31
|
+
function isVisible(el) {
|
|
32
|
+
if (!(el instanceof HTMLElement)) return false;
|
|
33
|
+
const style = window.getComputedStyle(el);
|
|
34
|
+
if (style.display === "none" || style.visibility === "hidden") return false;
|
|
35
|
+
if (Number(style.opacity) === 0) return false;
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
function toRadius(style, width, height) {
|
|
39
|
+
const raw = parseFloat(style.borderRadius || "0");
|
|
40
|
+
if (Number.isNaN(raw)) return 0;
|
|
41
|
+
return Math.min(raw, Math.min(width, height) / 2);
|
|
42
|
+
}
|
|
43
|
+
function autoKind(width, height, radius) {
|
|
44
|
+
const nearSquare = Math.abs(width - height) <= 2;
|
|
45
|
+
const likelyCircle = nearSquare && radius >= Math.min(width, height) * 0.45;
|
|
46
|
+
return likelyCircle ? "circle" : "rect";
|
|
47
|
+
}
|
|
48
|
+
function forcedShape(el) {
|
|
49
|
+
const shape = el.dataset.skeletonShape;
|
|
50
|
+
if (shape === "circle" || shape === "rect") return shape;
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
function explicitLineCount(el) {
|
|
54
|
+
const raw = el.dataset.skeletonLines;
|
|
55
|
+
if (!raw) return null;
|
|
56
|
+
const parsed = Number.parseInt(raw, 10);
|
|
57
|
+
if (!Number.isFinite(parsed) || parsed <= 0) return null;
|
|
58
|
+
return parsed;
|
|
59
|
+
}
|
|
60
|
+
function shouldIgnore(el, options) {
|
|
61
|
+
if (el.dataset.noSkeleton !== void 0) return true;
|
|
62
|
+
if (el.dataset.skeletonIgnore !== void 0) return true;
|
|
63
|
+
if (options.ignoreSelectors?.some((selector) => el.matches(selector))) return true;
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
function isMarkedContainer(el) {
|
|
67
|
+
return el.dataset.skeletonContainer !== void 0;
|
|
68
|
+
}
|
|
69
|
+
function hasDirectTextNode(el) {
|
|
70
|
+
for (const n of Array.from(el.childNodes)) {
|
|
71
|
+
if (n.nodeType === Node.TEXT_NODE && n.textContent && n.textContent.trim().length > 0) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
function hasVisibleElementChildren(el) {
|
|
78
|
+
for (const c of Array.from(el.children)) {
|
|
79
|
+
if (isVisible(c)) return true;
|
|
80
|
+
}
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
function shouldUseTextMode(el, forced) {
|
|
84
|
+
return !forced && hasDirectTextNode(el);
|
|
85
|
+
}
|
|
86
|
+
function shouldSkipContainerBone(el, forced) {
|
|
87
|
+
if (forced) return false;
|
|
88
|
+
if (isMarkedContainer(el)) return true;
|
|
89
|
+
return hasVisibleElementChildren(el) && !hasDirectTextNode(el);
|
|
90
|
+
}
|
|
91
|
+
function makeTextBones(el, rootRect, minSize) {
|
|
92
|
+
const rect = el.getBoundingClientRect();
|
|
93
|
+
if (rect.width < minSize || rect.height < minSize) return [];
|
|
94
|
+
const style = window.getComputedStyle(el);
|
|
95
|
+
const fontSize = parseFloat(style.fontSize || "16") || 16;
|
|
96
|
+
const lineHeightRaw = parseFloat(style.lineHeight || "0");
|
|
97
|
+
const lineHeight = Number.isFinite(lineHeightRaw) && lineHeightRaw > 0 ? lineHeightRaw : fontSize * 1.4;
|
|
98
|
+
const estimatedLines = Math.max(1, Math.round(rect.height / lineHeight));
|
|
99
|
+
const lines = explicitLineCount(el) ?? estimatedLines;
|
|
100
|
+
const bones = [];
|
|
101
|
+
for (let i = 0; i < lines; i++) {
|
|
102
|
+
const isLast = i === lines - 1;
|
|
103
|
+
const y = rect.top - rootRect.top + i * lineHeight;
|
|
104
|
+
const height = Math.max(minSize, Math.round(lineHeight * DEFAULT_TEXT_LINE_HEIGHT_FACTOR));
|
|
105
|
+
const width = Math.round(rect.width * (isLast && lines > 1 ? 0.75 : 1));
|
|
106
|
+
bones.push({
|
|
107
|
+
x: Math.round(rect.left - rootRect.left),
|
|
108
|
+
y: Math.round(y),
|
|
109
|
+
width,
|
|
110
|
+
height,
|
|
111
|
+
radius: DEFAULT_TEXT_RADIUS,
|
|
112
|
+
kind: "rect"
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
return bones;
|
|
116
|
+
}
|
|
117
|
+
function scanBones(root, options = {}) {
|
|
118
|
+
const minSize = options.minSize ?? DEFAULT_MIN_SIZE;
|
|
119
|
+
const rootRect = root.getBoundingClientRect();
|
|
120
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
|
121
|
+
const bones = [];
|
|
122
|
+
let current = walker.currentNode;
|
|
123
|
+
while (current) {
|
|
124
|
+
if (isVisible(current) && !shouldIgnore(current, options)) {
|
|
125
|
+
const el = current;
|
|
126
|
+
const forced = forcedShape(el);
|
|
127
|
+
if (shouldUseTextMode(el, forced)) {
|
|
128
|
+
bones.push(...makeTextBones(el, rootRect, minSize));
|
|
129
|
+
} else if (!shouldSkipContainerBone(el, forced)) {
|
|
130
|
+
const rect = el.getBoundingClientRect();
|
|
131
|
+
const width = Math.round(rect.width);
|
|
132
|
+
const height = Math.round(rect.height);
|
|
133
|
+
if (width >= minSize && height >= minSize) {
|
|
134
|
+
const style = window.getComputedStyle(el);
|
|
135
|
+
const radius = toRadius(style, width, height);
|
|
136
|
+
bones.push({
|
|
137
|
+
x: Math.round(rect.left - rootRect.left),
|
|
138
|
+
y: Math.round(rect.top - rootRect.top),
|
|
139
|
+
width,
|
|
140
|
+
height,
|
|
141
|
+
radius,
|
|
142
|
+
kind: forced ?? autoKind(width, height, radius)
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
current = walker.nextNode();
|
|
148
|
+
}
|
|
149
|
+
return dedupeBones(bones);
|
|
150
|
+
}
|
|
151
|
+
function dedupeBones(bones) {
|
|
152
|
+
const seen = /* @__PURE__ */ new Set();
|
|
153
|
+
const output = [];
|
|
154
|
+
for (const b of bones) {
|
|
155
|
+
const key = `${b.x}:${b.y}:${b.width}:${b.height}:${b.radius}:${b.kind}`;
|
|
156
|
+
if (!seen.has(key)) {
|
|
157
|
+
seen.add(key);
|
|
158
|
+
output.push(b);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return output;
|
|
162
|
+
}
|
|
163
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
164
|
+
0 && (module.exports = {
|
|
165
|
+
scanBones
|
|
166
|
+
});
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
type Bone = {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
radius: number;
|
|
7
|
+
kind: "rect" | "circle";
|
|
8
|
+
};
|
|
9
|
+
type ScanOptions = {
|
|
10
|
+
ignoreSelectors?: string[];
|
|
11
|
+
minSize?: number;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
declare function scanBones(root: HTMLElement, options?: ScanOptions): Bone[];
|
|
15
|
+
|
|
16
|
+
export { type Bone, type ScanOptions, scanBones };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
type Bone = {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
radius: number;
|
|
7
|
+
kind: "rect" | "circle";
|
|
8
|
+
};
|
|
9
|
+
type ScanOptions = {
|
|
10
|
+
ignoreSelectors?: string[];
|
|
11
|
+
minSize?: number;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
declare function scanBones(root: HTMLElement, options?: ScanOptions): Bone[];
|
|
15
|
+
|
|
16
|
+
export { type Bone, type ScanOptions, scanBones };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// src/scanner.ts
|
|
2
|
+
var DEFAULT_MIN_SIZE = 8;
|
|
3
|
+
var DEFAULT_TEXT_LINE_HEIGHT_FACTOR = 0.72;
|
|
4
|
+
var DEFAULT_TEXT_RADIUS = 4;
|
|
5
|
+
function isVisible(el) {
|
|
6
|
+
if (!(el instanceof HTMLElement)) return false;
|
|
7
|
+
const style = window.getComputedStyle(el);
|
|
8
|
+
if (style.display === "none" || style.visibility === "hidden") return false;
|
|
9
|
+
if (Number(style.opacity) === 0) return false;
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
function toRadius(style, width, height) {
|
|
13
|
+
const raw = parseFloat(style.borderRadius || "0");
|
|
14
|
+
if (Number.isNaN(raw)) return 0;
|
|
15
|
+
return Math.min(raw, Math.min(width, height) / 2);
|
|
16
|
+
}
|
|
17
|
+
function autoKind(width, height, radius) {
|
|
18
|
+
const nearSquare = Math.abs(width - height) <= 2;
|
|
19
|
+
const likelyCircle = nearSquare && radius >= Math.min(width, height) * 0.45;
|
|
20
|
+
return likelyCircle ? "circle" : "rect";
|
|
21
|
+
}
|
|
22
|
+
function forcedShape(el) {
|
|
23
|
+
const shape = el.dataset.skeletonShape;
|
|
24
|
+
if (shape === "circle" || shape === "rect") return shape;
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
function explicitLineCount(el) {
|
|
28
|
+
const raw = el.dataset.skeletonLines;
|
|
29
|
+
if (!raw) return null;
|
|
30
|
+
const parsed = Number.parseInt(raw, 10);
|
|
31
|
+
if (!Number.isFinite(parsed) || parsed <= 0) return null;
|
|
32
|
+
return parsed;
|
|
33
|
+
}
|
|
34
|
+
function shouldIgnore(el, options) {
|
|
35
|
+
if (el.dataset.noSkeleton !== void 0) return true;
|
|
36
|
+
if (el.dataset.skeletonIgnore !== void 0) return true;
|
|
37
|
+
if (options.ignoreSelectors?.some((selector) => el.matches(selector))) return true;
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
function isMarkedContainer(el) {
|
|
41
|
+
return el.dataset.skeletonContainer !== void 0;
|
|
42
|
+
}
|
|
43
|
+
function hasDirectTextNode(el) {
|
|
44
|
+
for (const n of Array.from(el.childNodes)) {
|
|
45
|
+
if (n.nodeType === Node.TEXT_NODE && n.textContent && n.textContent.trim().length > 0) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
function hasVisibleElementChildren(el) {
|
|
52
|
+
for (const c of Array.from(el.children)) {
|
|
53
|
+
if (isVisible(c)) return true;
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
function shouldUseTextMode(el, forced) {
|
|
58
|
+
return !forced && hasDirectTextNode(el);
|
|
59
|
+
}
|
|
60
|
+
function shouldSkipContainerBone(el, forced) {
|
|
61
|
+
if (forced) return false;
|
|
62
|
+
if (isMarkedContainer(el)) return true;
|
|
63
|
+
return hasVisibleElementChildren(el) && !hasDirectTextNode(el);
|
|
64
|
+
}
|
|
65
|
+
function makeTextBones(el, rootRect, minSize) {
|
|
66
|
+
const rect = el.getBoundingClientRect();
|
|
67
|
+
if (rect.width < minSize || rect.height < minSize) return [];
|
|
68
|
+
const style = window.getComputedStyle(el);
|
|
69
|
+
const fontSize = parseFloat(style.fontSize || "16") || 16;
|
|
70
|
+
const lineHeightRaw = parseFloat(style.lineHeight || "0");
|
|
71
|
+
const lineHeight = Number.isFinite(lineHeightRaw) && lineHeightRaw > 0 ? lineHeightRaw : fontSize * 1.4;
|
|
72
|
+
const estimatedLines = Math.max(1, Math.round(rect.height / lineHeight));
|
|
73
|
+
const lines = explicitLineCount(el) ?? estimatedLines;
|
|
74
|
+
const bones = [];
|
|
75
|
+
for (let i = 0; i < lines; i++) {
|
|
76
|
+
const isLast = i === lines - 1;
|
|
77
|
+
const y = rect.top - rootRect.top + i * lineHeight;
|
|
78
|
+
const height = Math.max(minSize, Math.round(lineHeight * DEFAULT_TEXT_LINE_HEIGHT_FACTOR));
|
|
79
|
+
const width = Math.round(rect.width * (isLast && lines > 1 ? 0.75 : 1));
|
|
80
|
+
bones.push({
|
|
81
|
+
x: Math.round(rect.left - rootRect.left),
|
|
82
|
+
y: Math.round(y),
|
|
83
|
+
width,
|
|
84
|
+
height,
|
|
85
|
+
radius: DEFAULT_TEXT_RADIUS,
|
|
86
|
+
kind: "rect"
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return bones;
|
|
90
|
+
}
|
|
91
|
+
function scanBones(root, options = {}) {
|
|
92
|
+
const minSize = options.minSize ?? DEFAULT_MIN_SIZE;
|
|
93
|
+
const rootRect = root.getBoundingClientRect();
|
|
94
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_ELEMENT);
|
|
95
|
+
const bones = [];
|
|
96
|
+
let current = walker.currentNode;
|
|
97
|
+
while (current) {
|
|
98
|
+
if (isVisible(current) && !shouldIgnore(current, options)) {
|
|
99
|
+
const el = current;
|
|
100
|
+
const forced = forcedShape(el);
|
|
101
|
+
if (shouldUseTextMode(el, forced)) {
|
|
102
|
+
bones.push(...makeTextBones(el, rootRect, minSize));
|
|
103
|
+
} else if (!shouldSkipContainerBone(el, forced)) {
|
|
104
|
+
const rect = el.getBoundingClientRect();
|
|
105
|
+
const width = Math.round(rect.width);
|
|
106
|
+
const height = Math.round(rect.height);
|
|
107
|
+
if (width >= minSize && height >= minSize) {
|
|
108
|
+
const style = window.getComputedStyle(el);
|
|
109
|
+
const radius = toRadius(style, width, height);
|
|
110
|
+
bones.push({
|
|
111
|
+
x: Math.round(rect.left - rootRect.left),
|
|
112
|
+
y: Math.round(rect.top - rootRect.top),
|
|
113
|
+
width,
|
|
114
|
+
height,
|
|
115
|
+
radius,
|
|
116
|
+
kind: forced ?? autoKind(width, height, radius)
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
current = walker.nextNode();
|
|
122
|
+
}
|
|
123
|
+
return dedupeBones(bones);
|
|
124
|
+
}
|
|
125
|
+
function dedupeBones(bones) {
|
|
126
|
+
const seen = /* @__PURE__ */ new Set();
|
|
127
|
+
const output = [];
|
|
128
|
+
for (const b of bones) {
|
|
129
|
+
const key = `${b.x}:${b.y}:${b.width}:${b.height}:${b.radius}:${b.kind}`;
|
|
130
|
+
if (!seen.has(key)) {
|
|
131
|
+
seen.add(key);
|
|
132
|
+
output.push(b);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return output;
|
|
136
|
+
}
|
|
137
|
+
export {
|
|
138
|
+
scanBones
|
|
139
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@auto-skeleton/core",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"main": "dist/index.cjs",
|
|
5
|
+
"module": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js",
|
|
11
|
+
"require": "./dist/index.cjs"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsup",
|
|
19
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
20
|
+
}
|
|
21
|
+
}
|