pub struct StyledText {
pub text: String,
pub runs: Vec<TextRun>,
pub paragraphs: Vec<ParagraphRun>,
}Expand description
A styled document: plain text, the character runs over it, and the paragraph attributes over the same offsets.
Fields§
§text: String§runs: Vec<TextRun>§paragraphs: Vec<ParagraphRun>Implementations§
Source§impl StyledText
impl StyledText
Sourcepub fn markdown(md: &str, base: Font) -> Self
pub fn markdown(md: &str, base: Font) -> Self
Parse markdown, the ergonomic way to seed a document
(styled_codec).
Sourcepub fn to_markdown(&self, base: Font) -> String
pub fn to_markdown(&self, base: Font) -> String
Write this document as markdown / HTML / RTF. Each is lossy in the ways
styled_codec documents.
pub fn to_html(&self, base: Font) -> String
pub fn to_rtf(&self, base: Font) -> String
Sourcepub fn new(text: impl Into<String>, runs: Vec<TextRun>) -> Self
pub fn new(text: impl Into<String>, runs: Vec<TextRun>) -> Self
Text with character runs and no paragraph attributes, which is what a label carries today.
pub fn is_empty(&self) -> bool
Sourcepub fn validate(&self) -> Result<(), String>
pub fn validate(&self) -> Result<(), String>
Are both range vectors well formed for the text (ascending, non-overlapping, in bounds, on character boundaries)?
Sourcepub fn segments(&self, base: Font) -> Vec<(Range<usize>, RunStyle)>
pub fn segments(&self, base: Font) -> Vec<(Range<usize>, RunStyle)>
The whole text as consecutive (range, style) segments, unstyled gaps included as base.
One view that the query and the mutation below are both written against, so neither has to reason about where a run ends and a gap begins.
Sourcepub fn style_of(&self, range: Range<usize>, base: Font) -> RunStyle
pub fn style_of(&self, range: Range<usize>, base: Font) -> RunStyle
The style in effect across range.
Where the segments under it disagree, the differing attribute comes back as its default rather than as one of the values. That is what a toolbar renders as its mixed state, and it is what makes pressing the button set the whole selection instead of toggling half of it.
A collapsed caret takes the style of the segment that ENDS at it, preferring that over the one it starts: typing after a bold word continues bold, which is what every editor does.
Sourcepub fn apply(
&mut self,
range: Range<usize>,
base: Font,
f: impl Fn(&mut RunStyle),
)
pub fn apply( &mut self, range: Range<usize>, base: Font, f: impl Fn(&mut RunStyle), )
Apply f to the style of every segment overlapping range, splitting at its boundaries so
only the selected text changes, then coalescing what came out.
Sourcepub fn paragraph_style_of(&self, range: Range<usize>) -> ParagraphStyle
pub fn paragraph_style_of(&self, range: Range<usize>) -> ParagraphStyle
The paragraph style in effect across range, by the same mixed-state rule as
StyledText::style_of.
Sourcepub fn apply_paragraph(
&mut self,
range: Range<usize>,
f: impl Fn(&mut ParagraphStyle),
)
pub fn apply_paragraph( &mut self, range: Range<usize>, f: impl Fn(&mut ParagraphStyle), )
Apply f to every paragraph the selection touches. Paragraph boundaries are \n, so a
caret anywhere inside a paragraph styles the whole of it, which is what every editor does.
Sourcepub fn reflow(&mut self, offset: usize, removed: usize, inserted: usize)
pub fn reflow(&mut self, offset: usize, removed: usize, inserted: usize)
Move the ranges over a character edit the native editor already made: removed bytes at
offset replaced by inserted bytes.
This is why a keystroke does not have to ship the whole document back. Ranges before the edit are untouched, ranges after it shift, a range containing it grows or shrinks, and a range the edit emptied is dropped. That is the rule every attributed-string implementation uses, exact, and O(runs) rather than O(document).
self.text must already be the new text; this only moves ranges.
Trait Implementations§
Source§impl Clone for StyledText
impl Clone for StyledText
Source§fn clone(&self) -> StyledText
fn clone(&self) -> StyledText
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more