From 7b16940b96f82beacb29c658ef194b0fb713a56c Mon Sep 17 00:00:00 2001 From: dbildhauer Date: Sun, 25 Oct 2026 10:53:04 +0100 Subject: [PATCH] feat: clip-liste mit schnell-eingabe und kopieren MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- frontend/package-lock.json | 10 ++ frontend/package.json | 1 + frontend/src/api/client.ts | 20 ++-- frontend/src/api/types.ts | 21 ++++ frontend/src/assets/main.css | 62 ++++++++++++ frontend/src/components/ClipCard.vue | 126 +++++++++++++++++++++++ frontend/src/components/CopyButton.vue | 43 ++++++++ frontend/src/components/QuickAdd.vue | 133 +++++++++++++++++++++++++ frontend/src/main.ts | 1 + frontend/src/stores/clips.ts | 113 +++++++++++++++++++++ frontend/src/utils/clipboard.ts | 26 +++++ frontend/src/utils/highlight.ts | 76 ++++++++++++++ frontend/src/views/HomeView.vue | 83 +++++++++------ 13 files changed, 673 insertions(+), 42 deletions(-) create mode 100644 frontend/src/components/ClipCard.vue create mode 100644 frontend/src/components/CopyButton.vue create mode 100644 frontend/src/components/QuickAdd.vue create mode 100644 frontend/src/stores/clips.ts create mode 100644 frontend/src/utils/clipboard.ts create mode 100644 frontend/src/utils/highlight.ts diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 3b2451e..dcadf76 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -8,6 +8,7 @@ "name": "lan-clipboard-frontend", "version": "0.1.0", "dependencies": { + "highlight.js": "^11.12.0", "pinia": "^4.1.0", "vue": "^3.5.44", "vue-router": "^5.4.0" @@ -2030,6 +2031,15 @@ "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": { "version": "5.5.3", "resolved": "https://registry.npmjs.org/hookable/-/hookable-5.5.3.tgz", diff --git a/frontend/package.json b/frontend/package.json index a67786b..4188915 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -13,6 +13,7 @@ "format:check": "prettier --check src/" }, "dependencies": { + "highlight.js": "^11.12.0", "pinia": "^4.1.0", "vue": "^3.5.44", "vue-router": "^5.4.0" diff --git a/frontend/src/api/client.ts b/frontend/src/api/client.ts index c9afdf6..960f52a 100644 --- a/frontend/src/api/client.ts +++ b/frontend/src/api/client.ts @@ -2,6 +2,8 @@ export class ApiError extends Error { constructor( public readonly status: number, message: string, + /** Parsed JSON body of the error response, if any */ + public readonly data: unknown = null, ) { super(message) } @@ -21,16 +23,11 @@ function readCookie(name: string): string | null { return match ? decodeURIComponent(match.slice(name.length + 1)) : null } -async function errorMessage(response: Response): Promise { - try { - const data: unknown = await response.json() - if (data && typeof data === 'object' && 'detail' in data) { - const detail = (data as { detail: unknown }).detail - if (typeof detail === 'string') return detail - if (Array.isArray(detail)) return 'Ungültige Eingabe.' - } - } catch { - // body is not JSON +function errorMessage(response: Response, data: unknown): string { + if (data && typeof data === 'object' && 'detail' in data) { + const detail = (data as { detail: unknown }).detail + if (typeof detail === 'string') return detail + if (Array.isArray(detail)) return 'Ungültige Eingabe.' } return `${response.status} ${response.statusText}` } @@ -52,7 +49,8 @@ export async function apiRequest(method: string, path: string, body?: unknown if (!response.ok) { 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 return (await response.json()) as T diff --git a/frontend/src/api/types.ts b/frontend/src/api/types.ts index 98bd798..3511bba 100644 --- a/frontend/src/api/types.ts +++ b/frontend/src/api/types.ts @@ -17,3 +17,24 @@ export interface SessionInfo { user_agent: string | null 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> diff --git a/frontend/src/assets/main.css b/frontend/src/assets/main.css index 8e42a05..3664042 100644 --- a/frontend/src/assets/main.css +++ b/frontend/src/assets/main.css @@ -153,3 +153,65 @@ button:disabled { border-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); +} diff --git a/frontend/src/components/ClipCard.vue b/frontend/src/components/ClipCard.vue new file mode 100644 index 0000000..581f8ed --- /dev/null +++ b/frontend/src/components/ClipCard.vue @@ -0,0 +1,126 @@ + + + + + diff --git a/frontend/src/components/CopyButton.vue b/frontend/src/components/CopyButton.vue new file mode 100644 index 0000000..0ada518 --- /dev/null +++ b/frontend/src/components/CopyButton.vue @@ -0,0 +1,43 @@ + + + + + diff --git a/frontend/src/components/QuickAdd.vue b/frontend/src/components/QuickAdd.vue new file mode 100644 index 0000000..def5503 --- /dev/null +++ b/frontend/src/components/QuickAdd.vue @@ -0,0 +1,133 @@ + + + + + diff --git a/frontend/src/main.ts b/frontend/src/main.ts index c5f68f3..0524453 100644 --- a/frontend/src/main.ts +++ b/frontend/src/main.ts @@ -1,3 +1,4 @@ +import 'highlight.js/styles/github-dark.css' import './assets/main.css' import { createPinia } from 'pinia' diff --git a/frontend/src/stores/clips.ts b/frontend/src/stores/clips.ts new file mode 100644 index 0000000..7c1cfea --- /dev/null +++ b/frontend/src/stores/clips.ts @@ -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([]) + const total = ref(0) + const loading = ref(false) + const error = ref(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 { + loading.value = true + error.value = null + try { + const page = await api.get(`/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 { + const page = await api.get(`/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 { + const clip = await api.get(`/clips/${id}`) + upsert(clip) + return clip + } + + async function create(input: ClipInput): Promise { + const clip = await api.post('/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 { + try { + const clip = await api.patch(`/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 { + await api.delete(`/clips/${id}`) + removeLocal(id) + } + + return { + items, + total, + loading, + error, + upsert, + removeLocal, + load, + loadMore, + fetchOne, + create, + update, + remove, + } +}) diff --git a/frontend/src/utils/clipboard.ts b/frontend/src/utils/clipboard.ts new file mode 100644 index 0000000..65a1e7f --- /dev/null +++ b/frontend/src/utils/clipboard.ts @@ -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 { + 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 { + if (!navigator.clipboard?.readText) { + throw new Error('Dieser Browser erlaubt kein Einfügen per Knopf. Bitte manuell einfügen.') + } + return navigator.clipboard.readText() +} diff --git a/frontend/src/utils/highlight.ts b/frontend/src/utils/highlight.ts new file mode 100644 index 0000000..4866ee0 --- /dev/null +++ b/frontend/src/utils/highlight.ts @@ -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 +} diff --git a/frontend/src/views/HomeView.vue b/frontend/src/views/HomeView.vue index 59070cf..2a2018d 100644 --- a/frontend/src/views/HomeView.vue +++ b/frontend/src/views/HomeView.vue @@ -1,48 +1,69 @@