trilium/src/public/javascripts/services/link.js

167 lines
4.6 KiB
JavaScript
Raw Normal View History

import treeService from './tree.js';
import treeUtils from './tree_utils.js';
import contextMenuService from "./context_menu.js";
import noteDetailService from "./note_detail.js";
2017-11-05 05:07:03 +08:00
function getNotePathFromUrl(url) {
const notePathMatch = /#(root[A-Za-z0-9/]*)$/.exec(url);
2017-11-05 05:07:03 +08:00
if (notePathMatch === null) {
2017-11-05 05:07:03 +08:00
return null;
}
else {
return notePathMatch[1];
}
}
2017-11-05 05:07:03 +08:00
async function createNoteLink(notePath, noteTitle = null) {
if (!noteTitle) {
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
2017-11-05 05:07:03 +08:00
2018-04-20 08:59:44 +08:00
noteTitle = await treeUtils.getNoteTitle(noteId);
}
const noteLink = $("<a>", {
href: 'javascript:',
text: noteTitle
}).attr('data-action', 'note')
.attr('data-note-path', notePath);
return noteLink;
}
function getNotePathFromLink($link) {
const notePathAttr = $link.attr("data-note-path");
if (notePathAttr) {
return notePathAttr;
}
2017-11-05 05:07:03 +08:00
const url = $link.attr('href');
2017-11-05 05:07:03 +08:00
return url ? getNotePathFromUrl(url) : null;
}
2017-12-10 03:11:35 +08:00
function goToLink(e) {
e.preventDefault();
const $link = $(e.target);
const notePath = getNotePathFromLink($link);
if (notePath) {
2019-05-09 01:10:45 +08:00
if (e.ctrlKey) {
2019-05-11 03:43:40 +08:00
noteDetailService.loadNoteDetail(notePath.split("/").pop(), { newTab: true });
2019-05-09 01:10:45 +08:00
}
else {
treeService.activateNote(notePath);
}
}
else {
const address = $link.attr('href');
if (address && address.startsWith('http')) {
window.open(address, '_blank');
}
}
}
function addLinkToEditor(linkTitle, linkHref) {
const editor = noteDetailService.getActiveComponent().getEditor();
editor.model.change( writer => {
const insertPosition = editor.model.document.selection.getFirstPosition();
writer.insertText(linkTitle, { linkHref: linkHref }, insertPosition);
});
}
function addTextToEditor(text) {
const editor = noteDetailService.getActiveComponent().getEditor();
editor.model.change(writer => {
const insertPosition = editor.model.document.selection.getFirstPosition();
writer.insertText(text, insertPosition);
});
}
function init() {
ko.bindingHandlers.noteLink = {
init: async function(element, valueAccessor, allBindings, viewModel, bindingContext) {
const noteId = ko.unwrap(valueAccessor());
if (noteId) {
const link = await createNoteLink(noteId);
$(element).append(link);
}
}
};
}
2019-05-09 01:55:24 +08:00
function tabContextMenu(e) {
const $link = $(e.target);
const notePath = getNotePathFromLink($link);
if (!notePath) {
return;
}
e.preventDefault();
contextMenuService.initContextMenu(e, {
getContextMenuItems: () => {
return [
{title: "Open note in new tab", cmd: "openNoteInNewTab", uiIcon: "empty"}
];
},
selectContextMenuItem: (e, cmd) => {
if (cmd === 'openNoteInNewTab') {
2019-05-11 03:43:40 +08:00
noteDetailService.loadNoteDetail(notePath.split("/").pop(), { newTab: true });
}
}
});
}
2019-05-09 01:55:24 +08:00
$(document).on('contextmenu', '.note-detail-text a', tabContextMenu);
$(document).on('contextmenu', "a[data-action='note']", tabContextMenu);
$(document).on('contextmenu', ".note-detail-render a", tabContextMenu);
// 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[data-action='note']", goToLink);
$(document).on('click', 'div.popover-content a, div.ui-tooltip-content a', goToLink);
2019-05-03 04:24:43 +08:00
$(document).on('dblclick', '.note-detail-text a', goToLink);
2019-05-09 01:10:45 +08:00
$(document).on('click', '.note-detail-text a', function (e) {
const notePath = getNotePathFromLink($(e.target));
if (notePath && e.ctrlKey) {
// if it's a ctrl-click, then we open on new tab, otherwise normal flow (CKEditor opens link-editing dialog)
e.preventDefault();
2019-05-11 03:43:40 +08:00
noteDetailService.loadNoteDetail(notePath.split("/").pop(), { newTab: true });
2019-05-09 01:10:45 +08:00
}
});
2019-05-03 04:24:43 +08:00
$(document).on('click', '.note-detail-render a', goToLink);
$(document).on('click', '.note-detail-text.ck-read-only a', goToLink);
$(document).on('click', 'span.ck-button__label', e => {
// this is a link preview dialog from CKEditor link editing
// for some reason clicked element is span
const url = $(e.target).text();
const notePath = getNotePathFromUrl(url);
if (notePath) {
treeService.activateNote(notePath);
e.preventDefault();
}
});
export default {
getNotePathFromUrl,
createNoteLink,
addLinkToEditor,
addTextToEditor,
init
};