[PR #6915] [MERGED] feat(chat): add selected message text to a lorebook #7382

Closed
opened 2026-10-23 04:19:14 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/6915
Author: @SpicyMarinara
Created: 9/30/2026
Status: ✅ Merged
Merged: 9/30/2026
Merged by: @SpicyMarinara

Base: staging ← Head: feat/selection-lorebook-entry


📝 Commits (6)

  • 1bd4b08 feat(chat): add selected message text to a lorebook
  • 42e6a7e fix(ui): return focus to where it was when a context menu closes
  • 02c289d fix(ui): keep context menu focus handling opt-in
  • 9f2a240 fix(chat): ignore lorebook picks while an entry is being created
  • 9639f03 Merge remote-tracking branch 'origin/staging' into feat/selection-lorebook-entry
  • ffed1b7 Merge branch 'staging' into feat/selection-lorebook-entry

📊 Changes

9 files changed (+317 additions, -23 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
➕ e2e/selection-lorebook-entry.e2e.ts (+91 -0)
📝 packages/client/src/components/chat/ChatArea.tsx (+7 -1)
➕ packages/client/src/components/chat/SelectionLorebookButton.tsx (+151 -0)
📝 packages/client/src/components/lorebooks/LorebookEditor.tsx (+16 -5)
📝 packages/client/src/components/ui/ContextMenu.tsx (+26 -5)
📝 packages/client/src/hooks/use-editor-sections.ts (+17 -11)
📝 packages/client/src/localization/locales/en.json (+3 -0)
📝 packages/client/src/stores/ui.store.ts (+5 -1)

📄 Description

Closes #6899

Why this change

Adding a lorebook entry for something that just came up in a chat means leaving the chat, opening Lorebooks, picking the book, adding an entry and typing its name. The request (NovelAI and SillyTavern extensions have the same idea) is to select the text and get a new entry already named after it. The maintainer asked about mobile; the reporter proposed a small plus button that appears on selection, since phones already show their own selection menu.

What changed

  • Add to lorebook button (SelectionLorebookButton, mounted once in ChatArea). It appears under a selection of up to 200 characters, the entry-name limit, inside any chat message, in every mode, on desktop and mobile.

    • It uses the same "selection is inside this message" check as Reply.
    • The browser's native context menu is untouched, so copy, paste, spellcheck and translate still work.
    • It opens on press, so a tap works before the phone collapses the selection.
  • Lorebook picker: the existing ContextMenu lists lorebooks, with the chat's own lorebooks first. Choosing one creates the entry through the existing useCreateLorebookEntry, with the same defaults as the editor's Add entry. The selection becomes both the entry's name and its keyword, and content is left for the user.

  • Opening the lorebook: openLorebookDetail(id, { initialTab: "entries", entryId }) opens the lorebook, and the editor calls its existing jumpToEntry once, so the entry opens expanded.

  • Keeping the entry in view: useEditorSections kept re-anchoring to its first section whenever content grew, so a new entry opened this way was scrolled back out of view. The hook now returns the same anchored scroll for any element (scrollToElement), and jumpToEntry uses it. Jumping to an entry from a lint warning or test result benefits too.

  • ContextMenu: scrolls when the list is taller than the screen, closes only when the page behind it scrolls (not when the menu itself does), and shows a focus style. A new opt-in autoFocus prop focuses the first item and returns focus on close. Only the new button uses it, and only when opened from the keyboard; existing menus keep their focus behaviour, and phones don't get focus handed back to the composer. The button stays mounted while its picker is open, so Escape returns focus to it.

  • No duplicates: picks are ignored while an entry is being created, so a slow request can't produce a duplicate entry.

  • Localization: three English keys under chat.selectionLorebook.*. The CHANGELOG entry is included.

  • Test: e2e/selection-lorebook-entry.e2e.ts runs on desktop Chromium, mobile Chromium and mobile WebKit, tapping on mobile. It selects "Silver Keep" in a message, adds it to a lorebook that already has 30 entries, and checks:

    • the entry is saved with that name and keyword;
    • the editor opens with the new entry in view and expanded.

    On desktop it also checks the keyboard path: Enter opens the picker with focus inside, and Escape returns focus to the button. With jumpToEntry reverted to plain scrollIntoView, the test fails with the entry out of view.

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

Agent-run results (not substitutes for the checklist above):

  • pnpm check passes.
  • The new e2e passes 3/3 across the three browser projects, also after merging current staging.
  • pnpm smoke:ui passes 7/7.
  • Local CodeRabbit reviewed all 9 files and raised one minor finding (a repeated pick during a slow create could duplicate the entry). It's fixed in 9f2a2405b.

To check by hand:

  • Manually select a name in a Roleplay, Conversation and Game message on desktop, press Add to lorebook, pick a lorebook, and confirm the entry opens expanded, named and keyed after the selection.
  • Manually do the same on a real phone (iOS Safari and Android Chrome). Check the button doesn't fight the native selection handles or menu, and one tap opens the picker.
  • Manually check a selection over 200 characters shows no button, and selecting text outside messages (composer, settings) shows none.
  • Manually open the lorebook picker with many lorebooks and confirm it scrolls and a keyboard user can Tab/Enter through it.
  • Manually right-click in the Game Assets browser and Bot Browser (other ContextMenu users) and confirm their menus still open, act and close as before.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

The CHANGELOG entry is included. No docs/ pages changed.

UI evidence (if applicable)

Desktop Chromium and mobile WebKit screenshots, in light and dark mode, were captured from this branch: the selection button, the picker, and the new entry open in the editor. Uploading them from the agent session wasn't permitted, so they're available from the author.

Summary by CodeRabbit

  • New Features
    • In desktop and mobile chats, select a word or short phrase to add it to a lorebook. Choose a lorebook to create an entry with the selection as its name and keyword; the new entry opens in the editor.
  • Usability
    • Lorebook selection menus can be scrolled without closing, stay within the viewport, and support keyboard focus. Dismissing the menu restores focus to where it was before it opened. When the editor opens the new entry, it is brought into view and expanded.

🔄 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/6915 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 9/30/2026 **Status:** ✅ Merged **Merged:** 9/30/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `feat/selection-lorebook-entry` --- ### 📝 Commits (6) - [`1bd4b08`](https://github.com/Pasta-Devs/Marinara-Engine/commit/1bd4b08fd89d51c97a1d7243bf99b0d4741df33e) feat(chat): add selected message text to a lorebook - [`42e6a7e`](https://github.com/Pasta-Devs/Marinara-Engine/commit/42e6a7ee14d0a12b5ae593c481842be3f8227aa3) fix(ui): return focus to where it was when a context menu closes - [`02c289d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/02c289db8d5bfa49a375a8bf9b63c7211fd9325b) fix(ui): keep context menu focus handling opt-in - [`9f2a240`](https://github.com/Pasta-Devs/Marinara-Engine/commit/9f2a2405b61bb2346d7158433acb63664c7e5189) fix(chat): ignore lorebook picks while an entry is being created - [`9639f03`](https://github.com/Pasta-Devs/Marinara-Engine/commit/9639f03c54227c4dec27063812cdbe53e6a7c019) Merge remote-tracking branch 'origin/staging' into feat/selection-lorebook-entry - [`ffed1b7`](https://github.com/Pasta-Devs/Marinara-Engine/commit/ffed1b795381a6fdd102148e0a73aaabcb722462) Merge branch 'staging' into feat/selection-lorebook-entry ### 📊 Changes **9 files changed** (+317 additions, -23 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) ➕ `e2e/selection-lorebook-entry.e2e.ts` (+91 -0) 📝 `packages/client/src/components/chat/ChatArea.tsx` (+7 -1) ➕ `packages/client/src/components/chat/SelectionLorebookButton.tsx` (+151 -0) 📝 `packages/client/src/components/lorebooks/LorebookEditor.tsx` (+16 -5) 📝 `packages/client/src/components/ui/ContextMenu.tsx` (+26 -5) 📝 `packages/client/src/hooks/use-editor-sections.ts` (+17 -11) 📝 `packages/client/src/localization/locales/en.json` (+3 -0) 📝 `packages/client/src/stores/ui.store.ts` (+5 -1) </details> ### 📄 Description Closes #6899 ## Why this change Adding a lorebook entry for something that just came up in a chat means leaving the chat, opening Lorebooks, picking the book, adding an entry and typing its name. The request (NovelAI and SillyTavern extensions have the same idea) is to select the text and get a new entry already named after it. The maintainer asked about mobile; the reporter proposed a small plus button that appears on selection, since phones already show their own selection menu. ## What changed - **Add to lorebook button** (`SelectionLorebookButton`, mounted once in `ChatArea`). It appears under a selection of up to 200 characters, the entry-name limit, inside any chat message, in every mode, on desktop and mobile. - It uses the same "selection is inside this message" check as Reply. - The browser's native context menu is untouched, so copy, paste, spellcheck and translate still work. - It opens on press, so a tap works before the phone collapses the selection. - **Lorebook picker:** the existing `ContextMenu` lists lorebooks, with the chat's own lorebooks first. Choosing one creates the entry through the existing `useCreateLorebookEntry`, with the same defaults as the editor's **Add entry**. The selection becomes both the entry's name and its keyword, and content is left for the user. - **Opening the lorebook:** `openLorebookDetail(id, { initialTab: "entries", entryId })` opens the lorebook, and the editor calls its existing `jumpToEntry` once, so the entry opens expanded. - **Keeping the entry in view:** `useEditorSections` kept re-anchoring to its first section whenever content grew, so a new entry opened this way was scrolled back out of view. The hook now returns the same anchored scroll for any element (`scrollToElement`), and `jumpToEntry` uses it. Jumping to an entry from a lint warning or test result benefits too. - **`ContextMenu`:** scrolls when the list is taller than the screen, closes only when the page behind it scrolls (not when the menu itself does), and shows a focus style. A new opt-in `autoFocus` prop focuses the first item and returns focus on close. Only the new button uses it, and only when opened from the keyboard; existing menus keep their focus behaviour, and phones don't get focus handed back to the composer. The button stays mounted while its picker is open, so Escape returns focus to it. - **No duplicates:** picks are ignored while an entry is being created, so a slow request can't produce a duplicate entry. - **Localization:** three English keys under `chat.selectionLorebook.*`. The CHANGELOG entry is included. - **Test:** `e2e/selection-lorebook-entry.e2e.ts` runs on desktop Chromium, mobile Chromium and mobile WebKit, tapping on mobile. It selects "Silver Keep" in a message, adds it to a lorebook that already has 30 entries, and checks: - the entry is saved with that name and keyword; - the editor opens with the new entry in view and expanded. On desktop it also checks the keyboard path: Enter opens the picker with focus inside, and Escape returns focus to the button. With `jumpToEntry` reverted to plain `scrollIntoView`, the test fails with the entry out of view. ## 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 Agent-run results (not substitutes for the checklist above): - `pnpm check` passes. - The new e2e passes 3/3 across the three browser projects, also after merging current staging. - `pnpm smoke:ui` passes 7/7. - Local CodeRabbit reviewed all 9 files and raised one minor finding (a repeated pick during a slow create could duplicate the entry). It's fixed in `9f2a2405b`. To check by hand: - Manually select a name in a Roleplay, Conversation and Game message on desktop, press **Add to lorebook**, pick a lorebook, and confirm the entry opens expanded, named and keyed after the selection. - Manually do the same on a real phone (iOS Safari and Android Chrome). Check the button doesn't fight the native selection handles or menu, and one tap opens the picker. - Manually check a selection over 200 characters shows no button, and selecting text outside messages (composer, settings) shows none. - Manually open the lorebook picker with many lorebooks and confirm it scrolls and a keyboard user can Tab/Enter through it. - Manually right-click in the Game Assets browser and Bot Browser (other `ContextMenu` users) and confirm their menus still open, act and close as before. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) The CHANGELOG entry is included. No `docs/` pages changed. ## UI evidence (if applicable) Desktop Chromium and mobile WebKit screenshots, in light and dark mode, were captured from this branch: the selection button, the picker, and the new entry open in the editor. Uploading them from the agent session wasn't permitted, so they're available from the author. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * In desktop and mobile chats, select a word or short phrase to add it to a lorebook. Choose a lorebook to create an entry with the selection as its name and keyword; the new entry opens in the editor. * **Usability** * Lorebook selection menus can be scrolled without closing, stay within the viewport, and support keyboard focus. Dismissing the menu restores focus to where it was before it opened. When the editor opens the new entry, it is brought into view and expanded. <!-- 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-23 04:19:14 +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#7382
No description provided.