import type { EventNames, EventData } from "../../components/app_context.js";
import appContext from "../../components/app_context.js";
import NoteContext from "../../components/note_context.js";
import { openDialog } from "../../services/dialog.js";
import BasicWidget from "../basic_widget.js";
import Container from "../containers/container.js";
const TPL = /*html*/`\
`;
export default class PopupEditorDialog extends Container {
doRender() {
// This will populate this.$widget with the content of the children.
super.doRender();
// Now we wrap it in the modal.
const $newWidget = $(TPL);
const $modalHeader = $newWidget.find(".modal-title");
const $modalBody = $newWidget.find(".modal-body");
const children = this.$widget.children();
$modalHeader.append(children[0]);
$modalBody.append(children.slice(1));
this.$widget = $newWidget;
}
async refresh(noteIdOrPath: string) {
const noteContext = new NoteContext("_popup-editor");
await noteContext.setNote(noteIdOrPath);
await this.handleEventInChildren("activeContextChanged", { noteContext });
return true;
}
async openInPopupEvent({ noteIdOrPath }: EventData<"openInPopup">) {
if (await this.refresh(noteIdOrPath)) {
const $dialog = await openDialog(this.$widget, false, {
focus: false
});
$dialog.on("shown.bs.modal", () => {
// Reduce the z-index of modals so that ckeditor popups are properly shown on top of it.
// The backdrop instance is not shared so it's OK to make a one-off modification.
$("body > .modal-backdrop").css("z-index", "998");
$dialog.css("z-index", "999");
// Mind map doesn't render off screen properly, so it needs refreshing once the modal is shown.
const $mindmap = $dialog.find(".note-detail-mind-map");
if ($mindmap.length) {
const mindmapComponent = appContext.getComponentByEl($mindmap[0]);
mindmapComponent.refresh();
}
});
}
}
handleEventInChildren(name: T, data: EventData): Promise | null {
// Avoid events related to the current tab interfere with our popup.
if (["noteSwitched", "noteSwitchedAndActivated"].includes(name)) {
return Promise.resolve();
}
return super.handleEventInChildren(name, data);
}
}