logigate/app/components/editor/LibraryDialog.vue

171 lines
7.0 KiB
Vue
Raw Permalink Normal View History

<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. A campaign can look, not add.
import { shortCount } from '~/lib/logic/analyze'
import { importProblem, type LibraryMeta } from '~/lib/logic/library'
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 (16 a side at most), inputs left and outputs right. */
function thumb(m: LibraryMeta) {
const ins = Math.min(m.inputs, 16)
const outs = Math.min(m.outputs, 16)
const rows = Math.max(1, ins, outs)
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(ins), outs: at(outs) }
}
/** Pin names for a tooltip, with a mark when there are more than the listing keeps. */
const pinList = (names: string[], n: number) => names.join(', ') + (n > names.length ? ', …' : '')
/** The entries with their pictures, and whether they are yours. */
const shown = computed(() => (entries.value ?? []).map(m => ({ m, t: thumb(m), mine: m.id in ed.libKeys.value })))
/** Why nothing can be added to this project (a campaign); null when it can. */
const blocked = computed(() => importProblem(ed.project.value))
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="blocked" class="hint mt-2">{{ blocked }}.</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 } 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="pinList(m.inNames, m.inputs)">{{ m.inputs }} in</span>
<span :title="pinList(m.outNames, m.outputs)">{{ m.outputs }} 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>
</div>
<div class="lib-actions">
<button class="btn btn-primary" :disabled="!!busy || !!blocked" :title="blocked ?? '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>