2018-03-25 13:41:29 -04:00
|
|
|
import linkService from '../services/link.js';
|
|
|
|
import noteDetailService from '../services/note_detail.js';
|
2018-03-25 13:02:39 -04:00
|
|
|
import treeUtils from '../services/tree_utils.js';
|
2018-08-16 21:02:42 +02:00
|
|
|
import noteAutocompleteService from "../services/note_autocomplete.js";
|
2019-06-10 22:45:03 +02:00
|
|
|
import utils from "../services/utils.js";
|
2018-03-25 11:09:17 -04:00
|
|
|
|
|
|
|
const $dialog = $("#add-link-dialog");
|
|
|
|
const $form = $("#add-link-form");
|
2019-11-05 23:11:25 +01:00
|
|
|
const $autoComplete = $("#add-link-note-autocomplete");
|
2018-03-25 11:09:17 -04:00
|
|
|
const $linkTitle = $("#link-title");
|
2019-11-05 22:40:44 +01:00
|
|
|
const $addLinkTitleFormGroup = $("#add-link-title-form-group");
|
2017-11-04 17:03:15 -04:00
|
|
|
|
2019-11-05 22:40:44 +01:00
|
|
|
export async function showDialog() {
|
2019-06-10 22:45:03 +02:00
|
|
|
utils.closeActiveDialog();
|
|
|
|
|
2019-11-05 22:40:44 +01:00
|
|
|
$addLinkTitleFormGroup.toggle(!hasSelection());
|
2017-12-21 21:54:25 -05:00
|
|
|
|
2019-11-05 22:40:44 +01:00
|
|
|
glob.activeDialog = $dialog;
|
2019-03-25 21:09:15 +01:00
|
|
|
|
2018-11-06 17:47:40 +01:00
|
|
|
$dialog.modal();
|
2017-09-09 12:06:15 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
$autoComplete.val('').focus();
|
|
|
|
$linkTitle.val('');
|
2017-09-09 12:06:15 -04:00
|
|
|
|
2018-04-12 20:03:23 -04:00
|
|
|
async function setDefaultLinkTitle(noteId) {
|
|
|
|
const noteTitle = await treeUtils.getNoteTitle(noteId);
|
2018-03-25 11:09:17 -04:00
|
|
|
|
|
|
|
$linkTitle.val(noteTitle);
|
|
|
|
}
|
2017-09-09 12:06:15 -04:00
|
|
|
|
2018-11-07 17:16:33 +01:00
|
|
|
noteAutocompleteService.initNoteAutocomplete($autoComplete);
|
2018-07-26 16:24:08 +02:00
|
|
|
|
2018-11-07 17:16:33 +01:00
|
|
|
$autoComplete.on('autocomplete:selected', function(event, suggestion, dataset) {
|
|
|
|
if (!suggestion.path) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-25 11:09:17 -04:00
|
|
|
|
2018-11-07 17:16:33 +01:00
|
|
|
const noteId = treeUtils.getNoteIdFromNotePath(suggestion.path);
|
2017-09-09 12:06:15 -04:00
|
|
|
|
2018-11-07 17:16:33 +01:00
|
|
|
if (noteId) {
|
|
|
|
setDefaultLinkTitle(noteId);
|
|
|
|
}
|
|
|
|
});
|
2018-03-25 11:09:17 -04:00
|
|
|
|
2018-11-07 17:16:33 +01:00
|
|
|
$autoComplete.on('autocomplete:cursorchanged', function(event, suggestion, dataset) {
|
|
|
|
const noteId = treeUtils.getNoteIdFromNotePath(suggestion.path);
|
2018-07-26 16:24:08 +02:00
|
|
|
|
2018-11-07 17:16:33 +01:00
|
|
|
setDefaultLinkTitle(noteId);
|
2018-03-25 11:09:17 -04:00
|
|
|
});
|
2018-07-26 16:24:08 +02:00
|
|
|
|
2018-11-07 17:16:33 +01:00
|
|
|
noteAutocompleteService.showRecentNotes($autoComplete);
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2017-09-09 12:06:15 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
$form.submit(() => {
|
2018-11-07 17:16:33 +01:00
|
|
|
const notePath = $autoComplete.getSelectedPath();
|
2017-09-09 12:06:15 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
if (notePath) {
|
2019-11-05 22:40:44 +01:00
|
|
|
const linkTitle = $linkTitle.val();
|
2017-12-21 21:54:25 -05:00
|
|
|
|
2019-11-05 22:40:44 +01:00
|
|
|
$dialog.modal('hide');
|
2017-12-21 21:54:25 -05:00
|
|
|
|
2019-11-05 22:40:44 +01:00
|
|
|
const linkHref = '#' + notePath;
|
|
|
|
const editor = noteDetailService.getActiveEditor();
|
2018-05-26 10:04:40 -04:00
|
|
|
|
2019-11-05 22:40:44 +01:00
|
|
|
if (hasSelection()) {
|
|
|
|
editor.execute('link', linkHref);
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2019-11-05 22:40:44 +01:00
|
|
|
else {
|
|
|
|
linkService.addLinkToEditor(linkTitle, linkHref);
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2017-12-21 21:54:25 -05:00
|
|
|
|
2019-11-05 22:40:44 +01:00
|
|
|
editor.editing.view.focus();
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2018-11-07 17:16:33 +01:00
|
|
|
else {
|
|
|
|
console.error("No path to add link.");
|
|
|
|
}
|
2017-09-09 12:06:15 -04:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
return false;
|
|
|
|
});
|
2017-09-09 12:06:15 -04:00
|
|
|
|
2018-05-26 10:04:40 -04:00
|
|
|
// returns true if user selected some text, false if there's no selection
|
|
|
|
function hasSelection() {
|
2019-05-19 09:13:13 +02:00
|
|
|
const model = noteDetailService.getActiveEditor().model;
|
2018-05-26 10:04:40 -04:00
|
|
|
const selection = model.document.selection;
|
|
|
|
|
|
|
|
return !selection.isCollapsed;
|
2019-11-05 22:40:44 +01:00
|
|
|
}
|