diff --git a/src/public/app/components/tab_manager.js b/src/public/app/components/tab_manager.js index e5ca32c45..ace30105f 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 690dcac6f..220fd2cca 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 3a17b7d20..988c537b1 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); + + 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]}"]`)); // activate context that now contains the original note - await appContext.tabManager.activateNoteContext(otherId); - - this.triggerEvent('noteContextSwitch'); + 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 55a5da24c..6e15f3458 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);