Skip to content
gpdoc
Playground

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.

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.

Terminal window
cargo add gpdoc-plugin-drag-handle

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");

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.

drag_handle().with_view(...)
├── DragHandlePart::Grip In the left gutter
└── DragHandlePart::Preview Beside the dragged block content

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"),
}
}

Apply these settings to drag_handle() before calling with_view, which returns the finished ViewPlugin.

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.));

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.));

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.

  • content holds the full-opacity copy at the original size.
  • range is the document range captured at drag start, with an exclusive end.
  • block_count counts 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));

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.

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

Drag handle implementation