| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  | import appContext from "./app_context.js"; | 
					
						
							|  |  |  | import utils from "./utils.js"; | 
					
						
							|  |  |  | import protectedSessionHolder from "./protected_session_holder.js"; | 
					
						
							|  |  |  | import server from "./server.js"; | 
					
						
							|  |  |  | import ws from "./ws.js"; | 
					
						
							|  |  |  | import treeCache from "./tree_cache.js"; | 
					
						
							| 
									
										
										
										
											2021-03-03 22:48:06 +01:00
										 |  |  | import treeService from "./tree.js"; | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  | import toastService from "./toast.js"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-03 22:48:06 +01:00
										 |  |  | async function createNote(parentNotePath, options = {}) { | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  |     options = Object.assign({ | 
					
						
							|  |  |  |         activate: true, | 
					
						
							| 
									
										
										
										
											2020-05-08 23:39:46 +02:00
										 |  |  |         focus: 'title', | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  |         target: 'into' | 
					
						
							|  |  |  |     }, options); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // if isProtected isn't available (user didn't enter password yet), then note is created as unencrypted
 | 
					
						
							|  |  |  |     // but this is quite weird since user doesn't see WHERE the note is being created so it shouldn't occur often
 | 
					
						
							|  |  |  |     if (!options.isProtected || !protectedSessionHolder.isProtectedSessionAvailable()) { | 
					
						
							|  |  |  |         options.isProtected = false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-07 21:08:55 +01:00
										 |  |  |     if (appContext.tabManager.getActiveTabNoteType() !== 'text') { | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  |         options.saveSelection = false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (options.saveSelection && utils.isCKEditorInitialized()) { | 
					
						
							|  |  |  |         [options.title, options.content] = parseSelectedHtml(window.cutToNote.getSelectedHtml()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const newNoteName = options.title || "new note"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-03 22:48:06 +01:00
										 |  |  |     const parentNoteId = treeService.getNoteIdFromNotePath(parentNotePath); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  |     const {note, branch} = await server.post(`notes/${parentNoteId}/children?target=${options.target}&targetBranchId=${options.targetBranchId}`, { | 
					
						
							|  |  |  |         title: newNoteName, | 
					
						
							|  |  |  |         content: options.content || "", | 
					
						
							|  |  |  |         isProtected: options.isProtected, | 
					
						
							| 
									
										
										
										
											2020-05-08 23:39:46 +02:00
										 |  |  |         type: options.type, | 
					
						
							|  |  |  |         mime: options.mime | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (options.saveSelection && utils.isCKEditorInitialized()) { | 
					
						
							|  |  |  |         // we remove the selection only after it was saved to server to make sure we don't lose anything
 | 
					
						
							|  |  |  |         window.cutToNote.removeSelection(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  |     await ws.waitForMaxKnownEntityChangeId(); | 
					
						
							| 
									
										
										
										
											2020-07-24 00:02:15 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  |     if (options.activate) { | 
					
						
							| 
									
										
										
										
											2020-02-07 21:08:55 +01:00
										 |  |  |         const activeTabContext = appContext.tabManager.getActiveTabContext(); | 
					
						
							| 
									
										
										
										
											2021-03-03 22:48:06 +01:00
										 |  |  |         await activeTabContext.setNote(`${parentNotePath}/${note.noteId}`); | 
					
						
							| 
									
										
										
										
											2020-05-05 21:42:18 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-08 23:39:46 +02:00
										 |  |  |         if (options.focus === 'title') { | 
					
						
							| 
									
										
										
										
											2021-01-29 22:44:59 +01:00
										 |  |  |             appContext.triggerEvent('focusAndSelectTitle'); | 
					
						
							| 
									
										
										
										
											2020-05-08 23:39:46 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |         else if (options.focus === 'content') { | 
					
						
							| 
									
										
										
										
											2021-01-29 22:44:59 +01:00
										 |  |  |             appContext.triggerEvent('focusOnDetail', {tabId: activeTabContext.tabId}); | 
					
						
							| 
									
										
										
										
											2020-05-08 23:39:46 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-09 14:25:27 +02:00
										 |  |  |     const noteEntity = await treeCache.getNote(note.noteId); | 
					
						
							| 
									
										
										
										
											2020-07-24 00:02:15 +02:00
										 |  |  |     const branchEntity = treeCache.getBranch(branch.branchId); | 
					
						
							| 
									
										
										
										
											2020-05-09 14:25:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         note: noteEntity, | 
					
						
							|  |  |  |         branch: branchEntity | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* If first element is heading, parse it out and use it as a new heading. */ | 
					
						
							|  |  |  | function parseSelectedHtml(selectedHtml) { | 
					
						
							|  |  |  |     const dom = $.parseHTML(selectedHtml); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (dom.length > 0 && dom[0].tagName && dom[0].tagName.match(/h[1-6]/i)) { | 
					
						
							|  |  |  |         const title = $(dom[0]).text(); | 
					
						
							|  |  |  |         // remove the title from content (only first occurence)
 | 
					
						
							|  |  |  |         const content = selectedHtml.replace(dom[0].outerHTML, ""); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return [title, content]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         return [null, selectedHtml]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-03 22:48:06 +01:00
										 |  |  | async function duplicateSubtree(noteId, parentNotePath) { | 
					
						
							|  |  |  |     const parentNoteId = treeService.getNoteIdFromNotePath(parentNotePath); | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  |     const {note} = await server.post(`notes/${noteId}/duplicate/${parentNoteId}`); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-02 23:27:48 +02:00
										 |  |  |     await ws.waitForMaxKnownEntityChangeId(); | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-09 23:40:15 +02:00
										 |  |  |     const activeTabContext = appContext.tabManager.getActiveTabContext(); | 
					
						
							|  |  |  |     activeTabContext.setNote(`${parentNotePath}/${note.noteId}`); | 
					
						
							| 
									
										
										
										
											2020-02-03 20:07:34 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const origNote = await treeCache.getNote(noteId); | 
					
						
							|  |  |  |     toastService.showMessage(`Note "${origNote.title}" has been duplicated`); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default { | 
					
						
							|  |  |  |     createNote, | 
					
						
							| 
									
										
										
										
											2020-11-19 14:06:32 +01:00
										 |  |  |     duplicateSubtree | 
					
						
							| 
									
										
										
										
											2020-05-09 14:25:27 +02:00
										 |  |  | }; |