feat: clip-liste mit schnell-eingabe und kopieren
- Startseite zeigt Clips, angeheftete zuerst, mit 'Weitere laden' - Schnell-Eingabe mit Text/Code, Spracherkennung und Einfügen aus der Zwischenablage - Kopieren-Button mit Clipboard API und Ausweichlösung - Syntax-Highlighting mit highlight.js (nur die 24 unterstützten Sprachen) - Pinia-Store für Clips als Grundlage für den Live-Sync Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4caab7e657
commit
7b16940b96
13 files changed
+673
-42
No files matched your search
Generated
+10
@@ -8,6 +8,7 @@
|
|||||||
"name": "lan-clipboard-frontend",
|
"name": "lan-clipboard-frontend",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"highlight.js": "^11.12.0",
|
||||||
"pinia": "^4.1.0",
|
"pinia": "^4.1.0",
|
||||||
"vue": "^3.5.44",
|
"vue": "^3.5.44",
|
||||||
"vue-router": "^5.4.0"
|
"vue-router": "^5.4.0"
|
||||||
@@ -2030,6 +2031,15 @@
|
|||||||
"node": ">=20"
|
"node": ">=20"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/highlight.js": {
|
||||||
|
"version": "11.12.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/highlight.js/-/highlight.js-11.12.0.tgz",
|
||||||
|
"integrity": "sha512-nbfWpyRMcMrPMmDwJB+dhX/eiaPKtc2RB+0QZskqJ3WjRA/FDS0e9hZrx8EC/lbEv8gXy98FcDbNa/dspAaJMg==",
|
||||||
|
"license": "BSD-3-Clause",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=12.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/hookable": {
|
"node_modules/hookable": {
|
||||||
"version": "5.5.3",
|
"version": "5.5.3",
|
||||||
"resolved": "https://registry.npmjs.org/hookable/-/hookable-5.5.3.tgz",
|
"resolved": "https://registry.npmjs.org/hookable/-/hookable-5.5.3.tgz",
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
"format:check": "prettier --check src/"
|
"format:check": "prettier --check src/"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"highlight.js": "^11.12.0",
|
||||||
"pinia": "^4.1.0",
|
"pinia": "^4.1.0",
|
||||||
"vue": "^3.5.44",
|
"vue": "^3.5.44",
|
||||||
"vue-router": "^5.4.0"
|
"vue-router": "^5.4.0"
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ export class ApiError extends Error {
|
|||||||
constructor(
|
constructor(
|
||||||
public readonly status: number,
|
public readonly status: number,
|
||||||
message: string,
|
message: string,
|
||||||
|
/** Parsed JSON body of the error response, if any */
|
||||||
|
public readonly data: unknown = null,
|
||||||
) {
|
) {
|
||||||
super(message)
|
super(message)
|
||||||
}
|
}
|
||||||
@@ -21,16 +23,11 @@ function readCookie(name: string): string | null {
|
|||||||
return match ? decodeURIComponent(match.slice(name.length + 1)) : null
|
return match ? decodeURIComponent(match.slice(name.length + 1)) : null
|
||||||
}
|
}
|
||||||
|
|
||||||
async function errorMessage(response: Response): Promise<string> {
|
function errorMessage(response: Response, data: unknown): string {
|
||||||
try {
|
if (data && typeof data === 'object' && 'detail' in data) {
|
||||||
const data: unknown = await response.json()
|
const detail = (data as { detail: unknown }).detail
|
||||||
if (data && typeof data === 'object' && 'detail' in data) {
|
if (typeof detail === 'string') return detail
|
||||||
const detail = (data as { detail: unknown }).detail
|
if (Array.isArray(detail)) return 'Ungültige Eingabe.'
|
||||||
if (typeof detail === 'string') return detail
|
|
||||||
if (Array.isArray(detail)) return 'Ungültige Eingabe.'
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// body is not JSON
|
|
||||||
}
|
}
|
||||||
return `${response.status} ${response.statusText}`
|
return `${response.status} ${response.statusText}`
|
||||||
}
|
}
|
||||||
@@ -52,7 +49,8 @@ export async function apiRequest<T>(method: string, path: string, body?: unknown
|
|||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
if (response.status === 401 && path !== '/auth/login') onUnauthorized?.()
|
if (response.status === 401 && path !== '/auth/login') onUnauthorized?.()
|
||||||
throw new ApiError(response.status, await errorMessage(response))
|
const data: unknown = await response.json().catch(() => null)
|
||||||
|
throw new ApiError(response.status, errorMessage(response, data), data)
|
||||||
}
|
}
|
||||||
if (response.status === 204) return undefined as T
|
if (response.status === 204) return undefined as T
|
||||||
return (await response.json()) as T
|
return (await response.json()) as T
|
||||||
|
|||||||
@@ -17,3 +17,24 @@ export interface SessionInfo {
|
|||||||
user_agent: string | null
|
user_agent: string | null
|
||||||
current: boolean
|
current: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type ClipKind = 'text' | 'code'
|
||||||
|
|
||||||
|
export interface Clip {
|
||||||
|
id: number
|
||||||
|
kind: ClipKind
|
||||||
|
title: string | null
|
||||||
|
content: string
|
||||||
|
language: string | null
|
||||||
|
pinned: boolean
|
||||||
|
version: number
|
||||||
|
created_at: string
|
||||||
|
updated_at: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ClipList {
|
||||||
|
items: Clip[]
|
||||||
|
total: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ClipInput = Partial<Pick<Clip, 'kind' | 'title' | 'content' | 'language' | 'pinned'>>
|
||||||
@@ -153,3 +153,65 @@ button:disabled {
|
|||||||
border-color: var(--accent);
|
border-color: var(--accent);
|
||||||
color: var(--accent);
|
color: var(--accent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
textarea {
|
||||||
|
width: 100%;
|
||||||
|
min-height: 6rem;
|
||||||
|
padding: 0.75rem;
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: var(--radius);
|
||||||
|
background: var(--bg);
|
||||||
|
color: var(--text);
|
||||||
|
font: inherit;
|
||||||
|
font-size: 16px;
|
||||||
|
line-height: 1.45;
|
||||||
|
resize: vertical;
|
||||||
|
}
|
||||||
|
|
||||||
|
textarea:focus {
|
||||||
|
outline: 2px solid var(--accent);
|
||||||
|
outline-offset: 1px;
|
||||||
|
}
|
||||||
|
|
||||||
|
textarea.mono,
|
||||||
|
pre,
|
||||||
|
code {
|
||||||
|
font-family: ui-monospace, 'SF Mono', 'Cascadia Code', Menlo, Consolas, monospace;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Link that looks like a button */
|
||||||
|
.button-link {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
min-height: 44px;
|
||||||
|
padding: 0 1rem;
|
||||||
|
border: 1px solid var(--accent);
|
||||||
|
border-radius: var(--radius);
|
||||||
|
color: var(--accent);
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.button-link.small {
|
||||||
|
min-height: 36px;
|
||||||
|
padding: 0 0.75rem;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Two-option toggle */
|
||||||
|
.segmented {
|
||||||
|
display: inline-flex;
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: var(--radius);
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.segmented button {
|
||||||
|
border: none;
|
||||||
|
border-radius: 0;
|
||||||
|
color: var(--muted);
|
||||||
|
}
|
||||||
|
|
||||||
|
.segmented button.active {
|
||||||
|
background: var(--accent);
|
||||||
|
color: var(--bg);
|
||||||
|
}
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { computed, ref } from 'vue'
|
||||||
|
import { RouterLink } from 'vue-router'
|
||||||
|
|
||||||
|
import type { Clip } from '@/api/types'
|
||||||
|
import CopyButton from '@/components/CopyButton.vue'
|
||||||
|
import { formatDateTime } from '@/utils/format'
|
||||||
|
import { highlight, languageLabel } from '@/utils/highlight'
|
||||||
|
|
||||||
|
const props = defineProps<{ clip: Clip }>()
|
||||||
|
const emit = defineEmits<{ togglePin: []; remove: [] }>()
|
||||||
|
|
||||||
|
const PREVIEW_LINES = 10
|
||||||
|
const PREVIEW_CHARS = 2000
|
||||||
|
|
||||||
|
const expanded = ref(false)
|
||||||
|
|
||||||
|
const lines = computed(() => props.clip.content.split('\n'))
|
||||||
|
const isLong = computed(
|
||||||
|
() => lines.value.length > PREVIEW_LINES || props.clip.content.length > PREVIEW_CHARS,
|
||||||
|
)
|
||||||
|
const visibleText = computed(() =>
|
||||||
|
expanded.value || !isLong.value
|
||||||
|
? props.clip.content
|
||||||
|
: lines.value.slice(0, PREVIEW_LINES).join('\n').slice(0, PREVIEW_CHARS),
|
||||||
|
)
|
||||||
|
// Highlight only what is visible; large clips stay cheap until expanded
|
||||||
|
const html = computed(() =>
|
||||||
|
props.clip.kind === 'code' ? highlight(visibleText.value, props.clip.language) : null,
|
||||||
|
)
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<article class="card clip" :class="{ pinned: clip.pinned }">
|
||||||
|
<header class="clip-head">
|
||||||
|
<div class="clip-meta">
|
||||||
|
<strong v-if="clip.title" class="clip-title">{{ clip.title }}</strong>
|
||||||
|
<span v-if="clip.pinned" class="badge accent">angeheftet</span>
|
||||||
|
<span v-if="clip.kind === 'code'" class="badge">{{ languageLabel(clip.language) }}</span>
|
||||||
|
</div>
|
||||||
|
<span class="muted small">{{ formatDateTime(clip.updated_at) }}</span>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<!-- eslint-disable-next-line vue/no-v-html -- highlight.js output is escaped -->
|
||||||
|
<pre v-if="html !== null" class="content code"><code class="hljs" v-html="html"></code></pre>
|
||||||
|
<pre v-else class="content text">{{ visibleText }}</pre>
|
||||||
|
|
||||||
|
<button v-if="isLong" type="button" class="link" @click="expanded = !expanded">
|
||||||
|
{{ expanded ? 'weniger' : `mehr (${lines.length} Zeilen)` }}
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<footer class="actions">
|
||||||
|
<CopyButton :text="clip.content" />
|
||||||
|
<RouterLink :to="`/clips/${clip.id}`" class="button-link small">Bearbeiten</RouterLink>
|
||||||
|
<button type="button" class="small" @click="emit('togglePin')">
|
||||||
|
{{ clip.pinned ? 'Lösen' : 'Anheften' }}
|
||||||
|
</button>
|
||||||
|
<button type="button" class="small danger" @click="emit('remove')">Löschen</button>
|
||||||
|
</footer>
|
||||||
|
</article>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.clip.pinned {
|
||||||
|
border-color: var(--accent);
|
||||||
|
}
|
||||||
|
|
||||||
|
.clip-head {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
align-items: baseline;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 0.25rem 1rem;
|
||||||
|
margin-bottom: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.clip-meta {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.clip-title {
|
||||||
|
overflow-wrap: anywhere;
|
||||||
|
}
|
||||||
|
|
||||||
|
.small {
|
||||||
|
font-size: 0.85rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.content {
|
||||||
|
margin: 0;
|
||||||
|
padding: 0.75rem;
|
||||||
|
border-radius: calc(var(--radius) - 4px);
|
||||||
|
background: var(--bg);
|
||||||
|
overflow-x: auto;
|
||||||
|
font-size: 0.9rem;
|
||||||
|
line-height: 1.45;
|
||||||
|
}
|
||||||
|
|
||||||
|
.content.text {
|
||||||
|
white-space: pre-wrap;
|
||||||
|
overflow-wrap: anywhere;
|
||||||
|
font-family: inherit;
|
||||||
|
}
|
||||||
|
|
||||||
|
.content code.hljs {
|
||||||
|
padding: 0;
|
||||||
|
background: transparent;
|
||||||
|
}
|
||||||
|
|
||||||
|
.link {
|
||||||
|
min-height: 32px;
|
||||||
|
padding: 0;
|
||||||
|
border: none;
|
||||||
|
color: var(--accent);
|
||||||
|
}
|
||||||
|
|
||||||
|
.actions {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 0.5rem;
|
||||||
|
margin-top: 0.75rem;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { ref } from 'vue'
|
||||||
|
|
||||||
|
import { copyText } from '@/utils/clipboard'
|
||||||
|
|
||||||
|
const props = defineProps<{ text: string }>()
|
||||||
|
|
||||||
|
const state = ref<'idle' | 'copied' | 'failed'>('idle')
|
||||||
|
let timer: ReturnType<typeof setTimeout> | undefined
|
||||||
|
|
||||||
|
async function copy(): Promise<void> {
|
||||||
|
try {
|
||||||
|
await copyText(props.text)
|
||||||
|
state.value = 'copied'
|
||||||
|
} catch {
|
||||||
|
state.value = 'failed'
|
||||||
|
}
|
||||||
|
clearTimeout(timer)
|
||||||
|
timer = setTimeout(() => (state.value = 'idle'), 1500)
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<button type="button" class="primary small copy" :class="state" @click="copy">
|
||||||
|
{{ state === 'copied' ? 'Kopiert ✓' : state === 'failed' ? 'Fehlgeschlagen' : 'Kopieren' }}
|
||||||
|
</button>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.copy {
|
||||||
|
min-width: 7.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.copy.copied {
|
||||||
|
background: var(--ok);
|
||||||
|
border-color: var(--ok);
|
||||||
|
}
|
||||||
|
|
||||||
|
.copy.failed {
|
||||||
|
background: var(--error);
|
||||||
|
border-color: var(--error);
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import { computed, ref, watch } from 'vue'
|
||||||
|
|
||||||
|
import type { ClipKind } from '@/api/types'
|
||||||
|
import { useClipsStore } from '@/stores/clips'
|
||||||
|
import { readClipboardText } from '@/utils/clipboard'
|
||||||
|
import { detectLanguage, LANGUAGES } from '@/utils/highlight'
|
||||||
|
|
||||||
|
const store = useClipsStore()
|
||||||
|
|
||||||
|
const content = ref('')
|
||||||
|
const kind = ref<ClipKind>('text')
|
||||||
|
const language = ref<string>('plaintext')
|
||||||
|
const languageTouched = ref(false)
|
||||||
|
const busy = ref(false)
|
||||||
|
const error = ref<string | null>(null)
|
||||||
|
|
||||||
|
const canSave = computed(() => content.value.trim().length > 0 && !busy.value)
|
||||||
|
|
||||||
|
// Suggest a language while typing until the user picks one
|
||||||
|
watch([content, kind], () => {
|
||||||
|
if (kind.value !== 'code' || languageTouched.value) return
|
||||||
|
language.value = detectLanguage(content.value) ?? 'plaintext'
|
||||||
|
})
|
||||||
|
|
||||||
|
async function paste(): Promise<void> {
|
||||||
|
error.value = null
|
||||||
|
try {
|
||||||
|
const text = await readClipboardText()
|
||||||
|
content.value = text
|
||||||
|
if (!languageTouched.value && detectLanguage(text)) kind.value = 'code'
|
||||||
|
} catch (e) {
|
||||||
|
error.value = e instanceof Error ? e.message : String(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function save(): Promise<void> {
|
||||||
|
if (!canSave.value) return
|
||||||
|
busy.value = true
|
||||||
|
error.value = null
|
||||||
|
try {
|
||||||
|
await store.create({
|
||||||
|
content: content.value,
|
||||||
|
kind: kind.value,
|
||||||
|
language: kind.value === 'code' ? language.value : null,
|
||||||
|
})
|
||||||
|
content.value = ''
|
||||||
|
kind.value = 'text'
|
||||||
|
languageTouched.value = false
|
||||||
|
} catch (e) {
|
||||||
|
error.value = e instanceof Error ? e.message : String(e)
|
||||||
|
} finally {
|
||||||
|
busy.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<form class="card quick-add" @submit.prevent="save">
|
||||||
|
<textarea
|
||||||
|
v-model="content"
|
||||||
|
:class="{ mono: kind === 'code' }"
|
||||||
|
rows="4"
|
||||||
|
placeholder="Text oder Code einfügen …"
|
||||||
|
autocapitalize="off"
|
||||||
|
spellcheck="false"
|
||||||
|
@keydown.enter.ctrl.prevent="save"
|
||||||
|
@keydown.enter.meta.prevent="save"
|
||||||
|
></textarea>
|
||||||
|
<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"
|
||||||
|
@change="languageTouched = true"
|
||||||
|
>
|
||||||
|
<option v-for="l in LANGUAGES" :key="l.id" :value="l.id">{{ l.label }}</option>
|
||||||
|
</select>
|
||||||
|
<div class="spacer"></div>
|
||||||
|
<button type="button" @click="paste">Einfügen</button>
|
||||||
|
<button type="submit" class="primary" :disabled="!canSave">Speichern</button>
|
||||||
|
</div>
|
||||||
|
<p v-if="error" class="error" role="alert">{{ error }}</p>
|
||||||
|
</form>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.quick-add {
|
||||||
|
display: grid;
|
||||||
|
gap: 0.75rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.row {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.spacer {
|
||||||
|
flex: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.language {
|
||||||
|
width: auto;
|
||||||
|
min-width: 9rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.error {
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import 'highlight.js/styles/github-dark.css'
|
||||||
import './assets/main.css'
|
import './assets/main.css'
|
||||||
|
|
||||||
import { createPinia } from 'pinia'
|
import { createPinia } from 'pinia'
|
||||||
|
|||||||
@@ -0,0 +1,113 @@
|
|||||||
|
import { defineStore } from 'pinia'
|
||||||
|
import { ref } from 'vue'
|
||||||
|
|
||||||
|
import { ApiError, api } from '@/api/client'
|
||||||
|
import type { Clip, ClipInput, ClipList } from '@/api/types'
|
||||||
|
|
||||||
|
const PAGE_SIZE = 50
|
||||||
|
|
||||||
|
export class ClipConflictError extends Error {
|
||||||
|
constructor(public readonly current: Clip) {
|
||||||
|
super('Der Clip wurde inzwischen auf einem anderen Gerät geändert.')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Pinned first, then most recently edited – same order as the backend. */
|
||||||
|
function compareClips(a: Clip, b: Clip): number {
|
||||||
|
if (a.pinned !== b.pinned) return a.pinned ? -1 : 1
|
||||||
|
if (a.updated_at !== b.updated_at) return a.updated_at < b.updated_at ? 1 : -1
|
||||||
|
return b.id - a.id
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useClipsStore = defineStore('clips', () => {
|
||||||
|
const items = ref<Clip[]>([])
|
||||||
|
const total = ref(0)
|
||||||
|
const loading = ref(false)
|
||||||
|
const error = ref<string | null>(null)
|
||||||
|
|
||||||
|
/** Insert or replace a clip locally (also the entry point for live sync later). */
|
||||||
|
function upsert(clip: Clip): void {
|
||||||
|
const index = items.value.findIndex((c) => c.id === clip.id)
|
||||||
|
if (index === -1) {
|
||||||
|
items.value.push(clip)
|
||||||
|
total.value += 1
|
||||||
|
} else {
|
||||||
|
items.value[index] = clip
|
||||||
|
}
|
||||||
|
items.value.sort(compareClips)
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeLocal(id: number): void {
|
||||||
|
const before = items.value.length
|
||||||
|
items.value = items.value.filter((c) => c.id !== id)
|
||||||
|
total.value -= before - items.value.length
|
||||||
|
}
|
||||||
|
|
||||||
|
async function load(): Promise<void> {
|
||||||
|
loading.value = true
|
||||||
|
error.value = null
|
||||||
|
try {
|
||||||
|
const page = await api.get<ClipList>(`/clips?limit=${PAGE_SIZE}&offset=0`)
|
||||||
|
items.value = page.items
|
||||||
|
total.value = page.total
|
||||||
|
} catch (e) {
|
||||||
|
error.value = e instanceof Error ? e.message : String(e)
|
||||||
|
} finally {
|
||||||
|
loading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadMore(): Promise<void> {
|
||||||
|
const page = await api.get<ClipList>(`/clips?limit=${PAGE_SIZE}&offset=${items.value.length}`)
|
||||||
|
for (const clip of page.items) upsert(clip)
|
||||||
|
total.value = page.total
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchOne(id: number): Promise<Clip> {
|
||||||
|
const clip = await api.get<Clip>(`/clips/${id}`)
|
||||||
|
upsert(clip)
|
||||||
|
return clip
|
||||||
|
}
|
||||||
|
|
||||||
|
async function create(input: ClipInput): Promise<Clip> {
|
||||||
|
const clip = await api.post<Clip>('/clips', input)
|
||||||
|
upsert(clip)
|
||||||
|
return clip
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Pass the version the edit is based on to detect concurrent changes. */
|
||||||
|
async function update(id: number, changes: ClipInput, version?: number): Promise<Clip> {
|
||||||
|
try {
|
||||||
|
const clip = await api.patch<Clip>(`/clips/${id}`, { ...changes, version })
|
||||||
|
upsert(clip)
|
||||||
|
return clip
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof ApiError && e.status === 409) {
|
||||||
|
const current = (e.data as { current: Clip }).current
|
||||||
|
upsert(current)
|
||||||
|
throw new ClipConflictError(current)
|
||||||
|
}
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function remove(id: number): Promise<void> {
|
||||||
|
await api.delete(`/clips/${id}`)
|
||||||
|
removeLocal(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
items,
|
||||||
|
total,
|
||||||
|
loading,
|
||||||
|
error,
|
||||||
|
upsert,
|
||||||
|
removeLocal,
|
||||||
|
load,
|
||||||
|
loadMore,
|
||||||
|
fetchOne,
|
||||||
|
create,
|
||||||
|
update,
|
||||||
|
remove,
|
||||||
|
}
|
||||||
|
})
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
/** Copy text; needs HTTPS and a user gesture. Falls back to execCommand for old browsers. */
|
||||||
|
export async function copyText(text: string): Promise<void> {
|
||||||
|
if (navigator.clipboard?.writeText) {
|
||||||
|
await navigator.clipboard.writeText(text)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const area = document.createElement('textarea')
|
||||||
|
area.value = text
|
||||||
|
area.setAttribute('readonly', '')
|
||||||
|
area.style.position = 'fixed'
|
||||||
|
area.style.opacity = '0'
|
||||||
|
document.body.appendChild(area)
|
||||||
|
area.select()
|
||||||
|
try {
|
||||||
|
if (!document.execCommand('copy')) throw new Error('Kopieren nicht möglich.')
|
||||||
|
} finally {
|
||||||
|
area.remove()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function readClipboardText(): Promise<string> {
|
||||||
|
if (!navigator.clipboard?.readText) {
|
||||||
|
throw new Error('Dieser Browser erlaubt kein Einfügen per Knopf. Bitte manuell einfügen.')
|
||||||
|
}
|
||||||
|
return navigator.clipboard.readText()
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
import hljs from 'highlight.js/lib/core'
|
||||||
|
import bash from 'highlight.js/lib/languages/bash'
|
||||||
|
import c from 'highlight.js/lib/languages/c'
|
||||||
|
import cpp from 'highlight.js/lib/languages/cpp'
|
||||||
|
import csharp from 'highlight.js/lib/languages/csharp'
|
||||||
|
import css from 'highlight.js/lib/languages/css'
|
||||||
|
import diff from 'highlight.js/lib/languages/diff'
|
||||||
|
import dockerfile from 'highlight.js/lib/languages/dockerfile'
|
||||||
|
import go from 'highlight.js/lib/languages/go'
|
||||||
|
import ini from 'highlight.js/lib/languages/ini'
|
||||||
|
import java from 'highlight.js/lib/languages/java'
|
||||||
|
import javascript from 'highlight.js/lib/languages/javascript'
|
||||||
|
import json from 'highlight.js/lib/languages/json'
|
||||||
|
import markdown from 'highlight.js/lib/languages/markdown'
|
||||||
|
import nginx from 'highlight.js/lib/languages/nginx'
|
||||||
|
import php from 'highlight.js/lib/languages/php'
|
||||||
|
import plaintext from 'highlight.js/lib/languages/plaintext'
|
||||||
|
import powershell from 'highlight.js/lib/languages/powershell'
|
||||||
|
import python from 'highlight.js/lib/languages/python'
|
||||||
|
import ruby from 'highlight.js/lib/languages/ruby'
|
||||||
|
import rust from 'highlight.js/lib/languages/rust'
|
||||||
|
import sql from 'highlight.js/lib/languages/sql'
|
||||||
|
import typescript from 'highlight.js/lib/languages/typescript'
|
||||||
|
import xml from 'highlight.js/lib/languages/xml'
|
||||||
|
import yaml from 'highlight.js/lib/languages/yaml'
|
||||||
|
|
||||||
|
/** Must match backend/app/languages.py */
|
||||||
|
export const LANGUAGES = [
|
||||||
|
{ id: 'plaintext', label: 'Nur Text', grammar: plaintext },
|
||||||
|
{ id: 'bash', label: 'Bash / Shell', grammar: bash },
|
||||||
|
{ id: 'powershell', label: 'PowerShell', grammar: powershell },
|
||||||
|
{ id: 'python', label: 'Python', grammar: python },
|
||||||
|
{ id: 'javascript', label: 'JavaScript', grammar: javascript },
|
||||||
|
{ id: 'typescript', label: 'TypeScript', grammar: typescript },
|
||||||
|
{ id: 'json', label: 'JSON', grammar: json },
|
||||||
|
{ id: 'yaml', label: 'YAML', grammar: yaml },
|
||||||
|
{ id: 'ini', label: 'INI / TOML', grammar: ini },
|
||||||
|
{ id: 'xml', label: 'HTML / XML', grammar: xml },
|
||||||
|
{ id: 'css', label: 'CSS', grammar: css },
|
||||||
|
{ id: 'sql', label: 'SQL', grammar: sql },
|
||||||
|
{ id: 'markdown', label: 'Markdown', grammar: markdown },
|
||||||
|
{ id: 'dockerfile', label: 'Dockerfile', grammar: dockerfile },
|
||||||
|
{ id: 'nginx', label: 'Nginx', grammar: nginx },
|
||||||
|
{ id: 'go', label: 'Go', grammar: go },
|
||||||
|
{ id: 'rust', label: 'Rust', grammar: rust },
|
||||||
|
{ id: 'java', label: 'Java', grammar: java },
|
||||||
|
{ id: 'c', label: 'C', grammar: c },
|
||||||
|
{ id: 'cpp', label: 'C++', grammar: cpp },
|
||||||
|
{ id: 'csharp', label: 'C#', grammar: csharp },
|
||||||
|
{ id: 'php', label: 'PHP', grammar: php },
|
||||||
|
{ id: 'ruby', label: 'Ruby', grammar: ruby },
|
||||||
|
{ id: 'diff', label: 'Diff', grammar: diff },
|
||||||
|
] as const
|
||||||
|
|
||||||
|
export type LanguageId = (typeof LANGUAGES)[number]['id']
|
||||||
|
|
||||||
|
for (const lang of LANGUAGES) hljs.registerLanguage(lang.id, lang.grammar)
|
||||||
|
|
||||||
|
const DETECTABLE = LANGUAGES.map((l) => l.id).filter((id) => id !== 'plaintext')
|
||||||
|
|
||||||
|
export function languageLabel(id: string | null): string {
|
||||||
|
return LANGUAGES.find((l) => l.id === id)?.label ?? id ?? ''
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns escaped HTML, safe for v-html. */
|
||||||
|
export function highlight(code: string, language: string | null): string {
|
||||||
|
const lang = language && hljs.getLanguage(language) ? language : 'plaintext'
|
||||||
|
return hljs.highlight(code, { language: lang, ignoreIllegals: true }).value
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Best guess for a snippet, or null if nothing fits convincingly. */
|
||||||
|
export function detectLanguage(code: string): LanguageId | null {
|
||||||
|
if (code.trim().length < 20) return null
|
||||||
|
const result = hljs.highlightAuto(code.slice(0, 5000), DETECTABLE)
|
||||||
|
return result.relevance >= 5 ? (result.language as LanguageId) : null
|
||||||
|
}
|
||||||
@@ -1,48 +1,69 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { onMounted } from 'vue'
|
import { onMounted } from 'vue'
|
||||||
|
|
||||||
import { useAuthStore } from '@/stores/auth'
|
import type { Clip } from '@/api/types'
|
||||||
import { useHealthStore } from '@/stores/health'
|
import ClipCard from '@/components/ClipCard.vue'
|
||||||
|
import QuickAdd from '@/components/QuickAdd.vue'
|
||||||
|
import { useClipsStore } from '@/stores/clips'
|
||||||
|
|
||||||
const auth = useAuthStore()
|
const store = useClipsStore()
|
||||||
const store = useHealthStore()
|
|
||||||
|
|
||||||
onMounted(store.refresh)
|
async function togglePin(clip: Clip): Promise<void> {
|
||||||
|
try {
|
||||||
|
await store.update(clip.id, { pinned: !clip.pinned })
|
||||||
|
} catch (e) {
|
||||||
|
store.error = e instanceof Error ? e.message : String(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function remove(clip: Clip): Promise<void> {
|
||||||
|
if (!confirm('Diesen Clip endgültig löschen?')) return
|
||||||
|
try {
|
||||||
|
await store.remove(clip.id)
|
||||||
|
} catch (e) {
|
||||||
|
store.error = e instanceof Error ? e.message : String(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onMounted(store.load)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<main class="page">
|
<main class="page">
|
||||||
<h1>Hallo {{ auth.user?.username }}</h1>
|
<QuickAdd />
|
||||||
<section class="card">
|
|
||||||
<h2>Systemstatus</h2>
|
<p v-if="store.error" class="error" role="alert">{{ store.error }}</p>
|
||||||
<p v-if="store.loading" class="muted">Prüfe …</p>
|
<p v-if="store.loading && store.items.length === 0" class="muted">Lade …</p>
|
||||||
<p v-else-if="store.error" class="error">Backend nicht erreichbar: {{ store.error }}</p>
|
<p v-else-if="!store.loading && store.items.length === 0" class="muted empty">
|
||||||
<dl v-else-if="store.health">
|
Noch keine Clips. Füge oben Text oder Code ein.
|
||||||
<dt>Status</dt>
|
</p>
|
||||||
<dd :class="store.health.status">{{ store.health.status }}</dd>
|
|
||||||
<dt>Datenbank</dt>
|
<ClipCard
|
||||||
<dd :class="store.health.db">{{ store.health.db }}</dd>
|
v-for="clip in store.items"
|
||||||
<dt>Version</dt>
|
:key="clip.id"
|
||||||
<dd>{{ store.health.version }}</dd>
|
:clip="clip"
|
||||||
</dl>
|
@toggle-pin="togglePin(clip)"
|
||||||
<button type="button" :disabled="store.loading" @click="store.refresh">Aktualisieren</button>
|
@remove="remove(clip)"
|
||||||
</section>
|
/>
|
||||||
|
|
||||||
|
<button
|
||||||
|
v-if="store.items.length < store.total"
|
||||||
|
type="button"
|
||||||
|
class="more"
|
||||||
|
@click="store.loadMore"
|
||||||
|
>
|
||||||
|
Weitere laden ({{ store.total - store.items.length }})
|
||||||
|
</button>
|
||||||
</main>
|
</main>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
dl {
|
.empty {
|
||||||
display: grid;
|
text-align: center;
|
||||||
grid-template-columns: max-content 1fr;
|
padding: 2rem 0;
|
||||||
gap: 0.25rem 1rem;
|
|
||||||
margin: 0 0 1rem;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dt {
|
.more {
|
||||||
color: var(--muted);
|
width: 100%;
|
||||||
}
|
|
||||||
|
|
||||||
dd {
|
|
||||||
margin: 0;
|
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
Reference in new issue
Block a user