2018-03-25 13:41:29 -04:00
|
|
|
import treeService from './tree.js';
|
2018-03-27 00:22:02 -04:00
|
|
|
import noteDetailText from './note_detail_text.js';
|
2018-03-25 11:09:17 -04:00
|
|
|
import treeUtils from './tree_utils.js';
|
2017-11-04 17:07:03 -04:00
|
|
|
|
2018-10-06 23:11:42 +02:00
|
|
|
function getNotePathFromUrl(url) {
|
2018-10-06 21:32:07 +02:00
|
|
|
const notePathMatch = /#(root[A-Za-z0-9/]*)$/.exec(url);
|
2017-11-04 17:07:03 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
if (notePathMatch === null) {
|
2017-11-04 17:07:03 -04:00
|
|
|
return null;
|
|
|
|
}
|
2018-03-25 11:09:17 -04:00
|
|
|
else {
|
|
|
|
return notePathMatch[1];
|
|
|
|
}
|
|
|
|
}
|
2017-11-04 17:07:03 -04:00
|
|
|
|
2018-07-28 17:59:55 +02:00
|
|
|
function getNotePathFromLabel(label) {
|
2018-10-06 23:11:42 +02:00
|
|
|
const notePathMatch = / \((root[A-Za-z0-9/]*)\)/.exec(label);
|
2017-11-29 22:03:03 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
if (notePathMatch !== null) {
|
|
|
|
return notePathMatch[1];
|
|
|
|
}
|
2017-11-21 20:04:06 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
return null;
|
|
|
|
}
|
2017-11-04 17:07:03 -04:00
|
|
|
|
2018-08-07 11:38:00 +02:00
|
|
|
async function createNoteLink(notePath, noteTitle = null) {
|
2018-03-25 11:09:17 -04:00
|
|
|
if (!noteTitle) {
|
|
|
|
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
|
2017-11-04 17:07:03 -04:00
|
|
|
|
2018-04-19 20:59:44 -04:00
|
|
|
noteTitle = await treeUtils.getNoteTitle(noteId);
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2017-12-23 07:48:59 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
const noteLink = $("<a>", {
|
|
|
|
href: 'javascript:',
|
|
|
|
text: noteTitle
|
2018-08-15 10:14:14 +02:00
|
|
|
}).attr('data-action', 'note')
|
|
|
|
.attr('data-note-path', notePath);
|
2017-12-23 07:48:59 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
return noteLink;
|
|
|
|
}
|
2017-12-23 07:48:59 -05:00
|
|
|
|
2018-10-06 23:11:42 +02:00
|
|
|
function getNotePathFromLink($link) {
|
|
|
|
const notePathAttr = $link.attr("data-note-path");
|
2017-12-23 07:48:59 -05:00
|
|
|
|
2018-10-06 23:11:42 +02:00
|
|
|
if (notePathAttr) {
|
|
|
|
return notePathAttr;
|
|
|
|
}
|
2017-11-04 17:07:03 -04:00
|
|
|
|
2018-10-06 23:11:42 +02:00
|
|
|
const url = $link.attr('href');
|
2017-11-04 17:07:03 -04:00
|
|
|
|
2018-10-06 23:11:42 +02:00
|
|
|
return url ? getNotePathFromUrl(url) : null;
|
|
|
|
}
|
2017-12-09 14:11:35 -05:00
|
|
|
|
2018-10-06 23:11:42 +02:00
|
|
|
function goToLink(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const $link = $(e.target);
|
|
|
|
|
|
|
|
const notePath = getNotePathFromLink($link);
|
|
|
|
|
|
|
|
if (notePath) {
|
2018-11-14 11:17:20 +01:00
|
|
|
treeService.activateNote(notePath);
|
2018-10-06 23:11:42 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
const address = $link.attr('href');
|
|
|
|
|
|
|
|
if (address && address.startsWith('http')) {
|
|
|
|
window.open(address, '_blank');
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addLinkToEditor(linkTitle, linkHref) {
|
2018-04-19 20:59:44 -04:00
|
|
|
const editor = noteDetailText.getEditor();
|
2018-03-25 11:09:17 -04:00
|
|
|
|
2018-04-12 20:03:23 -04:00
|
|
|
editor.model.change( writer => {
|
|
|
|
const insertPosition = editor.model.document.selection.getFirstPosition();
|
|
|
|
writer.insertText(linkTitle, { linkHref: linkHref }, insertPosition);
|
|
|
|
});
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function addTextToEditor(text) {
|
2018-03-27 00:22:02 -04:00
|
|
|
const editor = noteDetailText.getEditor();
|
2018-03-25 11:09:17 -04:00
|
|
|
|
2018-08-29 20:22:57 +02:00
|
|
|
editor.model.change(writer => {
|
|
|
|
const insertPosition = editor.model.document.selection.getFirstPosition();
|
|
|
|
writer.insertText(text, insertPosition);
|
|
|
|
});
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
|
|
|
|
2018-12-24 10:10:36 +01:00
|
|
|
function init() {
|
|
|
|
ko.bindingHandlers.noteLink = {
|
|
|
|
init: async function(element, valueAccessor, allBindings, viewModel, bindingContext) {
|
|
|
|
const noteId = ko.unwrap(valueAccessor());
|
2018-08-07 11:38:00 +02:00
|
|
|
|
2018-12-24 10:10:36 +01:00
|
|
|
if (noteId) {
|
|
|
|
const link = await createNoteLink(noteId);
|
2018-08-07 11:38:00 +02:00
|
|
|
|
2018-12-24 10:10:36 +01:00
|
|
|
$(element).append(link);
|
|
|
|
}
|
2018-08-07 11:38:00 +02:00
|
|
|
}
|
2018-12-24 10:10:36 +01:00
|
|
|
};
|
|
|
|
}
|
2018-08-07 11:38:00 +02:00
|
|
|
|
2018-03-25 11:09:17 -04: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
|
2018-08-15 10:14:14 +02:00
|
|
|
$(document).on('click', "a[data-action='note']", goToLink);
|
2018-03-25 11:09:17 -04:00
|
|
|
$(document).on('click', 'div.popover-content a, div.ui-tooltip-content a', goToLink);
|
2019-05-02 22:24:43 +02:00
|
|
|
$(document).on('dblclick', '.note-detail-text a', goToLink);
|
|
|
|
$(document).on('click', '.note-detail-render a', goToLink);
|
|
|
|
$(document).on('click', '.note-detail-text.ck-read-only a', goToLink);
|
2018-10-06 23:11:42 +02:00
|
|
|
$(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) {
|
2018-11-14 11:17:20 +01:00
|
|
|
treeService.activateNote(notePath);
|
2018-10-06 23:11:42 +02:00
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
});
|
2018-03-25 11:09:17 -04:00
|
|
|
|
|
|
|
export default {
|
2018-07-28 17:59:55 +02:00
|
|
|
getNotePathFromLabel,
|
2018-10-06 23:11:42 +02:00
|
|
|
getNotePathFromUrl,
|
2018-03-25 11:09:17 -04:00
|
|
|
createNoteLink,
|
|
|
|
addLinkToEditor,
|
2018-12-24 10:10:36 +01:00
|
|
|
addTextToEditor,
|
|
|
|
init
|
2018-03-25 11:09:17 -04:00
|
|
|
};
|