mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 02:16:05 +01:00 
			
		
		
		
	abstracted note detail components
This commit is contained in:
		| @@ -31,6 +31,23 @@ let noteChangeDisabled = false; | ||||
|  | ||||
| let isNoteChanged = false; | ||||
|  | ||||
| const components = { | ||||
|     'code': noteDetailCode, | ||||
|     'text': noteDetailText, | ||||
|     'file': noteDetailAttachment, | ||||
|     'search': noteDetailSearch, | ||||
|     'render': noteDetailRender | ||||
| }; | ||||
|  | ||||
| function getComponent(type) { | ||||
|     if (components[type]) { | ||||
|         return components[type]; | ||||
|     } | ||||
|     else { | ||||
|         infoService.throwError("Unrecognized type: " + type); | ||||
|     } | ||||
| } | ||||
|  | ||||
| function getCurrentNote() { | ||||
|     return currentNote; | ||||
| } | ||||
| @@ -84,23 +101,8 @@ async function saveNoteIfChanged() { | ||||
| } | ||||
|  | ||||
| function updateNoteFromInputs(note) { | ||||
|     if (note.type === 'text') { | ||||
|         note.content = noteDetailText.getContent(); | ||||
|     } | ||||
|     else if (note.type === 'code') { | ||||
|         note.content = noteDetailCode.getContent(); | ||||
|     } | ||||
|     else if (note.type === 'search') { | ||||
|         note.content = noteDetailSearch.getContent(); | ||||
|     } | ||||
|     else if (note.type === 'render') { | ||||
|         // nothing | ||||
|     } | ||||
|     else { | ||||
|         infoService.throwError("Unrecognized type: " + note.type); | ||||
|     } | ||||
|  | ||||
|     note.title = $noteTitle.val(); | ||||
|     note.content = getComponent(note.type).getContent(); | ||||
|  | ||||
|     treeService.setNoteTitle(note.noteId, note.title); | ||||
| } | ||||
| @@ -164,21 +166,7 @@ async function loadNoteToEditor(noteId) { | ||||
|  | ||||
|         $noteDetailComponents.hide(); | ||||
|  | ||||
|         if (currentNote.type === 'render') { | ||||
|             await noteDetailRender.showRenderNote(); | ||||
|         } | ||||
|         else if (currentNote.type === 'file') { | ||||
|             await noteDetailAttachment.showFileNote(); | ||||
|         } | ||||
|         else if (currentNote.type === 'text') { | ||||
|             await noteDetailText.showTextNote(); | ||||
|         } | ||||
|         else if (currentNote.type === 'code') { | ||||
|             await noteDetailCode.showCodeNote(); | ||||
|         } | ||||
|         else if (currentNote.type === 'search') { | ||||
|             noteDetailSearch.showSearchNote(); | ||||
|         } | ||||
|         await getComponent(currentNote.type).show(); | ||||
|     } | ||||
|     finally { | ||||
|         noteChangeDisabled = false; | ||||
| @@ -221,18 +209,7 @@ async function loadNote(noteId) { | ||||
| function focus() { | ||||
|     const note = getCurrentNote(); | ||||
|  | ||||
|     if (note.type === 'text') { | ||||
|         noteDetailText.focus(); | ||||
|     } | ||||
|     else if (note.type === 'code') { | ||||
|         noteDetailCode.focus(); | ||||
|     } | ||||
|     else if (note.type === 'render' || note.type === 'file' || note.type === 'search') { | ||||
|         // do nothing | ||||
|     } | ||||
|     else { | ||||
|         infoService.throwError('Unrecognized type: ' + note.type); | ||||
|     } | ||||
|     getComponent(note.type).focus(); | ||||
| } | ||||
|  | ||||
| messagingService.subscribeToMessages(syncData => { | ||||
|   | ||||
| @@ -11,7 +11,7 @@ const $attachmentFileSize = $("#attachment-filesize"); | ||||
| const $attachmentDownload = $("#attachment-download"); | ||||
| const $attachmentOpen = $("#attachment-open"); | ||||
|  | ||||
| async function showFileNote() { | ||||
| async function show() { | ||||
|     const currentNote = noteDetailService.getCurrentNote(); | ||||
|  | ||||
|     const labels = await server.get('notes/' + currentNote.noteId + '/labels'); | ||||
| @@ -44,7 +44,7 @@ function getAttachmentUrl() { | ||||
| } | ||||
|  | ||||
| export default { | ||||
|     showFileNote, | ||||
|     show, | ||||
|     getContent: () => null, | ||||
|     focus: () => null | ||||
| } | ||||
| @@ -9,7 +9,7 @@ let codeEditor = null; | ||||
| const $noteDetailCode = $('#note-detail-code'); | ||||
| const $executeScriptButton = $("#execute-script-button"); | ||||
|  | ||||
| async function showCodeNote() { | ||||
| async function show() { | ||||
|     if (!codeEditor) { | ||||
|         await utils.requireLibrary(utils.CODE_MIRROR); | ||||
|  | ||||
| @@ -84,7 +84,7 @@ $(document).bind('keydown', "ctrl+return", executeCurrentNote); | ||||
| $executeScriptButton.click(executeCurrentNote); | ||||
|  | ||||
| export default { | ||||
|     showCodeNote, | ||||
|     show, | ||||
|     getContent, | ||||
|     focus | ||||
| } | ||||
| @@ -4,7 +4,7 @@ import noteDetailService from "./note_detail.js"; | ||||
| 
 | ||||
| const $noteDetailRender = $('#note-detail-render'); | ||||
| 
 | ||||
| async function showRenderNote() { | ||||
| async function show() { | ||||
|     $noteDetailRender.show(); | ||||
| 
 | ||||
|     const bundle = await server.get('script/bundle/' + noteDetailService.getCurrentNoteId()); | ||||
| @@ -15,7 +15,7 @@ async function showRenderNote() { | ||||
| } | ||||
| 
 | ||||
| export default { | ||||
|     showRenderNote, | ||||
|     show, | ||||
|     getContent: () => null, | ||||
|     focus: () => null | ||||
| } | ||||
| @@ -9,7 +9,7 @@ function getContent() { | ||||
|     }); | ||||
| } | ||||
| 
 | ||||
| function showSearchNote() { | ||||
| function show() { | ||||
|     $noteDetailSearch.show(); | ||||
| 
 | ||||
|     try { | ||||
| @@ -27,6 +27,6 @@ function showSearchNote() { | ||||
| 
 | ||||
| export default { | ||||
|     getContent, | ||||
|     showSearchNote, | ||||
|     show, | ||||
|     focus: () => null | ||||
| } | ||||
| @@ -5,7 +5,7 @@ const $noteDetailText = $('#note-detail-text'); | ||||
|  | ||||
| let textEditor = null; | ||||
|  | ||||
| async function showTextNote() { | ||||
| async function show() { | ||||
|     if (!textEditor) { | ||||
|         await utils.requireLibrary(utils.CKEDITOR); | ||||
|  | ||||
| @@ -41,7 +41,7 @@ function getEditor() { | ||||
| } | ||||
|  | ||||
| export default { | ||||
|     showTextNote, | ||||
|     show, | ||||
|     getEditor, | ||||
|     getContent, | ||||
|     focus | ||||
|   | ||||
| @@ -69,7 +69,12 @@ async function ajax(url, method, data) { | ||||
|     }; | ||||
|  | ||||
|     if (data) { | ||||
|         try { | ||||
|             options.data = JSON.stringify(data); | ||||
|         } | ||||
|         catch (e) { | ||||
|             console.log("Can't stringify data: ", data, " because of error: ", e) | ||||
|         } | ||||
|         options.contentType = "application/json"; | ||||
|     } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user