2018-03-25 13:41:29 -04:00
|
|
|
import treeService from './tree.js';
|
2022-08-05 16:44:26 +02:00
|
|
|
import linkContextMenuService from "../menus/link_context_menu.js";
|
2022-12-01 13:07:23 +01:00
|
|
|
import appContext from "../components/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) {
|
2022-11-28 23:39:23 +01: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
|
|
|
|
2023-05-29 10:21:34 +02:00
|
|
|
async function getLinkIcon(noteId, viewMode) {
|
|
|
|
let icon;
|
|
|
|
|
|
|
|
if (viewMode === 'default') {
|
|
|
|
const note = await froca.getNote(noteId);
|
|
|
|
|
|
|
|
icon = note.getIcon();
|
|
|
|
} else if (viewMode === 'source') {
|
|
|
|
icon = 'bx bx-code-curly';
|
|
|
|
} else if (viewMode === 'attachments') {
|
|
|
|
icon = 'bx bx-file';
|
|
|
|
}
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
|
2023-05-29 00:19:54 +02:00
|
|
|
async function createLink(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]");
|
|
|
|
}
|
|
|
|
|
2022-11-14 20:30:52 +01:00
|
|
|
if (!notePath.startsWith("root")) {
|
|
|
|
// all note paths should start with "root/" (except for "root" itself)
|
2023-05-07 21:18:21 +02:00
|
|
|
// used, e.g., to find internal links
|
2022-12-21 15:19:05 +01:00
|
|
|
notePath = `root/${notePath}`;
|
2022-11-14 20:30:52 +01:00
|
|
|
}
|
|
|
|
|
2019-12-28 21:10:02 +01:00
|
|
|
const showTooltip = options.showTooltip === undefined ? true : options.showTooltip;
|
|
|
|
const showNotePath = options.showNotePath === undefined ? false : options.showNotePath;
|
2021-10-10 14:09:22 +02:00
|
|
|
const showNoteIcon = options.showNoteIcon === undefined ? false : options.showNoteIcon;
|
2021-12-04 12:50:02 +01:00
|
|
|
const referenceLink = options.referenceLink === undefined ? false : options.referenceLink;
|
2019-12-28 21:10:02 +01:00
|
|
|
|
2023-05-29 22:37:19 +02:00
|
|
|
const { noteId, parentNoteId } = treeService.getNoteIdAndParentIdFromUrl(notePath);
|
2023-05-29 00:19:54 +02:00
|
|
|
const viewScope = options.viewScope || {};
|
|
|
|
const viewMode = viewScope.viewMode || 'default';
|
|
|
|
let linkTitle = options.title;
|
|
|
|
|
|
|
|
if (!linkTitle) {
|
|
|
|
if (viewMode === 'attachments' && viewScope.attachmentId) {
|
|
|
|
const attachment = await froca.getAttachment(viewScope.attachmentId);
|
|
|
|
|
|
|
|
linkTitle = attachment ? attachment.title : '[missing attachment]';
|
|
|
|
} else {
|
|
|
|
linkTitle = await treeService.getNoteTitle(noteId, parentNoteId);
|
|
|
|
}
|
|
|
|
}
|
2017-12-23 07:48:59 -05:00
|
|
|
|
2021-10-10 14:09:22 +02:00
|
|
|
const $container = $("<span>");
|
|
|
|
|
|
|
|
if (showNoteIcon) {
|
2023-05-29 10:21:34 +02:00
|
|
|
let icon = await getLinkIcon(noteId, viewMode);
|
2021-10-10 14:09:22 +02:00
|
|
|
|
2023-05-29 00:19:54 +02:00
|
|
|
if (icon) {
|
|
|
|
$container
|
|
|
|
.append($("<span>").addClass(`bx ${icon}`))
|
|
|
|
.append(" ");
|
|
|
|
}
|
2021-10-10 14:09:22 +02:00
|
|
|
}
|
|
|
|
|
2023-05-07 21:18:21 +02:00
|
|
|
const hash = calculateHash({
|
|
|
|
notePath,
|
2023-05-29 00:19:54 +02:00
|
|
|
viewScope: viewScope
|
2023-05-07 21:18:21 +02:00
|
|
|
});
|
|
|
|
|
2019-10-01 21:11:11 +02:00
|
|
|
const $noteLink = $("<a>", {
|
2023-05-07 21:18:21 +02:00
|
|
|
href: hash,
|
2023-05-29 00:19:54 +02:00
|
|
|
text: linkTitle
|
2023-05-07 21:18:21 +02:00
|
|
|
});
|
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
|
|
|
|
2021-12-04 12:50:02 +01:00
|
|
|
if (referenceLink) {
|
|
|
|
$noteLink.addClass("reference-link");
|
|
|
|
}
|
|
|
|
|
2021-10-10 14:09:22 +02:00
|
|
|
$container.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
|
|
|
|
2023-06-29 23:32:19 +02:00
|
|
|
if (resolvedNotePathSegments) {
|
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) {
|
2022-12-21 15:19:05 +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
|
|
|
}
|
|
|
|
|
2023-04-11 21:41:55 +02:00
|
|
|
function calculateHash({notePath, ntxId, hoistedNoteId, viewScope = {}}) {
|
|
|
|
notePath = notePath || "";
|
|
|
|
const params = [
|
|
|
|
ntxId ? { ntxId: ntxId } : null,
|
|
|
|
(hoistedNoteId && hoistedNoteId !== 'root') ? { hoistedNoteId: hoistedNoteId } : null,
|
2023-05-07 23:32:30 +02:00
|
|
|
viewScope.viewMode && viewScope.viewMode !== 'default' ? { viewMode: viewScope.viewMode } : null,
|
2023-04-11 21:41:55 +02:00
|
|
|
viewScope.attachmentId ? { attachmentId: viewScope.attachmentId } : null
|
|
|
|
].filter(p => !!p);
|
|
|
|
|
|
|
|
const paramStr = params.map(pair => {
|
|
|
|
const name = Object.keys(pair)[0];
|
|
|
|
const value = pair[name];
|
|
|
|
|
|
|
|
return `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
|
|
|
|
}).join("&");
|
|
|
|
|
|
|
|
if (!notePath && !paramStr) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
let hash = `#${notePath}`;
|
|
|
|
|
|
|
|
if (paramStr) {
|
|
|
|
hash += `?${paramStr}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2023-05-07 21:18:21 +02:00
|
|
|
function parseNavigationStateFromUrl(url) {
|
2023-05-07 21:29:09 +02:00
|
|
|
if (!url) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const hashIdx = url.indexOf('#');
|
2023-05-07 21:18:21 +02:00
|
|
|
if (hashIdx === -1) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-05-07 21:29:09 +02:00
|
|
|
const hash = url.substr(hashIdx + 1); // strip also the initial '#'
|
2023-05-07 21:18:21 +02:00
|
|
|
const [notePath, paramString] = hash.split("?");
|
2023-08-02 23:23:31 +02:00
|
|
|
|
|
|
|
if (!notePath.match(/^[_a-z0-9]{4,}(\/[_a-z0-9]{4,})*$/i)) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-05-07 21:18:21 +02:00
|
|
|
const viewScope = {
|
|
|
|
viewMode: 'default'
|
|
|
|
};
|
|
|
|
let ntxId = null;
|
|
|
|
let hoistedNoteId = null;
|
2023-07-15 10:59:30 +02:00
|
|
|
let searchString = null;
|
2023-05-07 21:18:21 +02:00
|
|
|
|
|
|
|
if (paramString) {
|
|
|
|
for (const pair of paramString.split("&")) {
|
|
|
|
let [name, value] = pair.split("=");
|
|
|
|
name = decodeURIComponent(name);
|
|
|
|
value = decodeURIComponent(value);
|
|
|
|
|
|
|
|
if (name === 'ntxId') {
|
|
|
|
ntxId = value;
|
|
|
|
} else if (name === 'hoistedNoteId') {
|
|
|
|
hoistedNoteId = value;
|
2023-07-15 10:59:30 +02:00
|
|
|
} else if (name === 'searchString') {
|
|
|
|
searchString = value; // supports triggering search from URL, e.g. #?searchString=blabla
|
2023-05-07 21:18:21 +02:00
|
|
|
} else if (['viewMode', 'attachmentId'].includes(name)) {
|
|
|
|
viewScope[name] = value;
|
|
|
|
} else {
|
|
|
|
console.warn(`Unrecognized hash parameter '${name}'.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
notePath,
|
2023-05-29 22:37:19 +02:00
|
|
|
noteId: treeService.getNoteIdFromUrl(notePath),
|
2023-05-07 21:18:21 +02:00
|
|
|
ntxId,
|
|
|
|
hoistedNoteId,
|
2023-07-15 10:59:30 +02:00
|
|
|
viewScope,
|
|
|
|
searchString
|
2023-05-07 21:18:21 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-12-09 16:48:00 +01:00
|
|
|
function goToLink(evt) {
|
|
|
|
const $link = $(evt.target).closest("a,.block-link");
|
2023-05-07 21:18:21 +02:00
|
|
|
const hrefLink = $link.attr('href') || $link.attr('data-href');
|
2022-07-29 00:32:28 +02:00
|
|
|
|
2023-09-08 23:00:43 +02:00
|
|
|
return goToLinkExt(evt, hrefLink, $link);
|
|
|
|
}
|
|
|
|
|
|
|
|
function goToLinkExt(evt, hrefLink, $link) {
|
2023-04-11 17:45:51 +02:00
|
|
|
if (hrefLink?.startsWith("data:")) {
|
2022-07-29 00:32:28 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-12-09 16:48:00 +01:00
|
|
|
evt.preventDefault();
|
|
|
|
evt.stopPropagation();
|
2019-12-24 10:49:16 +01:00
|
|
|
|
2023-09-08 23:00:43 +02:00
|
|
|
const {notePath, viewScope} = parseNavigationStateFromUrl(hrefLink);
|
2018-10-06 23:11:42 +02:00
|
|
|
|
2022-12-09 16:48:00 +01:00
|
|
|
const ctrlKey = utils.isCtrlKey(evt);
|
2023-04-03 23:47:24 +02:00
|
|
|
const isLeftClick = evt.which === 1;
|
|
|
|
const isMiddleClick = evt.which === 2;
|
|
|
|
const openInNewTab = (isLeftClick && ctrlKey) || isMiddleClick;
|
2022-11-10 23:16:41 +01:00
|
|
|
|
2023-05-26 11:13:49 +02:00
|
|
|
const leftClick = evt.which === 1;
|
|
|
|
const middleClick = evt.which === 2;
|
|
|
|
|
2018-10-06 23:11:42 +02:00
|
|
|
if (notePath) {
|
2023-04-03 23:47:24 +02:00
|
|
|
if (openInNewTab) {
|
2023-09-08 23:00:43 +02:00
|
|
|
appContext.tabManager.openTabWithNoteWithHoisting(notePath, {viewScope});
|
|
|
|
} else if (isLeftClick) {
|
2022-12-09 16:48:00 +01:00
|
|
|
const ntxId = $(evt.target).closest("[data-ntx-id]").attr("data-ntx-id");
|
2021-05-21 22:44:08 +02:00
|
|
|
|
2021-05-22 12:26:45 +02:00
|
|
|
const noteContext = ntxId
|
|
|
|
? appContext.tabManager.getNoteContextById(ntxId)
|
2021-05-22 12:35:41 +02:00
|
|
|
: appContext.tabManager.getActiveContext();
|
2021-05-21 22:44:08 +02:00
|
|
|
|
2023-09-08 23:00:43 +02:00
|
|
|
noteContext.setNote(notePath, {viewScope}).then(() => {
|
2021-05-22 12:35:41 +02:00
|
|
|
if (noteContext !== appContext.tabManager.getActiveContext()) {
|
|
|
|
appContext.tabManager.activateNoteContext(noteContext.ntxId);
|
2021-05-21 22:44:08 +02:00
|
|
|
}
|
|
|
|
});
|
2019-05-08 19:10:45 +02:00
|
|
|
}
|
2023-09-08 23:00:43 +02:00
|
|
|
} else if (hrefLink) {
|
|
|
|
const withinEditLink = $link?.hasClass("ck-link-actions__preview");
|
|
|
|
const outsideOfCKEditor = !$link || $link.closest("[contenteditable]").length === 0;
|
2020-10-13 22:03:16 +02:00
|
|
|
|
2023-05-28 16:57:48 +02:00
|
|
|
if (openInNewTab
|
2023-05-26 11:13:49 +02:00
|
|
|
|| (withinEditLink && (leftClick || middleClick))
|
|
|
|
|| (outsideOfCKEditor && (leftClick || middleClick))
|
2020-09-27 23:02:21 +02:00
|
|
|
) {
|
2023-07-10 20:30:04 +02:00
|
|
|
if (hrefLink.toLowerCase().startsWith('http') || hrefLink.startsWith("api/")) {
|
2023-04-11 17:45:51 +02:00
|
|
|
window.open(hrefLink, '_blank');
|
2023-09-08 23:00:43 +02:00
|
|
|
} else if (hrefLink.toLowerCase().startsWith('file:') && utils.isElectron()) {
|
2023-04-11 17:45:51 +02:00
|
|
|
const electron = utils.dynamicRequire('electron');
|
|
|
|
|
|
|
|
electron.shell.openPath(hrefLink);
|
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");
|
2023-05-07 21:18:21 +02:00
|
|
|
const url = $link.attr("href") || $link.attr("data-href");
|
2019-05-07 22:33:53 +02:00
|
|
|
|
2023-05-07 21:18:21 +02:00
|
|
|
const { notePath, viewScope } = parseNavigationStateFromUrl(url);
|
2019-05-07 22:33:53 +02:00
|
|
|
|
|
|
|
if (!notePath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
2023-04-03 23:47:24 +02:00
|
|
|
linkContextMenuService.openContextMenu(notePath, e, viewScope, null);
|
2019-05-07 22:33:53 +02:00
|
|
|
}
|
|
|
|
|
2023-05-29 10:21:34 +02:00
|
|
|
async function loadReferenceLinkTitle($el, href = null) {
|
|
|
|
href = href || $el.find("a").attr("href");
|
|
|
|
if (!href) {
|
|
|
|
console.warn("Empty URL for parsing: " + $el[0].outerHTML);
|
2023-05-29 00:19:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-29 10:21:34 +02:00
|
|
|
const {noteId, viewScope} = parseNavigationStateFromUrl(href);
|
2021-04-16 22:57:37 +02:00
|
|
|
const note = await froca.getNote(noteId, true);
|
2020-08-27 14:54:56 +02:00
|
|
|
|
2023-05-29 10:21:34 +02:00
|
|
|
if (note) {
|
|
|
|
$el.addClass(note.getColorClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
const title = await getReferenceLinkTitle(href);
|
|
|
|
$el.text(title);
|
|
|
|
|
|
|
|
if (note) {
|
|
|
|
const icon = await getLinkIcon(noteId, viewScope.viewMode);
|
2020-08-27 14:54:56 +02:00
|
|
|
|
2023-05-29 10:21:34 +02:00
|
|
|
$el.prepend($("<span>").addClass(icon));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getReferenceLinkTitle(href) {
|
|
|
|
const {noteId, viewScope} = parseNavigationStateFromUrl(href);
|
|
|
|
if (!noteId) {
|
|
|
|
return "[missing note]";
|
|
|
|
}
|
|
|
|
|
|
|
|
const note = await froca.getNote(noteId);
|
2020-08-27 14:54:56 +02:00
|
|
|
if (!note) {
|
2023-05-29 10:21:34 +02:00
|
|
|
return "[missing note]";
|
2020-08-27 14:54:56 +02:00
|
|
|
}
|
2023-05-29 10:21:34 +02:00
|
|
|
|
|
|
|
if (viewScope?.viewMode === 'attachments' && viewScope?.attachmentId) {
|
|
|
|
const attachment = await note.getAttachmentById(viewScope.attachmentId);
|
|
|
|
|
|
|
|
return attachment ? attachment.title : "[missing attachment]";
|
|
|
|
} else {
|
|
|
|
return note.title;
|
2020-08-27 14:54:56 +02:00
|
|
|
}
|
2023-05-29 10:21:34 +02:00
|
|
|
}
|
2020-08-27 14:54:56 +02:00
|
|
|
|
2023-05-29 10:21:34 +02:00
|
|
|
function getReferenceLinkTitleSync(href) {
|
|
|
|
const {noteId, viewScope} = parseNavigationStateFromUrl(href);
|
|
|
|
if (!noteId) {
|
|
|
|
return "[missing note]";
|
2022-12-23 23:08:30 +01:00
|
|
|
}
|
|
|
|
|
2023-05-29 10:21:34 +02:00
|
|
|
const note = froca.getNoteFromCache(noteId);
|
|
|
|
if (!note) {
|
|
|
|
return "[missing note]";
|
|
|
|
}
|
2022-09-24 22:38:20 +02:00
|
|
|
|
2023-05-29 10:21:34 +02:00
|
|
|
if (viewScope?.viewMode === 'attachments' && viewScope?.attachmentId) {
|
|
|
|
if (!note.attachments) {
|
|
|
|
return "[loading title...]";
|
|
|
|
}
|
|
|
|
|
|
|
|
const attachment = note.attachments.find(att => att.attachmentId === viewScope.attachmentId);
|
|
|
|
|
|
|
|
return attachment ? attachment.title : "[missing attachment]";
|
|
|
|
} else {
|
|
|
|
return note.title;
|
2022-12-23 23:08:30 +01:00
|
|
|
}
|
2020-08-27 14:54:56 +02:00
|
|
|
}
|
|
|
|
|
2020-08-19 17:59:55 +02:00
|
|
|
$(document).on('click', "a", goToLink);
|
2023-05-05 23:41:11 +02:00
|
|
|
$(document).on('auxclick', "a", goToLink); // to handle the 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
|
|
|
|
2022-07-19 23:41:22 +02:00
|
|
|
$(document).on('mousedown', 'a', e => {
|
|
|
|
if (e.which === 2) {
|
|
|
|
// prevent paste on middle click
|
|
|
|
// https://github.com/zadam/trilium/issues/2995
|
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/Element/auxclick_event#preventing_default_actions
|
|
|
|
e.preventDefault();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
export default {
|
2018-10-06 23:11:42 +02:00
|
|
|
getNotePathFromUrl,
|
2023-05-29 00:19:54 +02:00
|
|
|
createLink,
|
2020-08-27 14:54:56 +02:00
|
|
|
goToLink,
|
2023-09-08 23:00:43 +02:00
|
|
|
goToLinkExt,
|
2023-04-11 17:45:51 +02:00
|
|
|
loadReferenceLinkTitle,
|
2023-05-29 10:21:34 +02:00
|
|
|
getReferenceLinkTitle,
|
|
|
|
getReferenceLinkTitleSync,
|
2023-05-07 21:18:21 +02:00
|
|
|
calculateHash,
|
|
|
|
parseNavigationStateFromUrl
|
2020-05-11 19:38:14 +02:00
|
|
|
};
|