...
This commit is contained in:
		
							
								
								
									
										103
									
								
								static/js/app.js
									
									
									
									
									
								
							
							
						
						
									
										103
									
								
								static/js/app.js
									
									
									
									
									
								
							| @@ -12,6 +12,23 @@ let collectionSelector; | ||||
| let clipboard = null; | ||||
| let currentFilePath = null; | ||||
|  | ||||
| // Simple event bus | ||||
| const eventBus = { | ||||
|     listeners: {}, | ||||
|     on(event, callback) { | ||||
|         if (!this.listeners[event]) { | ||||
|             this.listeners[event] = []; | ||||
|         } | ||||
|         this.listeners[event].push(callback); | ||||
|     }, | ||||
|     dispatch(event, data) { | ||||
|         if (this.listeners[event]) { | ||||
|             this.listeners[event].forEach(callback => callback(data)); | ||||
|         } | ||||
|     } | ||||
| }; | ||||
| window.eventBus = eventBus; | ||||
|  | ||||
| // Initialize application | ||||
| document.addEventListener('DOMContentLoaded', async () => { | ||||
|     // Initialize WebDAV client | ||||
| @@ -26,7 +43,7 @@ document.addEventListener('DOMContentLoaded', async () => { | ||||
|     // Initialize file tree | ||||
|     fileTree = new FileTree('fileTree', webdavClient); | ||||
|     fileTree.onFileSelect = async (item) => { | ||||
|         await loadFile(item.path); | ||||
|         await editor.loadFile(item.path); | ||||
|     }; | ||||
|      | ||||
|     // Initialize collection selector | ||||
| @@ -39,6 +56,15 @@ document.addEventListener('DOMContentLoaded', async () => { | ||||
|      | ||||
|     // Initialize editor | ||||
|     editor = new MarkdownEditor('editor', 'preview', 'filenameInput'); | ||||
|     editor.setWebDAVClient(webdavClient); | ||||
|  | ||||
|     // Add test content to verify preview works | ||||
|     setTimeout(() => { | ||||
|         if (!editor.editor.getValue()) { | ||||
|             editor.editor.setValue('# Welcome to Markdown Editor\n\nStart typing to see preview...\n'); | ||||
|             editor.updatePreview(); | ||||
|         } | ||||
|     }, 200); | ||||
|      | ||||
|     // Setup editor drop handler | ||||
|     const editorDropHandler = new EditorDropHandler( | ||||
| @@ -50,15 +76,15 @@ document.addEventListener('DOMContentLoaded', async () => { | ||||
|      | ||||
|     // Setup button handlers | ||||
|     document.getElementById('newBtn').addEventListener('click', () => { | ||||
|         newFile(); | ||||
|         editor.newFile(); | ||||
|     }); | ||||
|      | ||||
|     document.getElementById('saveBtn').addEventListener('click', async () => { | ||||
|         await saveFile(); | ||||
|         await editor.save(); | ||||
|     }); | ||||
|      | ||||
|     document.getElementById('deleteBtn').addEventListener('click', async () => { | ||||
|         await deleteCurrentFile(); | ||||
|         await editor.deleteFile(); | ||||
|     }); | ||||
|      | ||||
|     // Setup context menu handlers | ||||
| @@ -69,6 +95,13 @@ document.addEventListener('DOMContentLoaded', async () => { | ||||
|  | ||||
|     // Initialize file tree actions manager | ||||
|     window.fileTreeActions = new FileTreeActions(webdavClient, fileTree, editor); | ||||
|     // Listen for file-saved event to reload file tree | ||||
|     window.eventBus.on('file-saved', async (path) => { | ||||
|         if (fileTree) { | ||||
|             await fileTree.load(); | ||||
|             fileTree.selectNode(path); | ||||
|         } | ||||
|     }); | ||||
| }); | ||||
|  | ||||
| // Listen for column resize events to refresh editor | ||||
| @@ -81,66 +114,6 @@ window.addEventListener('column-resize', () => { | ||||
| /** | ||||
|  * File Operations | ||||
|  */ | ||||
| async function loadFile(path) { | ||||
|     try { | ||||
|         const content = await webdavClient.get(path); | ||||
|         editor.setValue(content); | ||||
|         document.getElementById('filenameInput').value = path; | ||||
|         currentFilePath = path; | ||||
|         showNotification('File loaded', 'success'); | ||||
|     } catch (error) { | ||||
|         console.error('Failed to load file:', error); | ||||
|         showNotification('Failed to load file', 'error'); | ||||
|     } | ||||
| } | ||||
|  | ||||
| function newFile() { | ||||
|     editor.setValue('# New File\n\nStart typing...\n'); | ||||
|     document.getElementById('filenameInput').value = ''; | ||||
|     document.getElementById('filenameInput').focus(); | ||||
|     currentFilePath = null; | ||||
|     showNotification('New file', 'info'); | ||||
| } | ||||
|  | ||||
| async function saveFile() { | ||||
|     const filename = document.getElementById('filenameInput').value.trim(); | ||||
|     if (!filename) { | ||||
|         showNotification('Please enter a filename', 'warning'); | ||||
|         return; | ||||
|     } | ||||
|      | ||||
|     try { | ||||
|         const content = editor.getValue(); | ||||
|         await webdavClient.put(filename, content); | ||||
|         currentFilePath = filename; | ||||
|         await fileTree.load(); | ||||
|         showNotification('Saved', 'success'); | ||||
|     } catch (error) { | ||||
|         console.error('Failed to save file:', error); | ||||
|         showNotification('Failed to save file', 'error'); | ||||
|     } | ||||
| } | ||||
|  | ||||
| async function deleteCurrentFile() { | ||||
|     if (!currentFilePath) { | ||||
|         showNotification('No file selected', 'warning'); | ||||
|         return; | ||||
|     } | ||||
|      | ||||
|     if (!confirm(`Delete ${currentFilePath}?`)) { | ||||
|         return; | ||||
|     } | ||||
|      | ||||
|     try { | ||||
|         await webdavClient.delete(currentFilePath); | ||||
|         await fileTree.load(); | ||||
|         newFile(); | ||||
|         showNotification('Deleted', 'success'); | ||||
|     } catch (error) { | ||||
|         console.error('Failed to delete file:', error); | ||||
|         showNotification('Failed to delete file', 'error'); | ||||
|     } | ||||
| } | ||||
|  | ||||
| /** | ||||
|  * Context Menu Handlers | ||||
| @@ -166,7 +139,7 @@ async function handleContextAction(action, targetPath, isDir) { | ||||
|     switch (action) { | ||||
|         case 'open': | ||||
|             if (!isDir) { | ||||
|                 await loadFile(targetPath); | ||||
|                 await editor.loadFile(targetPath); | ||||
|             } | ||||
|             break; | ||||
|              | ||||
|   | ||||
		Reference in New Issue
	
	Block a user