From 5f2595ae30b3ef07966e41f730a56a975f5e90a9 Mon Sep 17 00:00:00 2001 From: Florian <45694132+flo-bit@users.noreply.github.com> Date: Wed, 14 Jan 2026 04:34:15 +0100 Subject: [PATCH] small fixes --- src/lib/Profile.svelte | 2 +- src/lib/helper.ts | 43 +++++++++++++++++++++++++++++++++++++++++ src/lib/website/load.ts | 20 +++++++++++++++++-- 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/lib/Profile.svelte b/src/lib/Profile.svelte index 4692077..e92233d 100644 --- a/src/lib/Profile.svelte +++ b/src/lib/Profile.svelte @@ -41,7 +41,7 @@ {profileData?.displayName || handle} -
+
diff --git a/src/lib/helper.ts b/src/lib/helper.ts index c6600e1..520ce32 100644 --- a/src/lib/helper.ts +++ b/src/lib/helper.ts @@ -97,6 +97,49 @@ export function fixCollisions(items: Item[], movedItem: Item, mobile: boolean = compactItems(items, mobile); } +// Fix all collisions between items (not just one moved item) +// Items higher on the page have priority and stay in place +export function fixAllCollisions(items: Item[], mobile: boolean = false) { + // Sort by Y position (top-to-bottom, then left-to-right) + // Items at the top have priority and won't be moved + const sortedItems = items.toSorted((a, b) => + mobile ? a.mobileY - b.mobileY || a.mobileX - b.mobileX : a.y - b.y || a.x - b.x + ); + + // Process each item and push it down if it overlaps with any item above it + for (let i = 0; i < sortedItems.length; i++) { + const item = sortedItems[i]; + + // Clamp X to valid range + if (mobile) { + item.mobileX = clamp(item.mobileX, 0, COLUMNS - item.mobileW); + } else { + item.x = clamp(item.x, 0, COLUMNS - item.w); + } + + // Check for collisions with all items that come before (higher priority) + let hasCollision = true; + while (hasCollision) { + hasCollision = false; + for (let j = 0; j < i; j++) { + const other = sortedItems[j]; + if (overlaps(item, other, mobile)) { + // Push item down below the colliding item + if (mobile) { + item.mobileY = other.mobileY + other.mobileH; + } else { + item.y = other.y + other.h; + } + hasCollision = true; + break; // Restart collision check from the beginning + } + } + } + } + + compactItems(items, mobile); +} + // Move all items up as far as possible without collisions export function compactItems(items: Item[], mobile: boolean = false) { // Sort by Y position (top-to-bottom) so upper items settle first. diff --git a/src/lib/website/load.ts b/src/lib/website/load.ts index 5ad2031..4288e9b 100644 --- a/src/lib/website/load.ts +++ b/src/lib/website/load.ts @@ -9,6 +9,7 @@ import type { Record as ListRecord } from '@atproto/api/dist/client/types/com/at import { data } from './data'; import { CardDefinitionsByType } from '$lib/cards'; import type { Item } from '$lib/types'; +import { compactItems, fixAllCollisions, fixCollisions } from '$lib/helper'; type LoadedData = { did: string; @@ -38,7 +39,7 @@ export async function loadData( timePassed, 'seconds ago' ); - return migrateData(JSON.parse(cachedResult)); + return checkData(migrateData(JSON.parse(cachedResult))); } } catch (error) { console.log('getting cached result failed', error); @@ -146,7 +147,7 @@ export async function loadData( await platform?.env?.USER_DATA_CACHE?.put(handle, JSON.stringify(result)); - return migrateData(result); + return checkData(migrateData(result)); } function migrateFromV0ToV1(data: LoadedData): LoadedData { @@ -166,6 +167,21 @@ function migrateFromV0ToV1(data: LoadedData): LoadedData { return data; } +function checkData(data: LoadedData): LoadedData { + const cards = Object.values(data.data['app.blento.card']).map((i) => i.value) as Item[]; + + console.log(cards); + if (cards.length > 0) { + fixAllCollisions(cards); + fixAllCollisions(cards, true); + + compactItems(cards); + compactItems(cards, true); + } + + return data; +} + function migrateData(data: LoadedData): LoadedData { return migrateFromV0ToV1(data); } -- 2.51.2