Skip to content

Build your personal knowledge base with Trilium Notes

Use noteContextReorder event instead

dymani735852b3c1dc
diff --git a/src/public/app/components/tab_manager.js b/src/public/app/components/tab_manager.js
index e5ca32c452..ace30105f3 100644
--- a/src/public/app/components/tab_manager.js
+++ b/src/public/app/components/tab_manager.js
@@ -451,16 +451,15 @@ export default class TabManager extends Component {
this.tabsUpdate.scheduleUpdate();
}
- noteContextReorderEvent({ntxIdsInOrder}) {
- const order = {};
- let i = 0;
-
- for (const ntxId of ntxIdsInOrder) {
- order[ntxId] = i++;
- }
+ noteContextReorderEvent({ntxIdsInOrder, mainNtxIdsInOrder}) {
+ 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]);
+ }
+
this.tabsUpdate.scheduleUpdate();
}
diff --git a/src/public/app/widgets/buttons/close_pane_button.js b/src/public/app/widgets/buttons/close_pane_button.js
index 690dcac6f9..220fd2cca2 100644
--- a/src/public/app/widgets/buttons/close_pane_button.js
+++ b/src/public/app/widgets/buttons/close_pane_button.js
@@ -7,6 +7,10 @@ export default class ClosePaneButton extends OnClickButtonWidget {
&& this.noteContext && !!this.noteContext.mainNtxId;
}
+ async noteContextReorderEvent({ntxIdsInOrder}) {
+ this.refresh();
+ }
+
constructor() {
super();
diff --git a/src/public/app/widgets/containers/split_note_container.js b/src/public/app/widgets/containers/split_note_container.js
index 3a17b7d20f..988c537b19 100644
--- a/src/public/app/widgets/containers/split_note_container.js
+++ b/src/public/app/widgets/containers/split_note_container.js
@@ -83,32 +83,36 @@ export default class SplitNoteContainer extends FlexContainer {
const contexts = appContext.tabManager.noteContexts;
const currentIndex = contexts.findIndex(c => c.ntxId === ntxId);
- const otherIndex = currentIndex + (isMovingLeft ? -1 : 1);
+ const leftIndex = isMovingLeft ? currentIndex - 1 : currentIndex;
- if (currentIndex === -1 || otherIndex < 0 || otherIndex >= contexts.length) {
+ if (currentIndex === -1 || leftIndex < 0 || leftIndex + 1 >= contexts.length) {
logError("invalid context!");
return;
}
- if (contexts[currentIndex].isEmpty() && contexts[otherIndex].isEmpty())
+ if (contexts[leftIndex].isEmpty() && contexts[leftIndex + 1].isEmpty())
// no op
return;
- const currentId = contexts[currentIndex].ntxId;
- const currentPath = contexts[currentIndex].notePath;
-
- const otherId = contexts[otherIndex].ntxId;
- const otherPath = contexts[otherIndex].notePath;
-
- if (!!currentPath)
- await appContext.tabManager.switchToNoteContext(otherId, currentPath);
- if (!!otherPath)
- await appContext.tabManager.switchToNoteContext(currentId, otherPath);
+ const ntxIds = contexts.map(c => c.ntxId);
+ const mainNtxIds = contexts.map(c => c.mainNtxId);
- // activate context that now contains the original note
- await appContext.tabManager.activateNoteContext(otherId);
+ 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)
+ });
+
+ this.$widget.find(`[data-ntx-id="${ntxIds[leftIndex]}"]`)
+ .insertAfter(this.$widget.find(`[data-ntx-id="${ntxIds[leftIndex + 1]}"]`));
- this.triggerEvent('noteContextSwitch');
+ // activate context that now contains the original note
+ await appContext.tabManager.activateNoteContext(isMovingLeft ? ntxIds[leftIndex + 1] : ntxIds[leftIndex]);
}
activeContextChangedEvent() {
diff --git a/src/public/app/widgets/tab_row.js b/src/public/app/widgets/tab_row.js
index 55a5da24ca..6e15f3458f 100644
--- a/src/public/app/widgets/tab_row.js
+++ b/src/public/app/widgets/tab_row.js
@@ -609,6 +609,22 @@ 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
+ )
+ 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);
+ }
+ });
+ }
+
updateTabById(ntxId) {
const $tab = this.getTabById(ntxId);