2024-07-24 17:34:26 +08:00
|
|
|
import { t } from "../../services/i18n.js";
|
2025-01-09 18:07:02 +02:00
|
|
|
import treeService from "../../services/tree.js";
|
|
|
|
import noteAutocompleteService from "../../services/note_autocomplete.js";
|
2022-06-14 23:51:16 +02:00
|
|
|
import utils from "../../services/utils.js";
|
|
|
|
import froca from "../../services/froca.js";
|
|
|
|
import BasicWidget from "../basic_widget.js";
|
2025-02-21 20:41:00 +01:00
|
|
|
import { Modal } from "bootstrap";
|
2025-03-19 23:27:52 +02:00
|
|
|
import type { EventData } from "../../components/app_context.js";
|
|
|
|
import type EditableTextTypeWidget from "../type_widgets/editable_text.js";
|
2022-06-14 23:51:16 +02:00
|
|
|
|
|
|
|
const TPL = `
|
|
|
|
<div class="include-note-dialog modal mx-auto" tabindex="-1" role="dialog">
|
|
|
|
<div class="modal-dialog modal-lg" role="document">
|
|
|
|
<div class="modal-content">
|
|
|
|
<div class="modal-header">
|
2025-01-09 18:07:02 +02:00
|
|
|
<h5 class="modal-title">${t("include_note.dialog_title")}</h5>
|
|
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="${t("include_note.close")}"></button>
|
2022-06-14 23:51:16 +02:00
|
|
|
</div>
|
|
|
|
<form class="include-note-form">
|
|
|
|
<div class="modal-body">
|
|
|
|
<div class="form-group">
|
2025-01-09 18:07:02 +02:00
|
|
|
<label for="include-note-autocomplete">${t("include_note.label_note")}</label>
|
2022-06-14 23:51:16 +02:00
|
|
|
<div class="input-group">
|
2025-01-09 18:07:02 +02:00
|
|
|
<input class="include-note-autocomplete form-control" placeholder="${t("include_note.placeholder_search")}">
|
2022-06-14 23:51:16 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
${t("include_note.box_size_prompt")}
|
2022-06-14 23:51:16 +02:00
|
|
|
|
|
|
|
<div class="form-check">
|
2025-02-03 17:06:04 +02:00
|
|
|
<label class="form-check-label tn-radio">
|
|
|
|
<input class="form-check-input" type="radio" name="include-note-box-size" value="small">
|
|
|
|
${t("include_note.box_size_small")}
|
|
|
|
</label>
|
2022-06-14 23:51:16 +02:00
|
|
|
</div>
|
|
|
|
<div class="form-check">
|
2025-02-03 17:06:04 +02:00
|
|
|
<label class="form-check-label tn-radio">
|
|
|
|
<input class="form-check-input" type="radio" name="include-note-box-size" value="medium" checked>
|
|
|
|
${t("include_note.box_size_medium")}
|
|
|
|
</label>
|
2022-06-14 23:51:16 +02:00
|
|
|
</div>
|
|
|
|
<div class="form-check">
|
2025-02-03 17:06:04 +02:00
|
|
|
<label class="form-check-label tn-radio">
|
|
|
|
<input class="form-check-input" type="radio" name="include-note-box-size" value="full">
|
|
|
|
${t("include_note.box_size_full")}
|
|
|
|
</label>
|
2022-06-14 23:51:16 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="modal-footer">
|
2025-01-09 18:07:02 +02:00
|
|
|
<button type="submit" class="btn btn-primary">${t("include_note.button_include")}</button>
|
2022-06-14 23:51:16 +02:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
export default class IncludeNoteDialog extends BasicWidget {
|
2025-03-19 23:27:52 +02:00
|
|
|
|
|
|
|
private modal!: bootstrap.Modal;
|
|
|
|
private $form!: JQuery<HTMLElement>;
|
|
|
|
private $autoComplete!: JQuery<HTMLElement>;
|
|
|
|
private textTypeWidget?: EditableTextTypeWidget;
|
|
|
|
|
2022-06-14 23:51:16 +02:00
|
|
|
doRender() {
|
|
|
|
this.$widget = $(TPL);
|
2025-03-19 23:27:52 +02:00
|
|
|
this.modal = Modal.getOrCreateInstance(this.$widget[0]);
|
2022-06-14 23:51:16 +02:00
|
|
|
this.$form = this.$widget.find(".include-note-form");
|
|
|
|
this.$autoComplete = this.$widget.find(".include-note-autocomplete");
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$form.on("submit", () => {
|
2022-06-14 23:51:16 +02:00
|
|
|
const notePath = this.$autoComplete.getSelectedNotePath();
|
|
|
|
|
|
|
|
if (notePath) {
|
2024-09-03 18:15:10 +02:00
|
|
|
this.modal.hide();
|
2022-06-14 23:51:16 +02:00
|
|
|
this.includeNote(notePath);
|
2024-07-24 17:34:26 +08:00
|
|
|
} else {
|
2022-06-14 23:51:16 +02:00
|
|
|
logError("No noteId to include.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2025-01-09 18:07:02 +02:00
|
|
|
});
|
2022-06-14 23:51:16 +02:00
|
|
|
}
|
|
|
|
|
2025-03-19 23:27:52 +02:00
|
|
|
async showIncludeNoteDialogEvent({ textTypeWidget }: EventData<"showIncludeDialog">) {
|
2022-06-14 23:51:16 +02:00
|
|
|
this.textTypeWidget = textTypeWidget;
|
|
|
|
await this.refresh();
|
|
|
|
utils.openDialog(this.$widget);
|
2024-08-27 00:30:54 +03:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$autoComplete.trigger("focus").trigger("select"); // to be able to quickly remove entered text
|
2022-06-14 23:51:16 +02:00
|
|
|
}
|
|
|
|
|
2025-03-19 23:27:52 +02:00
|
|
|
async refresh() {
|
2025-01-09 18:07:02 +02:00
|
|
|
this.$autoComplete.val("");
|
2022-06-14 23:51:16 +02:00
|
|
|
noteAutocompleteService.initNoteAutocomplete(this.$autoComplete, {
|
|
|
|
hideGoToSelectedNoteButton: true,
|
|
|
|
allowCreatingNotes: true
|
|
|
|
});
|
|
|
|
noteAutocompleteService.showRecentNotes(this.$autoComplete);
|
|
|
|
}
|
|
|
|
|
2025-03-19 23:27:52 +02:00
|
|
|
async includeNote(notePath: string) {
|
2023-05-29 22:37:19 +02:00
|
|
|
const noteId = treeService.getNoteIdFromUrl(notePath);
|
2025-03-19 23:27:52 +02:00
|
|
|
if (!noteId) {
|
|
|
|
return;
|
|
|
|
}
|
2022-06-14 23:51:16 +02:00
|
|
|
const note = await froca.getNote(noteId);
|
|
|
|
const boxSize = $("input[name='include-note-box-size']:checked").val();
|
|
|
|
|
2025-03-19 23:27:52 +02:00
|
|
|
if (["image", "canvas", "mermaid"].includes(note?.type ?? "")) {
|
2023-01-15 21:04:17 +01:00
|
|
|
// there's no benefit to use insert note functionlity for images,
|
2022-06-14 23:51:16 +02:00
|
|
|
// so we'll just add an IMG tag
|
2025-03-19 23:27:52 +02:00
|
|
|
this.textTypeWidget?.addImage(noteId);
|
2024-07-24 17:34:26 +08:00
|
|
|
} else {
|
2025-03-19 23:27:52 +02:00
|
|
|
this.textTypeWidget?.addIncludeNote(noteId, boxSize);
|
2022-06-14 23:51:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|