The minimalistic document archiving platform.
feat(pdf-viewer): add document search controls (#1552)
Corentin Thomassete7acb46f9bc6
diff --git a/.changeset/solid-shoes-lick.md b/.changeset/solid-shoes-lick.mdnew file mode 100644index 00000000..7a674d7e--- /dev/null+++ b/.changeset/solid-shoes-lick.md@@ -0,0 +1,5 @@+---+'@papra/app': patch+---++Search across all pages in the advanced PDF viewer using Ctrl+F / Cmd+F, with match highlighting and previous/next navigation.diff --git a/apps/papra-client/src/locales/en.dictionary.ts b/apps/papra-client/src/locales/en.dictionary.tsindex 322cbd0e..09faee65 100644--- a/apps/papra-client/src/locales/en.dictionary.ts+++ b/apps/papra-client/src/locales/en.dictionary.ts@@ -582,6 +582,18 @@ export const translations = {'documents.pdf-viewer.toolbar.download': 'Download','documents.pdf-viewer.toolbar.print': 'Print',+ 'documents.pdf-viewer.search.label': 'Search document',+ 'documents.pdf-viewer.search.placeholder': 'Find in document...',+ 'documents.pdf-viewer.search.previous': 'Previous match (Shift+Enter)',+ 'documents.pdf-viewer.search.next': 'Next match (Enter)',+ 'documents.pdf-viewer.search.close': 'Close search (Escape)',+ 'documents.pdf-viewer.search.searching': 'Searching...',+ 'documents.pdf-viewer.search.no-matches': 'No matches',+ 'documents.pdf-viewer.search.matches': '{{current}} of {{total}}',+ 'documents.pdf-viewer.search.match-case': 'Match case',+ 'documents.pdf-viewer.search.whole-words': 'Whole words',+ 'documents.pdf-viewer.search.highlight-all': 'Highlight all matches',+'documents.pdf-viewer.zoom.zoom-out': 'Zoom out','documents.pdf-viewer.zoom.zoom-in': 'Zoom in','documents.pdf-viewer.zoom.auto': 'Auto',diff --git a/apps/papra-client/src/locales/fr.dictionary.ts b/apps/papra-client/src/locales/fr.dictionary.tsindex 21b32a4f..d2aedc44 100644--- a/apps/papra-client/src/locales/fr.dictionary.ts+++ b/apps/papra-client/src/locales/fr.dictionary.ts@@ -571,6 +571,18 @@ export const translations: Partial<TranslationsDictionary> = {'documents.pdf-viewer.toolbar.download': 'Télécharger','documents.pdf-viewer.toolbar.print': 'Imprimer',+ 'documents.pdf-viewer.search.label': 'Rechercher dans le document',+ 'documents.pdf-viewer.search.placeholder': 'Rechercher dans le document...',+ 'documents.pdf-viewer.search.previous': 'Résultat précédent (Maj+Entrée)',+ 'documents.pdf-viewer.search.next': 'Résultat suivant (Entrée)',+ 'documents.pdf-viewer.search.close': 'Fermer la recherche (Échap)',+ 'documents.pdf-viewer.search.searching': 'Recherche en cours...',+ 'documents.pdf-viewer.search.no-matches': 'Aucun résultat',+ 'documents.pdf-viewer.search.matches': '{{current}} sur {{total}}',+ 'documents.pdf-viewer.search.match-case': 'Respecter la casse',+ 'documents.pdf-viewer.search.whole-words': 'Mots entiers',+ 'documents.pdf-viewer.search.highlight-all': 'Tout surligner',+'documents.pdf-viewer.zoom.zoom-out': 'Dézoomer','documents.pdf-viewer.zoom.zoom-in': 'Zoomer','documents.pdf-viewer.zoom.auto': 'Automatique',diff --git a/apps/papra-client/src/modules/documents/components/pdf-viewer/full-pdf-viewer.component.tsx b/apps/papra-client/src/modules/documents/components/pdf-viewer/full-pdf-viewer.component.tsxindex 90a89fdc..7aab999d 100644--- a/apps/papra-client/src/modules/documents/components/pdf-viewer/full-pdf-viewer.component.tsx+++ b/apps/papra-client/src/modules/documents/components/pdf-viewer/full-pdf-viewer.component.tsx@@ -1,6 +1,7 @@import type { Component } from 'solid-js';-import { createSignal, onCleanup, onMount } from 'solid-js';+import { createSignal, createUniqueId, onCleanup, onMount, Show } from 'solid-js';import { cn } from '@/modules/shared/style/cn';+import { PdfSearch } from './pdf-search.component';import { SideBar } from './sidebar/sidebar.component';import { PdfViewerToolbar } from './toolbar/pdf-viewer-toolbar.component';import { usePdfViewer } from './use-pdf-viewer';@@ -20,6 +21,25 @@ export const PdfViewer: Component<{ url: string }> = (props) => {const [isSidebarOpen, setIsSidebarOpen] = createSignal(true);const [sidebarWidth, setSidebarWidth] = createSignal(SIDEBAR_DEFAULT_WIDTH);const [isDragging, setIsDragging] = createSignal(false);+ const [isSearchOpen, setIsSearchOpen] = createSignal(false);+ const searchId = createUniqueId();+ let searchInput: HTMLInputElement | undefined;+ let previousFocus: HTMLElement | undefined;++ const openSearch = () => {+ if (!isSearchOpen()) {+ previousFocus =+ document.activeElement instanceof HTMLElement ? document.activeElement : undefined;+ }+ setIsSearchOpen(true);+ searchInput?.focus();+ searchInput?.select();+ };++ const closeSearch = () => {+ setIsSearchOpen(false);+ previousFocus?.focus();+ };onMount(() => {const observer = new ResizeObserver((entries) => {@@ -42,11 +62,20 @@ export const PdfViewer: Component<{ url: string }> = (props) => {});const handleKeyDown = (e: KeyboardEvent) => {+ if (e.key === 'Escape' && isSearchOpen()) {+ e.preventDefault();+ closeSearch();+ return;+ }+if (!e.ctrlKey && !e.metaKey) {return;}- if (e.key === '=' || e.key === '+') {+ if (e.key.toLowerCase() === 'f' && !e.altKey && store.pdfSlick) {+ e.preventDefault();+ openSearch();+ } else if (e.key === '=' || e.key === '+') {e.preventDefault();store.pdfSlick?.viewer?.increaseScale();} else if (e.key === '-') {@@ -106,9 +135,12 @@ export const PdfViewer: Component<{ url: string }> = (props) => {store={store}isSidebarOpen={isSidebarOpen}setIsSidebarOpen={setIsSidebarOpen}+ isSearchOpen={isSearchOpen}+ searchId={searchId}+ onOpenSearch={openSearch}/>- <div class="flex-1 flex overflow-hidden min-h-0">+ <div class="relative flex-1 flex overflow-hidden min-h-0"><divclass={cn('shrink-0 h-full overflow-hidden', {'transition-width duration-200': !isDragging(),@@ -127,6 +159,20 @@ export const PdfViewer: Component<{ url: string }> = (props) => {<div class="flex-1 relative h-full min-w-0"><PDFSlickViewer {...{ store, viewerRef }} /></div>++ <Show when={store.pdfSlick} keyed>+ {(pdfSlick) => (+ <PdfSearch+ id={searchId}+ pdfSlick={pdfSlick}+ isOpen={isSearchOpen}+ onClose={closeSearch}+ inputRef={(input) => {+ searchInput = input;+ }}+ />+ )}+ </Show></div></div>);diff --git a/apps/papra-client/src/modules/documents/components/pdf-viewer/pdf-search.component.tsx b/apps/papra-client/src/modules/documents/components/pdf-viewer/pdf-search.component.tsxnew file mode 100644index 00000000..6314d335--- /dev/null+++ b/apps/papra-client/src/modules/documents/components/pdf-viewer/pdf-search.component.tsx@@ -0,0 +1,152 @@+import type { PDFSlick } from '@pdfslick/solid';+import type { Accessor, Component } from 'solid-js';+import { createEffect, Match, on, onCleanup, Switch } from 'solid-js';+import { useI18n } from '@/modules/i18n/i18n.provider';+import { cn } from '@/modules/shared/style/cn';+import { Button } from '@/modules/ui/components/button';+import { Checkbox, CheckboxControl, CheckboxLabel } from '@/modules/ui/components/checkbox';+import { TextField, TextFieldRoot } from '@/modules/ui/components/textfield';+import { createPdfSearch } from './pdf-search';++export const PdfSearch: Component<{+ id: string;+ pdfSlick: PDFSlick;+ isOpen: Accessor<boolean>;+ onClose: () => void;+ inputRef: (input: HTMLInputElement) => void;+}> = (props) => {+ const { t } = useI18n();+ const search = createPdfSearch({ eventBus: props.pdfSlick.eventBus });++ createEffect(+ on(props.isOpen, (isOpen, wasOpen) => {+ if (isOpen) {+ search.open();+ } else if (wasOpen) {+ search.close();+ }+ }),+ );++ onCleanup(search.dispose);++ const handleKeyDown = (event: KeyboardEvent) => {+ if (event.key === 'Enter' && !event.isComposing) {+ event.preventDefault();+ if (event.shiftKey) {+ search.previous();+ } else {+ search.next();+ }+ }+ };++ const cannotNavigate = () => !search.query() || search.status() === 'not-found';++ return (+ <div+ id={props.id}+ role="search"+ aria-label={t('documents.pdf-viewer.search.label')}+ class={cn(+ 'absolute top-2 right-2 z-20 w-96 rounded-lg border bg-popover text-popover-foreground p-2 shadow-md',+ {+ hidden: !props.isOpen(),+ },+ )}+ style={{ 'max-width': 'calc(100% - 1rem)' }}+ >+ <div class="flex items-center gap-1">+ <TextFieldRoot value={search.query()} onChange={search.setQuery} class="flex-1 min-w-0">+ <TextField+ ref={props.inputRef}+ aria-label={t('documents.pdf-viewer.search.label')}+ placeholder={t('documents.pdf-viewer.search.placeholder')}+ class="h-8 px-2"+ onKeyDown={handleKeyDown}+ />+ </TextFieldRoot>++ <Button+ variant="ghost"+ size="icon"+ class="size-8 shrink-0"+ aria-label={t('documents.pdf-viewer.search.previous')}+ title={t('documents.pdf-viewer.search.previous')}+ disabled={cannotNavigate()}+ onClick={search.previous}+ >+ <div class="i-tabler-chevron-up size-4" />+ </Button>+ <Button+ variant="ghost"+ size="icon"+ class="size-8 shrink-0"+ aria-label={t('documents.pdf-viewer.search.next')}+ title={t('documents.pdf-viewer.search.next')}+ disabled={cannotNavigate()}+ onClick={search.next}+ >+ <div class="i-tabler-chevron-down size-4" />+ </Button>+ <Button+ variant="ghost"+ size="icon"+ class="size-8 shrink-0"+ aria-label={t('documents.pdf-viewer.search.close')}+ title={t('documents.pdf-viewer.search.close')}+ onClick={props.onClose}+ >+ <div class="i-tabler-x size-4" />+ </Button>+ </div>++ <div role="status" aria-live="polite" class="min-h-4 mt-1 text-xs text-muted-foreground">+ <Switch>+ <Match when={search.status() === 'pending'}>+ {t('documents.pdf-viewer.search.searching')}+ </Match>+ <Match when={search.status() === 'not-found'}>+ {t('documents.pdf-viewer.search.no-matches')}+ </Match>+ <Match when={search.query() && search.matchesCount().total > 0}>+ {t('documents.pdf-viewer.search.matches', search.matchesCount())}+ </Match>+ </Switch>+ </div>++ <div class="flex flex-wrap gap-x-3 gap-y-2 mt-2">+ <Checkbox+ class="flex items-center gap-1.5"+ checked={search.options().caseSensitive}+ onChange={(value) => search.setOption('caseSensitive', value)}+ >+ <CheckboxControl />+ <CheckboxLabel class="text-xs">+ {t('documents.pdf-viewer.search.match-case')}+ </CheckboxLabel>+ </Checkbox>+ <Checkbox+ class="flex items-center gap-1.5"+ checked={search.options().entireWord}+ onChange={(value) => search.setOption('entireWord', value)}+ >+ <CheckboxControl />+ <CheckboxLabel class="text-xs">+ {t('documents.pdf-viewer.search.whole-words')}+ </CheckboxLabel>+ </Checkbox>+ <Checkbox+ class="flex items-center gap-1.5"+ checked={search.options().highlightAll}+ onChange={(value) => search.setOption('highlightAll', value)}+ >+ <CheckboxControl />+ <CheckboxLabel class="text-xs">+ {t('documents.pdf-viewer.search.highlight-all')}+ </CheckboxLabel>+ </Checkbox>+ </div>+ </div>+ );+};diff --git a/apps/papra-client/src/modules/documents/components/pdf-viewer/pdf-search.ts b/apps/papra-client/src/modules/documents/components/pdf-viewer/pdf-search.tsnew file mode 100644index 00000000..2a4ed3da--- /dev/null+++ b/apps/papra-client/src/modules/documents/components/pdf-viewer/pdf-search.ts@@ -0,0 +1,112 @@+import type { PDFSlick } from '@pdfslick/solid';+import { createSignal } from 'solid-js';++type SearchOptions = {+ caseSensitive: boolean;+ entireWord: boolean;+ highlightAll: boolean;+};++type MatchesCount = { current: number; total: number };+type FindControlStateEvent = {+ state: number;+ rawQuery: string | null;+ matchesCount: MatchesCount;+};++// PDF.js FindState values. Keep this adapter independent of PDF.js runtime imports.+const FIND_NOT_FOUND = 1;+const FIND_PENDING = 3;++export function createPdfSearch({+ eventBus,+}: {+ eventBus: Pick<PDFSlick['eventBus'], 'on' | 'off' | 'dispatch'>;+}) {+ const [query, setQuery] = createSignal('');+ const [options, setOptions] = createSignal<SearchOptions>({+ caseSensitive: false,+ entireWord: false,+ highlightAll: true,+ });+ const [status, setStatus] = createSignal<'idle' | 'pending' | 'found' | 'not-found'>('idle');+ const [matchesCount, setMatchesCount] = createSignal<MatchesCount>({ current: 0, total: 0 });++ const find = (type = '', findPrevious = false) => {+ // An empty type lets PDFFindController debounce typing; "again" navigates immediately.+ eventBus.dispatch('find', {+ source: eventBus,+ type,+ query: query(),+ ...options(),+ findPrevious,+ matchDiacritics: false,+ });+ };++ const updateControlState = (event: FindControlStateEvent) => {+ if (event.rawQuery !== query()) {+ return;+ }++ if (!query()) {+ setStatus('idle');+ } else if (event.state === FIND_PENDING) {+ setStatus('pending');+ } else if (event.state === FIND_NOT_FOUND) {+ setStatus('not-found');+ } else {+ setStatus('found');+ }++ setMatchesCount(event.state === FIND_PENDING ? { current: 0, total: 0 } : event.matchesCount);+ };++ const updateMatchesCount = ({ matchesCount }: { matchesCount: MatchesCount }) => {+ if (status() === 'found') {+ setMatchesCount(matchesCount);+ }+ };++ eventBus.on('updatefindcontrolstate', updateControlState);+ eventBus.on('updatefindmatchescount', updateMatchesCount);++ return {+ query,+ options,+ status,+ matchesCount,+ setQuery: (value: string) => {+ setQuery(value);+ find();+ },+ setOption: (option: keyof SearchOptions, value: boolean) => {+ setOptions((previous) => ({ ...previous, [option]: value }));+ const eventTypes = {+ caseSensitive: 'casesensitivitychange',+ entireWord: 'entirewordchange',+ highlightAll: 'highlightallchange',+ };+ find(eventTypes[option]);+ },+ open: () => {+ if (query()) {+ find('again');+ }+ },+ next: () => {+ find('again');+ },+ previous: () => {+ find('again', true);+ },+ close: () => {+ eventBus.dispatch('findbarclose', { source: eventBus });+ },+ dispose: () => {+ eventBus.off('updatefindcontrolstate', updateControlState);+ eventBus.off('updatefindmatchescount', updateMatchesCount);+ eventBus.dispatch('findbarclose', { source: eventBus });+ },+ };+}diff --git a/apps/papra-client/src/modules/documents/components/pdf-viewer/pdf-viewer.types.ts b/apps/papra-client/src/modules/documents/components/pdf-viewer/pdf-viewer.types.tsindex 258f8eea..b1d67266 100644--- a/apps/papra-client/src/modules/documents/components/pdf-viewer/pdf-viewer.types.ts+++ b/apps/papra-client/src/modules/documents/components/pdf-viewer/pdf-viewer.types.ts@@ -16,4 +16,7 @@ export type ThumbnailsBarProps = PdfViewerStoreProps & {export type ToolbarProps = PdfViewerStoreProps & {isSidebarOpen: Accessor<boolean>;setIsSidebarOpen: Setter<boolean>;+ isSearchOpen: Accessor<boolean>;+ searchId: string;+ onOpenSearch: () => void;};diff --git a/apps/papra-client/src/modules/documents/components/pdf-viewer/toolbar/pdf-viewer-toolbar.component.tsx b/apps/papra-client/src/modules/documents/components/pdf-viewer/toolbar/pdf-viewer-toolbar.component.tsxindex b38d9c4d..844b3784 100644--- a/apps/papra-client/src/modules/documents/components/pdf-viewer/toolbar/pdf-viewer-toolbar.component.tsx+++ b/apps/papra-client/src/modules/documents/components/pdf-viewer/toolbar/pdf-viewer-toolbar.component.tsx@@ -152,6 +152,28 @@ export const PdfViewerToolbar: Component<ToolbarProps> = (props) => {</div><div class="flex items-center">+ <Tooltip>+ <TooltipTrigger+ as={(triggerProps: Record<string, unknown>) => (+ <Button+ {...triggerProps}+ variant="ghost"+ size="icon"+ class="size-8"+ aria-label={t('documents.pdf-viewer.search.label')}+ aria-expanded={props.isSearchOpen()}+ aria-controls={props.searchId}+ aria-keyshortcuts="Control+f Meta+f"+ disabled={!props.store.pdfSlick}+ onClick={props.onOpenSearch}+ >+ <div class="i-tabler-search size-4" />+ </Button>+ )}+ />+ <TooltipContent>{t('documents.pdf-viewer.search.label')}</TooltipContent>+ </Tooltip>+<Tooltip><TooltipTriggeras={(triggerProps: Record<string, unknown>) => (