2018-03-26 01:41:29 +08:00
|
|
|
import treeService from './tree.js';
|
2018-03-25 23:09:17 +08:00
|
|
|
import treeUtils from './tree_utils.js';
|
2019-05-08 04:33:53 +08:00
|
|
|
import contextMenuService from "./context_menu.js";
|
|
|
|
import noteDetailService from "./note_detail.js";
|
2017-11-05 05:07:03 +08:00
|
|
|
|
2018-10-07 05:11:42 +08:00
|
|
|
function getNotePathFromUrl(url) {
|
2018-10-07 03:32:07 +08:00
|
|
|
const notePathMatch = /#(root[A-Za-z0-9/]*)$/.exec(url);
|
2017-11-05 05:07:03 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (notePathMatch === null) {
|
2017-11-05 05:07:03 +08:00
|
|
|
return null;
|
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
else {
|
|
|
|
return notePathMatch[1];
|
|
|
|
}
|
|
|
|
}
|
2017-11-05 05:07:03 +08:00
|
|
|
|
2019-10-02 03:41:20 +08:00
|
|
|
async function createNoteLink(notePath, noteTitle = null, tooltip = true) {
|
2018-03-25 23:09:17 +08:00
|
|
|
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);
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-12-23 20:48:59 +08:00
|
|
|
|
2019-10-02 03:11:11 +08:00
|
|
|
const $noteLink = $("<a>", {
|
2018-03-25 23:09:17 +08:00
|
|
|
href: 'javascript:',
|
|
|
|
text: noteTitle
|
2018-08-15 16:14:14 +08:00
|
|
|
}).attr('data-action', 'note')
|
|
|
|
.attr('data-note-path', notePath);
|
2017-12-23 20:48:59 +08:00
|
|
|
|
2019-10-02 03:41:20 +08:00
|
|
|
if (!tooltip) {
|
|
|
|
$noteLink.addClass("no-tooltip-preview");
|
|
|
|
}
|
2019-10-02 03:11:11 +08:00
|
|
|
|
|
|
|
return $noteLink;
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-12-23 20:48:59 +08:00
|
|
|
|
2019-09-03 02:26:18 +08:00
|
|
|
async function createNoteLinkWithPath(notePath, noteTitle = null) {
|
|
|
|
const $link = await createNoteLink(notePath, noteTitle);
|
2019-09-01 19:42:10 +08:00
|
|
|
|
|
|
|
const $res = $("<span>").append($link);
|
|
|
|
|
|
|
|
if (notePath.includes("/")) {
|
|
|
|
const noteIds = notePath.split("/");
|
|
|
|
noteIds.pop(); // remove last element
|
|
|
|
|
|
|
|
const parentNotePath = noteIds.join("/").trim();
|
|
|
|
|
|
|
|
if (parentNotePath) {
|
|
|
|
$res.append($("<small>").text(" (" + await treeUtils.getNotePathTitle(parentNotePath) + ")"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2018-10-07 05:11:42 +08:00
|
|
|
function getNotePathFromLink($link) {
|
|
|
|
const notePathAttr = $link.attr("data-note-path");
|
2017-12-23 20:48:59 +08:00
|
|
|
|
2018-10-07 05:11:42 +08:00
|
|
|
if (notePathAttr) {
|
|
|
|
return notePathAttr;
|
|
|
|
}
|
2017-11-05 05:07:03 +08:00
|
|
|
|
2018-10-07 05:11:42 +08:00
|
|
|
const url = $link.attr('href');
|
2017-11-05 05:07:03 +08:00
|
|
|
|
2018-10-07 05:11:42 +08:00
|
|
|
return url ? getNotePathFromUrl(url) : null;
|
|
|
|
}
|
2017-12-10 03:11:35 +08:00
|
|
|
|
2018-10-07 05:11:42 +08:00
|
|
|
function goToLink(e) {
|
|
|
|
const $link = $(e.target);
|
|
|
|
|
|
|
|
const notePath = getNotePathFromLink($link);
|
|
|
|
|
|
|
|
if (notePath) {
|
2019-05-16 03:50:27 +08:00
|
|
|
if ((e.which === 1 && e.ctrlKey) || e.which === 2) {
|
|
|
|
noteDetailService.openInTab(notePath);
|
2019-05-09 01:10:45 +08:00
|
|
|
}
|
2019-05-16 03:50:27 +08:00
|
|
|
else if (e.which === 1) {
|
2019-05-09 01:10:45 +08:00
|
|
|
treeService.activateNote(notePath);
|
|
|
|
}
|
2019-05-16 03:50:27 +08:00
|
|
|
else {
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-07 05:11:42 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
const address = $link.attr('href');
|
|
|
|
|
|
|
|
if (address && address.startsWith('http')) {
|
|
|
|
window.open(address, '_blank');
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
|
|
|
}
|
2019-05-16 03:50:27 +08:00
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
|
|
return true;
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function addLinkToEditor(linkTitle, linkHref) {
|
2019-05-19 15:13:13 +08:00
|
|
|
const editor = noteDetailService.getActiveEditor();
|
2018-03-25 23:09:17 +08:00
|
|
|
|
2019-05-19 15:13:13 +08:00
|
|
|
if (editor) {
|
|
|
|
editor.model.change(writer => {
|
|
|
|
const insertPosition = editor.model.document.selection.getFirstPosition();
|
|
|
|
writer.insertText(linkTitle, {linkHref: linkHref}, insertPosition);
|
|
|
|
});
|
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function addTextToEditor(text) {
|
2019-05-19 15:13:13 +08:00
|
|
|
const editor = noteDetailService.getActiveEditor();
|
2018-03-25 23:09:17 +08:00
|
|
|
|
2019-05-19 15:13:13 +08:00
|
|
|
if (editor) {
|
|
|
|
editor.model.change(writer => {
|
|
|
|
const insertPosition = editor.model.document.selection.getFirstPosition();
|
|
|
|
writer.insertText(text, insertPosition);
|
|
|
|
});
|
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
|
|
|
|
2019-05-09 01:55:24 +08:00
|
|
|
function tabContextMenu(e) {
|
2019-05-08 04:33:53 +08:00
|
|
|
const $link = $(e.target);
|
|
|
|
|
|
|
|
const notePath = getNotePathFromLink($link);
|
|
|
|
|
|
|
|
if (!notePath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
contextMenuService.initContextMenu(e, {
|
|
|
|
getContextMenuItems: () => {
|
|
|
|
return [
|
2019-05-22 02:43:03 +08:00
|
|
|
{title: "Open note in new tab", cmd: "openNoteInNewTab", uiIcon: "arrow-up-right"}
|
2019-05-08 04:33:53 +08:00
|
|
|
];
|
|
|
|
},
|
|
|
|
selectContextMenuItem: (e, cmd) => {
|
|
|
|
if (cmd === 'openNoteInNewTab') {
|
2019-05-11 03:43:40 +08:00
|
|
|
noteDetailService.loadNoteDetail(notePath.split("/").pop(), { newTab: true });
|
2019-05-08 04:33:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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);
|
2019-05-08 04:33:53 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
// 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
|
2019-05-16 03:50:27 +08:00
|
|
|
$(document).on('mousedown', "a[data-action='note']", goToLink);
|
|
|
|
$(document).on('mousedown', '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-16 03:50:27 +08:00
|
|
|
$(document).on('mousedown', '.note-detail-text a', function (e) {
|
2019-05-09 01:10:45 +08:00
|
|
|
const notePath = getNotePathFromLink($(e.target));
|
2019-05-16 03:50:27 +08:00
|
|
|
if (notePath && ((e.which === 1 && e.ctrlKey) || e.which === 2)) {
|
2019-05-09 01:10:45 +08:00
|
|
|
// if it's a ctrl-click, then we open on new tab, otherwise normal flow (CKEditor opens link-editing dialog)
|
|
|
|
e.preventDefault();
|
|
|
|
|
2019-05-16 03:50:27 +08:00
|
|
|
noteDetailService.loadNoteDetail(notePath, { newTab: true });
|
|
|
|
|
|
|
|
return true;
|
2019-05-09 01:10:45 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-05-16 03:50:27 +08:00
|
|
|
$(document).on('mousedown', '.note-detail-render a', goToLink);
|
|
|
|
$(document).on('mousedown', '.note-detail-text.ck-read-only a', goToLink);
|
|
|
|
$(document).on('mousedown', 'span.ck-button__label', e => {
|
2018-10-07 05:11:42 +08:00
|
|
|
// 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) {
|
2018-11-14 18:17:20 +08:00
|
|
|
treeService.activateNote(notePath);
|
2018-10-07 05:11:42 +08:00
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
2018-03-25 23:09:17 +08:00
|
|
|
|
|
|
|
export default {
|
2018-10-07 05:11:42 +08:00
|
|
|
getNotePathFromUrl,
|
2018-03-25 23:09:17 +08:00
|
|
|
createNoteLink,
|
2019-09-01 19:42:10 +08:00
|
|
|
createNoteLinkWithPath,
|
2018-03-25 23:09:17 +08:00
|
|
|
addLinkToEditor,
|
2018-12-24 17:10:36 +08:00
|
|
|
addTextToEditor,
|
2019-05-16 03:50:27 +08:00
|
|
|
goToLink
|
2018-03-25 23:09:17 +08:00
|
|
|
};
|