Notes/src/public/app/widgets/containers/split_note_container.ts

244 lines
8.6 KiB
TypeScript
Raw Normal View History

2021-05-19 22:45:34 +02:00
import FlexContainer from "./flex_container.js";
2022-12-01 13:07:23 +01:00
import appContext from "../../components/app_context.js";
2025-03-02 21:52:48 +01:00
import NoteContext from "../../components/note_context.js";
import type { CommandMappings, EventNames, EventData } from "../../components/app_context.js";
import type BasicWidget from "../basic_widget.js";
2021-05-19 22:45:34 +02:00
2025-03-02 21:52:48 +01:00
interface NoteContextEvent {
noteContext: NoteContext;
}
interface SplitNoteWidget extends BasicWidget {
hasBeenAlreadyShown?: boolean;
ntxId?: string;
}
type WidgetFactory = () => SplitNoteWidget;
interface Widgets {
[key: string]: SplitNoteWidget;
}
export default class SplitNoteContainer extends FlexContainer<SplitNoteWidget> {
private widgetFactory: WidgetFactory;
private widgets: Widgets;
constructor(widgetFactory: WidgetFactory) {
2025-01-09 18:07:02 +02:00
super("row");
2021-05-19 22:45:34 +02:00
this.widgetFactory = widgetFactory;
2021-05-20 23:13:34 +02:00
this.widgets = {};
2021-05-19 22:45:34 +02:00
2025-01-09 18:07:02 +02:00
this.class("split-note-container-widget");
this.css("flex-grow", "1");
2021-06-13 22:55:31 +02:00
this.collapsible();
2021-05-19 22:45:34 +02:00
}
2025-03-02 21:52:48 +01:00
async newNoteContextCreatedEvent({ noteContext }: NoteContextEvent) {
2021-05-20 23:13:34 +02:00
const widget = this.widgetFactory();
2021-05-19 23:00:03 +02:00
2021-05-20 23:13:34 +02:00
const $renderedWidget = widget.render();
2021-05-19 23:00:03 +02:00
2021-05-24 21:05:44 +02:00
$renderedWidget.attr("data-ntx-id", noteContext.ntxId);
2025-01-09 18:07:02 +02:00
$renderedWidget.on("click", () => appContext.tabManager.activateNoteContext(noteContext.ntxId));
2021-05-19 23:00:03 +02:00
2021-05-21 22:34:40 +02:00
this.$widget.append($renderedWidget);
2021-05-19 22:45:34 +02:00
2025-03-02 21:52:48 +01:00
widget.handleEvent("initialRenderComplete", {});
2021-05-22 22:55:24 +02:00
2021-05-22 17:58:46 +02:00
widget.toggleExt(false);
2025-03-02 21:52:48 +01:00
if (noteContext.ntxId) {
this.widgets[noteContext.ntxId] = widget;
}
2021-05-19 23:00:03 +02:00
2025-01-09 18:07:02 +02:00
await widget.handleEvent("setNoteContext", { noteContext });
2021-05-19 22:45:34 +02:00
2021-05-20 23:13:34 +02:00
this.child(widget);
}
2021-05-19 22:45:34 +02:00
2025-03-02 21:52:48 +01:00
async openNewNoteSplitEvent({ ntxId, notePath, hoistedNoteId, viewScope }: {
ntxId: string;
notePath?: string;
hoistedNoteId?: string;
viewScope?: any;
}) {
const mainNtxId = appContext.tabManager.getActiveMainContext()?.ntxId;
2022-12-08 23:29:08 +01:00
2025-03-02 21:52:48 +01:00
if (!mainNtxId) {
logError("empty mainNtxId!");
return;
}
if (!ntxId) {
logError("empty ntxId!");
2022-12-08 23:29:08 +01:00
ntxId = mainNtxId;
}
hoistedNoteId = hoistedNoteId || appContext.tabManager.getActiveContext()?.hoistedNoteId;
2022-12-08 23:29:08 +01:00
const noteContext = await appContext.tabManager.openEmptyTab(null, hoistedNoteId, mainNtxId);
2021-05-19 22:45:34 +02:00
2021-05-24 21:43:24 +02:00
// remove the original position of newly created note context
2025-01-09 18:07:02 +02:00
const ntxIds = appContext.tabManager.children.map((c) => c.ntxId).filter((id) => id !== noteContext.ntxId);
2021-05-24 21:05:44 +02:00
2021-05-24 21:43:24 +02:00
// insert the note context after the originating note context
2021-05-24 21:05:44 +02:00
ntxIds.splice(ntxIds.indexOf(ntxId) + 1, 0, noteContext.ntxId);
2025-03-02 21:52:48 +01:00
this.triggerCommand("noteContextReorder" as keyof CommandMappings, { ntxIdsInOrder: ntxIds });
2021-05-24 21:05:44 +02:00
2021-05-24 21:43:24 +02:00
// move the note context rendered widget after the originating widget
2025-01-09 18:07:02 +02:00
this.$widget.find(`[data-ntx-id="${noteContext.ntxId}"]`).insertAfter(this.$widget.find(`[data-ntx-id="${ntxId}"]`));
2021-05-24 21:05:44 +02:00
2021-05-22 12:35:41 +02:00
await appContext.tabManager.activateNoteContext(noteContext.ntxId);
2021-05-20 23:13:34 +02:00
if (notePath) {
await noteContext.setNote(notePath, { viewScope });
2025-01-09 18:07:02 +02:00
} else {
await noteContext.setEmpty();
}
2021-05-20 23:13:34 +02:00
}
2021-05-19 22:45:34 +02:00
2025-03-02 21:52:48 +01:00
closeThisNoteSplitCommand({ ntxId }: { ntxId: string }): void {
2021-05-24 22:29:49 +02:00
appContext.tabManager.removeNoteContext(ntxId);
}
2025-03-02 21:52:48 +01:00
async moveThisNoteSplitCommand({ ntxId, isMovingLeft }: { ntxId: string; isMovingLeft: boolean }): Promise<void> {
2023-05-30 02:24:56 +08:00
if (!ntxId) {
logError("empty ntxId!");
return;
}
const contexts = appContext.tabManager.noteContexts;
2025-01-09 18:07:02 +02:00
const currentIndex = contexts.findIndex((c) => c.ntxId === ntxId);
2023-05-31 01:53:55 +08:00
const leftIndex = isMovingLeft ? currentIndex - 1 : currentIndex;
2023-05-30 02:24:56 +08:00
2023-05-31 01:53:55 +08:00
if (currentIndex === -1 || leftIndex < 0 || leftIndex + 1 >= contexts.length) {
2023-06-01 00:48:37 +08:00
logError(`invalid context! currentIndex: ${currentIndex}, leftIndex: ${leftIndex}, contexts.length: ${contexts.length}`);
2023-05-30 02:24:56 +08:00
return;
}
2023-06-01 00:48:37 +08:00
if (contexts[leftIndex].isEmpty() && contexts[leftIndex + 1].isEmpty()) {
2023-05-30 02:24:56 +08:00
// no op
return;
2023-06-01 00:48:37 +08:00
}
2023-05-30 02:24:56 +08:00
2025-01-09 18:07:02 +02:00
const ntxIds = contexts.map((c) => c.ntxId);
const newNtxIds = [...ntxIds.slice(0, leftIndex), ntxIds[leftIndex + 1], ntxIds[leftIndex], ...ntxIds.slice(leftIndex + 2)];
2023-06-01 00:48:37 +08:00
const isChangingMainContext = !contexts[leftIndex].mainNtxId;
2023-05-30 02:24:56 +08:00
2025-03-02 21:52:48 +01:00
this.triggerCommand("noteContextReorder" as keyof CommandMappings, {
2023-06-01 00:48:37 +08:00
ntxIdsInOrder: newNtxIds,
oldMainNtxId: isChangingMainContext ? ntxIds[leftIndex] : null,
2025-01-09 18:07:02 +02:00
newMainNtxId: isChangingMainContext ? ntxIds[leftIndex + 1] : null
2023-05-31 01:53:55 +08:00
});
2023-06-01 00:48:37 +08:00
// reorder the note context widgets
2025-01-09 18:07:02 +02:00
this.$widget.find(`[data-ntx-id="${ntxIds[leftIndex]}"]`).insertAfter(this.$widget.find(`[data-ntx-id="${ntxIds[leftIndex + 1]}"]`));
2023-05-30 02:24:56 +08:00
2023-05-31 01:53:55 +08:00
// activate context that now contains the original note
await appContext.tabManager.activateNoteContext(isMovingLeft ? ntxIds[leftIndex + 1] : ntxIds[leftIndex]);
2023-05-30 02:24:56 +08:00
}
2025-03-02 21:52:48 +01:00
activeContextChangedEvent(): void {
2021-05-22 17:58:46 +02:00
this.refresh();
}
2025-03-02 21:52:48 +01:00
noteSwitchedAndActivatedEvent(): void {
2021-05-22 17:58:46 +02:00
this.refresh();
}
2025-03-02 21:52:48 +01:00
noteContextRemovedEvent({ ntxIds }: { ntxIds: string[] }): void {
this.children = this.children.filter((c) => c.ntxId && !ntxIds.includes(c.ntxId));
2021-05-24 22:29:49 +02:00
for (const ntxId of ntxIds) {
this.$widget.find(`[data-ntx-id="${ntxId}"]`).remove();
delete this.widgets[ntxId];
}
}
2025-03-02 21:52:48 +01:00
contextsReopenedEvent({ ntxId, afterNtxId }: { ntxId?: string; afterNtxId?: string }): void {
if (ntxId === undefined || afterNtxId === undefined) {
// no single split reopened
return;
}
2025-01-09 18:07:02 +02:00
this.$widget.find(`[data-ntx-id="${ntxId}"]`).insertAfter(this.$widget.find(`[data-ntx-id="${afterNtxId}"]`));
}
2025-03-02 21:52:48 +01:00
async refresh(): Promise<void> {
2021-05-21 22:34:40 +02:00
this.toggleExt(true);
}
2025-03-02 21:52:48 +01:00
toggleExt(show: boolean): void {
2021-05-22 13:04:08 +02:00
const activeMainContext = appContext.tabManager.getActiveMainContext();
const activeNtxId = activeMainContext ? activeMainContext.ntxId : null;
2021-05-21 22:34:40 +02:00
2021-05-22 12:26:45 +02:00
for (const ntxId in this.widgets) {
const noteContext = appContext.tabManager.getNoteContextById(ntxId);
2021-05-24 21:43:24 +02:00
2021-05-22 12:26:45 +02:00
const widget = this.widgets[ntxId];
2025-03-02 21:52:48 +01:00
widget.toggleExt(show && activeNtxId !== null && [noteContext.ntxId, noteContext.mainNtxId].includes(activeNtxId));
2021-05-21 22:34:40 +02:00
}
}
2021-05-20 23:13:34 +02:00
/**
* widget.hasBeenAlreadyShown is intended for lazy loading of cached tabs - initial note switches of new tabs
* are not executed, we're waiting for the first tab activation, and then we update the tab. After this initial
2023-06-30 11:18:34 +02:00
* activation, further note switches are always propagated to the tabs.
2021-05-20 23:13:34 +02:00
*/
2025-03-02 21:52:48 +01:00
handleEventInChildren<T extends EventNames>(name: T, data: EventData<T>): Promise<any> | null {
2025-01-09 18:07:02 +02:00
if (["noteSwitched", "noteSwitchedAndActivated"].includes(name)) {
2021-05-20 23:13:34 +02:00
// this event is propagated only to the widgets of a particular tab
2025-03-02 21:52:48 +01:00
const noteContext = (data as NoteContextEvent).noteContext;
const widget = noteContext.ntxId ? this.widgets[noteContext.ntxId] : undefined;
2021-05-20 23:13:34 +02:00
2021-05-21 22:34:40 +02:00
if (!widget) {
return Promise.resolve();
}
2025-03-03 22:56:24 +01:00
if (widget.hasBeenAlreadyShown || name === "noteSwitchedAndActivated" || appContext.tabManager.getActiveMainContext() === noteContext.getMainContext()) {
2021-05-22 17:58:46 +02:00
widget.hasBeenAlreadyShown = true;
2021-05-21 22:34:40 +02:00
2025-03-02 21:52:48 +01:00
return Promise.all([
widget.handleEvent("noteSwitched", { noteContext, notePath: noteContext.notePath }),
this.refreshNotShown({ noteContext })
]);
2025-01-09 18:07:02 +02:00
} else {
2021-05-22 17:58:46 +02:00
return Promise.resolve();
2021-05-20 23:13:34 +02:00
}
}
2025-01-09 18:07:02 +02:00
if (name === "activeContextChanged") {
2025-03-02 21:52:48 +01:00
return this.refreshNotShown(data as NoteContextEvent);
2021-05-22 17:58:46 +02:00
} else {
return super.handleEventInChildren(name, data);
}
}
2021-05-20 23:13:34 +02:00
2025-03-02 21:52:48 +01:00
private refreshNotShown(data: NoteContextEvent): Promise<any> {
const promises: Promise<any>[] = [];
2021-05-20 23:13:34 +02:00
2021-05-22 17:58:46 +02:00
for (const subContext of data.noteContext.getMainContext().getSubContexts()) {
2025-03-02 21:52:48 +01:00
if (!subContext.ntxId) {
continue;
}
2021-05-22 17:58:46 +02:00
const widget = this.widgets[subContext.ntxId];
2021-05-21 22:34:40 +02:00
2021-05-22 17:58:46 +02:00
if (!widget.hasBeenAlreadyShown) {
widget.hasBeenAlreadyShown = true;
2021-05-21 22:34:40 +02:00
2025-03-02 21:52:48 +01:00
const eventPromise = widget.handleEvent("activeContextChanged", { noteContext: subContext });
promises.push(eventPromise || Promise.resolve());
2021-05-20 23:13:34 +02:00
}
}
2021-05-22 17:58:46 +02:00
2021-05-22 22:55:24 +02:00
this.refresh();
2021-05-22 17:58:46 +02:00
return Promise.all(promises);
2021-05-19 22:45:34 +02:00
}
}