186 lines
5.6 KiB
JavaScript
Raw Normal View History

import treeService from './tree.js';
2020-02-29 13:03:05 +01:00
import contextMenu from "./context_menu.js";
2020-01-21 22:54:16 +01:00
import appContext from "./app_context.js";
2017-11-04 17:07:03 -04:00
function getNotePathFromUrl(url) {
const notePathMatch = /#(root[A-Za-z0-9/]*)$/.exec(url);
2017-11-04 17:07:03 -04:00
return notePathMatch === null ? null : notePathMatch[1];
}
2017-11-04 17:07:03 -04:00
2019-12-28 21:10:02 +01:00
async function createNoteLink(notePath, options = {}) {
2020-01-03 10:48:36 +01:00
if (!notePath || !notePath.trim()) {
console.error("Missing note path");
return $("<span>").text("[missing note]");
}
2019-12-28 21:10:02 +01:00
let noteTitle = options.title;
const showTooltip = options.showTooltip === undefined ? true : options.showTooltip;
const showNotePath = options.showNotePath === undefined ? false : options.showNotePath;
if (!noteTitle) {
2020-01-25 09:56:08 +01:00
const {noteId, parentNoteId} = treeService.getNoteIdAndParentIdFromNotePath(notePath);
2017-11-04 17:07:03 -04:00
2020-01-25 09:56:08 +01:00
noteTitle = await treeService.getNoteTitle(noteId, parentNoteId);
}
const $noteLink = $("<a>", {
href: '#' + notePath,
text: noteTitle
}).attr('data-action', 'note')
.attr('data-note-path', notePath);
2019-12-28 21:10:02 +01:00
if (!showTooltip) {
2019-10-01 21:41:20 +02:00
$noteLink.addClass("no-tooltip-preview");
}
2019-12-28 21:10:02 +01:00
const $container = $("<span>").append($noteLink);
2019-12-28 21:10:02 +01:00
if (showNotePath) {
notePath = await treeService.resolveNotePath(notePath);
if (notePath) {
const noteIds = notePath.split("/");
noteIds.pop(); // remove last element
const parentNotePath = noteIds.join("/").trim();
if (parentNotePath) {
2020-01-25 09:56:08 +01:00
$container.append($("<small>").text(" (" + await treeService.getNotePathTitle(parentNotePath) + ")"));
}
}
}
2019-12-28 21:10:02 +01:00
return $container;
}
function getNotePathFromLink($link) {
const notePathAttr = $link.attr("data-note-path");
if (notePathAttr) {
return notePathAttr;
}
2017-11-04 17:07:03 -04:00
const url = $link.attr('href');
2017-11-04 17:07:03 -04:00
return url ? getNotePathFromUrl(url) : null;
}
2017-12-09 14:11:35 -05:00
function goToLink(e) {
e.preventDefault();
e.stopPropagation();
const $link = $(e.target).closest("a");
const notePath = getNotePathFromLink($link);
if (notePath) {
if ((e.which === 1 && e.ctrlKey) || e.which === 2) {
2020-02-29 16:26:46 +01:00
appContext.tabManager.openTabWithNote(notePath);
2019-05-08 19:10:45 +02:00
}
else if (e.which === 1) {
const activeTabContext = appContext.tabManager.getActiveTabContext();
activeTabContext.setNote(notePath);
2019-05-08 19:10:45 +02:00
}
else {
return false;
}
}
else {
if (e.which === 1) {
const address = $link.attr('href');
if (address && address.startsWith('http')) {
window.open(address, '_blank');
}
}
else {
return false;
}
}
return true;
}
function linkContextMenu(e) {
const $link = $(e.target).closest("a");
const notePath = getNotePathFromLink($link);
if (!notePath) {
return;
}
e.preventDefault();
2020-02-29 13:03:05 +01:00
contextMenu.show({
x: e.pageX,
y: e.pageY,
items: [
{title: "Open note in new tab", command: "openNoteInNewTab", uiIcon: "empty"},
{title: "Open note in new window", command: "openNoteInNewWindow", uiIcon: "window-open"}
2020-02-29 13:03:05 +01:00
],
selectMenuItemHandler: ({command}) => {
if (command === 'openNoteInNewTab') {
2020-02-29 16:26:46 +01:00
appContext.tabManager.openTabWithNote(notePath);
}
2020-04-23 23:08:15 +02:00
else if (command === 'openNoteInNewWindow') {
appContext.openInNewWindow(notePath);
}
}
});
}
// 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('mousedown', "a[data-action='note']", goToLink);
$(document).on('mousedown', 'div.popover-content a, div.ui-tooltip-content a', goToLink);
2019-05-02 22:24:43 +02:00
$(document).on('dblclick', '.note-detail-text a', goToLink);
$(document).on('mousedown', '.note-detail-text a:not(.reference-link)', function (e) {
const $link = $(e.target).closest("a");
const notePath = getNotePathFromLink($link);
if ((e.which === 1 && e.ctrlKey) || e.which === 2) {
2019-05-08 19:10:45 +02:00
// if it's a ctrl-click, then we open on new tab, otherwise normal flow (CKEditor opens link-editing dialog)
e.preventDefault();
if (notePath) {
2020-02-29 16:26:46 +01:00
appContext.tabManager.openTabWithNote(notePath, false);
}
else {
const address = $link.attr('href');
window.open(address, '_blank');
}
return true;
2019-05-08 19:10:45 +02:00
}
});
2020-07-24 23:14:31 +02:00
$(document).on('mousedown', 'a.reference-link', goToLink);
2019-10-06 12:33:47 +02:00
$(document).on('mousedown', '.note-detail-book a', goToLink);
$(document).on('mousedown', '.note-detail-render a', goToLink);
$(document).on('mousedown', '.note-detail-readonly-text a', goToLink);
$(document).on('mousedown', 'a.ck-link-actions__preview', goToLink);
$(document).on('click', 'section.include-note a', goToLink);
$(document).on('click', 'a.ck-link-actions__preview', e => {
e.preventDefault();
e.stopPropagation();
});
$(document).on('contextmenu', 'a.ck-link-actions__preview', linkContextMenu);
$(document).on('contextmenu', '.note-detail-text a', linkContextMenu);
$(document).on('contextmenu', '.note-detail-readonly-text a', linkContextMenu);
2020-07-24 23:14:31 +02:00
$(document).on('contextmenu', 'a.reference-link', linkContextMenu);
$(document).on('contextmenu', "a[data-action='note']", linkContextMenu);
$(document).on('contextmenu', ".note-detail-render a", linkContextMenu);
$(document).on('contextmenu', ".note-paths-widget a", linkContextMenu);
$(document).on('contextmenu', "section.include-note a", linkContextMenu);
export default {
getNotePathFromUrl,
createNoteLink,
goToLink
};