logigate/app/components/editor/ShareDialog.vue

72 lines
3.3 KiB
Vue
Raw Permalink Normal View History

<script setup lang="ts">
const ed = useEditor()
const fileInput = ref<HTMLInputElement>()
const close = () => (ed.dialog.value = null)
const STATUS: Record<string, string> = {
saved: 'Saved online',
dirty: 'Changes are saved online in a moment',
saving: 'Saving…',
}
function onFile(e: Event) {
const file = (e.target as HTMLInputElement).files?.[0]
if (file) ed.importComponents(file)
;(e.target as HTMLInputElement).value = ''
}
function selectAll(e: Event) {
;(e.target as HTMLInputElement).select()
}
</script>
<template>
<div class="modal-backdrop" @click.self="close">
<div class="modal share" role="dialog" aria-labelledby="share-title">
<div class="flex items-start justify-between gap-4">
<h2 id="share-title">Save and share</h2>
<button class="tb-btn" title="Close" @click="close"><Icon name="lucide:x" class="size-4" /></button>
</div>
<h3>Link</h3>
<template v-if="ed.linkState.value === 'none'">
<p class="hint">
Save "{{ ed.project.value.name }}" online and get a link. Anyone with the link opens their own copy; only you can change what the link shows.
</p>
<button class="btn btn-primary" @click="ed.createLink()"><Icon name="lucide:link" class="size-4" /> Create link</button>
</template>
<template v-else>
<div class="link-row">
<input :value="ed.linkUrl.value" readonly @focus="selectAll" />
<button class="btn btn-primary" @click="ed.copyText(ed.linkUrl.value)"><Icon name="lucide:copy" class="size-4" /> Copy</button>
</div>
<p v-if="ed.linkState.value === 'viewer'" class="hint mt-2">
You opened this from someone else's link. As soon as you change something it becomes your own copy, and you can share your own link.
</p>
<template v-else>
<p v-if="ed.linkState.value === 'error'" class="result fail">
<Icon name="lucide:circle-x" class="size-4 shrink-0" /> Could not save: {{ ed.linkError.value }}
<button class="btn-sm ml-auto" @click="ed.pushLink()">Retry</button>
</p>
<p v-else class="result pass">
<Icon :name="ed.linkState.value === 'saved' ? 'lucide:cloud-check' : 'lucide:cloud-upload'" class="size-4 shrink-0" />
{{ STATUS[ed.linkState.value] }}
</p>
<p class="hint mt-2">Your changes go to this link automatically. The address bar shows it too, so you can bookmark it.</p>
</template>
</template>
<h3>File</h3>
<p class="hint">A file keeps everything offline: send it by mail, keep a backup, or move to another computer.</p>
<div class="flex flex-wrap gap-2">
<button class="btn" @click="ed.exportJson()"><Icon name="lucide:download" class="size-4" /> Export project</button>
<button class="btn" title="Adds the components from a project file to this project" @click="fileInput?.click()">
<Icon name="lucide:upload" class="size-4" /> Add components from file
</button>
<button class="btn btn-ghost" @click="ed.dialog.value = 'projects'"><Icon name="lucide:folder-open" class="size-4" /> Open file as project</button>
<input ref="fileInput" type="file" accept="application/json,.json" class="hidden" @change="onFile" />
</div>
</div>
</div>
</template>