some refactorings of note detail service

This commit is contained in:
azivner 2018-03-26 23:48:45 -04:00
parent 9c1b8da573
commit 7e856283ee
7 changed files with 103 additions and 89 deletions

View File

@ -84,18 +84,21 @@ function registerEntrypoints() {
} }
}); });
// FIXME: do we really need these at this point?
utils.bindShortcut("ctrl+shift+up", () => { utils.bindShortcut("ctrl+shift+up", () => {
const node = treeService.getCurrentNode(); const node = treeService.getCurrentNode();
node.navigate($.ui.keyCode.UP, true); node.navigate($.ui.keyCode.UP, true);
$("#note-detail").focus(); $("#note-detail-text").focus();
}); });
// FIXME: do we really need these at this point?
utils.bindShortcut("ctrl+shift+down", () => { utils.bindShortcut("ctrl+shift+down", () => {
const node = treeService.getCurrentNode(); const node = treeService.getCurrentNode();
node.navigate($.ui.keyCode.DOWN, true); node.navigate($.ui.keyCode.DOWN, true);
$("#note-detail").focus(); $("#note-detail-text").focus();
}); });
$(document).bind('keydown', 'ctrl+-', () => { $(document).bind('keydown', 'ctrl+-', () => {
@ -120,7 +123,7 @@ function registerEntrypoints() {
} }
}); });
$("#note-title").bind('keydown', 'return', () => $("#note-detail").focus()); $("#note-title").bind('keydown', 'return', () => $("#note-detail-text").focus());
$("#upload-attachment-button").click(attachmentService.uploadAttachment); $("#upload-attachment-button").click(attachmentService.uploadAttachment);
} }

View File

@ -92,7 +92,7 @@ function addTextToEditor(text) {
// of opening the link in new window/tab // of opening the link in new window/tab
$(document).on('click', "a[action='note']", goToLink); $(document).on('click', "a[action='note']", goToLink);
$(document).on('click', 'div.popover-content a, div.ui-tooltip-content a', goToLink); $(document).on('click', 'div.popover-content a, div.ui-tooltip-content a', goToLink);
$(document).on('dblclick', '#note-detail a', goToLink); $(document).on('dblclick', '#note-detail-text a', goToLink);
export default { export default {
getNodePathFromLabel, getNodePathFromLabel,

View File

@ -12,7 +12,7 @@ import NoteFull from "../entities/note_full.js";
const $noteTitle = $("#note-title"); const $noteTitle = $("#note-title");
const $noteDetail = $('#note-detail'); const $noteDetailText = $('#note-detail-text');
const $noteDetailCode = $('#note-detail-code'); const $noteDetailCode = $('#note-detail-code');
const $noteDetailSearch = $('#note-detail-search'); const $noteDetailSearch = $('#note-detail-search');
const $noteDetailRender = $('#note-detail-render'); const $noteDetailRender = $('#note-detail-render');
@ -33,7 +33,7 @@ const $searchString = $("#search-string");
const $executeScriptButton = $("#execute-script-button"); const $executeScriptButton = $("#execute-script-button");
let editor = null; let textEditor = null;
let codeEditor = null; let codeEditor = null;
let currentNote = null; let currentNote = null;
@ -90,7 +90,7 @@ async function saveNoteIfChanged() {
function updateNoteFromInputs(note) { function updateNoteFromInputs(note) {
if (note.type === 'text') { if (note.type === 'text') {
let content = editor.getData(); let content = textEditor.getData();
// if content is only tags/whitespace (typically <p>&nbsp;</p>), then just make it empty // if content is only tags/whitespace (typically <p>&nbsp;</p>), then just make it empty
// this is important when setting new note to code // this is important when setting new note to code
@ -144,22 +144,43 @@ function newNoteCreated() {
isNewNoteCreated = true; isNewNoteCreated = true;
} }
async function setContent(content) { async function showRenderNote() {
if (currentNote.type === 'text') { $noteDetailRender.show();
if (!editor) {
const bundle = await server.get('script/bundle/' + getCurrentNoteId());
$noteDetailRender.html(bundle.html);
bundleService.executeBundle(bundle);
}
async function showFileNote() {
const labels = await server.get('notes/' + currentNote.noteId + '/labels');
const labelMap = utils.toObject(labels, l => [l.name, l.value]);
$noteDetailAttachment.show();
$attachmentFileName.text(labelMap.original_file_name);
$attachmentFileSize.text(labelMap.file_size + " bytes");
$attachmentFileType.text(currentNote.mime);
}
async function showTextNote() {
if (!textEditor) {
await utils.requireLibrary(utils.CKEDITOR); await utils.requireLibrary(utils.CKEDITOR);
editor = await BalloonEditor.create($noteDetail[0], {}); textEditor = await BalloonEditor.create($noteDetailText[0], {});
editor.document.on('change', noteChanged); textEditor.document.on('change', noteChanged);
} }
// temporary workaround for https://github.com/ckeditor/ckeditor5-enter/issues/49 // temporary workaround for https://github.com/ckeditor/ckeditor5-enter/issues/49
editor.setData(content ? content : "<p></p>"); textEditor.setData(currentNote.content || "<p></p>");
$noteDetail.show(); $noteDetailText.show();
} }
else if (currentNote.type === 'code') {
async function showCodeNote() {
if (!codeEditor) { if (!codeEditor) {
await utils.requireLibrary(utils.CODE_MIRROR); await utils.requireLibrary(utils.CODE_MIRROR);
@ -173,8 +194,8 @@ async function setContent(content) {
viewportMargin: Infinity, viewportMargin: Infinity,
indentUnit: 4, indentUnit: 4,
matchBrackets: true, matchBrackets: true,
matchTags: { bothTags: true }, matchTags: {bothTags: true},
highlightSelectionMatches: { showToken: /\w/, annotateScrollbar: false }, highlightSelectionMatches: {showToken: /\w/, annotateScrollbar: false},
lint: true, lint: true,
gutters: ["CodeMirror-lint-markers"], gutters: ["CodeMirror-lint-markers"],
lineNumbers: true lineNumbers: true
@ -186,7 +207,7 @@ async function setContent(content) {
$noteDetailCode.show(); $noteDetailCode.show();
// this needs to happen after the element is shown, otherwise the editor won't be refresheds // this needs to happen after the element is shown, otherwise the editor won't be refresheds
codeEditor.setValue(content); codeEditor.setValue(currentNote.content);
const info = CodeMirror.findModeByMIME(currentNote.mime); const info = CodeMirror.findModeByMIME(currentNote.mime);
@ -196,12 +217,13 @@ async function setContent(content) {
} }
codeEditor.refresh(); codeEditor.refresh();
} }
else if (currentNote.type === 'search') {
function showSearchNote() {
$noteDetailSearch.show(); $noteDetailSearch.show();
try { try {
const json = JSON.parse(content); const json = JSON.parse(currentNote.content);
$searchString.val(json.searchString); $searchString.val(json.searchString);
} }
@ -211,7 +233,6 @@ async function setContent(content) {
} }
$searchString.on('input', noteChanged); $searchString.on('input', noteChanged);
}
} }
async function loadNoteToEditor(noteId) { async function loadNoteToEditor(noteId) {
@ -244,33 +265,26 @@ async function loadNoteToEditor(noteId) {
noteTypeService.setNoteType(currentNote.type); noteTypeService.setNoteType(currentNote.type);
noteTypeService.setNoteMime(currentNote.mime); noteTypeService.setNoteMime(currentNote.mime);
$noteDetail.hide(); $noteDetailText.hide();
$noteDetailSearch.hide(); $noteDetailSearch.hide();
$noteDetailCode.hide(); $noteDetailCode.hide();
$noteDetailRender.html('').hide(); $noteDetailRender.html('').hide();
$noteDetailAttachment.hide(); $noteDetailAttachment.hide();
if (currentNote.type === 'render') { if (currentNote.type === 'render') {
$noteDetailRender.show(); await showRenderNote();
const bundle = await server.get('script/bundle/' + getCurrentNoteId());
$noteDetailRender.html(bundle.html);
bundleService.executeBundle(bundle);
} }
else if (currentNote.type === 'file') { else if (currentNote.type === 'file') {
const labels = await server.get('notes/' + currentNote.noteId + '/labels'); await showFileNote();
const labelMap = utils.toObject(labels, l => [l.name, l.value]);
$noteDetailAttachment.show();
$attachmentFileName.text(labelMap.original_file_name);
$attachmentFileSize.text(labelMap.file_size + " bytes");
$attachmentFileType.text(currentNote.mime);
} }
else { else if (currentNote.type === 'text') {
await setContent(currentNote.content); await showTextNote();
}
else if (currentNote.type === 'code') {
await showCodeNote();
}
else if (currentNote.type === 'search') {
showSearchNote();
} }
noteChangeDisabled = false; noteChangeDisabled = false;
@ -310,14 +324,14 @@ async function loadNote(noteId) {
} }
function getEditor() { function getEditor() {
return editor; return textEditor;
} }
function focus() { function focus() {
const note = getCurrentNote(); const note = getCurrentNote();
if (note.type === 'text') { if (note.type === 'text') {
$noteDetail.focus(); $noteDetailText.focus();
} }
else if (note.type === 'code') { else if (note.type === 'code') {
codeEditor.focus(); codeEditor.focus();
@ -392,7 +406,7 @@ $(document).ready(() => {
}); });
// so that tab jumps from note title (which has tabindex 1) // so that tab jumps from note title (which has tabindex 1)
$noteDetail.attr("tabindex", 2); $noteDetailText.attr("tabindex", 2);
}); });
// this makes sure that when user e.g. reloads the page or navigates away from the page, the note's content is saved // this makes sure that when user e.g. reloads the page or navigates away from the page, the note's content is saved
@ -408,7 +422,6 @@ setInterval(saveNoteIfChanged, 5000);
export default { export default {
reload, reload,
switchToNote, switchToNote,
saveNoteIfChanged,
updateNoteFromInputs, updateNoteFromInputs,
saveNoteToServer, saveNoteToServer,
setNoteBackgroundIfProtected, setNoteBackgroundIfProtected,
@ -419,7 +432,5 @@ export default {
newNoteCreated, newNoteCreated,
getEditor, getEditor,
focus, focus,
executeCurrentNote, loadLabelList
loadLabelList,
setContent
}; };

View File

@ -4,7 +4,7 @@ import linkService from "./link.js";
function setupTooltip() { function setupTooltip() {
$(document).tooltip({ $(document).tooltip({
items: "#note-detail a", items: "#note-detail-text a",
content: function (callback) { content: function (callback) {
const notePath = linkService.getNotePathFromLink($(this).attr("href")); const notePath = linkService.getNotePathFromLink($(this).attr("href"));

View File

@ -276,7 +276,7 @@ async function treeInitialized() {
} }
if (startNotePath) { if (startNotePath) {
activateNode(startNotePath); await activateNode(startNotePath);
// looks like this this doesn't work when triggered immediatelly after activating node // looks like this this doesn't work when triggered immediatelly after activating node
// so waiting a second helps // so waiting a second helps
@ -322,7 +322,7 @@ function initFancyTree(branch) {
}, },
expand: (event, data) => setExpandedToServer(data.node.data.branchId, true), expand: (event, data) => setExpandedToServer(data.node.data.branchId, true),
collapse: (event, data) => setExpandedToServer(data.node.data.branchId, false), collapse: (event, data) => setExpandedToServer(data.node.data.branchId, false),
init: (event, data) => treeInitialized, init: (event, data) => treeInitialized(), // don't collapse to short form
hotkeys: { hotkeys: {
keydown: treeKeyBindings keydown: treeKeyBindings
}, },

View File

@ -28,7 +28,7 @@
align-items: center; align-items: center;
} }
#note-detail { #note-detail-text {
border: 0 !important; border: 0 !important;
box-shadow: none !important; box-shadow: none !important;
/* This is because with empty content height of editor is 0 and it's impossible to click into it */ /* This is because with empty content height of editor is 0 and it's impossible to click into it */
@ -40,7 +40,7 @@
background-color: #eee; background-color: #eee;
} }
#note-detail p { #note-detail-text p {
padding: 0; padding: 0;
margin: 0; margin: 0;
} }

View File

@ -133,7 +133,7 @@
</div> </div>
<div style="position: relative; overflow: auto; grid-area: note-content; padding-left: 10px; padding-top: 10px;" id="note-detail-wrapper"> <div style="position: relative; overflow: auto; grid-area: note-content; padding-left: 10px; padding-top: 10px;" id="note-detail-wrapper">
<div id="note-detail" style="display: none;"></div> <div id="note-detail-text" style="display: none;"></div>
<div id="note-detail-search" style="display: none;"> <div id="note-detail-search" style="display: none;">
<div style="display: flex; align-items: center;"> <div style="display: flex; align-items: center;">