|
| 1 | +<script lang="ts"> |
| 2 | + import { Select, TextInput } from '$components/base'; |
| 3 | + import ItemCardGrid from '$components/card/ItemCardGrid.svelte'; |
| 4 | + import { ItemChipList } from '$components/chip'; |
| 5 | + import { ItemPicker } from '$components/pickers'; |
| 6 | + import { getDescendant } from '$lib/itemData'; |
| 7 | + import itemId, { type ItemId } from '$lib/itemId'; |
| 8 | + import { linkDisplayStyles } from '$lib/links'; |
| 9 | + import type { ItemData } from '$lib/server/data/item'; |
| 10 | + import type { BacklinksSection } from '$lib/server/data/item/section'; |
| 11 | + import { capitalize } from '$lib/util'; |
| 12 | +
|
| 13 | + type Props = { |
| 14 | + item: ItemId, |
| 15 | + portfolio: ItemData, |
| 16 | + section: BacklinksSection, |
| 17 | + editing: boolean, |
| 18 | + onchange: () => void, |
| 19 | + }; |
| 20 | +
|
| 21 | + const { |
| 22 | + item: item, |
| 23 | + portfolio, |
| 24 | + editing, |
| 25 | + section = $bindable(), |
| 26 | + onchange, |
| 27 | + }: Props = $props(); |
| 28 | +
|
| 29 | + const parent = $derived(getDescendant(portfolio, section.parentItem)); |
| 30 | + const backlinkItems = $derived( |
| 31 | + parent.info.children |
| 32 | + .map(child => itemId.child(section.parentItem, child)) |
| 33 | + .filter(childId => |
| 34 | + getDescendant(portfolio, childId).info.sections.find( |
| 35 | + sect => sect.type === 'links' && sect.items.includes(item), |
| 36 | + ) !== undefined)); |
| 37 | +</script> |
| 38 | + |
| 39 | +{#snippet display()} |
| 40 | + {#if section.style === 'chip'} |
| 41 | + <div class="link-chips"> |
| 42 | + <h3>{section.label}</h3> |
| 43 | + <ItemChipList |
| 44 | + {portfolio} |
| 45 | + items={[backlinkItems.map(i => ({ itemId: i, selected: false }))]} |
| 46 | + link={!editing} |
| 47 | + /> |
| 48 | + </div> |
| 49 | + {:else} |
| 50 | + <h2>{section.label}</h2> |
| 51 | + <ItemCardGrid {portfolio} itemIds={backlinkItems} {editing} /> |
| 52 | + {/if} |
| 53 | +{/snippet} |
| 54 | + |
| 55 | +{#if editing} |
| 56 | + <div class="edit-outer"> |
| 57 | + <p>Children of the selected item which link to this item will be shown.</p> |
| 58 | + <div class="edit-grid"> |
| 59 | + <label for="links-label-text">Label text</label> |
| 60 | + <TextInput |
| 61 | + id="links-label-text" |
| 62 | + bind:value={section.label} |
| 63 | + oninput={onchange} |
| 64 | + placeholder={'See also'} |
| 65 | + /> |
| 66 | + <label for="links-style">Display style</label> |
| 67 | + <Select id="links-style" bind:value={section.style} {onchange}> |
| 68 | + {#each linkDisplayStyles as style} |
| 69 | + <option value={style}>{capitalize(style)}</option> |
| 70 | + {/each} |
| 71 | + </Select> |
| 72 | + <label for="links-item-picker">Parent item</label> |
| 73 | + <div class="item-picker-control"> |
| 74 | + <ItemPicker |
| 75 | + id="links-item-picker" |
| 76 | + {portfolio} |
| 77 | + bind:value={section.parentItem} |
| 78 | + {onchange} |
| 79 | + /> |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + {@render display()} |
| 83 | + </div> |
| 84 | +{:else} |
| 85 | + {@render display()} |
| 86 | +{/if} |
| 87 | + |
| 88 | +<style> |
| 89 | + .link-chips { |
| 90 | + display: flex; |
| 91 | + align-items: baseline; |
| 92 | + gap: 10px; |
| 93 | + } |
| 94 | + .link-chips h3 { |
| 95 | + margin: 0; |
| 96 | + } |
| 97 | +
|
| 98 | + .edit-outer { |
| 99 | + display: flex; |
| 100 | + flex-direction: column; |
| 101 | + gap: 10px; |
| 102 | + align-items: center; |
| 103 | + } |
| 104 | +
|
| 105 | + .edit-grid { |
| 106 | + display: grid; |
| 107 | + grid-template-columns: 1fr 1fr; |
| 108 | + gap: 10px; |
| 109 | + } |
| 110 | +
|
| 111 | + label { |
| 112 | + display: flex; |
| 113 | + align-items: center; |
| 114 | + } |
| 115 | +
|
| 116 | + .item-picker-control { |
| 117 | + display: flex; |
| 118 | + gap: 5px; |
| 119 | + } |
| 120 | +</style> |
0 commit comments