138 lines
5.9 KiB
Vue
138 lines
5.9 KiB
Vue
<script setup lang="ts">
|
|
// Publishing the open circuit to the library: its name and description as
|
|
// everyone will see them, your name, and what goes along with it. Published
|
|
// from this browser before, it goes up as a new version of the same entry.
|
|
import { shortCount } from '~/lib/logic/analyze'
|
|
import { chipPins } from '~/lib/logic/geometry'
|
|
import { LIBRARY_LIMITS, libraryBundle } from '~/lib/logic/library'
|
|
import type { LibraryOrigin } from '~/lib/logic/types'
|
|
|
|
const ed = useEditor()
|
|
const close = () => (ed.dialog.value = null)
|
|
|
|
const def = ed.def
|
|
/** The entry this is a new version of, when this browser published it before. */
|
|
const own = ed.ownEntry(def.value)
|
|
const name = ref(def.value.name)
|
|
const description = ref(def.value.description ?? '')
|
|
const author = ref(ed.playerName.value)
|
|
const busy = ref(false)
|
|
const error = ref('')
|
|
const done = ref<LibraryOrigin | null>(null)
|
|
const confirmRemove = ref(false)
|
|
|
|
const pins = computed(() => chipPins(def.value))
|
|
|
|
/** A list of names, the first few spelled out. */
|
|
const names = (list: string[]) => (list.length > 6 ? `${list.slice(0, 6).join(', ')} and ${list.length - 6} more` : list.join(', '))
|
|
|
|
/** What goes along with it, in words, and whether it all fits. */
|
|
const along = computed(() => {
|
|
const b = libraryBundle(ed.project.value, def.value.id)
|
|
const uses = b.components.filter(d => d.id !== b.root).map(d => d.name)
|
|
const types = (b.connectors ?? []).map(c => c.name)
|
|
const parts = [
|
|
uses.length ? `the ${uses.length === 1 ? 'component' : `${uses.length} components`} it uses (${names(uses)})` : '',
|
|
types.length ? `${types.length === 1 ? 'the connector type' : 'the connector types'} ${names(types)}` : '',
|
|
].filter(Boolean)
|
|
const text = parts.length ? `Goes along: ${parts.join(' and ')}.` : 'It uses no other components.'
|
|
return { text, tooLarge: JSON.stringify(b).length > LIBRARY_LIMITS.bytes }
|
|
})
|
|
const problem = computed(() => ed.libraryProblem.value ?? (along.value.tooLarge ? 'Too large for the library (1 MB at most)' : null))
|
|
|
|
async function publish() {
|
|
busy.value = true
|
|
error.value = ''
|
|
try {
|
|
done.value = await ed.publishToLibrary(name.value, description.value, author.value)
|
|
ed.play('unlock')
|
|
} catch (e) {
|
|
error.value = (e as Error).message
|
|
} finally {
|
|
busy.value = false
|
|
}
|
|
}
|
|
|
|
async function remove() {
|
|
if (!confirmRemove.value) return (confirmRemove.value = true)
|
|
busy.value = true
|
|
error.value = ''
|
|
try {
|
|
await ed.removeFromLibrary(own!)
|
|
ed.notify(`"${def.value.name}" is out of the library. Projects that added it keep their copy.`)
|
|
close()
|
|
} catch (e) {
|
|
error.value = (e as Error).message
|
|
} finally {
|
|
busy.value = false
|
|
}
|
|
}
|
|
|
|
function onKey(e: KeyboardEvent) {
|
|
if (e.key !== 'Escape') return
|
|
e.stopPropagation()
|
|
close()
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div class="modal-backdrop" @click.self="close">
|
|
<div class="modal" role="dialog" aria-labelledby="lib-publish-title" @keydown="onKey">
|
|
<div class="flex items-start justify-between gap-4">
|
|
<h2 id="lib-publish-title">{{ own ? 'Update in the library' : 'Publish to the library' }}</h2>
|
|
<button class="tb-btn" title="Close" @click="close"><Icon name="lucide:x" class="size-4" /></button>
|
|
</div>
|
|
|
|
<template v-if="!done">
|
|
<p class="hint mt-1">
|
|
<template v-if="own">It goes up as the next version of your entry; projects that added it before are offered the update.</template>
|
|
<template v-else>Everyone can find it in the library and add it to their projects, together with everything it uses.</template>
|
|
Only this browser can update or remove it later.
|
|
</p>
|
|
<label class="field mt-4">
|
|
<span>Name</span>
|
|
<input v-model="name" :maxlength="LIBRARY_LIMITS.name" />
|
|
</label>
|
|
<label class="field">
|
|
<span>Description</span>
|
|
<textarea v-model="description" rows="3" :maxlength="LIBRARY_LIMITS.description" placeholder="What does it do, how do you use it?" />
|
|
</label>
|
|
<label class="field">
|
|
<span>Your name, also on the leaderboards</span>
|
|
<input v-model="author" maxlength="24" placeholder="Anonymous" autocomplete="nickname" />
|
|
</label>
|
|
<p class="hint">{{ pins.inputs.length }} in, {{ pins.outputs.length }} out, {{ shortCount(ed.transistors(def.id)) }} transistors. {{ along.text }}</p>
|
|
<p v-if="problem" class="result fail"><Icon name="lucide:circle-x" class="size-4 shrink-0" /> {{ problem }}</p>
|
|
<p v-if="error" class="result fail"><Icon name="lucide:circle-x" class="size-4 shrink-0" /> {{ error }}</p>
|
|
<div class="mt-3 flex flex-wrap items-center gap-2">
|
|
<button class="btn btn-primary" :disabled="!!problem || busy || !name.trim()" @click="publish">
|
|
<Icon name="lucide:book-up" class="size-4" /> {{ busy ? 'Publishing…' : own ? 'Publish new version' : 'Publish' }}
|
|
</button>
|
|
<button
|
|
v-if="own"
|
|
class="btn-sm ml-auto"
|
|
:class="{ danger: confirmRemove }"
|
|
:disabled="busy"
|
|
:title="confirmRemove ? 'Click again to take it out of the library' : 'Take it out of the library; copies others added stay theirs'"
|
|
@click="remove"
|
|
@pointerleave="confirmRemove = false"
|
|
>
|
|
{{ confirmRemove ? 'Remove from the library?' : 'Remove from the library' }}
|
|
</button>
|
|
</div>
|
|
</template>
|
|
|
|
<template v-else>
|
|
<p class="result pass">
|
|
<Icon name="lucide:circle-check" class="size-4 shrink-0" />
|
|
<span>"{{ def.name }}" is in the library as version {{ done.version }}. Everyone can find it and add it to their projects.</span>
|
|
</p>
|
|
<div class="mt-3 flex gap-2">
|
|
<button class="btn btn-primary" @click="ed.dialog.value = 'library'"><Icon name="lucide:library-big" class="size-4" /> Open the library</button>
|
|
<button class="btn" @click="close">Done</button>
|
|
</div>
|
|
</template>
|
|
</div>
|
|
</div>
|
|
</template>
|