Drag handle
Configure the gutter width, grip height, grip renderer, and drag preview.
drag_handle() adds a grip in the left gutter. Dragging the grip moves the block or the selected range of sibling blocks on drop.
Try it
Section titled “Try it”Drag the grip beside “Collect feedback” to move the whole quote. To try the count badge, click “Draft the outline”, press Shift + Down Arrow twice, and drag the quote’s grip.
Install
Section titled “Install”cargo add gpdoc-plugin-drag-handleExample
Section titled “Example”Pass block_grip to with_view to display the built-in grip in the gutter and during a drag.
use gpdoc::EditorConfig;use gpdoc_plugin_drag_handle::{block_grip, drag_handle};use gpdoc_plugin_paragraph::paragraph;
let config = EditorConfig::builder() .plugin(paragraph()) .plugin(drag_handle().with_view(block_grip)) .build() .expect("configure editor");Compose the drag handle
Section titled “Compose the drag handle”The with_view renderer receives a DragHandleSlot. Use slot.part to give the grip a different appearance during a drag. The slot also provides the editor entity and before, the document position immediately before the block that owns the grip.
Anatomy
Section titled “Anatomy”drag_handle().with_view(...)├── DragHandlePart::Grip In the left gutter└── DragHandlePart::Preview Beside the dragged block contentShow a label while dragging
Section titled “Show a label while dragging”Replace block_grip in the example above with render_handle. The gutter shows a 14 px grip. When you drag it, “Move” appears beside the block copy.
use gpdoc_plugin_drag_handle::{DragHandlePart, DragHandleSlot};use gpui::{App, Div, Window, div, prelude::*, px};
fn render_handle(slot: DragHandleSlot, _: &mut Window, _: &mut App) -> Div { match slot.part { DragHandlePart::Grip => div().text_size(px(14.)).child("⠿"), DragHandlePart::Preview => div().child("Move"), }}Settings
Section titled “Settings”Apply these settings to drag_handle() before calling with_view, which returns the finished ViewPlugin.
gutter_width
Section titled “gutter_width”Sets the width of the left gutter and the area where a grip drag can start. Defaults to px(20.).
let handle = drag_handle().gutter_width(gpui::px(28.));handle_height
Section titled “handle_height”Sets the height of the grip’s drag target and the minimum height of each top-level block. Defaults to px(24.).
let handle = drag_handle().handle_height(gpui::px(32.));with_preview
Section titled “with_preview”Sets the renderer for the dragged block content. By default, the block copy follows the pointer at 55% opacity. The renderer receives a DragPreviewSlot with three fields.
contentholds the full-opacity copy at the original size.rangeis the document range captured at drag start, with an exclusive end.block_countcounts the sibling blocks being moved. A quote containing two paragraphs counts as one when dragged as a whole.
use gpui::{div, prelude::*};
let handle = drag_handle().with_preview(|slot, _, _| div().opacity(0.75).child(slot.content));Preview placement
Section titled “Preview placement”DragPreviewPlacement::over_blocks
Section titled “DragPreviewPlacement::over_blocks”Aligns the preview with the top-left corner of the dragged block range at drag start. Returning a plain GPUI element from with_preview uses this placement by default.
DragPreviewPlacement::beside_handle
Section titled “DragPreviewPlacement::beside_handle”Aligns the preview with the top-right corner of the grip being dragged. Use it for a count badge that follows the grabbed grip even when the selection starts several blocks above it.
use gpdoc_plugin_drag_handle::DragPreviewPlacement;use gpui::{div, prelude::*};
let handle = drag_handle().with_preview(|slot, _, _| { if slot.block_count == 1 { return DragPreviewPlacement::over_blocks(div().opacity(0.55).child(slot.content)); } let badge = div().whitespace_nowrap().child(format!("{} blocks", slot.block_count)); DragPreviewPlacement::beside_handle(badge)});