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