feat: fernänderungen in der bearbeiten-ansicht
- 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>
This commit is contained in:
1 parent
e20af699dc
commit
56b58243af
1 file changed
+55
-3
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref } from 'vue'
|
||||
import { computed, onMounted, ref, watch } from 'vue'
|
||||
import { onBeforeRouteLeave, useRouter } from 'vue-router'
|
||||
|
||||
import { ApiError } from '@/api/client'
|
||||
@@ -19,6 +19,10 @@ 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('')
|
||||
@@ -34,6 +38,7 @@ function fillForm(source: Clip): void {
|
||||
kind.value = source.kind
|
||||
language.value = source.language ?? detectLanguage(source.content) ?? 'plaintext'
|
||||
baseVersion.value = source.version
|
||||
remoteChange.value = null
|
||||
}
|
||||
|
||||
const dirty = computed(() => {
|
||||
@@ -51,6 +56,21 @@ 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
|
||||
@@ -67,6 +87,7 @@ async function save(version = baseVersion.value): Promise<void> {
|
||||
version,
|
||||
)
|
||||
conflict.value = null
|
||||
leaving = true
|
||||
clip.value = null // nothing left to lose; skip the leave guard
|
||||
await router.push('/')
|
||||
} catch (e) {
|
||||
@@ -81,17 +102,20 @@ async function save(version = baseVersion.value): Promise<void> {
|
||||
}
|
||||
|
||||
function takeTheirs(): void {
|
||||
if (conflict.value) fillForm(conflict.value)
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -117,6 +141,23 @@ onMounted(async () => {
|
||||
</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>
|
||||
@@ -180,7 +221,9 @@ onMounted(async () => {
|
||||
</template>
|
||||
|
||||
<div class="actions">
|
||||
<button type="submit" class="primary" :disabled="busy || !content.trim()">Speichern</button>
|
||||
<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>
|
||||
@@ -220,6 +263,15 @@ onMounted(async () => {
|
||||
border-color: var(--error);
|
||||
}
|
||||
|
||||
.notice {
|
||||
border-color: var(--accent);
|
||||
}
|
||||
|
||||
.conflict p,
|
||||
.notice p {
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
.preview {
|
||||
margin: 0;
|
||||
padding: 0.75rem;
|
||||
|
||||
Reference in new issue
Block a user