Skip to main content

StyledText

Struct StyledText 

Source
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

Source

pub fn plain(text: impl Into<String>) -> StyledText

Unstyled text.

Source

pub fn markdown(md: &str, base: Font) -> StyledText

Parse markdown, the ergonomic way to seed a document (styled_codec).

Source

pub fn html(html: &str, base: Font) -> StyledText

Parse an HTML fragment.

Source

pub fn rtf(rtf: &str, base: Font) -> StyledText

Parse the RTF subset Day reads (docs/texteditor.md).

Source

pub fn to_markdown(&self, base: Font) -> String

Write this document as markdown / HTML / RTF. Each is lossy in the ways styled_codec documents.

Source

pub fn to_html(&self, base: Font) -> String

Source

pub fn to_rtf(&self, base: Font) -> String

Source

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.

Source

pub fn is_empty(&self) -> bool

Source

pub fn validate(&self) -> Result<(), String>

Are both range vectors well formed for the text (ascending, non-overlapping, in bounds, on character boundaries)?

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn clamp(&mut self)

Clamp every range into the text and onto character boundaries, dropping what cannot be saved. The last defense before a range reaches a backend and panics an str slice.

Source

pub fn splice(&mut self, range: Range<usize>, text: &str)

Replace range with text, moving every other range over the edit. The app-side twin of what a keystroke does, for a programmatic insert or delete.

Trait Implementations§

Source§

impl Clone for StyledText

Source§

fn clone(&self) -> StyledText

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for StyledText

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for StyledText

Source§

fn default() -> StyledText

Returns the “default value” for a type. Read more
Source§

impl From<&str> for StyledText

Source§

fn from(text: &str) -> StyledText

Converts to this type from the input type.
Source§

impl From<String> for StyledText

Source§

fn from(text: String) -> StyledText

Converts to this type from the input type.
Source§

impl PartialEq for StyledText

Source§

fn eq(&self, other: &StyledText) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for StyledText

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> AnyEq for T
where T: Any + PartialEq,

§

fn equals(&self, other: &(dyn Any + 'static)) -> bool

§

fn as_any(&self) -> &(dyn Any + 'static)

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> IntoReactive<T, StaticMark> for T
where T: Clone + 'static,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Day API ↩ Guides· daybrite.dev