75 lines
2.0 KiB
JavaScript
Raw Normal View History

2020-01-25 09:56:08 +01:00
import treeService from '../services/tree.js';
import noteAutocompleteService from "../services/note_autocomplete.js";
import utils from "../services/utils.js";
2020-02-02 11:14:44 +01:00
import appContext from "../services/app_context.js";
const $dialog = $("#add-link-dialog");
const $form = $("#add-link-form");
2019-11-05 23:11:25 +01:00
const $autoComplete = $("#add-link-note-autocomplete");
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() {
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;
$dialog.modal();
2019-11-09 17:45:22 +01:00
$autoComplete.val('').trigger('focus');
$linkTitle.val('');
async function setDefaultLinkTitle(noteId) {
2020-01-25 09:56:08 +01:00
const noteTitle = await treeService.getNoteTitle(noteId);
$linkTitle.val(noteTitle);
}
2018-11-07 17:16:33 +01:00
noteAutocompleteService.initNoteAutocomplete($autoComplete);
2018-11-07 17:16:33 +01:00
$autoComplete.on('autocomplete:selected', function(event, suggestion, dataset) {
if (!suggestion.path) {
return false;
}
2020-01-25 09:56:08 +01:00
const noteId = treeService.getNoteIdFromNotePath(suggestion.path);
2018-11-07 17:16:33 +01:00
if (noteId) {
setDefaultLinkTitle(noteId);
}
});
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-11-07 17:16:33 +01:00
setDefaultLinkTitle(noteId);
});
2018-11-07 17:16:33 +01:00
noteAutocompleteService.showRecentNotes($autoComplete);
}
2019-11-09 17:45:22 +01:00
$form.on('submit', () => {
2018-11-07 17:16:33 +01:00
const notePath = $autoComplete.getSelectedPath();
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-11-07 17:16:33 +01:00
else {
console.error("No path to add link.");
}
return false;
2020-02-02 10:41:43 +01:00
});