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"; import type { EventData } from "../../components/app_context.js"; import type EditableTextTypeWidget from "../type_widgets/editable_text.js"; const TPL = ` `; export default class IncludeNoteDialog extends BasicWidget { private modal!: bootstrap.Modal; private $form!: JQuery; private $autoComplete!: JQuery; private textTypeWidget?: EditableTextTypeWidget; doRender() { this.$widget = $(TPL); this.modal = Modal.getOrCreateInstance(this.$widget[0]); 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 }: EventData<"showIncludeDialog">) { 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() { this.$autoComplete.val(""); noteAutocompleteService.initNoteAutocomplete(this.$autoComplete, { hideGoToSelectedNoteButton: true, allowCreatingNotes: true }); noteAutocompleteService.showRecentNotes(this.$autoComplete); } async includeNote(notePath: string) { const noteId = treeService.getNoteIdFromUrl(notePath); if (!noteId) { return; } const note = await froca.getNote(noteId); const boxSize = $("input[name='include-note-box-size']:checked").val() as string; 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); } } }