use std::{
cell::{Cell, RefCell},
collections::HashMap,
ops::Range,
sync::Arc,
};
use super::{
bidi_paragraphs::link_directions,
link_block::LinkBlock,
links::{MarkdownLink, MarkdownLinkPresentation},
tooltip::{LinkTooltip, TooltipHover, TooltipKey},
};
use crate::{
app::{
events::{MouseButton, MouseEvent},
theme::ThemeMarkdown,
},
layout::{quad::RichTextSpan, text_style::TextStyle},
markdown::{
Direction, FIRST_STRONG_ISOLATE, MarkdownInline, POP_DIRECTIONAL_ISOLATE, is_invisible, markdown_inline_rich_text,
paragraph_breaks_as_line_feeds, paragraph_breaks_as_spaces, strip_bidi_controls,
},
node::{CursorIcon, EventHandler, Node, inline_link::InlineLink},
};
pub(super) type MarkdownLinkCallback = Arc<dyn Fn(&MarkdownLink) + Send + Sync>;
pub(super) type MarkdownLinkActivator = Arc<dyn Fn(&MarkdownLink, &MouseEvent) + Send + Sync>;
pub(super) type MarkdownLinkPresenter = Arc<dyn Fn(&MarkdownLink) -> MarkdownLinkPresentation + Send + Sync>;
pub(super) struct LinkSession {
next_index: Cell<usize>,
occurrences: RefCell<HashMap<Arc<str>, usize>>,
presenter: Option<MarkdownLinkPresenter>,
on_link_click: Option<MarkdownLinkCallback>,
on_link_activate: Option<MarkdownLinkActivator>,
#[cfg(feature = "router")]
navigator: Option<crate::router::Navigator>,
hover: TooltipHover,
tooltips: RefCell<Vec<LinkTooltip>>,
}
pub(super) struct LinkCheckpoint {
next_index: usize,
occurrences: HashMap<Arc<str>, usize>,
tooltips: usize,
}
impl LinkSession {
pub(super) fn new(
presenter: Option<MarkdownLinkPresenter>,
on_link_click: Option<MarkdownLinkCallback>,
on_link_activate: Option<MarkdownLinkActivator>,
#[cfg(feature = "router")] navigator: Option<crate::router::Navigator>,
hover: TooltipHover,
) -> Self {
Self {
next_index: Cell::new(0),
occurrences: RefCell::new(HashMap::new()),
presenter,
on_link_click,
on_link_activate,
#[cfg(feature = "router")]
navigator,
hover,
tooltips: RefCell::new(Vec::new()),
}
}
pub(super) fn take_tooltips(&self) -> Vec<LinkTooltip> {
let tooltips = self.tooltips.take();
self.hover.keep_anchors(&tooltips);
tooltips
}
pub(super) fn checkpoint(&self) -> LinkCheckpoint {
LinkCheckpoint {
next_index: self.next_index.get(),
occurrences: self.occurrences.borrow().clone(),
tooltips: self.tooltips.borrow().len(),
}
}
pub(super) fn restore(&self, checkpoint: LinkCheckpoint) {
self.next_index.set(checkpoint.next_index);
*self.occurrences.borrow_mut() = checkpoint.occurrences;
self.tooltips.borrow_mut().truncate(checkpoint.tooltips);
}
fn next_link(&self, destination: &str, title: &str, words: String) -> (MarkdownLink, TooltipKey) {
let index = self.next_index.get();
self.next_index.set(index + 1);
let destination: Arc<str> = Arc::from(destination);
let occurrence = {
let mut occurrences = self.occurrences.borrow_mut();
let count = occurrences.entry(destination.clone()).or_default();
*count += 1;
*count - 1
};
let link = MarkdownLink {
destination: destination.clone(),
title: Arc::from(title),
text: Arc::from(words),
index,
};
(link, TooltipKey::new(destination, occurrence))
}
fn present(&self, link: &MarkdownLink) -> MarkdownLinkPresentation {
self
.presenter
.as_ref()
.map_or_else(MarkdownLinkPresentation::link, |present| present(link))
}
fn activation_handler(&self, link: MarkdownLink) -> EventHandler<MouseEvent> {
let on_link_activate = self.on_link_activate.clone();
let on_link_click = self.on_link_click.clone();
#[cfg(feature = "router")]
let navigator = self.navigator.clone();
EventHandler::new(move |event: &MouseEvent| {
if let Some(on_link_activate) = &on_link_activate {
on_link_activate(&link, event);
}
if event.button != MouseButton::Left {
return;
}
if let Some(on_link_click) = &on_link_click {
on_link_click(&link);
return;
}
#[cfg(feature = "router")]
if on_link_activate.is_none()
&& let Some(navigator) = &navigator
&& is_router_link(link.destination())
{
navigator.push(link.destination().to_owned());
}
})
}
fn follows(&self, node: Node, link: MarkdownLink) -> Node {
let activate = self.activation_handler(link);
let node = node.on_click(activate.clone());
if self.on_link_activate.is_none() {
return node;
}
node
.on_mouse_down(hold_middle_press)
.on_mouse_click(MouseButton::Middle, activate)
}
fn with_tooltip(&self, node: Node, key: TooltipKey, text: Option<Arc<str>>) -> Node {
let Some(text) = text else {
return node;
};
let anchor = self.hover.anchor(&key);
self
.tooltips
.borrow_mut()
.push(LinkTooltip::new(key.clone(), anchor.clone(), text));
let (enter, leave) = (self.hover.clone(), self.hover.clone());
let enter_key = key.clone();
node
.ref_element(anchor)
.on_mouse_enter(move || enter.enter(&enter_key))
.on_mouse_leave(move || leave.leave(&key))
}
}
fn hold_middle_press(event: MouseEvent) {
if event.button == MouseButton::Middle {
event.prevent_default();
}
}
#[cfg(feature = "router")]
fn is_router_link(destination: &str) -> bool {
destination.starts_with('/') || destination.starts_with('#') || destination.starts_with('?')
}
pub(super) enum FlowItem<'i> {
Text(Box<Node>),
Image(&'i MarkdownInline, TextStyle),
}
pub(super) struct Paragraph<'a> {
spans: Vec<RichTextSpan>,
sealed: usize,
generated: Vec<Range<usize>>,
strip_controls: bool,
directions: std::vec::IntoIter<Direction>,
links: Vec<Node>,
theme: &'a ThemeMarkdown,
session: &'a LinkSession,
}
impl<'a> Paragraph<'a> {
pub(super) fn new(
inlines: &[MarkdownInline],
flow: bool,
theme: &'a ThemeMarkdown,
session: &'a LinkSession,
) -> Self {
let strip_controls = has_link(inlines);
let directions = if strip_controls {
link_directions(inlines, flow, theme)
} else {
Vec::new()
};
Self {
spans: Vec::new(),
sealed: 0,
generated: Vec::new(),
strip_controls,
directions: directions.into_iter(),
links: Vec::new(),
theme,
session,
}
}
pub(super) fn is_empty(&self) -> bool {
self.spans.is_empty()
}
pub(super) fn take_node(&mut self) -> Node {
self.sealed = 0;
Node::rich_text_with_links(
std::mem::take(&mut self.spans),
std::mem::take(&mut self.links),
std::mem::take(&mut self.generated),
)
}
pub(super) fn push_inlines<'i>(
&mut self,
inlines: &'i [MarkdownInline],
style: &TextStyle,
flow: &mut Option<&mut Vec<FlowItem<'i>>>,
) {
for inline in inlines {
match inline {
MarkdownInline::Link {
destination,
title,
children,
} => self.push_link(destination, title, children, style),
MarkdownInline::Image { .. } if flow.is_some() => {
if let Some(flow) = flow.as_mut() {
if !self.is_empty() {
flow.push(FlowItem::Text(Box::new(self.take_node())));
}
flow.push(FlowItem::Image(inline, style.clone()));
}
}
MarkdownInline::Emphasis(children) => {
self.push_inlines(children, &self.theme.emphasis.apply(style), flow);
}
MarkdownInline::Strong(children) => {
self.push_inlines(children, &self.theme.strong.apply(style), flow);
}
MarkdownInline::Strikethrough(children) => {
self.push_inlines(children, &self.theme.strikethrough.apply(style), flow);
}
_ => {
for span in markdown_inline_rich_text(std::slice::from_ref(inline), style, self.theme) {
self.push_span(span);
}
}
}
}
}
fn push_link(&mut self, destination: &str, title: &str, children: &[MarkdownInline], style: &TextStyle) {
let direction = self.directions.next().unwrap_or(Direction::LeftToRight);
let (link, key) = self
.session
.next_link(destination, title, link_words(children, self.theme));
if is_invisible(link.text()) {
self.push_invisible_words(children, style);
return;
}
let presentation = self.session.present(&link);
let words_style = if presentation.as_text {
style.clone()
} else {
presentation.style.as_ref().unwrap_or(&self.theme.link).apply(style)
};
let words = self.words(children, &words_style);
let runs = presentation
.trailing
.iter()
.map(|run| RichTextSpan {
text: paragraph_breaks_as_spaces(&strip_bidi_controls(&run.text)).into_owned(),
style: run.style.apply(style),
})
.collect();
let block = self.push_block(LinkBlock::new(direction, words, runs, style));
if presentation.as_text || block.label.is_empty() {
return;
}
let inline_link = InlineLink {
label: block.label,
trailing: block.trailing,
target: block.target,
look: presentation.look(self.theme),
};
let node = Node::inline_link(inline_link, link.text())
.tab_index(0)
.cursor(CursorIcon::Pointer);
let node = self.session.follows(node, link);
self
.links
.push(self.session.with_tooltip(node, key, presentation.tooltip));
}
fn words(&mut self, children: &[MarkdownInline], style: &TextStyle) -> Vec<RichTextSpan> {
let start = self.seal();
self.push_inlines(children, style, &mut None);
let mut words = self.spans.split_off(start);
self.seal();
for span in &mut words {
span.text = paragraph_breaks_as_spaces(&span.text).into_owned();
}
words
}
fn push_invisible_words(&mut self, children: &[MarkdownInline], style: &TextStyle) {
let words = self.words(children, style);
self.push_mark(FIRST_STRONG_ISOLATE, style);
for span in words {
self.push_span(span);
}
self.push_mark(POP_DIRECTIONAL_ISOLATE, style);
}
fn push_block(&mut self, block: LinkBlock) -> PlacedLink {
let offset = self.seal();
let shift = |range: Range<usize>| range.start + offset..range.end + offset;
self.spans.extend(block.spans);
self.generated.extend(block.generated.into_iter().map(shift));
self.seal();
PlacedLink {
label: shift(block.label),
trailing: shift(block.trailing),
target: shift(block.target),
}
}
fn push_mark(&mut self, mark: char, style: &TextStyle) {
let index = self.seal();
self.spans.push(RichTextSpan {
text: mark.to_string(),
style: style.clone(),
});
self.generated.push(index..index + 1);
self.seal();
}
fn push_span(&mut self, mut span: RichTextSpan) {
if self.strip_controls {
span.text = paragraph_breaks_as_line_feeds(&strip_bidi_controls(&span.text)).into_owned();
}
if span.text.is_empty() {
return;
}
if self.spans.len() > self.sealed
&& let Some(last) = self.spans.last_mut()
&& last.style == span.style
{
last.text.push_str(&span.text);
return;
}
self.spans.push(span);
}
fn seal(&mut self) -> usize {
self.sealed = self.spans.len();
self.sealed
}
}
fn has_link(inlines: &[MarkdownInline]) -> bool {
inlines.iter().any(|inline| match inline {
MarkdownInline::Link { .. } => true,
MarkdownInline::Emphasis(children) | MarkdownInline::Strong(children) | MarkdownInline::Strikethrough(children) => {
has_link(children)
}
_ => false,
})
}
struct PlacedLink {
label: Range<usize>,
trailing: Range<usize>,
target: Range<usize>,
}
fn link_words(children: &[MarkdownInline], theme: &ThemeMarkdown) -> String {
let words = markdown_inline_rich_text(children, &TextStyle::default(), theme)
.into_iter()
.map(|span| span.text)
.collect::<String>();
paragraph_breaks_as_spaces(&strip_bidi_controls(&words)).into_owned()
}