fix: stabilize block menu undo focus
This commit is contained in:
@@ -3655,6 +3655,20 @@ fn schedule_scroll_mnote_block_anchor_from_hash_retry(remaining: u8) {
|
||||
callback.forget();
|
||||
}
|
||||
|
||||
fn schedule_editor_focus(editor: TiptapEditorHandle) {
|
||||
let Some(win) = window() else {
|
||||
return;
|
||||
};
|
||||
let callback = Closure::<dyn FnMut()>::new(move || {
|
||||
let _ = editor.focus();
|
||||
});
|
||||
let _ = win.set_timeout_with_callback_and_timeout_and_arguments_0(
|
||||
callback.as_ref().unchecked_ref(),
|
||||
0,
|
||||
);
|
||||
callback.forget();
|
||||
}
|
||||
|
||||
fn current_viewport_scroll() -> Option<(f64, f64)> {
|
||||
let win = window()?;
|
||||
let x = win.scroll_x().ok()?;
|
||||
@@ -8313,6 +8327,28 @@ 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() {
|
||||
let key = event.key();
|
||||
let undo_shortcut = !event.shift_key() && key.eq_ignore_ascii_case("z");
|
||||
let redo_shortcut =
|
||||
key.eq_ignore_ascii_case("y")
|
||||
|| (event.shift_key() && key.eq_ignore_ascii_case("z"));
|
||||
if undo_shortcut || redo_shortcut {
|
||||
event.prevent_default();
|
||||
if redo_shortcut {
|
||||
let _ = editor.redo();
|
||||
} else {
|
||||
let _ = editor.undo();
|
||||
}
|
||||
schedule_editor_focus(editor);
|
||||
return;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -10092,6 +10128,10 @@ fn App(mount_options: MountOptions) -> impl IntoView {
|
||||
<button
|
||||
class="block-drag-menu-item"
|
||||
data-testid="block-drag-menu-item-delete"
|
||||
on:mousedown=move |event: MouseEvent| {
|
||||
event.prevent_default();
|
||||
event.stop_propagation();
|
||||
}
|
||||
on:click=move |event: MouseEvent| {
|
||||
event.stop_propagation();
|
||||
match editor_runtime::history_safe_commands::delete_top_level_block_with_history(&editor, block_index) {
|
||||
@@ -10110,7 +10150,7 @@ fn App(mount_options: MountOptions) -> impl IntoView {
|
||||
set_block_menu_open.set(false);
|
||||
set_block_menu_anchor.set(None);
|
||||
set_hovered_block.set(None);
|
||||
let _ = editor.focus();
|
||||
schedule_editor_focus(editor);
|
||||
}
|
||||
Err(err) => set_command_feedback.set(err),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user