2018-03-26 01:41:29 +08:00
|
|
|
import treeService from './tree.js';
|
2018-03-27 12:22:02 +08:00
|
|
|
import noteDetailText from './note_detail_text.js';
|
2018-03-25 23:09:17 +08:00
|
|
|
import treeUtils from './tree_utils.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
|
|
|
|
2018-07-28 23:59:55 +08:00
|
|
|
function getNotePathFromLabel(label) {
|
2018-10-07 05:11:42 +08:00
|
|
|
const notePathMatch = / \((root[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-08-07 17:38:00 +08:00
|
|
|
async function createNoteLink(notePath, noteTitle = null) {
|
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
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
const noteLink = $("<a>", {
|
|
|
|
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
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
return noteLink;
|
|
|
|
}
|
2017-12-23 20:48:59 +08:00
|
|
|
|
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) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
const $link = $(e.target);
|
|
|
|
|
|
|
|
const notePath = getNotePathFromLink($link);
|
|
|
|
|
|
|
|
if (notePath) {
|
2018-11-14 18:17:20 +08:00
|
|
|
treeService.activateNote(notePath);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function addLinkToEditor(linkTitle, linkHref) {
|
2018-04-20 08:59:44 +08:00
|
|
|
const editor = noteDetailText.getEditor();
|
2018-03-25 23:09:17 +08:00
|
|
|
|
2018-04-13 08:03:23 +08:00
|
|
|
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) {
|
2018-03-27 12:22:02 +08:00
|
|
|
const editor = noteDetailText.getEditor();
|
2018-03-25 23:09:17 +08:00
|
|
|
|
2018-08-30 02:22:57 +08:00
|
|
|
editor.model.change(writer => {
|
|
|
|
const insertPosition = editor.model.document.selection.getFirstPosition();
|
|
|
|
writer.insertText(text, insertPosition);
|
|
|
|
});
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
|
|
|
|
2018-12-24 17:10:36 +08:00
|
|
|
function init() {
|
|
|
|
ko.bindingHandlers.noteLink = {
|
|
|
|
init: async function(element, valueAccessor, allBindings, viewModel, bindingContext) {
|
|
|
|
const noteId = ko.unwrap(valueAccessor());
|
2018-08-07 17:38:00 +08:00
|
|
|
|
2018-12-24 17:10:36 +08:00
|
|
|
if (noteId) {
|
|
|
|
const link = await createNoteLink(noteId);
|
2018-08-07 17:38:00 +08:00
|
|
|
|
2018-12-24 17:10:36 +08:00
|
|
|
$(element).append(link);
|
|
|
|
}
|
2018-08-07 17:38:00 +08:00
|
|
|
}
|
2018-12-24 17:10:36 +08:00
|
|
|
};
|
|
|
|
}
|
2018-08-07 17:38:00 +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
|
2018-08-15 16:14:14 +08:00
|
|
|
$(document).on('click', "a[data-action='note']", goToLink);
|
2018-03-25 23:09:17 +08:00
|
|
|
$(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-10-07 05:11:42 +08: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 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-07-28 23:59:55 +08:00
|
|
|
getNotePathFromLabel,
|
2018-10-07 05:11:42 +08:00
|
|
|
getNotePathFromUrl,
|
2018-03-25 23:09:17 +08:00
|
|
|
createNoteLink,
|
|
|
|
addLinkToEditor,
|
2018-12-24 17:10:36 +08:00
|
|
|
addTextToEditor,
|
|
|
|
init
|
2018-03-25 23:09:17 +08:00
|
|
|
};
|