|
| 1 | +<script setup lang="ts"> |
| 2 | +import { ref, computed } from 'vue' |
| 3 | +import { Search, X, ArrowUp, ArrowDown, Replace } from 'lucide-vue-next' |
| 4 | +import { Button } from '@/components/ui/button' |
| 5 | +import { Input } from '@/components/ui/input' |
| 6 | +import { Label } from '@/components/ui/label' |
| 7 | +import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' |
| 8 | +import type { NoteMatch } from './find-replace' |
| 9 | +import { searchNotes, replaceInNote } from './find-replace' |
| 10 | +import type { NoteModel } from './note-model' |
| 11 | +
|
| 12 | +const props = defineProps<{ |
| 13 | + open: boolean |
| 14 | + notes: { id: string; model: NoteModel }[] |
| 15 | +}>() |
| 16 | +
|
| 17 | +const emit = defineEmits<{ |
| 18 | + close: [] |
| 19 | +}>() |
| 20 | +
|
| 21 | +const query = ref('') |
| 22 | +const replacement = ref('') |
| 23 | +const matches = ref<NoteMatch[]>([]) |
| 24 | +const currentMatchIndex = ref(0) |
| 25 | +
|
| 26 | +const currentMatch = computed(() => { |
| 27 | + if (matches.value.length === 0 || currentMatchIndex.value >= matches.value.length) return null |
| 28 | + return matches.value[currentMatchIndex.value] |
| 29 | +}) |
| 30 | +
|
| 31 | +const matchCount = computed(() => matches.value.length) |
| 32 | +
|
| 33 | +function performSearch() { |
| 34 | + if (!query.value.trim()) { |
| 35 | + matches.value = [] |
| 36 | + currentMatchIndex.value = 0 |
| 37 | + return |
| 38 | + } |
| 39 | + matches.value = searchNotes(props.notes, query.value) |
| 40 | + currentMatchIndex.value = 0 |
| 41 | +} |
| 42 | +
|
| 43 | +function nextMatch() { |
| 44 | + if (matches.value.length === 0) return |
| 45 | + currentMatchIndex.value = (currentMatchIndex.value + 1) % matches.value.length |
| 46 | +} |
| 47 | +
|
| 48 | +function previousMatch() { |
| 49 | + if (matches.value.length === 0) return |
| 50 | + currentMatchIndex.value = (currentMatchIndex.value - 1 + matches.value.length) % matches.value.length |
| 51 | +} |
| 52 | +
|
| 53 | +function replaceCurrent() { |
| 54 | + const match = currentMatch.value |
| 55 | + if (!match) return |
| 56 | + |
| 57 | + const note = props.notes.find(n => n.id === match.noteId) |
| 58 | + if (!note) return |
| 59 | + |
| 60 | + replaceInNote(note.model, match.field, match.index, match.length, replacement.value) |
| 61 | + |
| 62 | + // Re-search after replacement |
| 63 | + performSearch() |
| 64 | +} |
| 65 | +
|
| 66 | +function replaceAll() { |
| 67 | + for (const match of matches.value) { |
| 68 | + const note = props.notes.find(n => n.id === match.noteId) |
| 69 | + if (note) { |
| 70 | + replaceInNote(note.model, match.field, match.index, match.length, replacement.value) |
| 71 | + } |
| 72 | + } |
| 73 | + matches.value = [] |
| 74 | + currentMatchIndex.value = 0 |
| 75 | +} |
| 76 | +
|
| 77 | +function handleClose() { |
| 78 | + emit('close') |
| 79 | + query.value = '' |
| 80 | + replacement.value = '' |
| 81 | + matches.value = [] |
| 82 | + currentMatchIndex.value = 0 |
| 83 | +} |
| 84 | +</script> |
| 85 | + |
| 86 | +<template> |
| 87 | + <Teleport to="body"> |
| 88 | + <div |
| 89 | + v-if="open" |
| 90 | + class="fixed inset-0 z-50 flex items-center justify-center bg-black/50" |
| 91 | + @click.self="handleClose" |
| 92 | + > |
| 93 | + <Card class="w-full max-w-md"> |
| 94 | + <CardHeader class="flex flex-row items-center justify-between space-y-0 pb-4"> |
| 95 | + <CardTitle>Find and Replace</CardTitle> |
| 96 | + <Button variant="ghost" size="icon" @click="handleClose"> |
| 97 | + <X class="h-4 w-4" /> |
| 98 | + </Button> |
| 99 | + </CardHeader> |
| 100 | + |
| 101 | + <CardContent class="space-y-4"> |
| 102 | + <div class="space-y-2"> |
| 103 | + <Label for="search">Find</Label> |
| 104 | + <div class="flex gap-2"> |
| 105 | + <Input |
| 106 | + id="search" |
| 107 | + v-model="query" |
| 108 | + placeholder="Search in notes..." |
| 109 | + @input="performSearch" |
| 110 | + @keydown.enter.prevent="nextMatch" |
| 111 | + /> |
| 112 | + <Button |
| 113 | + variant="outline" |
| 114 | + size="icon" |
| 115 | + :disabled="matchCount === 0" |
| 116 | + @click="previousMatch" |
| 117 | + title="Previous match (Shift+Enter)" |
| 118 | + > |
| 119 | + <ArrowUp class="h-4 w-4" /> |
| 120 | + </Button> |
| 121 | + <Button |
| 122 | + variant="outline" |
| 123 | + size="icon" |
| 124 | + :disabled="matchCount === 0" |
| 125 | + @click="nextMatch" |
| 126 | + title="Next match (Enter)" |
| 127 | + > |
| 128 | + <ArrowDown class="h-4 w-4" /> |
| 129 | + </Button> |
| 130 | + </div> |
| 131 | + <p v-if="matchCount > 0" class="text-xs text-muted-foreground"> |
| 132 | + {{ currentMatchIndex + 1 }} of {{ matchCount }} matches |
| 133 | + </p> |
| 134 | + <p v-else-if="query" class="text-xs text-muted-foreground"> |
| 135 | + No matches found |
| 136 | + </p> |
| 137 | + </div> |
| 138 | + |
| 139 | + <div class="space-y-2"> |
| 140 | + <Label for="replace">Replace with</Label> |
| 141 | + <div class="flex gap-2"> |
| 142 | + <Input |
| 143 | + id="replace" |
| 144 | + v-model="replacement" |
| 145 | + placeholder="Replacement text..." |
| 146 | + @keydown.enter.prevent="replaceCurrent" |
| 147 | + /> |
| 148 | + <Button |
| 149 | + variant="outline" |
| 150 | + size="icon" |
| 151 | + :disabled="matchCount === 0" |
| 152 | + @click="replaceCurrent" |
| 153 | + title="Replace current match" |
| 154 | + > |
| 155 | + <Replace class="h-4 w-4" /> |
| 156 | + </Button> |
| 157 | + </div> |
| 158 | + </div> |
| 159 | + |
| 160 | + <div class="flex justify-end gap-2"> |
| 161 | + <Button |
| 162 | + variant="outline" |
| 163 | + :disabled="matchCount === 0" |
| 164 | + @click="replaceAll" |
| 165 | + > |
| 166 | + Replace All ({{ matchCount }}) |
| 167 | + </Button> |
| 168 | + </div> |
| 169 | + |
| 170 | + <div v-if="currentMatch" class="rounded-md border bg-muted/50 p-3 text-sm"> |
| 171 | + <p class="font-medium">Current match:</p> |
| 172 | + <p class="text-muted-foreground"> |
| 173 | + Note: <span class="font-mono">{{ currentMatch.noteId }}</span> |
| 174 | + </p> |
| 175 | + <p class="text-muted-foreground"> |
| 176 | + Field: {{ currentMatch.field }} |
| 177 | + </p> |
| 178 | + <p class="text-muted-foreground mt-1"> |
| 179 | + "{{ currentMatch.text.substring(currentMatch.index, currentMatch.index + currentMatch.length) }}" |
| 180 | + </p> |
| 181 | + </div> |
| 182 | + </CardContent> |
| 183 | + </Card> |
| 184 | + </div> |
| 185 | + </Teleport> |
| 186 | +</template> |
0 commit comments