Skip to content
gpdoc
Playground

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.

Terminal window
cargo add gpdoc-plugin-text-style

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.

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()?;

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()?;

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.

Set the foreground color of the selected text.

editor.run(gpdoc_plugin_text_style::SetColor(Some("#2456aa".into())))?;

Clear its foreground color.

editor.run(gpdoc_plugin_text_style::UnsetColor)?;

Set the background behind selected characters.

editor.run(gpdoc_plugin_text_style::SetBackgroundColor(Some("#ffe887".into())))?;

Clear the background color from selected characters.

editor.run(gpdoc_plugin_text_style::UnsetBackgroundColor)?;

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())))?;

Restore the inherited font family.

editor.run(gpdoc_plugin_text_style::UnsetFontFamily)?;

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())))?;

Restore the inherited font size.

editor.run(gpdoc_plugin_text_style::UnsetFontSize)?;

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())))?;

Restore the inherited line height.

editor.run(gpdoc_plugin_text_style::UnsetLineHeight)?;

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()
})?;

Remove marks in the selection when all their properties are null or empty strings.

editor.run(gpdoc_plugin_text_style::RemoveEmpty)?;