port split note container to ts

This commit is contained in:
Jin 2025-03-02 21:52:48 +01:00
parent 67509bc92f
commit 8e0b9d17a4

View File

@ -1,8 +1,29 @@
import FlexContainer from "./flex_container.js"; import FlexContainer from "./flex_container.js";
import appContext from "../../components/app_context.js"; import appContext from "../../components/app_context.js";
import NoteContext from "../../components/note_context.js";
import type { CommandMappings, EventNames, EventData } from "../../components/app_context.js";
import type BasicWidget from "../basic_widget.js";
export default class SplitNoteContainer extends FlexContainer { interface NoteContextEvent {
constructor(widgetFactory) { 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) {
super("row"); super("row");
this.widgetFactory = widgetFactory; this.widgetFactory = widgetFactory;
@ -13,7 +34,7 @@ export default class SplitNoteContainer extends FlexContainer {
this.collapsible(); this.collapsible();
} }
async newNoteContextCreatedEvent({ noteContext }) { async newNoteContextCreatedEvent({ noteContext }: NoteContextEvent) {
const widget = this.widgetFactory(); const widget = this.widgetFactory();
const $renderedWidget = widget.render(); const $renderedWidget = widget.render();
@ -23,20 +44,32 @@ export default class SplitNoteContainer extends FlexContainer {
this.$widget.append($renderedWidget); this.$widget.append($renderedWidget);
widget.handleEvent("initialRenderComplete"); widget.handleEvent("initialRenderComplete", {});
widget.toggleExt(false); widget.toggleExt(false);
this.widgets[noteContext.ntxId] = widget; if (noteContext.ntxId) {
this.widgets[noteContext.ntxId] = widget;
}
await widget.handleEvent("setNoteContext", { noteContext }); await widget.handleEvent("setNoteContext", { noteContext });
this.child(widget); this.child(widget);
} }
async openNewNoteSplitEvent({ ntxId, notePath, hoistedNoteId, viewScope }) { async openNewNoteSplitEvent({ ntxId, notePath, hoistedNoteId, viewScope }: {
ntxId: string;
notePath?: string;
hoistedNoteId?: string;
viewScope?: any;
}) {
const mainNtxId = appContext.tabManager.getActiveMainContext().ntxId; const mainNtxId = appContext.tabManager.getActiveMainContext().ntxId;
if (!mainNtxId) {
logError("empty mainNtxId!");
return;
}
if (!ntxId) { if (!ntxId) {
logError("empty ntxId!"); logError("empty ntxId!");
@ -53,7 +86,7 @@ export default class SplitNoteContainer extends FlexContainer {
// insert the note context after the originating note context // insert the note context after the originating note context
ntxIds.splice(ntxIds.indexOf(ntxId) + 1, 0, noteContext.ntxId); ntxIds.splice(ntxIds.indexOf(ntxId) + 1, 0, noteContext.ntxId);
this.triggerCommand("noteContextReorder", { ntxIdsInOrder: ntxIds }); this.triggerCommand("noteContextReorder" as keyof CommandMappings, { ntxIdsInOrder: ntxIds });
// move the note context rendered widget after the originating widget // move the note context rendered widget after the originating widget
this.$widget.find(`[data-ntx-id="${noteContext.ntxId}"]`).insertAfter(this.$widget.find(`[data-ntx-id="${ntxId}"]`)); this.$widget.find(`[data-ntx-id="${noteContext.ntxId}"]`).insertAfter(this.$widget.find(`[data-ntx-id="${ntxId}"]`));
@ -67,11 +100,11 @@ export default class SplitNoteContainer extends FlexContainer {
} }
} }
closeThisNoteSplitCommand({ ntxId }) { closeThisNoteSplitCommand({ ntxId }: { ntxId: string }): void {
appContext.tabManager.removeNoteContext(ntxId); appContext.tabManager.removeNoteContext(ntxId);
} }
async moveThisNoteSplitCommand({ ntxId, isMovingLeft }) { async moveThisNoteSplitCommand({ ntxId, isMovingLeft }: { ntxId: string; isMovingLeft: boolean }): Promise<void> {
if (!ntxId) { if (!ntxId) {
logError("empty ntxId!"); logError("empty ntxId!");
return; return;
@ -96,7 +129,7 @@ export default class SplitNoteContainer extends FlexContainer {
const newNtxIds = [...ntxIds.slice(0, leftIndex), ntxIds[leftIndex + 1], ntxIds[leftIndex], ...ntxIds.slice(leftIndex + 2)]; const newNtxIds = [...ntxIds.slice(0, leftIndex), ntxIds[leftIndex + 1], ntxIds[leftIndex], ...ntxIds.slice(leftIndex + 2)];
const isChangingMainContext = !contexts[leftIndex].mainNtxId; const isChangingMainContext = !contexts[leftIndex].mainNtxId;
this.triggerCommand("noteContextReorder", { this.triggerCommand("noteContextReorder" as keyof CommandMappings, {
ntxIdsInOrder: newNtxIds, ntxIdsInOrder: newNtxIds,
oldMainNtxId: isChangingMainContext ? ntxIds[leftIndex] : null, oldMainNtxId: isChangingMainContext ? ntxIds[leftIndex] : null,
newMainNtxId: isChangingMainContext ? ntxIds[leftIndex + 1] : null newMainNtxId: isChangingMainContext ? ntxIds[leftIndex + 1] : null
@ -109,16 +142,16 @@ export default class SplitNoteContainer extends FlexContainer {
await appContext.tabManager.activateNoteContext(isMovingLeft ? ntxIds[leftIndex + 1] : ntxIds[leftIndex]); await appContext.tabManager.activateNoteContext(isMovingLeft ? ntxIds[leftIndex + 1] : ntxIds[leftIndex]);
} }
activeContextChangedEvent() { activeContextChangedEvent(): void {
this.refresh(); this.refresh();
} }
noteSwitchedAndActivatedEvent() { noteSwitchedAndActivatedEvent(): void {
this.refresh(); this.refresh();
} }
noteContextRemovedEvent({ ntxIds }) { noteContextRemovedEvent({ ntxIds }: { ntxIds: string[] }): void {
this.children = this.children.filter((c) => !ntxIds.includes(c.ntxId)); this.children = this.children.filter((c) => c.ntxId && !ntxIds.includes(c.ntxId));
for (const ntxId of ntxIds) { for (const ntxId of ntxIds) {
this.$widget.find(`[data-ntx-id="${ntxId}"]`).remove(); this.$widget.find(`[data-ntx-id="${ntxId}"]`).remove();
@ -127,7 +160,7 @@ export default class SplitNoteContainer extends FlexContainer {
} }
} }
contextsReopenedEvent({ ntxId, afterNtxId }) { contextsReopenedEvent({ ntxId, afterNtxId }: { ntxId?: string; afterNtxId?: string }): void {
if (ntxId === undefined || afterNtxId === undefined) { if (ntxId === undefined || afterNtxId === undefined) {
// no single split reopened // no single split reopened
return; return;
@ -135,13 +168,11 @@ export default class SplitNoteContainer extends FlexContainer {
this.$widget.find(`[data-ntx-id="${ntxId}"]`).insertAfter(this.$widget.find(`[data-ntx-id="${afterNtxId}"]`)); this.$widget.find(`[data-ntx-id="${ntxId}"]`).insertAfter(this.$widget.find(`[data-ntx-id="${afterNtxId}"]`));
} }
async refresh() { async refresh(): Promise<void> {
this.toggleExt(true); this.toggleExt(true);
} }
toggleInt(show) {} // not needed toggleExt(show: boolean): void {
toggleExt(show) {
const activeMainContext = appContext.tabManager.getActiveMainContext(); const activeMainContext = appContext.tabManager.getActiveMainContext();
const activeNtxId = activeMainContext ? activeMainContext.ntxId : null; const activeNtxId = activeMainContext ? activeMainContext.ntxId : null;
@ -149,7 +180,7 @@ export default class SplitNoteContainer extends FlexContainer {
const noteContext = appContext.tabManager.getNoteContextById(ntxId); const noteContext = appContext.tabManager.getNoteContextById(ntxId);
const widget = this.widgets[ntxId]; const widget = this.widgets[ntxId];
widget.toggleExt(show && activeNtxId && [noteContext.ntxId, noteContext.mainNtxId].includes(activeNtxId)); widget.toggleExt(show && activeNtxId !== null && [noteContext.ntxId, noteContext.mainNtxId].includes(activeNtxId));
} }
} }
@ -158,41 +189,50 @@ export default class SplitNoteContainer extends FlexContainer {
* are not executed, we're waiting for the first tab activation, and then we update the tab. After this initial * are not executed, we're waiting for the first tab activation, and then we update the tab. After this initial
* activation, further note switches are always propagated to the tabs. * activation, further note switches are always propagated to the tabs.
*/ */
handleEventInChildren(name, data) { handleEventInChildren<T extends EventNames>(name: T, data: EventData<T>): Promise<any> | null {
if (["noteSwitched", "noteSwitchedAndActivated"].includes(name)) { if (["noteSwitched", "noteSwitchedAndActivated"].includes(name)) {
// this event is propagated only to the widgets of a particular tab // this event is propagated only to the widgets of a particular tab
const widget = this.widgets[data.noteContext.ntxId]; const noteContext = (data as NoteContextEvent).noteContext;
const widget = noteContext.ntxId ? this.widgets[noteContext.ntxId] : undefined;
if (!widget) { if (!widget) {
return Promise.resolve(); return Promise.resolve();
} }
if (widget.hasBeenAlreadyShown || name === "noteSwitchedAndActivated" || appContext.tabManager.getActiveMainContext() === data.noteContext.getMainContext()) { if (widget.hasBeenAlreadyShown || name === "noteSwitchedAndActivatedEvent" || appContext.tabManager.getActiveMainContext() === noteContext.getMainContext()) {
widget.hasBeenAlreadyShown = true; widget.hasBeenAlreadyShown = true;
return [widget.handleEvent("noteSwitched", data), this.refreshNotShown(data)]; return Promise.all([
widget.handleEvent("noteSwitched", { noteContext, notePath: noteContext.notePath }),
this.refreshNotShown({ noteContext })
]);
} else { } else {
return Promise.resolve(); return Promise.resolve();
} }
} }
if (name === "activeContextChanged") { if (name === "activeContextChanged") {
return this.refreshNotShown(data); return this.refreshNotShown(data as NoteContextEvent);
} else { } else {
return super.handleEventInChildren(name, data); return super.handleEventInChildren(name, data);
} }
} }
refreshNotShown(data) { private refreshNotShown(data: NoteContextEvent): Promise<any> {
const promises = []; const promises: Promise<any>[] = [];
for (const subContext of data.noteContext.getMainContext().getSubContexts()) { for (const subContext of data.noteContext.getMainContext().getSubContexts()) {
if (!subContext.ntxId) {
continue;
}
const widget = this.widgets[subContext.ntxId]; const widget = this.widgets[subContext.ntxId];
if (!widget.hasBeenAlreadyShown) { if (!widget.hasBeenAlreadyShown) {
widget.hasBeenAlreadyShown = true; widget.hasBeenAlreadyShown = true;
promises.push(widget.handleEvent("activeContextChanged", { noteContext: subContext })); const eventPromise = widget.handleEvent("activeContextChanged", { noteContext: subContext });
promises.push(eventPromise || Promise.resolve());
} }
} }