Skip to content

Build your personal knowledge base with Trilium Notes

Simplify noteContextReorder event call

dymani58253567cd55
diff --git a/src/public/app/components/tab_manager.js b/src/public/app/components/tab_manager.js
index ace30105f3..53c3ccbbd2 100644
--- a/src/public/app/components/tab_manager.js
+++ b/src/public/app/components/tab_manager.js
@@ -451,13 +451,21 @@ export default class TabManager extends Component {
this.tabsUpdate.scheduleUpdate();
}
- noteContextReorderEvent({ntxIdsInOrder, mainNtxIdsInOrder}) {
+ noteContextReorderEvent({ntxIdsInOrder, oldMainNtxId, newMainNtxId}) {
const order = Object.fromEntries(ntxIdsInOrder.map((v, i) => [v, i]));
this.children.sort((a, b) => order[a.ntxId] < order[b.ntxId] ? -1 : 1);
- if (!!mainNtxIdsInOrder && mainNtxIdsInOrder.length === this.children.length) {
- this.children.forEach((c, i) => c.mainNtxId = mainNtxIdsInOrder[i]);
+ if (oldMainNtxId && newMainNtxId) {
+ this.children.forEach(c => {
+ if (c.ntxId === newMainNtxId) {
+ // new main context has null mainNtxId
+ c.mainNtxId = null;
+ } else if (c.ntxId === oldMainNtxId || c.mainNtxId === oldMainNtxId) {
+ // old main context or subcontexts all have the new mainNtxId
+ c.mainNtxId = newMainNtxId;
+ }
+ });
}
this.tabsUpdate.scheduleUpdate();
diff --git a/src/public/app/widgets/buttons/move_pane_button.js b/src/public/app/widgets/buttons/move_pane_button.js
index 9ea4dbe61b..632651ca5f 100644
--- a/src/public/app/widgets/buttons/move_pane_button.js
+++ b/src/public/app/widgets/buttons/move_pane_button.js
@@ -2,10 +2,26 @@ import OnClickButtonWidget from "./onclick_button.js";
import appContext from "../../components/app_context.js";
export default class MovePaneButton extends OnClickButtonWidget {
+ constructor(isMovingLeft) {
+ super();
+
+ this.isMovingLeft = isMovingLeft;
+
+ this.icon(isMovingLeft ? "bx-chevron-left" : "bx-chevron-right")
+ .title(isMovingLeft ? "Move left" : "Move right")
+ .titlePlacement("bottom")
+ .onClick(async (widget, e) => {
+ e.stopPropagation();
+ widget.triggerCommand("moveThisNoteSplit", {ntxId: widget.getClosestNtxId(), isMovingLeft: this.isMovingLeft});
+ })
+ .class("icon-action");
+ }
+
isEnabled() {
- if (!super.isEnabled())
+ if (!super.isEnabled()) {
return false;
-
+ }
+
if (this.isMovingLeft) {
// movable if the current context is not a main context, i.e. non-null mainNtxId
return !!this.noteContext?.mainNtxId;
@@ -17,39 +33,15 @@ export default class MovePaneButton extends OnClickButtonWidget {
}
}
- initialRenderCompleteEvent() {
- this.refresh();
- super.initialRenderCompleteEvent();
- }
-
- async noteContextRemovedEvent({ntxIds}) {
+ async noteContextRemovedEvent() {
this.refresh();
}
- async newNoteContextCreatedEvent({noteContext}) {
+ async newNoteContextCreatedEvent() {
this.refresh();
}
- async noteContextSwitchEvent() {
+ async noteContextReorderEvent() {
this.refresh();
}
-
- async noteContextReorderEvent({ntxIdsInOrder}) {
- this.refresh();
- }
-
- constructor(isMovingLeft) {
- super();
-
- this.isMovingLeft = isMovingLeft;
-
- this.icon(isMovingLeft ? "bx-chevron-left" : "bx-chevron-right")
- .title(isMovingLeft ? "Move left" : "Move right")
- .titlePlacement("bottom")
- .onClick(async (widget, e) => {
- e.stopPropagation();
- widget.triggerCommand("moveThisNoteSplit", {ntxId: widget.getClosestNtxId(), isMovingLeft: this.isMovingLeft});
- })
- .class("icon-action");
- }
}
diff --git a/src/public/app/widgets/containers/split_note_container.js b/src/public/app/widgets/containers/split_note_container.js
index 988c537b19..0188add6d7 100644
--- a/src/public/app/widgets/containers/split_note_container.js
+++ b/src/public/app/widgets/containers/split_note_container.js
@@ -86,28 +86,31 @@ export default class SplitNoteContainer extends FlexContainer {
const leftIndex = isMovingLeft ? currentIndex - 1 : currentIndex;
if (currentIndex === -1 || leftIndex < 0 || leftIndex + 1 >= contexts.length) {
- logError("invalid context!");
+ logError(`invalid context! currentIndex: ${currentIndex}, leftIndex: ${leftIndex}, contexts.length: ${contexts.length}`);
return;
}
- if (contexts[leftIndex].isEmpty() && contexts[leftIndex + 1].isEmpty())
+ if (contexts[leftIndex].isEmpty() && contexts[leftIndex + 1].isEmpty()) {
// no op
return;
+ }
const ntxIds = contexts.map(c => c.ntxId);
- const mainNtxIds = contexts.map(c => c.mainNtxId);
+ const newNtxIds = [
+ ...ntxIds.slice(0, leftIndex),
+ ntxIds[leftIndex + 1],
+ ntxIds[leftIndex],
+ ...ntxIds.slice(leftIndex + 2),
+ ];
+ const isChangingMainContext = !contexts[leftIndex].mainNtxId;
this.triggerCommand("noteContextReorder", {
- ntxIdsInOrder: [
- ...ntxIds.slice(0, leftIndex),
- ntxIds[leftIndex + 1],
- ntxIds[leftIndex],
- ...ntxIds.slice(leftIndex + 2),
- ],
- oldNtxIdsInOrder: ntxIds,
- mainNtxIdsInOrder: mainNtxIds.map(id => id === ntxIds[leftIndex] ? ntxIds[leftIndex + 1] : id)
+ ntxIdsInOrder: newNtxIds,
+ oldMainNtxId: isChangingMainContext ? ntxIds[leftIndex] : null,
+ newMainNtxId: isChangingMainContext ? ntxIds[leftIndex + 1]: null,
});
+ // reorder the note context widgets
this.$widget.find(`[data-ntx-id="${ntxIds[leftIndex]}"]`)
.insertAfter(this.$widget.find(`[data-ntx-id="${ntxIds[leftIndex + 1]}"]`));
diff --git a/src/public/app/widgets/tab_row.js b/src/public/app/widgets/tab_row.js
index 6e15f3458f..9081ed9e1d 100644
--- a/src/public/app/widgets/tab_row.js
+++ b/src/public/app/widgets/tab_row.js
@@ -609,20 +609,15 @@ export default class TabRowWidget extends BasicWidget {
this.updateTabById(noteContext.mainNtxId || noteContext.ntxId);
}
- noteContextReorderEvent({ntxIdsInOrder, oldNtxIdsInOrder, mainNtxIdsInOrder}) {
- if (!oldNtxIdsInOrder || !mainNtxIdsInOrder
- || ntxIdsInOrder.length !== oldNtxIdsInOrder.length
- || ntxIdsInOrder.length !== mainNtxIdsInOrder.length
- )
+ noteContextReorderEvent({oldMainNtxId, newMainNtxId}) {
+ if (!oldMainNtxId || !newMainNtxId) {
+ // no need to update tab row
return;
+ }
- ntxIdsInOrder.forEach((id, i) => {
- // update main context that is not a tab
- if (!mainNtxIdsInOrder[i] && this.getTabById(id).length === 0) {
- this.getTabById(oldNtxIdsInOrder[i]).attr("data-ntx-id", id);
- this.updateTabById(id);
- }
- });
+ // update tab id for the new main context
+ this.getTabById(oldMainNtxId).attr("data-ntx-id", newMainNtxId);
+ this.updateTabById(newMainNtxId);
}
updateTabById(ntxId) {