recent notes and insert link now include whole note path

This commit is contained in:
azivner 2017-09-03 15:08:17 -04:00
parent 8a388842aa
commit 81c534104f
4 changed files with 68 additions and 25 deletions

View file

@ -97,7 +97,7 @@
</select>
</div>
<div id="insertLinkDialog" title="Recent notes" style="display: none;">
<div id="insertLinkDialog" title="Insert link" style="display: none;">
<form id="insertLinkForm">
<div class="form-group">
<label for="noteAutocomplete">Link to note</label>
@ -109,7 +109,7 @@
<input id="linkTitle" style="width: 100%;">
</div>
<button id="addLinkButton" type="submit" class="btn btn-sm">Add link</button>
<button id="addLinkButton" class="btn btn-sm">Add link</button>
</form>
</div>

View file

@ -29,7 +29,8 @@ $(document).bind('keydown', 'alt+h', function() {
$(document).bind('keydown', 'alt+q', function() {
$("#recentNotesDialog").dialog({
modal: true
modal: true,
width: 500
});
let recentNotesSelectBox = $('#recentNotesSelectBox');
@ -40,7 +41,7 @@ $(document).bind('keydown', 'alt+q', function() {
let recNotes = recentNotes.filter(note => note !== globalNote.detail.note_id);
$.each(recNotes, function(key, valueNoteId) {
let noteTitle = globalNoteNames[valueNoteId];
let noteTitle = getFullName(valueNoteId);
if (!noteTitle) {
return;
@ -62,7 +63,7 @@ $(document).bind('keydown', 'alt+q', function() {
function setActiveNoteBasedOnRecentNotes() {
let noteId = $("#recentNotesSelectBox option:selected").val();
$("#tree").fancytree('getNodeByKey', noteId).setActive();
getNodeByKey(noteId).setActive();
$("#recentNotesDialog").dialog('close');
}
@ -90,12 +91,22 @@ $(document).on('click', 'div.popover-content a', function(e) {
if (noteIdMatch !== null) {
const noteId = noteIdMatch[1];
$("#tree").fancytree('getNodeByKey', noteId).setActive();
getNodeByKey(noteId).setActive();
e.preventDefault();
}
});
function getNodeIdFromLabel(label) {
const noteIdMatch = / \(([A-Za-z0-9]{22})\)/.exec(label);
if (noteIdMatch !== null) {
return noteIdMatch[1];
}
return null;
}
$(document).bind('keydown', 'alt+l', function() {
var range = $('#noteDetail').summernote('createRange');
console.log("range:", range);
@ -107,43 +118,52 @@ $(document).bind('keydown', 'alt+l', function() {
noteDetail.summernote('editor.saveRange');
$("#insertLinkDialog").dialog({
modal: true
modal: true,
width: 500
});
let autocompleteItems = [];
for (let noteId in globalNoteNames) {
let fullName = getFullName(noteId);
autocompleteItems.push({
value: globalNoteNames[noteId] + " (" + noteId + ")",
label: globalNoteNames[noteId]
value: fullName + " (" + noteId + ")",
label: fullName
});
}
function setDefaultLinkTitle() {
const val = $("#noteAutocomplete").val();
const noteId = getNodeIdFromLabel(val);
if (noteId) {
const note = getNodeByKey(noteId);
let noteTitle = note.title;
if (noteTitle.endsWith(" (clone)")) {
noteTitle = noteTitle.substr(0, noteTitle.length - 8);
}
$("#linkTitle").val(noteTitle);
}
}
$("#noteAutocomplete").autocomplete({
source: autocompleteItems,
minLength: 0,
change: function() {
let val = $("#noteAutocomplete").val();
val = val.replace(/ \([A-Za-z0-9]{22}\)/, "");
$("#linkTitle").val(val);
},
focus: function(event, ui) {
$("#linkTitle").val(ui.item.label);
}
change: setDefaultLinkTitle,
focus: setDefaultLinkTitle
});
});
$("#insertLinkForm").submit(function addLink() {
let val = $("#noteAutocomplete").val();
const noteIdMatch = / \(([A-Za-z0-9]{22})\)/.exec(val);
const noteId = getNodeIdFromLabel(val);
if (noteIdMatch !== null) {
const noteId = noteIdMatch[1];
if (noteId) {
const linkTitle = $("#linkTitle").val();
const noteDetail = $('#noteDetail');
$("#insertLinkDialog").dialog("close");

View file

@ -38,7 +38,7 @@ function saveNoteIfChanged(callback) {
let title = $('#noteTitle').val();
$("#tree").fancytree('getNodeByKey', note.detail.note_id).setTitle(title);
getNodeByKey(note.detail.note_id).setTitle(title);
note.detail.note_title = title;

View file

@ -104,6 +104,28 @@ const keybindings = {
const globalNoteNames = {};
let globalTree;
function getNodeByKey(noteId) {
return globalTree.fancytree('getNodeByKey', noteId);
}
function getFullName(noteId) {
let note = getNodeByKey(noteId);
const path = [];
while (note) {
path.push(note.title);
note = note.getParent();
}
// remove "root" element
path.pop();
return path.reverse().join(" > ");
}
$(function(){
$.get(baseUrl + 'tree').then(resp => {
const notes = resp.notes;
@ -145,7 +167,8 @@ $(function(){
});
}
$("#tree").fancytree({
globalTree = $("#tree");
globalTree.fancytree({
autoScroll: true,
extensions: ["hotkeys", "filter"],
source: notes,