2018-03-25 13:41:29 -04:00
|
|
|
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";
|
2021-04-16 23:01:56 +02:00
|
|
|
import froca from "./froca.js";
|
2020-10-13 22:03:16 +02:00
|
|
|
import utils from "./utils.js";
|
2017-11-04 17:07:03 -04:00
|
|
|
|
2018-10-06 23:11:42 +02:00
|
|
|
function getNotePathFromUrl(url) {
|
2018-10-06 21:32:07 +02:00
|
|
|
const notePathMatch = /#(root[A-Za-z0-9/]*)$/.exec(url);
|
2017-11-04 17:07:03 -04:00
|
|
|
|
2019-12-24 10:49:16 +01:00
|
|
|
return notePathMatch === null ? null : notePathMatch[1];
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
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()) {
|
2020-10-12 21:05:34 +02:00
|
|
|
logError("Missing note path");
|
2020-01-03 10:48:36 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
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);
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2017-12-23 07:48:59 -05:00
|
|
|
|
2019-10-01 21:11:11 +02:00
|
|
|
const $noteLink = $("<a>", {
|
2020-05-28 23:59:08 +02:00
|
|
|
href: '#' + notePath,
|
2018-03-25 11:09:17 -04:00
|
|
|
text: noteTitle
|
2018-08-15 10:14:14 +02:00
|
|
|
}).attr('data-action', 'note')
|
|
|
|
.attr('data-note-path', notePath);
|
2017-12-23 07:48:59 -05:00
|
|
|
|
2019-12-28 21:10:02 +01:00
|
|
|
if (!showTooltip) {
|
2019-10-01 21:41:20 +02:00
|
|
|
$noteLink.addClass("no-tooltip-preview");
|
|
|
|
}
|
2019-10-01 21:11:11 +02:00
|
|
|
|
2019-12-28 21:10:02 +01:00
|
|
|
const $container = $("<span>").append($noteLink);
|
2019-09-01 13:42:10 +02:00
|
|
|
|
2019-12-28 21:10:02 +01:00
|
|
|
if (showNotePath) {
|
2020-08-24 23:33:27 +02:00
|
|
|
const resolvedNotePathSegments = await treeService.resolveNotePathToSegments(notePath);
|
2019-09-01 13:42:10 +02:00
|
|
|
|
2020-01-03 09:04:38 +01:00
|
|
|
if (notePath) {
|
2020-08-24 23:33:27 +02:00
|
|
|
resolvedNotePathSegments.pop(); // remove last element
|
2019-09-01 13:42:10 +02:00
|
|
|
|
2020-08-24 23:33:27 +02:00
|
|
|
const parentNotePath = resolvedNotePathSegments.join("/").trim();
|
2019-09-01 13:42:10 +02:00
|
|
|
|
2020-01-03 09:04:38 +01:00
|
|
|
if (parentNotePath) {
|
2020-01-25 09:56:08 +01:00
|
|
|
$container.append($("<small>").text(" (" + await treeService.getNotePathTitle(parentNotePath) + ")"));
|
2020-01-03 09:04:38 +01:00
|
|
|
}
|
2019-09-01 13:42:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 21:10:02 +01:00
|
|
|
return $container;
|
2019-09-01 13:42:10 +02:00
|
|
|
}
|
|
|
|
|
2018-10-06 23:11:42 +02:00
|
|
|
function getNotePathFromLink($link) {
|
|
|
|
const notePathAttr = $link.attr("data-note-path");
|
2017-12-23 07:48:59 -05:00
|
|
|
|
2018-10-06 23:11:42 +02:00
|
|
|
if (notePathAttr) {
|
|
|
|
return notePathAttr;
|
|
|
|
}
|
2017-11-04 17:07:03 -04:00
|
|
|
|
2018-10-06 23:11:42 +02:00
|
|
|
const url = $link.attr('href');
|
2017-11-04 17:07:03 -04:00
|
|
|
|
2018-10-06 23:11:42 +02:00
|
|
|
return url ? getNotePathFromUrl(url) : null;
|
|
|
|
}
|
2017-12-09 14:11:35 -05:00
|
|
|
|
2018-10-06 23:11:42 +02:00
|
|
|
function goToLink(e) {
|
2019-12-24 10:49:16 +01:00
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
2021-03-30 23:26:14 +02:00
|
|
|
const $link = $(e.target).closest("a,.block-link");
|
2018-10-06 23:11:42 +02:00
|
|
|
|
|
|
|
const notePath = getNotePathFromLink($link);
|
|
|
|
|
|
|
|
if (notePath) {
|
2019-05-15 21:50:27 +02:00
|
|
|
if ((e.which === 1 && e.ctrlKey) || e.which === 2) {
|
2020-11-24 23:24:05 +01:00
|
|
|
appContext.tabManager.openTabWithNoteWithHoisting(notePath);
|
2019-05-08 19:10:45 +02:00
|
|
|
}
|
2019-05-15 21:50:27 +02:00
|
|
|
else if (e.which === 1) {
|
2021-05-21 22:44:08 +02:00
|
|
|
const tabId = $(e.target).closest("[data-tab-id]").attr("data-tab-id");
|
|
|
|
|
|
|
|
const tabContext = tabId
|
|
|
|
? appContext.tabManager.getTabContextById(tabId)
|
|
|
|
: appContext.tabManager.getActiveTabContext();
|
|
|
|
|
|
|
|
tabContext.setNote(notePath).then(() => {
|
|
|
|
if (tabContext !== appContext.tabManager.getActiveTabContext()) {
|
|
|
|
appContext.tabManager.activateTab(tabContext.tabId);
|
|
|
|
}
|
|
|
|
});
|
2019-05-08 19:10:45 +02:00
|
|
|
}
|
2018-10-06 23:11:42 +02:00
|
|
|
}
|
|
|
|
else {
|
2020-09-27 23:02:21 +02:00
|
|
|
if ((e.which === 1 && e.ctrlKey) || e.which === 2
|
|
|
|
|| $link.hasClass("ck-link-actions__preview") // within edit link dialog single click suffices
|
2021-04-06 20:18:16 +02:00
|
|
|
|| $link.closest("[contenteditable]").length === 0 // outside of CKEditor single click suffices
|
2020-09-27 23:02:21 +02:00
|
|
|
) {
|
2020-05-11 20:08:55 +02:00
|
|
|
const address = $link.attr('href');
|
2021-01-31 12:15:36 +01:00
|
|
|
|
2020-10-13 22:03:16 +02:00
|
|
|
if (address) {
|
|
|
|
if (address.toLowerCase().startsWith('http')) {
|
|
|
|
window.open(address, '_blank');
|
|
|
|
}
|
|
|
|
else if (address.toLowerCase().startsWith('file:') && utils.isElectron()) {
|
|
|
|
const electron = utils.dynamicRequire('electron');
|
|
|
|
|
|
|
|
electron.shell.openPath(address);
|
|
|
|
}
|
2020-05-11 20:08:55 +02:00
|
|
|
}
|
|
|
|
}
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2019-05-15 21:50:27 +02:00
|
|
|
|
|
|
|
return true;
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
|
|
|
|
2020-05-11 20:08:55 +02:00
|
|
|
function linkContextMenu(e) {
|
2019-12-24 10:49:16 +01:00
|
|
|
const $link = $(e.target).closest("a");
|
2019-05-07 22:33:53 +02:00
|
|
|
|
|
|
|
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: [
|
2020-05-11 20:08:55 +02:00
|
|
|
{title: "Open note in new tab", command: "openNoteInNewTab", uiIcon: "empty"},
|
2020-05-05 19:30:03 +02:00
|
|
|
{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-11-24 23:24:05 +01:00
|
|
|
appContext.tabManager.openTabWithNoteWithHoisting(notePath);
|
2019-05-07 22:33:53 +02:00
|
|
|
}
|
2020-04-23 23:08:15 +02:00
|
|
|
else if (command === 'openNoteInNewWindow') {
|
2021-02-07 21:27:09 +01:00
|
|
|
appContext.triggerCommand('openInWindow', {notePath, hoistedNoteId: 'root'});
|
2020-04-23 23:08:15 +02:00
|
|
|
}
|
2019-05-07 22:33:53 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-08-27 14:54:56 +02:00
|
|
|
async function loadReferenceLinkTitle(noteId, $el) {
|
2021-04-16 22:57:37 +02:00
|
|
|
const note = await froca.getNote(noteId, true);
|
2020-08-27 14:54:56 +02:00
|
|
|
|
|
|
|
let title;
|
|
|
|
|
|
|
|
if (!note) {
|
|
|
|
title = '[missing]';
|
|
|
|
}
|
|
|
|
else {
|
2020-12-18 22:35:40 +01:00
|
|
|
title = note.isDeleted ? `${note.title} (deleted)` : note.title;
|
2020-08-27 14:54:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$el.text(title);
|
|
|
|
}
|
|
|
|
|
2020-08-19 17:59:55 +02:00
|
|
|
$(document).on('click', "a", goToLink);
|
|
|
|
$(document).on('auxclick', "a", goToLink); // to handle middle button
|
2020-08-15 22:30:40 +02:00
|
|
|
$(document).on('contextmenu', 'a', linkContextMenu);
|
2020-10-12 22:11:49 +02:00
|
|
|
$(document).on('dblclick', "a", e => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
const $link = $(e.target).closest("a");
|
|
|
|
|
|
|
|
const address = $link.attr('href');
|
|
|
|
|
|
|
|
if (address && address.startsWith('http')) {
|
|
|
|
window.open(address, '_blank');
|
|
|
|
}
|
|
|
|
});
|
2019-12-24 10:49:16 +01:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
export default {
|
2018-10-06 23:11:42 +02:00
|
|
|
getNotePathFromUrl,
|
2018-03-25 11:09:17 -04:00
|
|
|
createNoteLink,
|
2020-08-27 14:54:56 +02:00
|
|
|
goToLink,
|
|
|
|
loadReferenceLinkTitle
|
2020-05-11 19:38:14 +02:00
|
|
|
};
|