2018-03-26 01:41:29 +08:00
|
|
|
import cloningService from '../services/cloning.js';
|
|
|
|
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");
|
|
|
|
const $autoComplete = $("#note-autocomplete");
|
|
|
|
const $linkTitle = $("#link-title");
|
|
|
|
const $clonePrefix = $("#clone-prefix");
|
|
|
|
const $linkTitleFormGroup = $("#add-link-title-form-group");
|
|
|
|
const $prefixFormGroup = $("#add-link-prefix-form-group");
|
2018-05-26 22:04:40 +08:00
|
|
|
const $linkTypeDiv = $("#add-link-type-div");
|
2018-03-25 23:09:17 +08:00
|
|
|
const $linkTypes = $("input[name='add-link-type']");
|
|
|
|
const $linkTypeHtml = $linkTypes.filter('input[value="html"]');
|
|
|
|
|
|
|
|
function setLinkType(linkType) {
|
|
|
|
$linkTypes.each(function () {
|
|
|
|
$(this).prop('checked', $(this).val() === linkType);
|
|
|
|
});
|
2017-11-05 02:43:20 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
linkTypeChanged();
|
|
|
|
}
|
2017-11-05 05:03:15 +08:00
|
|
|
|
2019-03-26 04:09:15 +08:00
|
|
|
async function showDialogForClone() {
|
2019-03-30 17:15:19 +08:00
|
|
|
showDialog('selected-to-active');
|
2019-03-26 04:09:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function showDialog(linkType) {
|
2019-06-11 04:45:03 +08:00
|
|
|
utils.closeActiveDialog();
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
glob.activeDialog = $dialog;
|
2017-12-22 10:54:25 +08:00
|
|
|
|
2019-03-15 03:21:27 +08:00
|
|
|
if (noteDetailService.getActiveNoteType() === 'text') {
|
2018-03-25 23:09:17 +08:00
|
|
|
$linkTypeHtml.prop('disabled', false);
|
2017-12-22 10:54:25 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
setLinkType('html');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$linkTypeHtml.prop('disabled', true);
|
2017-12-22 10:54:25 +08:00
|
|
|
|
2019-03-30 17:15:19 +08:00
|
|
|
setLinkType('selected-to-active');
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2019-03-30 17:15:19 +08:00
|
|
|
if (linkType === 'selected-to-active') {
|
2019-03-26 04:09:15 +08:00
|
|
|
setLinkType(linkType);
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
$clonePrefix.val('');
|
|
|
|
$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();
|
2018-03-25 23:09:17 +08:00
|
|
|
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (notePath) {
|
|
|
|
const linkType = $("input[name='add-link-type']:checked").val();
|
2017-12-22 10:54:25 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (linkType === 'html') {
|
|
|
|
const linkTitle = $linkTitle.val();
|
2017-12-22 10:54:25 +08:00
|
|
|
|
2018-11-07 00:47:40 +08:00
|
|
|
$dialog.modal('hide');
|
2017-12-22 10:54:25 +08:00
|
|
|
|
2018-05-26 22:04:40 +08:00
|
|
|
const linkHref = '#' + notePath;
|
2019-05-19 15:13:13 +08:00
|
|
|
const editor = noteDetailService.getActiveEditor();
|
2018-05-26 22:04:40 +08:00
|
|
|
|
|
|
|
if (hasSelection()) {
|
|
|
|
editor.execute('link', linkHref);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
linkService.addLinkToEditor(linkTitle, linkHref);
|
|
|
|
}
|
2019-05-14 03:13:04 +08:00
|
|
|
|
|
|
|
editor.editing.view.focus();
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2019-03-30 17:15:19 +08:00
|
|
|
else if (linkType === 'selected-to-active') {
|
2018-03-25 23:09:17 +08:00
|
|
|
const prefix = $clonePrefix.val();
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2019-03-15 03:21:27 +08:00
|
|
|
cloningService.cloneNoteTo(noteId, noteDetailService.getActiveNoteId(), prefix);
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2018-11-07 00:47:40 +08:00
|
|
|
$dialog.modal('hide');
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2019-03-30 17:15:19 +08:00
|
|
|
else if (linkType === 'active-to-selected') {
|
2018-03-25 23:09:17 +08:00
|
|
|
const prefix = $clonePrefix.val();
|
2017-12-22 10:54:25 +08:00
|
|
|
|
2019-03-15 03:21:27 +08:00
|
|
|
cloningService.cloneNoteTo(noteDetailService.getActiveNoteId(), noteId, prefix);
|
2017-12-22 10:54:25 +08:00
|
|
|
|
2018-11-07 00:47:40 +08:00
|
|
|
$dialog.modal('hide');
|
2017-11-05 02:43:20 +08:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
function linkTypeChanged() {
|
|
|
|
const value = $linkTypes.filter(":checked").val();
|
2018-01-24 09:14:10 +08:00
|
|
|
|
2018-05-26 22:04:40 +08:00
|
|
|
$linkTitleFormGroup.toggle(!hasSelection() && value === 'html');
|
|
|
|
$prefixFormGroup.toggle(!hasSelection() && value !== 'html');
|
|
|
|
|
|
|
|
$linkTypeDiv.toggle(!hasSelection());
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2018-01-24 09:14:10 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
$linkTypes.change(linkTypeChanged);
|
2017-12-22 10:54:25 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
export default {
|
2019-03-26 04:09:15 +08:00
|
|
|
showDialog,
|
|
|
|
showDialogForClone
|
2018-03-25 23:09:17 +08:00
|
|
|
};
|