your personal website on atproto - mirror blento.app
at card-command-bar 447 lines 15 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 { getCanEdit, getIsMobile } from '$lib/website/context'; 11 import PlainTextEditor from '$lib/components/PlainTextEditor.svelte'; 12 import { fixAllCollisions, fixCollisions } from '$lib/helper'; 13 14 let colorsChoices = [ 15 { class: 'text-base-500', label: 'base' }, 16 { class: 'text-accent-500', label: 'accent' }, 17 { class: 'text-base-300 dark:text-base-700', label: 'transparent' }, 18 { class: 'text-red-500', label: 'red' }, 19 { class: 'text-orange-500', label: 'orange' }, 20 { class: 'text-amber-500', label: 'amber' }, 21 { class: 'text-yellow-500', label: 'yellow' }, 22 { class: 'text-lime-500', label: 'lime' }, 23 { class: 'text-green-500', label: 'green' }, 24 { class: 'text-emerald-500', label: 'emerald' }, 25 { class: 'text-teal-500', label: 'teal' }, 26 { class: 'text-cyan-500', label: 'cyan' }, 27 { class: 'text-sky-500', label: 'sky' }, 28 { class: 'text-blue-500', label: 'blue' }, 29 { class: 'text-indigo-500', label: 'indigo' }, 30 { class: 'text-violet-500', label: 'violet' }, 31 { class: 'text-purple-500', label: 'purple' }, 32 { class: 'text-fuchsia-500', label: 'fuchsia' }, 33 { class: 'text-pink-500', label: 'pink' }, 34 { class: 'text-rose-500', label: 'rose' } 35 ]; 36 37 export type BaseEditingCardProps = { 38 item: Item; 39 ondelete: () => void; 40 onsetsize: (newW: number, newH: number) => void; 41 } & WithElementRef<HTMLAttributes<HTMLDivElement>>; 42 43 let { 44 item = $bindable(), 45 children, 46 ref = $bindable(null), 47 onsetsize, 48 ondelete, 49 ...rest 50 }: BaseEditingCardProps = $props(); 51 52 let selectedColor = $derived(colorsChoices.find((c) => getColor(item) === c.label)); 53 54 let canEdit = getCanEdit(); 55 let isMobile = getIsMobile(); 56 57 let colorPopoverOpen = $state(false); 58 59 const cardDef = $derived(CardDefinitionsByType[item.cardType] ?? {}); 60 61 const minW = $derived(cardDef.minW ?? (isMobile() ? 2 : 2)); 62 const minH = $derived(cardDef.minH ?? (isMobile() ? 2 : 2)); 63 64 const maxW = $derived(cardDef.maxW ?? COLUMNS); 65 const maxH = $derived(cardDef.maxH ?? (isMobile() ? 12 : 6)); 66 67 // Resize handle state 68 let isResizing = $state(false); 69 let resizeStartX = $state(0); 70 let resizeStartY = $state(0); 71 let resizeStartW = $state(0); 72 let resizeStartH = $state(0); 73 74 function handleResizeStart(e: PointerEvent) { 75 e.preventDefault(); 76 e.stopPropagation(); 77 isResizing = true; 78 resizeStartX = e.clientX; 79 resizeStartY = e.clientY; 80 // For mobile view, sizes are doubled so we need to account for that 81 resizeStartW = isMobile() ? (item.mobileW ?? item.w) : item.w; 82 resizeStartH = isMobile() ? (item.mobileH ?? item.h) : item.h; 83 84 document.addEventListener('pointermove', handleResizeMove); 85 document.addEventListener('pointerup', handleResizeEnd); 86 } 87 88 function handleResizeMove(e: PointerEvent) { 89 if (!isResizing || !ref) return; 90 91 // Get the container width to calculate cell size 92 const container = ref.closest('.\\@container\\/grid') as HTMLElement; 93 if (!container) return; 94 95 const containerRect = container.getBoundingClientRect(); 96 const cellSize = containerRect.width / COLUMNS; 97 98 // Calculate delta in grid units (each visual unit is 2 grid units) 99 const deltaX = e.clientX - resizeStartX; 100 const deltaY = e.clientY - resizeStartY; 101 102 // Convert pixel delta to grid units (2 grid units = 1 visual cell) 103 const gridDeltaW = Math.round(deltaX / cellSize); 104 const gridDeltaH = Math.round(deltaY / cellSize); 105 106 let newW = resizeStartW + gridDeltaW; 107 let newH = resizeStartH + gridDeltaH; 108 109 if (isMobile()) { 110 newW = Math.round(newW / 4) * 4; 111 } else { 112 newW = Math.round(newW / 2) * 2; 113 } 114 let mult = isMobile() ? 2 : 1; 115 116 // Clamp to min/max 117 newW = Math.max(minW * mult, Math.min(maxW, newW)); 118 newH = Math.max(minH * mult, Math.min(maxH, newH)); 119 120 // Only call onsetsize if size changed 121 const currentW = isMobile() ? (item.mobileW ?? item.w) : item.w; 122 const currentH = isMobile() ? (item.mobileH ?? item.h) : item.h; 123 124 if (newW !== currentW || newH !== currentH) { 125 onsetsize?.(newW, newH); 126 } 127 } 128 129 function handleResizeEnd() { 130 isResizing = false; 131 document.removeEventListener('pointermove', handleResizeMove); 132 document.removeEventListener('pointerup', handleResizeEnd); 133 } 134 135 function canSetSize(w: number, h: number) { 136 if (!cardDef) return false; 137 138 if (isMobile()) { 139 return w >= minW && w * 2 <= maxW && h >= minH && h * 2 <= maxH; 140 } 141 142 return w >= minW && w <= maxW && h >= minH && h <= maxH; 143 } 144 145 function setSize(w: number, h: number) { 146 if (isMobile()) { 147 w *= 2; 148 h *= 2; 149 } 150 onsetsize?.(w, h); 151 } 152 153 let settingsPopoverOpen = $state(false); 154 let changePopoverOpen = $state(false); 155 156 const changeOptions = $derived(AllCardDefinitions.filter((def) => def.canChange?.(item))); 157 158 function applyChange(def: (typeof AllCardDefinitions)[number]) { 159 const updated = def.change ? def.change(item) : item; 160 if (updated && updated !== item) { 161 item = updated; 162 } 163 item.cardType = def.type; 164 changePopoverOpen = false; 165 } 166 167 function getChangeLabel(def: (typeof AllCardDefinitions)[number]) { 168 return def.name; 169 } 170</script> 171 172<BaseCard 173 {item} 174 isEditing={true} 175 bind:ref 176 showOutline={isResizing} 177 locked={item.cardData?.locked} 178 class="scale-100 opacity-100 starting:scale-0 starting:opacity-0" 179 {...rest} 180> 181 {#if !item.cardData?.locked} 182 <div class="absolute inset-0 cursor-grab"></div> 183 {/if} 184 {@render children?.()} 185 186 {#if cardDef.canHaveLabel} 187 <div 188 class={cn( 189 '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', 190 !item.cardData.label && 'hidden group-hover/card:block' 191 )} 192 > 193 <PlainTextEditor 194 class="text-base-900 dark:text-base-50 w-fit text-base font-semibold" 195 key="label" 196 bind:contentDict={item.cardData} 197 placeholder="Label" 198 /> 199 </div> 200 {/if} 201 202 {#snippet controls()} 203 <!-- 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" --> 204 {#if canEdit()} 205 {#if changeOptions.length > 1} 206 <div 207 class={[ 208 'absolute -top-3 -right-3 hidden group-focus-within:inline-flex group-hover/card:inline-flex', 209 changePopoverOpen ? 'inline-flex' : '' 210 ]} 211 > 212 <Popover bind:open={changePopoverOpen} class="bg-base-50 dark:bg-base-900"> 213 {#snippet child({ props })} 214 <Button size="icon" variant="secondary" {...props}> 215 <svg 216 xmlns="http://www.w3.org/2000/svg" 217 fill="none" 218 viewBox="0 0 24 24" 219 stroke-width="1.5" 220 stroke="currentColor" 221 class="size-6" 222 > 223 <path 224 stroke-linecap="round" 225 stroke-linejoin="round" 226 d="M7.5 21 3 16.5m0 0L7.5 12M3 16.5h13.5m0-13.5L21 7.5m0 0L16.5 12M21 7.5H7.5" 227 /> 228 </svg> 229 230 <span class="sr-only">Change card type</span> 231 </Button> 232 {/snippet} 233 234 <div class="flex min-w-36 flex-col gap-1"> 235 <Label class="mb-2">Card type</Label> 236 {#each changeOptions as changeDef, i (i)} 237 <Button 238 class="justify-start" 239 variant={changeDef.type === item.cardType ? 'primary' : 'ghost'} 240 onclick={() => applyChange(changeDef)} 241 > 242 {getChangeLabel(changeDef)} 243 </Button> 244 {/each} 245 </div> 246 </Popover> 247 </div> 248 {/if} 249 250 <Button 251 size="icon" 252 variant="rose" 253 onclick={() => { 254 ondelete(); 255 }} 256 class="absolute -top-3 -left-3 hidden group-focus-within:inline-flex group-hover/card:inline-flex" 257 > 258 <svg 259 xmlns="http://www.w3.org/2000/svg" 260 fill="none" 261 viewBox="0 0 24 24" 262 stroke-width="1.5" 263 stroke="currentColor" 264 > 265 <path 266 stroke-linecap="round" 267 stroke-linejoin="round" 268 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" 269 /> 270 </svg> 271 272 <span class="sr-only">Delete card</span> 273 </Button> 274 275 <div 276 class={[ 277 'absolute -bottom-7 w-full items-center justify-center text-xs group-focus-within:inline-flex group-hover/card:inline-flex', 278 colorPopoverOpen || settingsPopoverOpen ? 'inline-flex' : 'hidden' 279 ]} 280 > 281 <div 282 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" 283 > 284 {#if cardDef.allowSetColor !== false} 285 <Popover bind:open={colorPopoverOpen}> 286 {#snippet child({ props })} 287 <button 288 {...props} 289 class={[ 290 'm-2 size-4 cursor-pointer rounded-full', 291 !item.color || item.color === 'base' || item.color === 'transparent' 292 ? 'text-base-800 dark:text-base-200' 293 : 'text-accent-500' 294 ]} 295 > 296 <svg 297 xmlns="http://www.w3.org/2000/svg" 298 viewBox="0 0 24 24" 299 fill="currentColor" 300 class="size-4" 301 > 302 <path 303 fill-rule="evenodd" 304 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" 305 clip-rule="evenodd" 306 /> 307 </svg> 308 </button> 309 {/snippet} 310 <ColorSelect 311 selected={selectedColor} 312 colors={colorsChoices} 313 onselected={(color, previous) => { 314 if (typeof previous === 'string' || typeof color === 'string') { 315 return; 316 } 317 318 item.color = color.label; 319 }} 320 class="w-64" 321 /> 322 </Popover> 323 {/if} 324 325 {#if canSetSize(2, 2)} 326 <button 327 onclick={() => { 328 setSize(2, 2); 329 }} 330 class="hover:bg-accent-500/10 cursor-pointer rounded-xl p-2" 331 > 332 <div class="border-base-900 dark:border-base-50 size-3 rounded-sm border-2"></div> 333 334 <span class="sr-only">set size to 1x1</span> 335 </button> 336 {/if} 337 338 {#if canSetSize(4, 2)} 339 <button 340 onclick={() => { 341 setSize(4, 2); 342 }} 343 class="hover:bg-accent-500/10 cursor-pointer rounded-xl p-2" 344 > 345 <div class="border-base-900 dark:border-base-50 h-3 w-5 rounded-sm border-2"></div> 346 <span class="sr-only">set size to 2x1</span> 347 </button> 348 {/if} 349 {#if canSetSize(2, 4)} 350 <button 351 onclick={() => { 352 setSize(2, 4); 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 h-5 w-3 rounded-sm border-2"></div> 357 358 <span class="sr-only">set size to 1x2</span> 359 </button> 360 {/if} 361 {#if canSetSize(4, 4)} 362 <button 363 onclick={() => { 364 setSize(4, 4); 365 }} 366 class="hover:bg-accent-500/10 cursor-pointer rounded-xl p-2" 367 > 368 <div class="border-base-900 dark:border-base-50 h-5 w-5 rounded-sm border-2"></div> 369 370 <span class="sr-only">set size to 2x2</span> 371 </button> 372 {/if} 373 374 {#if cardDef.settingsComponent} 375 <Popover bind:open={settingsPopoverOpen} class="bg-base-50 dark:bg-base-900"> 376 {#snippet child({ props })} 377 <button {...props} class="hover:bg-accent-500/10 cursor-pointer rounded-xl p-2"> 378 <svg 379 xmlns="http://www.w3.org/2000/svg" 380 fill="none" 381 viewBox="0 0 24 24" 382 stroke-width="2" 383 stroke="currentColor" 384 class="size-5" 385 > 386 <path 387 stroke-linecap="round" 388 stroke-linejoin="round" 389 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" 390 /> 391 <path 392 stroke-linecap="round" 393 stroke-linejoin="round" 394 d="M15 12a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z" 395 /> 396 </svg> 397 </button> 398 {/snippet} 399 <cardDef.settingsComponent 400 bind:item 401 onclose={() => { 402 settingsPopoverOpen = false; 403 }} 404 /> 405 </Popover> 406 {/if} 407 </div> 408 </div> 409 410 {#if cardDef.canResize !== false} 411 <!-- Resize handle at bottom right corner --> 412 <div 413 onpointerdown={handleResizeStart} 414 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 group-hover/card:block" 415 > 416 <svg 417 xmlns="http://www.w3.org/2000/svg" 418 viewBox="0 0 24 24" 419 fill="none" 420 stroke="currentColor" 421 stroke-width="2" 422 stroke-linecap="round" 423 stroke-linejoin="round" 424 class=" dark:text-base-400 text-base-600 size-4" 425 > 426 <circle cx="12" cy="5" r="1" /><circle cx="19" cy="5" r="1" /><circle 427 cx="5" 428 cy="5" 429 r="1" 430 /> 431 <circle cx="12" cy="12" r="1" /><circle cx="19" cy="12" r="1" /><circle 432 cx="5" 433 cy="12" 434 r="1" 435 /> 436 <circle cx="12" cy="19" r="1" /><circle cx="19" cy="19" r="1" /><circle 437 cx="5" 438 cy="19" 439 r="1" 440 /> 441 </svg> 442 <span class="sr-only">Resize card</span> 443 </div> 444 {/if} 445 {/if} 446 {/snippet} 447</BaseCard>