Add buttons to move split panes

This commit is contained in:
dymani 2023-05-30 02:24:56 +08:00
parent bf6106f4dc
commit e1b67e20ec
3 changed files with 96 additions and 0 deletions

View file

@ -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) {
@ -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())
)

View file

@ -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) {
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");
}
}

View file

@ -1,5 +1,6 @@
import FlexContainer from "./flex_container.js";
import appContext from "../../components/app_context.js";
import NoteContext from "../../components/note_context.js";
export default class SplitNoteContainer extends FlexContainer {
constructor(widgetFactory) {
@ -74,6 +75,43 @@ 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 otherIndex = currentIndex + (isMovingLeft ? -1 : 1);
if (currentIndex === -1 || otherIndex < 0 || otherIndex >= contexts.length) {
logError("invalid context!");
return;
}
if (contexts[currentIndex].isEmpty() && contexts[otherIndex].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);
// activate context that now contains the original note
await appContext.tabManager.activateNoteContext(otherId);
this.triggerEvent('noteContextSwitch');
}
activeContextChangedEvent() {
this.refresh();
}