Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add buttons to reorder split panes #3984

Merged
merged 4 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/public/app/components/tab_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
3 changes: 3 additions & 0 deletions src/public/app/layouts/desktop_layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import CodeButtonsWidget from "../widgets/floating_buttons/code_buttons.js";
import ApiLogWidget from "../widgets/api_log.js";
import HideFloatingButtonsButton from "../widgets/floating_buttons/hide_floating_buttons_button.js";
import ScriptExecutorWidget from "../widgets/ribbon_widgets/script_executor.js";
import MovePaneButton from "../widgets/buttons/move_pane_button.js";

export default class DesktopLayout {
constructor(customWidgets) {
Expand Down Expand Up @@ -123,6 +124,8 @@ export default class DesktopLayout {
.child(new NoteIconWidget())
.child(new NoteTitleWidget())
.child(new SpacerWidget(0, 1))
.child(new MovePaneButton(true))
.child(new MovePaneButton(false))
.child(new ClosePaneButton())
.child(new CreatePaneButton())
)
Expand Down
4 changes: 4 additions & 0 deletions src/public/app/widgets/buttons/close_pane_button.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export default class ClosePaneButton extends OnClickButtonWidget {
&& this.noteContext && !!this.noteContext.mainNtxId;
}

async noteContextReorderEvent({ntxIdsInOrder}) {
this.refresh();
}

constructor() {
super();

Expand Down
55 changes: 55 additions & 0 deletions src/public/app/widgets/buttons/move_pane_button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import OnClickButtonWidget from "./onclick_button.js";
import appContext from "../../components/app_context.js";

export default class MovePaneButton extends OnClickButtonWidget {
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;
} else {
const currentIndex = appContext.tabManager.noteContexts.findIndex(c => c.ntxId === this.ntxId);
const nextContext = appContext.tabManager.noteContexts[currentIndex + 1];
// movable if the next context is not null and not a main context, i.e. non-null mainNtxId
return !!nextContext?.mainNtxId;
}
}

initialRenderCompleteEvent() {
this.refresh();
super.initialRenderCompleteEvent();
}

async noteContextRemovedEvent({ntxIds}) {
this.refresh();
}

async newNoteContextCreatedEvent({noteContext}) {
this.refresh();
}

async noteContextSwitchEvent() {
this.refresh();
}

async noteContextReorderEvent({ntxIdsInOrder}) {
this.refresh();
}

constructor(isMovingLeft) {
dymani marked this conversation as resolved.
Show resolved Hide resolved
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");
}
}
41 changes: 41 additions & 0 deletions src/public/app/widgets/containers/split_note_container.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,47 @@ export default class SplitNoteContainer extends FlexContainer {
appContext.tabManager.removeNoteContext(ntxId);
}

async moveThisNoteSplitCommand({ntxId, isMovingLeft}) {
if (!ntxId) {
logError("empty ntxId!");
return;
}

const contexts = appContext.tabManager.noteContexts;

const currentIndex = contexts.findIndex(c => c.ntxId === ntxId);
const leftIndex = isMovingLeft ? currentIndex - 1 : currentIndex;

if (currentIndex === -1 || leftIndex < 0 || leftIndex + 1 >= contexts.length) {
logError("invalid context!");
dymani marked this conversation as resolved.
Show resolved Hide resolved
return;
}

if (contexts[leftIndex].isEmpty() && contexts[leftIndex + 1].isEmpty())
// no op
return;
dymani marked this conversation as resolved.
Show resolved Hide resolved

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)
dymani marked this conversation as resolved.
Show resolved Hide resolved
});

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(isMovingLeft ? ntxIds[leftIndex + 1] : ntxIds[leftIndex]);
}

activeContextChangedEvent() {
this.refresh();
}
Expand Down
16 changes: 16 additions & 0 deletions src/public/app/widgets/tab_row.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down