Text style
Set text color, background color, font family, font size, or line height.
Text style adds the textStyle mark. It can store multiple style properties on the same text, such as a custom color and font size.
Try it
Section titled “Try it”Install
Section titled “Install”cargo add gpdoc-plugin-text-styleExample
Section titled “Example”Register the text style formatter and the color and font size properties in the Quickstart editor configuration. The mark and each property install their own default views.
use gpdoc::{EditorConfig, PluginViewExt};use gpdoc_plugin_text_style::{color, font_size, text_style, text_style_format};use gpdoc_plugin_paragraph::paragraph;
let config = EditorConfig::builder() .plugin(paragraph()) .plugin(text_style().with_view(|| text_style_format(["textStyle"]))) .plugin(color()) .plugin(font_size()) .build() .expect("configure editor");Use setColor and setFontSize on the same selection to apply both properties. Clearing the size with unsetFontSize keeps the color.
Settings
Section titled “Settings”text_style_kit
Section titled “text_style_kit”Use .plugins(text_style_kit()) to install the mark and all five properties with their default views. For a smaller config, register text_style() with one or more of color(), background_color(), font_family(), font_size(), and line_height().
use gpdoc::EditorConfig;use gpdoc_plugin_text_style::{color, text_style};
let config = EditorConfig::builder() .plugin(paragraph()) .plugin(text_style()) .plugin(color()) .build()?;StyleProperty::on
Section titled “StyleProperty::on”Choose which registered node or mark types accept an attribute. on(self, types: impl IntoIterator<Item = impl Into<String>>) accepts the variants Color, BackgroundColor, FontFamily, FontSize, and LineHeight. Include textStyle to use that property with the text commands below.
use gpdoc::EditorConfig;use gpdoc_plugin_text_style::{text_style, StyleProperty};
let color = StyleProperty::Color.on(["textStyle", "paragraph"]);let config = EditorConfig::builder() .plugin(paragraph()) .plugin(text_style()) .plugin(color) .build()?;Commands
Section titled “Commands”Each setter accepts Some(value) to assign a string or None to clear it. A setter preserves the other properties. An unset command clears its property and runs removeEmptyTextStyle. editor.run returns Result<CommandOutcome, Error>. A setter returns NotApplicable when the selection cannot take the style.
setColor
Section titled “setColor”Set the foreground color of the selected text.
editor.run(gpdoc_plugin_text_style::SetColor(Some("#2456aa".into())))?;unsetColor
Section titled “unsetColor”Clear its foreground color.
editor.run(gpdoc_plugin_text_style::UnsetColor)?;setBackgroundColor
Section titled “setBackgroundColor”Set the background behind selected characters.
editor.run(gpdoc_plugin_text_style::SetBackgroundColor(Some("#ffe887".into())))?;unsetBackgroundColor
Section titled “unsetBackgroundColor”Clear the background color from selected characters.
editor.run(gpdoc_plugin_text_style::UnsetBackgroundColor)?;setFontFamily
Section titled “setFontFamily”Set the font family. Supply a CSS family name or a comma-separated list.
editor.run(gpdoc_plugin_text_style::SetFontFamily(Some("Georgia, serif".into())))?;unsetFontFamily
Section titled “unsetFontFamily”Restore the inherited font family.
editor.run(gpdoc_plugin_text_style::UnsetFontFamily)?;setFontSize
Section titled “setFontSize”Set the font size using a CSS length. GPUI renders common lengths such as px, em, and rem. Your host must resolve expressions such as calc() and var().
editor.run(gpdoc_plugin_text_style::SetFontSize(Some("18px".into())))?;unsetFontSize
Section titled “unsetFontSize”Restore the inherited font size.
editor.run(gpdoc_plugin_text_style::UnsetFontSize)?;setLineHeight
Section titled “setLineHeight”Set line height using a CSS value. A unitless number specifies a multiple of the font size.
editor.run(gpdoc_plugin_text_style::SetLineHeight(Some("1.5".into())))?;unsetLineHeight
Section titled “unsetLineHeight”Restore the inherited line height.
editor.run(gpdoc_plugin_text_style::UnsetLineHeight)?;toggleTextStyle
Section titled “toggleTextStyle”Set the style fields to match. If all eligible text already matches, remove the whole mark. Otherwise merge the supplied values with existing styles.
editor.run(gpdoc_plugin_text_style::Toggle { font_size: Some("18px".into()), ..Default::default()})?;removeEmptyTextStyle
Section titled “removeEmptyTextStyle”Remove marks in the selection when all their properties are null or empty strings.
editor.run(gpdoc_plugin_text_style::RemoveEmpty)?;