bezel-markdown 0.2.21

A Notion-style block document model for gpui — markdown in, markdown out
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
//! A range scrolled into view and a picture as a control, under gpui's own
//! harness — both are answers only a real layout gives.

use std::{cell::RefCell, rc::Rc};

use gpui::{
    App, Context, Modifiers, MouseButton, Render, ScrollHandle, TestAppContext, VisualTestContext,
    Window, div, point, prelude::*, px, size,
};
use markdown::{
    BlockLayouts, Cursor, Doc, Editing, ImageOverlay, OnImage, OnImageMenu, Part, Selection,
    SourceText, parse, render_source, render_with,
};

const WIDTH: f32 = 320.0;
const HEIGHT: f32 = 400.0;

struct Page {
    doc: Doc,
    source: Option<String>,
    layouts: BlockLayouts,
    scroll: ScrollHandle,
    /// Every block the image hook was called with, in order.
    clicked: Rc<RefCell<Vec<usize>>>,
    /// Every block the image menu hook was called with, in order.
    menued: Rc<RefCell<Vec<usize>>>,
    hooked: bool,
    overlay: Option<ImageOverlay>,
    overlay_anchor: markdown::ImageOverlayCorner,
    presses: usize,
    virtualized: bool,
}

impl Render for Page {
    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        let clicked = self.clicked.clone();
        let image = self.hooked.then(|| {
            Rc::new(move |ix: usize, _: &mut Window, _: &mut App| clicked.borrow_mut().push(ix))
                as OnImage
        });
        let menued = self.menued.clone();
        let image_menu = self.hooked.then(|| {
            Rc::new(move |ix: usize, _, _: &mut Window, _: &mut App| menued.borrow_mut().push(ix))
                as OnImageMenu
        });
        let editing = Editing {
            layouts: self.virtualized.then_some(&self.layouts),
            scroll: Some(&self.scroll),
            image,
            image_menu,
            image_overlay: self.overlay.clone(),
            image_overlay_corner: self.overlay_anchor,
            ..Editing::default()
        };
        let body = match &self.source {
            Some(source) => render_source(&SourceText::new(source.as_str(), cx), editing, cx),
            None => render_with(&self.doc, editing, window, cx),
        };
        div()
            .id("page")
            .w(px(WIDTH))
            .h(px(HEIGHT))
            .overflow_y_scroll()
            .track_scroll(&self.scroll)
            .on_any_mouse_down(cx.listener(|this, _, _, _| this.presses += 1))
            .child(body)
    }
}

fn open(
    source: &str,
    as_source: bool,
    hooked: bool,
    cx: &mut TestAppContext,
) -> (gpui::Entity<Page>, VisualTestContext) {
    cx.update(|cx| theme::Theme::install(theme::Appearance::Dark, cx));
    let window = cx.add_window(|_, _| Page {
        doc: parse(source),
        source: as_source.then(|| source.to_string()),
        layouts: BlockLayouts::default(),
        scroll: ScrollHandle::new(),
        clicked: Rc::new(RefCell::new(Vec::new())),
        menued: Rc::new(RefCell::new(Vec::new())),
        hooked,
        overlay: None,
        overlay_anchor: markdown::ImageOverlayCorner::BottomRight,
        presses: 0,
        virtualized: true,
    });
    let page = window.root(cx).unwrap();
    let mut visual = VisualTestContext::from_window(window.into(), cx);
    visual.simulate_resize(size(px(WIDTH), px(HEIGHT)));
    settle(&mut visual);
    (page, visual)
}

fn settle(cx: &mut VisualTestContext) {
    for _ in 0..8 {
        cx.update(|window, _| window.refresh());
        cx.run_until_parked();
    }
}

fn paragraphs(count: usize) -> String {
    (0..count)
        .map(|ix| format!("paragraph {ix}"))
        .collect::<Vec<_>>()
        .join("\n\n")
}

fn shown(page: &gpui::Entity<Page>, range: Selection, cx: &mut VisualTestContext) -> bool {
    let rows = cx.update(|_, cx| page.read(cx).layouts.rects(range));
    rows.first()
        .is_some_and(|row| row.top() >= px(0.0) && row.bottom() <= px(HEIGHT))
}

#[gpui::test]
fn a_reveal_scrolls_a_block_never_built_into_view(cx: &mut TestAppContext) {
    let (page, mut cx) = open(&paragraphs(300), false, false, cx);
    let range = Selection::new(
        Cursor::new(250, Part::Body, 0),
        Cursor::new(250, Part::Body, 9),
    );
    assert!(!shown(&page, range, &mut cx), "the block starts off-screen");

    cx.update(|_, cx| page.read(cx).layouts.reveal(range));
    settle(&mut cx);

    assert!(shown(&page, range, &mut cx), "the range is on screen");
}

#[gpui::test]
fn a_reveal_scrolls_back_up(cx: &mut TestAppContext) {
    let (page, mut cx) = open(&paragraphs(300), false, false, cx);
    let (low, high) = (
        Selection::new(
            Cursor::new(280, Part::Body, 0),
            Cursor::new(280, Part::Body, 9),
        ),
        Selection::new(Cursor::new(3, Part::Body, 0), Cursor::new(3, Part::Body, 9)),
    );
    cx.update(|_, cx| page.read(cx).layouts.reveal(low));
    settle(&mut cx);
    cx.update(|_, cx| page.read(cx).layouts.reveal(high));
    settle(&mut cx);

    assert!(shown(&page, high, &mut cx), "the range is on screen");
}

#[gpui::test]
fn a_nearest_reveal_stops_at_the_bottom_edge(cx: &mut TestAppContext) {
    let (page, mut cx) = open(&paragraphs(300), false, false, cx);
    let range = Selection::new(
        Cursor::new(40, Part::Body, 0),
        Cursor::new(40, Part::Body, 9),
    );
    cx.update(|_, cx| {
        page.read(cx)
            .layouts
            .reveal_with(range, markdown::RevealMode::Nearest)
    });
    settle(&mut cx);

    let rows = cx.update(|_, cx| page.read(cx).layouts.rects(range));
    let bottom = rows.first().expect("the range painted").bottom();
    assert!(
        bottom <= px(HEIGHT - 16.0) && bottom > px(HEIGHT - 40.0),
        "the range sits just inside the bottom, at {bottom:?}"
    );
}

#[gpui::test]
fn a_nearest_reveal_leaves_a_shown_range_alone(cx: &mut TestAppContext) {
    let (page, mut cx) = open(&paragraphs(300), false, false, cx);
    let range = Selection::new(Cursor::new(1, Part::Body, 0), Cursor::new(1, Part::Body, 9));
    let rects = |cx: &mut VisualTestContext| cx.update(|_, cx| page.read(cx).layouts.rects(range));
    let before = rects(&mut cx);
    cx.update(|_, cx| {
        page.read(cx)
            .layouts
            .reveal_with(range, markdown::RevealMode::Nearest)
    });
    settle(&mut cx);

    assert_eq!(rects(&mut cx), before);
}

#[gpui::test]
fn a_reveal_in_source_finds_its_line(cx: &mut TestAppContext) {
    let source = paragraphs(300);
    let offset = source.find("paragraph 260").unwrap();
    let (page, mut cx) = open(&source, true, false, cx);
    let range = Selection::new(
        Cursor::new(0, Part::Code, offset),
        Cursor::new(0, Part::Code, offset + 13),
    );

    cx.update(|_, cx| page.read(cx).layouts.reveal(range));
    settle(&mut cx);

    assert!(shown(&page, range, &mut cx), "the line is on screen");
}

fn picture(page: &gpui::Entity<Page>, cx: &mut VisualTestContext) -> gpui::Bounds<gpui::Pixels> {
    cx.update(|_, cx| page.read(cx).layouts.picture_bounds(1))
        .expect("the picture painted")
}

fn clicked(page: &gpui::Entity<Page>, cx: &mut VisualTestContext) -> Vec<usize> {
    cx.update(|_, cx| page.read(cx).clicked.borrow().clone())
}

/// A 1×1 PNG, which a stated width scales to a square.
const PIXEL: &[u8] = &[
    137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 1, 0, 0, 0, 1, 8, 6, 0,
    0, 0, 31, 21, 196, 137, 0, 0, 0, 13, 73, 68, 65, 84, 120, 218, 99, 100, 96, 248, 95, 15, 0, 2,
    135, 1, 128, 235, 71, 186, 146, 0, 0, 0, 0, 73, 69, 78, 68, 174, 66, 96, 130,
];

/// A picture between two paragraphs, as block 1.
fn picture_doc() -> String {
    let path = std::env::temp_dir().join(format!("bezel-find-{}.png", std::process::id()));
    std::fs::write(&path, PIXEL).unwrap();
    format!("before\n\n![a picture|200]({})\n\nafter", path.display())
}

#[gpui::test]
fn a_click_on_a_picture_calls_the_hook(cx: &mut TestAppContext) {
    let (page, mut cx) = open(&picture_doc(), false, true, cx);

    let center = picture(&page, &mut cx).center();
    cx.simulate_click(center, Modifiers::default());

    assert_eq!(clicked(&page, &mut cx), vec![1]);
}

#[gpui::test]
fn a_drag_over_a_picture_does_not(cx: &mut TestAppContext) {
    let (page, mut cx) = open(&picture_doc(), false, true, cx);

    let from = picture(&page, &mut cx).center();
    let to = from + point(px(20.0), px(0.0));
    cx.simulate_mouse_down(from, MouseButton::Left, Modifiers::default());
    cx.simulate_mouse_move(to, MouseButton::Left, Modifiers::default());
    cx.simulate_mouse_up(to, MouseButton::Left, Modifiers::default());

    assert!(clicked(&page, &mut cx).is_empty());
}

#[gpui::test]
fn a_picture_without_a_hook_takes_nothing(cx: &mut TestAppContext) {
    let (page, mut cx) = open(&picture_doc(), false, false, cx);

    let center = picture(&page, &mut cx).center();
    cx.simulate_click(center, Modifiers::default());
    cx.simulate_mouse_down(center, MouseButton::Right, Modifiers::default());

    assert!(clicked(&page, &mut cx).is_empty());
    assert_eq!(cx.update(|_, cx| page.read(cx).presses), 2);
}

#[gpui::test]
fn a_right_press_on_a_picture_calls_the_menu_hook_and_stops(cx: &mut TestAppContext) {
    let (page, mut cx) = open(&picture_doc(), false, true, cx);

    let center = picture(&page, &mut cx).center();
    cx.simulate_mouse_down(center, MouseButton::Right, Modifiers::default());

    assert_eq!(
        cx.update(|_, cx| page.read(cx).menued.borrow().clone()),
        vec![1]
    );
    assert!(clicked(&page, &mut cx).is_empty());
    assert_eq!(cx.update(|_, cx| page.read(cx).presses), 0);
}

#[gpui::test]
fn the_picture_overlay_is_inset_and_owns_its_presses(cx: &mut TestAppContext) {
    for (virtualized, anchor) in [true, false].into_iter().flat_map(|virtualized| {
        [
            markdown::ImageOverlayCorner::TopLeft,
            markdown::ImageOverlayCorner::TopRight,
            markdown::ImageOverlayCorner::BottomLeft,
            markdown::ImageOverlayCorner::BottomRight,
        ]
        .map(|anchor| (virtualized, anchor))
    }) {
        let doc = picture_doc();
        let (page, mut cx) = open(&doc, false, true, cx);
        let original = picture(&page, &mut cx);
        let painted = Rc::new(RefCell::new(None));
        let hits = Rc::new(RefCell::new(0));
        let bounds = painted.clone();
        let clicks = hits.clone();
        cx.update(|_, cx| {
            page.update(cx, |page, cx| {
                page.virtualized = virtualized;
                page.overlay_anchor = anchor;
                page.overlay = Some(Rc::new(move |ix, url, _, _| {
                    assert_eq!(ix, 1);
                    assert!(url.ends_with(".png"));
                    let bounds = bounds.clone();
                    let clicks = clicks.clone();
                    Some(
                        div()
                            .id("app-picture-control")
                            .size(px(24.0))
                            .on_click(move |_, _, _| *clicks.borrow_mut() += 1)
                            .child(
                                gpui::canvas(
                                    |_, _, _| (),
                                    move |rect, _, _, _| {
                                        *bounds.borrow_mut() = Some(rect);
                                    },
                                )
                                .size_full(),
                            )
                            .into_any_element(),
                    )
                }));
                cx.notify();
            })
        });
        settle(&mut cx);
        assert!(painted.borrow().is_none(), "hidden before hovering");
        assert_eq!(picture(&page, &mut cx), original);
        cx.simulate_mouse_move(original.center(), None, Modifiers::default());
        settle(&mut cx);
        let control = painted.borrow().expect("painted on picture hover");
        let horizontal = match anchor {
            markdown::ImageOverlayCorner::TopLeft | markdown::ImageOverlayCorner::BottomLeft => {
                control.left() - original.left()
            }
            _ => original.right() - control.right(),
        };
        let vertical = match anchor {
            markdown::ImageOverlayCorner::TopLeft | markdown::ImageOverlayCorner::TopRight => {
                control.top() - original.top()
            }
            _ => original.bottom() - control.bottom(),
        };
        assert!((horizontal - px(6.0)).abs() < px(1.0));
        assert!((vertical - px(6.0)).abs() < px(1.0));
        cx.simulate_click(control.center(), Modifiers::default());
        assert_eq!(*hits.borrow(), 1);
        assert!(clicked(&page, &mut cx).is_empty());
        assert_eq!(cx.update(|_, cx| page.read(cx).presses), 0);
        *painted.borrow_mut() = None;
        cx.simulate_mouse_move(point(px(310.0), px(390.0)), None, Modifiers::default());
        settle(&mut cx);
        assert!(painted.borrow().is_none(), "hidden after leaving picture");
        cx.simulate_click(original.center(), Modifiers::default());
        assert_eq!(clicked(&page, &mut cx), vec![1]);
    }
}

#[gpui::test]
fn declining_an_overlay_leaves_picture_presses_alone(cx: &mut TestAppContext) {
    let (page, mut cx) = open(&picture_doc(), false, true, cx);
    let original = picture(&page, &mut cx);
    cx.update(|_, cx| {
        page.update(cx, |page, cx| {
            page.overlay = Some(Rc::new(|_, _, _, _| None));
            cx.notify();
        })
    });
    settle(&mut cx);
    assert_eq!(picture(&page, &mut cx), original);
    cx.simulate_click(original.center(), Modifiers::default());
    assert_eq!(clicked(&page, &mut cx), vec![1]);
    assert_eq!(cx.update(|_, cx| page.read(cx).presses), 1);
}

/// A picture `width` wide with no width stated, as block 1.
fn unsized_doc(width: u32) -> String {
    let path =
        std::env::temp_dir().join(format!("bezel-unsized-{}-{width}.svg", std::process::id()));
    let svg = format!(r#"<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="20"/>"#);
    std::fs::write(&path, svg).unwrap();
    format!("before\n\n![a picture]({})\n\nafter", path.display())
}

#[gpui::test]
fn an_unsized_picture_s_frame_hugs_it(cx: &mut TestAppContext) {
    let (page, mut cx) = open(&unsized_doc(40), false, false, cx);

    assert!(picture(&page, &mut cx).size.width < px(60.0));
}

#[gpui::test]
fn an_unsized_picture_wider_than_the_page_stays_on_it(cx: &mut TestAppContext) {
    let (page, mut cx) = open(&unsized_doc(1000), false, false, cx);

    assert!(picture(&page, &mut cx).size.width <= px(WIDTH));
}

#[gpui::test]
fn a_picture_narrowed_to_the_page_keeps_its_proportions(cx: &mut TestAppContext) {
    let (page, mut cx) = open(&unsized_doc(1000), false, false, cx);

    let frame = picture(&page, &mut cx).size;
    assert!(frame.height < px(12.0), "{frame:?}");
}