Skip to content

Build your personal knowledge base with Trilium Notes

abstracted note detail components

azivner913b6bb6f670
diff --git a/src/public/javascripts/services/note_detail.js b/src/public/javascripts/services/note_detail.js
index 37a2897e65..081e6d3f3d 100644
--- a/src/public/javascripts/services/note_detail.js
+++ b/src/public/javascripts/services/note_detail.js
@@ -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 => {
diff --git a/src/public/javascripts/services/note_detail_attachment.js b/src/public/javascripts/services/note_detail_attachment.js
index 11298467df..f6a9ceb71a 100644
--- a/src/public/javascripts/services/note_detail_attachment.js
+++ b/src/public/javascripts/services/note_detail_attachment.js
@@ -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
}
\ No newline at end of file
diff --git a/src/public/javascripts/services/note_detail_code.js b/src/public/javascripts/services/note_detail_code.js
index 00326f146c..b92c1d77ef 100644
--- a/src/public/javascripts/services/note_detail_code.js
+++ b/src/public/javascripts/services/note_detail_code.js
@@ -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
}
\ No newline at end of file
diff --git a/src/public/javascripts/services/node_detail_render.js b/src/public/javascripts/services/note_detail_render.js
similarity index 89%
rename from src/public/javascripts/services/node_detail_render.js
rename to src/public/javascripts/services/note_detail_render.js
index d26b583cf2..75b0018753 100644
--- a/src/public/javascripts/services/node_detail_render.js
+++ b/src/public/javascripts/services/note_detail_render.js
@@ -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
}
\ No newline at end of file
diff --git a/src/public/javascripts/services/node_detail_search.js b/src/public/javascripts/services/note_detail_search.js
similarity index 92%
rename from src/public/javascripts/services/node_detail_search.js
rename to src/public/javascripts/services/note_detail_search.js
index abc82295a3..0b1b8306d2 100644
--- a/src/public/javascripts/services/node_detail_search.js
+++ b/src/public/javascripts/services/note_detail_search.js
@@ -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
}
\ 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 8ad913b0f2..18c691bb56 100644
--- a/src/public/javascripts/services/note_detail_text.js
+++ b/src/public/javascripts/services/note_detail_text.js
@@ -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
diff --git a/src/public/javascripts/services/server.js b/src/public/javascripts/services/server.js
index 947ab865bc..c8aaf955ff 100644
--- a/src/public/javascripts/services/server.js
+++ b/src/public/javascripts/services/server.js
@@ -69,7 +69,12 @@ async function ajax(url, method, data) {
};
if (data) {
- options.data = JSON.stringify(data);
+ try {
+ options.data = JSON.stringify(data);
+ }
+ catch (e) {
+ console.log("Can't stringify data: ", data, " because of error: ", e)
+ }
options.contentType = "application/json";
}