From 3b0cb97ed2d641363e537ec7bc2e03d19bd23585 Mon Sep 17 00:00:00 2001 From: hugo Date: Sun, 15 Feb 2026 13:47:05 +0100 Subject: [PATCH] fix: link post when image is saved --- src/renderer/components/Editor/Editor.tsx | 14 ++++++++++++-- .../components/InsertModal/InsertModal.tsx | 8 +++++--- tests/setup.ts | 9 +++++++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/renderer/components/Editor/Editor.tsx b/src/renderer/components/Editor/Editor.tsx index 0f7b91c..60de147 100644 --- a/src/renderer/components/Editor/Editor.tsx +++ b/src/renderer/components/Editor/Editor.tsx @@ -1065,7 +1065,7 @@ const PostEditor: React.FC = ({ postId }) => { }, []); // Handle image insertion from InsertModal (for media library) - const handleInsertImage = useCallback((url: string, alt: string) => { + const handleInsertImage = useCallback(async (url: string, alt: string, mediaId?: string) => { const editor = editorRef.current as any; if (!editor) return; @@ -1078,8 +1078,18 @@ const PostEditor: React.FC = ({ postId }) => { forceMoveMarkers: true }]); + // Link the media to this post if mediaId is provided (from media library) + if (mediaId) { + try { + await window.electronAPI?.postMedia.link(postId, mediaId); + console.log(`[Editor] Linked media ${mediaId} to post ${postId}`); + } catch (error) { + console.error('Failed to link media to post:', error); + } + } + setShowMediaSearch(false); - }, []); + }, [postId]); // Configure Monaco before mount to add macro syntax highlighting const handleEditorWillMount = (monaco: Monaco) => { diff --git a/src/renderer/components/InsertModal/InsertModal.tsx b/src/renderer/components/InsertModal/InsertModal.tsx index e1aefc5..5d765dc 100644 --- a/src/renderer/components/InsertModal/InsertModal.tsx +++ b/src/renderer/components/InsertModal/InsertModal.tsx @@ -34,7 +34,7 @@ type Tab = 'external' | 'internal'; interface InsertModalProps { mode: InsertMode; onInsertLink: (url: string, text?: string) => void; - onInsertImage: (url: string, alt: string) => void; + onInsertImage: (url: string, alt: string, mediaId?: string) => void; onClose: () => void; initialText?: string; // Selected text in editor } @@ -149,7 +149,8 @@ export const InsertModal: React.FC = ({ if (url) { // Extract filename without extension for alt text const altText = result.originalName.replace(/\.[^.]+$/, ''); - onInsertImage(url, altText); + // Pass mediaId so the editor can link this media to the post + onInsertImage(url, altText, result.id); } } onClose(); @@ -162,7 +163,8 @@ export const InsertModal: React.FC = ({ if (mode === 'link') { onInsertLink(externalUrl, externalText || undefined); } else { - onInsertImage(externalUrl, externalAlt || 'Image'); + // External images don't have a mediaId + onInsertImage(externalUrl, externalAlt || 'Image', undefined); } onClose(); }, [mode, externalUrl, externalText, externalAlt, onInsertLink, onInsertImage, onClose]); diff --git a/tests/setup.ts b/tests/setup.ts index fe97b51..4f093ec 100644 --- a/tests/setup.ts +++ b/tests/setup.ts @@ -6,6 +6,13 @@ import { vi, beforeEach, afterEach } from 'vitest'; import '@testing-library/jest-dom/vitest'; +// Polyfill for IE-specific event methods that React DOM's input polyfill checks for +// jsdom doesn't implement these, but React tries to use them for IE compatibility +if (typeof Element !== 'undefined' && !Element.prototype.attachEvent) { + (Element.prototype as any).attachEvent = function() {}; + (Element.prototype as any).detachEvent = function() {}; +} + // Mock localStorage for Zustand persist middleware const localStorageMock = (() => { let store: Record = {}; @@ -65,6 +72,8 @@ Object.defineProperty(globalThis, 'window', { rebuildFromFiles: vi.fn(), getThumbnail: vi.fn(), regenerateThumbnails: vi.fn(), + search: vi.fn(), + getUrl: vi.fn(), }, sync: { configure: vi.fn(),