2020-01-25 09:56:08 +01:00
|
|
|
import treeService from '../services/tree.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";
|
2020-02-02 11:14:44 +01:00
|
|
|
import appContext from "../services/app_context.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();
|
|
|
|
|
2020-02-02 11:14:44 +01:00
|
|
|
appContext.trigger('executeInActiveEditor', {
|
|
|
|
callback: textEditor => {
|
|
|
|
const hasSelection = !textEditor.model.document.selection.isCollapsed;
|
|
|
|
|
|
|
|
$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
|
|
|
|
2019-11-09 17:45:22 +01:00
|
|
|
$autoComplete.val('').trigger('focus');
|
2018-03-25 11:09:17 -04:00
|
|
|
$linkTitle.val('');
|
2017-09-09 12:06:15 -04:00
|
|
|
|
2018-04-12 20:03:23 -04:00
|
|
|
async function setDefaultLinkTitle(noteId) {
|
2020-01-25 09:56:08 +01:00
|
|
|
const noteTitle = await treeService.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
|
|
|
|
2020-01-25 09:56:08 +01:00
|
|
|
const noteId = treeService.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) {
|
2020-01-25 09:56:08 +01:00
|
|
|
const noteId = treeService.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
|
|
|
|
2019-11-09 17:45:22 +01:00
|
|
|
$form.on('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
|
|
|
$dialog.modal('hide');
|
2017-12-21 21:54:25 -05:00
|
|
|
|
2020-02-02 10:41:43 +01:00
|
|
|
appContext.trigger(`addLinkToActiveEditor`, {
|
|
|
|
linkTitle: $linkTitle.val(),
|
|
|
|
linkHref: '#' + notePath
|
|
|
|
});
|
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;
|
2020-02-02 10:41:43 +01:00
|
|
|
});
|