2018-03-26 01:41:29 +08:00
|
|
|
import treeService from './tree.js';
|
|
|
|
import noteDetailService from './note_detail.js';
|
2018-03-25 23:09:17 +08:00
|
|
|
import treeUtils from './tree_utils.js';
|
2017-11-05 05:07:03 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
function getNotePathFromLink(url) {
|
|
|
|
const notePathMatch = /#([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
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
function getNodePathFromLabel(label) {
|
|
|
|
const notePathMatch = / \(([A-Za-z0-9/]+)\)/.exec(label);
|
2017-11-30 11:03:03 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (notePathMatch !== null) {
|
|
|
|
return notePathMatch[1];
|
|
|
|
}
|
2017-11-22 09:04:06 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
return null;
|
|
|
|
}
|
2017-11-05 05:07:03 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
function createNoteLink(notePath, noteTitle) {
|
|
|
|
if (!noteTitle) {
|
|
|
|
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
|
2017-11-05 05:07:03 +08:00
|
|
|
|
2018-03-27 09:50:47 +08:00
|
|
|
noteTitle = treeUtils.getNoteTitle(noteId);
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-12-23 20:48:59 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
const noteLink = $("<a>", {
|
|
|
|
href: 'javascript:',
|
|
|
|
text: noteTitle
|
|
|
|
}).attr('action', 'note')
|
|
|
|
.attr('note-path', notePath);
|
2017-12-23 20:48:59 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
return noteLink;
|
|
|
|
}
|
2017-12-23 20:48:59 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
function goToLink(e) {
|
|
|
|
e.preventDefault();
|
2017-12-23 20:48:59 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
const $link = $(e.target);
|
|
|
|
let notePath = $link.attr("note-path");
|
2017-12-23 20:48:59 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (!notePath) {
|
|
|
|
const address = $link.attr("note-path") ? $link.attr("note-path") : $link.attr('href');
|
2018-01-03 09:16:17 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (!address) {
|
|
|
|
return;
|
2018-01-03 09:16:17 +08:00
|
|
|
}
|
2017-12-26 22:10:54 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (address.startsWith('http')) {
|
|
|
|
window.open(address, '_blank');
|
2017-11-05 05:07:03 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
return;
|
2017-11-05 05:07:03 +08:00
|
|
|
}
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
notePath = getNotePathFromLink(address);
|
2017-12-10 03:11:35 +08:00
|
|
|
}
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
treeService.activateNode(notePath);
|
2017-12-10 03:17:03 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
// this is quite ugly hack, but it seems like we can't close the tooltip otherwise
|
|
|
|
$("[role='tooltip']").remove();
|
2017-12-10 03:17:03 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (glob.activeDialog) {
|
|
|
|
try {
|
|
|
|
glob.activeDialog.dialog('close');
|
|
|
|
}
|
|
|
|
catch (e) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addLinkToEditor(linkTitle, linkHref) {
|
2018-03-26 01:41:29 +08:00
|
|
|
const editor = noteDetailService.getEditor();
|
2018-03-25 23:09:17 +08:00
|
|
|
const doc = editor.document;
|
|
|
|
|
|
|
|
doc.enqueueChanges(() => editor.data.insertLink(linkTitle, linkHref), doc.selection);
|
|
|
|
}
|
|
|
|
|
|
|
|
function addTextToEditor(text) {
|
2018-03-26 01:41:29 +08:00
|
|
|
const editor = noteDetailService.getEditor();
|
2018-03-25 23:09:17 +08:00
|
|
|
const doc = editor.document;
|
|
|
|
|
|
|
|
doc.enqueueChanges(() => editor.data.insertText(text), doc.selection);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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[action='note']", goToLink);
|
|
|
|
$(document).on('click', 'div.popover-content a, div.ui-tooltip-content a', goToLink);
|
2018-03-27 11:48:45 +08:00
|
|
|
$(document).on('dblclick', '#note-detail-text a', goToLink);
|
2018-03-25 23:09:17 +08:00
|
|
|
|
|
|
|
export default {
|
|
|
|
getNodePathFromLabel,
|
|
|
|
getNotePathFromLink,
|
|
|
|
createNoteLink,
|
|
|
|
addLinkToEditor,
|
|
|
|
addTextToEditor
|
|
|
|
};
|