diff --git a/src/public/javascripts/services/note_detail.js b/src/public/javascripts/services/note_detail.js index 5506c96d3..c17223b69 100644 --- a/src/public/javascripts/services/note_detail.js +++ b/src/public/javascripts/services/note_detail.js @@ -187,6 +187,12 @@ async function loadNoteDetail(noteId) { noteTypeService.setNoteType(currentNote.type); noteTypeService.setNoteMime(currentNote.mime); + for (const componentType in components) { + if (componentType !== currentNote.type) { + components[componentType].cleanup(); + } + } + $noteDetailComponents.hide(); const newSessionCreated = await handleProtectedSession(); diff --git a/src/public/javascripts/services/note_detail_code.js b/src/public/javascripts/services/note_detail_code.js index 4ed8452f3..2c5551f4b 100644 --- a/src/public/javascripts/services/note_detail_code.js +++ b/src/public/javascripts/services/note_detail_code.js @@ -97,5 +97,10 @@ export default { show, getContent, focus, - onNoteChange -} \ No newline at end of file + onNoteChange, + cleanup: () => { + if (codeEditor) { + codeEditor.setValue(''); + } + } +} diff --git a/src/public/javascripts/services/note_detail_file.js b/src/public/javascripts/services/note_detail_file.js index d90ce502b..c71443220 100644 --- a/src/public/javascripts/services/note_detail_file.js +++ b/src/public/javascripts/services/note_detail_file.js @@ -47,5 +47,6 @@ export default { show, getContent: () => null, focus: () => null, - onNoteChange: () => null + onNoteChange: () => null, + cleanup: () => null } \ No newline at end of file diff --git a/src/public/javascripts/services/note_detail_relation_map.js b/src/public/javascripts/services/note_detail_relation_map.js index 2b5c286ab..882e0ae25 100644 --- a/src/public/javascripts/services/note_detail_relation_map.js +++ b/src/public/javascripts/services/note_detail_relation_map.js @@ -7,6 +7,7 @@ import treeService from "./tree.js"; const $noteDetailRelationMap = $("#note-detail-relation-map"); const $relationMapCanvas = $("#relation-map-canvas"); const $addChildNotesButton = $("#relation-map-add-child-notes"); +const $createChildNote = $("#relation-map-create-child-note"); const $zoomInButton = $("#relation-map-zoom-in"); const $zoomOutButton = $("#relation-map-zoom-out"); @@ -64,7 +65,12 @@ async function show() { loadMapData(); - jsPlumb.ready(initJsPlumb); + jsPlumb.ready(() => { + initJsPlumbInstance(); + + loadNotesAndRelations(); + }); + } async function loadNotesAndRelations() { @@ -142,7 +148,23 @@ function initPanZoom() { $zoomOutButton.click(() => pz.zoomTo(0, 0, 0.8)); } -async function initJsPlumb () { +function cleanup() { + if (instance) { + // delete all endpoints and connections + instance.deleteEveryEndpoint(); + + // without this we still end up with note boxes remaining in the canvas + $relationMapCanvas.empty(); + } +} + +function initJsPlumbInstance () { + if (instance) { + cleanup(); + + return; + } + instance = jsPlumb.getInstance({ Endpoint: ["Dot", {radius: 2}], Connector: "StateMachine", @@ -183,13 +205,9 @@ async function initJsPlumb () { $relationMapCanvas.contextmenuRelation("open", e, { connection: c }); }); - await loadNotesAndRelations(); - // so that canvas is not panned when clicking/dragging note box $relationMapCanvas.on('mousedown touchstart', '.note-box, .connection-label', e => e.stopPropagation()); - jsPlumb.fire("jsPlumbDemoLoaded", instance); - initPanZoom(); } @@ -372,9 +390,29 @@ $addChildNotesButton.click(async () => { await loadNotesAndRelations(); }); +$createChildNote.click(async () => { + const title = prompt("Enter title of new note", "new note"); + + if (!title.trim()) { + return; + } + + const {note} = await server.post(`notes/${noteDetailService.getCurrentNoteId()}/children`, { + title, + target: 'into' + }); + + const [x, y] = getFreePosition(); + + mapData.notes.push({ id: note.noteId, x, y }); + + await createNoteBox(id, title, x, y); +}); + export default { show, getContent: () => JSON.stringify(mapData), focus: () => null, - onNoteChange: () => null + onNoteChange: () => null, + cleanup } \ No newline at end of file diff --git a/src/public/javascripts/services/note_detail_render.js b/src/public/javascripts/services/note_detail_render.js index 281e2ff56..f9900bfc3 100644 --- a/src/public/javascripts/services/note_detail_render.js +++ b/src/public/javascripts/services/note_detail_render.js @@ -35,5 +35,6 @@ export default { show: render, getContent: () => "", focus: () => null, - onNoteChange: () => null + onNoteChange: () => null, + cleanup: () => $noteDetailRenderContent.empty() } \ No newline at end of file diff --git a/src/public/javascripts/services/note_detail_search.js b/src/public/javascripts/services/note_detail_search.js index 05b63e524..8d85227b8 100644 --- a/src/public/javascripts/services/note_detail_search.js +++ b/src/public/javascripts/services/note_detail_search.js @@ -29,5 +29,6 @@ export default { getContent, show, focus: () => null, - onNoteChange: () => null + onNoteChange: () => null, + cleanup: () => null } \ No newline at end of file diff --git a/src/public/javascripts/services/note_detail_text.js b/src/public/javascripts/services/note_detail_text.js index b6f970834..24d56a02e 100644 --- a/src/public/javascripts/services/note_detail_text.js +++ b/src/public/javascripts/services/note_detail_text.js @@ -111,5 +111,10 @@ export default { getEditor, getContent, focus, - onNoteChange + onNoteChange, + cleanup: () => { + if (textEditor) { + textEditor.setData(''); + } + } } \ No newline at end of file diff --git a/src/services/notes.js b/src/services/notes.js index b48506bb0..1675cf871 100644 --- a/src/services/notes.js +++ b/src/services/notes.js @@ -49,6 +49,18 @@ async function createNewNote(parentNoteId, noteData) { const parentNote = await repository.getNote(parentNoteId); + if (!noteData.type) { + if (parentNote.type === 'text' || parentNote.type === 'code') { + noteData.type = parentNote.type; + noteData.mime = parentNote.mime; + } + else { + // inheriting note type makes sense only for text and code + noteData.type = 'text'; + noteData.mime = 'text/html'; + } + } + noteData.type = noteData.type || parentNote.type; noteData.mime = noteData.mime || parentNote.mime; diff --git a/src/views/index.ejs b/src/views/index.ejs index 2d3af3197..00d880b94 100644 --- a/src/views/index.ejs +++ b/src/views/index.ejs @@ -263,7 +263,11 @@