mirror of
https://github.com/amitwh/markdown-converter.git
synced 2026-08-02 10:00:17 +05:30
feat: add image paste and drag-drop support
This commit is contained in:
+29
@@ -3880,6 +3880,35 @@ ipcMain.on('open-table-generator', () => {
|
|||||||
openTableGenerator();
|
openTableGenerator();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// IPC Handler for saving pasted/dropped images
|
||||||
|
ipcMain.handle('save-pasted-image', async (event, { base64, ext }) => {
|
||||||
|
try {
|
||||||
|
let saveDir;
|
||||||
|
|
||||||
|
if (currentFile) {
|
||||||
|
// Save relative to current file
|
||||||
|
saveDir = path.join(path.dirname(currentFile), 'assets');
|
||||||
|
} else {
|
||||||
|
// Use temp directory
|
||||||
|
saveDir = path.join(app.getPath('temp'), 'markdown-converter-images');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!fs.existsSync(saveDir)) {
|
||||||
|
fs.mkdirSync(saveDir, { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
const filename = `image-${Date.now()}.${ext}`;
|
||||||
|
const filePath = path.join(saveDir, filename);
|
||||||
|
const buffer = Buffer.from(base64, 'base64');
|
||||||
|
fs.writeFileSync(filePath, buffer);
|
||||||
|
|
||||||
|
return { relativePath: `assets/${filename}`, absolutePath: filePath };
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to save pasted image:', error);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// IPC Handler to receive generated content from generator windows
|
// IPC Handler to receive generated content from generator windows
|
||||||
ipcMain.on('insert-generated-content', (event, content) => {
|
ipcMain.on('insert-generated-content', (event, content) => {
|
||||||
if (mainWindow && !mainWindow.isDestroyed()) {
|
if (mainWindow && !mainWindow.isDestroyed()) {
|
||||||
|
|||||||
+4
-1
@@ -91,7 +91,10 @@ const ALLOWED_SEND_CHANNELS = [
|
|||||||
'open-table-generator',
|
'open-table-generator',
|
||||||
|
|
||||||
// Insert generated content
|
// Insert generated content
|
||||||
'insert-generated-content'
|
'insert-generated-content',
|
||||||
|
|
||||||
|
// Image paste/drop
|
||||||
|
'save-pasted-image'
|
||||||
];
|
];
|
||||||
|
|
||||||
const ALLOWED_RECEIVE_CHANNELS = [
|
const ALLOWED_RECEIVE_CHANNELS = [
|
||||||
|
|||||||
@@ -1112,6 +1112,53 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
render: (container) => { container.innerHTML = '<p style="color:#888;padding:8px;">Templates coming soon...</p>'; }
|
render: (container) => { container.innerHTML = '<p style="color:#888;padding:8px;">Templates coming soon...</p>'; }
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Image paste handler
|
||||||
|
document.addEventListener('paste', async (e) => {
|
||||||
|
const items = e.clipboardData?.items;
|
||||||
|
if (!items) return;
|
||||||
|
|
||||||
|
for (const item of items) {
|
||||||
|
if (item.type.startsWith('image/')) {
|
||||||
|
e.preventDefault();
|
||||||
|
const blob = item.getAsFile();
|
||||||
|
const reader = new FileReader();
|
||||||
|
reader.onload = async () => {
|
||||||
|
const base64 = reader.result.split(',')[1];
|
||||||
|
const ext = item.type.split('/')[1] === 'png' ? 'png' : 'jpg';
|
||||||
|
const result = await ipcRenderer.invoke('save-pasted-image', { base64, ext });
|
||||||
|
if (result?.relativePath) {
|
||||||
|
tabManager.insertAtCursor(`\n`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
reader.readAsDataURL(blob);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Drag and drop images
|
||||||
|
const editorContainer = document.querySelector('.editor-container');
|
||||||
|
editorContainer?.addEventListener('dragover', (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
e.dataTransfer.dropEffect = 'copy';
|
||||||
|
});
|
||||||
|
editorContainer?.addEventListener('drop', async (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
const files = Array.from(e.dataTransfer.files).filter(f => f.type.startsWith('image/'));
|
||||||
|
for (const file of files) {
|
||||||
|
const reader = new FileReader();
|
||||||
|
reader.onload = async () => {
|
||||||
|
const base64 = reader.result.split(',')[1];
|
||||||
|
const ext = file.name.split('.').pop() || 'png';
|
||||||
|
const result = await ipcRenderer.invoke('save-pasted-image', { base64, ext });
|
||||||
|
if (result?.relativePath) {
|
||||||
|
tabManager.insertAtCursor(`\n`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
reader.readAsDataURL(file);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Initialize command palette
|
// Initialize command palette
|
||||||
const commandPalette = new CommandPalette();
|
const commandPalette = new CommandPalette();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user