feat: move database access to rust

This commit is contained in:
PandaDEV 2024-12-11 14:29:05 +10:00
parent 4b3b6eaf21
commit a94496dbdb
No known key found for this signature in database
GPG key ID: 13EFF9BAF70EE75C
5 changed files with 687 additions and 317 deletions

View file

@ -1,8 +1,21 @@
<template> <template>
<div class="bg" @keydown.down.prevent="selectNext" @keydown.up.prevent="selectPrevious" <div
@keydown.enter.prevent="pasteSelectedItem" @keydown.esc="hideApp" tabindex="0"> class="bg"
<input ref="searchInput" v-model="searchQuery" @input="searchHistory" autocorrect="off" autocapitalize="off" @keydown.down.prevent="selectNext"
spellcheck="false" class="search" type="text" placeholder="Type to filter entries..." /> @keydown.up.prevent="selectPrevious"
@keydown.enter.prevent="pasteSelectedItem"
@keydown.esc="hideApp"
tabindex="0">
<input
ref="searchInput"
v-model="searchQuery"
@input="searchHistory"
autocorrect="off"
autocapitalize="off"
spellcheck="false"
class="search"
type="text"
placeholder="Type to filter entries..." />
<div class="bottom-bar"> <div class="bottom-bar">
<div class="left"> <div class="left">
<img class="logo" width="18px" src="../public/logo.png" alt="" /> <img class="logo" width="18px" src="../public/logo.png" alt="" />
@ -17,36 +30,68 @@
<div class="actions"> <div class="actions">
<p>Actions</p> <p>Actions</p>
<div> <div>
<img v-if="os === 'windows' || os === 'linux'" src="../public/ctrl.svg" alt="" /> <img
v-if="os === 'windows' || os === 'linux'"
src="../public/ctrl.svg"
alt="" />
<img v-if="os === 'macos'" src="../public/cmd.svg" alt="" /> <img v-if="os === 'macos'" src="../public/cmd.svg" alt="" />
<img src="../public/k.svg" alt="" /> <img src="../public/k.svg" alt="" />
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<OverlayScrollbarsComponent class="results" ref="resultsContainer" <OverlayScrollbarsComponent
class="results"
ref="resultsContainer"
:options="{ scrollbars: { autoHide: 'scroll' } }"> :options="{ scrollbars: { autoHide: 'scroll' } }">
<template v-for="(group, groupIndex) in groupedHistory" :key="groupIndex"> <template v-for="(group, groupIndex) in groupedHistory" :key="groupIndex">
<div class="time-separator">{{ group.label }}</div> <div class="time-separator">{{ group.label }}</div>
<div v-for="(item, index) in group.items" :key="item.id" :class="[ <div
'result clothoid-corner', v-for="(item, index) in group.items"
{ selected: isSelected(groupIndex, index) }, :key="item.id"
]" @click="selectItem(groupIndex, index)" :ref="(el) => { :class="[
if (isSelected(groupIndex, index)) 'result clothoid-corner',
selectedElement = el as HTMLElement; { selected: isSelected(groupIndex, index) },
} ]"
@click="selectItem(groupIndex, index)"
:ref="
(el) => {
if (isSelected(groupIndex, index))
selectedElement = el as HTMLElement;
}
"> ">
<template v-if="item.content_type === 'image'"> <template v-if="item.content_type === 'image'">
<img v-if="!imageLoading && !imageLoadError" :src="getComputedImageUrl(item)" alt="Image" class="image" <img
v-if="imageUrls[item.id]"
:src="imageUrls[item.id]"
alt="Image"
class="image"
@error="onImageError" /> @error="onImageError" />
<img v-if="imageLoading || imageLoadError" src="../public/icons/Image.svg" class="icon" /> <img
v-else
src="../public/icons/Image.svg"
class="icon" />
</template> </template>
<img v-else-if="hasFavicon(item.favicon ?? '')" :src="getFaviconFromDb(item.favicon ?? '')" alt="Favicon" <img
v-else-if="hasFavicon(item.favicon ?? '')"
:src="getFaviconFromDb(item.favicon ?? '')"
alt="Favicon"
class="favicon" /> class="favicon" />
<img src="../public/icons/File.svg" class="icon" v-else-if="item.content_type === 'files'" /> <img
<img src="../public/icons/Text.svg" class="icon" v-else-if="item.content_type === 'text'" /> src="../public/icons/File.svg"
<img src="../public/icons/Code.svg" class="icon" v-else-if="item.content_type === 'code'" /> class="icon"
<span v-if="item.content_type === 'image'">Image ({{ item.dimensions || "Loading..." }})</span> v-else-if="item.content_type === ContentType.File" />
<img
src="../public/icons/Text.svg"
class="icon"
v-else-if="item.content_type === ContentType.Text" />
<img
src="../public/icons/Code.svg"
class="icon"
v-else-if="item.content_type === ContentType.Code" />
<span v-if="item.content_type === ContentType.Image">
Image ({{ imageDimensions[item.id] || 'Loading...' }})
</span>
<span v-else>{{ truncateContent(item.content) }}</span> <span v-else>{{ truncateContent(item.content) }}</span>
</div> </div>
</template> </template>
@ -55,8 +100,11 @@
<img :src="getComputedImageUrl(selectedItem)" alt="Image" class="image" /> <img :src="getComputedImageUrl(selectedItem)" alt="Image" class="image" />
</div> </div>
<OverlayScrollbarsComponent v-else class="content"> <OverlayScrollbarsComponent v-else class="content">
<img v-if="selectedItem?.content && isYoutubeWatchUrl(selectedItem.content)" <img
:src="getYoutubeThumbnail(selectedItem.content)" alt="YouTube Thumbnail" class="full-image" /> v-if="selectedItem?.content && isYoutubeWatchUrl(selectedItem.content)"
:src="getYoutubeThumbnail(selectedItem.content)"
alt="YouTube Thumbnail"
class="full-image" />
<span v-else>{{ selectedItem?.content || "" }}</span> <span v-else>{{ selectedItem?.content || "" }}</span>
</OverlayScrollbarsComponent> </OverlayScrollbarsComponent>
<Noise /> <Noise />
@ -65,108 +113,174 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed, onMounted, watch, nextTick, shallowRef } from "vue"; import { ref, computed, onMounted, watch, nextTick, shallowRef } from "vue";
import Database from "@tauri-apps/plugin-sql";
import { OverlayScrollbarsComponent } from "overlayscrollbars-vue"; import { OverlayScrollbarsComponent } from "overlayscrollbars-vue";
import "overlayscrollbars/overlayscrollbars.css"; import "overlayscrollbars/overlayscrollbars.css";
import { app, window } from "@tauri-apps/api"; import { app, window } from "@tauri-apps/api";
import { platform } from "@tauri-apps/plugin-os"; import { platform } from "@tauri-apps/plugin-os";
import { invoke } from "@tauri-apps/api/core";
import { enable, isEnabled } from "@tauri-apps/plugin-autostart"; import { enable, isEnabled } from "@tauri-apps/plugin-autostart";
import { listen } from "@tauri-apps/api/event"; import { listen } from "@tauri-apps/api/event";
import { readFile } from "@tauri-apps/plugin-fs"; import { useNuxtApp } from "#app";
import { HistoryItem, ContentType } from "~/types/types";
interface HistoryItem {
id: number;
content: string;
content_type: string;
timestamp: string;
favicon?: string;
dimensions?: string;
}
interface GroupedHistory { interface GroupedHistory {
label: string; label: string;
items: HistoryItem[]; items: HistoryItem[];
} }
const db: Ref<Database | null> = ref(null); const { $history, $settings } = useNuxtApp();
const history: Ref<HistoryItem[]> = ref([]); const CHUNK_SIZE = 50;
const chunkSize: number = 50; const SCROLL_THRESHOLD = 100;
let offset: number = 0; const IMAGE_LOAD_DEBOUNCE = 300;
let isLoading: boolean = false;
const resultsContainer: Ref<InstanceType<
typeof OverlayScrollbarsComponent
> | null> = ref(null);
const searchQuery: Ref<string> = ref("");
const selectedGroupIndex: Ref<number> = ref(0);
const selectedItemIndex: Ref<number> = ref(0);
const selectedElement: Ref<HTMLElement | null> = ref(null);
const searchInput: Ref<HTMLInputElement | null> = ref(null);
const os: Ref<string> = ref("");
const imageLoadError = ref(false);
const imageLoading = ref(true);
const imageUrls: Ref<Record<number, string>> = shallowRef({});
const groupedHistory: ComputedRef<GroupedHistory[]> = computed(() => { const history = shallowRef<HistoryItem[]>([]);
let offset = 0;
let isLoading = false;
const resultsContainer = shallowRef<InstanceType<typeof OverlayScrollbarsComponent> | null>(null);
const searchQuery = ref("");
const selectedGroupIndex = ref(0);
const selectedItemIndex = ref(0);
const selectedElement = shallowRef<HTMLElement | null>(null);
const searchInput = ref<HTMLInputElement | null>(null);
const os = ref<string>("");
const imageUrls = shallowRef<Record<string, string>>({});
const imageDimensions = shallowRef<Record<string, string>>({});
const lastUpdateTime = ref<number>(Date.now());
const imageLoadError = ref<boolean>(false);
const imageLoading = ref<boolean>(false);
const isSameDay = (date1: Date, date2: Date): boolean => {
return date1.getFullYear() === date2.getFullYear()
&& date1.getMonth() === date2.getMonth()
&& date1.getDate() === date2.getDate();
};
const getWeekNumber = (date: Date): number => {
const firstDayOfYear = new Date(date.getFullYear(), 0, 1);
return Math.ceil(((date.getTime() - firstDayOfYear.getTime()) / 86400000 + firstDayOfYear.getDay() + 1) / 7);
};
const groupedHistory = computed<GroupedHistory[]>(() => {
const now = new Date(); const now = new Date();
const today = new Date(now.getFullYear(), now.getMonth(), now.getDate()); const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
const getWeekNumber = (d: Date): number => {
d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay() || 7));
const yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil(((Number(d) - Number(yearStart)) / 86400000 + 1) / 7);
};
const thisWeek = getWeekNumber(now); const thisWeek = getWeekNumber(now);
const thisYear = now.getFullYear(); const thisYear = now.getFullYear();
const groups: GroupedHistory[] = [ const groups: Record<string, HistoryItem[]> = {
{ label: "Today", items: [] }, Today: [],
{ label: "Yesterday", items: [] }, Yesterday: [],
{ label: "This Week", items: [] }, 'This Week': [],
{ label: "Last Week", items: [] }, 'Last Week': [],
{ label: "This Year", items: [] }, 'This Year': [],
{ label: "Last Year", items: [] }, 'Last Year': []
]; };
const filteredItems = searchQuery.value const filteredItems = searchQuery.value
? history.value.filter((item) => ? history.value.filter(item =>
item.content.toLowerCase().includes(searchQuery.value.toLowerCase()) item.content.toLowerCase().includes(searchQuery.value.toLowerCase()))
)
: history.value; : history.value;
filteredItems.forEach((item) => { const yesterday = new Date(today.getTime() - 86400000);
filteredItems.forEach(item => {
const itemDate = new Date(item.timestamp); const itemDate = new Date(item.timestamp);
const itemWeek = getWeekNumber(itemDate); const itemWeek = getWeekNumber(itemDate);
const itemYear = itemDate.getFullYear(); const itemYear = itemDate.getFullYear();
if (itemDate.toDateString() === today.toDateString()) { if (isSameDay(itemDate, today)) groups.Today.push(item);
groups[0].items.push(item); else if (isSameDay(itemDate, yesterday)) groups.Yesterday.push(item);
} else if ( else if (itemYear === thisYear && itemWeek === thisWeek) groups['This Week'].push(item);
itemDate.toDateString() === else if (itemYear === thisYear && itemWeek === thisWeek - 1) groups['Last Week'].push(item);
new Date(today.getTime() - 86400000).toDateString() else if (itemYear === thisYear) groups['This Year'].push(item);
) { else groups['Last Year'].push(item);
groups[1].items.push(item);
} else if (itemYear === thisYear && itemWeek === thisWeek) {
groups[2].items.push(item);
} else if (itemYear === thisYear && itemWeek === thisWeek - 1) {
groups[3].items.push(item);
} else if (itemYear === thisYear) {
groups[4].items.push(item);
} else {
groups[5].items.push(item);
}
}); });
return groups.filter((group) => group.items.length > 0); return Object.entries(groups)
.filter(([_, items]) => items.length > 0)
.map(([label, items]) => ({ label, items }));
}); });
const selectedItem: ComputedRef<HistoryItem | null> = computed(() => { const selectedItem = computed<HistoryItem | null>(() => {
const group = groupedHistory.value[selectedGroupIndex.value]; const group = groupedHistory.value[selectedGroupIndex.value];
return group ? group.items[selectedItemIndex.value] : null; return group?.items[selectedItemIndex.value] ?? null;
}); });
const loadHistoryChunk = async (): Promise<void> => {
if (isLoading) return;
isLoading = true;
try {
const results = await $history.loadHistoryChunk(offset, CHUNK_SIZE);
if (!results.length) {
isLoading = false;
return;
}
const processedItems = await Promise.all(
results.map(async item => {
const historyItem = new HistoryItem(
item.content_type as ContentType,
item.content,
item.favicon
);
Object.assign(historyItem, {
id: item.id,
timestamp: new Date(item.timestamp)
});
if (historyItem.content_type === ContentType.Image) {
await Promise.all([
getItemDimensions(historyItem),
loadImageUrl(historyItem)
]);
}
return historyItem;
})
);
history.value = [...history.value, ...processedItems];
offset += CHUNK_SIZE;
} catch (error) {
console.error("Failed to load history:", error);
} finally {
isLoading = false;
}
};
const handleScroll = (): void => {
const viewport = resultsContainer.value?.osInstance()?.elements().viewport;
if (!viewport) return;
const { scrollTop, scrollHeight, clientHeight } = viewport;
if (scrollHeight - scrollTop - clientHeight < SCROLL_THRESHOLD) {
loadHistoryChunk();
}
};
const scrollToSelectedItem = (forceScrollTop: boolean = false): void => {
nextTick(() => {
const osInstance = resultsContainer.value?.osInstance();
const viewport = osInstance?.elements().viewport;
if (!selectedElement.value || !viewport) return;
if (!forceScrollTop) {
const viewportRect = viewport.getBoundingClientRect();
const elementRect = selectedElement.value.getBoundingClientRect();
const isAbove = elementRect.top < viewportRect.top;
const isBelow = elementRect.bottom > viewportRect.bottom - 8;
if (isAbove || isBelow) {
const scrollOffset = isAbove
? elementRect.top - viewportRect.top - 8
: elementRect.bottom - viewportRect.bottom + 9;
viewport.scrollBy({ top: scrollOffset, behavior: "smooth" });
}
}
});
};
const isSelected = (groupIndex: number, itemIndex: number): boolean => { const isSelected = (groupIndex: number, itemIndex: number): boolean => {
return ( return (
selectedGroupIndex.value === groupIndex && selectedGroupIndex.value === groupIndex &&
@ -175,26 +289,11 @@ const isSelected = (groupIndex: number, itemIndex: number): boolean => {
}; };
const searchHistory = async (): Promise<void> => { const searchHistory = async (): Promise<void> => {
if (!db.value) return; const results = await $history.searchHistory(searchQuery.value);
history.value = results.map(item => Object.assign(
history.value = []; new HistoryItem(item.content_type as ContentType, item.content, item.favicon),
offset = 0; { id: item.id, timestamp: new Date(item.timestamp) }
));
const query = `%${searchQuery.value}%`;
const results = await db.value.select<HistoryItem[]>(
"SELECT * FROM history WHERE content LIKE ? ORDER BY timestamp DESC LIMIT ?",
[query, chunkSize]
);
history.value = await Promise.all(
results.map(async (item) => {
if (item.content_type === "image") {
const dimensions = await getImageDimensions(item.content);
return { ...item, dimensions };
}
return item;
})
);
}; };
const selectNext = (): void => { const selectNext = (): void => {
@ -229,20 +328,17 @@ const pasteSelectedItem = async (): Promise<void> => {
if (!selectedItem.value) return; if (!selectedItem.value) return;
let content = selectedItem.value.content; let content = selectedItem.value.content;
let contentType: String = selectedItem.value.content_type; let contentType: string = selectedItem.value.content_type;
if (contentType === "image") { if (contentType === "image") {
try { try {
content = readFile(content).toString(); content = await $history.getImagePath(content);
} catch (error) { } catch (error) {
console.error("Error reading image file:", error); console.error("Error reading image file:", error);
return; return;
} }
} }
await hideApp(); await hideApp();
await invoke("write_and_paste", { await $history.writeAndPaste({ content, contentType });
content,
contentType,
});
}; };
const truncateContent = (content: string): string => { const truncateContent = (content: string): string => {
@ -273,134 +369,130 @@ const getYoutubeThumbnail = (url: string): string => {
} else { } else {
videoId = url.match(/[?&]v=([^&]+)/)?.[1]; videoId = url.match(/[?&]v=([^&]+)/)?.[1];
} }
return `https://img.youtube.com/vi/${videoId}/0.jpg`; return videoId
? `https://img.youtube.com/vi/${videoId}/0.jpg`
: "https://via.placeholder.com/150";
}; };
const getFaviconFromDb = (favicon: string): string => { const getFaviconFromDb = (favicon: string): string => {
return `data:image/png;base64,${favicon}`; return `data:image/png;base64,${favicon}`;
}; };
const getImageDimensions = (path: string): Promise<string> => { const getImageData = async (item: HistoryItem): Promise<{ url: string; dimensions: string }> => {
return new Promise(async (resolve) => {
const img = new Image();
img.onload = () => {
imageLoadError.value = false;
imageLoading.value = false;
resolve(`${img.width}x${img.height}`);
};
img.onerror = (e) => {
console.error("Error loading image:", e);
imageLoadError.value = true;
imageLoading.value = false;
resolve("0x0");
};
try {
imageLoading.value = true;
const dataUrl = await getImageUrl(path);
img.src = dataUrl;
} catch (error) {
console.error("Error getting image URL:", error);
imageLoadError.value = true;
imageLoading.value = false;
resolve("0x0");
}
});
};
const getImageUrl = async (path: string): Promise<string> => {
const isWindows = path.includes("\\");
const separator = isWindows ? "\\" : "/";
const filename = path.split(separator).pop();
try { try {
imageLoading.value = true; const base64 = await $history.readImage({ filename: item.content });
const base64 = await invoke<string>("read_image", { filename });
if (!base64 || base64.length === 0) {
throw new Error("Received empty image data");
}
const dataUrl = `data:image/png;base64,${base64}`; const dataUrl = `data:image/png;base64,${base64}`;
const img = new Image();
img.src = dataUrl;
imageLoadError.value = false; await new Promise<void>((resolve, reject) => {
imageLoading.value = false; img.onload = () => resolve();
return dataUrl; img.onerror = reject;
});
return {
url: dataUrl,
dimensions: `${img.width}x${img.height}`
};
} catch (error) { } catch (error) {
console.error("Error reading image file:", error); console.error("Error processing image:", error);
imageLoadError.value = true; return { url: "", dimensions: "Error" };
imageLoading.value = false;
return "";
} }
}; };
const getComputedImageUrl = (item: HistoryItem): string => { const processHistoryItem = async (item: any): Promise<HistoryItem> => {
if (!imageUrls.value[item.id]) { const historyItem = new HistoryItem(
imageUrls.value[item.id] = ""; item.content_type as ContentType,
getImageUrl(item.content) item.content,
.then((url) => { item.favicon
imageUrls.value = { ...imageUrls.value, [item.id]: url };
})
.catch((error) => {
console.error("Failed to get image URL:", error);
imageUrls.value = { ...imageUrls.value, [item.id]: "" };
});
}
return imageUrls.value[item.id] || "";
};
const loadHistoryChunk = async (): Promise<void> => {
if (!db.value || isLoading) return;
isLoading = true;
let results: HistoryItem[];
if (searchQuery.value) {
const query = `%${searchQuery.value}%`;
results = await db.value.select(
"SELECT * FROM history WHERE content LIKE ? ORDER BY timestamp DESC LIMIT ? OFFSET ?",
[query, chunkSize, offset]
);
} else {
results = await db.value.select(
"SELECT * FROM history ORDER BY timestamp DESC LIMIT ? OFFSET ?",
[chunkSize, offset]
);
}
if (results.length === 0) {
isLoading = false;
return;
}
const processedChunk = await Promise.all(
results.map(async (item) => {
if (item.content_type === "image") {
const dimensions = await getImageDimensions(item.content);
getComputedImageUrl(item);
return { ...item, dimensions };
}
return item;
})
); );
history.value = [...history.value, ...processedChunk]; Object.assign(historyItem, {
offset += chunkSize; id: item.id,
isLoading = false; timestamp: new Date(item.timestamp)
});
if (historyItem.content_type === ContentType.Image) {
const { url, dimensions } = await getImageData(historyItem);
imageUrls.value[historyItem.id] = url;
imageDimensions.value[historyItem.id] = dimensions;
}
return historyItem;
}; };
const handleScroll = (): void => { const updateHistory = async (resetScroll: boolean = false): Promise<void> => {
if (!resultsContainer.value) return; history.value = [];
offset = 0;
await loadHistoryChunk();
const viewport = resultsContainer.value?.osInstance()?.elements().viewport; if (resetScroll && resultsContainer.value?.osInstance()?.elements().viewport) {
const scrollTop = viewport?.scrollTop ?? 0; resultsContainer.value.osInstance()?.elements().viewport?.scrollTo({
const scrollHeight = viewport?.scrollHeight ?? 0; top: 0,
const clientHeight = viewport?.clientHeight ?? 0; behavior: "smooth"
});
if (scrollHeight - scrollTop - clientHeight < 100) {
loadHistoryChunk();
} }
}; };
const handleSelection = (groupIndex: number, itemIndex: number, shouldScroll: boolean = true): void => {
selectedGroupIndex.value = groupIndex;
selectedItemIndex.value = itemIndex;
if (shouldScroll) scrollToSelectedItem();
};
const handleMediaContent = async (content: string, type: string): Promise<string> => {
if (type === "image") {
return await $history.getImagePath(content);
}
if (isYoutubeWatchUrl(content)) {
const videoId = content.includes("youtu.be")
? content.split("youtu.be/")[1]
: content.match(/[?&]v=([^&]+)/)?.[1];
return videoId ? `https://img.youtube.com/vi/${videoId}/0.jpg` : "";
}
return content;
};
const setupEventListeners = async (): Promise<void> => {
await listen("clipboard-content-updated", async () => {
lastUpdateTime.value = Date.now();
handleSelection(0, 0, false);
await updateHistory(true);
});
await listen("tauri://focus", async () => {
const currentTime = Date.now();
if (currentTime - lastUpdateTime.value > 0) {
const previousState = {
groupIndex: selectedGroupIndex.value,
itemIndex: selectedItemIndex.value,
scroll: resultsContainer.value?.osInstance()?.elements().viewport?.scrollTop || 0
};
await updateHistory();
lastUpdateTime.value = currentTime;
handleSelection(previousState.groupIndex, previousState.itemIndex, false);
nextTick(() => {
const viewport = resultsContainer.value?.osInstance()?.elements().viewport;
if (viewport) {
viewport.scrollTo({
top: previousState.scroll,
behavior: "instant"
});
}
});
}
focusSearchInput();
});
await listen("tauri://blur", () => {
searchInput.value?.blur();
});
};
const hideApp = async (): Promise<void> => { const hideApp = async (): Promise<void> => {
await app.hide(); await app.hide();
await window.getCurrentWindow().hide(); await window.getCurrentWindow().hide();
@ -412,36 +504,6 @@ const focusSearchInput = (): void => {
}); });
}; };
const scrollToSelectedItem = (forceScrollTop: boolean = false): void => {
nextTick(() => {
if (selectedElement.value && resultsContainer.value) {
const osInstance = resultsContainer.value.osInstance();
const viewport = osInstance?.elements().viewport;
if (!viewport) return;
if (!forceScrollTop) {
const viewportRect = viewport.getBoundingClientRect();
const elementRect = selectedElement.value.getBoundingClientRect();
const isAbove = elementRect.top < viewportRect.top;
const isBelow = elementRect.bottom > viewportRect.bottom - 8;
if (isAbove || isBelow) {
let scrollOffset;
if (isAbove) {
scrollOffset = elementRect.top - viewportRect.top - 8;
} else {
scrollOffset = elementRect.bottom - viewportRect.bottom + 9;
}
viewport.scrollBy({
top: scrollOffset,
behavior: "smooth",
});
}
}
}
});
};
const onImageError = (): void => { const onImageError = (): void => {
imageLoadError.value = true; imageLoadError.value = true;
imageLoading.value = false; imageLoading.value = false;
@ -455,77 +517,64 @@ watch(searchQuery, () => {
searchHistory(); searchHistory();
}); });
const lastUpdateTime = ref<number>(Date.now());
onMounted(async () => { onMounted(async () => {
db.value = await Database.load("sqlite:data.db"); try {
await loadHistoryChunk(); os.value = await platform();
resultsContainer.value
?.osInstance()
?.elements()
?.viewport?.addEventListener("scroll", handleScroll);
await listen("clipboard-content-updated", async () => {
lastUpdateTime.value = Date.now();
selectedGroupIndex.value = 0;
selectedItemIndex.value = 0;
history.value = [];
offset = 0;
await loadHistoryChunk(); await loadHistoryChunk();
if (resultsContainer.value?.osInstance()?.elements().viewport) {
resultsContainer.value.osInstance()?.elements().viewport?.scrollTo({ resultsContainer.value
top: 0, ?.osInstance()
behavior: "smooth", ?.elements()
}); ?.viewport?.addEventListener("scroll", handleScroll);
await setupEventListeners();
if (!(await isEnabled())) {
await enable();
} }
}); } catch (error) {
console.error("Error during onMounted:", error);
await listen("tauri://focus", async () => {
const currentTime = Date.now();
if (currentTime - lastUpdateTime.value > 0) {
const previousGroupIndex = selectedGroupIndex.value;
const previousItemIndex = selectedItemIndex.value;
const previousScroll =
resultsContainer.value?.osInstance()?.elements().viewport?.scrollTop ||
0;
history.value = [];
offset = 0;
await loadHistoryChunk();
lastUpdateTime.value = currentTime;
selectedGroupIndex.value = previousGroupIndex;
selectedItemIndex.value = previousItemIndex;
nextTick(() => {
if (resultsContainer.value?.osInstance()?.elements().viewport) {
resultsContainer.value.osInstance()?.elements().viewport?.scrollTo({
top: previousScroll,
behavior: "instant",
});
}
});
}
focusSearchInput();
});
await listen("tauri://blur", () => {
if (searchInput.value) {
searchInput.value.blur();
}
});
if (!(await isEnabled())) {
await enable();
} }
os.value = platform();
}); });
watch([selectedGroupIndex, selectedItemIndex], () => watch([selectedGroupIndex, selectedItemIndex], () =>
scrollToSelectedItem(false) scrollToSelectedItem(false)
); );
const getItemDimensions = async (item: HistoryItem) => {
if (!imageDimensions.value[item.id]) {
try {
const base64 = await $history.readImage({ filename: item.content });
const img = new Image();
img.src = `data:image/png;base64,${base64}`;
await new Promise<void>((resolve, reject) => {
img.onload = () => resolve();
img.onerror = () => reject();
});
imageDimensions.value[item.id] = `${img.width}x${img.height}`;
} catch (error) {
console.error("Error loading image dimensions:", error);
imageDimensions.value[item.id] = "Error";
}
}
return imageDimensions.value[item.id] || 'Loading...';
};
const loadImageUrl = async (item: HistoryItem) => {
if (!imageUrls.value[item.id]) {
try {
const base64 = await $history.readImage({ filename: item.content });
imageUrls.value[item.id] = `data:image/png;base64,${base64}`;
} catch (error) {
console.error("Error loading image:", error);
}
}
};
const getComputedImageUrl = (item: HistoryItem | null): string => {
if (!item) return '';
return imageUrls.value[item.id] || '';
};
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">

47
plugins/history.ts Normal file
View file

@ -0,0 +1,47 @@
import { invoke } from "@tauri-apps/api/core";
import type { HistoryItem } from "~/types/types";
export default defineNuxtPlugin(() => {
return {
provide: {
history: {
async getHistory(): Promise<HistoryItem[]> {
return await invoke<HistoryItem[]>("get_history");
},
async addHistoryItem(item: HistoryItem): Promise<void> {
await invoke<void>("add_history_item", { item });
},
async searchHistory(query: string): Promise<HistoryItem[]> {
return await invoke<HistoryItem[]>("search_history", { query });
},
async loadHistoryChunk(
offset: number,
limit: number
): Promise<HistoryItem[]> {
return await invoke<HistoryItem[]>("load_history_chunk", {
offset,
limit,
});
},
async getImagePath(path: string): Promise<string> {
return await invoke<string>("get_image_path", { path });
},
async writeAndPaste(data: {
content: string;
contentType: string;
}): Promise<void> {
await invoke<void>("write_and_paste", data);
},
async readImage(data: { filename: string }): Promise<string> {
return await invoke<string>("read_image", data);
},
},
},
};
});

26
plugins/settings.ts Normal file
View file

@ -0,0 +1,26 @@
import { invoke } from "@tauri-apps/api/core";
import type { Settings } from "~/types/types";
export default defineNuxtPlugin(() => {
return {
provide: {
settings: {
async getSetting(key: string): Promise<string> {
return await invoke<string>("get_setting", { key });
},
async saveSetting(key: string, value: string): Promise<void> {
await invoke<void>("save_setting", { key, value });
},
async getKeybind(): Promise<string[]> {
return await invoke<string[]>("get_keybind");
},
async saveKeybind(keybind: string[]): Promise<void> {
await invoke<void>("save_keybind", { keybind });
},
},
},
};
});

148
src-tauri/src/db/history.rs Normal file
View file

@ -0,0 +1,148 @@
use sqlx::{Row, SqlitePool};
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use crate::utils::types::{HistoryItem, ContentType};
use std::fs;
use base64::{Engine, engine::general_purpose::STANDARD};
pub async fn initialize_history(pool: &SqlitePool) -> Result<(), Box<dyn std::error::Error>> {
let id: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(16)
.map(char::from)
.collect();
sqlx::query(
"INSERT INTO history (id, content_type, content, timestamp) VALUES (?, ?, ?, CURRENT_TIMESTAMP)"
)
.bind(id)
.bind("text")
.bind("Welcome to your clipboard history!")
.execute(pool)
.await?;
Ok(())
}
#[tauri::command]
pub async fn get_history(pool: tauri::State<'_, SqlitePool>) -> Result<Vec<HistoryItem>, String> {
let rows = sqlx::query(
"SELECT id, content_type, content, favicon, timestamp FROM history ORDER BY timestamp DESC"
)
.fetch_all(&*pool)
.await
.map_err(|e| e.to_string())?;
let items = rows.iter().map(|row| HistoryItem {
id: row.get("id"),
content_type: ContentType::from(row.get::<String, _>("content_type")),
content: row.get("content"),
favicon: row.get("favicon"),
timestamp: row.get("timestamp"),
}).collect();
Ok(items)
}
#[tauri::command]
pub async fn add_history_item(
pool: tauri::State<'_, SqlitePool>,
item: HistoryItem,
) -> Result<(), String> {
let (id, content_type, content, favicon, timestamp) = item.to_row();
sqlx::query(
"INSERT INTO history (id, content_type, content, favicon, timestamp) VALUES (?, ?, ?, ?, ?)"
)
.bind(id)
.bind(content_type)
.bind(content)
.bind(favicon)
.bind(timestamp)
.execute(&*pool)
.await
.map_err(|e| e.to_string())?;
Ok(())
}
#[tauri::command]
pub async fn search_history(
pool: tauri::State<'_, SqlitePool>,
query: String
) -> Result<Vec<HistoryItem>, String> {
let query = format!("%{}%", query);
let rows = sqlx::query(
"SELECT id, content_type, content, favicon, timestamp FROM history WHERE content LIKE ? ORDER BY timestamp DESC"
)
.bind(query)
.fetch_all(&*pool)
.await
.map_err(|e| e.to_string())?;
let items = rows.iter().map(|row| HistoryItem {
id: row.get("id"),
content_type: ContentType::from(row.get::<String, _>("content_type")),
content: row.get("content"),
favicon: row.get("favicon"),
timestamp: row.get("timestamp"),
}).collect();
Ok(items)
}
#[tauri::command]
pub async fn load_history_chunk(
pool: tauri::State<'_, SqlitePool>,
offset: i64,
limit: i64
) -> Result<Vec<HistoryItem>, String> {
let rows = sqlx::query(
"SELECT id, content_type, content, favicon, timestamp FROM history ORDER BY timestamp DESC LIMIT ? OFFSET ?"
)
.bind(limit)
.bind(offset)
.fetch_all(&*pool)
.await
.map_err(|e| e.to_string())?;
let items = rows.iter().map(|row| HistoryItem {
id: row.get("id"),
content_type: ContentType::from(row.get::<String, _>("content_type")),
content: row.get("content"),
favicon: row.get("favicon"),
timestamp: row.get("timestamp"),
}).collect();
Ok(items)
}
#[tauri::command]
pub async fn delete_history_item(
pool: tauri::State<'_, SqlitePool>,
id: String
) -> Result<(), String> {
sqlx::query("DELETE FROM history WHERE id = ?")
.bind(id)
.execute(&*pool)
.await
.map_err(|e| e.to_string())?;
Ok(())
}
#[tauri::command]
pub async fn clear_history(pool: tauri::State<'_, SqlitePool>) -> Result<(), String> {
sqlx::query("DELETE FROM history")
.execute(&*pool)
.await
.map_err(|e| e.to_string())?;
Ok(())
}
#[tauri::command]
pub async fn read_image(filename: String) -> Result<String, String> {
let bytes = fs::read(filename).map_err(|e| e.to_string())?;
Ok(STANDARD.encode(bytes))
}

View file

@ -0,0 +1,100 @@
use serde::{Deserialize, Serialize};
use sqlx::SqlitePool;
use serde_json;
use tauri::{Emitter, Manager};
use sqlx::Row;
#[derive(Deserialize, Serialize)]
struct KeybindSetting {
keybind: Vec<String>,
}
pub async fn initialize_settings(pool: &SqlitePool) -> Result<(), Box<dyn std::error::Error>> {
let default_keybind = KeybindSetting {
keybind: vec!["Meta".to_string(), "V".to_string()],
};
let json = serde_json::to_string(&default_keybind)?;
sqlx::query(
"INSERT INTO settings (key, value) VALUES ('keybind', ?)"
)
.bind(json)
.execute(pool)
.await?;
Ok(())
}
#[tauri::command]
pub async fn save_keybind(
app_handle: tauri::AppHandle,
keybind: Vec<String>,
pool: tauri::State<'_, SqlitePool>,
) -> Result<(), String> {
let json = serde_json::to_string(&keybind).map_err(|e| e.to_string())?;
sqlx::query("INSERT OR REPLACE INTO settings (key, value) VALUES ('keybind', ?)")
.bind(json)
.execute(&*pool)
.await
.map_err(|e| e.to_string())?;
let keybind_str = keybind.join("+");
app_handle
.emit("update-shortcut", keybind_str)
.map_err(|e| e.to_string())?;
Ok(())
}
#[tauri::command]
pub async fn get_setting(
pool: tauri::State<'_, SqlitePool>,
key: String
) -> Result<String, String> {
let row = sqlx::query("SELECT value FROM settings WHERE key = ?")
.bind(key)
.fetch_optional(&*pool)
.await
.map_err(|e| e.to_string())?;
Ok(row.map(|r| r.get("value")).unwrap_or_default())
}
#[tauri::command]
pub async fn save_setting(
pool: tauri::State<'_, SqlitePool>,
key: String,
value: String
) -> Result<(), String> {
sqlx::query("INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)")
.bind(key)
.bind(value)
.execute(&*pool)
.await
.map_err(|e| e.to_string())?;
Ok(())
}
#[tauri::command]
pub async fn get_keybind(
app_handle: tauri::AppHandle,
) -> Result<Vec<String>, String> {
let pool = app_handle.state::<SqlitePool>();
let row = sqlx::query("SELECT value FROM settings WHERE key = 'keybind'")
.fetch_optional(&*pool)
.await
.map_err(|e| e.to_string())?;
let json = row
.map(|r| r.get::<String, _>("value"))
.unwrap_or_else(|| {
serde_json::to_string(&vec!["Meta".to_string(), "V".to_string()])
.expect("Failed to serialize default keybind")
});
serde_json::from_str::<Vec<String>>(&json)
.map_err(|e| e.to_string())
}