use super::compile_import_rules::CompiledOverlayRules;
use super::dom_import_extension::{DomImportConfig, DomImportExtensionOutput};
use crate::dom::DomNode;
use crate::events::DataTransfer;
use crate::extension::get_peer_dependency_from_editor;
use crate::html::internal::constants::DOM_IMPORT_EXTENSION_NAME;
use crate::html::internal::context_record::{
ContextConfig, ContextKind, ContextPairOrUpdater, ContextRecord, create_context_state,
get_context_record, get_context_value, with_context,
};
use crate::lexical_dom::{is_block_dom_node, is_inline_dom_node};
use crate::set_dom_style::StyleObject;
use crate::{Editor, LexicalError};
use std::rc::Rc;
pub type ImportStateConfig<V> = ContextConfig<V>;
pub type ImportContextPairOrUpdater = ContextPairOrUpdater;
pub fn create_import_state<V: Clone + PartialEq + 'static>(
name: &str,
get_default_value: impl FnOnce() -> V,
) -> ImportStateConfig<V> {
create_context_state(ContextKind::Import, name, get_default_value)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ImportSourceKind {
Paste,
Unknown,
}
pub type DomNodePredicate = Rc<dyn Fn(&DomNode) -> bool>;
#[derive(Clone)]
pub struct WhitespaceImportConfig {
pub preserves_whitespace: DomNodePredicate,
pub is_inline: DomNodePredicate,
}
impl PartialEq for WhitespaceImportConfig {
fn eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.preserves_whitespace, &other.preserves_whitespace)
&& Rc::ptr_eq(&self.is_inline, &other.is_inline)
}
}
pub fn default_preserves_whitespace(node: &DomNode) -> bool {
if !node.is_element() {
return false;
}
if node.node_name() == "PRE" {
return true;
}
node.style()
.get_property_value("white-space")
.starts_with("pre")
}
pub fn default_is_inline(node: &DomNode) -> bool {
if node.is_text() {
return true;
}
if !node.is_element() {
return false;
}
let display = node.style().get_property_value("display");
if !display.is_empty() {
return display.starts_with("inline");
}
if is_block_dom_node(node) {
return false;
}
is_inline_dom_node(node)
}
struct States {
source: ImportStateConfig<ImportSourceKind>,
source_data_transfer: ImportStateConfig<Option<SharedDataTransfer>>,
text_format: ImportStateConfig<i32>,
text_style: ImportStateConfig<StyleObject>,
whitespace: ImportStateConfig<WhitespaceImportConfig>,
overlays: ImportStateConfig<Vec<Rc<CompiledOverlayRules>>>,
}
thread_local! {
static STATES: States = States {
source: create_import_state("importSource", || ImportSourceKind::Unknown),
source_data_transfer: create_import_state("importSourceDataTransfer", || None),
text_format: create_import_state("textFormat", || 0),
text_style: create_import_state("textStyle", StyleObject::new),
whitespace: create_import_state("whitespaceConfig", || WhitespaceImportConfig {
is_inline: Rc::new(default_is_inline),
preserves_whitespace: Rc::new(default_preserves_whitespace),
}),
overlays: create_import_state("importOverlays", Vec::new),
};
}
pub fn import_source() -> ImportStateConfig<ImportSourceKind> {
STATES.with(|s| s.source.clone())
}
#[derive(Clone, Debug)]
pub struct SharedDataTransfer(pub Rc<DataTransfer>);
impl PartialEq for SharedDataTransfer {
fn eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.0, &other.0)
}
}
pub fn import_source_data_transfer() -> ImportStateConfig<Option<SharedDataTransfer>> {
STATES.with(|s| s.source_data_transfer.clone())
}
pub fn import_text_format() -> ImportStateConfig<i32> {
STATES.with(|s| s.text_format.clone())
}
pub fn import_text_style() -> ImportStateConfig<StyleObject> {
STATES.with(|s| s.text_style.clone())
}
pub fn import_whitespace_config() -> ImportStateConfig<WhitespaceImportConfig> {
STATES.with(|s| s.whitespace.clone())
}
pub fn import_overlays() -> ImportStateConfig<Vec<Rc<CompiledOverlayRules>>> {
STATES.with(|s| s.overlays.clone())
}
#[derive(Clone)]
pub struct ImportSession {
pub(crate) record: Rc<ContextRecord>,
}
impl ImportSession {
pub fn get<V: Clone + PartialEq + 'static>(&self, cfg: &ImportStateConfig<V>) -> V {
get_context_value(Some(&self.record), cfg)
}
pub fn set<V: Clone + PartialEq + 'static>(&self, cfg: &ImportStateConfig<V>, value: V) {
self.record.set(cfg, value);
}
pub fn update<V: Clone + PartialEq + 'static>(
&self,
cfg: &ImportStateConfig<V>,
updater: impl FnOnce(V) -> V,
) {
let value = updater(self.get(cfg));
self.record.set(cfg, value);
}
pub fn has<V: Clone + PartialEq + 'static>(&self, cfg: &ImportStateConfig<V>) -> bool {
self.record.has_own(cfg)
}
}
fn get_default_import_context(editor: &Editor) -> Option<Rc<ContextRecord>> {
get_peer_dependency_from_editor::<DomImportConfig, DomImportExtensionOutput, ()>(
editor,
DOM_IMPORT_EXTENSION_NAME,
)
.ok()
.flatten()
.and_then(|dep| dep.output)
.and_then(|output| output.defaults.clone())
}
pub fn get_import_context_value<V: Clone + PartialEq + 'static>(
cfg: &ImportStateConfig<V>,
editor: &Editor,
) -> V {
let record = get_context_record(ContextKind::Import, editor)
.or_else(|| get_default_import_context(editor));
get_context_value(record.as_ref(), cfg)
}
pub fn with_import_context<T>(
cfg: &[ImportContextPairOrUpdater],
editor: &Editor,
f: impl FnOnce() -> Result<T, LexicalError>,
) -> Result<T, LexicalError> {
with_context(
ContextKind::Import,
get_default_import_context,
cfg,
editor,
f,
)
}