Files
gpx.studio/website/src/lib/utils.ts

107 lines
2.8 KiB
TypeScript
Raw Normal View History

2024-04-08 17:12:39 +02:00
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
import { cubicOut } from "svelte/easing";
import type { TransitionConfig } from "svelte/transition";
2024-06-06 11:58:50 +02:00
import { get } from "svelte/store";
import { map } from "./stores";
2024-07-08 19:13:31 +02:00
import { base } from "$app/paths";
2024-04-08 17:12:39 +02:00
export function cn(...inputs: ClassValue[]) {
2024-06-06 11:58:50 +02:00
return twMerge(clsx(inputs));
2024-04-08 17:12:39 +02:00
}
type FlyAndScaleParams = {
2024-06-06 11:58:50 +02:00
y?: number;
x?: number;
start?: number;
duration?: number;
2024-04-08 17:12:39 +02:00
};
export const flyAndScale = (
2024-06-06 11:58:50 +02:00
node: Element,
params: FlyAndScaleParams = { y: -8, x: 0, start: 0.95, duration: 150 }
2024-04-08 17:12:39 +02:00
): TransitionConfig => {
2024-06-06 11:58:50 +02:00
const style = getComputedStyle(node);
const transform = style.transform === "none" ? "" : style.transform;
const scaleConversion = (
valueA: number,
scaleA: [number, number],
scaleB: [number, number]
) => {
const [minA, maxA] = scaleA;
const [minB, maxB] = scaleB;
const percentage = (valueA - minA) / (maxA - minA);
const valueB = percentage * (maxB - minB) + minB;
return valueB;
};
const styleToString = (
style: Record<string, number | string | undefined>
): string => {
return Object.keys(style).reduce((str, key) => {
if (style[key] === undefined) return str;
return str + `${key}:${style[key]};`;
}, "");
};
return {
duration: params.duration ?? 200,
delay: 0,
css: (t) => {
const y = scaleConversion(t, [0, 1], [params.y ?? 5, 0]);
const x = scaleConversion(t, [0, 1], [params.x ?? 0, 0]);
const scale = scaleConversion(t, [0, 1], [params.start ?? 0.95, 1]);
return styleToString({
transform: `${transform} translate3d(${x}px, ${y}px, 0) scale(${scale})`,
opacity: t
});
},
easing: cubicOut
};
};
let previousCursors: string[] = [];
export function setCursor(cursor: string) {
let m = get(map);
if (m) {
previousCursors.push(m.getCanvas().style.cursor);
m.getCanvas().style.cursor = cursor;
}
}
export function resetCursor() {
let m = get(map);
if (m) {
m.getCanvas().style.cursor = previousCursors.pop() ?? '';
}
}
export function setPointerCursor() {
setCursor('pointer');
}
export function setGrabbingCursor() {
setCursor('grabbing');
}
export function setCrosshairCursor() {
setCursor('crosshair');
2024-07-08 19:13:31 +02:00
}
export function getURLForLanguage(route: string | null, lang: string | null | undefined): string {
if (route === null) {
return base + '/';
}
let url = route.replace('[...language]', (lang === null || lang === undefined) ? 'en' : lang).replace('/en', '');
if (url === '') {
return base + '/';
} else {
return base + url;
}
2024-06-06 11:58:50 +02:00
}