refac
Co-Authored-By: Leandro Ygor Loli <77518998+leandroyloli@users.noreply.github.com>
This commit is contained in:
1 parent
8c485b260f
commit
2a804541e0
2 files changed
+27
-7
No files matched your search
@@ -36,6 +36,14 @@ log = logging.getLogger(__name__)
|
|||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
def _truncate_note_data(data: Optional[dict], max_length: int = 1000) -> Optional[dict]:
|
||||||
|
if not data:
|
||||||
|
return data
|
||||||
|
md = (data.get("content") or {}).get("md") or ""
|
||||||
|
return {"content": {"md": md[:max_length]}}
|
||||||
|
|
||||||
|
|
||||||
############################
|
############################
|
||||||
# GetNotes
|
# GetNotes
|
||||||
############################
|
############################
|
||||||
@@ -82,6 +90,7 @@ async def get_notes(
|
|||||||
NoteUserResponse(
|
NoteUserResponse(
|
||||||
**{
|
**{
|
||||||
**note.model_dump(),
|
**note.model_dump(),
|
||||||
|
"data": _truncate_note_data(note.data),
|
||||||
"user": UserResponse(**users[note.user_id].model_dump()),
|
"user": UserResponse(**users[note.user_id].model_dump()),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@@ -135,7 +144,10 @@ async def search_notes(
|
|||||||
|
|
||||||
filter["user_id"] = user.id
|
filter["user_id"] = user.id
|
||||||
|
|
||||||
return Notes.search_notes(user.id, filter, skip=skip, limit=limit, db=db)
|
result = Notes.search_notes(user.id, filter, skip=skip, limit=limit, db=db)
|
||||||
|
for note in result.items:
|
||||||
|
note.data = _truncate_note_data(note.data)
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
############################
|
############################
|
||||||
|
|||||||
@@ -31,7 +31,7 @@
|
|||||||
|
|
||||||
import { goto } from '$app/navigation';
|
import { goto } from '$app/navigation';
|
||||||
import { WEBUI_NAME, config, prompts as _prompts, user } from '$lib/stores';
|
import { WEBUI_NAME, config, prompts as _prompts, user } from '$lib/stores';
|
||||||
import { createNewNote, deleteNoteById, getNoteList, searchNotes } from '$lib/apis/notes';
|
import { createNewNote, deleteNoteById, getNoteById, getNoteList, searchNotes } from '$lib/apis/notes';
|
||||||
import { capitalizeFirstLetter, copyToClipboard, getTimeRange } from '$lib/utils';
|
import { capitalizeFirstLetter, copyToClipboard, getTimeRange } from '$lib/utils';
|
||||||
import { downloadPdf, createNoteHandler } from './utils';
|
import { downloadPdf, createNoteHandler } from './utils';
|
||||||
|
|
||||||
@@ -73,15 +73,23 @@
|
|||||||
let allItemsLoaded = false;
|
let allItemsLoaded = false;
|
||||||
|
|
||||||
const downloadHandler = async (type) => {
|
const downloadHandler = async (type) => {
|
||||||
|
// Fetch the full note since the list response may not contain full content
|
||||||
|
const note = await getNoteById(localStorage.token, selectedNote.id).catch((error) => {
|
||||||
|
toast.error(`${error}`);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!note) return;
|
||||||
|
|
||||||
if (type === 'txt') {
|
if (type === 'txt') {
|
||||||
const blob = new Blob([selectedNote.data.content.md], { type: 'text/plain' });
|
const blob = new Blob([note.data.content.md], { type: 'text/plain' });
|
||||||
saveAs(blob, `${selectedNote.title}.txt`);
|
saveAs(blob, `${note.title}.txt`);
|
||||||
} else if (type === 'md') {
|
} else if (type === 'md') {
|
||||||
const blob = new Blob([selectedNote.data.content.md], { type: 'text/markdown' });
|
const blob = new Blob([note.data.content.md], { type: 'text/markdown' });
|
||||||
saveAs(blob, `${selectedNote.title}.md`);
|
saveAs(blob, `${note.title}.md`);
|
||||||
} else if (type === 'pdf') {
|
} else if (type === 'pdf') {
|
||||||
try {
|
try {
|
||||||
await downloadPdf(selectedNote);
|
await downloadPdf(note);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
toast.error(`${error}`);
|
toast.error(`${error}`);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user