2024-07-22 17:31:54 +08:00
|
|
|
import { t } from "../../services/i18n.js";
|
2022-06-16 11:03:04 +02:00
|
|
|
import treeService from '../../services/tree.js';
|
|
|
|
import noteAutocompleteService from "../../services/note_autocomplete.js";
|
|
|
|
import utils from "../../services/utils.js";
|
|
|
|
import BasicWidget from "../basic_widget.js";
|
|
|
|
|
|
|
|
const TPL = `
|
|
|
|
<div class="add-link-dialog modal mx-auto" tabindex="-1" role="dialog">
|
|
|
|
<div class="modal-dialog modal-lg" style="max-width: 1000px" role="document">
|
|
|
|
<div class="modal-content">
|
|
|
|
<div class="modal-header">
|
2024-09-03 18:15:10 +02:00
|
|
|
<h5 class="modal-title flex-grow-1">${t('add_link.add_link')}</h5>
|
2024-08-11 06:26:32 +03:00
|
|
|
<button type="button" class="help-button" title="${t('add_link.help_on_links')}" data-help-page="links.html">?</button>
|
2024-09-03 18:15:10 +02:00
|
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="${t('add_link.close')}"></button>
|
2022-06-16 11:03:04 +02:00
|
|
|
</div>
|
|
|
|
<form class="add-link-form">
|
|
|
|
<div class="modal-body">
|
|
|
|
<div class="form-group">
|
2024-07-22 17:31:54 +08:00
|
|
|
<label for="add-link-note-autocomplete">${t('add_link.note')}</label>
|
2022-06-16 11:03:04 +02:00
|
|
|
|
|
|
|
<div class="input-group">
|
2024-07-22 17:31:54 +08:00
|
|
|
<input class="add-link-note-autocomplete form-control" placeholder="${t('add_link.search_note')}">
|
2022-06-16 11:03:04 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="add-link-title-settings">
|
|
|
|
<div class="add-link-title-radios form-check">
|
|
|
|
<label class="form-check-label">
|
|
|
|
<input class="form-check-input" type="radio" name="link-type" value="reference-link" checked>
|
2024-07-22 17:31:54 +08:00
|
|
|
${t('add_link.link_title_mirrors')}
|
2022-06-16 11:03:04 +02:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
<div class="add-link-title-radios form-check">
|
|
|
|
<label class="form-check-label">
|
|
|
|
<input class="form-check-input" type="radio" name="link-type" value="hyper-link">
|
2024-07-22 17:31:54 +08:00
|
|
|
${t('add_link.link_title_arbitrary')}
|
2022-06-16 11:03:04 +02:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="add-link-title-form-group form-group">
|
|
|
|
<br/>
|
|
|
|
<label>
|
2024-07-22 17:31:54 +08:00
|
|
|
${t('add_link.link_title')}
|
2022-06-16 11:03:04 +02:00
|
|
|
|
|
|
|
<input class="link-title form-control" style="width: 100%;">
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="modal-footer">
|
2024-12-22 18:21:49 +01:00
|
|
|
<button type="submit" class="btn btn-primary">${t('add_link.button_add_link')}</button>
|
2022-06-16 11:03:04 +02:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
export default class AddLinkDialog extends BasicWidget {
|
|
|
|
doRender() {
|
|
|
|
this.$widget = $(TPL);
|
|
|
|
this.$form = this.$widget.find(".add-link-form");
|
|
|
|
this.$autoComplete = this.$widget.find(".add-link-note-autocomplete");
|
|
|
|
this.$linkTitle = this.$widget.find(".link-title");
|
|
|
|
this.$addLinkTitleSettings = this.$widget.find(".add-link-title-settings");
|
|
|
|
this.$addLinkTitleRadios = this.$widget.find(".add-link-title-radios");
|
|
|
|
this.$addLinkTitleFormGroup = this.$widget.find(".add-link-title-form-group");
|
|
|
|
|
|
|
|
/** @var TextTypeWidget */
|
|
|
|
this.textTypeWidget = null;
|
|
|
|
|
|
|
|
this.$form.on('submit', () => {
|
|
|
|
if (this.$autoComplete.getSelectedNotePath()) {
|
|
|
|
this.$widget.modal('hide');
|
|
|
|
|
|
|
|
const linkTitle = this.getLinkType() === 'reference-link' ? null : this.$linkTitle.val();
|
|
|
|
|
|
|
|
this.textTypeWidget.addLink(this.$autoComplete.getSelectedNotePath(), linkTitle);
|
|
|
|
}
|
|
|
|
else if (this.$autoComplete.getSelectedExternalLink()) {
|
|
|
|
this.$widget.modal('hide');
|
|
|
|
|
|
|
|
this.textTypeWidget.addLink(this.$autoComplete.getSelectedExternalLink(), this.$linkTitle.val());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logError("No link to add.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async showAddLinkDialogEvent({textTypeWidget, text = ''}) {
|
|
|
|
this.textTypeWidget = textTypeWidget;
|
|
|
|
|
|
|
|
this.$addLinkTitleSettings.toggle(!this.textTypeWidget.hasSelection());
|
|
|
|
|
|
|
|
this.$addLinkTitleSettings.find('input[type=radio]').on('change', e => this.updateTitleSettingsVisibility(e));
|
|
|
|
|
2023-01-15 21:04:17 +01:00
|
|
|
// with selection hyperlink is implied
|
2022-06-16 11:03:04 +02:00
|
|
|
if (this.textTypeWidget.hasSelection()) {
|
|
|
|
this.$addLinkTitleSettings.find("input[value='hyper-link']").prop("checked", true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.$addLinkTitleSettings.find("input[value='reference-link']").prop("checked", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateTitleSettingsVisibility();
|
|
|
|
|
|
|
|
utils.openDialog(this.$widget);
|
|
|
|
|
|
|
|
this.$autoComplete.val('');
|
|
|
|
this.$linkTitle.val('');
|
|
|
|
|
|
|
|
const setDefaultLinkTitle = async noteId => {
|
|
|
|
const noteTitle = await treeService.getNoteTitle(noteId);
|
|
|
|
|
|
|
|
this.$linkTitle.val(noteTitle);
|
|
|
|
};
|
|
|
|
|
|
|
|
noteAutocompleteService.initNoteAutocomplete(this.$autoComplete, {
|
|
|
|
allowExternalLinks: true,
|
|
|
|
allowCreatingNotes: true
|
|
|
|
});
|
|
|
|
|
|
|
|
this.$autoComplete.on('autocomplete:noteselected', (event, suggestion, dataset) => {
|
|
|
|
if (!suggestion.notePath) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateTitleSettingsVisibility();
|
|
|
|
|
2023-05-29 22:37:19 +02:00
|
|
|
const noteId = treeService.getNoteIdFromUrl(suggestion.notePath);
|
2022-06-16 11:03:04 +02:00
|
|
|
|
|
|
|
if (noteId) {
|
|
|
|
setDefaultLinkTitle(noteId);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.$autoComplete.on('autocomplete:externallinkselected', (event, suggestion, dataset) => {
|
|
|
|
if (!suggestion.externalLink) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateTitleSettingsVisibility();
|
|
|
|
|
|
|
|
this.$linkTitle.val(suggestion.externalLink);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.$autoComplete.on('autocomplete:cursorchanged', (event, suggestion, dataset) => {
|
|
|
|
if (suggestion.externalLink) {
|
|
|
|
this.$linkTitle.val(suggestion.externalLink)
|
|
|
|
}
|
|
|
|
else {
|
2023-05-29 22:37:19 +02:00
|
|
|
const noteId = treeService.getNoteIdFromUrl(suggestion.notePath);
|
2022-06-16 11:03:04 +02:00
|
|
|
|
|
|
|
if (noteId) {
|
|
|
|
setDefaultLinkTitle(noteId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (text && text.trim()) {
|
|
|
|
noteAutocompleteService.setText(this.$autoComplete, text);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
noteAutocompleteService.showRecentNotes(this.$autoComplete);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$autoComplete
|
|
|
|
.trigger('focus')
|
|
|
|
.trigger('select'); // to be able to quickly remove entered text
|
|
|
|
}
|
|
|
|
|
|
|
|
getLinkType() {
|
|
|
|
if (this.$autoComplete.getSelectedExternalLink()) {
|
|
|
|
return 'external-link';
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.$addLinkTitleSettings.find('input[type=radio]:checked').val();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateTitleSettingsVisibility() {
|
|
|
|
const linkType = this.getLinkType();
|
|
|
|
|
|
|
|
this.$addLinkTitleFormGroup.toggle(linkType !== 'reference-link');
|
|
|
|
this.$addLinkTitleRadios.toggle(linkType !== 'external-link')
|
|
|
|
}
|
|
|
|
}
|