mirror of
https://github.com/zadam/trilium.git
synced 2025-03-04 02:53:30 +08:00
add link converted to module
This commit is contained in:
parent
979acc8b4a
commit
c723bfc3ac
1 changed files with 92 additions and 80 deletions
|
@ -1,106 +1,118 @@
|
|||
$(document).bind('keydown', 'alt+l', () => {
|
||||
$("#note-autocomplete").val('');
|
||||
$("#link-title").val('');
|
||||
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");
|
||||
|
||||
const noteDetail = $('#note-detail');
|
||||
noteDetail.summernote('editor.saveRange');
|
||||
function showDialog() {
|
||||
noteDetailEl.summernote('editor.saveRange');
|
||||
|
||||
$("#insert-link-dialog").dialog({
|
||||
modal: true,
|
||||
width: 500
|
||||
});
|
||||
dialogEl.dialog({
|
||||
modal: true,
|
||||
width: 500
|
||||
});
|
||||
|
||||
function setDefaultLinkTitle(noteId) {
|
||||
const noteTitle = getNoteTitle(noteId);
|
||||
autoCompleteEl.val('').focus();
|
||||
linkTitleEl.val('');
|
||||
|
||||
$("#link-title").val(noteTitle);
|
||||
}
|
||||
function setDefaultLinkTitle(noteId) {
|
||||
const noteTitle = getNoteTitle(noteId);
|
||||
|
||||
$("#note-autocomplete").autocomplete({
|
||||
source: getAutocompleteItems(glob.allNoteIds),
|
||||
minLength: 0,
|
||||
change: () => {
|
||||
const val = $("#note-autocomplete").val();
|
||||
const noteId = getNodeIdFromLabel(val);
|
||||
linkTitleEl.val(noteTitle);
|
||||
}
|
||||
|
||||
autoCompleteEl.autocomplete({
|
||||
source: getAutocompleteItems(glob.allNoteIds),
|
||||
minLength: 0,
|
||||
change: () => {
|
||||
const val = autoCompleteEl.val();
|
||||
const noteId = getNodeIdFromLabel(val);
|
||||
|
||||
if (noteId) {
|
||||
setDefaultLinkTitle(noteId);
|
||||
}
|
||||
},
|
||||
// this is called when user goes through autocomplete list with keyboard
|
||||
// at this point the item isn't selected yet so we use supplied ui.item to see where the cursor is
|
||||
focus: (event, ui) => {
|
||||
const noteId = getNodeIdFromLabel(ui.item.value);
|
||||
|
||||
if (noteId) {
|
||||
setDefaultLinkTitle(noteId);
|
||||
}
|
||||
},
|
||||
// this is called when user goes through autocomplete list with keyboard
|
||||
// at this point the item isn't selected yet so we use supplied ui.item to see where the cursor is
|
||||
focus: (event, ui) => {
|
||||
const noteId = getNodeIdFromLabel(ui.item.value);
|
||||
|
||||
setDefaultLinkTitle(noteId);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$("#insert-link-form").submit(() => {
|
||||
let val = $("#note-autocomplete").val();
|
||||
|
||||
const noteId = getNodeIdFromLabel(val);
|
||||
|
||||
if (noteId) {
|
||||
const linkTitle = $("#link-title").val();
|
||||
const noteDetail = $('#note-detail');
|
||||
|
||||
$("#insert-link-dialog").dialog("close");
|
||||
|
||||
noteDetail.summernote('editor.restoreRange');
|
||||
|
||||
noteDetail.summernote('createLink', {
|
||||
text: linkTitle,
|
||||
url: 'app#' + noteId,
|
||||
isNewWindow: true
|
||||
});
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
formEl.submit(() => {
|
||||
let val = autoCompleteEl.val();
|
||||
|
||||
// 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
|
||||
$(document).on('click', 'div.popover-content a, div.ui-tooltip-content', goToInternalNote);
|
||||
$(document).on('dblclick', '.note-editable a, div.ui-tooltip-content', goToInternalNote);
|
||||
const noteId = getNodeIdFromLabel(val);
|
||||
|
||||
function goToInternalNote(e, callback) {
|
||||
const targetUrl = $(e.target).attr("href");
|
||||
if (noteId) {
|
||||
const linkTitle = linkTitleEl.val();
|
||||
|
||||
const noteId = getNoteIdFromLink(targetUrl);
|
||||
dialogEl.dialog("close");
|
||||
|
||||
if (noteId !== null) {
|
||||
getNodeByKey(noteId).setActive();
|
||||
noteDetailEl.summernote('editor.restoreRange');
|
||||
|
||||
// this is quite ugly hack, but it seems like we can't close the tooltip otherwise
|
||||
$("[role='tooltip']").remove();
|
||||
noteDetailEl.summernote('createLink', {
|
||||
text: linkTitle,
|
||||
url: 'app#' + noteId,
|
||||
isNewWindow: true
|
||||
});
|
||||
}
|
||||
|
||||
e.preventDefault();
|
||||
return false;
|
||||
});
|
||||
|
||||
if (callback) {
|
||||
callback();
|
||||
// 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
|
||||
$(document).on('click', 'div.popover-content a, div.ui-tooltip-content', goToInternalNote);
|
||||
$(document).on('dblclick', '.note-editable a, div.ui-tooltip-content', goToInternalNote);
|
||||
|
||||
function goToInternalNote(e, callback) {
|
||||
const targetUrl = $(e.target).attr("href");
|
||||
|
||||
const noteId = getNoteIdFromLink(targetUrl);
|
||||
|
||||
if (noteId !== null) {
|
||||
getNodeByKey(noteId).setActive();
|
||||
|
||||
// this is quite ugly hack, but it seems like we can't close the tooltip otherwise
|
||||
$("[role='tooltip']").remove();
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
if (callback) {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function getNoteIdFromLink(url) {
|
||||
const noteIdMatch = /app#([A-Za-z0-9]{12})/.exec(url);
|
||||
function getNoteIdFromLink(url) {
|
||||
const noteIdMatch = /app#([A-Za-z0-9]{12})/.exec(url);
|
||||
|
||||
if (noteIdMatch === null) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
return noteIdMatch[1];
|
||||
}
|
||||
}
|
||||
|
||||
function getNodeIdFromLabel(label) {
|
||||
const noteIdMatch = / \(([A-Za-z0-9]{12})\)/.exec(label);
|
||||
|
||||
if (noteIdMatch !== null) {
|
||||
return noteIdMatch[1];
|
||||
}
|
||||
|
||||
if (noteIdMatch === null) {
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
return noteIdMatch[1];
|
||||
}
|
||||
}
|
||||
|
||||
function getNodeIdFromLabel(label) {
|
||||
const noteIdMatch = / \(([A-Za-z0-9]{12})\)/.exec(label);
|
||||
$(document).bind('keydown', 'alt+l', showDialog);
|
||||
|
||||
if (noteIdMatch !== null) {
|
||||
return noteIdMatch[1];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
showDialog
|
||||
};
|
||||
})();
|
Loading…
Reference in a new issue