gpui-kit-markdown-editor 0.1.1

A block-based WYSIWYG Markdown editor for GPUI, built on gpui-kit's editor host API.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
//! `BlockEditor` view (design ยง7).
//!
//! `render` takes the cached projection and renders it with gpui-kit's own
//! `BlockNode` renderer, so the editor looks exactly like `TextView`. The
//! [`EditorRoot`] element installs a frame-local [`FrameSink`] while its
//! children prepaint: keyed text leaves report their layouts into it and take
//! their selection backgrounds from it (`text::editor_seam`, D3). The root
//! then paints the caret and the composition underline from that geometry.
//! Paint never updates the editor entity.

use std::{cell::Cell, collections::HashMap, ops::Range, rc::Rc};

use gpui::prelude::FluentBuilder as _;
use gpui::{
    AnyElement, App, Bounds, CursorStyle, DefiniteLength, DispatchPhase, Edges, Element, ElementId,
    ElementInputHandler, Entity, GlobalElementId, Hitbox, HitboxBehavior, Hsla, InspectorElementId,
    InteractiveElement, IntoElement, LayoutId, MouseButton, MouseDownEvent, MouseMoveEvent,
    MouseUpEvent, ParentElement, Pixels, Refineable, RenderOnce, Role, StatefulInteractiveElement,
    Style, StyleRefinement, Styled, TextStyle, Window, canvas, div, fill, point, px, size,
};

use super::{cull::Measured, id::BlockId, projection::Snapshot, state::*, style::BlockEditorStyle};
use gpui_kit::base::StyledExt as _;
use gpui_kit::base::text::editor_host::{
    BlockNode, FlowPosition, RenderContext, collect_leaf_geometry,
};

/// Items per cull unit of a long top-level list.
const LIST_CHUNK: usize = 32;

/// A block-based WYSIWYG Markdown editor (spike). Hold an
/// `Entity<BlockEditorState>` and pass it here in `render`. Instance styling
/// applies to the native input root; it installs no padding or height defaults.
#[derive(IntoElement)]
pub struct BlockEditor {
    state: Entity<BlockEditorState>,
    style: StyleRefinement,
}

impl BlockEditor {
    pub fn new(state: &Entity<BlockEditorState>) -> Self {
        Self {
            state: state.clone(),
            style: StyleRefinement::default(),
        }
    }
}

// Presentation is applied inside EditorRoot so padding is part of its
// native hitbox and input bounds, not an unfocusable outer wrapper.
impl Styled for BlockEditor {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

/// The accessible role (and heading level) of a top-level block (Phase 5,
/// per-block accessibility). `None` for blocks that are not exposed on
/// their own (definitions, breaks).
pub(crate) fn block_role(node: &BlockNode) -> Option<(Role, Option<usize>)> {
    Some(match node {
        BlockNode::Paragraph(_) => (Role::Paragraph, None),
        BlockNode::Heading { level, .. } => (Role::Heading, Some(*level as usize)),
        BlockNode::Blockquote { .. } => (Role::Blockquote, None),
        BlockNode::List { .. } => (Role::List, None),
        BlockNode::CodeBlock(_) => (Role::Code, None),
        BlockNode::Table(_) => (Role::Table, None),
        BlockNode::HorizontalRule { .. } => (Role::Splitter, None),
        BlockNode::Custom { .. } => (Role::Group, None),
        BlockNode::ListItem { .. } => (Role::ListItem, None),
        BlockNode::Root { .. }
        | BlockNode::Break { .. }
        | BlockNode::Definition { .. }
        | BlockNode::Unknown => return None,
        _ => return None,
    })
}

impl RenderOnce for BlockEditor {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = gpui_kit::base::Theme::global(cx);
        // Links are followed by the editor itself (Cmd/Ctrl-click); the shared
        // renderer must never open a URL on its own.
        let render_cx = RenderContext::from_app(cx).on_link_click(|_, _, _, _| {});
        let text_style = render_cx.style().clone();
        let mut style = self.state.read(cx).style.resolved(&theme.tokens);
        if style.selection_is_default() {
            style = style.with_selection(text_style.selection());
        }
        // Read the prior frame before request_layout can reset shared text
        // layouts. Manual scrolling must not become a reflow reveal request.
        let caret_was_visible = {
            let state = self.state.read(cx);
            state
                .scroll_handle
                .as_ref()
                .zip(state.model.caret())
                .is_some_and(|(handle, pos)| {
                    state
                        .frame
                        .borrow()
                        .caret_bounds(pos, style.caret_width())
                        .is_some_and(|caret| handle.bounds().contains(&caret.center()))
                })
        };
        let (focus, snapshot, backgrounds, extensions, keep, list_items) =
            self.state.update(cx, |state, _| {
                let snapshot = state.snapshot();
                let mut backgrounds: HashMap<u64, Vec<(Range<usize>, Hsla)>> = HashMap::new();
                for (block, range) in state.selection_ranges() {
                    // Atoms paint whole (O(1) lookup, not a scan of the leaves).
                    let range = match state.model.doc.get(block).map(|e| &e.content) {
                        Some(super::document::BlockContent::Atom(_)) => {
                            0..state.model.doc.leaf_len(block).unwrap_or(range.end)
                        }
                        _ => range,
                    };
                    backgrounds
                        .entry(block.as_u64())
                        .or_default()
                        .push((range, style.selection()));
                }
                (
                    state.focus_handle.clone(),
                    snapshot,
                    backgrounds,
                    state.model.doc.extensions.clone(),
                    state.pinned_blocks(),
                    // Only needed to rebuild the cached cull units.
                    (!state.cull.borrow().index.is_current(state.model.revision())).then(|| {
                        let doc = &state.model.doc;
                        doc.children(doc.root)
                            .iter()
                            .filter(|id| {
                                matches!(
                                    doc.kind(**id),
                                    Some(super::kind::BlockKindData::List { .. })
                                ) && doc.children(**id).len() > LIST_CHUNK
                            })
                            .map(|id| (*id, Rc::<[BlockId]>::from(doc.children(*id))))
                            .collect::<HashMap<_, _>>()
                    }),
                )
            });

        // Match the native root's effective inherited/instance typography
        // before deriving body-relative node metrics and culling geometry.
        let mut typography = window.text_style();
        typography.refine(&self.style.text);
        let node_cx = render_cx
            .with_markdown_extensions(extensions)
            .with_body_font_size(typography.font_size.to_pixels(window.rem_size()));
        let (cull, revision) = {
            let state = self.state.read(cx);
            (state.cull.clone(), state.model.revision())
        };
        let mut children: Vec<AnyElement> = Vec::new();
        {
            let mut cull_state = cull.borrow_mut();
            cull_state.measured.clear();
            let window_span = cull_state.visible.map(|(top, bottom)| {
                // Overscan two viewports each way, so a fast scroll stays
                // within laid-out blocks until the next frame catches up.
                let margin = ((bottom - top) * 2.).max(px(600.));
                (top - margin, bottom + margin)
            });
            let cull_state = &mut *cull_state;
            if let Some(list_items) = list_items {
                // Cull units: each top-level node, except that a long
                // top-level list is split into chunks of `LIST_CHUNK` items,
                // each rendered as a list of its own (continuing the
                // numbering, no gap between chunks). Rebuilt per revision.
                let nodes: Vec<&BlockNode> = snapshot
                    .blocks
                    .iter()
                    .flat_map(|b| b.nodes.iter())
                    .collect();
                let mut units = Vec::with_capacity(nodes.len());
                let mut node_ix = 0;
                for (block_ix, block) in snapshot.blocks.iter().enumerate() {
                    for (node_in_block, node) in block.nodes.iter().enumerate() {
                        let flow = FlowPosition::at(&nodes, node_ix, false);
                        let unit = |id, items| super::cull::UnitDesc {
                            id,
                            block: block_ix,
                            node: node_in_block,
                            node_ix,
                            items,
                            flow,
                        };
                        match (node, list_items.get(&block.id)) {
                            (BlockNode::List { children, .. }, Some(ids))
                                if ids.len() == children.len() =>
                            {
                                for start in (0..ids.len()).step_by(LIST_CHUNK) {
                                    let range = start..(start + LIST_CHUNK).min(ids.len());
                                    units.push(unit(ids[start], Some((range, ids.clone()))));
                                }
                            }
                            _ => units.push(unit(block.id, None)),
                        }
                        node_ix += 1;
                    }
                }
                cull_state.index.rebuild(
                    revision,
                    units.iter().map(|u| {
                        let covered: &[BlockId] = match &u.items {
                            Some((range, ids)) => &ids[range.clone()],
                            None => &[],
                        };
                        (u.id, covered)
                    }),
                    &cull_state.heights,
                );
                cull_state.units = units;
                cull_state.node_count = nodes.len();
            }
            let units = &cull_state.units;
            let count = cull_state.node_count;
            let index = &cull_state.index;
            let n = units.len();
            // Units to lay out: the visible range (binary search over the
            // prefix sums) plus pinned units (selection ends, IME).
            let (first, last) = match window_span {
                None => (0, n),
                Some((top, bottom)) => {
                    let first = index.find(top.max(px(0.))).min(n);
                    (first, (index.find(bottom) + 1).min(n).max(first))
                }
            };
            let mut wanted: Vec<usize> = keep
                .iter()
                .filter_map(|id| index.unit_of.get(id).copied())
                .filter(|ix| *ix < first || *ix >= last)
                .collect();
            wanted.extend(first..last);
            wanted.sort_unstable();
            wanted.dedup();
            let mut cursor = px(0.);
            let mut rendered = 0;
            for unit_ix in wanted {
                let unit = &units[unit_ix];
                let node_ix = unit.node_ix;
                let top = index.prefix(unit_ix);
                if top > cursor {
                    children.push(div().h(top - cursor).into_any_element());
                }
                cursor = top + index.height(unit_ix);
                rendered += 1;
                let flow = unit.flow;
                let is_last_node = node_ix + 1 == count;
                let node = &snapshot.blocks[unit.block].nodes[unit.node];
                let element = match (&unit.items, node) {
                    (
                        Some((range, ids)),
                        BlockNode::List {
                            children: items,
                            ordered,
                            start,
                            ..
                        },
                    ) => {
                        let last_chunk = range.end == ids.len();
                        let chunk = BlockNode::List {
                            children: items[range.clone()].to_vec(),
                            ordered: *ordered,
                            start: if *ordered {
                                Some(start.unwrap_or(1) + range.start as u32)
                            } else {
                                *start
                            },
                            span: None,
                        };
                        let flow = if range.start == 0 {
                            flow
                        } else {
                            flow.first(false)
                        };
                        chunk.render(
                            node_ix,
                            // Inner chunks drop the list's bottom gap.
                            flow.last(!last_chunk || is_last_node),
                            &node_cx,
                            window,
                            cx,
                        )
                    }
                    _ => node.render(node_ix, flow.last(is_last_node), &node_cx, window, cx),
                };
                // Per-block accessibility node around the rendered block.
                let element = match block_role(node) {
                    Some((role, level)) => div()
                        .id(("block", unit.id.as_u64() as usize))
                        .role(role)
                        .when_some(level, |this, level| this.aria_level(level))
                        .child(element)
                        .into_any_element(),
                    None => element,
                };
                children.push(
                    Measured {
                        id: unit.id,
                        index: unit_ix,
                        state: cull.clone(),
                        child: Some(element),
                    }
                    .into_any_element(),
                );
            }
            let total = index.prefix(n);
            if total > cursor {
                children.push(div().h(total - cursor).into_any_element());
            }
            cull_state.rendered = rendered;
        }

        let content_bounds = Rc::new(Cell::new(Bounds::default()));
        let measured_content = content_bounds.clone();
        let mut root_style = Style::default();
        root_style.refine(&self.style);
        let s = &self.state;
        div()
            .id("block-editor")
            // D8: one multi-line text input over the document; the value is
            // built only while accessibility is active (cached per revision).
            .role(Role::MultilineTextInput)
            .when(window.is_a11y_active() || cfg!(test), |this| {
                this.aria_value(self.state.read(cx).accessible_value())
            })
            .key_context(self.state.read(cx).key_context())
            .track_focus(&focus)
            .cursor(if self.state.read(cx).grip_hover {
                CursorStyle::OpenHand
            } else if self.state.read(cx).pointer_hand {
                CursorStyle::PointingHand
            } else {
                CursorStyle::IBeam
            })
            .w_full()
            .on_action(window.listener_for(s, BlockEditorState::on_backspace))
            .on_action(window.listener_for(s, BlockEditorState::on_delete))
            .on_action(window.listener_for(s, BlockEditorState::on_delete_word_left))
            .on_action(window.listener_for(s, BlockEditorState::on_delete_word_right))
            .on_action(window.listener_for(s, BlockEditorState::on_delete_to_line_start))
            .on_action(window.listener_for(s, BlockEditorState::on_delete_to_line_end))
            .on_action(window.listener_for(s, BlockEditorState::on_move_paragraph_start))
            .on_action(window.listener_for(s, BlockEditorState::on_move_paragraph_end))
            .on_action(window.listener_for(s, BlockEditorState::on_select_paragraph_start))
            .on_action(window.listener_for(s, BlockEditorState::on_select_paragraph_end))
            .on_action(window.listener_for(s, BlockEditorState::on_enter))
            .on_action(window.listener_for(s, BlockEditorState::on_structural_enter))
            .on_action(window.listener_for(s, BlockEditorState::on_soft_break))
            .on_action(window.listener_for(s, BlockEditorState::on_exit_block))
            .on_action(window.listener_for(s, BlockEditorState::on_move_left))
            .on_action(window.listener_for(s, BlockEditorState::on_move_right))
            .on_action(window.listener_for(s, BlockEditorState::on_move_up))
            .on_action(window.listener_for(s, BlockEditorState::on_move_down))
            .on_action(window.listener_for(s, BlockEditorState::on_move_home))
            .on_action(window.listener_for(s, BlockEditorState::on_move_end))
            .on_action(window.listener_for(s, BlockEditorState::on_select_left))
            .on_action(window.listener_for(s, BlockEditorState::on_move_word_left))
            .on_action(window.listener_for(s, BlockEditorState::on_move_word_right))
            .on_action(window.listener_for(s, BlockEditorState::on_select_word_left))
            .on_action(window.listener_for(s, BlockEditorState::on_select_word_right))
            .on_action(window.listener_for(s, BlockEditorState::on_select_right))
            .on_action(window.listener_for(s, BlockEditorState::on_select_up))
            .on_action(window.listener_for(s, BlockEditorState::on_select_down))
            .on_action(window.listener_for(s, BlockEditorState::on_select_home))
            .on_action(window.listener_for(s, BlockEditorState::on_select_end))
            .on_action(window.listener_for(s, BlockEditorState::on_move_doc_start))
            .on_action(window.listener_for(s, BlockEditorState::on_move_doc_end))
            .on_action(window.listener_for(s, BlockEditorState::on_select_doc_start))
            .on_action(window.listener_for(s, BlockEditorState::on_select_doc_end))
            .on_action(window.listener_for(s, BlockEditorState::on_select_all))
            .on_action(window.listener_for(s, BlockEditorState::on_undo))
            .on_action(window.listener_for(s, BlockEditorState::on_redo))
            .on_action(window.listener_for(s, BlockEditorState::on_normal_text))
            .on_action(window.listener_for(s, BlockEditorState::on_table_insert_column_left))
            .on_action(window.listener_for(s, BlockEditorState::on_table_insert_column_right))
            .on_action(window.listener_for(s, BlockEditorState::on_table_insert_row_above))
            .on_action(window.listener_for(s, BlockEditorState::on_table_insert_row_below))
            .on_action(window.listener_for(s, BlockEditorState::on_table_delete_line))
            .on_action(window.listener_for(s, BlockEditorState::on_close_table_menu))
            .on_action(window.listener_for(s, BlockEditorState::on_toggle_bold))
            .on_action(window.listener_for(s, BlockEditorState::on_toggle_italic))
            .on_action(window.listener_for(s, BlockEditorState::on_edit_link))
            .on_action(window.listener_for(s, BlockEditorState::on_toggle_code))
            .on_action(window.listener_for(s, BlockEditorState::on_indent))
            .on_action(window.listener_for(s, BlockEditorState::on_outdent))
            .on_action(window.listener_for(s, BlockEditorState::on_copy))
            .on_action(window.listener_for(s, BlockEditorState::on_cut))
            .on_action(window.listener_for(s, BlockEditorState::on_paste))
            .on_action(window.listener_for(s, BlockEditorState::on_native_paste))
            .on_action(window.listener_for(s, BlockEditorState::on_paste_plain))
            .children(super::table_ui::render_menu(&self.state, cx))
            .child(EditorRoot {
                state: self.state.clone(),
                child: Some(
                    div()
                        .w_full()
                        .refine_style(&self.style)
                        .child(
                            div().relative().w_full().children(children).child(
                                canvas(
                                    move |bounds, _, _| measured_content.set(bounds),
                                    |_, _, _, _| {},
                                )
                                .absolute()
                                .top_0()
                                .left_0()
                                .size_full(),
                            ),
                        )
                        .into_any_element(),
                ),
                content_bounds,
                padding: root_style.padding,
                typography,
                caret_was_visible,
                backgrounds: Some(backgrounds),
                snapshot,
                style,
            })
    }
}

type LeafBackgrounds = HashMap<u64, Vec<(Range<usize>, Hsla)>>;

struct EditorRoot {
    state: Entity<BlockEditorState>,
    child: Option<AnyElement>,
    backgrounds: Option<LeafBackgrounds>,
    snapshot: Rc<Snapshot>,
    style: BlockEditorStyle,
    content_bounds: Rc<Cell<Bounds<Pixels>>>,
    padding: Edges<DefiniteLength>,
    typography: TextStyle,
    caret_was_visible: bool,
}

impl IntoElement for EditorRoot {
    type Element = Self;
    fn into_element(self) -> Self::Element {
        self
    }
}

pub(crate) struct Prepaint {
    hitbox: Hitbox,
}

impl EditorRoot {
    /// Scroll the caret into view in the owner's scroll container, once its
    /// block has been laid out (a culled block is pinned, so it is laid out
    /// in the frame after the caret moved there).
    fn reveal_caret(&self, bounds: Bounds<Pixels>, window: &mut Window, cx: &mut App) {
        let state = self.state.read(cx);
        if !state.reveal.get() {
            return;
        }
        let (Some(handle), Some(pos)) = (state.scroll_handle.clone(), state.model.caret()) else {
            state.reveal.set(false);
            return;
        };
        let Some(caret) = state
            .frame
            .borrow()
            .caret_bounds(pos, self.style.caret_width())
        else {
            return;
        };
        state.reveal.set(false);
        // The scroll owner stays at the region edge; text and the revealed
        // caret stay inside the root's presentation inset at either scroll end.
        let padding = self
            .padding
            .to_pixels(bounds.size.map(Into::into), window.rem_size());
        let mut view = handle.bounds();
        view.origin.y += padding.top;
        view.size.height = (view.size.height - padding.top - padding.bottom).max(Pixels::ZERO);
        let mut offset = handle.offset();
        if caret.top() < view.top() {
            offset.y += view.top() - caret.top();
        } else if caret.bottom() > view.bottom() {
            offset.y -= caret.bottom() - view.bottom();
        } else {
            return;
        }
        handle.set_offset(offset);
        window.refresh();
    }
}

impl Element for EditorRoot {
    type RequestLayoutState = AnyElement;
    type PrepaintState = Prepaint;

    fn id(&self) -> Option<ElementId> {
        None
    }

    fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
        None
    }

    fn request_layout(
        &mut self,
        _: Option<&GlobalElementId>,
        _: Option<&InspectorElementId>,
        window: &mut Window,
        cx: &mut App,
    ) -> (LayoutId, AnyElement) {
        let mut child = self
            .child
            .take()
            .unwrap_or_else(|| div().into_any_element());
        let layout_id = child.request_layout(window, cx);
        (layout_id, child)
    }

    fn prepaint(
        &mut self,
        _: Option<&GlobalElementId>,
        _: Option<&InspectorElementId>,
        bounds: Bounds<Pixels>,
        child: &mut AnyElement,
        window: &mut Window,
        cx: &mut App,
    ) -> Prepaint {
        if let Some(backgrounds) = self.backgrounds.take() {
            let (_, geometries) = collect_leaf_geometry(backgrounds, || child.prepaint(window, cx));
            // Swap in the complete frame; handlers read it until the next one.
            let mut frame = super::frame::into_frame(
                geometries,
                self.snapshot.leaves.clone(),
                &self.snapshot.tables,
            );
            frame.content = Some(self.content_bounds.get());
            {
                let state = self.state.read(cx);
                let doc = &state.model.doc;
                let shapes: Vec<super::frame::TableShape> = self
                    .snapshot
                    .tables
                    .iter()
                    .map(|(table, _)| super::frame::TableShape {
                        table: *table,
                        grid: super::table::grid(doc, *table),
                        aligns: match doc.kind(*table) {
                            Some(super::kind::BlockKindData::Table { aligns }) => aligns.clone(),
                            _ => Vec::new(),
                        },
                    })
                    .collect();
                // gpui-kit's cell padding is `px_3`: 0.75rem.
                let pad = window.rem_size() * 0.75;
                let right = frame.content.map(|c| c.right());
                frame.layout_tables(&shapes, pad, right);
            }
            *self.state.read(cx).frame.borrow_mut() = frame;
            let mask = window.content_mask().bounds;
            let content = self.content_bounds.get();
            let cull = self.state.read(cx).cull.clone();
            let geometry_changed = cull.borrow_mut().set_geometry(
                content.size.width,
                window.rem_size(),
                &self.typography,
            );
            // Retain a visible caret or an existing reveal request, but do not
            // interrupt manual scrolling. Wait for fresh spacer positions.
            if geometry_changed {
                if self.caret_was_visible {
                    self.state.read(cx).reveal.set(true);
                }
            } else {
                self.reveal_caret(bounds, window, cx);
            }
            if geometry_changed || cull.borrow_mut().finish_frame(content, mask) {
                // Geometry changed or scrolling left the laid-out span.
                let state = self.state.clone();
                window.on_next_frame(move |_, cx| state.update(cx, |_, cx| cx.notify()));
            }
        } else {
            child.prepaint(window, cx);
        }
        Prepaint {
            hitbox: window.insert_hitbox(bounds, HitboxBehavior::Normal),
        }
    }

    fn paint(
        &mut self,
        _: Option<&GlobalElementId>,
        _: Option<&InspectorElementId>,
        bounds: Bounds<Pixels>,
        child: &mut AnyElement,
        prepaint: &mut Prepaint,
        window: &mut Window,
        cx: &mut App,
    ) {
        let state = self.state.read(cx);
        let focus = state.focus_handle.clone();
        let collapsed = state.selection_ranges().is_empty();
        let marked = state.marked_range();
        let caret = state.model.caret();
        let show_caret = state.show_caret(window, cx) && collapsed;
        let frame = state.frame.clone();
        let menu_line = state.table_menu.as_ref().map(|m| (m.table, m.line));
        let grips = state.table_grips_at(window.mouse_position());
        // Node selection (a table, code block, โ€ฆ): the blocks' laid-out area.
        let selected_area = state.model.selected_blocks().and_then(|(parent, range)| {
            let doc = &state.model.doc;
            let ids = doc.children(parent).get(range)?.to_vec();
            let frame = frame.borrow();
            frame
                .leaves
                .iter()
                .filter(|leaf| {
                    let path = doc.path(leaf.info.block);
                    ids.iter()
                        .any(|id| *id == leaf.info.block || path.contains(id))
                })
                .map(|leaf| leaf.bounds())
                .reduce(|a, b| a.union(&b))
        });

        window.handle_input(
            &focus,
            ElementInputHandler::new(bounds, self.state.clone()),
            cx,
        );
        child.paint(window, cx);

        let frame = frame.borrow();
        let style = &self.style;
        // Block handle: a grip in the left edge of the hovered table or code
        // block; clicking the edge node-selects it (`block_handle_at`).
        let mouse = window.mouse_position();
        let hovered = frame
            .tables
            .iter()
            .map(|(_, b)| *b)
            .chain(
                frame
                    .leaves
                    .iter()
                    .filter(|l| state_plain(&self.state, l.info.block, cx))
                    .map(|l| l.bounds()),
            )
            .find(|b| {
                mouse.y >= b.top()
                    && mouse.y < b.bottom()
                    && mouse.x >= b.left() - px(24.)
                    && mouse.x < b.right()
            });
        if let Some(b) = hovered {
            let grip = Bounds::new(
                point(b.left() - px(14.), b.top() + px(2.)),
                size(px(4.), px(16.).min(b.size.height)),
            );
            window.paint_quad(fill(grip, style.caret().opacity(0.35)).corner_radii(px(2.)));
        }
        // Table grips (D20): only for the hovered column / row, or the
        // line whose menu is open.
        for (table, line, grip) in grips {
            let open = menu_line == Some((table, line));
            let alpha = if open { 0.8 } else { 0.35 };
            window.paint_quad(fill(grip, style.caret().opacity(alpha)).corner_radii(px(2.)));
        }
        if let Some(area) = selected_area {
            let area = area.dilate(px(4.));
            window.paint_quad(gpui::quad(
                area,
                px(4.),
                style.selection().opacity(0.35),
                px(2.),
                style.caret(),
                gpui::BorderStyle::Solid,
            ));
        }
        // D22: where a dragged block would drop.
        if let Some(line) = self.state.read(cx).drop_indicator() {
            window.paint_quad(fill(line, style.caret()).corner_radii(px(1.)));
        }
        if let Some((block, range)) = marked
            && let Some(leaf) = frame.leaf(block)
        {
            let a = leaf.caret_point(range.start);
            let b = leaf.caret_point(range.end);
            let thickness = style.caret_width();
            let underline = Bounds::new(
                point(a.x, a.y + leaf.line_height() - thickness),
                size((b.x - a.x).max(thickness), thickness),
            );
            window.paint_quad(fill(underline, style.composition_underline()));
        }
        if show_caret
            && let Some(pos) = caret
            && let Some(b) = frame.caret_bounds(pos, style.caret_width())
        {
            // Whole device pixels, like the input caret: a 1px line at a
            // fractional x would blur over two pixels.
            let b = crate::support::snap_caret_to_device_pixels(b, window.scale_factor());
            window.paint_quad(fill(b, style.caret()));
        }
        drop(frame);

        // Mouse: handlers update the entity at event time, never in paint.
        let hitbox = prepaint.hitbox.clone();
        let entity = self.state.clone();
        window.on_mouse_event(move |event: &MouseDownEvent, phase, window, cx| {
            if phase != DispatchPhase::Bubble
                || event.button != MouseButton::Left
                || !hitbox.is_hovered(window)
            {
                return;
            }
            // A table grip selects its row / column and opens the menu;
            // any other click closes an open menu.
            if !event.modifiers.shift
                && entity.update(cx, |state, cx| {
                    let hit = state.click_table_grip(event.position, cx);
                    if !hit {
                        state.close_table_menu(cx);
                    }
                    hit
                })
            {
                let focus = entity.read(cx).focus_handle.clone();
                window.focus(&focus, cx);
                cx.stop_propagation();
                return;
            }
            // A task checkbox toggles without moving the caret or starting
            // a drag; nothing else handles this mouse-down.
            if !event.modifiers.shift
                && entity.update(cx, |state, cx| state.click_checkbox(event.position, cx))
            {
                cx.stop_propagation();
                return;
            }
            entity.update(cx, |state, cx| {
                let focus = state.focus_handle.clone();
                window.focus(&focus, cx);
                if event.modifiers.secondary() && state.follow_link_at(event.position, cx) {
                    return;
                }
                state.dragging = true;
                if !event.modifiers.shift {
                    state.arm_block_drag(event.position);
                }
                state.mouse_at(event.position, event.modifiers.shift, cx);
                if !event.modifiers.shift && event.click_count >= 2 {
                    state.select_unit(event.click_count >= 3, cx);
                }
            });
        });
        let entity = self.state.clone();
        window.on_mouse_event(move |event: &MouseMoveEvent, phase, _, cx| {
            if phase != DispatchPhase::Bubble {
                return;
            }
            if event.pressed_button.is_none() {
                entity.update(cx, |state, cx| {
                    state.hover_at(event.position, event.modifiers.secondary(), cx)
                });
                return;
            }
            if event.pressed_button != Some(MouseButton::Left) {
                return;
            }
            entity.update(cx, |state, cx| {
                if state.block_drag_to(event.position, cx) {
                    return;
                }
                if state.dragging {
                    state.mouse_at(event.position, true, cx);
                }
            });
        });
        let entity = self.state.clone();
        window.on_mouse_event(move |_: &MouseUpEvent, phase, _, cx| {
            if phase == DispatchPhase::Bubble {
                entity.update(cx, |state, cx| {
                    state.dragging = false;
                    state.finish_block_drag(cx);
                });
            }
        });
    }
}

fn state_plain(state: &Entity<BlockEditorState>, block: BlockId, cx: &App) -> bool {
    state.read(cx).model.doc.plain(block).is_some()
}