your personal website on atproto - mirror blento.app
at copy-page 471 lines 16 kB view raw
1<script lang="ts"> 2 import type { WithElementRef } from 'bits-ui'; 3 import type { HTMLAttributes } from 'svelte/elements'; 4 import BaseCard from './BaseCard.svelte'; 5 import type { Item } from '$lib/types'; 6 import { Button, cn, Label, Popover } from '@foxui/core'; 7 import { ColorSelect } from '@foxui/colors'; 8 import { AllCardDefinitions, CardDefinitionsByType, getColor } from '../..'; 9 import { COLUMNS } from '$lib'; 10 import { 11 getCanEdit, 12 getIsCoarse, 13 getIsMobile, 14 getSelectedCardId, 15 getSelectCard 16 } from '$lib/website/context'; 17 import PlainTextEditor from '$lib/components/PlainTextEditor.svelte'; 18 19 let colorsChoices = [ 20 { class: 'text-base-500', label: 'base' }, 21 { class: 'text-accent-500', label: 'accent' }, 22 { class: 'text-base-300 dark:text-base-700', label: 'transparent' }, 23 { class: 'text-red-500', label: 'red' }, 24 { class: 'text-orange-500', label: 'orange' }, 25 { class: 'text-amber-500', label: 'amber' }, 26 { class: 'text-yellow-500', label: 'yellow' }, 27 { class: 'text-lime-500', label: 'lime' }, 28 { class: 'text-green-500', label: 'green' }, 29 { class: 'text-emerald-500', label: 'emerald' }, 30 { class: 'text-teal-500', label: 'teal' }, 31 { class: 'text-cyan-500', label: 'cyan' }, 32 { class: 'text-sky-500', label: 'sky' }, 33 { class: 'text-blue-500', label: 'blue' }, 34 { class: 'text-indigo-500', label: 'indigo' }, 35 { class: 'text-violet-500', label: 'violet' }, 36 { class: 'text-purple-500', label: 'purple' }, 37 { class: 'text-fuchsia-500', label: 'fuchsia' }, 38 { class: 'text-pink-500', label: 'pink' }, 39 { class: 'text-rose-500', label: 'rose' } 40 ]; 41 42 export type BaseEditingCardProps = { 43 item: Item; 44 ondelete: () => void; 45 onsetsize: (newW: number, newH: number) => void; 46 } & WithElementRef<HTMLAttributes<HTMLDivElement>>; 47 48 let { 49 item = $bindable(), 50 children, 51 ref = $bindable(null), 52 onsetsize, 53 ondelete, 54 ...rest 55 }: BaseEditingCardProps = $props(); 56 57 let selectedColor = $derived(colorsChoices.find((c) => getColor(item) === c.label)); 58 59 let canEdit = getCanEdit(); 60 let isMobile = getIsMobile(); 61 let isCoarse = getIsCoarse(); 62 63 let selectedCardId = getSelectedCardId(); 64 let selectCard = getSelectCard(); 65 let isSelected = $derived(selectedCardId?.() === item.id); 66 let isDimmed = $derived(isCoarse?.() && selectedCardId?.() != null && !isSelected); 67 68 let colorPopoverOpen = $state(false); 69 70 const cardDef = $derived(CardDefinitionsByType[item.cardType] ?? {}); 71 72 const minW = $derived(cardDef.minW ?? (isMobile() ? 2 : 2)); 73 const minH = $derived(cardDef.minH ?? (isMobile() ? 2 : 2)); 74 75 const maxW = $derived(cardDef.maxW ?? COLUMNS); 76 const maxH = $derived(cardDef.maxH ?? (isMobile() ? 12 : 6)); 77 78 // Resize handle state 79 let isResizing = $state(false); 80 let resizeStartX = $state(0); 81 let resizeStartY = $state(0); 82 let resizeStartW = $state(0); 83 let resizeStartH = $state(0); 84 85 function handleResizeStart(e: PointerEvent) { 86 e.preventDefault(); 87 e.stopPropagation(); 88 isResizing = true; 89 resizeStartX = e.clientX; 90 resizeStartY = e.clientY; 91 // For mobile view, sizes are doubled so we need to account for that 92 resizeStartW = isMobile() ? (item.mobileW ?? item.w) : item.w; 93 resizeStartH = isMobile() ? (item.mobileH ?? item.h) : item.h; 94 95 document.addEventListener('pointermove', handleResizeMove); 96 document.addEventListener('pointerup', handleResizeEnd); 97 } 98 99 function handleResizeMove(e: PointerEvent) { 100 if (!isResizing || !ref) return; 101 102 // Get the container width to calculate cell size 103 const container = ref.closest('.\\@container\\/grid') as HTMLElement; 104 if (!container) return; 105 106 const containerRect = container.getBoundingClientRect(); 107 const cellSize = containerRect.width / COLUMNS; 108 109 // Calculate delta in grid units (each visual unit is 2 grid units) 110 const deltaX = e.clientX - resizeStartX; 111 const deltaY = e.clientY - resizeStartY; 112 113 // Convert pixel delta to grid units (2 grid units = 1 visual cell) 114 const gridDeltaW = Math.round(deltaX / cellSize); 115 const gridDeltaH = Math.round(deltaY / cellSize); 116 117 let newW = resizeStartW + gridDeltaW; 118 let newH = resizeStartH + gridDeltaH; 119 120 if (isMobile()) { 121 newW = Math.round(newW / 4) * 4; 122 } else { 123 newW = Math.round(newW / 2) * 2; 124 } 125 let mult = isMobile() ? 2 : 1; 126 127 // Clamp to min/max 128 newW = Math.max(minW * mult, Math.min(maxW, newW)); 129 newH = Math.max(minH * mult, Math.min(maxH, newH)); 130 131 // Only call onsetsize if size changed 132 const currentW = isMobile() ? (item.mobileW ?? item.w) : item.w; 133 const currentH = isMobile() ? (item.mobileH ?? item.h) : item.h; 134 135 if (newW !== currentW || newH !== currentH) { 136 onsetsize?.(newW, newH); 137 } 138 } 139 140 function handleResizeEnd() { 141 isResizing = false; 142 document.removeEventListener('pointermove', handleResizeMove); 143 document.removeEventListener('pointerup', handleResizeEnd); 144 } 145 146 function canSetSize(w: number, h: number) { 147 if (!cardDef) return false; 148 149 if (isMobile()) { 150 return w >= minW && w * 2 <= maxW && h >= minH && h * 2 <= maxH; 151 } 152 153 return w >= minW && w <= maxW && h >= minH && h <= maxH; 154 } 155 156 function setSize(w: number, h: number) { 157 if (isMobile()) { 158 w *= 2; 159 h *= 2; 160 } 161 onsetsize?.(w, h); 162 } 163 164 let settingsPopoverOpen = $state(false); 165 let changePopoverOpen = $state(false); 166 167 const changeOptions = $derived(AllCardDefinitions.filter((def) => def.canChange?.(item))); 168 169 function applyChange(def: (typeof AllCardDefinitions)[number]) { 170 const updated = def.change ? def.change(item) : item; 171 if (updated && updated !== item) { 172 item = updated; 173 } 174 item.cardType = def.type; 175 changePopoverOpen = false; 176 } 177 178 function getChangeLabel(def: (typeof AllCardDefinitions)[number]) { 179 return def.name; 180 } 181</script> 182 183<BaseCard 184 {item} 185 isEditing={true} 186 bind:ref 187 showOutline={isResizing || (isCoarse?.() && isSelected)} 188 locked={item.cardData?.locked} 189 class={[ 190 'scale-100 starting:scale-0 starting:opacity-0', 191 isCoarse?.() && isSelected ? 'ring-accent-500 z-10 ring-2 ring-offset-2' : '', 192 isDimmed ? 'opacity-70' : 'opacity-100' 193 ]} 194 {...rest} 195> 196 {#if isCoarse?.() && !isSelected} 197 <!-- svelte-ignore a11y_click_events_have_key_events --> 198 <div 199 role="button" 200 tabindex="-1" 201 class="absolute inset-0 z-20 cursor-pointer" 202 onclick={(e) => { 203 e.stopPropagation(); 204 selectCard?.(item.id); 205 }} 206 ></div> 207 {/if} 208 {@render children?.()} 209 210 {#if cardDef.canHaveLabel} 211 <div 212 class={cn( 213 'bg-base-200/50 dark:bg-base-900/50 absolute top-2 left-2 z-100 w-fit max-w-[calc(100%-1rem)] rounded-xl p-1 px-2 backdrop-blur-md', 214 !item.cardData.label && 'hidden lg:group-hover/card:block' 215 )} 216 > 217 <PlainTextEditor 218 class="text-base-900 dark:text-base-50 w-fit text-base font-semibold" 219 key="label" 220 bind:contentDict={item.cardData} 221 placeholder="Label" 222 /> 223 </div> 224 {/if} 225 226 {#snippet controls()} 227 <!-- class="bg-base-100 border-base-200 dark:bg-base-800 dark:border-base-700 absolute -top-3 -left-3 hidden cursor-pointer items-center justify-center rounded-full border p-2 shadow-lg group-focus-within:inline-flex group-hover/card:inline-flex" --> 228 {#if canEdit()} 229 {#if changeOptions.length > 1} 230 <div 231 class={[ 232 'absolute -top-3 -right-3 hidden lg:group-focus-within:inline-flex lg:group-hover/card:inline-flex', 233 changePopoverOpen ? 'inline-flex' : '' 234 ]} 235 > 236 <Popover bind:open={changePopoverOpen} class="bg-base-50 dark:bg-base-900"> 237 {#snippet child({ props })} 238 <Button size="icon" variant="secondary" {...props}> 239 <svg 240 xmlns="http://www.w3.org/2000/svg" 241 fill="none" 242 viewBox="0 0 24 24" 243 stroke-width="1.5" 244 stroke="currentColor" 245 class="size-6" 246 > 247 <path 248 stroke-linecap="round" 249 stroke-linejoin="round" 250 d="M7.5 21 3 16.5m0 0L7.5 12M3 16.5h13.5m0-13.5L21 7.5m0 0L16.5 12M21 7.5H7.5" 251 /> 252 </svg> 253 254 <span class="sr-only">Change card type</span> 255 </Button> 256 {/snippet} 257 258 <div class="flex min-w-36 flex-col gap-1"> 259 <Label class="mb-2">Card type</Label> 260 {#each changeOptions as changeDef, i (i)} 261 <Button 262 class="justify-start" 263 variant={changeDef.type === item.cardType ? 'primary' : 'ghost'} 264 onclick={() => applyChange(changeDef)} 265 > 266 {getChangeLabel(changeDef)} 267 </Button> 268 {/each} 269 </div> 270 </Popover> 271 </div> 272 {/if} 273 274 <Button 275 size="icon" 276 variant="rose" 277 onclick={() => { 278 ondelete(); 279 }} 280 class="absolute -top-3 -left-3 hidden lg:group-focus-within:inline-flex lg:group-hover/card:inline-flex" 281 > 282 <svg 283 xmlns="http://www.w3.org/2000/svg" 284 fill="none" 285 viewBox="0 0 24 24" 286 stroke-width="1.5" 287 stroke="currentColor" 288 > 289 <path 290 stroke-linecap="round" 291 stroke-linejoin="round" 292 d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0" 293 /> 294 </svg> 295 296 <span class="sr-only">Delete card</span> 297 </Button> 298 299 <div 300 class={[ 301 'absolute -bottom-7 w-full items-center justify-center text-xs lg:group-focus-within:inline-flex lg:group-hover/card:inline-flex', 302 colorPopoverOpen || settingsPopoverOpen ? 'inline-flex' : 'hidden' 303 ]} 304 > 305 <div 306 class="bg-base-100 border-base-200 dark:bg-base-800 dark:border-base-700 z-100 inline-flex items-center gap-0.5 rounded-2xl border p-1 px-2 shadow-lg" 307 > 308 {#if cardDef.allowSetColor !== false} 309 <Popover bind:open={colorPopoverOpen}> 310 {#snippet child({ props })} 311 <button 312 {...props} 313 class={[ 314 'm-2 size-4 cursor-pointer rounded-full', 315 !item.color || item.color === 'base' || item.color === 'transparent' 316 ? 'text-base-800 dark:text-base-200' 317 : 'text-accent-500' 318 ]} 319 > 320 <svg 321 xmlns="http://www.w3.org/2000/svg" 322 viewBox="0 0 24 24" 323 fill="currentColor" 324 class="size-4" 325 > 326 <path 327 fill-rule="evenodd" 328 d="M20.599 1.5c-.376 0-.743.111-1.055.32l-5.08 3.385a18.747 18.747 0 0 0-3.471 2.987 10.04 10.04 0 0 1 4.815 4.815 18.748 18.748 0 0 0 2.987-3.472l3.386-5.079A1.902 1.902 0 0 0 20.599 1.5Zm-8.3 14.025a18.76 18.76 0 0 0 1.896-1.207 8.026 8.026 0 0 0-4.513-4.513A18.75 18.75 0 0 0 8.475 11.7l-.278.5a5.26 5.26 0 0 1 3.601 3.602l.502-.278ZM6.75 13.5A3.75 3.75 0 0 0 3 17.25a1.5 1.5 0 0 1-1.601 1.497.75.75 0 0 0-.7 1.123 5.25 5.25 0 0 0 9.8-2.62 3.75 3.75 0 0 0-3.75-3.75Z" 329 clip-rule="evenodd" 330 /> 331 </svg> 332 </button> 333 {/snippet} 334 <ColorSelect 335 selected={selectedColor} 336 colors={colorsChoices} 337 onselected={(color, previous) => { 338 if (typeof previous === 'string' || typeof color === 'string') { 339 return; 340 } 341 342 item.color = color.label; 343 }} 344 class="w-64" 345 /> 346 </Popover> 347 {/if} 348 349 {#if canSetSize(2, 2)} 350 <button 351 onclick={() => { 352 setSize(2, 2); 353 }} 354 class="hover:bg-accent-500/10 cursor-pointer rounded-xl p-2" 355 > 356 <div class="border-base-900 dark:border-base-50 size-3 rounded-sm border-2"></div> 357 358 <span class="sr-only">set size to 1x1</span> 359 </button> 360 {/if} 361 362 {#if canSetSize(4, 2)} 363 <button 364 onclick={() => { 365 setSize(4, 2); 366 }} 367 class="hover:bg-accent-500/10 cursor-pointer rounded-xl p-2" 368 > 369 <div class="border-base-900 dark:border-base-50 h-3 w-5 rounded-sm border-2"></div> 370 <span class="sr-only">set size to 2x1</span> 371 </button> 372 {/if} 373 {#if canSetSize(2, 4)} 374 <button 375 onclick={() => { 376 setSize(2, 4); 377 }} 378 class="hover:bg-accent-500/10 cursor-pointer rounded-xl p-2" 379 > 380 <div class="border-base-900 dark:border-base-50 h-5 w-3 rounded-sm border-2"></div> 381 382 <span class="sr-only">set size to 1x2</span> 383 </button> 384 {/if} 385 {#if canSetSize(4, 4)} 386 <button 387 onclick={() => { 388 setSize(4, 4); 389 }} 390 class="hover:bg-accent-500/10 cursor-pointer rounded-xl p-2" 391 > 392 <div class="border-base-900 dark:border-base-50 h-5 w-5 rounded-sm border-2"></div> 393 394 <span class="sr-only">set size to 2x2</span> 395 </button> 396 {/if} 397 398 {#if cardDef.settingsComponent} 399 <Popover bind:open={settingsPopoverOpen} class="bg-base-50 dark:bg-base-900"> 400 {#snippet child({ props })} 401 <button {...props} class="hover:bg-accent-500/10 cursor-pointer rounded-xl p-2"> 402 <svg 403 xmlns="http://www.w3.org/2000/svg" 404 fill="none" 405 viewBox="0 0 24 24" 406 stroke-width="2" 407 stroke="currentColor" 408 class="size-5" 409 > 410 <path 411 stroke-linecap="round" 412 stroke-linejoin="round" 413 d="M9.594 3.94c.09-.542.56-.94 1.11-.94h2.593c.55 0 1.02.398 1.11.94l.213 1.281c.063.374.313.686.645.87.074.04.147.083.22.127.325.196.72.257 1.075.124l1.217-.456a1.125 1.125 0 0 1 1.37.49l1.296 2.247a1.125 1.125 0 0 1-.26 1.431l-1.003.827c-.293.241-.438.613-.43.992a7.723 7.723 0 0 1 0 .255c-.008.378.137.75.43.991l1.004.827c.424.35.534.955.26 1.43l-1.298 2.247a1.125 1.125 0 0 1-1.369.491l-1.217-.456c-.355-.133-.75-.072-1.076.124a6.47 6.47 0 0 1-.22.128c-.331.183-.581.495-.644.869l-.213 1.281c-.09.543-.56.94-1.11.94h-2.594c-.55 0-1.019-.398-1.11-.94l-.213-1.281c-.062-.374-.312-.686-.644-.87a6.52 6.52 0 0 1-.22-.127c-.325-.196-.72-.257-1.076-.124l-1.217.456a1.125 1.125 0 0 1-1.369-.49l-1.297-2.247a1.125 1.125 0 0 1 .26-1.431l1.004-.827c.292-.24.437-.613.43-.991a6.932 6.932 0 0 1 0-.255c.007-.38-.138-.751-.43-.992l-1.004-.827a1.125 1.125 0 0 1-.26-1.43l1.297-2.247a1.125 1.125 0 0 1 1.37-.491l1.216.456c.356.133.751.072 1.076-.124.072-.044.146-.086.22-.128.332-.183.582-.495.644-.869l.214-1.28Z" 414 /> 415 <path 416 stroke-linecap="round" 417 stroke-linejoin="round" 418 d="M15 12a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z" 419 /> 420 </svg> 421 </button> 422 {/snippet} 423 <cardDef.settingsComponent 424 bind:item 425 onclose={() => { 426 settingsPopoverOpen = false; 427 }} 428 /> 429 </Popover> 430 {/if} 431 </div> 432 </div> 433 434 {#if cardDef.canResize !== false} 435 <!-- Resize handle at bottom right corner --> 436 <div 437 onpointerdown={handleResizeStart} 438 class="bg-base-300/70 dark:bg-base-900/70 pointer-events-auto absolute right-0.5 bottom-0.5 hidden cursor-se-resize rounded-md rounded-br-3xl p-1 lg:group-hover/card:block" 439 > 440 <svg 441 xmlns="http://www.w3.org/2000/svg" 442 viewBox="0 0 24 24" 443 fill="none" 444 stroke="currentColor" 445 stroke-width="2" 446 stroke-linecap="round" 447 stroke-linejoin="round" 448 class=" dark:text-base-400 text-base-600 size-4" 449 > 450 <circle cx="12" cy="5" r="1" /><circle cx="19" cy="5" r="1" /><circle 451 cx="5" 452 cy="5" 453 r="1" 454 /> 455 <circle cx="12" cy="12" r="1" /><circle cx="19" cy="12" r="1" /><circle 456 cx="5" 457 cy="12" 458 r="1" 459 /> 460 <circle cx="12" cy="19" r="1" /><circle cx="19" cy="19" r="1" /><circle 461 cx="5" 462 cy="19" 463 r="1" 464 /> 465 </svg> 466 <span class="sr-only">Resize card</span> 467 </div> 468 {/if} 469 {/if} 470 {/snippet} 471</BaseCard>