Image
Insert an image with alternative text and optional dimensions.
image() adds a block image with a source, alternative text, optional title, and optional dimensions. Use image_with_placement(Placement::Inline) for images inside text.
Try it
Section titled “Try it”Install
Section titled “Install”cargo add gpdoc-plugin-imageExample
Section titled “Example”Register the view that draws each image at its stored size in the Quickstart editor configuration.
use gpdoc::{EditorConfig, PluginViewExt, ViewPlugin};use gpdoc_plugin_image::{image, image_content};use gpdoc_plugin_paragraph::paragraph;
let config = EditorConfig::builder() .plugin(paragraph()) .plugin(image().with_view(image_tools)) .build() .expect("configure editor");
fn image_tools() -> ViewPlugin { image_content()}Type  at the start of a paragraph. Supply the asset at that path to see the image. The editor shows the alt text while the image is unavailable.
Compose an image control
Section titled “Compose an image control”Use image_controls to place a GPUI element over the picture. The editor clips the element to the image bounds.
Anatomy
Section titled “Anatomy”image_controls(...) Draws the picture and supplies its ImageNode└── div() Your overlay └── div().on_click() Your button to apply image.resize(...)Restore the original image size
Section titled “Restore the original image size”In the example above, replace the image_content import with image_controls. Use this version of image_tools to add a reset button to images with a stored width or height.
use gpui::{div, prelude::*};
fn image_tools() -> ViewPlugin { image_controls(|image, _, _| { let overlay = div(); if image.dimensions() == (None, None) { return overlay; }
let reset = image.resize(None, None); let editor = image.slot.view.clone(); let button = div() .id(("original-image-size", image.slot.before.0)) .child("Original size") .on_click(move |_, window, cx| { editor.update(cx, |view, cx| { if let Err(error) = view.apply_node_view(reset.clone(), window, cx) { eprintln!("Could not resize image: {error}"); } }); }); overlay.child(button) })}Insert the 240-pixel image from the setImage example below. The image shows an Original size button. Click the button to clear both stored dimensions. Undo restores the width and the button. To set a specific size, pass a positive pixel value to resize for either dimension. Leaving one dimension at None preserves the aspect ratio.
Settings
Section titled “Settings”placement
Section titled “placement”image() uses Placement::Block. To keep pictures inline with text, register image_with_placement(placement: Placement) in your config.
let image = gpdoc_plugin_image::image_with_placement(gpdoc_plugin::core::Placement::Inline);Commands
Section titled “Commands”setImage
Section titled “setImage”Inserts an image at the caret or replaces the selection. Supply the image source and any optional display attributes.
editor.run(gpdoc_plugin_image::Set { src: "images/swallow.png".into(), alt: Some("A swallow in flight".into()), width: std::num::NonZeroU32::new(240), ..Default::default()})?;| Argument | Value |
|---|---|
src |
Resource path or URL string. A nonempty value enables image rendering. |
alt, title |
Strings. GPUI labels the node with the first nonempty value, falling back to "image". |
width, height |
Positive pixel sizes (Option<NonZeroU32>). |
GPUI can display app assets and HTTP(S) URLs through the host’s configured HTTP client. Supply your own resize controls for pointer-driven resizing.
Typing shortcuts
Section titled “Typing shortcuts”At the start of a paragraph, type . The closing parenthesis converts the bracketed text to alt and the path to src.