@tarve/svelte 0.0.0-stage → 0.8.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 +54 -3
- package/package.json +50 -4
- package/src/TitleBar.svelte +44 -0
- package/src/bun-preload.ts +4 -0
- package/src/bun.ts +13 -0
- package/src/compiler.ts +39 -0
- package/src/css.ts +606 -0
- package/src/dom-animation.ts +89 -0
- package/src/dom.ts +1533 -0
- package/src/editable.ts +372 -0
- package/src/elements.d.ts +43 -0
- package/src/events.ts +90 -0
- package/src/index.ts +126 -0
- package/src/install.ts +4 -0
- package/src/plugin.ts +83 -0
- package/src/render.ts +466 -0
- package/src/resolve.ts +39 -0
- package/src/selectors.ts +309 -0
- package/src/svelte-client.ts +8 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
|
|
2
|
+
export type Keyframe = { offset: number; values: Record<string, string> };
|
|
3
|
+
|
|
4
|
+
/** Keyframes with offsets (missing ones spread evenly, as Web Animations does). */
|
|
5
|
+
export function keyframesOf(input: Record<string, string | number | null | undefined>[]): Keyframe[] {
|
|
6
|
+
const frames = input.map(frame => {
|
|
7
|
+
const values: Record<string, string> = {};
|
|
8
|
+
for (const [key, value] of Object.entries(frame)) if (key !== "offset" && key !== "easing" && key !== "composite" && value != null) values[key] = String(value);
|
|
9
|
+
return { offset: typeof frame.offset === "number" ? frame.offset : NaN, values };
|
|
10
|
+
});
|
|
11
|
+
if (frames.length === 1) frames.unshift({ offset: 0, values: { ...frames[0]!.values } });
|
|
12
|
+
frames.forEach((frame, index) => { if (Number.isNaN(frame.offset)) frame.offset = frames.length > 1 ? index / (frames.length - 1) : 0; });
|
|
13
|
+
return frames.sort((a, b) => a.offset - b.offset);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const COLOR = /#([0-9a-f]{3,8})\b|rgba?\(([^)]*)\)/gi;
|
|
17
|
+
/** Colours as `rgba(r, g, b, a)`, so they interpolate as numbers. */
|
|
18
|
+
export function normalizeColors(value: string): string {
|
|
19
|
+
return value.replace(COLOR, (_match, hex: string | undefined, rgb: string | undefined) => {
|
|
20
|
+
if (rgb !== undefined) {
|
|
21
|
+
const parts = rgb.split(/[\s,/]+/).filter(Boolean).map(Number);
|
|
22
|
+
return `rgba(${parts[0] ?? 0}, ${parts[1] ?? 0}, ${parts[2] ?? 0}, ${parts[3] ?? 1})`;
|
|
23
|
+
}
|
|
24
|
+
let digits = hex!;
|
|
25
|
+
if (digits.length <= 4) digits = [...digits].map(char => char + char).join("");
|
|
26
|
+
const value = (index: number) => parseInt(digits.slice(index, index + 2), 16);
|
|
27
|
+
return `rgba(${value(0)}, ${value(2)}, ${value(4)}, ${digits.length === 8 ? value(6) / 255 : 1})`;
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const NUMBER = /-?\d*\.?\d+(?:e-?\d+)?/gi;
|
|
32
|
+
/** Interpolates two CSS values number by number when they share a shape; else switches halfway. */
|
|
33
|
+
export function mix(from: string, to: string, t: number): string {
|
|
34
|
+
const a = normalizeColors(from), b = normalizeColors(to);
|
|
35
|
+
const shapeA = a.replace(NUMBER, "#"), shapeB = b.replace(NUMBER, "#");
|
|
36
|
+
if (shapeA !== shapeB) return t < 0.5 ? from : to;
|
|
37
|
+
const numbersB = b.match(NUMBER) ?? [];
|
|
38
|
+
let index = 0;
|
|
39
|
+
return a.replace(NUMBER, number => {
|
|
40
|
+
const start = Number(number), end = Number(numbersB[index++]);
|
|
41
|
+
const value = start + (end - start) * t;
|
|
42
|
+
return String(Math.round(value * 1000) / 1000);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function interpolateFrames(frames: Keyframe[], progress: number): Record<string, string> | undefined {
|
|
47
|
+
if (!frames.length) return undefined;
|
|
48
|
+
if (progress <= frames[0]!.offset) return frames[0]!.values;
|
|
49
|
+
for (let index = 1; index < frames.length; index++) {
|
|
50
|
+
const previous = frames[index - 1]!, next = frames[index]!;
|
|
51
|
+
if (progress > next.offset) continue;
|
|
52
|
+
const span = next.offset - previous.offset;
|
|
53
|
+
const t = span ? (progress - previous.offset) / span : 1;
|
|
54
|
+
const out: Record<string, string> = {};
|
|
55
|
+
for (const key of new Set([...Object.keys(previous.values), ...Object.keys(next.values)])) {
|
|
56
|
+
const from = previous.values[key], to = next.values[key];
|
|
57
|
+
out[key] = from === undefined ? to! : to === undefined ? from : mix(from, to, t);
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
return frames[frames.length - 1]!.values;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** A CSS timing function as a function of progress. */
|
|
65
|
+
export function easing(name: string): (t: number) => number {
|
|
66
|
+
const bezier = (x1: number, y1: number, x2: number, y2: number) => (t: number) => {
|
|
67
|
+
// Solve x(u) = t by Newton's method, then return y(u).
|
|
68
|
+
let u = t;
|
|
69
|
+
for (let index = 0; index < 8; index++) {
|
|
70
|
+
const x = 3 * x1 * u * (1 - u) ** 2 + 3 * x2 * u ** 2 * (1 - u) + u ** 3 - t;
|
|
71
|
+
const slope = 3 * x1 * (1 - u) ** 2 + 6 * (x2 - x1) * u * (1 - u) + 3 * (1 - x2) * u ** 2;
|
|
72
|
+
if (Math.abs(x) < 1e-5 || !slope) break;
|
|
73
|
+
u = Math.min(1, Math.max(0, u - x / slope));
|
|
74
|
+
}
|
|
75
|
+
return 3 * y1 * u * (1 - u) ** 2 + 3 * y2 * u ** 2 * (1 - u) + u ** 3;
|
|
76
|
+
};
|
|
77
|
+
const value = name.trim();
|
|
78
|
+
if (value === "ease") return bezier(0.25, 0.1, 0.25, 1);
|
|
79
|
+
if (value === "ease-in") return bezier(0.42, 0, 1, 1);
|
|
80
|
+
if (value === "ease-out") return bezier(0, 0, 0.58, 1);
|
|
81
|
+
if (value === "ease-in-out") return bezier(0.42, 0, 0.58, 1);
|
|
82
|
+
const cubic = /^cubic-bezier\(([^)]*)\)$/.exec(value);
|
|
83
|
+
if (cubic) { const [a = 0, b = 0, c = 1, e = 1] = cubic[1]!.split(",").map(Number); return bezier(a, b, c, e); }
|
|
84
|
+
const steps = /^steps\(\s*(\d+)\s*(?:,\s*(start|end|jump-start|jump-end))?\s*\)$/.exec(value);
|
|
85
|
+
if (steps) { const count = Number(steps[1]); const start = steps[2]?.includes("start"); return t => Math.min(1, (start ? Math.ceil(t * count) : Math.floor(t * count)) / count); }
|
|
86
|
+
if (value === "step-start") return t => (t > 0 ? 1 : 0);
|
|
87
|
+
if (value === "step-end") return t => (t >= 1 ? 1 : 0);
|
|
88
|
+
return t => t;
|
|
89
|
+
}
|