trilium/src/public/javascripts/link.js

103 lines
2.7 KiB
JavaScript
Raw Normal View History

"use strict";
2017-11-05 05:07:03 +08:00
const link = (function() {
2017-11-20 09:36:13 +08:00
function getNotePathFromLink(url) {
2017-12-18 06:37:19 +08:00
const notePathMatch = /#([A-Za-z0-9/]+)$/.exec(url);
2017-11-05 05:07:03 +08:00
2017-11-20 09:36:13 +08:00
if (notePathMatch === null) {
2017-11-05 05:07:03 +08:00
return null;
}
else {
2017-11-20 09:36:13 +08:00
return notePathMatch[1];
2017-11-05 05:07:03 +08:00
}
}
2017-11-20 09:36:13 +08:00
function getNodePathFromLabel(label) {
const notePathMatch = / \(([A-Za-z0-9/]+)\)/.exec(label);
2017-11-05 05:07:03 +08:00
2017-11-20 09:36:13 +08:00
if (notePathMatch !== null) {
return notePathMatch[1];
2017-11-05 05:07:03 +08:00
}
return null;
}
function createNoteLink(notePath, noteTitle) {
if (!noteTitle) {
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
2018-03-25 09:39:15 +08:00
noteTitle = treeService.getNoteTitle(noteId);
}
2017-11-05 05:07:03 +08:00
const noteLink = $("<a>", {
href: 'javascript:',
text: noteTitle
2017-11-05 05:07:03 +08:00
}).attr('action', 'note')
.attr('note-path', notePath);
2017-11-05 05:07:03 +08:00
return noteLink;
}
function goToLink(e) {
e.preventDefault();
2018-02-15 12:31:20 +08:00
const $link = $(e.target);
let notePath = $link.attr("note-path");
2018-01-03 09:16:17 +08:00
if (!notePath) {
2018-02-15 12:31:20 +08:00
const address = $link.attr("note-path") ? $link.attr("note-path") : $link.attr('href');
2018-01-03 09:16:17 +08:00
if (!address) {
return;
}
2018-01-03 09:16:17 +08:00
if (address.startsWith('http')) {
window.open(address, '_blank');
2018-01-03 09:16:17 +08:00
return;
}
notePath = getNotePathFromLink(address);
}
2018-03-25 09:39:15 +08:00
treeService.activateNode(notePath);
2017-11-05 05:07:03 +08:00
// this is quite ugly hack, but it seems like we can't close the tooltip otherwise
$("[role='tooltip']").remove();
2017-11-05 05:07:03 +08:00
if (glob.activeDialog) {
try {
glob.activeDialog.dialog('close');
2017-11-05 05:07:03 +08:00
}
catch (e) {}
2017-11-05 05:07:03 +08:00
}
}
2017-12-10 03:11:35 +08:00
function addLinkToEditor(linkTitle, linkHref) {
const editor = noteEditor.getEditor();
const doc = editor.document;
doc.enqueueChanges(() => editor.data.insertLink(linkTitle, linkHref), doc.selection);
}
2017-12-10 03:17:03 +08:00
function addTextToEditor(text) {
const editor = noteEditor.getEditor();
const doc = editor.document;
doc.enqueueChanges(() => editor.data.insertText(text), doc.selection);
}
2017-11-05 05:07:03 +08:00
// 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', "a[action='note']", goToLink);
$(document).on('click', 'div.popover-content a, div.ui-tooltip-content a', goToLink);
$(document).on('dblclick', '#note-detail a', goToLink);
2017-11-05 05:07:03 +08:00
return {
2017-11-20 09:36:13 +08:00
getNodePathFromLabel,
getNotePathFromLink,
2017-12-10 03:11:35 +08:00
createNoteLink,
2017-12-10 03:17:03 +08:00
addLinkToEditor,
addTextToEditor
2017-11-05 05:07:03 +08:00
};
})();