Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(core): error due to CLS on standalone
Elian Doran35317b3daba0
diff --git a/packages/trilium-core/src/services/image.ts b/packages/trilium-core/src/services/image.tsindex 98ff4f6170..e513c44598 100644--- a/packages/trilium-core/src/services/image.ts+++ b/packages/trilium-core/src/services/image.ts@@ -6,6 +6,7 @@import sanitizeFilename from "sanitize-filename";import becca from "../becca/becca.js";+import { getContext } from "./context.js";import { getLog } from "./log.js";import { getImageProvider } from "./image_provider.js";import noteService from "./notes.js";@@ -33,10 +34,12 @@ function updateImage(noteId: string, uploadBuffer: Uint8Array, originalName: str// Process image asynchronouslygetImageProvider().processImage(uploadBuffer, originalName, true).then(({ buffer, format }) => {- getSql().transactional(() => {- note.mime = getImageMimeFromExtension(format.ext);- note.save();- note.setContent(buffer);+ getContext().init(() => {+ getSql().transactional(() => {+ note.mime = getImageMimeFromExtension(format.ext);+ note.save();+ note.setContent(buffer);+ });});});}@@ -73,16 +76,18 @@ function saveImage(// Process image asynchronouslygetImageProvider().processImage(uploadBuffer, originalName, shrinkImageSwitch).then(({ buffer, format }) => {- getSql().transactional(() => {- note.mime = getImageMimeFromExtension(format.ext);-- if (!originalName.includes(".")) {- originalName += `.${format.ext}`;- note.setLabel("originalFileName", originalName);- note.title = sanitizeFilename(originalName);- }-- note.setContent(buffer, { forceSave: true });+ getContext().init(() => {+ getSql().transactional(() => {+ note.mime = getImageMimeFromExtension(format.ext);++ if (!originalName.includes(".")) {+ originalName += `.${format.ext}`;+ note.setLabel("originalFileName", originalName);+ note.title = sanitizeFilename(originalName);+ }++ note.setContent(buffer, { forceSave: true });+ });});});@@ -118,29 +123,33 @@ function saveImageToAttachment(// Schedule post-processing to mark unused attachmentssetTimeout(() => {- getSql().transactional(() => {- const note = becca.getNoteOrThrow(noteId);- noteService.asyncPostProcessContent(note, note.getContent());+ getContext().init(() => {+ getSql().transactional(() => {+ const note = becca.getNoteOrThrow(noteId);+ noteService.asyncPostProcessContent(note, note.getContent());+ });});}, 5000);// Process image asynchronouslyconst attachmentId = attachment.attachmentId;getImageProvider().processImage(uploadBuffer, originalName, !!shrinkImageSwitch).then(({ buffer, format }) => {- getSql().transactional(() => {- if (!attachmentId) {- throw new Error("Missing attachment ID.");- }- attachment = becca.getAttachmentOrThrow(attachmentId);-- attachment.mime = getImageMimeFromExtension(format.ext);-- if (!originalName.includes(".")) {- originalName += `.${format.ext}`;- attachment.title = sanitizeFilename(originalName);- }-- attachment.setContent(buffer, { forceSave: true });+ getContext().init(() => {+ getSql().transactional(() => {+ if (!attachmentId) {+ throw new Error("Missing attachment ID.");+ }+ attachment = becca.getAttachmentOrThrow(attachmentId);++ attachment.mime = getImageMimeFromExtension(format.ext);++ if (!originalName.includes(".")) {+ originalName += `.${format.ext}`;+ attachment.title = sanitizeFilename(originalName);+ }++ attachment.setContent(buffer, { forceSave: true });+ });});});diff --git a/packages/trilium-core/src/services/notes.ts b/packages/trilium-core/src/services/notes.tsindex 502ef1beb1..1830af3a5e 100644--- a/packages/trilium-core/src/services/notes.ts+++ b/packages/trilium-core/src/services/notes.ts@@ -636,41 +636,43 @@ function downloadImages(noteId: string, content: string) {// are downloaded and the IMG references are not updated. For this occasion we have this code// which upon the download of all the images will update the note if the links have not been fixed before- getSql().transactional(() => {- const imageNotes = becca.getNotes(Object.values(imageUrlToAttachmentIdMapping), true);- const log = getLog();+ cls.getContext().init(() => {+ getSql().transactional(() => {+ const imageNotes = becca.getNotes(Object.values(imageUrlToAttachmentIdMapping), true);+ const log = getLog();- const origNote = becca.getNote(noteId);+ const origNote = becca.getNote(noteId);- if (!origNote) {- log.error(`Cannot find note '${noteId}' to replace image link.`);- return;- }+ if (!origNote) {+ log.error(`Cannot find note '${noteId}' to replace image link.`);+ return;+ }- const origContent = origNote.getContent();- let updatedContent = origContent;+ const origContent = origNote.getContent();+ let updatedContent = origContent;- if (typeof updatedContent !== "string") {- log.error(`Note '${noteId}' has a non-string content, cannot replace image link.`);- return;- }+ if (typeof updatedContent !== "string") {+ log.error(`Note '${noteId}' has a non-string content, cannot replace image link.`);+ return;+ }- for (const url in imageUrlToAttachmentIdMapping) {- const imageNote = imageNotes.find((note) => note.noteId === imageUrlToAttachmentIdMapping[url]);+ for (const url in imageUrlToAttachmentIdMapping) {+ const imageNote = imageNotes.find((note) => note.noteId === imageUrlToAttachmentIdMapping[url]);- if (imageNote) {- updatedContent = replaceUrl(updatedContent, url, imageNote);+ if (imageNote) {+ updatedContent = replaceUrl(updatedContent, url, imageNote);+ }}- }- // update only if the links have not been already fixed.- if (updatedContent !== origContent) {- origNote.setContent(updatedContent);+ // update only if the links have not been already fixed.+ if (updatedContent !== origContent) {+ origNote.setContent(updatedContent);- asyncPostProcessContent(origNote, updatedContent);+ asyncPostProcessContent(origNote, updatedContent);- console.log(`Fixed the image links for note '${noteId}' to the offline saved.`);- }+ console.log(`Fixed the image links for note '${noteId}' to the offline saved.`);+ }+ });});}, 5000);});