Skip to content

feat(desktop): rename sidebar selection with F2 / Enter (#1618) - #5638

Merged
Jocs merged 8 commits into
developfrom
feat/1618-sidebar-rename-keyboard
Oct 7, 2026
Merged

Jocs merged 8 commits into
developfrom
feat/1618-sidebar-rename-keyboard

Conversation

@Jocs

@Jocs Jocs commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Closes #1618

Summary

Make the sidebar project tree a DOM focus scope, then build the keyboard rename on top of it.

Focus scope

  • .tree-wrapper is focusable (tabindex="0"). Clicking the tree takes focus; clicking outside moves it away (browser default).
  • Clicking empty space inside the tree clears the selection while the tree keeps focus; clicking outside keeps the selection and only loses focus — both match VS Code's explorer (setFocus([]) + setSelection([]) on empty click, inactive selection on blur).
  • The keydown handler moved from a global document capture listener to the tree container, so keys only act on the sidebar while the tree owns focus. The isMuyaEditorTarget allow-list is gone.

Open / focus semantics

  • A single click opens a file but keeps DOM focus in the tree; a double click hands it to the editor. This is what makes Enter / F2 rename work right after opening a file (issue Press Enter on file name to rename selected file #1618's original request).
  • The sidebar records a single pathname-keyed open intent (keep focus in the tree, or hand it to the editor). The intent is read at whichever focus point runs for that open — the editor pane's first mount or file-loaded — so no flag has to travel through main's open path. A new Muya.init({ focus }) option lets the pane skip its initial caret.

Rename

  • F2 / Enter open the same inline rename input the context menu uses, with the file stem preselected (extension kept).
  • Empty / unchanged names close the input; conflicts surface a notification instead of failing silently.

Delete

Styling

  • Focus outline drawn on the focused row while the tree has focus (--focusColor, matching list.focusOutline), selection dims once focus leaves (matching list.inactiveSelectionBackground), and inline name inputs use the focus border.

Type of change

  • New feature (non-breaking, adds functionality)

Test plan

  • New tests added (or explain why not needed)

  • Manually tested on: macOS

  • test/unit/specs/sidebar-rename-key.spec.ts, sidebar-trash-key.spec.ts — shortcut/selection rules and stem preselection.

  • test/e2e/sidebar-focus-model.spec.ts — click row / empty space / outside, double-click focus.

  • test/e2e/issue-1618-sidebar-rename-key.spec.ts — single click keeps tree focus then F2 renames; double click focuses the editor where Enter stays a line break; Enter renames a folder; focus returns to the tree after committing.

  • test/e2e/issue-3148-sidebar-delete-key.spec.ts — Delete trashes the tree selection; Delete inside the editor edits text and leaves the file alone.

  • pnpm run lint, pnpm run typecheck, the muya suite (2361 tests) and the existing sidebar specs (Sidebar collapse state memory inconsistant #2421, failed to new file | 新建文件失败 #3439, Find panel behaviour wanting #3458) pass.

Notes for reviewers

  • Behavior changes to call out in the changelog: a single click no longer focuses the editor (double click or click into the editor does), and F2 / Enter / Delete only apply to the sidebar while the tree has focus.
  • packages/muya/src/editor/index.ts gained an optional init({ focus }) argument (default true, so existing callers are unchanged).

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

F2 (VS Code) and Enter (Finder / Windows Explorer) open the inline rename input on the selected file or folder, reusing the context-menu flow.

The input preselects the file stem so typing replaces the name and keeps the extension, matching VS Code's explorer. Folders select the whole name.

Enter stays a line break in the WYSIWYG editor: the shortcut defers to editable surfaces, except F2 which has no editor meaning.
@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Build artifacts for PR #5638:

Run: https://github.com/marktext/marktext/actions/runs/37577168114

Artifact Size Link
marktext-macos-x64 295.9 MB Download
marktext-macos-arm64 285.6 MB Download
marktext-linux 635.0 MB Download
marktext-windows-arm64 278.1 MB Download
marktext-windows-x64 286.3 MB Download

Jocs added 7 commits October 7, 2026 11:43
Clicking the project tree now takes DOM focus and clicking outside moves it away, so F2 / Enter / Delete act on the sidebar only while the tree is focused.

A single click opens a file but keeps that focus; a double click hands it to the editor. Delete no longer hijacks the editor's forward-delete.

Adds focusOutline/inactive-selection styling and click-empty-space-clears-selection, matching VS Code.
…1618)

Restoring the target tab's caret sets a DOM selection inside the contenteditable, which pulls DOM focus into the editor. After a sidebar-initiated switch, ask the tree to take the focus back so the keyboard scope stays in the sidebar.
Keep only the ones recording behaviour the code cannot express: the browser's mousedown focus default, the DOM-selection focus pull, and the double-click/pending-focus mechanism.
…1618)

pendingFocusPath, preserveFocusOnMount and the file-loaded preserveFocus payload are replaced by a single pathname-keyed open intent in the editor store, read at whichever focus point runs (first mount or file-loaded). TabOptions.preserveFocus and its main-process passthrough are gone.
Escape clears the selection directly and the trailing "any other key ends the selection" block is gone — the keydown handler is focus-scoped now, so a stale selection cannot happen.

Drop the name-input early return (both predicates already bail on editableTarget / isEditingName), collapse the closest() guards, and remove the now-unused isModifierKey and its test.
@Jocs
Jocs merged commit 638e9d9 into develop Oct 7, 2026
17 checks passed
@Jocs
Jocs deleted the feat/1618-sidebar-rename-keyboard branch October 7, 2026 06:22
Jocs added a commit that referenced this pull request Oct 7, 2026
* fix(desktop): commit sidebar rename/create when clicking away (#3207, #3385)

The sidebar's inline rename and new-file inputs only committed on Enter;
clicking or focusing away ran CLEAR_NAME_INPUT_STATE() and discarded the
edit (and cancelled a pending file creation). This matches neither Finder
nor Windows Explorer, which accept the edit on click-away.

The pending text now lives in the project store (nameInputValue) so the
tree's click / context-menu handlers can commit it via the new
COMMIT_NAME_INPUT() action, mirroring Enter. Escape still cancels. Seeding
the value in the store's SIDEBAR::rename / SIDEBAR::new handlers keeps the
single shared ref from being overwritten by every mounted row component.

The existing stem-only preselection for files (#5638) is unchanged.

Tests:
- unit: sidebar-name-input-commit.spec.ts
- e2e: issue-3207-sidebar-rename-commit.spec.ts

* fix(desktop): address click-away review findings (#3207, #3385)

Three follow-ups from the Copilot review of #5646:

- Listen for the document click in the capture phase. Targets that call
  event.stopPropagation() (project heading / collapse arrow, editor tabs)
  never reached the bubbling handler, so the pending edit was not committed
  — collapsing the tree even hid the input without saving.
- Keep the sidebar selection on the renamed path. A context menu opened
  while the commit was in flight kept the old pathname, so its actions
  targeted a path that no longer existed (Delete/Rename no-op, New File
  created nowhere). The selection now follows the rename.
- Make rename/create single-flight per input session. The caches are only
  cleared once the filesystem work resolves, so a second click could submit
  the same edit again (double move -> spurious error, double write).

Regression coverage: capture-phase e2e (project heading), duplicate-commit
unit tests, and a selection-sync unit test.

* fix(desktop): don't commit sidebar rename on right-click (#3207)

Committing the pending name on contextmenu was fragile: the row opens the
native menu (and sets the selection) synchronously, while the rename commits
asynchronously, and the menu's items read the selection when clicked. Even
after syncing the selection to the new path, "New File" from that menu still
produced no input, because the renamed folder is re-created collapsed and
handleInputFocus had already run before the node existed.

Keep the click-away commit for left clicks (the #3207 behaviour) and restore
the pre-existing cancel-on-right-click. The selection is still synced to the
renamed path so F2 / Delete right after a rename act on the new node.

* refactor(desktop): close the name input on submit instead of a pending flag

Replace the `nameInputPending` single-flight guard with a simpler rule:
`RENAME_IN_SIDEBAR` / `CREATE_FILE_DIRECTORY` capture what they need from the
caches and clear the input state synchronously, before the async filesystem
work. A second click-away then finds no open input and is a no-op, and the
input closes immediately (like Finder) rather than staying visible until the
filesystem call resolves.

Trade-off: on failure the input no longer stays open for a retry; the error
notification is shown and the user can rename/create again.

Also drops the now-unneeded `finally` plumbing.
csells added a commit to csells/MarkText that referenced this pull request Oct 13, 2026
Upstream's tree keeps focus when a single click opens or switches to a file
(marktext#5638). A Core document view mounts for each document and placing its
restored caret moves the browser's focus into it. The leased view now owns
the open intent, and a view mounted while the tree, or a name being edited
in it, had focus returns focus there unless the open asked for the editor.
csells added a commit to csells/MarkText that referenced this pull request Oct 13, 2026
Upstream marktext#5638 focused the contenteditable before Muya's focus() so a
double click on a sidebar file leaves a blinking caret. Muya's own focus()
here takes DOM focus on every path before restoring the model's exact
selection, which keeps command-palette review commands on their selection.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Press Enter on file name to rename selected file

1 participant