trilium/public/javascripts/dialogs/add_link.js

77 lines
2 KiB
JavaScript
Raw Normal View History

"use strict";
2017-11-05 02:43:20 +08:00
const addLink = (function() {
const dialogEl = $("#insert-link-dialog");
const formEl = $("#insert-link-form");
const autoCompleteEl = $("#note-autocomplete");
const noteDetailEl = $('#note-detail');
const linkTitleEl = $("#link-title");
function showDialog() {
2017-11-05 05:03:15 +08:00
glob.activeDialog = dialogEl;
2017-11-05 02:43:20 +08:00
noteDetailEl.summernote('editor.saveRange');
dialogEl.dialog({
modal: true,
width: 500
});
2017-11-05 02:43:20 +08:00
autoCompleteEl.val('').focus();
linkTitleEl.val('');
2017-11-05 02:43:20 +08:00
function setDefaultLinkTitle(noteId) {
2017-11-05 10:18:36 +08:00
const noteTitle = treeUtils.getNoteTitle(noteId);
2017-11-05 02:43:20 +08:00
linkTitleEl.val(noteTitle);
}
2017-11-05 02:43:20 +08:00
autoCompleteEl.autocomplete({
2017-11-20 08:39:39 +08:00
source: noteTree.getAutocompleteItems(),
2017-11-05 02:43:20 +08:00
minLength: 0,
change: () => {
const val = autoCompleteEl.val();
2017-11-20 09:36:13 +08:00
const noteId = link.getNodePathFromLabel(val);
2017-11-05 02:43:20 +08:00
if (noteId) {
setDefaultLinkTitle(noteId);
}
},
// this is called when user goes through autocomplete list with keyboard
2017-11-21 12:51:28 +08:00
// at this point the item isn't selected yet so we use supplied ui.item to see WHERE the cursor is
2017-11-05 02:43:20 +08:00
focus: (event, ui) => {
2017-11-20 09:36:13 +08:00
const noteId = link.getNodePathFromLabel(ui.item.value);
setDefaultLinkTitle(noteId);
}
2017-11-05 02:43:20 +08:00
});
}
2017-11-05 02:43:20 +08:00
formEl.submit(() => {
2017-11-20 11:31:30 +08:00
const value = autoCompleteEl.val();
2017-11-20 11:31:30 +08:00
const notePath = link.getNodePathFromLabel(value);
2017-11-20 11:31:30 +08:00
if (notePath) {
2017-11-05 02:43:20 +08:00
const linkTitle = linkTitleEl.val();
2017-11-05 02:43:20 +08:00
dialogEl.dialog("close");
2017-11-05 02:43:20 +08:00
noteDetailEl.summernote('editor.restoreRange');
2017-11-05 02:43:20 +08:00
noteDetailEl.summernote('createLink', {
text: linkTitle,
2017-11-20 11:31:30 +08:00
url: 'app#' + notePath,
2017-11-05 02:43:20 +08:00
isNewWindow: true
});
}
2017-11-05 02:43:20 +08:00
return false;
});
2017-11-05 02:43:20 +08:00
$(document).bind('keydown', 'alt+l', showDialog);
return {
showDialog
};
})();