- ohne eigene Änderungen wird das Formular still aktualisiert - mit eigenen Änderungen Hinweis samt 'Andere Version laden' - auf anderem Gerät gelöschter Clip: Hinweis, Speichern gesperrt Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
290 lines
7.7 KiB
Vue
290 lines
7.7 KiB
Vue
<script setup lang="ts">
|
|
import { computed, onMounted, ref, watch } from 'vue'
|
|
import { onBeforeRouteLeave, useRouter } from 'vue-router'
|
|
|
|
import { ApiError } from '@/api/client'
|
|
import type { Clip, ClipKind } from '@/api/types'
|
|
import CopyButton from '@/components/CopyButton.vue'
|
|
import { ClipConflictError, useClipsStore } from '@/stores/clips'
|
|
import { formatDateTime } from '@/utils/format'
|
|
import { detectLanguage, highlight, LANGUAGES } from '@/utils/highlight'
|
|
|
|
const props = defineProps<{ id: string }>()
|
|
|
|
const store = useClipsStore()
|
|
const router = useRouter()
|
|
|
|
const clip = ref<Clip | null>(null)
|
|
const notFound = ref(false)
|
|
const error = ref<string | null>(null)
|
|
const busy = ref(false)
|
|
const conflict = ref<Clip | null>(null)
|
|
// Live-sync notices: newer version from another device, or deleted there
|
|
const remoteChange = ref<Clip | null>(null)
|
|
const remoteDeleted = ref(false)
|
|
let leaving = false
|
|
|
|
// Form state
|
|
const title = ref('')
|
|
const content = ref('')
|
|
const kind = ref<ClipKind>('text')
|
|
const language = ref('plaintext')
|
|
const baseVersion = ref(0)
|
|
|
|
function fillForm(source: Clip): void {
|
|
clip.value = source
|
|
title.value = source.title ?? ''
|
|
content.value = source.content
|
|
kind.value = source.kind
|
|
language.value = source.language ?? detectLanguage(source.content) ?? 'plaintext'
|
|
baseVersion.value = source.version
|
|
remoteChange.value = null
|
|
}
|
|
|
|
const dirty = computed(() => {
|
|
const c = clip.value
|
|
if (!c) return false
|
|
return (
|
|
title.value !== (c.title ?? '') ||
|
|
content.value !== c.content ||
|
|
kind.value !== c.kind ||
|
|
(kind.value === 'code' && language.value !== c.language)
|
|
)
|
|
})
|
|
|
|
const previewHtml = computed(() =>
|
|
kind.value === 'code' ? highlight(content.value, language.value) : null,
|
|
)
|
|
|
|
// React to live-sync updates of the clip being edited
|
|
watch(
|
|
() => store.items.find((c) => c.id === clip.value?.id),
|
|
(remote) => {
|
|
if (!clip.value || leaving) return
|
|
if (!remote) {
|
|
remoteDeleted.value = true
|
|
return
|
|
}
|
|
if (remote.version <= baseVersion.value) return
|
|
if (dirty.value) remoteChange.value = remote
|
|
else fillForm(remote)
|
|
},
|
|
)
|
|
|
|
async function save(version = baseVersion.value): Promise<void> {
|
|
if (!clip.value) return
|
|
busy.value = true
|
|
error.value = null
|
|
try {
|
|
await store.update(
|
|
clip.value.id,
|
|
{
|
|
title: title.value.trim() || null,
|
|
content: content.value,
|
|
kind: kind.value,
|
|
language: kind.value === 'code' ? language.value : null,
|
|
},
|
|
version,
|
|
)
|
|
conflict.value = null
|
|
leaving = true
|
|
clip.value = null // nothing left to lose; skip the leave guard
|
|
await router.push('/')
|
|
} catch (e) {
|
|
if (e instanceof ClipConflictError) {
|
|
conflict.value = e.current
|
|
} else {
|
|
error.value = e instanceof Error ? e.message : String(e)
|
|
}
|
|
} finally {
|
|
busy.value = false
|
|
}
|
|
}
|
|
|
|
function takeTheirs(): void {
|
|
const theirs = conflict.value ?? remoteChange.value
|
|
if (theirs) fillForm(theirs)
|
|
conflict.value = null
|
|
}
|
|
|
|
async function remove(): Promise<void> {
|
|
if (!clip.value || !confirm('Diesen Clip endgültig löschen?')) return
|
|
leaving = true
|
|
try {
|
|
await store.remove(clip.value.id)
|
|
clip.value = null
|
|
await router.push('/')
|
|
} catch (e) {
|
|
leaving = false
|
|
error.value = e instanceof Error ? e.message : String(e)
|
|
}
|
|
}
|
|
|
|
onBeforeRouteLeave(() => !dirty.value || confirm('Ungespeicherte Änderungen verwerfen?'))
|
|
|
|
onMounted(async () => {
|
|
try {
|
|
fillForm(await store.fetchOne(Number(props.id)))
|
|
} catch (e) {
|
|
if (e instanceof ApiError && e.status === 404) notFound.value = true
|
|
else error.value = e instanceof Error ? e.message : String(e)
|
|
}
|
|
})
|
|
</script>
|
|
|
|
<template>
|
|
<main class="page">
|
|
<h1>Clip bearbeiten</h1>
|
|
|
|
<p v-if="notFound" class="muted">
|
|
Diesen Clip gibt es nicht (mehr). <RouterLink to="/">Zur Übersicht</RouterLink>
|
|
</p>
|
|
<p v-if="error" class="error" role="alert">{{ error }}</p>
|
|
|
|
<section v-if="remoteDeleted" class="card conflict" role="alert">
|
|
<p>
|
|
Dieser Clip wurde auf einem anderen Gerät gelöscht. Du kannst den Inhalt noch kopieren;
|
|
speichern ist nicht mehr möglich.
|
|
</p>
|
|
</section>
|
|
|
|
<section v-else-if="remoteChange && !conflict" class="card notice" role="status">
|
|
<p>
|
|
Auf einem anderen Gerät geändert ({{ formatDateTime(remoteChange.updated_at) }}). Deine
|
|
Änderungen sind noch nicht gespeichert.
|
|
</p>
|
|
<div class="actions">
|
|
<button type="button" @click="takeTheirs">Andere Version laden</button>
|
|
</div>
|
|
</section>
|
|
|
|
<section v-if="conflict" class="card conflict" role="alert">
|
|
<h2>Konflikt</h2>
|
|
<p>
|
|
Der Clip wurde am {{ formatDateTime(conflict.updated_at) }} auf einem anderen Gerät
|
|
geändert. Deine Änderungen sind noch nicht gespeichert.
|
|
</p>
|
|
<div class="actions">
|
|
<button type="button" @click="takeTheirs">Andere Version laden</button>
|
|
<button type="button" class="danger" :disabled="busy" @click="save(conflict.version)">
|
|
Meine Version speichern
|
|
</button>
|
|
</div>
|
|
</section>
|
|
|
|
<form v-if="clip" class="card form" @submit.prevent="save()">
|
|
<label>
|
|
Titel (optional)
|
|
<input v-model="title" maxlength="200" />
|
|
</label>
|
|
|
|
<div class="row">
|
|
<div class="segmented" role="radiogroup" aria-label="Art">
|
|
<button
|
|
type="button"
|
|
role="radio"
|
|
:aria-checked="kind === 'text'"
|
|
:class="{ active: kind === 'text' }"
|
|
@click="kind = 'text'"
|
|
>
|
|
Text
|
|
</button>
|
|
<button
|
|
type="button"
|
|
role="radio"
|
|
:aria-checked="kind === 'code'"
|
|
:class="{ active: kind === 'code' }"
|
|
@click="kind = 'code'"
|
|
>
|
|
Code
|
|
</button>
|
|
</div>
|
|
<select v-if="kind === 'code'" v-model="language" aria-label="Sprache" class="language">
|
|
<option v-for="l in LANGUAGES" :key="l.id" :value="l.id">{{ l.label }}</option>
|
|
</select>
|
|
</div>
|
|
|
|
<textarea
|
|
v-model="content"
|
|
:class="{ mono: kind === 'code' }"
|
|
rows="14"
|
|
autocapitalize="off"
|
|
spellcheck="false"
|
|
@keydown.enter.ctrl.prevent="save()"
|
|
@keydown.enter.meta.prevent="save()"
|
|
></textarea>
|
|
|
|
<template v-if="previewHtml !== null">
|
|
<span class="muted small">Vorschau</span>
|
|
<!-- eslint-disable-next-line vue/no-v-html -- highlight.js output is escaped -->
|
|
<pre class="preview"><code class="hljs" v-html="previewHtml"></code></pre>
|
|
</template>
|
|
|
|
<div class="actions">
|
|
<button type="submit" class="primary" :disabled="busy || remoteDeleted || !content.trim()">
|
|
Speichern
|
|
</button>
|
|
<CopyButton :text="content" />
|
|
<RouterLink to="/" class="button-link">Abbrechen</RouterLink>
|
|
<div class="spacer"></div>
|
|
<button type="button" class="danger" @click="remove">Löschen</button>
|
|
</div>
|
|
<p class="muted small">
|
|
Angelegt {{ formatDateTime(clip.created_at) }} · zuletzt geändert
|
|
{{ formatDateTime(clip.updated_at) }}
|
|
</p>
|
|
</form>
|
|
</main>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.row,
|
|
.actions {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
align-items: center;
|
|
gap: 0.5rem;
|
|
}
|
|
|
|
.spacer {
|
|
flex: 1;
|
|
}
|
|
|
|
.language {
|
|
width: auto;
|
|
min-width: 9rem;
|
|
}
|
|
|
|
.small {
|
|
font-size: 0.85rem;
|
|
}
|
|
|
|
.conflict {
|
|
border-color: var(--error);
|
|
}
|
|
|
|
.notice {
|
|
border-color: var(--accent);
|
|
}
|
|
|
|
.conflict p,
|
|
.notice p {
|
|
margin-top: 0;
|
|
}
|
|
|
|
.preview {
|
|
margin: 0;
|
|
padding: 0.75rem;
|
|
max-height: 24rem;
|
|
overflow: auto;
|
|
border-radius: calc(var(--radius) - 4px);
|
|
background: var(--bg);
|
|
font-size: 0.9rem;
|
|
}
|
|
|
|
.preview code.hljs {
|
|
padding: 0;
|
|
background: transparent;
|
|
}
|
|
</style>
|