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        .get(&block_id)
18        .cloned()
19        .unwrap_or_default()
20}
21
22/// Fetch the image anchors for a block.
23pub fn get_block_images(store: &Store, block_id: EntityId) -> Vec<ImageAnchor> {
24    store
25        .block_images
26        .read()
27        .get(&block_id)
28        .cloned()
29        .unwrap_or_default()
30}
31
32/// Synthesize the `Vec<InlineSegment>` view for a block from its
33/// format_runs and block_images. Callers must pass the block's
34/// `plain_text` (which they already have in scope from a prior
35/// `get_block` call) — this avoids re-locking the blocks table.
36///
37/// This is the Phase 1.14b-and-forward reader function. Returns segments
38/// in document order.
39pub fn inline_segments_for_block(
40    store: &Store,
41    block_id: EntityId,
42    block_plain_text: &str,
43) -> Vec<InlineSegment> {
44    let runs = get_format_runs(store, block_id);
45    let images = get_block_images(store, block_id);
46    inline_segments_view(block_plain_text, &runs, &images)
47}