import { t } from "../../services/i18n.js"; import treeService from "../../services/tree.js"; import noteAutocompleteService from "../../services/note_autocomplete.js"; import utils from "../../services/utils.js"; import froca from "../../services/froca.js"; import BasicWidget from "../basic_widget.js"; import { Modal } from "bootstrap"; const TPL = ` `; export default class IncludeNoteDialog extends BasicWidget { doRender() { this.$widget = $(TPL); this.modal = Modal.getOrCreateInstance(this.$widget); this.$form = this.$widget.find(".include-note-form"); this.$autoComplete = this.$widget.find(".include-note-autocomplete"); this.$form.on("submit", () => { const notePath = this.$autoComplete.getSelectedNotePath(); if (notePath) { this.modal.hide(); this.includeNote(notePath); } else { logError("No noteId to include."); } return false; }); } async showIncludeNoteDialogEvent({ textTypeWidget }) { this.textTypeWidget = textTypeWidget; await this.refresh(); utils.openDialog(this.$widget); this.$autoComplete.trigger("focus").trigger("select"); // to be able to quickly remove entered text } async refresh(widget) { this.$autoComplete.val(""); noteAutocompleteService.initNoteAutocomplete(this.$autoComplete, { hideGoToSelectedNoteButton: true, allowCreatingNotes: true }); noteAutocompleteService.showRecentNotes(this.$autoComplete); } async includeNote(notePath) { const noteId = treeService.getNoteIdFromUrl(notePath); const note = await froca.getNote(noteId); const boxSize = $("input[name='include-note-box-size']:checked").val(); if (["image", "canvas", "mermaid"].includes(note.type)) { // there's no benefit to use insert note functionlity for images, // so we'll just add an IMG tag this.textTypeWidget.addImage(noteId); } else { this.textTypeWidget.addIncludeNote(noteId, boxSize); } } }