| 
									
										
										
										
											2019-08-26 20:21:43 +02:00
										 |  |  | import ws from './ws.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  | import noteDetailService from './note_detail.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 21:16:57 -04:00
										 |  |  | import protectedSessionHolder from './protected_session_holder.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | import treeUtils from './tree_utils.js'; | 
					
						
							|  |  |  | import utils from './utils.js'; | 
					
						
							|  |  |  | import server from './server.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 12:29:00 -04:00
										 |  |  | import treeCache from './tree_cache.js'; | 
					
						
							| 
									
										
										
										
											2019-10-20 10:00:18 +02:00
										 |  |  | import toastService from "./toast.js"; | 
					
						
							| 
									
										
										
										
											2018-03-26 23:18:50 -04:00
										 |  |  | import treeBuilder from "./tree_builder.js"; | 
					
						
							| 
									
										
										
										
											2018-12-12 20:39:56 +01:00
										 |  |  | import hoistedNoteService from '../services/hoisted_note.js'; | 
					
						
							| 
									
										
										
										
											2019-08-25 17:36:13 +02:00
										 |  |  | import optionsService from "../services/options.js"; | 
					
						
							| 
									
										
										
										
											2019-05-29 21:10:28 +02:00
										 |  |  | import bundle from "./bundle.js"; | 
					
						
							| 
									
										
										
										
											2019-11-24 09:50:19 +01:00
										 |  |  | import keyboardActionService from "./keyboard_actions.js"; | 
					
						
							| 
									
										
										
										
											2020-01-12 11:15:23 +01:00
										 |  |  | import appContext from "./app_context.js"; | 
					
						
							| 
									
										
										
										
											2020-01-12 09:12:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-29 21:10:28 +02:00
										 |  |  | let setFrontendAsLoaded; | 
					
						
							|  |  |  | const frontendLoaded = new Promise(resolve => { setFrontendAsLoaded = resolve; }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  | async function setPrefix(branchId, prefix) { | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     utils.assertArguments(branchId); | 
					
						
							| 
									
										
										
										
											2017-11-28 10:17:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-26 09:58:00 +02:00
										 |  |  |     const branch = treeCache.getBranch(branchId); | 
					
						
							| 
									
										
										
										
											2018-03-12 23:14:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  |     branch.prefix = prefix; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 11:15:23 +01:00
										 |  |  |     for (const node of await appContext.getMainNoteTree().getNodesByBranchId(branchId)) { | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  |         await setNodeTitleWithPrefix(node); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-12 23:14:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  | async function setNodeTitleWithPrefix(node) { | 
					
						
							| 
									
										
										
										
											2018-03-26 21:50:47 -04:00
										 |  |  |     const noteTitle = await treeUtils.getNoteTitle(node.data.noteId); | 
					
						
							| 
									
										
										
										
											2019-10-26 09:58:00 +02:00
										 |  |  |     const branch = treeCache.getBranch(node.data.branchId); | 
					
						
							| 
									
										
										
										
											2018-03-12 23:14:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     const prefix = branch.prefix; | 
					
						
							| 
									
										
										
										
											2017-11-28 10:17:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     const title = (prefix ? (prefix + " - ") : "") + noteTitle; | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     node.setTitle(utils.escapeHtml(title)); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-25 10:06:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 13:09:00 +02:00
										 |  |  | /** @return {FancytreeNode} */ | 
					
						
							| 
									
										
										
										
											2019-01-01 19:32:34 +01:00
										 |  |  | async function activateNote(notePath, noteLoadedListener) { | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     utils.assertArguments(notePath); | 
					
						
							| 
									
										
										
										
											2018-01-15 20:54:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-10 19:53:42 +01:00
										 |  |  |     // notePath argument can contain only noteId which is not good when hoisted since
 | 
					
						
							|  |  |  |     // then we need to check the whole note path
 | 
					
						
							|  |  |  |     const runNotePath = await getRunPath(notePath); | 
					
						
							| 
									
										
										
										
											2019-05-14 22:29:47 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!runNotePath) { | 
					
						
							|  |  |  |         console.log("Cannot activate " + notePath); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-13 23:28:48 +01:00
										 |  |  |     const hoistedNoteId = await hoistedNoteService.getHoistedNoteId(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-10 19:53:42 +01:00
										 |  |  |     if (hoistedNoteId !== 'root' && !runNotePath.includes(hoistedNoteId)) { | 
					
						
							| 
									
										
										
										
											2019-08-20 21:40:47 +02:00
										 |  |  |         const confirmDialog = await import('../dialogs/confirm.js'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-13 23:28:48 +01:00
										 |  |  |         if (!await confirmDialog.confirm("Requested note is outside of hoisted note subtree. Do you want to unhoist?")) { | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // unhoist so we can activate the note
 | 
					
						
							| 
									
										
										
										
											2018-12-15 20:29:08 +01:00
										 |  |  |         await hoistedNoteService.unhoist(); | 
					
						
							| 
									
										
										
										
											2018-12-13 23:28:48 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-10 22:45:03 +02:00
										 |  |  |     utils.closeActiveDialog(); | 
					
						
							| 
									
										
										
										
											2018-11-14 11:17:20 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 11:15:23 +01:00
										 |  |  |     const node = await appContext.getMainNoteTree().expandToNote(notePath); | 
					
						
							| 
									
										
										
										
											2018-01-15 20:54:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-01 19:32:34 +01:00
										 |  |  |     if (noteLoadedListener) { | 
					
						
							|  |  |  |         noteDetailService.addDetailLoadedListener(node.data.noteId, noteLoadedListener); | 
					
						
							| 
									
										
										
										
											2018-08-14 13:50:04 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-02 19:07:50 +01:00
										 |  |  |     await node.setActive(true); | 
					
						
							| 
									
										
										
										
											2018-01-01 18:53:52 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-13 08:42:37 +02:00
										 |  |  |     return node; | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-03 17:46:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-11 21:27:27 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Accepts notePath which might or might not be valid and returns an existing path as close to the original | 
					
						
							|  |  |  |  * notePath as possible. | 
					
						
							| 
									
										
										
										
											2019-10-20 13:09:00 +02:00
										 |  |  |  * @return {string|null} | 
					
						
							| 
									
										
										
										
											2019-05-11 21:27:27 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | async function resolveNotePath(notePath) { | 
					
						
							|  |  |  |     const runPath = await getRunPath(notePath); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-14 22:29:47 +02:00
										 |  |  |     return runPath ? runPath.join("/") : null; | 
					
						
							| 
									
										
										
										
											2019-05-11 21:27:27 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Accepts notePath and tries to resolve it. Part of the path might not be valid because of note moving (which causes | 
					
						
							|  |  |  |  * path change) or other corruption, in that case this will try to get some other valid path to the correct note. | 
					
						
							| 
									
										
										
										
											2019-10-20 13:09:00 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @return {string[]} | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |  */ | 
					
						
							|  |  |  | async function getRunPath(notePath) { | 
					
						
							|  |  |  |     utils.assertArguments(notePath); | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-28 14:08:05 +02:00
										 |  |  |     notePath = notePath.split("-")[0].trim(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (notePath.length === 0) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-09 22:12:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     const path = notePath.split("/").reverse(); | 
					
						
							| 
									
										
										
										
											2018-05-26 16:16:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!path.includes("root")) { | 
					
						
							|  |  |  |         path.push('root'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-12 20:39:56 +01:00
										 |  |  |     const hoistedNoteId = await hoistedNoteService.getHoistedNoteId(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     const effectivePath = []; | 
					
						
							|  |  |  |     let childNoteId = null; | 
					
						
							|  |  |  |     let i = 0; | 
					
						
							| 
									
										
										
										
											2017-11-19 18:16:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     while (true) { | 
					
						
							|  |  |  |         if (i >= path.length) { | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-30 00:02:32 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         const parentNoteId = path[i++]; | 
					
						
							| 
									
										
										
										
											2017-11-19 11:28:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         if (childNoteId !== null) { | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  |             const child = await treeCache.getNote(childNoteId); | 
					
						
							| 
									
										
										
										
											2018-11-12 23:34:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (!child) { | 
					
						
							| 
									
										
										
										
											2019-05-14 22:29:47 +02:00
										 |  |  |                 console.log("Can't find note " + childNoteId); | 
					
						
							|  |  |  |                 return; | 
					
						
							| 
									
										
										
										
											2018-11-12 23:34:22 +01:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             const parents = await child.getParentNotes(); | 
					
						
							| 
									
										
										
										
											2017-11-19 11:28:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             if (!parents) { | 
					
						
							| 
									
										
										
										
											2019-08-26 20:21:43 +02:00
										 |  |  |                 ws.logError("No parents found for " + childNoteId); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-11-26 21:00:42 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             if (!parents.some(p => p.noteId === parentNoteId)) { | 
					
						
							| 
									
										
										
										
											2018-11-14 11:17:20 +01:00
										 |  |  |                 console.debug(utils.now(), "Did not find parent " + parentNoteId + " for child " + childNoteId); | 
					
						
							| 
									
										
										
										
											2017-11-19 11:28:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |                 if (parents.length > 0) { | 
					
						
							| 
									
										
										
										
											2018-11-14 11:17:20 +01:00
										 |  |  |                     console.debug(utils.now(), "Available parents:", parents); | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |                     const someNotePath = await getSomeNotePath(parents[0]); | 
					
						
							| 
									
										
										
										
											2017-12-23 12:19:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |                     if (someNotePath) { // in case it's root the path may be empty
 | 
					
						
							|  |  |  |                         const pathToRoot = someNotePath.split("/").reverse(); | 
					
						
							| 
									
										
										
										
											2017-11-19 18:16:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |                         for (const noteId of pathToRoot) { | 
					
						
							|  |  |  |                             effectivePath.push(noteId); | 
					
						
							| 
									
										
										
										
											2017-11-30 00:02:32 -05:00
										 |  |  |                         } | 
					
						
							| 
									
										
										
										
											2018-05-27 12:26:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |                         effectivePath.push('root'); | 
					
						
							| 
									
										
										
										
											2017-11-19 11:28:46 -05:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |                     break; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 else { | 
					
						
							| 
									
										
										
										
											2019-08-26 20:21:43 +02:00
										 |  |  |                     ws.logError("No parents, can't activate node."); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |                     return; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-03 17:46:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-12 20:39:56 +01:00
										 |  |  |         effectivePath.push(parentNoteId); | 
					
						
							|  |  |  |         childNoteId = parentNoteId; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (parentNoteId === hoistedNoteId) { | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     return effectivePath.reverse(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | async function getSomeNotePath(note) { | 
					
						
							|  |  |  |     utils.assertArguments(note); | 
					
						
							| 
									
										
										
										
											2017-11-21 20:04:06 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     const path = []; | 
					
						
							| 
									
										
										
										
											2017-11-21 20:04:06 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     let cur = note; | 
					
						
							| 
									
										
										
										
											2017-11-21 20:04:06 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     while (cur.noteId !== 'root') { | 
					
						
							|  |  |  |         path.push(cur.noteId); | 
					
						
							| 
									
										
										
										
											2018-03-24 23:00:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         const parents = await cur.getParentNotes(); | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         if (!parents.length) { | 
					
						
							| 
									
										
										
										
											2020-01-03 21:15:45 +01:00
										 |  |  |             console.error(`Can't find parents for note ${cur.noteId}`); | 
					
						
							| 
									
										
										
										
											2019-05-19 18:21:29 +02:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2017-11-21 20:04:06 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         cur = parents[0]; | 
					
						
							| 
									
										
										
										
											2017-11-21 20:04:06 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-03 10:48:36 +01:00
										 |  |  |     path.push('root'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     return path.reverse().join('/'); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | async function setExpandedToServer(branchId, isExpanded) { | 
					
						
							|  |  |  |     utils.assertArguments(branchId); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     const expandedNum = isExpanded ? 1 : 0; | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 20:33:10 -04:00
										 |  |  |     await server.put('branches/' + branchId + '/expanded/' + expandedNum); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-11-19 18:16:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  | async function treeInitialized() { | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  |     if (appContext.getTabContexts().length > 0) { | 
					
						
							| 
									
										
										
										
											2019-05-11 21:27:27 +02:00
										 |  |  |         // this is just tree reload - tabs are already in place
 | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-25 17:36:13 +02:00
										 |  |  |     const options = await optionsService.waitForOptions(); | 
					
						
							| 
									
										
										
										
											2019-05-10 21:43:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-22 23:31:02 +02:00
										 |  |  |     const openTabs = options.getJson('openTabs') || []; | 
					
						
							| 
									
										
										
										
											2019-05-10 21:43:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 22:12:05 +02:00
										 |  |  |     // if there's notePath in the URL, make sure it's open and active
 | 
					
						
							|  |  |  |     // (useful, among others, for opening clipped notes from clipper)
 | 
					
						
							|  |  |  |     if (location.hash) { | 
					
						
							|  |  |  |         const notePath = location.hash.substr(1); | 
					
						
							|  |  |  |         const noteId = treeUtils.getNoteIdFromNotePath(notePath); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-17 10:22:26 +01:00
										 |  |  |         if (noteId && await treeCache.noteExists(noteId)) { | 
					
						
							| 
									
										
										
										
											2019-07-09 22:12:05 +02:00
										 |  |  |             for (const tab of openTabs) { | 
					
						
							|  |  |  |                 tab.active = false; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const foundTab = openTabs.find(tab => noteId === treeUtils.getNoteIdFromNotePath(tab.notePath)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (foundTab) { | 
					
						
							|  |  |  |                 foundTab.active = true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 openTabs.push({ | 
					
						
							|  |  |  |                     notePath: notePath, | 
					
						
							|  |  |  |                     active: true | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-28 14:08:05 +02:00
										 |  |  |     let filteredTabs = []; | 
					
						
							| 
									
										
										
										
											2018-12-30 22:36:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-10 21:43:40 +02:00
										 |  |  |     for (const openTab of openTabs) { | 
					
						
							|  |  |  |         const noteId = treeUtils.getNoteIdFromNotePath(openTab.notePath); | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-10 21:43:40 +02:00
										 |  |  |         if (await treeCache.noteExists(noteId)) { | 
					
						
							|  |  |  |             // note doesn't exist so don't try to open tab for it
 | 
					
						
							|  |  |  |             filteredTabs.push(openTab); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-28 14:08:05 +02:00
										 |  |  |     if (utils.isMobile()) { | 
					
						
							|  |  |  |         // mobile frontend doesn't have tabs so show only the active tab
 | 
					
						
							|  |  |  |         filteredTabs = filteredTabs.filter(tab => tab.active); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-10 21:43:40 +02:00
										 |  |  |     if (filteredTabs.length === 0) { | 
					
						
							|  |  |  |         filteredTabs.push({ | 
					
						
							|  |  |  |             notePath: 'root', | 
					
						
							|  |  |  |             active: true | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-13 22:10:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-14 22:29:47 +02:00
										 |  |  |     if (!filteredTabs.find(tab => tab.active)) { | 
					
						
							|  |  |  |         filteredTabs[0].active = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-10 21:43:40 +02:00
										 |  |  |     for (const tab of filteredTabs) { | 
					
						
							|  |  |  |         await noteDetailService.loadNoteDetail(tab.notePath, { | 
					
						
							| 
									
										
										
										
											2019-08-16 21:29:44 +02:00
										 |  |  |             state: tab, | 
					
						
							| 
									
										
										
										
											2019-05-10 21:43:40 +02:00
										 |  |  |             newTab: true, | 
					
						
							| 
									
										
										
										
											2019-09-03 22:01:45 +02:00
										 |  |  |             activate: tab.active, | 
					
						
							|  |  |  |             async: true // faster initial load
 | 
					
						
							| 
									
										
										
										
											2019-05-10 21:43:40 +02:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-05-10 21:43:40 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // previous opening triggered task to save tab changes but these are bogus changes (this is init)
 | 
					
						
							|  |  |  |     // so we'll cancel it
 | 
					
						
							| 
									
										
										
										
											2020-01-12 12:48:17 +01:00
										 |  |  |     appContext.clearOpenTabsTask(); | 
					
						
							| 
									
										
										
										
											2019-05-29 21:10:28 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     setFrontendAsLoaded(); | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | async function reload() { | 
					
						
							| 
									
										
										
										
											2020-01-03 21:15:45 +01:00
										 |  |  |     const notes = await loadTreeData(); | 
					
						
							| 
									
										
										
										
											2018-03-12 23:14:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 11:15:23 +01:00
										 |  |  |     const activeNode = appContext.getMainNoteTree().getActiveNode(); | 
					
						
							| 
									
										
										
										
											2019-04-20 21:14:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 11:15:23 +01:00
										 |  |  |     const activeNotePath = activeNode !== null ? await treeUtils.getNotePath(activeNode) : null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await appContext.getMainNoteTree().reload(notes); | 
					
						
							| 
									
										
										
										
											2019-06-01 14:12:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // reactivate originally activated node, but don't trigger note loading
 | 
					
						
							|  |  |  |     if (activeNotePath) { | 
					
						
							| 
									
										
										
										
											2020-01-12 11:15:23 +01:00
										 |  |  |         const node = await appContext.getMainNoteTree().getNodeFromPath(activeNotePath, true); | 
					
						
							| 
									
										
										
										
											2019-06-01 14:12:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         await node.setActive(true, {noEvents: true}); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-12 23:27:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-25 22:18:34 +01:00
										 |  |  | function isNotePathInAddress() { | 
					
						
							| 
									
										
										
										
											2019-05-14 22:29:47 +02:00
										 |  |  |     const [notePath, tabId] = getHashValueFromAddress(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return notePath.startsWith("root") | 
					
						
							|  |  |  |         // empty string is for empty/uninitialized tab
 | 
					
						
							|  |  |  |         || (notePath === '' && !!tabId); | 
					
						
							| 
									
										
										
										
											2019-01-25 22:18:34 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getHashValueFromAddress() { | 
					
						
							| 
									
										
										
										
											2019-05-14 22:29:47 +02:00
										 |  |  |     const str = document.location.hash ? document.location.hash.substr(1) : ""; // strip initial #
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return str.split("-"); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-03-12 23:27:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-03 21:15:45 +01:00
										 |  |  | async function loadTreeData() { | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     const resp = await server.get('tree'); | 
					
						
							| 
									
										
										
										
											2017-11-22 19:58:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-25 21:47:14 +02:00
										 |  |  |     treeCache.load(resp.notes, resp.branches); | 
					
						
							| 
									
										
										
										
											2019-03-18 23:03:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return await treeBuilder.prepareTree(); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-11-05 09:52:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | function setProtected(noteId, isProtected) { | 
					
						
							| 
									
										
										
										
											2020-01-12 11:15:23 +01:00
										 |  |  |     appContext.getMainNoteTree().getNodesByNoteId(noteId).map(node => { | 
					
						
							| 
									
										
										
										
											2019-10-20 12:29:34 +02:00
										 |  |  |         node.data.isProtected = isProtected; | 
					
						
							|  |  |  |         node.toggleClass("protected", isProtected); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  | async function setNoteTitle(noteId, title) { | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     utils.assertArguments(noteId); | 
					
						
							| 
									
										
										
										
											2017-11-22 19:58:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 22:37:02 -04:00
										 |  |  |     const note = await treeCache.getNote(noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     note.title = title; | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 11:15:23 +01:00
										 |  |  |     for (const clone of appContext.getMainNoteTree().getNodesByNoteId(noteId)) { | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  |         await setNodeTitleWithPrefix(clone); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | async function createNewTopLevelNote() { | 
					
						
							| 
									
										
										
										
											2019-01-15 20:30:54 +01:00
										 |  |  |     const hoistedNoteId = await hoistedNoteService.getHoistedNoteId(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 11:15:23 +01:00
										 |  |  |     const rootNode = appContext.getMainNoteTree().getNodesByNoteId(hoistedNoteId)[0]; | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 23:24:41 +01:00
										 |  |  |     await createNote(rootNode, hoistedNoteId, "into"); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-29 21:18:12 +02:00
										 |  |  | async function createNote(node, parentNoteId, target, extraOptions = {}) { | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     utils.assertArguments(node, parentNoteId, target); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-24 21:40:50 +01:00
										 |  |  |     extraOptions.activate = extraOptions.activate === undefined ? true : !!extraOptions.activate; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     // 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
 | 
					
						
							| 
									
										
										
										
											2019-03-29 23:24:41 +01:00
										 |  |  |     if (!extraOptions.isProtected || !protectedSessionHolder.isProtectedSessionAvailable()) { | 
					
						
							|  |  |  |         extraOptions.isProtected = false; | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  |     if (appContext.getActiveTabNoteType() !== 'text') { | 
					
						
							| 
									
										
										
										
											2019-03-29 23:24:41 +01:00
										 |  |  |         extraOptions.saveSelection = false; | 
					
						
							| 
									
										
										
										
											2018-09-06 22:58:46 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 23:24:41 +01:00
										 |  |  |     if (extraOptions.saveSelection) { | 
					
						
							|  |  |  |         [extraOptions.title, extraOptions.content] = parseSelectedHtml(window.cutToNote.getSelectedHtml()); | 
					
						
							| 
									
										
										
										
											2018-09-06 22:58:46 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 23:24:41 +01:00
										 |  |  |     const newNoteName = extraOptions.title || "new note"; | 
					
						
							| 
									
										
										
										
											2018-03-25 00:20:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-16 12:28:47 +01:00
										 |  |  |     const {note, branch} = await server.post(`notes/${parentNoteId}/children?target=${target}&targetBranchId=${node.data.branchId}`, { | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         title: newNoteName, | 
					
						
							| 
									
										
										
										
											2019-11-16 17:00:22 +01:00
										 |  |  |         content: extraOptions.content || "", | 
					
						
							| 
									
										
										
										
											2019-03-29 23:24:41 +01:00
										 |  |  |         isProtected: extraOptions.isProtected, | 
					
						
							|  |  |  |         type: extraOptions.type | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-03-25 00:20:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 23:24:41 +01:00
										 |  |  |     if (extraOptions.saveSelection) { | 
					
						
							| 
									
										
										
										
											2018-09-06 22:58:46 +02:00
										 |  |  |         // we remove the selection only after it was saved to server to make sure we don't lose anything
 | 
					
						
							|  |  |  |         window.cutToNote.removeSelection(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-01 22:19:29 +02:00
										 |  |  |     await noteDetailService.saveNotesIfChanged(); | 
					
						
							| 
									
										
										
										
											2018-09-06 22:58:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-10 22:46:08 +01:00
										 |  |  |     noteDetailService.addDetailLoadedListener(note.noteId, noteDetailService.focusAndSelectTitle); | 
					
						
							| 
									
										
										
										
											2018-10-31 18:21:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-26 09:51:08 +02:00
										 |  |  |     const noteEntity = await treeCache.getNote(note.noteId); | 
					
						
							| 
									
										
										
										
											2019-10-26 09:58:00 +02:00
										 |  |  |     const branchEntity = treeCache.getBranch(branch.branchId); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-24 21:40:50 +01:00
										 |  |  |     let newNodeData = { | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         title: newNoteName, | 
					
						
							| 
									
										
										
										
											2018-04-01 11:42:12 -04:00
										 |  |  |         noteId: branchEntity.noteId, | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         parentNoteId: parentNoteId, | 
					
						
							| 
									
										
										
										
											2018-04-01 11:42:12 -04:00
										 |  |  |         refKey: branchEntity.noteId, | 
					
						
							|  |  |  |         branchId: branchEntity.branchId, | 
					
						
							| 
									
										
										
										
											2019-03-29 23:24:41 +01:00
										 |  |  |         isProtected: extraOptions.isProtected, | 
					
						
							| 
									
										
										
										
											2019-11-04 22:41:06 +01:00
										 |  |  |         type: noteEntity.type, | 
					
						
							| 
									
										
										
										
											2018-11-14 08:42:00 +01:00
										 |  |  |         extraClasses: await treeBuilder.getExtraClasses(noteEntity), | 
					
						
							| 
									
										
										
										
											2019-03-29 23:24:41 +01:00
										 |  |  |         icon: await treeBuilder.getIcon(noteEntity), | 
					
						
							|  |  |  |         folder: extraOptions.type === 'search', | 
					
						
							| 
									
										
										
										
											2019-06-27 21:24:25 +02:00
										 |  |  |         lazy: true, | 
					
						
							|  |  |  |         key: utils.randomString(12) // this should prevent some "duplicate key" errors
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-24 21:40:50 +01:00
										 |  |  |     /** @var {FancytreeNode} */ | 
					
						
							|  |  |  |     let newNode; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     if (target === 'after') { | 
					
						
							| 
									
										
										
										
											2019-11-24 21:40:50 +01:00
										 |  |  |         newNode = node.appendSibling(newNodeData); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     else if (target === 'into') { | 
					
						
							|  |  |  |         if (!node.getChildren() && node.isFolder()) { | 
					
						
							| 
									
										
										
										
											2019-07-06 13:14:33 +02:00
										 |  |  |             // folder is not loaded - load will bring up the note since it was already put into cache
 | 
					
						
							|  |  |  |             await node.load(true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             await node.setExpanded(); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-07-06 13:14:33 +02:00
										 |  |  |         else { | 
					
						
							| 
									
										
										
										
											2019-11-24 21:40:50 +01:00
										 |  |  |             node.addChildren(newNodeData); | 
					
						
							| 
									
										
										
										
											2019-07-06 13:14:33 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-24 21:40:50 +01:00
										 |  |  |         newNode = node.getLastChild(); | 
					
						
							| 
									
										
										
										
											2018-02-13 23:25:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-22 21:21:44 +01:00
										 |  |  |         const parentNoteEntity = await treeCache.getNote(node.data.noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         node.folder = true; | 
					
						
							| 
									
										
										
										
											2019-01-22 21:21:44 +01:00
										 |  |  |         node.icon = await treeBuilder.getIcon(parentNoteEntity); // icon might change into folder
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         node.renderTitle(); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     else { | 
					
						
							| 
									
										
										
										
											2019-10-20 10:00:18 +02:00
										 |  |  |         toastService.throwError("Unrecognized target: " + target); | 
					
						
							| 
									
										
										
										
											2018-01-13 17:00:40 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-24 21:40:50 +01:00
										 |  |  |     if (extraOptions.activate) { | 
					
						
							|  |  |  |         await newNode.setActive(true); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 23:55:28 +01:00
										 |  |  |     // need to refresh because original doesn't have methods like .getParent()
 | 
					
						
							| 
									
										
										
										
											2020-01-12 11:15:23 +01:00
										 |  |  |     newNodeData = appContext.getMainNoteTree().getNodesByNoteId(branchEntity.noteId)[0]; | 
					
						
							| 
									
										
										
										
											2019-03-29 23:55:28 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // following for cycle will make sure that also clones of a parent are refreshed
 | 
					
						
							| 
									
										
										
										
											2020-01-12 11:15:23 +01:00
										 |  |  |     for (const newParentNode of appContext.getMainNoteTree().getNodesByNoteId(parentNoteId)) { | 
					
						
							| 
									
										
										
										
											2019-11-24 21:40:50 +01:00
										 |  |  |         if (newParentNode.key === newNodeData.getParent().key) { | 
					
						
							| 
									
										
										
										
											2019-03-29 23:55:28 +01:00
										 |  |  |             // we've added a note into this one so no need to refresh
 | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         await newParentNode.load(true); // force reload to show up new note
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 11:15:23 +01:00
										 |  |  |         await appContext.getMainNoteTree().checkFolderStatus(newParentNode); | 
					
						
							| 
									
										
										
										
											2019-03-29 23:55:28 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 23:45:37 +02:00
										 |  |  |     return {note, branch}; | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-02-24 21:23:04 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-06 22:58:46 +02: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(); | 
					
						
							| 
									
										
										
										
											2018-09-07 10:26:23 +02:00
										 |  |  |         // remove the title from content (only first occurence)
 | 
					
						
							|  |  |  |         const content = selectedHtml.replace(dom[0].outerHTML, ""); | 
					
						
							| 
									
										
										
										
											2018-09-06 22:58:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return [title, content]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         return [null, selectedHtml]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | async function sortAlphabetically(noteId) { | 
					
						
							|  |  |  |     await server.put('notes/' + noteId + '/sort'); | 
					
						
							| 
									
										
										
										
											2018-03-13 19:31:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     await reload(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-18 23:41:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-26 20:21:43 +02:00
										 |  |  | ws.subscribeToMessages(message => { | 
					
						
							| 
									
										
										
										
											2018-08-01 09:26:02 +02:00
										 |  |  |    if (message.type === 'refresh-tree') { | 
					
						
							|  |  |  |        reload(); | 
					
						
							|  |  |  |    } | 
					
						
							| 
									
										
										
										
											2019-06-23 13:25:00 +02:00
										 |  |  |    else if (message.type === 'open-note') { | 
					
						
							| 
									
										
										
										
											2019-07-07 11:15:55 +02:00
										 |  |  |        noteDetailService.activateOrOpenNote(message.noteId); | 
					
						
							| 
									
										
										
										
											2019-06-23 13:25:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |        if (utils.isElectron()) { | 
					
						
							|  |  |  |            const currentWindow = require("electron").remote.getCurrentWindow(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            currentWindow.show(); | 
					
						
							|  |  |  |        } | 
					
						
							|  |  |  |    } | 
					
						
							| 
									
										
										
										
											2018-08-01 09:26:02 +02:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 17:49:58 +02:00
										 |  |  | // this is a synchronous handler - it returns only once the data has been updated
 | 
					
						
							|  |  |  | ws.subscribeToOutsideSyncMessages(async syncData => { | 
					
						
							| 
									
										
										
										
											2019-10-20 13:09:00 +02:00
										 |  |  |     const noteIdsToRefresh = new Set(); | 
					
						
							| 
									
										
										
										
											2019-10-20 12:29:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 13:09:00 +02:00
										 |  |  |     // this has the problem that the former parentNoteId might not be invalidated
 | 
					
						
							|  |  |  |     // and the former location of the branch/note won't be removed.
 | 
					
						
							|  |  |  |     syncData.filter(sync => sync.entityName === 'branches').forEach(sync => noteIdsToRefresh.add(sync.parentNoteId)); | 
					
						
							| 
									
										
										
										
											2018-03-25 21:16:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 17:49:58 +02:00
										 |  |  |     syncData.filter(sync => sync.entityName === 'notes').forEach(sync => noteIdsToRefresh.add(sync.entityId)); | 
					
						
							| 
									
										
										
										
											2018-03-25 21:16:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 13:09:00 +02:00
										 |  |  |     syncData.filter(sync => sync.entityName === 'note_reordering').forEach(sync => noteIdsToRefresh.add(sync.entityId)); | 
					
						
							| 
									
										
										
										
											2019-10-20 12:29:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-02 08:04:22 +01:00
										 |  |  |     syncData.filter(sync => sync.entityName === 'attributes').forEach(sync => { | 
					
						
							|  |  |  |         const note = treeCache.notes[sync.noteId]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-02 22:27:06 +01:00
										 |  |  |         if (note && note.__attributeCache) { | 
					
						
							| 
									
										
										
										
											2019-11-02 08:04:22 +01:00
										 |  |  |             noteIdsToRefresh.add(sync.entityId); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-20 17:49:58 +02:00
										 |  |  |     if (noteIdsToRefresh.size > 0) { | 
					
						
							|  |  |  |         await reloadNotes(Array.from(noteIdsToRefresh)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-25 21:16:57 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-24 10:40:18 +01:00
										 |  |  | keyboardActionService.setGlobalActionHandler('CreateNoteAfter', async () => { | 
					
						
							| 
									
										
										
										
											2020-01-12 11:15:23 +01:00
										 |  |  |     const node = appContext.getMainNoteTree().getActiveNode(); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     const parentNoteId = node.data.parentNoteId; | 
					
						
							| 
									
										
										
										
											2019-04-16 21:40:04 +02:00
										 |  |  |     const isProtected = await treeUtils.getParentProtectedStatus(node); | 
					
						
							| 
									
										
										
										
											2017-12-18 23:41:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-02 18:59:08 +01:00
										 |  |  |     if (node.data.noteId === 'root' || node.data.noteId === await hoistedNoteService.getHoistedNoteId()) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-29 23:24:41 +01:00
										 |  |  |     await createNote(node, parentNoteId, 'after', { | 
					
						
							|  |  |  |         isProtected: isProtected, | 
					
						
							|  |  |  |         saveSelection: true | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2017-12-18 23:41:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-01 22:59:51 +01:00
										 |  |  | async function createNoteInto(saveSelection = false) { | 
					
						
							| 
									
										
										
										
											2020-01-12 11:15:23 +01:00
										 |  |  |     const node = appContext.getMainNoteTree().getActiveNode(); | 
					
						
							| 
									
										
										
										
											2017-12-18 23:41:13 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-06 13:14:33 +02:00
										 |  |  |     if (node) { | 
					
						
							|  |  |  |         await createNote(node, node.data.noteId, 'into', { | 
					
						
							|  |  |  |             isProtected: node.data.isProtected, | 
					
						
							| 
									
										
										
										
											2020-01-01 22:59:51 +01:00
										 |  |  |             saveSelection: saveSelection | 
					
						
							| 
									
										
										
										
											2019-07-06 13:14:33 +02:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-09-06 22:58:46 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-26 22:50:46 +02:00
										 |  |  | async function reloadNotes(noteIds, activateNotePath = null) { | 
					
						
							| 
									
										
										
										
											2019-10-20 17:49:58 +02:00
										 |  |  |     if (noteIds.length === 0) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-26 09:51:08 +02:00
										 |  |  |     await treeCache.reloadNotes(noteIds); | 
					
						
							| 
									
										
										
										
											2019-10-20 12:29:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-26 22:50:46 +02:00
										 |  |  |     if (!activateNotePath) { | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  |         activateNotePath = appContext.getActiveTabNotePath(); | 
					
						
							| 
									
										
										
										
											2019-10-26 22:50:46 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-13 22:10:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  |     appContext.trigger('notesReloaded', { noteIds, activateNotePath }); | 
					
						
							| 
									
										
										
										
											2019-03-18 22:33:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-01 22:59:51 +01:00
										 |  |  | window.glob.cutIntoNote = () => createNoteInto(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | keyboardActionService.setGlobalActionHandler('CutIntoNote', () => createNoteInto(true)); | 
					
						
							| 
									
										
										
										
											2018-09-06 22:58:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-24 10:40:18 +01:00
										 |  |  | keyboardActionService.setGlobalActionHandler('CreateNoteInto', createNoteInto); | 
					
						
							| 
									
										
										
										
											2017-12-17 16:56:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 11:15:23 +01:00
										 |  |  | keyboardActionService.setGlobalActionHandler('ScrollToActiveNote', () => appContext.getMainNoteTree().scrollToActiveNote()); | 
					
						
							| 
									
										
										
										
											2017-12-19 19:54:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-16 21:40:04 +02:00
										 |  |  | $(window).bind('hashchange', async function() { | 
					
						
							| 
									
										
										
										
											2019-01-25 22:18:34 +01:00
										 |  |  |     if (isNotePathInAddress()) { | 
					
						
							| 
									
										
										
										
											2019-05-14 22:29:47 +02:00
										 |  |  |         const [notePath, tabId] = getHashValueFromAddress(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-12 12:30:30 +01:00
										 |  |  |         appContext.switchToTab(tabId, notePath); | 
					
						
							| 
									
										
										
										
											2017-12-19 19:54:55 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2017-12-19 19:54:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-19 12:36:16 +02:00
										 |  |  | async function duplicateNote(noteId, parentNoteId) { | 
					
						
							|  |  |  |     const {note} = await server.post(`notes/${noteId}/duplicate/${parentNoteId}`); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await reload(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await activateNote(note.noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const origNote = await treeCache.getNote(noteId); | 
					
						
							| 
									
										
										
										
											2019-10-20 10:00:18 +02:00
										 |  |  |     toastService.showMessage(`Note "${origNote.title}" has been duplicated`); | 
					
						
							| 
									
										
										
										
											2019-10-19 12:36:16 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-29 21:10:28 +02:00
										 |  |  | frontendLoaded.then(bundle.executeStartupBundles); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | export default { | 
					
						
							|  |  |  |     reload, | 
					
						
							|  |  |  |     setProtected, | 
					
						
							| 
									
										
										
										
											2018-08-23 12:55:45 +02:00
										 |  |  |     activateNote, | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     setNoteTitle, | 
					
						
							| 
									
										
										
										
											2018-03-26 21:50:47 -04:00
										 |  |  |     setPrefix, | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     createNote, | 
					
						
							| 
									
										
										
										
											2018-03-26 22:29:14 -04:00
										 |  |  |     sortAlphabetically, | 
					
						
							| 
									
										
										
										
											2020-01-03 21:15:45 +01:00
										 |  |  |     loadTreeData, | 
					
						
							| 
									
										
										
										
											2018-12-24 10:10:36 +01:00
										 |  |  |     treeInitialized, | 
					
						
							| 
									
										
										
										
											2019-01-25 22:18:34 +01:00
										 |  |  |     setExpandedToServer, | 
					
						
							| 
									
										
										
										
											2019-10-20 12:29:34 +02:00
										 |  |  |     reloadNotes, | 
					
						
							| 
									
										
										
										
											2019-05-19 18:21:29 +02:00
										 |  |  |     resolveNotePath, | 
					
						
							| 
									
										
										
										
											2019-06-16 11:12:07 +02:00
										 |  |  |     getSomeNotePath, | 
					
						
							| 
									
										
										
										
											2020-01-11 21:19:56 +01:00
										 |  |  |     createNewTopLevelNote, | 
					
						
							| 
									
										
										
										
											2020-01-03 21:15:45 +01:00
										 |  |  |     duplicateNote, | 
					
						
							| 
									
										
										
										
											2020-01-12 11:15:23 +01:00
										 |  |  |     getRunPath | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | }; |