atproto blogging
1//! Editor actions and keybinding system.
2//!
3//! This module re-exports core types and provides Dioxus-specific conversions.
4//! Action execution delegates entirely to `weaver_editor_core`.
5
6use dioxus::prelude::ModifiersInteraction;
7
8use super::document::SignalEditorDocument;
9use weaver_editor_browser::Platform;
10
11// Re-export core types.
12pub use weaver_editor_core::{
13 EditorAction, Key, KeyCombo, KeybindingConfig, KeydownResult, Modifiers, Range,
14};
15
16// === Dioxus conversion helpers ===
17
18/// Convert a dioxus keyboard_types::Key to our Key type.
19pub fn key_from_dioxus(key: dioxus::prelude::keyboard_types::Key) -> Key {
20 use dioxus::prelude::keyboard_types::Key as KT;
21
22 match key {
23 KT::Character(s) => Key::character(s.as_str()),
24 KT::Unidentified => Key::Unidentified,
25 KT::Backspace => Key::Backspace,
26 KT::Delete => Key::Delete,
27 KT::Enter => Key::Enter,
28 KT::Tab => Key::Tab,
29 KT::Escape => Key::Escape,
30 KT::Insert => Key::Insert,
31 KT::Clear => Key::Clear,
32 KT::ArrowLeft => Key::ArrowLeft,
33 KT::ArrowRight => Key::ArrowRight,
34 KT::ArrowUp => Key::ArrowUp,
35 KT::ArrowDown => Key::ArrowDown,
36 KT::Home => Key::Home,
37 KT::End => Key::End,
38 KT::PageUp => Key::PageUp,
39 KT::PageDown => Key::PageDown,
40 KT::Alt => Key::Alt,
41 KT::AltGraph => Key::AltGraph,
42 KT::CapsLock => Key::CapsLock,
43 KT::Control => Key::Control,
44 KT::Fn => Key::Fn,
45 KT::FnLock => Key::FnLock,
46 KT::Meta => Key::Meta,
47 KT::NumLock => Key::NumLock,
48 KT::ScrollLock => Key::ScrollLock,
49 KT::Shift => Key::Shift,
50 KT::Symbol => Key::Symbol,
51 KT::SymbolLock => Key::SymbolLock,
52 KT::Hyper => Key::Hyper,
53 KT::Super => Key::Super,
54 KT::F1 => Key::F1,
55 KT::F2 => Key::F2,
56 KT::F3 => Key::F3,
57 KT::F4 => Key::F4,
58 KT::F5 => Key::F5,
59 KT::F6 => Key::F6,
60 KT::F7 => Key::F7,
61 KT::F8 => Key::F8,
62 KT::F9 => Key::F9,
63 KT::F10 => Key::F10,
64 KT::F11 => Key::F11,
65 KT::F12 => Key::F12,
66 KT::F13 => Key::F13,
67 KT::F14 => Key::F14,
68 KT::F15 => Key::F15,
69 KT::F16 => Key::F16,
70 KT::F17 => Key::F17,
71 KT::F18 => Key::F18,
72 KT::F19 => Key::F19,
73 KT::F20 => Key::F20,
74 KT::ContextMenu => Key::ContextMenu,
75 KT::PrintScreen => Key::PrintScreen,
76 KT::Pause => Key::Pause,
77 KT::Help => Key::Help,
78 KT::Copy => Key::Copy,
79 KT::Cut => Key::Cut,
80 KT::Paste => Key::Paste,
81 KT::Undo => Key::Undo,
82 KT::Redo => Key::Redo,
83 KT::Find => Key::Find,
84 KT::Select => Key::Select,
85 KT::MediaPlayPause => Key::MediaPlayPause,
86 KT::MediaStop => Key::MediaStop,
87 KT::MediaTrackNext => Key::MediaTrackNext,
88 KT::MediaTrackPrevious => Key::MediaTrackPrevious,
89 KT::AudioVolumeDown => Key::AudioVolumeDown,
90 KT::AudioVolumeUp => Key::AudioVolumeUp,
91 KT::AudioVolumeMute => Key::AudioVolumeMute,
92 KT::Compose => Key::Compose,
93 KT::Convert => Key::Convert,
94 KT::NonConvert => Key::NonConvert,
95 KT::Dead => Key::Dead,
96 KT::HangulMode => Key::HangulMode,
97 KT::HanjaMode => Key::HanjaMode,
98 KT::JunjaMode => Key::JunjaMode,
99 KT::Eisu => Key::Eisu,
100 KT::Hankaku => Key::Hankaku,
101 KT::Hiragana => Key::Hiragana,
102 KT::HiraganaKatakana => Key::HiraganaKatakana,
103 KT::KanaMode => Key::KanaMode,
104 KT::KanjiMode => Key::KanjiMode,
105 KT::Katakana => Key::Katakana,
106 KT::Romaji => Key::Romaji,
107 KT::Zenkaku => Key::Zenkaku,
108 KT::ZenkakuHankaku => Key::ZenkakuHankaku,
109 _ => Key::Unidentified,
110 }
111}
112
113/// Create a KeyCombo from a dioxus keyboard event.
114pub fn keycombo_from_dioxus_event(event: &dioxus::events::KeyboardData) -> KeyCombo {
115 let key = key_from_dioxus(event.key());
116 let modifiers = Modifiers {
117 ctrl: event.modifiers().ctrl(),
118 alt: event.modifiers().alt(),
119 shift: event.modifiers().shift(),
120 meta: event.modifiers().meta(),
121 hyper: false,
122 super_: false,
123 };
124 KeyCombo::with_modifiers(key, modifiers)
125}
126
127/// Create a default KeybindingConfig for the given platform.
128pub fn default_keybindings(platform: &Platform) -> KeybindingConfig {
129 KeybindingConfig::default_for_platform(platform.mac)
130}
131
132/// Execute an editor action on a document with browser clipboard support.
133#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
134pub fn execute_action(doc: &mut SignalEditorDocument, action: &EditorAction) -> bool {
135 use weaver_editor_browser::BrowserClipboard;
136 use weaver_editor_core::execute_action_with_clipboard;
137
138 let clipboard = BrowserClipboard::empty();
139 execute_action_with_clipboard(doc, action, &clipboard)
140}
141
142/// Execute an editor action on a document (non-browser fallback).
143#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
144pub fn execute_action(doc: &mut SignalEditorDocument, action: &EditorAction) -> bool {
145 weaver_editor_core::execute_action(doc, action)
146}
147
148/// Handle a keydown event with browser clipboard support.
149#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
150pub fn handle_keydown_with_bindings(
151 doc: &mut SignalEditorDocument,
152 config: &KeybindingConfig,
153 combo: KeyCombo,
154 range: Range,
155) -> KeydownResult {
156 use weaver_editor_browser::BrowserClipboard;
157 use weaver_editor_core::handle_keydown_with_clipboard;
158
159 let clipboard = BrowserClipboard::empty();
160 handle_keydown_with_clipboard(doc, config, combo, range, &clipboard)
161}
162
163/// Handle a keydown event (non-browser fallback).
164#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
165pub fn handle_keydown_with_bindings(
166 doc: &mut SignalEditorDocument,
167 config: &KeybindingConfig,
168 combo: KeyCombo,
169 range: Range,
170) -> KeydownResult {
171 weaver_editor_core::handle_keydown(doc, config, combo, range)
172}