186 lines
5.2 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";
2021-04-16 23:01:56 +02:00
import froca from "./froca.js";
import utils from "./utils.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()) {
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;
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) {
2020-08-24 23:33:27 +02:00
const resolvedNotePathSegments = await treeService.resolveNotePathToSegments(notePath);
if (notePath) {
2020-08-24 23:33:27 +02:00
resolvedNotePathSegments.pop(); // remove last element
2020-08-24 23:33:27 +02:00
const parentNotePath = resolvedNotePathSegments.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,.block-link");
const notePath = getNotePathFromLink($link);
if (notePath) {
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
}
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
}
}
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
|| $link.closest("[contenteditable]").length === 0 // outside of CKEditor single click suffices
2020-09-27 23:02:21 +02:00
) {
const address = $link.attr('href');
2021-01-31 12:15:36 +01: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);
}
}
}
}
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-11-24 23:24:05 +01:00
appContext.tabManager.openTabWithNoteWithHoisting(notePath);
}
2020-04-23 23:08:15 +02:00
else if (command === 'openNoteInNewWindow') {
appContext.triggerCommand('openInWindow', {notePath, hoistedNoteId: 'root'});
2020-04-23 23:08:15 +02:00
}
}
});
}
async function loadReferenceLinkTitle(noteId, $el) {
2021-04-16 22:57:37 +02:00
const note = await froca.getNote(noteId, true);
let title;
if (!note) {
title = '[missing]';
}
else {
2020-12-18 22:35:40 +01:00
title = note.isDeleted ? `${note.title} (deleted)` : note.title;
}
$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
$(document).on('contextmenu', 'a', linkContextMenu);
$(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');
}
});
export default {
getNotePathFromUrl,
createNoteLink,
goToLink,
loadReferenceLinkTitle
};