use super::context_record::{
ContextConfig, ContextPairOrUpdater, ContextRecord, get_context_value,
};
use crate::dom::DomNode;
use crate::lexical_dom::DomExportOutput;
use crate::lexical_selection::SelectionRef;
use crate::node::{LexicalNode, NodeRef};
use crate::{Editor, LexicalError};
use std::rc::Rc;
pub type RenderStateConfig<V> = ContextConfig<V>;
pub struct RenderContextReader {
pub(crate) record: Option<Rc<ContextRecord>>,
}
impl RenderContextReader {
pub fn get<V: Clone + PartialEq + 'static>(&self, cfg: &RenderStateConfig<V>) -> V {
get_context_value(self.record.as_ref(), cfg)
}
}
pub type NodePredicate = Rc<dyn Fn(&dyn LexicalNode, &NodeRef) -> bool>;
#[derive(Clone)]
pub enum NodeMatch {
Class {
node_type: &'static str,
super_types: Vec<&'static str>,
},
Predicate(NodePredicate),
}
impl NodeMatch {
pub fn class<T: LexicalNode + Default>() -> Self {
let node = T::default();
NodeMatch::Class {
node_type: node.get_type(),
super_types: node.super_types(),
}
}
pub fn predicate(f: impl Fn(&dyn LexicalNode, &NodeRef) -> bool + 'static) -> Self {
NodeMatch::Predicate(Rc::new(f))
}
}
pub(crate) fn is_instance(node: &dyn LexicalNode, node_type: &str) -> bool {
node.get_type() == node_type || node.super_types().contains(&node_type)
}
pub type RenderGate = Rc<dyn Fn(&RenderContextReader) -> bool>;
#[derive(Clone, Default)]
pub struct DomOverrideOptions {
pub disabled_for_editor: Option<RenderGate>,
pub disabled_for_session: Option<RenderGate>,
}
pub type RenderNext<'a, T> = &'a dyn Fn() -> Result<T, LexicalError>;
pub type CreateDomOverride = Rc<
dyn Fn(
&dyn LexicalNode,
&NodeRef,
RenderNext<'_, DomNode>,
&Editor,
) -> Result<DomNode, LexicalError>,
>;
pub type ExportDomOverride = Rc<
dyn Fn(
&dyn LexicalNode,
&NodeRef,
RenderNext<'_, DomExportOutput>,
&Editor,
) -> Result<DomExportOutput, LexicalError>,
>;
pub type ExtractWithChildOverride = Rc<
dyn Fn(
&NodeRef,
&NodeRef,
Option<&SelectionRef>,
&str,
RenderNext<'_, bool>,
&Editor,
) -> Result<bool, LexicalError>,
>;
pub type SelectionOverride = Rc<
dyn Fn(
&NodeRef,
Option<&SelectionRef>,
RenderNext<'_, bool>,
&Editor,
) -> Result<bool, LexicalError>,
>;
#[derive(Clone, Default)]
pub struct DomRenderMatchConfig {
pub create_dom: Option<CreateDomOverride>,
pub export_dom: Option<ExportDomOverride>,
pub extract_with_child: Option<ExtractWithChildOverride>,
pub should_exclude: Option<SelectionOverride>,
pub should_include: Option<SelectionOverride>,
}
pub struct DomRenderMatch {
pub nodes: Option<Vec<NodeMatch>>,
pub config: DomRenderMatchConfig,
pub options: DomOverrideOptions,
}
#[derive(Clone, Default)]
pub struct DomRenderConfig {
pub overrides: Vec<Rc<DomRenderMatch>>,
pub context_defaults: Vec<ContextPairOrUpdater>,
}
#[derive(Clone, Default)]
pub struct DomRenderConfigOverrides {
pub overrides: Option<Vec<Rc<DomRenderMatch>>>,
pub context_defaults: Option<Vec<ContextPairOrUpdater>>,
}