use crate::clipboard::{handle_plain_text_drop, write_drag_source_to_data_transfer};
use crate::commands::{
CONTROLLED_TEXT_INSERTION_COMMAND, DELETE_CHARACTER_COMMAND, DELETE_LINE_COMMAND,
DELETE_WORD_COMMAND, INSERT_LINE_BREAK_COMMAND, INSERT_PARAGRAPH_COMMAND, KEY_ENTER_COMMAND,
REMOVE_TEXT_COMMAND, SELECT_ALL_COMMAND,
};
use crate::commands::{COPY_COMMAND, CUT_COMMAND, PASTE_COMMAND};
use crate::commands::{
DRAGSTART_COMMAND, DROP_COMMAND, KEY_ARROW_LEFT_COMMAND, KEY_ARROW_RIGHT_COMMAND,
KEY_BACKSPACE_COMMAND, KEY_DELETE_COMMAND,
};
use crate::editor::COMMAND_PRIORITY_EDITOR;
use crate::events::KeyboardEvent;
use crate::events::{ClipboardOrKeyboardEvent, PasteCommandType};
use crate::lexical_selection::{SelectionRef, get_selection};
use crate::selection::{move_character, should_override_default_character_selection};
use crate::selection_insert_nodes::select_all;
use crate::{CUT_TAG, PASTE_TAG, UpdateOptions};
use crate::{Editor, LexicalError, Unregister};
use std::rc::Rc;
fn on_copy_for_plain_text(event: Option<&ClipboardOrKeyboardEvent>) -> Result<(), LexicalError> {
let Some(editor) = crate::updates::get_active_editor() else {
return Ok(());
};
let event = event.cloned();
#[cfg(feature = "html")]
let html_editor = editor.clone();
editor.update(
move || {
let Some(event) = event else {
return Ok(());
};
let clipboard_data = ClipboardOrKeyboardEvent::clipboard_event(Some(&event))
.and_then(|e| e.clipboard_data.clone());
let selection = get_selection();
if let (Some(selection), Some(clipboard_data)) = (selection, clipboard_data)
&& !selection.range().is_some_and(|r| r.is_collapsed())
{
event.event().prevent_default();
#[cfg(feature = "html")]
clipboard_data.set_data(
"text/html",
&crate::clipboard::get_html_content(&html_editor, Some(&selection))?,
);
clipboard_data.set_data("text/plain", &selection.get_text_content()?);
}
Ok(())
},
UpdateOptions::default(),
)
}
fn on_paste_for_plain_text(event: &PasteCommandType) -> Result<(), LexicalError> {
let Some(editor) = crate::updates::get_active_editor() else {
return Ok(());
};
event.event().prevent_default();
let clipboard_data = match event {
PasteCommandType::Clipboard(event) => event.clipboard_data.clone(),
PasteCommandType::Input(_) | PasteCommandType::Keyboard(_) => None,
};
editor.update(
move || {
if let (Some(clipboard_data), Some(selection)) = (clipboard_data, range_selection()) {
crate::clipboard::insert_data_transfer_for_plain_text(&clipboard_data, &selection)?;
}
Ok(())
},
UpdateOptions {
tags: vec![PASTE_TAG.to_owned()],
..UpdateOptions::default()
},
)
}
fn on_cut_for_plain_text(
event: Option<&ClipboardOrKeyboardEvent>,
editor: &Editor,
) -> Result<(), LexicalError> {
on_copy_for_plain_text(event)?;
editor.update(
|| {
if let Some(selection) = range_selection() {
selection.remove_text()?;
}
Ok(())
},
UpdateOptions {
tags: vec![CUT_TAG.to_owned()],
..UpdateOptions::default()
},
)
}
fn range_selection() -> Option<SelectionRef> {
get_selection().filter(SelectionRef::is_range)
}
fn on_range<P: 'static>(
editor: &Editor,
command: &crate::LexicalCommand<P>,
listener: impl Fn(&SelectionRef, &P) -> Result<(), LexicalError> + 'static,
) -> Unregister {
editor.register_command(
command,
move |payload, _| {
let Some(selection) = range_selection() else {
return Ok(false);
};
listener(&selection, payload)?;
Ok(true)
},
COMMAND_PRIORITY_EDITOR,
)
}
fn horizontal_arrow(event: &KeyboardEvent, is_backward: bool) -> Result<bool, LexicalError> {
let Some(selection) = range_selection() else {
return Ok(false);
};
let range = selection.range().expect("a range selection");
if should_override_default_character_selection(&range, is_backward)? {
event.prevent_default();
move_character(&selection, event.shift_key, is_backward)?;
return Ok(true);
}
Ok(false)
}
pub fn register_plain_text(editor: &Editor) -> Unregister {
let unregisters: Vec<Unregister> = vec![
on_range(
editor,
&DELETE_CHARACTER_COMMAND,
|selection, &is_backward| selection.delete_character(is_backward),
),
on_range(editor, &DELETE_WORD_COMMAND, |selection, &is_backward| {
selection.delete_word(is_backward)
}),
on_range(editor, &DELETE_LINE_COMMAND, |selection, &is_backward| {
selection.delete_line(is_backward)
}),
on_range(
editor,
&CONTROLLED_TEXT_INSERTION_COMMAND,
|selection, text| selection.insert_text(text),
),
on_range(editor, &REMOVE_TEXT_COMMAND, |selection, _| {
selection.remove_text()
}),
on_range(
editor,
&INSERT_LINE_BREAK_COMMAND,
|selection, &select_start| selection.insert_line_break(select_start),
),
on_range(editor, &INSERT_PARAGRAPH_COMMAND, |selection, _| {
selection.insert_line_break(false)
}),
editor.register_command(
&KEY_ARROW_LEFT_COMMAND,
|event, _| horizontal_arrow(event, true),
COMMAND_PRIORITY_EDITOR,
),
editor.register_command(
&KEY_ARROW_RIGHT_COMMAND,
|event, _| horizontal_arrow(event, false),
COMMAND_PRIORITY_EDITOR,
),
editor.register_command(
&KEY_BACKSPACE_COMMAND,
|event, editor| {
if range_selection().is_none() {
return Ok(false);
}
event.prevent_default();
editor.dispatch_command(&DELETE_CHARACTER_COMMAND, true)
},
COMMAND_PRIORITY_EDITOR,
),
editor.register_command(
&KEY_DELETE_COMMAND,
|event, editor| {
if range_selection().is_none() {
return Ok(false);
}
event.prevent_default();
editor.dispatch_command(&DELETE_CHARACTER_COMMAND, false)
},
COMMAND_PRIORITY_EDITOR,
),
editor.register_command(
&KEY_ENTER_COMMAND,
|event, editor| {
if !get_selection().is_some_and(|s| s.is_range()) {
return Ok(false);
}
if let Some(event) = event {
event.prevent_default();
}
editor.dispatch_command(&INSERT_LINE_BREAK_COMMAND, false)
},
COMMAND_PRIORITY_EDITOR,
),
editor.register_command(
&SELECT_ALL_COMMAND,
|_, _| {
match get_selection().filter(SelectionRef::is_range) {
Some(selection)
if crate::slot::get_slot_frame(
&selection
.point(crate::Which::Anchor)
.expect("a range")
.get_node()?,
)
.is_some() =>
{
crate::selection_insert_nodes::select_all_in(&selection)?;
}
_ => {
select_all()?;
}
}
Ok(true)
},
COMMAND_PRIORITY_EDITOR,
),
on_range(editor, ©_COMMAND, |_, event| {
on_copy_for_plain_text(event.as_ref())
}),
editor.register_command(
&CUT_COMMAND,
|event, editor| {
if range_selection().is_none() {
return Ok(false);
}
on_cut_for_plain_text(event.as_ref(), editor)?;
Ok(true)
},
COMMAND_PRIORITY_EDITOR,
),
on_range(editor, &PASTE_COMMAND, |_, event| {
on_paste_for_plain_text(event)
}),
editor.register_command(
&DROP_COMMAND,
|event, editor| handle_plain_text_drop(event, editor),
COMMAND_PRIORITY_EDITOR,
),
editor.register_command(
&DRAGSTART_COMMAND,
|event, editor| {
let Some(selection) = range_selection() else {
return Ok(false);
};
if !selection.range().is_some_and(|r| r.is_collapsed())
&& let Some(data_transfer) = &event.data_transfer
{
write_drag_source_to_data_transfer(data_transfer, editor);
}
Ok(true)
},
COMMAND_PRIORITY_EDITOR,
),
];
Box::new(move || {
for unregister in unregisters {
unregister();
}
})
}
#[cfg(feature = "extension")]
pub type PlainTextExtensionType = crate::LexicalExtension;
#[cfg(feature = "extension")]
fn make_plain_text_extension() -> Rc<PlainTextExtensionType> {
use crate::extension_core::{ExtensionRegisterState, LexicalExtension, define_extension};
define_extension(LexicalExtension {
conflicts_with: vec!["@lexical/rich-text".to_owned()],
dependencies: vec![
crate::extension::normalize_inline_elements_extension().into(),
crate::extension::normalize_triple_click_selection_extension().into(),
],
register: Some(Rc::new(
|editor: &Editor, _: &(), _: &ExtensionRegisterState<(), ()>| {
Ok(register_plain_text(editor))
},
)),
..LexicalExtension::new("@lexical/plain-text", ())
})
}
#[cfg(feature = "extension")]
thread_local! {
static PLAIN_TEXT_EXTENSION: Rc<PlainTextExtensionType> = make_plain_text_extension();
}
#[cfg(feature = "extension")]
pub fn plain_text_extension() -> Rc<PlainTextExtensionType> {
PLAIN_TEXT_EXTENSION.with(Rc::clone)
}