Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(outliner): manual folder reordering — drag folders to reposition siblings

esengine932ee787e9b3
diff --git a/desktop/src/engine/EditorControlSurface.ts b/desktop/src/engine/EditorControlSurface.ts
index 1844ddc1d..aa5639b5f 100644
--- a/desktop/src/engine/EditorControlSurface.ts
+++ b/desktop/src/engine/EditorControlSurface.ts
@@ -172,6 +172,10 @@ export class EditorControlSurfaceImpl {
deleteFolder(path: string): void {
this.s.commands.deleteFolder(path);
}
+ /** Reorder a folder before/after a sibling (manual sort). */
+ reorderFolder(srcPath: string, targetPath: string, before: boolean): void {
+ this.s.commands.reorderFolder(srcPath, targetPath, before);
+ }
/** Move entities into a folder (`null` = scene root); un-parents them. */
moveToFolder(ids: readonly EntityId[], path: string | null): void {
this.s.commands.moveToFolder(ids, path);
diff --git a/desktop/src/engine/SceneCommands.ts b/desktop/src/engine/SceneCommands.ts
index 236fd8894..5b833e46a 100644
--- a/desktop/src/engine/SceneCommands.ts
+++ b/desktop/src/engine/SceneCommands.ts
@@ -554,6 +554,32 @@ export class SceneCommandsImpl {
this.history.record(records.length > 1 ? `Move ${records.length} to Folder` : 'Move to Folder', apply, revert);
}
+ /**
+ * Reorder a folder among its siblings (drag-between, manual sort). Promotes both
+ * folders to explicit (so the order persists) and places `srcPath` immediately
+ * before/after `targetPath` in the scene folder list. One undo step; only valid
+ * for same-parent siblings (a cross-parent move is a nest via {@link renameFolder}).
+ */
+ reorderFolder(srcPath: string, targetPath: string, before: boolean): void {
+ const src = normalizeFolder(srcPath);
+ const tgt = normalizeFolder(targetPath);
+ if (!src || !tgt || src === tgt) return;
+ if (folderParent(src) !== folderParent(tgt)) return; // not siblings → not a reorder
+
+ const beforeList = this.model.sceneFolders();
+ const list = [...beforeList];
+ if (!list.includes(src)) list.push(src);
+ if (!list.includes(tgt)) list.push(tgt);
+ const without = list.filter((f) => f !== src);
+ const ti = without.indexOf(tgt);
+ without.splice(before ? ti : ti + 1, 0, src);
+
+ const same = beforeList.length === without.length && beforeList.every((f, i) => f === without[i]);
+ if (same) return;
+ this.model.setSceneFolders(without);
+ this.history.record('Reorder Folder', () => this.model.setSceneFolders(without), () => this.model.setSceneFolders(beforeList));
+ }
+
/**
* Drag-reorder: move an entity to sit immediately before/after a sibling target.
* Aligns the dragged entity's parent + folder to the target's first (so it
diff --git a/desktop/src/outliner/OutlinerModel.ts b/desktop/src/outliner/OutlinerModel.ts
index c28e375ed..182f1218c 100644
--- a/desktop/src/outliner/OutlinerModel.ts
+++ b/desktop/src/outliner/OutlinerModel.ts
@@ -152,8 +152,19 @@ export function buildOutlinerItems(data: SceneData | null, opts: BuildOutlinerOp
}
if (!active) for (const f of opts.folders ?? []) for (const pre of folderPrefixes(normalizeFolder(f))) allFolders.add(pre);
- const childFolders = (path: string): string[] =>
- [...allFolders].filter((p) => folderParent(p) === path).sort((a, b) => folderName(a).localeCompare(folderName(b)));
+ // Manual sort orders sibling folders by their position in the scene's explicit
+ // folder list (drag-reorderable, like entities); derived/list-absent folders
+ // fall to the end alphabetically. name/type sort is plain alphabetical.
+ const folderRank = sort === 'manual' ? new Map((opts.folders ?? []).map((f, i) => [normalizeFolder(f), i])) : null;
+ const childFolders = (path: string): string[] => {
+ const kids = [...allFolders].filter((p) => folderParent(p) === path);
+ if (folderRank) {
+ return kids.sort(
+ (a, b) => (folderRank.get(a) ?? Infinity) - (folderRank.get(b) ?? Infinity) || folderName(a).localeCompare(folderName(b)),
+ );
+ }
+ return kids.sort((a, b) => folderName(a).localeCompare(folderName(b)));
+ };
const countUnder = (path: string): number => {
let n = 0;
for (const [fp, arr] of rootsByFolder) if (isFolderUnder(fp, path)) n += arr.length;
diff --git a/desktop/src/panels/Outliner.tsx b/desktop/src/panels/Outliner.tsx
index 260961e4f..75f5f8ca9 100644
--- a/desktop/src/panels/Outliner.tsx
+++ b/desktop/src/panels/Outliner.tsx
@@ -317,6 +317,16 @@ export function Outliner() {
const rect = e.currentTarget.getBoundingClientRect();
const rel = (e.clientY - rect.top) / rect.height;
pos = rel < 0.25 ? 'before' : rel > 0.75 ? 'after' : 'on';
+ } else if (
+ dragFolder.current &&
+ item.kind === 'folder' &&
+ sortMode === 'manual' &&
+ folderParent(dragFolder.current) === folderParent(item.path)
+ ) {
+ // Sibling folders → top/bottom third reorders, middle nests.
+ const rect = e.currentTarget.getBoundingClientRect();
+ const rel = (e.clientY - rect.top) / rect.height;
+ pos = rel < 0.33 ? 'before' : rel > 0.66 ? 'after' : 'on';
}
if (drop?.key !== item.key || drop?.pos !== pos) setDrop({ key: item.key, pos });
};
@@ -351,9 +361,15 @@ export function Outliner() {
e.stopPropagation();
const pos = drop?.pos ?? 'on';
setDrop(null);
- // A folder being dragged → nest it under the target's folder.
+ // A folder being dragged → reorder among siblings (between-rows) or nest (on).
if (dragFolder.current != null) {
- moveFolderInto(item.kind === 'folder' ? item.path : SceneModel.folderOf(item.id));
+ const src = dragFolder.current;
+ if (item.kind === 'folder' && pos !== 'on' && folderParent(src) === folderParent(item.path)) {
+ dragFolder.current = null;
+ SceneCommands.reorderFolder(src, item.path, pos === 'before');
+ } else {
+ moveFolderInto(item.kind === 'folder' ? item.path : SceneModel.folderOf(item.id));
+ }
return;
}
if (item.kind === 'folder') {
diff --git a/desktop/tests/outliner-folders.test.ts b/desktop/tests/outliner-folders.test.ts
index e23a5ef8e..5a6ed5d39 100644
--- a/desktop/tests/outliner-folders.test.ts
+++ b/desktop/tests/outliner-folders.test.ts
@@ -111,6 +111,23 @@ describe('SceneCommands folders (undoable)', () => {
expect(S.model.sceneFolders()).toContain('A/B/C');
});
+ it('reorderFolder moves a sibling folder in the manual order; undo restores', () => {
+ S.commands.createFolder('Zebra');
+ S.commands.createFolder('Apple');
+ expect(S.model.sceneFolders()).toEqual(['Zebra', 'Apple']);
+ S.commands.reorderFolder('Apple', 'Zebra', true); // Apple before Zebra
+ expect(S.model.sceneFolders()).toEqual(['Apple', 'Zebra']);
+ S.history.undo();
+ expect(S.model.sceneFolders()).toEqual(['Zebra', 'Apple']);
+ });
+
+ it('reorderFolder only touches same-parent siblings (cross-parent is a no-op)', () => {
+ S.commands.createFolder('A');
+ S.commands.createFolder('B/C');
+ S.commands.reorderFolder('B/C', 'A', true); // different parents → rejected
+ expect(S.model.sceneFolders()).toEqual(['A', 'B/C']);
+ });
+
it('folders round-trip through serialize (lossless, editor-only)', () => {
S.commands.moveToFolder([1], 'Enemies');
S.commands.createFolder('Empty/Leaf');
diff --git a/desktop/tests/outliner-model.test.ts b/desktop/tests/outliner-model.test.ts
index c98e39381..dba61da04 100644
--- a/desktop/tests/outliner-model.test.ts
+++ b/desktop/tests/outliner-model.test.ts
@@ -186,4 +186,11 @@ describe('sort mode', () => {
it('type sorts by kind then name', () => {
expect(sortedKeys('type')).toEqual(['e2', 'e4', 'e3', 'e1']); // camera(Apple), then sprites Aaa, Mid, Zed
});
+
+ it('manual sort orders sibling folders by the scene folder list, not alphabetically', () => {
+ const data = { version: '1.0', name: 'f', entities: [] } as unknown as SceneData;
+ const folders = ['Zebra', 'Apple']; // created Z then A
+ expect(keys(buildOutlinerItems(data, { expanded: new Set(), folders }))).toEqual([folderKey('Zebra'), folderKey('Apple')]);
+ expect(keys(buildOutlinerItems(data, { expanded: new Set(), folders, sort: 'name' }))).toEqual([folderKey('Apple'), folderKey('Zebra')]);
+ });
});