diff --git a/frontend/src/components/QuickAdd.vue b/frontend/src/components/QuickAdd.vue index def5503..c52b31a 100644 --- a/frontend/src/components/QuickAdd.vue +++ b/frontend/src/components/QuickAdd.vue @@ -23,6 +23,17 @@ watch([content, kind], () => { language.value = detectLanguage(content.value) ?? 'plaintext' }) +// Content shared from another app (share target) arrives as a draft +watch( + () => store.draft, + (draft) => { + if (!draft) return + content.value = draft + store.draft = null + }, + { immediate: true }, +) + async function paste(): Promise { error.value = null try { diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts index 6b0ffc6..2af8471 100644 --- a/frontend/src/router/index.ts +++ b/frontend/src/router/index.ts @@ -2,6 +2,8 @@ import { createRouter, createWebHistory } from 'vue-router' import { ApiError } from '@/api/client' import { useAuthStore } from '@/stores/auth' +import { useClipsStore } from '@/stores/clips' +import { sharedContent } from '@/utils/share' import HomeView from '@/views/HomeView.vue' declare module 'vue-router' { @@ -34,6 +36,16 @@ const router = createRouter({ component: () => import('@/views/AdminUsersView.vue'), meta: { admin: true }, }, + { + // Web Share Target: hand the shared content to the quick-add form + path: '/share', + name: 'share', + component: HomeView, + beforeEnter: (to) => { + useClipsStore().draft = sharedContent(to.query) + return { name: 'home', replace: true } + }, + }, { path: '/offline', name: 'offline', diff --git a/frontend/src/stores/clips.ts b/frontend/src/stores/clips.ts index 75025f8..eeeb2aa 100644 --- a/frontend/src/stores/clips.ts +++ b/frontend/src/stores/clips.ts @@ -25,6 +25,8 @@ export const useClipsStore = defineStore('clips', () => { const loading = ref(false) const error = ref(null) const loaded = ref(false) + /** Content handed over from the share target, picked up by the quick-add form. */ + const draft = ref(null) /** * Insert or replace a clip locally. Used for API responses and live-sync events; @@ -49,6 +51,7 @@ export const useClipsStore = defineStore('clips', () => { total.value = 0 error.value = null loaded.value = false + draft.value = null } function removeLocal(id: number): void { @@ -117,6 +120,7 @@ export const useClipsStore = defineStore('clips', () => { loading, error, loaded, + draft, upsert, removeLocal, reset, diff --git a/frontend/src/utils/share.test.ts b/frontend/src/utils/share.test.ts new file mode 100644 index 0000000..3155272 --- /dev/null +++ b/frontend/src/utils/share.test.ts @@ -0,0 +1,36 @@ +import { describe, expect, it } from 'vitest' + +import { sharedContent } from './share' + +describe('sharedContent', () => { + it('combines text and url', () => { + expect(sharedContent({ text: 'Look at this', url: 'https://example.com' })).toBe( + 'Look at this\nhttps://example.com', + ) + }) + + it('does not repeat a url already contained in the text', () => { + expect(sharedContent({ text: 'see https://example.com', url: 'https://example.com' })).toBe( + 'see https://example.com', + ) + }) + + it('uses the url alone', () => { + expect(sharedContent({ title: 'Example', url: 'https://example.com' })).toBe( + 'https://example.com', + ) + }) + + it('falls back to the title', () => { + expect(sharedContent({ title: 'Only a title' })).toBe('Only a title') + }) + + it('returns null for empty shares', () => { + expect(sharedContent({})).toBeNull() + expect(sharedContent({ text: ' ', url: '' })).toBeNull() + }) + + it('takes the first value of repeated parameters', () => { + expect(sharedContent({ text: ['first', 'second'] })).toBe('first') + }) +}) diff --git a/frontend/src/utils/share.ts b/frontend/src/utils/share.ts new file mode 100644 index 0000000..9689e91 --- /dev/null +++ b/frontend/src/utils/share.ts @@ -0,0 +1,22 @@ +import type { LocationQuery } from 'vue-router' + +function param(query: LocationQuery, name: string): string { + const value = query[name] + const first = Array.isArray(value) ? value[0] : value + return (first ?? '').trim() +} + +/** + * Build clip content from Web Share Target parameters (title, text, url). + * Apps fill these inconsistently: the link may be in `text`, in `url`, or in both. + */ +export function sharedContent(query: LocationQuery): string | null { + const title = param(query, 'title') + const text = param(query, 'text') + const url = param(query, 'url') + + const parts = [text] + if (url && !text.includes(url)) parts.push(url) + const content = parts.filter(Boolean).join('\n') + return content || title || null +} diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index cf9fe91..0c8f3d0 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -23,6 +23,12 @@ export default defineConfig({ display: 'standalone', background_color: '#0f1115', theme_color: '#0f1115', + // Android / Chrome: appear in the system share sheet; see src/utils/share.ts + share_target: { + action: '/share', + method: 'GET', + params: { title: 'title', text: 'text', url: 'url' }, + }, icons: [ { src: '/icons/icon-192.png', sizes: '192x192', type: 'image/png' }, { src: '/icons/icon-512.png', sizes: '512x512', type: 'image/png' },