use crate::commands::{BEFORE_INPUT_COMMAND, COMPOSITION_END_COMMAND, COMPOSITION_START_COMMAND};
use crate::commands::{
CONTROLLED_TEXT_INSERTION_COMMAND, COPY_COMMAND, CUT_COMMAND, DELETE_CHARACTER_COMMAND,
DELETE_LINE_COMMAND, DELETE_WORD_COMMAND, FORMAT_TEXT_COMMAND, INSERT_LINE_BREAK_COMMAND,
INSERT_PARAGRAPH_COMMAND, KEY_ARROW_DOWN_COMMAND, KEY_ARROW_LEFT_COMMAND,
KEY_ARROW_RIGHT_COMMAND, KEY_ARROW_UP_COMMAND, KEY_BACKSPACE_COMMAND, KEY_DELETE_COMMAND,
KEY_DOWN_COMMAND, KEY_ENTER_COMMAND, KEY_ESCAPE_COMMAND, KEY_MODIFIER_COMMAND,
KEY_SPACE_COMMAND, KEY_TAB_COMMAND, MOVE_TO_END, MOVE_TO_START, PASTE_COMMAND, REDO_COMMAND,
REMOVE_TEXT_COMMAND, SELECT_ALL_COMMAND, UNDO_COMMAND,
};
use crate::constants::DOUBLE_LINE_BREAK;
use crate::editor::{
COMMAND_PRIORITY_EDITOR, COMPOSITION_END_TAG, COMPOSITION_START_TAG, SKIP_SELECTION_FOCUS_TAG,
};
use crate::environment::is_apple;
use crate::keyboard_shortcuts::{
CompiledKeyboardShortcuts, KeyboardShortcutMatch, compile_keyboard_shortcuts, control_or_alt,
control_or_meta,
};
use crate::lexical_selection::{SelectionRef, Which, get_selection, set_selection};
use crate::lexical_utils::{
KeyboardEventModifierMask, KeyboardEventModifiers, ModifierExpectation, get_node_by_key,
is_modifier, is_token_or_segmented, set_composition_key,
};
use crate::node::{NodeKey, NodeRef};
use crate::nodes::text::create_text_node;
use crate::selection_insert_nodes::{find_matching_parent, get_previous_selection};
use crate::{Editor, LexicalCommand, LexicalError, Unregister, utf16};
use std::cell::{Cell, RefCell};
use std::ops::Deref;
use std::rc::Rc;
#[derive(Debug)]
pub struct Event {
pub type_: String,
pub cancelable: bool,
pub time_stamp: f64,
default_prevented: Cell<bool>,
propagation_stopped: Cell<bool>,
}
impl Event {
pub fn new(type_: &str) -> Self {
Self::with_cancelable(type_, true)
}
fn with_cancelable(type_: &str, cancelable: bool) -> Self {
Self {
type_: type_.to_owned(),
cancelable,
time_stamp: 0.0,
default_prevented: Cell::new(false),
propagation_stopped: Cell::new(false),
}
}
pub fn prevent_default(&self) {
if self.cancelable {
self.default_prevented.set(true);
}
}
pub fn default_prevented(&self) -> bool {
self.default_prevented.get()
}
pub fn stop_propagation(&self) {
self.propagation_stopped.set(true);
}
pub fn propagation_stopped(&self) -> bool {
self.propagation_stopped.get()
}
}
macro_rules! extends {
($name:ty, $field:ident: $base:ty) => {
impl Deref for $name {
type Target = $base;
fn deref(&self) -> &$base {
&self.$field
}
}
};
}
#[derive(Debug)]
pub struct KeyboardEvent {
pub event: Event,
pub key: String,
pub code: String,
pub alt_key: bool,
pub ctrl_key: bool,
pub meta_key: bool,
pub shift_key: bool,
pub repeat: bool,
pub is_composing: bool,
}
extends!(KeyboardEvent, event: Event);
impl Default for KeyboardEvent {
fn default() -> Self {
Self {
event: Event::new("keydown"),
key: String::new(),
code: String::new(),
alt_key: false,
ctrl_key: false,
meta_key: false,
shift_key: false,
repeat: false,
is_composing: false,
}
}
}
impl KeyboardEventModifiers for KeyboardEvent {
fn key(&self) -> &str {
&self.key
}
fn code(&self) -> &str {
&self.code
}
fn alt_key(&self) -> bool {
self.alt_key
}
fn ctrl_key(&self) -> bool {
self.ctrl_key
}
fn meta_key(&self) -> bool {
self.meta_key
}
fn shift_key(&self) -> bool {
self.shift_key
}
}
#[derive(Debug)]
pub struct MouseEvent {
pub event: Event,
pub client_x: f64,
pub client_y: f64,
pub button: i32,
pub detail: i32,
pub alt_key: bool,
pub ctrl_key: bool,
pub meta_key: bool,
pub shift_key: bool,
pub target_key: Option<NodeKey>,
}
extends!(MouseEvent, event: Event);
impl MouseEvent {
pub fn new(type_: &str) -> Self {
Self {
event: Event::new(type_),
client_x: 0.0,
client_y: 0.0,
button: 0,
detail: 1,
alt_key: false,
ctrl_key: false,
meta_key: false,
shift_key: false,
target_key: None,
}
}
}
impl Default for MouseEvent {
fn default() -> Self {
Self::new("click")
}
}
impl KeyboardEventModifiers for MouseEvent {
fn key(&self) -> &str {
""
}
fn code(&self) -> &str {
""
}
fn alt_key(&self) -> bool {
self.alt_key
}
fn ctrl_key(&self) -> bool {
self.ctrl_key
}
fn meta_key(&self) -> bool {
self.meta_key
}
fn shift_key(&self) -> bool {
self.shift_key
}
}
#[derive(Debug)]
pub struct DragEvent {
pub mouse: MouseEvent,
pub data_transfer: Option<Rc<DataTransfer>>,
}
extends!(DragEvent, mouse: MouseEvent);
impl DragEvent {
pub fn new(type_: &str, data_transfer: Option<Rc<DataTransfer>>) -> Self {
Self {
mouse: MouseEvent::new(type_),
data_transfer,
}
}
}
#[derive(Debug)]
pub struct FocusEvent {
pub event: Event,
}
extends!(FocusEvent, event: Event);
impl FocusEvent {
pub fn new(type_: &str) -> Self {
Self {
event: Event::with_cancelable(type_, false),
}
}
}
#[derive(Debug)]
pub struct InputEvent {
pub event: Event,
pub input_type: String,
pub data: Option<String>,
pub data_transfer: Option<Rc<DataTransfer>>,
pub is_composing: bool,
}
extends!(InputEvent, event: Event);
impl InputEvent {
pub fn new(input_type: &str) -> Self {
Self {
event: Event::new("beforeinput"),
input_type: input_type.to_owned(),
data: None,
data_transfer: None,
is_composing: false,
}
}
}
#[derive(Debug)]
pub struct CompositionEvent {
pub event: Event,
pub data: String,
}
extends!(CompositionEvent, event: Event);
impl CompositionEvent {
pub fn new(type_: &str, data: &str) -> Self {
Self {
event: Event::with_cancelable(type_, false),
data: data.to_owned(),
}
}
}
#[derive(Debug)]
pub struct ClipboardEvent {
pub event: Event,
pub clipboard_data: Option<Rc<DataTransfer>>,
}
extends!(ClipboardEvent, event: Event);
impl ClipboardEvent {
pub fn new(type_: &str, clipboard_data: Option<Rc<DataTransfer>>) -> Self {
Self {
event: Event::new(type_),
clipboard_data,
}
}
}
#[derive(Clone, Debug)]
pub enum PasteCommandType {
Clipboard(Rc<ClipboardEvent>),
Input(Rc<InputEvent>),
Keyboard(Rc<KeyboardEvent>),
}
impl PasteCommandType {
pub fn event(&self) -> &Event {
match self {
PasteCommandType::Clipboard(e) => e,
PasteCommandType::Input(e) => e,
PasteCommandType::Keyboard(e) => e,
}
}
}
#[derive(Clone, Debug)]
pub enum ClipboardOrKeyboardEvent {
Clipboard(Rc<ClipboardEvent>),
Keyboard(Rc<KeyboardEvent>),
}
impl ClipboardOrKeyboardEvent {
pub fn event(&self) -> &Event {
match self {
ClipboardOrKeyboardEvent::Clipboard(e) => e,
ClipboardOrKeyboardEvent::Keyboard(e) => e,
}
}
pub fn clipboard_event(event: Option<&Self>) -> Option<&Rc<ClipboardEvent>> {
match event {
Some(ClipboardOrKeyboardEvent::Clipboard(e)) => Some(e),
_ => None,
}
}
}
#[derive(Clone)]
pub struct DataTransferFile {
pub name: String,
pub type_: String,
pub size: Option<usize>,
load: Rc<dyn Fn() -> std::io::Result<Vec<u8>>>,
}
impl DataTransferFile {
pub fn new(name: &str, type_: &str, data: Vec<u8>) -> Self {
let size = data.len();
Self::lazy(name, type_, Some(size), move || Ok(data.clone()))
}
pub fn lazy(
name: &str,
type_: &str,
size: Option<usize>,
load: impl Fn() -> std::io::Result<Vec<u8>> + 'static,
) -> Self {
Self {
name: name.to_owned(),
type_: type_.to_owned(),
size,
load: Rc::new(load),
}
}
pub fn bytes(&self) -> std::io::Result<Vec<u8>> {
(self.load)()
}
}
impl std::fmt::Debug for DataTransferFile {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DataTransferFile")
.field("name", &self.name)
.field("type_", &self.type_)
.field("size", &self.size)
.finish_non_exhaustive()
}
}
#[derive(Debug)]
pub struct DataTransfer {
entries: RefCell<Vec<(String, String)>>,
files: RefCell<Vec<DataTransferFile>>,
drop_effect: RefCell<String>,
effect_allowed: RefCell<String>,
}
impl Default for DataTransfer {
fn default() -> Self {
Self {
entries: RefCell::new(Vec::new()),
files: RefCell::new(Vec::new()),
drop_effect: RefCell::new("none".to_owned()),
effect_allowed: RefCell::new("uninitialized".to_owned()),
}
}
}
impl DataTransfer {
pub fn new() -> Self {
Self::default()
}
fn format(format: &str) -> String {
let format = format.to_lowercase();
match format.as_str() {
"text" => "text/plain".to_owned(),
"url" => "text/uri-list".to_owned(),
_ => format,
}
}
pub fn types(&self) -> Vec<String> {
let mut types: Vec<String> = self
.entries
.borrow()
.iter()
.map(|(t, _)| t.clone())
.collect();
if !self.files.borrow().is_empty() {
types.push("Files".to_owned());
}
types
}
pub fn get_data(&self, format: &str) -> String {
let format = Self::format(format);
self.entries
.borrow()
.iter()
.find(|(t, _)| *t == format)
.map(|(_, data)| data.clone())
.unwrap_or_default()
}
pub fn set_data(&self, format: &str, data: &str) {
let format = Self::format(format);
let mut entries = self.entries.borrow_mut();
match entries.iter_mut().find(|(t, _)| *t == format) {
Some(entry) => entry.1 = data.to_owned(),
None => entries.push((format, data.to_owned())),
}
}
pub fn clear_data(&self, format: Option<&str>) {
let mut entries = self.entries.borrow_mut();
match format {
Some(format) => {
let format = Self::format(format);
entries.retain(|(t, _)| *t != format);
}
None => entries.clear(),
}
}
pub fn files(&self) -> Vec<DataTransferFile> {
self.files.borrow().clone()
}
pub fn set_files(&self, files: Vec<DataTransferFile>) {
*self.files.borrow_mut() = files;
}
pub fn drop_effect(&self) -> String {
self.drop_effect.borrow().clone()
}
pub fn set_drop_effect(&self, drop_effect: &str) {
*self.drop_effect.borrow_mut() = drop_effect.to_owned();
}
pub fn effect_allowed(&self) -> String {
self.effect_allowed.borrow().clone()
}
pub fn set_effect_allowed(&self, effect_allowed: &str) {
*self.effect_allowed.borrow_mut() = effect_allowed.to_owned();
}
}
#[derive(Debug, Default)]
pub(crate) struct InputState {
pub(crate) is_insert_line_break: bool,
pub(crate) last_key_code: Option<String>,
pub(crate) composed_segmented_key: Option<NodeKey>,
}
pub fn on_key_down(event: &Rc<KeyboardEvent>, editor: &Editor) -> Result<(), LexicalError> {
editor.0.input_state.borrow_mut().last_key_code = Some(event.key.clone());
if editor.is_composing() {
return Ok(());
}
editor.dispatch_command(&KEY_DOWN_COMMAND, event.clone())?;
Ok(())
}
pub fn on_before_input(event: &Rc<InputEvent>, editor: &Editor) -> Result<(), LexicalError> {
if matches!(
event.input_type.as_str(),
"deleteCompositionText" | "insertCompositionText"
) {
return Ok(());
}
editor.dispatch_command(&BEFORE_INPUT_COMMAND, event.clone())?;
Ok(())
}
pub fn on_composition_start(
event: &Rc<CompositionEvent>,
editor: &Editor,
) -> Result<(), LexicalError> {
editor.dispatch_command(&COMPOSITION_START_COMMAND, event.clone())?;
Ok(())
}
pub fn on_composition_end(
event: &Rc<CompositionEvent>,
editor: &Editor,
) -> Result<(), LexicalError> {
editor.dispatch_command(&COMPOSITION_END_COMMAND, event.clone())?;
Ok(())
}
pub fn on_selection_change(
editor: &Editor,
points: Option<(crate::Point, crate::Point)>,
is_active: bool,
) -> Result<(), LexicalError> {
crate::updates::update_editor_sync(
editor,
|| {
let Some((anchor, focus)) = points.filter(|_| is_active) else {
set_selection(None);
return Ok(());
};
let (format, style) = match editor
.get_editor_state()
.selection()
.and_then(|s| s.range())
{
Some(last) => (last.format, last.style),
None => (0.0, String::new()),
};
let selection = SelectionRef::new(crate::lexical_selection::Selection::Range(
crate::RangeSelection::new(anchor, focus, format, style),
));
set_selection(Some(selection.clone()));
selection.set_dirty(false);
let range = selection.range().expect("a range selection");
if range.is_collapsed() {
update_collapsed_selection_format_style(editor, &selection, &range)?;
} else {
update_range_selection_format(&selection, &range)?;
}
let force = selection.is_dirty();
crate::updates::dispatch_selection_change_command(editor, Some(&selection), force)
},
None,
)
}
fn update_selection_format_style(selection: &SelectionRef, format: f64, style: &str) {
selection.with_range(|r| {
if r.format != format || r.style != style {
r.format = format;
r.style = style.to_owned();
r.dirty = true;
}
});
}
fn update_collapsed_selection_format_style(
editor: &Editor,
selection: &SelectionRef,
range: &crate::RangeSelection,
) -> Result<(), LexicalError> {
let anchor_node = range.anchor.get_node()?;
if range.anchor.point_type == crate::PointType::Text {
if !anchor_node.is_text() {
return Err(LexicalError::new(
"Point.getNode() must return TextNode when type is text",
));
}
let data = anchor_node.text_data();
update_selection_format_style(selection, data.get_format(), data.get_style());
} else if editor.is_composing() || !crate::get_root().get_text_content().is_empty() {
if !anchor_node.is_element() {
return Err(LexicalError::new(
"Point.getNode() must return ElementNode when type is element",
));
}
if anchor_node.is_empty() {
crate::selection_editing::internal_refresh_selection_format_and_style(selection, "")?;
} else {
update_selection_format_style(selection, range.format, "");
}
}
Ok(())
}
fn update_range_selection_format(
selection: &SelectionRef,
range: &crate::RangeSelection,
) -> Result<(), LexicalError> {
let nodes = selection.get_nodes()?;
let (start, end) = if selection.is_backward()? {
(&range.focus, &range.anchor)
} else {
(&range.anchor, &range.focus)
};
let mut combined = crate::constants::IS_ALL_FORMATTING;
let mut has_text_nodes = false;
let last = nodes.len().wrapping_sub(1);
for (i, node) in nodes.iter().enumerate() {
let size = node.get_text_content_size();
let key = node.get_key();
if node.is_text()
&& size != 0
&& !((i == 0 && key == start.key && start.offset == size)
|| (i == last && key == end.key && end.offset == 0))
{
has_text_nodes = true;
combined &= crate::nodes::text::to_int32(node.text_data().get_format());
if combined == 0 {
break;
}
}
}
let format = if has_text_nodes { combined as f64 } else { 0.0 };
selection.with_range(|r| r.format = format);
Ok(())
}
type OnMatch = Rc<dyn Fn(&Rc<KeyboardEvent>, &Editor) -> Result<(), LexicalError>>;
#[derive(Clone)]
pub(crate) struct KeyDownShortcut {
key: &'static str,
modifiers: KeyboardEventModifierMask,
on_match: OnMatch,
}
impl KeyboardShortcutMatch for KeyDownShortcut {
fn key(&self) -> &str {
self.key
}
fn modifiers(&self) -> KeyboardEventModifierMask {
self.modifiers
}
}
const ANY: ModifierExpectation = ModifierExpectation::Any;
const PRESSED: ModifierExpectation = ModifierExpectation::Pressed;
const RELEASED: ModifierExpectation = ModifierExpectation::Released;
const ANY_MODIFIERS: KeyboardEventModifierMask = KeyboardEventModifierMask {
alt_key: ANY,
ctrl_key: ANY,
meta_key: ANY,
shift_key: ANY,
control_or_other: None,
};
const NO_MODIFIERS: KeyboardEventModifierMask = KeyboardEventModifierMask {
alt_key: RELEASED,
ctrl_key: RELEASED,
meta_key: RELEASED,
shift_key: RELEASED,
control_or_other: None,
};
const CTRL_KEY: KeyboardEventModifierMask = KeyboardEventModifierMask {
ctrl_key: PRESSED,
..NO_MODIFIERS
};
const META_KEY: KeyboardEventModifierMask = KeyboardEventModifierMask {
meta_key: PRESSED,
..NO_MODIFIERS
};
const SHIFT_KEY_ANY: KeyboardEventModifierMask = KeyboardEventModifierMask {
shift_key: ANY,
..NO_MODIFIERS
};
const ALT_SHIFT_KEY_ANY: KeyboardEventModifierMask = KeyboardEventModifierMask {
alt_key: ANY,
shift_key: ANY,
..NO_MODIFIERS
};
pub(crate) fn build_key_down_shortcuts() -> Vec<KeyDownShortcut> {
fn shortcut(
key: &'static str,
modifiers: KeyboardEventModifierMask,
on_match: impl Fn(&Rc<KeyboardEvent>, &Editor) -> Result<(), LexicalError> + 'static,
) -> KeyDownShortcut {
KeyDownShortcut {
key,
modifiers,
on_match: Rc::new(on_match),
}
}
fn dispatch(
key: &'static str,
modifiers: KeyboardEventModifierMask,
command: LexicalCommand<Rc<KeyboardEvent>>,
) -> KeyDownShortcut {
shortcut(key, modifiers, move |event, editor| {
editor.dispatch_command(&command, event.clone())?;
Ok(())
})
}
fn prevent<T: Clone + 'static>(
key: &'static str,
modifiers: KeyboardEventModifierMask,
command: LexicalCommand<T>,
payload: T,
) -> KeyDownShortcut {
shortcut(key, modifiers, move |event, editor| {
event.prevent_default();
editor.dispatch_command(&command, payload.clone())?;
Ok(())
})
}
fn enter(modifiers: KeyboardEventModifierMask, is_insert_line_break: bool) -> KeyDownShortcut {
shortcut("Enter", modifiers, move |event, editor| {
editor.0.input_state.borrow_mut().is_insert_line_break = is_insert_line_break;
editor.dispatch_command(&KEY_ENTER_COMMAND, Some(event.clone()))?;
if event.default_prevented() {
editor.0.input_state.borrow_mut().is_insert_line_break = false;
}
Ok(())
})
}
fn copy_or_cut(
key: &'static str,
command: LexicalCommand<Option<ClipboardOrKeyboardEvent>>,
) -> KeyDownShortcut {
shortcut(key, control_or_meta(), move |event, editor| {
let prev_selection = editor.get_editor_state().selection();
if prev_selection.is_some_and(|s| !s.is_range()) {
event.prevent_default();
editor.dispatch_command(
&command,
Some(ClipboardOrKeyboardEvent::Keyboard(event.clone())),
)?;
}
Ok(())
})
}
let mut shortcuts = vec![
dispatch("ArrowRight", SHIFT_KEY_ANY, KEY_ARROW_RIGHT_COMMAND),
dispatch("ArrowLeft", SHIFT_KEY_ANY, KEY_ARROW_LEFT_COMMAND),
dispatch("ArrowUp", ALT_SHIFT_KEY_ANY, KEY_ARROW_UP_COMMAND),
dispatch("ArrowDown", ALT_SHIFT_KEY_ANY, KEY_ARROW_DOWN_COMMAND),
enter(
KeyboardEventModifierMask {
shift_key: PRESSED,
..ANY_MODIFIERS
},
true,
),
enter(
KeyboardEventModifierMask {
shift_key: RELEASED,
..ANY_MODIFIERS
},
false,
),
dispatch(" ", ANY_MODIFIERS, KEY_SPACE_COMMAND),
dispatch("Backspace", SHIFT_KEY_ANY, KEY_BACKSPACE_COMMAND),
dispatch("Escape", ANY_MODIFIERS, KEY_ESCAPE_COMMAND),
dispatch("Delete", NO_MODIFIERS, KEY_DELETE_COMMAND),
prevent("Backspace", control_or_alt(), DELETE_WORD_COMMAND, true),
prevent("Delete", control_or_alt(), DELETE_WORD_COMMAND, false),
prevent(
"b",
control_or_meta(),
FORMAT_TEXT_COMMAND,
"bold".to_owned(),
),
prevent(
"u",
control_or_meta(),
FORMAT_TEXT_COMMAND,
"underline".to_owned(),
),
prevent(
"i",
control_or_meta(),
FORMAT_TEXT_COMMAND,
"italic".to_owned(),
),
dispatch("Tab", SHIFT_KEY_ANY, KEY_TAB_COMMAND),
prevent("z", control_or_meta(), UNDO_COMMAND, ()),
prevent(
"z",
KeyboardEventModifierMask {
shift_key: PRESSED,
..control_or_meta()
},
REDO_COMMAND,
(),
),
];
if is_apple() {
shortcuts.extend([
prevent("o", CTRL_KEY, INSERT_LINE_BREAK_COMMAND, true),
dispatch(
"ArrowLeft",
KeyboardEventModifierMask {
meta_key: PRESSED,
..SHIFT_KEY_ANY
},
MOVE_TO_START,
),
dispatch(
"ArrowRight",
KeyboardEventModifierMask {
meta_key: PRESSED,
..SHIFT_KEY_ANY
},
MOVE_TO_END,
),
prevent("h", CTRL_KEY, DELETE_CHARACTER_COMMAND, true),
prevent("d", CTRL_KEY, DELETE_CHARACTER_COMMAND, false),
prevent("Backspace", META_KEY, DELETE_LINE_COMMAND, true),
prevent("Delete", META_KEY, DELETE_LINE_COMMAND, false),
prevent("k", CTRL_KEY, DELETE_LINE_COMMAND, false),
]);
} else {
shortcuts.extend([
dispatch("Home", SHIFT_KEY_ANY, MOVE_TO_START),
dispatch("End", SHIFT_KEY_ANY, MOVE_TO_END),
prevent("y", CTRL_KEY, REDO_COMMAND, ()),
]);
}
shortcuts.extend([
shortcut("a", control_or_meta(), |event, editor| {
event.prevent_default();
editor.dispatch_command(&SELECT_ALL_COMMAND, event.clone())?;
Ok(())
}),
copy_or_cut("c", COPY_COMMAND),
copy_or_cut("x", CUT_COMMAND),
]);
shortcuts
}
pub(crate) type KeyDownShortcuts = (bool, Rc<CompiledKeyboardShortcuts<KeyDownShortcut>>);
pub(crate) fn handle_key_down(
event: &Rc<KeyboardEvent>,
editor: &Editor,
) -> Result<bool, LexicalError> {
let is_apple = is_apple();
let cached = editor
.0
.key_down_shortcuts
.borrow()
.as_ref()
.filter(|(apple, _)| *apple == is_apple)
.map(|(_, shortcuts)| shortcuts.clone());
let compiled = match cached {
Some(compiled) => compiled,
None => {
let compiled = Rc::new(compile_keyboard_shortcuts(build_key_down_shortcuts())?);
*editor.0.key_down_shortcuts.borrow_mut() = Some((is_apple, compiled.clone()));
compiled
}
};
if let Some(shortcut) = compiled.r#match(&**event) {
(shortcut.on_match)(event, editor)?;
}
if is_modifier(&**event) {
editor.dispatch_command(&KEY_MODIFIER_COMMAND, event.clone())?;
}
Ok(true)
}
fn is_token_or_tab(node: &NodeRef) -> bool {
node.get_type() == "tab" || node.is_token()
}
fn maybe_move_selection_past_trailing_acceptance_boundary(
editor: &Editor,
inserted_text: Option<&str>,
) -> Result<(), LexicalError> {
let last_key_code = editor.0.input_state.borrow().last_key_code.clone();
let (Some(inserted_text), Some(last_key_code)) = (inserted_text, last_key_code) else {
return Ok(());
};
if utf16::len(inserted_text) <= 1 {
return Ok(());
}
let character_to_search_for: u16 = if utf16::len(&last_key_code) == 1 {
last_key_code.encode_utf16().next().unwrap_or(0)
} else if last_key_code == "Enter" {
u16::from(b'\n')
} else if last_key_code == "Tab" {
u16::from(b'\t')
} else {
return Ok(());
};
let Some(selection) = get_selection().filter(SelectionRef::is_range) else {
return Ok(());
};
let Some(range) = selection.range() else {
return Ok(());
};
if !range.is_collapsed() {
return Ok(());
}
let anchor_node = range.anchor.get_node()?;
if !anchor_node.is_text() {
return Ok(());
}
let offset = range.anchor.offset;
let text: Vec<u16> = anchor_node.get_text_content().encode_utf16().collect();
if text.len() == offset {
let next_sibling = anchor_node.get_next_sibling();
if character_to_search_for == u16::from(b'\n') {
match next_sibling {
Some(line_break) if line_break.get_type() == "linebreak" => {
line_break.select_end()?;
}
None => {
let block =
find_matching_parent(&anchor_node, |n| n.is_element() && !n.is_inline());
if let Some(next_block) = block.and_then(|b| b.get_next_sibling())
&& next_block.is_element()
{
next_block.select_start()?;
}
}
Some(_) => {}
}
} else if character_to_search_for == u16::from(b'\t') {
if let Some(tab) = next_sibling.filter(|n| n.get_type() == "tab") {
tab.select_end()?;
}
} else if let Some(next) = next_sibling.filter(NodeRef::is_text)
&& next.get_text_content().encode_utf16().next() == Some(character_to_search_for)
{
next.select(Some(1), Some(1))?;
}
} else if text.get(offset) == Some(&character_to_search_for) {
anchor_node.select(Some(offset + 1), Some(offset + 1))?;
}
Ok(())
}
fn can_remove_text(anchor_node: &NodeRef, focus_node: &NodeRef) -> bool {
anchor_node.get_key() != focus_node.get_key()
|| anchor_node.is_element()
|| focus_node.is_element()
|| !is_token_or_tab(anchor_node)
|| !is_token_or_tab(focus_node)
}
pub(crate) fn handle_before_input(
event: &Rc<InputEvent>,
editor: &Editor,
) -> Result<bool, LexicalError> {
let input_type = event.input_type.as_str();
let selection = get_selection();
if input_type == "deleteContentBackward" {
match &selection {
None => {
let Some(prev_selection) = get_previous_selection().filter(SelectionRef::is_range)
else {
return Ok(true);
};
set_selection(Some(prev_selection.clone_selection()));
}
Some(selection) if selection.is_range() => {
set_composition_key(None);
event.prevent_default();
editor.dispatch_command(&DELETE_CHARACTER_COMMAND, true)?;
return Ok(true);
}
Some(_) => {}
}
}
let Some(selection) = selection.filter(SelectionRef::is_range) else {
if input_type == "historyUndo" || input_type == "historyRedo" {
event.prevent_default();
let command = if input_type == "historyUndo" {
UNDO_COMMAND
} else {
REDO_COMMAND
};
editor.dispatch_command(&command, ())?;
}
return Ok(true);
};
let data = event.data.as_deref();
if input_type == "insertText" || input_type == "insertTranspose" {
if data == Some("\n") {
event.prevent_default();
editor.dispatch_command(&INSERT_LINE_BREAK_COMMAND, false)?;
} else if data == Some(DOUBLE_LINE_BREAK) {
event.prevent_default();
editor.dispatch_command(&INSERT_PARAGRAPH_COMMAND, ())?;
} else if data.is_none()
&& let Some(data_transfer) = &event.data_transfer
{
let text = data_transfer.get_data("text/plain");
event.prevent_default();
selection.insert_raw_text(&text)?;
} else if let Some(data) = data {
event.prevent_default();
editor.dispatch_command(&CONTROLLED_TEXT_INSERTION_COMMAND, data.to_owned())?;
maybe_move_selection_past_trailing_acceptance_boundary(editor, Some(data))?;
}
return Ok(true);
}
event.prevent_default();
let range = selection.range().expect("a range selection");
let anchor_node = range.anchor.get_node()?;
let focus_node = range.focus.get_node()?;
match input_type {
"insertFromYank" | "insertFromDrop" | "insertReplacementText" => {
let text = event
.data_transfer
.as_ref()
.map(|d| d.get_data("text/plain"));
let inserted = text.as_deref().or(data);
editor.dispatch_command(
&CONTROLLED_TEXT_INSERTION_COMMAND,
inserted.unwrap_or("").to_owned(),
)?;
maybe_move_selection_past_trailing_acceptance_boundary(editor, inserted)?;
}
"insertFromComposition" => {
let prev_composition_key = editor.composition_key();
set_composition_key(None);
editor.dispatch_command(
&CONTROLLED_TEXT_INSERTION_COMMAND,
data.unwrap_or("").to_owned(),
)?;
cleanup_composed_subclass(editor, prev_composition_key.as_deref())?;
}
"insertLineBreak" => {
set_composition_key(None);
editor.0.input_state.borrow_mut().is_insert_line_break = false;
editor.dispatch_command(&INSERT_LINE_BREAK_COMMAND, false)?;
}
"insertParagraph" => {
set_composition_key(None);
let is_insert_line_break =
std::mem::take(&mut editor.0.input_state.borrow_mut().is_insert_line_break);
if is_insert_line_break {
editor.dispatch_command(&INSERT_LINE_BREAK_COMMAND, false)?;
} else {
editor.dispatch_command(&INSERT_PARAGRAPH_COMMAND, ())?;
}
}
"insertFromPaste" | "insertFromPasteAsQuotation" => {
editor.dispatch_command(&PASTE_COMMAND, PasteCommandType::Input(event.clone()))?;
}
"deleteByComposition" => {
if can_remove_text(&anchor_node, &focus_node) {
editor.dispatch_command(&REMOVE_TEXT_COMMAND, ())?;
}
}
"deleteByDrag" => {
editor.add_tags(&[SKIP_SELECTION_FOCUS_TAG.to_owned()]);
editor.dispatch_command(&REMOVE_TEXT_COMMAND, ())?;
}
"deleteByCut" => {
editor.dispatch_command(&REMOVE_TEXT_COMMAND, ())?;
}
"deleteContent" => {
editor.dispatch_command(&DELETE_CHARACTER_COMMAND, false)?;
}
"deleteWordBackward" => {
editor.dispatch_command(&DELETE_WORD_COMMAND, true)?;
}
"deleteWordForward" => {
editor.dispatch_command(&DELETE_WORD_COMMAND, false)?;
}
"deleteHardLineBackward" | "deleteSoftLineBackward" => {
editor.dispatch_command(&DELETE_LINE_COMMAND, true)?;
}
"deleteContentForward" | "deleteHardLineForward" | "deleteSoftLineForward" => {
editor.dispatch_command(&DELETE_LINE_COMMAND, false)?;
}
"formatStrikeThrough" => {
editor.dispatch_command(&FORMAT_TEXT_COMMAND, "strikethrough".to_owned())?;
}
"formatBold" => {
editor.dispatch_command(&FORMAT_TEXT_COMMAND, "bold".to_owned())?;
}
"formatItalic" => {
editor.dispatch_command(&FORMAT_TEXT_COMMAND, "italic".to_owned())?;
}
"formatUnderline" => {
editor.dispatch_command(&FORMAT_TEXT_COMMAND, "underline".to_owned())?;
}
"historyUndo" => {
editor.dispatch_command(&UNDO_COMMAND, ())?;
}
"historyRedo" => {
editor.dispatch_command(&REDO_COMMAND, ())?;
}
_ => {}
}
Ok(true)
}
fn handle_composition_start(
_event: &Rc<CompositionEvent>,
editor: &Editor,
) -> Result<bool, LexicalError> {
if let Some(range) = get_selection().and_then(|s| s.range())
&& !editor.is_composing()
{
set_composition_key(Some(&range.anchor.key));
editor.add_tags(&[COMPOSITION_START_TAG.to_owned()]);
}
Ok(true)
}
fn cleanup_composed_subclass(
editor: &Editor,
composition_key: Option<&str>,
) -> Result<(), LexicalError> {
let composed_segmented_key = editor
.0
.input_state
.borrow_mut()
.composed_segmented_key
.take();
let Some(composition_key) = composition_key else {
return Ok(());
};
if composed_segmented_key.as_deref() != Some(composition_key) {
return Ok(());
}
let Some(composed_node) = get_node_by_key(composition_key) else {
return Ok(());
};
if !composed_node.is_text()
|| composed_node.get_type() == "text"
|| is_token_or_segmented(&composed_node)
|| !composed_node.is_attached()
{
return Ok(());
}
let offset = get_selection()
.and_then(|s| s.range())
.filter(|r| r.anchor.key == composition_key)
.map(|r| r.anchor.offset);
let replacement = create_text_node(&composed_node.get_text_content());
replacement.set_format(composed_node.text_data().get_format());
replacement.set_style(composed_node.text_data().get_style());
composed_node.replace(&replacement, false)?;
if let Some(offset) = offset {
let safe_offset = offset.min(replacement.get_text_content_size());
replacement.select(Some(safe_offset), Some(safe_offset))?;
}
Ok(())
}
fn on_composition_end_impl(editor: &Editor, data: Option<&str>) -> Result<bool, LexicalError> {
let composition_key = editor.composition_key();
set_composition_key(None);
if let (Some(key), Some(data)) = (composition_key.as_deref(), data) {
if data.is_empty() {
cleanup_composed_subclass(editor, Some(key))?;
return Ok(false);
}
if data.ends_with('\n')
&& let Some(selection) = get_selection()
{
if let Some(range) = selection.range() {
let focus = range.focus;
selection.point_set(Which::Anchor, &focus.key, focus.offset, focus.point_type)?;
}
editor.dispatch_command(&KEY_ENTER_COMMAND, None)?;
cleanup_composed_subclass(editor, Some(key))?;
return Ok(false);
}
if let Some(node) = get_node_by_key(key).filter(NodeRef::is_text)
&& is_token_or_segmented(&node)
{
node.mark_dirty();
let text_len = node.get_text_content_size();
let offset = get_selection()
.and_then(|s| s.range())
.filter(|r| r.anchor.key == key)
.map_or(text_len, |r| r.anchor.offset);
node.select(Some(offset), Some(offset))?.insert_text(data)?;
return Ok(true);
}
}
cleanup_composed_subclass(editor, composition_key.as_deref())?;
Ok(false)
}
fn handle_composition_end(
event: &Rc<CompositionEvent>,
editor: &Editor,
) -> Result<bool, LexicalError> {
on_composition_end_impl(editor, Some(&event.data))?;
editor.add_tags(&[COMPOSITION_END_TAG.to_owned()]);
Ok(true)
}
pub(crate) fn register_default_command_handlers(editor: &Editor) -> Unregister {
let unregisters = [
editor.register_command(
&BEFORE_INPUT_COMMAND,
handle_before_input,
COMMAND_PRIORITY_EDITOR,
),
editor.register_command(
&COMPOSITION_START_COMMAND,
handle_composition_start,
COMMAND_PRIORITY_EDITOR,
),
editor.register_command(
&COMPOSITION_END_COMMAND,
handle_composition_end,
COMMAND_PRIORITY_EDITOR,
),
editor.register_command(&KEY_DOWN_COMMAND, handle_key_down, COMMAND_PRIORITY_EDITOR),
];
Box::new(move || {
for unregister in unregisters {
unregister();
}
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn data_by_format_in_order() {
let data_transfer = DataTransfer::new();
data_transfer.set_data("text/html", "");
data_transfer.set_data("Text", "plain");
data_transfer.set_data("text/html", "<b>x</b>");
assert_eq!(data_transfer.types(), ["text/html", "text/plain"]);
assert_eq!(data_transfer.get_data("text/plain"), "plain");
assert_eq!(data_transfer.get_data("TEXT/HTML"), "<b>x</b>");
assert_eq!(data_transfer.get_data("URL"), "");
data_transfer.set_files(vec![DataTransferFile::new(
"a.txt",
"text/plain",
b"a".to_vec(),
)]);
assert_eq!(data_transfer.types(), ["text/html", "text/plain", "Files"]);
assert_eq!(data_transfer.files()[0].bytes().unwrap(), b"a");
data_transfer.clear_data(Some("text"));
assert_eq!(data_transfer.types(), ["text/html", "Files"]);
data_transfer.clear_data(None);
assert_eq!(data_transfer.types(), ["Files"]);
assert_eq!(data_transfer.drop_effect(), "none");
assert_eq!(data_transfer.effect_allowed(), "uninitialized");
}
struct Root;
impl crate::RootElement for Root {}
#[test]
fn selection_change_sets_points_format_and_notifies() {
use crate::lexical_selection::Selection;
const STATE: &str = r#"{"root":{"children":[{"children":[{"detail":0,"format":1,"mode":"normal","style":"color: red","text":"bold","type":"text","version":1},{"detail":0,"format":0,"mode":"normal","style":"","text":"plain","type":"text","version":1}],"direction":null,"format":"","indent":0,"textFormat":0,"textStyle":"","type":"paragraph","version":1}],"direction":null,"format":"","indent":0,"type":"root","version":1}}"#;
let editor = crate::create_editor(crate::CreateEditorArgs::default());
editor
.set_root_element(Some(crate::RootElementRef(Rc::new(Root))))
.unwrap();
let state = editor.parse_editor_state(STATE).unwrap();
editor.set_editor_state(&state, None).unwrap();
let notified = Rc::new(Cell::new(0));
let n = notified.clone();
let _unregister = editor.register_command(
&crate::commands::SELECTION_CHANGE_COMMAND,
move |_, _| {
n.set(n.get() + 1);
Ok(false)
},
COMMAND_PRIORITY_EDITOR,
);
let (bold, plain) = editor
.read(|| {
let p = crate::get_root().get_first_child().unwrap();
let bold = p.get_first_child().unwrap();
let plain = bold.get_next_sibling().unwrap();
(bold.get_key().to_owned(), plain.get_key().to_owned())
})
.unwrap();
let text = |key: &str, offset| crate::Point::new(key, offset, crate::PointType::Text);
let range = |editor: &Editor| {
editor
.read(|| match get_selection().map(|s| s.get()) {
Some(Selection::Range(r)) => r,
_ => panic!("no range selection"),
})
.unwrap()
};
on_selection_change(&editor, Some((text(&bold, 2), text(&bold, 2))), true).unwrap();
let caret = range(&editor);
assert_eq!((caret.format, caret.style.as_str()), (1.0, "color: red"));
assert_eq!(caret.anchor, text(&bold, 2));
assert_eq!(notified.get(), 1);
on_selection_change(&editor, Some((text(&bold, 2), text(&bold, 2))), true).unwrap();
assert_eq!(notified.get(), 1);
on_selection_change(&editor, Some((text(&bold, 1), text(&plain, 2))), true).unwrap();
let mixed = range(&editor);
assert_eq!((mixed.format, mixed.style.as_str()), (0.0, "color: red"));
assert_eq!(notified.get(), 2);
on_selection_change(&editor, Some((text(&plain, 0), text(&bold, 1))), true).unwrap();
assert_eq!(range(&editor).format, 1.0);
assert_eq!(notified.get(), 3);
on_selection_change(&editor, Some((text(&bold, 1), text(&bold, 1))), false).unwrap();
assert!(editor.read(get_selection).unwrap().is_none());
}
}