[PR #2602] [MERGED] feat(lorebooks): nested folders #5686

Closed
opened 2026-10-22 04:18:30 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2602
Author: @kolacheee
Created: 6/17/2026
Status: ✅ Merged
Merged: 6/18/2026
Merged by: @SpicyMarinara

Base: staging ← Head: feat/staging-nested-lorebook-folders


📝 Commits (10+)

  • dedf986 feat(lorebooks): nested folders — server unblock + cycle guard
  • 8ca8af7 feat(lorebooks): nested folders — client tree UI + ancestor-aware disable
  • 600f730 feat(lorebooks): drag-to-nest folders — three-zone drag + root un-nest
  • 5dab11a feat(lorebooks): clone a folder — deep copy of sub-folders and entries
  • 916fa1d fix(lorebooks): drop entries into the innermost folder under the cursor
  • c7cb2b9 fix(lorebooks): arm folder drag synchronously so it reliably starts
  • 2688826 fix(lorebooks): nest a dragged folder when dropped into another folder's body
  • be3cdda fix(lorebooks): stop the un-nest strip from cancelling nested-folder drags
  • c039951 fix(lorebooks): un-nest a subfolder by dragging it down into its parent's body
  • b9377d4 fix(lorebooks): un-nest a folder dropped onto the root-entries area

📊 Changes

10 files changed (+850 additions, -202 deletions)

View changed files

📝 packages/client/src/components/lorebooks/LorebookEditor.tsx (+320 -156)
📝 packages/client/src/components/lorebooks/LorebookFolderRow.tsx (+112 -10)
📝 packages/client/src/components/ui/AppDialogRenderer.tsx (+30 -15)
📝 packages/client/src/hooks/use-lorebooks.ts (+16 -2)
📝 packages/client/src/lib/app-dialogs.ts (+21 -1)
📝 packages/client/src/stores/dialog.store.ts (+2 -0)
📝 packages/server/src/routes/lorebooks.routes.ts (+37 -10)
📝 packages/server/src/services/storage/lorebooks.storage.ts (+120 -8)
📝 packages/shared/src/index.ts (+1 -0)
➕ packages/shared/src/utils/lorebook-folder-tree.ts (+191 -0)

📄 Description

Linked issue

Closes #2582

Why this change

Lorebook folders are flat today. The data layer was already stubbed for nesting (the parent_folder_id column, LorebookFolder.parentFolderId, and the Zod schemas all exist) but the routes deliberately rejected any non-null parent with a 400. This finishes the feature so users can organize large lorebooks hierarchically (folders within folders).

What changed

Shared

  • utils/lorebook-folder-tree.ts (new) — one source of truth for server + client: canReparentFolder (no self-parent, same-lorebook, no descendant cycle via an upward parent-walk; no depth limit), buildFolderForest (flat rows → tree; a dangling/cyclic parent falls back to root), collectHiddenFolderIds (collapse hides the subtree), collectEffectivelyDisabledFolderIds (a disabled ancestor gates its descendants).

Server

  • lorebooks.routes.ts — removed the two 400 "Nested folders not supported" guards. Create now requires the parent to exist in the lorebook; move (PATCH) runs canReparentFolder and returns the specific reason on rejection.
  • lorebooks.storage.ts removeFolder — deleting a folder promotes its direct child folders to root (parentFolderId = null), mirroring how entries already reparent. No cascade.
  • lorebooks.storage.ts cloneFolder + POST /:id/folders/:folderId/clone — deep-clones a folder, its entries, and its whole sub-folder subtree into the same lorebook, as a sibling of the original. Folders are recreated top-down (each parent before its children); entries are then copied into the mapped new folders. Reuses createFolder/createEntry; a seen-guard makes a malformed cycle safe.

Client

  • LorebookEditor renders the folder forest recursively (indent + collapse-hides-subtree + disabled-ancestor propagation; per-render cycle guard).
  • LorebookFolderRow gained a parent-folder dropdown, filtered through canReparentFolder so invalid (self / descendant / cross-lorebook) targets never appear.
  • The activation gate (lorebooks.storage.ts) uses collectEffectivelyDisabledFolderIds, so a disabled parent gates entries in its enabled children.
  • Drag-to-nest: a folder header has three drop bands — the middle nests the dragged folder inside (reparent), top/bottom reorder it as a sibling. To un-nest by drag, drop the folder onto the folder-list area (the list itself is the drop zone) or drag it past its parent and out the bottom — the per-row parent dropdown also offers a "(top level)" option. The nest band only arms for a legal move (canReparentFolder) and is re-checked at drop; it draws an amber ring on the target. Reparenting reuses the existing folder PATCH (no new endpoint); entry-move drag is unchanged.
  • Clone action on each folder row (useCloneLorebookFolder, Copy icon beside delete) — one click deep-copies the folder and its whole subtree; only the root copy is renamed "(Copy)".

Re-parenting is available two ways: the per-row parent dropdown, or drag-to-nest (above). Nesting depth is unlimited.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

  • Shared build, server typecheck, and client typecheck + lint all pass.
  • Created a folder, then create/move another folder under it (parent dropdown) → it nests and renders indented.
  • Dragged a folder onto the middle of another folder's header → it nests inside (amber ring on the target). Drag onto the top/bottom edge → it reorders as a sibling.
  • Dragged a nested folder out onto the folder-list area → it un-nests back to the top level (also via the row's "(top level)" dropdown option).
  • Cloned a folder that has sub-folders and entries → a sibling "(Copy)" appears with the whole subtree duplicated; the originals stay untouched, and editing the clone doesn't change the original.
  • Dragged an entry into a deeply-nested folder's empty area → it lands in that folder (not a top-level ancestor); while dragging, slide left onto an ancestor's indent rail (it highlights amber) → it targets that ancestor instead.
  • Nested several levels deep (A › B › C › D) and confirm each indents; neither the dropdown nor drag ever offers a folder its own descendant as a parent.
  • Confirmed the API rejects a cycle with a clear message (the same canReparentFolder guard runs server-side).
  • Deleted a parent folder → its child folders rise to the top level (not deleted) and its entries fall back to root (unless selecting the option to delete all contents).

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (if applicable)

{1DCB24D5-FB5F-44FF-8F54-765121A4383D} {AAF9E982-3B8F-4747-B760-D604152D2A31} {D79F8B5A-E1F4-4810-AF20-13CFBAA4B70A} {E5B50388-61D0-4F8D-B03F-D8FCA186104A}

Summary by CodeRabbit

  • New Features
    • Added recursive folder nesting in lorebook entries via drag-and-drop
    • Added folder cloning functionality to duplicate folders with all content
    • Added cascade deletion option when removing folders with nested entries or subfolders
    • Enhanced folder organization with improved drop zones and root-level lifting support

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/2602 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 6/17/2026 **Status:** ✅ Merged **Merged:** 6/18/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `feat/staging-nested-lorebook-folders` --- ### 📝 Commits (10+) - [`dedf986`](https://github.com/Pasta-Devs/Marinara-Engine/commit/dedf9868ef6b2e6b12e1431dd2922de6ded0a534) feat(lorebooks): nested folders — server unblock + cycle guard - [`8ca8af7`](https://github.com/Pasta-Devs/Marinara-Engine/commit/8ca8af717ab8b817e57e3b522bb69b043ce5caf3) feat(lorebooks): nested folders — client tree UI + ancestor-aware disable - [`600f730`](https://github.com/Pasta-Devs/Marinara-Engine/commit/600f7309926837cc085bf2f216931bc6ba36fbf8) feat(lorebooks): drag-to-nest folders — three-zone drag + root un-nest - [`5dab11a`](https://github.com/Pasta-Devs/Marinara-Engine/commit/5dab11a4a8a7ff3b880b0a385de3f6038d9c5d17) feat(lorebooks): clone a folder — deep copy of sub-folders and entries - [`916fa1d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/916fa1dedc1963933df075d9a7743c7aac2799d8) fix(lorebooks): drop entries into the innermost folder under the cursor - [`c7cb2b9`](https://github.com/Pasta-Devs/Marinara-Engine/commit/c7cb2b92452662df96999ff59cd1a1a253f4ffdf) fix(lorebooks): arm folder drag synchronously so it reliably starts - [`2688826`](https://github.com/Pasta-Devs/Marinara-Engine/commit/26888263b1d2b12cb84b9ba519733c1e2a8b03d9) fix(lorebooks): nest a dragged folder when dropped into another folder's body - [`be3cdda`](https://github.com/Pasta-Devs/Marinara-Engine/commit/be3cdda08ff57477535eb86dc53dea5d23d7f68f) fix(lorebooks): stop the un-nest strip from cancelling nested-folder drags - [`c039951`](https://github.com/Pasta-Devs/Marinara-Engine/commit/c039951d17fd607c93b69ab0add8e9cda1ba7f45) fix(lorebooks): un-nest a subfolder by dragging it down into its parent's body - [`b9377d4`](https://github.com/Pasta-Devs/Marinara-Engine/commit/b9377d44f916ebde293594f06258c3caa95a26ea) fix(lorebooks): un-nest a folder dropped onto the root-entries area ### 📊 Changes **10 files changed** (+850 additions, -202 deletions) <details> <summary>View changed files</summary> 📝 `packages/client/src/components/lorebooks/LorebookEditor.tsx` (+320 -156) 📝 `packages/client/src/components/lorebooks/LorebookFolderRow.tsx` (+112 -10) 📝 `packages/client/src/components/ui/AppDialogRenderer.tsx` (+30 -15) 📝 `packages/client/src/hooks/use-lorebooks.ts` (+16 -2) 📝 `packages/client/src/lib/app-dialogs.ts` (+21 -1) 📝 `packages/client/src/stores/dialog.store.ts` (+2 -0) 📝 `packages/server/src/routes/lorebooks.routes.ts` (+37 -10) 📝 `packages/server/src/services/storage/lorebooks.storage.ts` (+120 -8) 📝 `packages/shared/src/index.ts` (+1 -0) ➕ `packages/shared/src/utils/lorebook-folder-tree.ts` (+191 -0) </details> ### 📄 Description ## Linked issue Closes #2582 ## Why this change Lorebook folders are flat today. The data layer was already stubbed for nesting (the `parent_folder_id` column, `LorebookFolder.parentFolderId`, and the Zod schemas all exist) but the routes deliberately rejected any non-null parent with a 400. This finishes the feature so users can organize large lorebooks hierarchically (folders within folders). ## What changed **Shared** - `utils/lorebook-folder-tree.ts` (new) — one source of truth for server + client: `canReparentFolder` (no self-parent, same-lorebook, no descendant cycle via an upward parent-walk; no depth limit), `buildFolderForest` (flat rows → tree; a dangling/cyclic parent falls back to root), `collectHiddenFolderIds` (collapse hides the subtree), `collectEffectivelyDisabledFolderIds` (a disabled ancestor gates its descendants). **Server** - `lorebooks.routes.ts` — removed the two `400 "Nested folders not supported"` guards. Create now requires the parent to exist in the lorebook; move (PATCH) runs `canReparentFolder` and returns the specific reason on rejection. - `lorebooks.storage.ts` `removeFolder` — deleting a folder promotes its direct child folders to root (`parentFolderId = null`), mirroring how entries already reparent. No cascade. - `lorebooks.storage.ts` `cloneFolder` + `POST /:id/folders/:folderId/clone` — deep-clones a folder, its entries, and its whole sub-folder subtree into the same lorebook, as a sibling of the original. Folders are recreated top-down (each parent before its children); entries are then copied into the mapped new folders. Reuses `createFolder`/`createEntry`; a seen-guard makes a malformed cycle safe. **Client** - `LorebookEditor` renders the folder forest recursively (indent + collapse-hides-subtree + disabled-ancestor propagation; per-render cycle guard). - `LorebookFolderRow` gained a parent-folder dropdown, filtered through `canReparentFolder` so invalid (self / descendant / cross-lorebook) targets never appear. - The activation gate (`lorebooks.storage.ts`) uses `collectEffectivelyDisabledFolderIds`, so a disabled parent gates entries in its enabled children. - **Drag-to-nest**: a folder header has three drop bands — the middle nests the dragged folder inside (reparent), top/bottom reorder it as a sibling. To un-nest by drag, drop the folder onto the folder-list area (the list itself is the drop zone) or drag it past its parent and out the bottom — the per-row parent dropdown also offers a "(top level)" option. The nest band only arms for a legal move (`canReparentFolder`) and is re-checked at drop; it draws an amber ring on the target. Reparenting reuses the existing folder PATCH (no new endpoint); entry-move drag is unchanged. - **Clone** action on each folder row (`useCloneLorebookFolder`, Copy icon beside delete) — one click deep-copies the folder and its whole subtree; only the root copy is renamed "(Copy)". **Re-parenting** is available two ways: the per-row parent dropdown, or drag-to-nest (above). Nesting depth is unlimited. ## Validation - [x] `pnpm check` passes locally - [x] Container (Docker / Podman) built and ran without issue - [x] Ran the app, clicked through the changes manually - [x] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [x] Above manual verification completed (describe below) - [x] Read and followed `CONTRIBUTING.md` ### Manual verification notes - Shared build, server typecheck, and client typecheck + lint all pass. - Created a folder, then create/move another folder under it (parent dropdown) → it nests and renders indented. - Dragged a folder onto the **middle** of another folder's header → it nests inside (amber ring on the target). Drag onto the **top/bottom edge** → it reorders as a sibling. - Dragged a nested folder out onto the folder-list area → it un-nests back to the top level (also via the row's **"(top level)"** dropdown option). - Cloned a folder that has sub-folders and entries → a sibling **"(Copy)"** appears with the whole subtree duplicated; the originals stay untouched, and editing the clone doesn't change the original. - Dragged an entry into a deeply-nested folder's empty area → it lands in **that** folder (not a top-level ancestor); while dragging, slide left onto an ancestor's indent rail (it highlights amber) → it targets that ancestor instead. - Nested several levels deep (A › B › C › D) and confirm each indents; neither the dropdown nor drag ever offers a folder its own descendant as a parent. - Confirmed the API rejects a cycle with a clear message (the same `canReparentFolder` guard runs server-side). - Deleted a parent folder → its child folders rise to the top level (not deleted) and its entries fall back to root (unless selecting the option to delete all contents). ## Docs and release impact - [x] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (if applicable) <img width="783" height="556" alt="{1DCB24D5-FB5F-44FF-8F54-765121A4383D}" src="https://github.com/user-attachments/assets/5d89c4e4-85ec-4987-92b0-5a7518b76e47" /> <img width="647" height="256" alt="{AAF9E982-3B8F-4747-B760-D604152D2A31}" src="https://github.com/user-attachments/assets/c318c704-6009-43ce-a44c-d157fd6702a6" /> <img width="638" height="246" alt="{D79F8B5A-E1F4-4810-AF20-13CFBAA4B70A}" src="https://github.com/user-attachments/assets/8fb5f912-0af8-48c9-8381-7b4b73027118" /> <img width="780" height="552" alt="{E5B50388-61D0-4F8D-B03F-D8FCA186104A}" src="https://github.com/user-attachments/assets/a8db2f03-f4e3-4de0-ba0d-5ac74b396b8e" /> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added recursive folder nesting in lorebook entries via drag-and-drop * Added folder cloning functionality to duplicate folders with all content * Added cascade deletion option when removing folders with nested entries or subfolders * Enhanced folder organization with improved drop zones and root-level lifting support <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:18:30 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#5686
No description provided.