Skip to content

Commit

Permalink
Fewer text nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Aug 6, 2024
1 parent 10b3ec0 commit 8588f83
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ pub fn StylizedCode(props: &StylizedCodeProps) -> Html {
let bg = theme.settings.background.unwrap_or(Color::WHITE);
let mut highlighter = HighlightLines::new(syntax, theme);
let mut output = Vec::new();
let mut rc_style: Option<Rc<str>> = None;
let mut last_style: Option<Rc<str>> = None;
let mut last_string = String::new();

for line in LinesWithEndings::from(&props.text) {
let regions = highlighter.highlight_line(line, &SYNTAX_SET).unwrap_throw();
Expand Down Expand Up @@ -107,20 +108,36 @@ pub fn StylizedCode(props: &StylizedCodeProps) -> Html {
true => None,
false => Some(style),
};
if style.as_deref() != rc_style.as_deref() {
rc_style = style.map(Rc::from);
if style.as_deref() != last_style.as_deref() {
last_style = style.map(Rc::from);
}

output.push(html!(<span style={rc_style.clone()}>{text}</span>));
if let Some(last_style) = last_style.clone() {
if !last_string.is_empty() {
output.push(html!({ String::from(&last_string) }));
last_string.clear();
}
output.push(html!(<span style={last_style}>{text}</span>));
} else {
last_string.push_str(text);
}
}
}
if !last_string.is_empty() {
output.push(html!({ last_string }));
}

let style = format!(
"color:#{:02x}{:02x}{:02x};background-color:#{:02x}{:02x}{:02x};",
fg.r, fg.g, fg.b, bg.r, bg.g, bg.b,
);

html!(<pre {style}> {output} {"\u{feff}"} </pre>)
html! {
<pre {style}>
{output}
{"\u{feff}"}
</pre>
}
}

#[derive(Properties, PartialEq)]
Expand Down

0 comments on commit 8588f83

Please sign in to comment.