fix(client/pdf): blob reloaded when saving

This commit is contained in:
Elian Doran 2025-12-29 16:46:30 +02:00
parent 359f398afa
commit bb374a5ce2
No known key found for this signature in database
4 changed files with 26 additions and 20 deletions

View File

@ -85,13 +85,15 @@ async function remove<T>(url: string, componentId?: string) {
return await call<T>("DELETE", url, componentId);
}
async function upload(url: string, fileToUpload: File) {
async function upload(url: string, fileToUpload: File, componentId?: string) {
const formData = new FormData();
formData.append("upload", fileToUpload);
return await $.ajax({
url: window.glob.baseApiUrl + url,
headers: await getHeaders(),
headers: await getHeaders(componentId ? {
"trilium-component-id": componentId
} : undefined),
data: formData,
type: "PUT",
timeout: 60 * 60 * 1000,

View File

@ -10,13 +10,13 @@ import { TypeWidgetProps } from "./type_widget";
const TEXT_MAX_NUM_CHARS = 5000;
export default function FileTypeWidget({ note }: TypeWidgetProps) {
const blob = useNoteBlob(note);
export default function FileTypeWidget({ note, parentComponent }: TypeWidgetProps) {
const blob = useNoteBlob(note, parentComponent?.componentId);
if (blob?.content) {
return <TextPreview content={blob.content} />;
} else if (note.mime === "application/pdf") {
return <PdfPreview note={note} />;
return <PdfPreview note={note} componentId={parentComponent?.componentId} />;
} else if (note.mime.startsWith("video/")) {
return <VideoPreview note={note} />;
} else if (note.mime.startsWith("audio/")) {

View File

@ -12,7 +12,10 @@ const VARIABLE_WHITELIST = new Set([
"main-text-color"
]);
export default function PdfPreview({ note }: { note: FNote }) {
export default function PdfPreview({ note, componentId }: {
note: FNote,
componentId: string | undefined;
}) {
const iframeRef = useRef<HTMLIFrameElement>(null);
const { onLoad } = useStyleInjection(iframeRef);
const historyConfig = useViewModeConfig(note, "pdfHistory");
@ -21,7 +24,7 @@ export default function PdfPreview({ note }: { note: FNote }) {
function handleMessage(event: MessageEvent) {
if (event.data?.type === "pdfjs-viewer-document-modified" && event.data?.data) {
const blob = new Blob([event.data.data], { type: note.mime });
server.upload(`notes/${note.noteId}/file`, new File([blob], note.title, { type: note.mime }));
server.upload(`notes/${note.noteId}/file`, new File([blob], note.title, { type: note.mime }), componentId);
}
if (event.data.type === "pdfjs-viewer-save-view-history" && event.data?.data) {

View File

@ -1,20 +1,21 @@
"use strict";
import protectedSessionService from "../../services/protected_session.js";
import utils from "../../services/utils.js";
import log from "../../services/log.js";
import noteService from "../../services/notes.js";
import tmp from "tmp";
import chokidar from "chokidar";
import type { Request, Response } from "express";
import fs from "fs";
import { Readable } from "stream";
import chokidar from "chokidar";
import ws from "../../services/ws.js";
import tmp from "tmp";
import becca from "../../becca/becca.js";
import ValidationError from "../../errors/validation_error.js";
import type { Request, Response } from "express";
import type BNote from "../../becca/entities/bnote.js";
import type BAttachment from "../../becca/entities/battachment.js";
import type BNote from "../../becca/entities/bnote.js";
import ValidationError from "../../errors/validation_error.js";
import dataDirs from "../../services/data_dir.js";
import log from "../../services/log.js";
import noteService from "../../services/notes.js";
import protectedSessionService from "../../services/protected_session.js";
import utils from "../../services/utils.js";
import ws from "../../services/ws.js";
function updateFile(req: Request) {
const note = becca.getNoteOrThrow(req.params.noteId);
@ -189,8 +190,8 @@ function saveToTmpDir(fileName: string, content: string | Buffer, entityType: st
chokidar.watch(tmpObj.name).on("change", (path, stats) => {
ws.sendMessageToAllClients({
type: "openedFileUpdated",
entityType: entityType,
entityId: entityId,
entityType,
entityId,
lastModifiedMs: stats?.atimeMs,
filePath: tmpObj.name
});