174 lines
7.1 KiB
Vue
174 lines
7.1 KiB
Vue
|
|
<script setup lang="ts">
|
||
|
|
// The public library: components everyone published, newest first. Type to
|
||
|
|
// search names, descriptions and authors. "Add" brings one into the open
|
||
|
|
// project with everything it uses and starts placing it; entries published
|
||
|
|
// from this browser can be taken out again.
|
||
|
|
import { shortCount } from '~/lib/logic/analyze'
|
||
|
|
import { lockedKinds, type LibraryMeta } from '~/lib/logic/library'
|
||
|
|
import { KIND_NAMES } from '~/lib/logic/progress'
|
||
|
|
|
||
|
|
const ed = useEditor()
|
||
|
|
const { place } = usePlace()
|
||
|
|
const close = () => (ed.dialog.value = null)
|
||
|
|
|
||
|
|
const query = ref('')
|
||
|
|
const entries = ref<LibraryMeta[] | null>(null)
|
||
|
|
const total = ref(0)
|
||
|
|
const error = ref('')
|
||
|
|
const busy = ref('')
|
||
|
|
const confirmRemove = ref('')
|
||
|
|
const input = ref<HTMLInputElement>()
|
||
|
|
|
||
|
|
// Only the answer to the latest search counts.
|
||
|
|
let asked = 0
|
||
|
|
let timer: ReturnType<typeof setTimeout> | undefined
|
||
|
|
async function load() {
|
||
|
|
const n = ++asked
|
||
|
|
try {
|
||
|
|
const res = await ed.listLibrary(query.value)
|
||
|
|
if (n !== asked) return
|
||
|
|
entries.value = res.entries
|
||
|
|
total.value = res.total
|
||
|
|
error.value = ''
|
||
|
|
} catch (e) {
|
||
|
|
if (n === asked) error.value = `Could not load the library: ${(e as Error).message}`
|
||
|
|
}
|
||
|
|
}
|
||
|
|
watch(query, () => {
|
||
|
|
clearTimeout(timer)
|
||
|
|
timer = setTimeout(load, 250)
|
||
|
|
})
|
||
|
|
onMounted(() => {
|
||
|
|
void load()
|
||
|
|
input.value?.focus()
|
||
|
|
})
|
||
|
|
onBeforeUnmount(() => clearTimeout(timer))
|
||
|
|
|
||
|
|
const date = (t: number) => new Date(t).toLocaleDateString()
|
||
|
|
|
||
|
|
/** A box with a stub per pin, inputs left and outputs right. */
|
||
|
|
function thumb(m: LibraryMeta) {
|
||
|
|
const rows = Math.max(1, m.inputs.length, m.outputs.length)
|
||
|
|
const at = (n: number) => Array.from({ length: n }, (_, i) => 1 + i + (rows - n) / 2)
|
||
|
|
return { box: `0 0 8 ${rows + 1}`, h: rows, ins: at(m.inputs.length), outs: at(m.outputs.length) }
|
||
|
|
}
|
||
|
|
|
||
|
|
/** The entries with their pictures, whether they are yours, and the parts a campaign still lacks for them. */
|
||
|
|
const shown = computed(() =>
|
||
|
|
(entries.value ?? []).map(m => ({
|
||
|
|
m,
|
||
|
|
t: thumb(m),
|
||
|
|
mine: m.id in ed.libKeys.value,
|
||
|
|
missing: lockedKinds(ed.project.value, m.kinds).map(k => KIND_NAMES[k] ?? k),
|
||
|
|
})),
|
||
|
|
)
|
||
|
|
|
||
|
|
async function add(m: LibraryMeta) {
|
||
|
|
busy.value = m.id
|
||
|
|
error.value = ''
|
||
|
|
try {
|
||
|
|
const root = await ed.addFromLibrary(m.id)
|
||
|
|
close()
|
||
|
|
if (ed.screen.value === 'menu') ed.enterGame()
|
||
|
|
place('chip', root)
|
||
|
|
} catch (e) {
|
||
|
|
error.value = (e as Error).message
|
||
|
|
} finally {
|
||
|
|
busy.value = ''
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function remove(m: LibraryMeta) {
|
||
|
|
if (confirmRemove.value !== m.id) return (confirmRemove.value = m.id)
|
||
|
|
confirmRemove.value = ''
|
||
|
|
busy.value = m.id
|
||
|
|
error.value = ''
|
||
|
|
try {
|
||
|
|
await ed.removeFromLibrary(m.id)
|
||
|
|
entries.value = entries.value?.filter(e => e.id !== m.id) ?? null
|
||
|
|
total.value--
|
||
|
|
ed.notify(`"${m.name}" is out of the library. Projects that added it keep their copy.`)
|
||
|
|
} catch (e) {
|
||
|
|
error.value = (e as Error).message
|
||
|
|
} finally {
|
||
|
|
busy.value = ''
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function onKey(e: KeyboardEvent) {
|
||
|
|
if (e.key !== 'Escape') return
|
||
|
|
e.stopPropagation()
|
||
|
|
close()
|
||
|
|
}
|
||
|
|
</script>
|
||
|
|
|
||
|
|
<template>
|
||
|
|
<div class="modal-backdrop" @click.self="close">
|
||
|
|
<div class="modal library-modal" role="dialog" aria-labelledby="library-title" @keydown="onKey">
|
||
|
|
<div class="flex items-start justify-between gap-4">
|
||
|
|
<h2 id="library-title">Library</h2>
|
||
|
|
<button class="tb-btn" title="Close" @click="close"><Icon name="lucide:x" class="size-4" /></button>
|
||
|
|
</div>
|
||
|
|
<p class="hint mt-1">
|
||
|
|
Components everyone published. Add one to "{{ ed.project.value.name }}": it comes with every component it uses, ready to place. To publish your own,
|
||
|
|
open it and use the menu on its name.
|
||
|
|
</p>
|
||
|
|
<div class="search part-search mt-3">
|
||
|
|
<Icon name="lucide:search" class="size-3.5 text-(--muted)" />
|
||
|
|
<input ref="input" v-model="query" placeholder="Search by name, description or author…" />
|
||
|
|
</div>
|
||
|
|
<p v-if="ed.project.value.mode === 'campaign'" class="hint mt-2">
|
||
|
|
In a campaign you can add what is built from parts you have earned. Challenges are still yours to solve: nothing from the library goes into them.
|
||
|
|
</p>
|
||
|
|
<p v-if="error" class="result fail"><Icon name="lucide:circle-x" class="size-4 shrink-0" /> {{ error }}</p>
|
||
|
|
|
||
|
|
<p v-if="entries === null" class="hint mt-3">{{ error ? '' : 'Loading…' }}</p>
|
||
|
|
<p v-else-if="!entries.length" class="hint mt-3">
|
||
|
|
{{ query.trim() ? `Nothing called "${query.trim()}" yet.` : 'Nothing published yet. Be the first: open a component and publish it from the menu on its name.' }}
|
||
|
|
</p>
|
||
|
|
<ul v-else class="lib-list">
|
||
|
|
<li v-for="{ m, t, mine, missing } in shown" :key="m.id" class="lib-entry">
|
||
|
|
<svg class="lib-thumb" :viewBox="t.box" preserveAspectRatio="xMidYMid meet" aria-hidden="true">
|
||
|
|
<line v-for="(y, i) in t.ins" :key="`i${i}`" x1="0.6" :y1="y" x2="2" :y2="y" />
|
||
|
|
<line v-for="(y, i) in t.outs" :key="`o${i}`" x1="6" :y1="y" x2="7.4" :y2="y" />
|
||
|
|
<rect x="2" y="0.5" width="4" :height="t.h" rx="0.4" />
|
||
|
|
</svg>
|
||
|
|
<div class="lib-main">
|
||
|
|
<div class="lib-title">
|
||
|
|
<span class="truncate">{{ m.name }}</span>
|
||
|
|
<span v-if="mine" class="you-tag">yours</span>
|
||
|
|
</div>
|
||
|
|
<div class="lib-by">by {{ m.author || 'Anonymous' }} · version {{ m.version }} · {{ date(m.updated) }}</div>
|
||
|
|
<p v-if="m.description" class="lib-desc">{{ m.description }}</p>
|
||
|
|
<div class="lib-stats">
|
||
|
|
<span title="Transistors at every depth, 4 per NAND">{{ shortCount(m.transistors) }} transistors</span>
|
||
|
|
<span :title="m.inputs.join(', ')">{{ m.inputs.length }} in</span>
|
||
|
|
<span :title="m.outputs.join(', ')">{{ m.outputs.length }} out</span>
|
||
|
|
<span v-if="m.uses" title="Components it uses; they come along">{{ m.uses }} inside</span>
|
||
|
|
<span v-if="m.connectors">{{ m.connectors }} connector {{ m.connectors === 1 ? 'type' : 'types' }}</span>
|
||
|
|
</div>
|
||
|
|
<p v-if="missing.length" class="lib-lock"><Icon name="lucide:lock" class="size-3.5 shrink-0" /> Needs parts you have not earned yet: {{ missing.join(', ') }}</p>
|
||
|
|
</div>
|
||
|
|
<div class="lib-actions">
|
||
|
|
<button class="btn btn-primary" :disabled="!!busy || missing.length > 0" title="Add it to this project and place it" @click="add(m)">
|
||
|
|
<Icon name="lucide:plus" class="size-4" /> {{ busy === m.id ? 'Adding…' : 'Add' }}
|
||
|
|
</button>
|
||
|
|
<button
|
||
|
|
v-if="mine"
|
||
|
|
class="btn-sm"
|
||
|
|
:class="{ danger: confirmRemove === m.id }"
|
||
|
|
:disabled="!!busy"
|
||
|
|
:title="confirmRemove === m.id ? 'Click again to take it out of the library' : 'Take it out of the library; copies others added stay theirs'"
|
||
|
|
@click="remove(m)"
|
||
|
|
@pointerleave="confirmRemove === m.id && (confirmRemove = '')"
|
||
|
|
>
|
||
|
|
{{ confirmRemove === m.id ? 'Remove?' : 'Remove' }}
|
||
|
|
</button>
|
||
|
|
</div>
|
||
|
|
</li>
|
||
|
|
</ul>
|
||
|
|
<p v-if="entries && !query.trim() && total > entries.length" class="hint mt-2">The newest {{ entries.length }} of {{ total }}. Search to find the others.</p>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
</template>
|