Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(splits): ability to swap split positions (#5600)

Seamusfc9c2c758749
diff --git a/apps/web/src/components/app/split-layout/components/SplitHeader.tsx b/apps/web/src/components/app/split-layout/components/SplitHeader.tsx
index e229e2ca55..29b1ece6e6 100644
--- a/apps/web/src/components/app/split-layout/components/SplitHeader.tsx
+++ b/apps/web/src/components/app/split-layout/components/SplitHeader.tsx
@@ -18,6 +18,8 @@ import { AnimatedSquareSidebarIcon } from '@icon/square-sidebar';
import SplitIcon from '@icon/wide-newSplit.svg';
import { ContextMenu } from '@kobalte/core/context-menu';
import ArrowClockwise from '@phosphor/arrow-clockwise.svg';
+import ArrowLeft from '@phosphor/arrow-left.svg';
+import ArrowRight from '@phosphor/arrow-right.svg';
import CollapseIcon from '@phosphor/arrows-in.svg';
import ExpandIcon from '@phosphor/arrows-out.svg';
import CaretDown from '@phosphor/caret-down.svg';
@@ -308,6 +310,8 @@ function SplitHeaderContextMenu(props: ParentProps) {
() => panel.handle.content().type === 'component'
);
const canToggleSpotlight = createMemo(() => canSpotlight(layout.manager));
+ const canSwapWith = (direction: 'left' | 'right') =>
+ layout.manager.canSwapSplit(panel.handle.id, direction);
const newSplitContent = () => ({
type: 'component' as const,
@@ -387,6 +391,19 @@ function SplitHeaderContextMenu(props: ParentProps) {
onClick={() => insertSplitBeside('right', duplicateContent())}
/>
<MenuSeparator />
+ <MenuItem
+ icon={ArrowLeft}
+ text="Swap split left"
+ disabled={!canSwapWith('left')}
+ onClick={() => layout.manager.swapSplit(panel.handle.id, 'left')}
+ />
+ <MenuItem
+ icon={ArrowRight}
+ text="Swap split right"
+ disabled={!canSwapWith('right')}
+ onClick={() => layout.manager.swapSplit(panel.handle.id, 'right')}
+ />
+ <MenuSeparator />
<MenuItem
icon={panel.handle.isSpotLight() ? CollapseIcon : ExpandIcon}
hotkeyToken={TOKENS.window.spotlight.toggle}
diff --git a/apps/web/src/components/app/split-layout/layoutManager.ts b/apps/web/src/components/app/split-layout/layoutManager.ts
index a3ddc7ce5b..f5d46400f8 100644
--- a/apps/web/src/components/app/split-layout/layoutManager.ts
+++ b/apps/web/src/components/app/split-layout/layoutManager.ts
@@ -319,6 +319,12 @@ export type SplitManager = {
/** Remove a split by its split id */
removeSplit: (id: SplitId) => void;
+ /** Swap a split with its immediate neighbor in the requested direction. */
+ swapSplit: (id: SplitId, direction: 'left' | 'right') => void;
+
+ /** Whether a split group has a neighbor in the requested direction. */
+ canSwapSplit: (id: SplitId, direction: 'left' | 'right') => boolean;
+
/** Create a new split with the provided initial content and activate it */
createNewSplit: (options: CreateNewSplitOptions) => SplitHandle;
@@ -1270,6 +1276,68 @@ export function createSplitLayout(
});
}
+ function splitGroupBounds(
+ id: SplitId
+ ): readonly [number, number] | undefined {
+ const index = splitIndexById(id);
+ if (index < 0) return undefined;
+
+ const viewerId = state.previewPairs[id]?.viewerId;
+ if (viewerId && state.splits[index + 1]?.id === viewerId) {
+ return [index, index + 1];
+ }
+
+ const controllerIndex = state.splits.findIndex(
+ (split) => state.previewPairs[split.id]?.viewerId === id
+ );
+ if (controllerIndex >= 0 && controllerIndex + 1 === index) {
+ return [controllerIndex, index];
+ }
+
+ return [index, index];
+ }
+
+ function canSwapSplit(id: SplitId, direction: 'left' | 'right') {
+ const bounds = splitGroupBounds(id);
+ if (!bounds) return false;
+
+ const targetIndex = direction === 'left' ? bounds[0] - 1 : bounds[1] + 1;
+ return targetIndex >= 0 && targetIndex < state.splits.length;
+ }
+
+ function swapSplit(id: SplitId, direction: 'left' | 'right') {
+ const bounds = splitGroupBounds(id);
+ if (!bounds) return;
+
+ const targetIndex = direction === 'left' ? bounds[0] - 1 : bounds[1] + 1;
+ if (targetIndex < 0 || targetIndex >= state.splits.length) return;
+
+ const target = state.splits[targetIndex];
+ if (!target) return;
+ const targetBounds = splitGroupBounds(target.id);
+ if (!targetBounds) return;
+
+ const [sourceStart, sourceEnd] = bounds;
+ const [targetStart, targetEnd] = targetBounds;
+ const [leftStart, leftEnd, rightStart, rightEnd] =
+ sourceStart < targetStart
+ ? [sourceStart, sourceEnd, targetStart, targetEnd]
+ : [targetStart, targetEnd, sourceStart, sourceEnd];
+
+ batch(() => {
+ resizeContext()?.swap(id, target.id);
+ setState('splits', (splits) => {
+ return [
+ ...splits.slice(0, leftStart),
+ ...splits.slice(rightStart, rightEnd + 1),
+ ...splits.slice(leftEnd + 1, rightStart),
+ ...splits.slice(leftStart, leftEnd + 1),
+ ...splits.slice(rightEnd + 1),
+ ];
+ });
+ });
+ }
+
/**
* An unclaimed placeholder split sitting immediately right of the
* controller that engage adopts as the viewer instead of creating a
@@ -2019,6 +2087,8 @@ export function createSplitLayout(
getSplit,
openWithSplit,
removeSplit,
+ swapSplit,
+ canSwapSplit,
createNewSplit,
getUrlSegments,
getUrl,
diff --git a/apps/web/src/components/app/split-layout/tests/layoutManager.test.ts b/apps/web/src/components/app/split-layout/tests/layoutManager.test.ts
index 8d1d83e950..0cb52cb8bf 100644
--- a/apps/web/src/components/app/split-layout/tests/layoutManager.test.ts
+++ b/apps/web/src/components/app/split-layout/tests/layoutManager.test.ts
@@ -51,6 +51,58 @@ function createMockOrchestrator(): BlockOrchestrator {
}
describe('layoutManager', () => {
+ describe('swapSplit', () => {
+ it('swaps adjacent splits and delegates the panel reorder to Resize', () => {
+ createRoot((dispose) => {
+ const manager = createSplitLayout(createMockOrchestrator(), [
+ { type: 'component', id: 'inbox' },
+ { type: 'component', id: 'calendar' },
+ ]);
+ const [first, second] = manager.splits();
+ const swap = vi.fn();
+ manager.setResizeContext({
+ canFit: () => true,
+ swap,
+ } as unknown as ResizeZoneCtx);
+
+ manager.swapSplit(second!.id, 'left');
+
+ expect(manager.splits().map((split) => split.id)).toEqual([
+ second!.id,
+ first!.id,
+ ]);
+ expect(swap).toHaveBeenCalledWith(second!.id, first!.id);
+
+ dispose();
+ });
+ });
+
+ it('swaps a preview pair as a unit', () => {
+ createRoot((dispose) => {
+ const manager = createSplitLayout(createMockOrchestrator(), [
+ { type: 'component', id: 'inbox' },
+ { type: 'component', id: 'calendar' },
+ ]);
+ const controllerId = manager.splits()[0]!.id;
+ manager.engagePreviewMode(controllerId);
+ const viewerId = manager.viewerOf(controllerId)!;
+ const calendarId = manager.splits()[2]!.id;
+
+ manager.swapSplit(viewerId, 'right');
+
+ expect(manager.splits().map((split) => split.id)).toEqual([
+ calendarId,
+ controllerId,
+ viewerId,
+ ]);
+ expect(manager.canSwapSplit(viewerId, 'right')).toBe(false);
+ expect(manager.canSwapSplit(viewerId, 'left')).toBe(true);
+
+ dispose();
+ });
+ });
+ });
+
describe('reconciler', () => {
it('should reconcile between current state and url changes', () => {
createRoot((dispose) => {
diff --git a/apps/web/src/lib/core/component/Resize/Resize.tsx b/apps/web/src/lib/core/component/Resize/Resize.tsx
index 664c55e5ae..3989fb91d6 100644
--- a/apps/web/src/lib/core/component/Resize/Resize.tsx
+++ b/apps/web/src/lib/core/component/Resize/Resize.tsx
@@ -144,6 +144,7 @@ function Zone(props: ParentProps<ZoneProps>) {
offsetOf,
sizeOf,
canFit: solver.canFitPanel,
+ swap: solver.swap,
hide: solver.hide,
show: solver.show,
isHidden: solver.isHidden,
diff --git a/apps/web/src/lib/core/component/Resize/solver.test.ts b/apps/web/src/lib/core/component/Resize/solver.test.ts
index 4687d3da51..436802f8f1 100644
--- a/apps/web/src/lib/core/component/Resize/solver.test.ts
+++ b/apps/web/src/lib/core/component/Resize/solver.test.ts
@@ -3,6 +3,59 @@ import { describe, expect, it } from 'vitest';
import { createResizeSolver } from './solver';
describe('createResizeSolver', () => {
+ describe('swap', () => {
+ it('reorders registered panels without changing their sizing intent', async () => {
+ const { solver, dispose } = createRoot((dispose) => ({
+ dispose,
+ solver: createResizeSolver({
+ direction: 'horizontal',
+ gutter: () => 0,
+ size: () => 1000,
+ panels: [],
+ }),
+ }));
+
+ solver.addPanel({ id: 'A', minSize: 0 });
+ solver.addPanel({ id: 'B', minSize: 0 });
+ await Promise.resolve();
+ solver.moveHandle(0, 200);
+ expect(solver.solve().sizes.get('A')).toBe(700);
+ expect(solver.solve().sizes.get('B')).toBe(300);
+
+ solver.swap('A', 'B');
+
+ expect(solver.order()).toEqual(['B', 'A']);
+ expect(solver.solve().sizes.get('A')).toBe(700);
+ expect(solver.solve().sizes.get('B')).toBe(300);
+
+ dispose();
+ });
+
+ it('moves all contiguous members of a share group together', async () => {
+ const { solver, dispose } = createRoot((dispose) => ({
+ dispose,
+ solver: createResizeSolver({
+ direction: 'horizontal',
+ gutter: () => 0,
+ size: () => 1000,
+ panels: [],
+ }),
+ }));
+
+ solver.addPanel({ id: 'A', minSize: 0 });
+ solver.addPanel({ id: 'B', minSize: 0, shareGroup: 'pair' });
+ solver.addPanel({ id: 'C', minSize: 0, shareGroup: 'pair' });
+ solver.addPanel({ id: 'D', minSize: 0 });
+ await Promise.resolve();
+
+ solver.swap('B', 'D');
+
+ expect(solver.order()).toEqual(['A', 'D', 'B', 'C']);
+
+ dispose();
+ });
+ });
+
describe('addPanel', () => {
it('should insert a panel at index 0 when index=0 is passed', () => {
createRoot((dispose) => {
diff --git a/apps/web/src/lib/core/component/Resize/solver.ts b/apps/web/src/lib/core/component/Resize/solver.ts
index 4bab72ef34..0382404f88 100644
--- a/apps/web/src/lib/core/component/Resize/solver.ts
+++ b/apps/web/src/lib/core/component/Resize/solver.ts
@@ -27,6 +27,7 @@ type ResizeSolver = {
order: () => PanelId[];
hasPanel: (id: PanelId) => boolean;
canFitPanel: (panel: PanelConfig) => boolean;
+ swap: (firstId: PanelId, secondId: PanelId) => void;
hide: (id: PanelId) => void;
show: (id: PanelId) => void;
isHidden: (id: PanelId) => boolean;
@@ -850,6 +851,50 @@ export function createResizeSolver(params: {
setDirty('manual');
}
+ function swap(firstId: PanelId, secondId: PanelId) {
+ if (firstId === secondId) return;
+
+ const ids = untrack(order);
+ const firstIndex = ids.indexOf(firstId);
+ const secondIndex = ids.indexOf(secondId);
+ if (firstIndex < 0 || secondIndex < 0) return;
+
+ const groupBounds = (index: number) => {
+ const group = panelData[ids[index]]?.shareGroup;
+ if (group === undefined) return [index, index] as const;
+
+ let start = index;
+ let end = index;
+ while (start > 0 && panelData[ids[start - 1]]?.shareGroup === group) {
+ start -= 1;
+ }
+ while (
+ end < ids.length - 1 &&
+ panelData[ids[end + 1]]?.shareGroup === group
+ ) {
+ end += 1;
+ }
+ return [start, end] as const;
+ };
+
+ const [firstStart, firstEnd] = groupBounds(firstIndex);
+ const [secondStart, secondEnd] = groupBounds(secondIndex);
+ if (firstStart === secondStart) return;
+
+ const [leftStart, leftEnd, rightStart, rightEnd] =
+ firstStart < secondStart
+ ? [firstStart, firstEnd, secondStart, secondEnd]
+ : [secondStart, secondEnd, firstStart, firstEnd];
+ const nextIds = [
+ ...ids.slice(0, leftStart),
+ ...ids.slice(rightStart, rightEnd + 1),
+ ...ids.slice(leftEnd + 1, rightStart),
+ ...ids.slice(leftStart, leftEnd + 1),
+ ...ids.slice(rightEnd + 1),
+ ];
+ setOrder(nextIds);
+ }
+
return {
direction: params.direction,
addPanel,
@@ -880,6 +925,7 @@ export function createResizeSolver(params: {
},
order,
moveHandle,
+ swap,
hasPanel: (id: PanelId) => {
return order().includes(id) && id in panelData;
},
diff --git a/apps/web/src/lib/core/component/Resize/types.ts b/apps/web/src/lib/core/component/Resize/types.ts
index 1d18fac5a8..82f897d54e 100644
--- a/apps/web/src/lib/core/component/Resize/types.ts
+++ b/apps/web/src/lib/core/component/Resize/types.ts
@@ -57,6 +57,8 @@ export type ResizeZoneCtx = {
sizeOf: (id: PanelId) => () => number;
offsetOf: (id: PanelId) => () => number;
canFit: (panel: Partial<PanelConfig>) => boolean;
+ /** Swap two registered panels without changing their sizing intent. */
+ swap: (firstId: PanelId, secondId: PanelId) => void;
hide: (id: PanelId) => void;
show: (id: PanelId) => void;
isHidden: (id: PanelId) => boolean;