trilium/src/public/javascripts/dialogs/add_link.js

185 lines
5.2 KiB
JavaScript
Raw Normal View History

import cloningService from '../services/cloning.js';
import linkService from '../services/link.js';
import noteDetailService from '../services/note_detail.js';
import treeUtils from '../services/tree_utils.js';
2018-05-26 22:04:40 +08:00
import server from "../services/server.js";
import noteDetailText from "../services/note_detail_text.js";
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");
const $linkTypes = $("input[name='add-link-type']");
const $linkTypeHtml = $linkTypes.filter('input[value="html"]');
const $showRecentNotesButton = $("#add-link-show-recent-notes");
function setLinkType(linkType) {
$linkTypes.each(function () {
$(this).prop('checked', $(this).val() === linkType);
});
2017-11-05 02:43:20 +08:00
linkTypeChanged();
}
2017-11-05 05:03:15 +08:00
async function showDialog() {
glob.activeDialog = $dialog;
2017-12-22 10:54:25 +08:00
if (noteDetailService.getCurrentNoteType() === 'text') {
$linkTypeHtml.prop('disabled', false);
2017-12-22 10:54:25 +08:00
setLinkType('html');
}
else {
$linkTypeHtml.prop('disabled', true);
2017-12-22 10:54:25 +08:00
setLinkType('selected-to-current');
}
$dialog.dialog({
modal: true,
width: 700
});
$autoComplete.val('').focus();
$clonePrefix.val('');
$linkTitle.val('');
async function setDefaultLinkTitle(noteId) {
const noteTitle = await treeUtils.getNoteTitle(noteId);
$linkTitle.val(noteTitle);
}
await $autoComplete.autocomplete({
2018-05-26 22:04:40 +08:00
source: async function(request, response) {
const result = await server.get('autocomplete?query=' + encodeURIComponent(request.term));
2018-06-08 11:08:41 +08:00
if (result.length > 0) {
response(result.map(row => {
return {
label: row.label,
value: row.label + ' (' + row.value + ')'
}
}));
2018-06-08 11:08:41 +08:00
}
else {
response([{
label: "No results",
value: "No results"
}]);
}
2018-05-26 22:04:40 +08:00
},
minLength: 0,
change: async (event, ui) => {
if (!ui.item) {
return;
}
const notePath = linkService.getNodePathFromLabel(ui.item.value);
if (!notePath) {
return;
}
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
if (noteId) {
await setDefaultLinkTitle(noteId);
}
},
select: function (event, ui) {
if (ui.item.value === 'No results') {
return false;
}
},
// this is called when user goes through autocomplete list with keyboard
// at this point the item isn't selected yet so we use supplied ui.item to see WHERE the cursor is
focus: async (event, ui) => {
const notePath = linkService.getNodePathFromLabel(ui.item.value);
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
await setDefaultLinkTitle(noteId);
event.preventDefault();
}
});
showRecentNotes();
}
$form.submit(() => {
const value = $autoComplete.val();
const notePath = linkService.getNodePathFromLabel(value);
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
if (notePath) {
const linkType = $("input[name='add-link-type']:checked").val();
2017-12-22 10:54:25 +08:00
if (linkType === 'html') {
const linkTitle = $linkTitle.val();
2017-12-22 10:54:25 +08:00
$dialog.dialog("close");
2017-12-22 10:54:25 +08:00
2018-05-26 22:04:40 +08:00
const linkHref = '#' + notePath;
if (hasSelection()) {
const editor = noteDetailText.getEditor();
editor.execute('link', linkHref);
}
else {
linkService.addLinkToEditor(linkTitle, linkHref);
}
}
else if (linkType === 'selected-to-current') {
const prefix = $clonePrefix.val();
cloningService.cloneNoteTo(noteId, noteDetailService.getCurrentNoteId(), prefix);
$dialog.dialog("close");
}
else if (linkType === 'current-to-selected') {
const prefix = $clonePrefix.val();
2017-12-22 10:54:25 +08:00
cloningService.cloneNoteTo(noteDetailService.getCurrentNoteId(), noteId, prefix);
2017-12-22 10:54:25 +08:00
$dialog.dialog("close");
2017-11-05 02:43:20 +08:00
}
}
return false;
});
2018-05-26 22:04:40 +08:00
// returns true if user selected some text, false if there's no selection
function hasSelection() {
const model = noteDetailText.getEditor().model;
const selection = model.document.selection;
return !selection.isCollapsed;
}
function linkTypeChanged() {
const value = $linkTypes.filter(":checked").val();
2018-05-26 22:04:40 +08:00
$linkTitleFormGroup.toggle(!hasSelection() && value === 'html');
$prefixFormGroup.toggle(!hasSelection() && value !== 'html');
$linkTypeDiv.toggle(!hasSelection());
}
function showRecentNotes() {
$autoComplete.autocomplete("search", "");
}
$linkTypes.change(linkTypeChanged);
2017-12-22 10:54:25 +08:00
$showRecentNotesButton.click(showRecentNotes);
export default {
showDialog
};