130 lines
6.1 KiB
Vue
130 lines
6.1 KiB
Vue
<script setup lang="ts">
|
|
// Your projects: open, rename, copy, delete, restore, and start new ones.
|
|
// Used by the Projects dialog and the main menu's Free play page.
|
|
const props = defineProps<{ /** Hide campaign and challenge projects (they have their own menu pages). */ freeOnly?: boolean }>()
|
|
const emit = defineEmits<{ done: [] }>()
|
|
const ed = useEditor()
|
|
const list = computed(() => (props.freeOnly ? ed.projects.value.filter(m => !ed.projectMode(m)) : ed.projects.value))
|
|
const fileInput = ref<HTMLInputElement>()
|
|
const renaming = ref<string | null>(null)
|
|
const renameText = ref('')
|
|
const confirmDelete = ref<string | null>(null)
|
|
|
|
const close = () => emit('done')
|
|
|
|
const showTrash = ref(false)
|
|
const trash = ref<{ id: string; name: string; deleted: number; v: number }[] | null>(null)
|
|
async function toggleTrash() {
|
|
showTrash.value = !showTrash.value
|
|
if (showTrash.value) trash.value = await ed.listTrash().catch(() => [])
|
|
}
|
|
async function restoreDeleted(t: { id: string; v: number }) {
|
|
await ed.restoreDeleted(t)
|
|
close()
|
|
}
|
|
const when = (t: number) => new Intl.DateTimeFormat(undefined, { dateStyle: 'medium', timeStyle: 'short' }).format(t)
|
|
|
|
function open(id: string) {
|
|
ed.switchProject(id)
|
|
close()
|
|
}
|
|
|
|
function startRename(id: string, name: string) {
|
|
renaming.value = id
|
|
renameText.value = name
|
|
nextTick(() => document.querySelector<HTMLInputElement>('.project-list input')?.select())
|
|
}
|
|
|
|
function finishRename() {
|
|
if (renaming.value) ed.renameProject(renaming.value, renameText.value)
|
|
renaming.value = null
|
|
}
|
|
|
|
function onFile(e: Event) {
|
|
const file = (e.target as HTMLInputElement).files?.[0]
|
|
if (file) ed.importAsProject(file).then(close)
|
|
;(e.target as HTMLInputElement).value = ''
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div class="projects">
|
|
<p class="hint mt-1">
|
|
Saved in this browser as you work.
|
|
<template v-if="ed.account.value">Also kept in your account, so they are on every computer you sign in on.</template>
|
|
<a v-else href="#" @click.prevent="ed.dialog.value = 'account'">Sign in to have them on every computer.</a>
|
|
</p>
|
|
|
|
<p v-if="!list.length" class="hint mt-3">No projects yet. Start one below.</p>
|
|
<ul class="project-list">
|
|
<li v-for="m in list" :key="m.id" :class="{ current: m.id === ed.meta.value.id }">
|
|
<input
|
|
v-if="renaming === m.id"
|
|
v-model="renameText"
|
|
class="rename"
|
|
@keydown.enter="finishRename"
|
|
@keydown.esc="renaming = null"
|
|
@blur="finishRename"
|
|
/>
|
|
<button v-else class="pick" @click="open(m.id)">
|
|
<span class="name">
|
|
{{ m.name }}
|
|
<span v-if="m.id === ed.meta.value.id" class="badge">open</span>
|
|
</span>
|
|
<span class="meta">
|
|
{{ when(m.updated) }}
|
|
<template v-if="m.link"> · <Icon name="lucide:link" class="size-3" /> {{ m.link.key ? 'your link' : 'from a link' }}</template>
|
|
<template v-if="ed.account.value && m.cloud?.owner === ed.account.value.id">
|
|
· <Icon :name="m.dirty ? 'lucide:cloud-upload' : 'lucide:cloud-check'" class="size-3" /> {{ m.dirty ? 'saving to account' : 'in your account' }}
|
|
</template>
|
|
<template v-else-if="ed.account.value && !m.cloud"> · <Icon name="lucide:cloud-upload" class="size-3" /> uploading</template>
|
|
<template v-else-if="!ed.account.value"> · <Icon name="lucide:laptop" class="size-3" /> this browser only</template>
|
|
</span>
|
|
</button>
|
|
<button
|
|
v-if="ed.account.value && m.cloud?.owner === ed.account.value.id"
|
|
class="icon-btn"
|
|
title="Savegames: earlier versions"
|
|
@click="ed.versionsFor.value = m.id; ed.dialog.value = 'versions'"
|
|
>
|
|
<Icon name="lucide:history" class="size-3.5" />
|
|
</button>
|
|
<button class="icon-btn" title="Rename" @click="startRename(m.id, m.name)"><Icon name="lucide:pencil" class="size-3.5" /></button>
|
|
<button class="icon-btn" title="Duplicate" @click="ed.duplicateProject(m.id); close()"><Icon name="lucide:copy" class="size-3.5" /></button>
|
|
<button v-if="confirmDelete !== m.id" class="icon-btn" title="Delete" @click="confirmDelete = m.id"><Icon name="lucide:trash-2" class="size-3.5" /></button>
|
|
<button v-else class="btn-sm btn-danger" @click="ed.deleteProject(m.id); confirmDelete = null">Delete?</button>
|
|
</li>
|
|
</ul>
|
|
|
|
<template v-if="ed.account.value">
|
|
<button class="section-toggle mt-4" @click="toggleTrash">
|
|
<Icon :name="showTrash ? 'lucide:chevron-down' : 'lucide:chevron-right'" class="size-4" />
|
|
Recently deleted
|
|
</button>
|
|
<template v-if="showTrash">
|
|
<p v-if="!trash" class="hint mt-2">Loading…</p>
|
|
<p v-else-if="!trash.length" class="hint mt-2">Nothing deleted in the last 30 days.</p>
|
|
<ul v-else class="project-list">
|
|
<li v-for="t in trash" :key="t.id">
|
|
<div class="pick">
|
|
<span class="name">{{ t.name }}</span>
|
|
<span class="meta">deleted {{ when(t.deleted) }}</span>
|
|
</div>
|
|
<button class="btn-sm" @click="restoreDeleted(t)">Restore</button>
|
|
</li>
|
|
</ul>
|
|
</template>
|
|
</template>
|
|
|
|
<div class="mt-4 flex flex-wrap gap-2">
|
|
<button v-if="!freeOnly" class="btn btn-primary" title="Start with NAND only and earn every other part by solving challenges" @click="ed.newProject(false, true); close()">
|
|
<Icon name="lucide:trophy" class="size-4" /> New campaign
|
|
</button>
|
|
<button class="btn" @click="ed.newProject(false); close()"><Icon name="lucide:file" class="size-4" /> New (NAND only)</button>
|
|
<button class="btn" @click="ed.newProject(true); close()"><Icon name="lucide:file-plus" class="size-4" /> New with examples</button>
|
|
<button class="btn" @click="fileInput?.click()"><Icon name="lucide:folder-open" class="size-4" /> Open file</button>
|
|
<input ref="fileInput" type="file" accept="application/json,.json" class="hidden" @change="onFile" />
|
|
</div>
|
|
</div>
|
|
</template>
|