#![cfg(feature = "extension")]
use crate::caret::{CaretDirection, PointCaret, get_text_point_caret};
use crate::extension::{LexicalEditorWithDispose, build_editor_from_extensions};
use crate::rich_text::{DRAG_DROP_PASTE, rich_text_extension};
use crate::upstream_tests::support::update;
use crate::{
COMMAND_PRIORITY_CRITICAL, DROP_COMMAND, DataTransfer, DataTransferFile, DragEvent,
InitialEditorStateType, LexicalExtension, MouseEvent, RootElement, RootElementRef,
create_paragraph_node, create_text_node, define_extension, get_root,
};
use std::cell::RefCell;
use std::rc::Rc;
fn build(name: &'static str, initial: Option<InitialEditorStateType>) -> LexicalEditorWithDispose {
build_editor_from_extensions([define_extension(LexicalExtension {
initial_editor_state: initial,
dependencies: vec![rich_text_extension().into()],
..LexicalExtension::<(), (), ()>::new(name, ())
})
.into()])
.unwrap()
}
fn fake_image() -> DataTransferFile {
DataTransferFile::new("photo.png", "image/png", b"fake-bytes".to_vec())
}
fn create_drop_event_with_files(files: Vec<DataTransferFile>) -> Rc<DragEvent> {
let data_transfer = DataTransfer::new();
data_transfer.set_files(files);
Rc::new(DragEvent {
mouse: MouseEvent {
client_x: 10.0,
client_y: 10.0,
..MouseEvent::new("drop")
},
data_transfer: Some(Rc::new(data_transfer)),
})
}
fn dispatch_drop_and_capture_files(
editor: &LexicalEditorWithDispose,
event: Rc<DragEvent>,
) -> Option<Vec<String>> {
let dispatched: Rc<RefCell<Option<Vec<String>>>> = Rc::default();
let captured = dispatched.clone();
let dispose = editor.register_command(
&DRAG_DROP_PASTE,
move |files, _| {
*captured.borrow_mut() = Some(files.iter().map(|f| f.name.clone()).collect());
Ok(true)
},
COMMAND_PRIORITY_CRITICAL,
);
editor.dispatch_command(&DROP_COMMAND, event).unwrap();
dispose();
dispatched.take()
}
#[test]
fn unhandled_desktop_text_files_are_still_consumed() {
let editor = build("test-desktop-text-file-drop", None);
let event = create_drop_event_with_files(vec![DataTransferFile::new(
"document.txt",
"text/plain",
b"text".to_vec(),
)]);
assert!(
editor
.dispatch_command(&DROP_COMMAND, event.clone())
.unwrap()
);
assert!(event.default_prevented());
editor.dispose();
}
#[test]
fn dropped_files_are_forwarded_even_when_the_drop_point_resolves_to_no_caret() {
let editor = build("test-drop-no-caret", None);
let files =
dispatch_drop_and_capture_files(&editor, create_drop_event_with_files(vec![fake_image()]));
assert_eq!(files, Some(vec!["photo.png".to_owned()]));
editor.dispose();
}
struct CaretRootElement;
impl RootElement for CaretRootElement {
fn caret_from_point(&self, _x: f64, _y: f64) -> Option<PointCaret> {
let text = get_root().get_first_descendant()?;
get_text_point_caret(&text, CaretDirection::Next, 1).ok()
}
}
#[test]
fn dropped_files_are_forwarded_when_the_drop_point_does_resolve_to_a_caret() {
let editor = build(
"test-drop-with-caret",
Some(InitialEditorStateType::Update(Rc::new(|_| {
let paragraph = create_paragraph_node();
paragraph.append(&[create_text_node("hello")])?;
get_root().append(&[paragraph])
}))),
);
editor
.set_root_element(Some(RootElementRef(Rc::new(CaretRootElement))))
.unwrap();
update(&editor, || Ok(()));
let files =
dispatch_drop_and_capture_files(&editor, create_drop_event_with_files(vec![fake_image()]));
assert_eq!(files, Some(vec!["photo.png".to_owned()]));
editor.set_root_element(None).unwrap();
editor.dispose();
}