Skip to main content

common/
format_runs_query.rs

1//! Store-aware readers that synthesize inline content views from
2//! per-block `format_runs` + `block_images`. The canonical entry
3//! point is [`inline_segments_for_block`], which returns the
4//! `Vec<InlineSegment>` view used by export, fragments, cursor, and
5//! tests.
6
7use crate::database::Store;
8use crate::format_runs::{FormatRun, ImageAnchor, InlineSegment, inline_segments_view};
9use crate::types::EntityId;
10
11/// Fetch the format runs for a block. Returns an empty Vec if the block
12/// has no runs (treated the same as a missing entry).
13pub fn get_format_runs(store: &Store, block_id: EntityId) -> Vec<FormatRun> {
14    store
15        .format_runs
16        .read()
17        .unwrap()
18        .get(&block_id)
19        .cloned()
20        .unwrap_or_default()
21}
22
23/// Fetch the image anchors for a block.
24pub fn get_block_images(store: &Store, block_id: EntityId) -> Vec<ImageAnchor> {
25    store
26        .block_images
27        .read()
28        .unwrap()
29        .get(&block_id)
30        .cloned()
31        .unwrap_or_default()
32}
33
34/// Synthesize the `Vec<InlineSegment>` view for a block from its
35/// format_runs and block_images. Callers must pass the block's
36/// `plain_text` (which they already have in scope from a prior
37/// `get_block` call) — this avoids re-locking the blocks table.
38///
39/// This is the Phase 1.14b-and-forward reader function. Returns segments
40/// in document order.
41pub fn inline_segments_for_block(
42    store: &Store,
43    block_id: EntityId,
44    block_plain_text: &str,
45) -> Vec<InlineSegment> {
46    let runs = get_format_runs(store, block_id);
47    let images = get_block_images(store, block_id);
48    inline_segments_view(block_plain_text, &runs, &images)
49}