add link converted to module

This commit is contained in:
azivner 2017-11-04 14:43:20 -04:00
parent 979acc8b4a
commit c723bfc3ac

View file

@ -1,26 +1,32 @@
$(document).bind('keydown', 'alt+l', () => { const addLink = (function() {
$("#note-autocomplete").val(''); const dialogEl = $("#insert-link-dialog");
$("#link-title").val(''); const formEl = $("#insert-link-form");
const autoCompleteEl = $("#note-autocomplete");
const noteDetailEl = $('#note-detail');
const linkTitleEl = $("#link-title");
const noteDetail = $('#note-detail'); function showDialog() {
noteDetail.summernote('editor.saveRange'); noteDetailEl.summernote('editor.saveRange');
$("#insert-link-dialog").dialog({ dialogEl.dialog({
modal: true, modal: true,
width: 500 width: 500
}); });
autoCompleteEl.val('').focus();
linkTitleEl.val('');
function setDefaultLinkTitle(noteId) { function setDefaultLinkTitle(noteId) {
const noteTitle = getNoteTitle(noteId); const noteTitle = getNoteTitle(noteId);
$("#link-title").val(noteTitle); linkTitleEl.val(noteTitle);
} }
$("#note-autocomplete").autocomplete({ autoCompleteEl.autocomplete({
source: getAutocompleteItems(glob.allNoteIds), source: getAutocompleteItems(glob.allNoteIds),
minLength: 0, minLength: 0,
change: () => { change: () => {
const val = $("#note-autocomplete").val(); const val = autoCompleteEl.val();
const noteId = getNodeIdFromLabel(val); const noteId = getNodeIdFromLabel(val);
if (noteId) { if (noteId) {
@ -35,22 +41,21 @@ $(document).bind('keydown', 'alt+l', () => {
setDefaultLinkTitle(noteId); setDefaultLinkTitle(noteId);
} }
}); });
}); }
$("#insert-link-form").submit(() => { formEl.submit(() => {
let val = $("#note-autocomplete").val(); let val = autoCompleteEl.val();
const noteId = getNodeIdFromLabel(val); const noteId = getNodeIdFromLabel(val);
if (noteId) { if (noteId) {
const linkTitle = $("#link-title").val(); const linkTitle = linkTitleEl.val();
const noteDetail = $('#note-detail');
$("#insert-link-dialog").dialog("close"); dialogEl.dialog("close");
noteDetail.summernote('editor.restoreRange'); noteDetailEl.summernote('editor.restoreRange');
noteDetail.summernote('createLink', { noteDetailEl.summernote('createLink', {
text: linkTitle, text: linkTitle,
url: 'app#' + noteId, url: 'app#' + noteId,
isNewWindow: true isNewWindow: true
@ -58,14 +63,14 @@ $("#insert-link-form").submit(() => {
} }
return false; return false;
}); });
// when click on link popup, in case of internal link, just go the the referenced note instead of default behavior // when click on link popup, in case of internal link, just go the the referenced note instead of default behavior
// of opening the link in new window/tab // of opening the link in new window/tab
$(document).on('click', 'div.popover-content a, div.ui-tooltip-content', goToInternalNote); $(document).on('click', 'div.popover-content a, div.ui-tooltip-content', goToInternalNote);
$(document).on('dblclick', '.note-editable a, div.ui-tooltip-content', goToInternalNote); $(document).on('dblclick', '.note-editable a, div.ui-tooltip-content', goToInternalNote);
function goToInternalNote(e, callback) { function goToInternalNote(e, callback) {
const targetUrl = $(e.target).attr("href"); const targetUrl = $(e.target).attr("href");
const noteId = getNoteIdFromLink(targetUrl); const noteId = getNoteIdFromLink(targetUrl);
@ -82,9 +87,9 @@ function goToInternalNote(e, callback) {
callback(); callback();
} }
} }
} }
function getNoteIdFromLink(url) { function getNoteIdFromLink(url) {
const noteIdMatch = /app#([A-Za-z0-9]{12})/.exec(url); const noteIdMatch = /app#([A-Za-z0-9]{12})/.exec(url);
if (noteIdMatch === null) { if (noteIdMatch === null) {
@ -93,9 +98,9 @@ function getNoteIdFromLink(url) {
else { else {
return noteIdMatch[1]; return noteIdMatch[1];
} }
} }
function getNodeIdFromLabel(label) { function getNodeIdFromLabel(label) {
const noteIdMatch = / \(([A-Za-z0-9]{12})\)/.exec(label); const noteIdMatch = / \(([A-Za-z0-9]{12})\)/.exec(label);
if (noteIdMatch !== null) { if (noteIdMatch !== null) {
@ -103,4 +108,11 @@ function getNodeIdFromLabel(label) {
} }
return null; return null;
} }
$(document).bind('keydown', 'alt+l', showDialog);
return {
showDialog
};
})();