refactor: move body focus detection

This commit is contained in:
lix-2026
2026-05-25 02:19:57 +08:00
parent 6d0852e431
commit a3abeadfe8
5 changed files with 20 additions and 17 deletions
@@ -20,6 +20,14 @@ pub(crate) fn schedule_editor_focus(editor: TiptapEditorHandle) {
callback.forget();
}
pub(crate) fn body_has_focus() -> bool {
window()
.and_then(|win| win.document())
.and_then(|document| document.active_element())
.map(|element| element.tag_name().eq_ignore_ascii_case("body"))
.unwrap_or(false)
}
pub(crate) fn active_editor_stage() -> bool {
let active_element_inside_stage = window()
.and_then(|win| win.document())
+2 -7
View File
@@ -30,7 +30,7 @@ use editor_runtime::command_sync::{
use editor_runtime::dom_selection::{
has_active_text_selection, selection_summary,
};
use editor_runtime::editor_focus::{active_editor_stage, schedule_editor_focus};
use editor_runtime::editor_focus::{active_editor_stage, body_has_focus, schedule_editor_focus};
use editor_runtime::overlays::{
floating_toolbar_anchor_from_selection, sync_text_selection_overlay, FloatingToolbarAnchor,
};
@@ -8284,12 +8284,7 @@ fn App(mount_options: MountOptions) -> impl IntoView {
let keydown_handle = window_event_listener(ev::keydown, move |event| {
let focused = active_editor_stage();
if !focused {
let body_has_focus = window()
.and_then(|win| win.document())
.and_then(|document| document.active_element())
.map(|element| element.tag_name().eq_ignore_ascii_case("body"))
.unwrap_or(false);
if body_has_focus && (event.ctrl_key() || event.meta_key()) && !event.alt_key() {
if body_has_focus() && (event.ctrl_key() || event.meta_key()) && !event.alt_key() {
let key = event.key();
let undo_shortcut = !event.shift_key() && key.eq_ignore_ascii_case("z");
let redo_shortcut =