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 plain(text: impl Into<String>) -> StyledText
pub fn plain(text: impl Into<String>) -> StyledText
Unstyled text.
Sourcepub fn markdown(md: &str, base: Font) -> StyledText
pub fn markdown(md: &str, base: Font) -> StyledText
Parse markdown, the ergonomic way to seed a document
(styled_codec).
Sourcepub fn html(html: &str, base: Font) -> StyledText
pub fn html(html: &str, base: Font) -> StyledText
Parse an HTML fragment.
Sourcepub fn rtf(rtf: &str, base: Font) -> StyledText
pub fn rtf(rtf: &str, base: Font) -> StyledText
Parse the RTF subset Day reads (docs/texteditor.md).
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>) -> StyledText
pub fn new(text: impl Into<String>, runs: Vec<TextRun>) -> StyledText
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 moreSource§impl Debug for StyledText
impl Debug for StyledText
Source§impl Default for StyledText
impl Default for StyledText
Source§fn default() -> StyledText
fn default() -> StyledText
Source§impl From<&str> for StyledText
impl From<&str> for StyledText
Source§fn from(text: &str) -> StyledText
fn from(text: &str) -> StyledText
Source§impl From<String> for StyledText
impl From<String> for StyledText
Source§fn from(text: String) -> StyledText
fn from(text: String) -> StyledText
Source§impl PartialEq for StyledText
impl PartialEq for StyledText
impl StructuralPartialEq for StyledText
Auto Trait Implementations§
impl Freeze for StyledText
impl RefUnwindSafe for StyledText
impl Send for StyledText
impl Sync for StyledText
impl Unpin for StyledText
impl UnsafeUnpin for StyledText
impl UnwindSafe for StyledText
Blanket Implementations§
§impl<T> AnyEq for T
impl<T> AnyEq for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more