betteroffice-pptx-edit 0.3.0

Collaborative yrs-backed editing core for PPTX decks.
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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
use std::sync::Arc;

use ooxml_drawingml::{Theme, resolve_color_value_to_hex_with_theme};
use pptx_parse::{RunProperties, TextBody};
use yrs::branch::{Branch, BranchPtr};
use yrs::types::Attrs;
use yrs::types::text::YChange;
use yrs::{
    Any, Assoc, IndexedSequence, Map, MapPrelim, MapRef, Out, ReadTxn, StickyIndex, Text,
    TextPrelim, TextRef, Transact, TransactionMut,
};

use crate::model::validate_xml_text;
use crate::{
    CaretAnchor, DeckSession, EditError, EditResult, KIND, PARA_ID, PILCROW_KIND,
    ParagraphSnapshot, STORIES, StorySnapshot, TextCaps, TextReceipt, TextRunSnapshot, TextStyle,
    TextStylePatch,
};

const UNDERLINE_TYPES: [&str; 18] = [
    "none",
    "words",
    "sng",
    "dbl",
    "heavy",
    "dotted",
    "dottedHeavy",
    "dash",
    "dashHeavy",
    "dashLong",
    "dashLongHeavy",
    "dotDash",
    "dotDashHeavy",
    "dotDotDash",
    "dotDotDashHeavy",
    "wavy",
    "wavyHeavy",
    "wavyDbl",
];

const ALIGNMENTS: [&str; 7] = ["l", "ctr", "r", "just", "justLow", "dist", "thaiDist"];

/// The values land in schema-typed attributes, so junk must fail the edit
/// rather than the file.
pub(crate) fn validate_style_values(
    font_family: Option<&str>,
    underline: Option<&str>,
    color: Option<&str>,
    font_size_pt: Option<f64>,
    spacing_pt: Option<f64>,
    baseline_pct: Option<f64>,
) -> EditResult<()> {
    if let Some(font_family) = font_family {
        validate_xml_text(font_family)?;
    }
    if let Some(underline) = underline
        && !UNDERLINE_TYPES.contains(&underline)
    {
        return Err(EditError::InvalidText(format!(
            "unrecognized underline type {underline:?}"
        )));
    }
    if let Some(color) = color {
        let rgb = color.strip_prefix('#').unwrap_or(color);
        if rgb.len() != 6 || !rgb.bytes().all(|byte| byte.is_ascii_hexdigit()) {
            return Err(EditError::InvalidText(format!(
                "color {color:?} must be a six-digit hex value"
            )));
        }
    }
    if let Some(size) = font_size_pt
        && (!size.is_finite() || !(1.0..=4_000.0).contains(&size))
    {
        return Err(EditError::InvalidText(format!(
            "font size {size}pt is outside the 1-4000pt range"
        )));
    }
    if let Some(spacing) = spacing_pt
        && (!spacing.is_finite() || !(-4_000.0..=4_000.0).contains(&spacing))
    {
        return Err(EditError::InvalidText(format!(
            "letter spacing {spacing}pt is outside the -4000-4000pt range"
        )));
    }
    if let Some(baseline) = baseline_pct
        && (!baseline.is_finite()
            || !(f64::from(i32::MIN)..=f64::from(i32::MAX)).contains(&(baseline * 1000.0)))
    {
        return Err(EditError::InvalidText(
            "baseline exceeds the signed percentage range".into(),
        ));
    }
    Ok(())
}

pub(crate) fn validate_alignment(alignment: Option<&str>) -> EditResult<()> {
    if let Some(alignment) = alignment
        && !ALIGNMENTS.contains(&alignment)
    {
        return Err(EditError::InvalidText(format!(
            "unrecognized paragraph alignment {alignment:?}"
        )));
    }
    Ok(())
}

pub(crate) fn seed_story(
    stories: &MapRef,
    txn: &mut TransactionMut<'_>,
    story_id: &str,
    body: &TextBody,
    theme: Option<&Theme>,
) -> EditResult<()> {
    let story = stories.insert(txn, story_id, TextPrelim::new(""));
    if body.paragraphs.is_empty() {
        append_pilcrow(&story, txn, &format!("para:{story_id}:0"), None, 0, None);
        return Ok(());
    }
    for (paragraph_index, paragraph) in body.paragraphs.iter().enumerate() {
        for run in &paragraph.runs {
            if !run.text.is_empty() {
                let style = style_from_run_properties(&run.properties, theme);
                let index = story.len(txn);
                insert_styled_text(&story, txn, index, &run.text, &style);
            }
        }
        let bullet_json = paragraph
            .properties
            .bullet
            .as_ref()
            .map(serde_json::to_string)
            .transpose()
            .map_err(|error| EditError::Json(error.to_string()))?;
        append_pilcrow(
            &story,
            txn,
            &format!("para:{story_id}:{paragraph_index}"),
            paragraph.properties.alignment.as_deref(),
            paragraph.properties.level,
            bullet_json.as_deref(),
        );
    }
    Ok(())
}

pub(crate) fn seed_plain_story(
    stories: &MapRef,
    txn: &mut TransactionMut<'_>,
    story_id: &str,
    paragraph_id: &str,
    text: &str,
    style: &TextStyle,
) -> TextRef {
    let story = stories.insert(txn, story_id, TextPrelim::new(""));
    if !text.is_empty() {
        insert_styled_text(&story, txn, 0, text, style);
    }
    append_pilcrow(&story, txn, paragraph_id, None, 0, None);
    story
}

pub(crate) fn import_source_numbering_restarts(
    doc: &yrs::Doc,
    package: &pptx_parse::PptxPackage,
) -> EditResult<()> {
    let source = crate::doc_with_client_id(crate::BOOTSTRAP_CLIENT_ID);
    crate::deck::seed_doc(&source, package, "")?;
    let source_txn = source.transact();
    let source_stories = crate::deck::required_map(&source_txn, STORIES)?;
    let mut restarts = std::collections::HashMap::new();
    for (_, value) in source_stories.iter(&source_txn) {
        let Out::YText(story) = value else { continue };
        for diff in story.diff(&source_txn, YChange::identity) {
            let Out::YMap(map) = diff.insert else {
                continue;
            };
            let (Some(id), Some(json)) = (
                map_string(&map, &source_txn, PARA_ID),
                map_string(&map, &source_txn, "bulletJson"),
            ) else {
                continue;
            };
            let mut bullet: pptx_parse::Bullet =
                serde_json::from_str(&json).map_err(|error| EditError::Json(error.to_string()))?;
            if let pptx_parse::Bullet::AutoNumber {
                restart: restart @ true,
                ..
            } = &mut bullet
            {
                *restart = false;
                restarts.insert(id, (bullet, json));
            }
        }
    }
    if restarts.is_empty() {
        return Ok(());
    }
    let mut txn = doc.transact_mut_with(crate::MIGRATE_ORIGIN);
    let stories = crate::deck::required_map(&txn, STORIES)?;
    let mut updates = Vec::new();
    for (_, value) in stories.iter(&txn) {
        let Out::YText(story) = value else { continue };
        for diff in story.diff(&txn, YChange::identity) {
            let Out::YMap(map) = diff.insert else {
                continue;
            };
            let Some(id) = map_string(&map, &txn, PARA_ID) else {
                continue;
            };
            let Some((legacy, source)) = restarts.get(&id) else {
                continue;
            };
            let current = map_string(&map, &txn, "bulletJson")
                .and_then(|json| serde_json::from_str::<pptx_parse::Bullet>(&json).ok());
            if current.as_ref() == Some(legacy) {
                updates.push((map, source.clone()));
            }
        }
    }
    for (map, json) in updates {
        map.insert(&mut txn, "bulletJson", json);
    }
    Ok(())
}

fn append_pilcrow(
    story: &TextRef,
    txn: &mut TransactionMut<'_>,
    paragraph_id: &str,
    alignment: Option<&str>,
    level: u32,
    bullet_json: Option<&str>,
) {
    let index = story.len(txn);
    let pilcrow =
        story.insert_embed_with_attributes(txn, index, MapPrelim::default(), Attrs::default());
    pilcrow.insert(txn, KIND, PILCROW_KIND);
    pilcrow.insert(txn, PARA_ID, paragraph_id);
    pilcrow.insert(txn, "level", level as f64);
    if let Some(alignment) = alignment {
        pilcrow.insert(txn, "alignment", alignment);
    }
    if let Some(bullet_json) = bullet_json {
        pilcrow.insert(txn, "bulletJson", bullet_json);
    }
}

impl DeckSession {
    pub fn story(&self, story_id: &str) -> EditResult<StorySnapshot> {
        let txn = self.doc.transact();
        let story = story_ref(&txn, story_id)?;
        snapshot_story(&story, &txn, story_id)
    }

    pub fn anchor_caret(&self, story_id: &str, index: u32) -> EditResult<CaretAnchor> {
        let txn = self.doc.transact();
        let story = story_ref(&txn, story_id)?;
        let length = final_pilcrow_index(&story, &txn)?;
        if index > length {
            return Err(EditError::OutOfBounds { index, length });
        }
        let position = if index == 0 {
            StickyIndex::from_type(&txn, &story, Assoc::Before)
        } else {
            story
                .sticky_index(&txn, index, Assoc::After)
                .ok_or(EditError::OutOfBounds { index, length })?
        };
        Ok(CaretAnchor {
            story_id: story_id.to_owned(),
            position,
        })
    }

    pub fn resolve_caret_anchor(&self, anchor: &CaretAnchor) -> Option<u32> {
        let txn = self.doc.transact();
        let story = story_ref(&txn, &anchor.story_id).ok()?;
        let offset = anchor.position.get_offset(&txn)?;
        let expected = BranchPtr::from(<TextRef as AsRef<Branch>>::as_ref(&story));
        if offset.branch != expected {
            return None;
        }
        let length = final_pilcrow_index(&story, &txn).ok()?;
        Some(offset.index.min(length))
    }

    pub fn insert_text(
        &self,
        context: &crate::EditCtx,
        story_id: &str,
        index: u32,
        text: &str,
        style: &TextStyle,
    ) -> EditResult<TextReceipt> {
        validate_xml_text(text)?;
        validate_style_values(
            style.font_family.as_deref(),
            style.underline.as_deref(),
            style.color.as_deref(),
            style.font_size_pt,
            style.spacing_pt,
            style.baseline_pct,
        )?;
        let mut txn = self.transact_for(context);
        let story = story_ref(&txn, story_id)?;
        let final_pilcrow = final_pilcrow_index(&story, &txn)?;
        if index > final_pilcrow {
            return Err(EditError::OutOfBounds {
                index,
                length: final_pilcrow,
            });
        }
        if !text.is_empty() {
            insert_styled_text(&story, &mut txn, index, text, style);
        }
        let length = text.encode_utf16().count() as u32;
        Ok(TextReceipt {
            story_id: story_id.to_owned(),
            start: index,
            end: index + length,
            text: text.to_owned(),
        })
    }

    pub fn delete_text(
        &self,
        context: &crate::EditCtx,
        story_id: &str,
        start: u32,
        end: u32,
    ) -> EditResult<TextReceipt> {
        let mut txn = self.transact_for(context);
        let story = story_ref(&txn, story_id)?;
        check_text_range(&story, &txn, start, end)?;
        let text = text_in_range(&story, &txn, start, end);
        if end > start {
            story.remove_range(&mut txn, start, end - start);
        }
        Ok(TextReceipt {
            story_id: story_id.to_owned(),
            start,
            end,
            text,
        })
    }

    pub fn format_text(
        &self,
        context: &crate::EditCtx,
        story_id: &str,
        start: u32,
        end: u32,
        patch: &TextStylePatch,
    ) -> EditResult<TextReceipt> {
        validate_style_values(
            patch.font_family.as_deref(),
            patch.underline.as_deref(),
            patch.color.as_deref(),
            patch.font_size_pt,
            patch.spacing_pt,
            patch.baseline_pct,
        )?;
        let mut txn = self.transact_for(context);
        let story = story_ref(&txn, story_id)?;
        check_text_bounds(&story, &txn, start, end)?;
        let text = text_in_range(&story, &txn, start, end);
        for (segment_start, segment_end) in paragraph_text_segments(&story, &txn, start, end) {
            story.format(
                &mut txn,
                segment_start,
                segment_end - segment_start,
                attrs_from_patch(patch),
            );
        }
        Ok(TextReceipt {
            story_id: story_id.to_owned(),
            start,
            end,
            text,
        })
    }

    /// Sets `a:pPr@algn` on every paragraph the range touches; `None` clears
    /// the value so the placeholder cascade applies again.
    pub fn set_paragraph_alignment(
        &self,
        context: &crate::EditCtx,
        story_id: &str,
        start: u32,
        end: u32,
        alignment: Option<&str>,
    ) -> EditResult<TextReceipt> {
        validate_alignment(alignment)?;
        let mut txn = self.transact_for(context);
        let story = story_ref(&txn, story_id)?;
        check_text_bounds(&story, &txn, start, end)?;
        let text = text_in_range(&story, &txn, start, end);
        let pilcrows = selected_pilcrows(&story, &txn, start, end);
        for pilcrow in pilcrows {
            match alignment {
                Some(alignment) => {
                    pilcrow.insert(&mut txn, "alignment", alignment);
                }
                None => {
                    pilcrow.remove(&mut txn, "alignment");
                }
            }
        }
        Ok(TextReceipt {
            story_id: story_id.to_owned(),
            start,
            end,
            text,
        })
    }

    pub fn insert_paragraph_break(
        &self,
        context: &crate::EditCtx,
        story_id: &str,
        index: u32,
    ) -> EditResult<TextReceipt> {
        let mut txn = self.transact_for(context);
        let story = story_ref(&txn, story_id)?;
        let final_pilcrow = final_pilcrow_index(&story, &txn)?;
        if index > final_pilcrow {
            return Err(EditError::OutOfBounds {
                index,
                length: final_pilcrow,
            });
        }
        let paragraph_id = self.next_id("para");
        let pilcrow = story.insert_embed_with_attributes(
            &mut txn,
            index,
            MapPrelim::default(),
            Attrs::default(),
        );
        pilcrow.insert(&mut txn, KIND, PILCROW_KIND);
        pilcrow.insert(&mut txn, PARA_ID, paragraph_id);
        pilcrow.insert(&mut txn, "level", 0_f64);
        Ok(TextReceipt {
            story_id: story_id.to_owned(),
            start: index,
            end: index + 1,
            text: "\n".to_owned(),
        })
    }

    pub fn delete_paragraph_break(
        &self,
        context: &crate::EditCtx,
        story_id: &str,
        index: u32,
    ) -> EditResult<TextReceipt> {
        let mut txn = self.transact_for(context);
        let story = story_ref(&txn, story_id)?;
        let final_pilcrow = final_pilcrow_index(&story, &txn)?;
        if index >= final_pilcrow {
            return Err(EditError::OutOfBounds {
                index,
                length: final_pilcrow,
            });
        }
        let mut offset = 0;
        let is_pilcrow = story.diff(&txn, YChange::identity).into_iter().any(|diff| {
            let length = out_len(&diff.insert);
            let found = offset == index
                && matches!(
                    diff.insert,
                    Out::YMap(ref map)
                        if map_string(map, &txn, KIND).as_deref() == Some(PILCROW_KIND)
                );
            offset += length;
            found
        });
        if !is_pilcrow {
            return Err(EditError::ParagraphBoundary {
                start: index,
                end: index.saturating_add(1),
            });
        }
        story.remove_range(&mut txn, index, 1);
        Ok(TextReceipt {
            story_id: story_id.to_owned(),
            start: index,
            end: index + 1,
            text: "\n".to_owned(),
        })
    }
}

pub(crate) fn validate_story<T: ReadTxn>(
    story: &TextRef,
    txn: &T,
    story_id: &str,
) -> EditResult<()> {
    let mut offset = 0;
    let mut pilcrows = Vec::new();
    for diff in story.diff(txn, YChange::identity) {
        let length = out_len(&diff.insert);
        if let Out::YMap(map) = diff.insert {
            if map_string(&map, txn, KIND).as_deref() != Some(PILCROW_KIND) {
                return Err(EditError::InvalidState(format!(
                    "story {story_id} contains a non-pilcrow embed"
                )));
            }
            pilcrows.push(offset);
        }
        offset += length;
    }
    if pilcrows.last().copied() != story.len(txn).checked_sub(1) {
        return Err(EditError::InvalidState(format!(
            "story {story_id} has no final pilcrow"
        )));
    }
    Ok(())
}

/// The snapshot `snapshot_story` reads back from a story `seed_story` wrote,
/// computed without materializing a scratch document.
pub(crate) fn baseline_story(
    story_id: &str,
    body: &TextBody,
    theme: Option<&Theme>,
) -> EditResult<StorySnapshot> {
    let mut paragraphs = Vec::with_capacity(body.paragraphs.len().max(1));
    let mut length = 1_u32;
    if body.paragraphs.is_empty() {
        paragraphs.push(ParagraphSnapshot {
            id: format!("para:{story_id}:0"),
            alignment: None,
            level: 0,
            bullet_json: None,
            runs: Vec::new(),
        });
    } else {
        length = 0;
        for (paragraph_index, paragraph) in body.paragraphs.iter().enumerate() {
            let mut runs: Vec<TextRunSnapshot> = Vec::new();
            let mut last_attrs: Option<TextStyle> = None;
            for run in &paragraph.runs {
                if run.text.is_empty() {
                    continue;
                }
                let style = style_from_run_properties(&run.properties, theme);
                // yrs' format-gap cleanup deletes every marker between two
                // identically styled runs, so their text items squash into a
                // single run in the snapshot.
                if last_attrs.as_ref() == Some(&style) {
                    if let Some(last) = runs.last_mut() {
                        last.text.push_str(&run.text);
                    }
                } else {
                    runs.push(TextRunSnapshot {
                        text: run.text.clone(),
                        style: baseline_style(style.clone()),
                    });
                }
                last_attrs = Some(style);
                length += run.text.encode_utf16().count() as u32;
            }
            length += 1;
            let bullet_json = paragraph
                .properties
                .bullet
                .as_ref()
                .map(serde_json::to_string)
                .transpose()
                .map_err(|error| EditError::Json(error.to_string()))?;
            paragraphs.push(ParagraphSnapshot {
                id: format!("para:{story_id}:{paragraph_index}"),
                alignment: paragraph.properties.alignment.clone(),
                level: paragraph.properties.level,
                bullet_json,
                runs,
            });
        }
    }
    Ok(StorySnapshot {
        id: story_id.to_owned(),
        length,
        paragraphs,
    })
}

/// Attribute reads drop non-finite numbers, so the snapshot applies the same
/// normalization the seeded `Any::Number` attributes go through.
fn baseline_style(style: TextStyle) -> TextStyle {
    TextStyle {
        font_size_pt: style.font_size_pt.filter(|value| value.is_finite()),
        spacing_pt: style.spacing_pt.filter(|value| value.is_finite()),
        baseline_pct: style.baseline_pct.filter(|value| value.is_finite()),
        ..style
    }
}

pub(crate) fn snapshot_story<T: ReadTxn>(
    story: &TextRef,
    txn: &T,
    story_id: &str,
) -> EditResult<StorySnapshot> {
    validate_story(story, txn, story_id)?;
    let mut paragraphs = Vec::new();
    let mut runs = Vec::new();
    for diff in story.diff(txn, YChange::identity) {
        match diff.insert {
            Out::Any(Any::String(text)) => runs.push(TextRunSnapshot {
                text: text.to_string(),
                style: style_from_attrs(diff.attributes.as_deref()),
            }),
            Out::YMap(map) => {
                paragraphs.push(ParagraphSnapshot {
                    id: map_string(&map, txn, PARA_ID).unwrap_or_default(),
                    alignment: map_string(&map, txn, "alignment"),
                    level: map_number(&map, txn, "level").unwrap_or_default() as u32,
                    bullet_json: map_string(&map, txn, "bulletJson"),
                    runs: std::mem::take(&mut runs),
                });
            }
            _ => {}
        }
    }
    Ok(StorySnapshot {
        id: story_id.to_owned(),
        length: story.len(txn),
        paragraphs,
    })
}

pub(crate) fn story_ref<T: ReadTxn>(txn: &T, story_id: &str) -> EditResult<TextRef> {
    txn.get_map(STORIES)
        .and_then(|stories| stories.get(txn, story_id))
        .and_then(|value| value.cast::<TextRef>().ok())
        .ok_or_else(|| EditError::StoryNotFound(story_id.to_owned()))
}

fn final_pilcrow_index<T: ReadTxn>(story: &TextRef, txn: &T) -> EditResult<u32> {
    validate_story(story, txn, "requested")?;
    Ok(story.len(txn) - 1)
}

fn check_text_range<T: ReadTxn>(story: &TextRef, txn: &T, start: u32, end: u32) -> EditResult<()> {
    check_text_bounds(story, txn, start, end)?;
    let mut offset = 0;
    for diff in story.diff(txn, YChange::identity) {
        let item_length = out_len(&diff.insert);
        if matches!(diff.insert, Out::YMap(_)) && start <= offset && offset < end {
            return Err(EditError::ParagraphBoundary { start, end });
        }
        offset += item_length;
    }
    Ok(())
}

fn check_text_bounds<T: ReadTxn>(story: &TextRef, txn: &T, start: u32, end: u32) -> EditResult<()> {
    let length = story.len(txn);
    if start > end || end > length {
        return Err(EditError::OutOfBounds {
            index: end.max(start),
            length,
        });
    }
    Ok(())
}

/// Whether a paragraph-level edit over `start..end` touches the paragraph spanning
/// `paragraph_start..=pilcrow`. A collapsed caret picks the paragraph it sits in; a range
/// stopping at a paragraph start does not.
pub(crate) fn selects_paragraph(start: u32, end: u32, paragraph_start: u32, pilcrow: u32) -> bool {
    start <= pilcrow && (end > paragraph_start || (start == end && start >= paragraph_start))
}

/// The pilcrow of every paragraph the range touches, as [`selects_paragraph`] decides.
fn selected_pilcrows<T: ReadTxn>(story: &TextRef, txn: &T, start: u32, end: u32) -> Vec<MapRef> {
    let start = start.min(story.len(txn).saturating_sub(1));
    let mut pilcrows = Vec::new();
    let mut paragraph_start = 0;
    let mut offset = 0;
    for diff in story.diff(txn, YChange::identity) {
        let item_length = out_len(&diff.insert);
        if let Out::YMap(map) = diff.insert {
            if selects_paragraph(start, end, paragraph_start, offset) {
                pilcrows.push(map);
            }
            paragraph_start = offset + item_length;
        }
        offset += item_length;
    }
    pilcrows
}

fn paragraph_text_segments<T: ReadTxn>(
    story: &TextRef,
    txn: &T,
    start: u32,
    end: u32,
) -> Vec<(u32, u32)> {
    let mut segments = Vec::new();
    let mut paragraph_start = 0;
    let mut offset = 0;
    for diff in story.diff(txn, YChange::identity) {
        let item_length = out_len(&diff.insert);
        if matches!(diff.insert, Out::YMap(_)) {
            let segment_start = start.max(paragraph_start);
            let segment_end = end.min(offset);
            if segment_start < segment_end {
                segments.push((segment_start, segment_end));
            }
            paragraph_start = offset + item_length;
        }
        offset += item_length;
    }
    segments
}

fn text_in_range<T: ReadTxn>(story: &TextRef, txn: &T, start: u32, end: u32) -> String {
    let mut output = String::new();
    let mut offset = 0;
    for diff in story.diff(txn, YChange::identity) {
        let length = out_len(&diff.insert);
        if let Out::Any(Any::String(text)) = diff.insert {
            let overlap_start = start.saturating_sub(offset).min(length);
            let overlap_end = end.saturating_sub(offset).min(length);
            if overlap_end > overlap_start {
                let utf16 = text.encode_utf16().collect::<Vec<_>>();
                output.push_str(&String::from_utf16_lossy(
                    &utf16[overlap_start as usize..overlap_end as usize],
                ));
            }
        }
        offset += length;
        if offset >= end {
            break;
        }
    }
    output
}

fn insert_styled_text(
    story: &TextRef,
    txn: &mut TransactionMut<'_>,
    index: u32,
    text: &str,
    style: &TextStyle,
) {
    story.insert(txn, index, text);
    let length = text.encode_utf16().count() as u32;
    for (key, value) in style_values(style) {
        story.format(txn, index, length, Attrs::from([(Arc::from(key), value)]));
    }
}

fn style_values(style: &TextStyle) -> [(&'static str, Any); 9] {
    [
        ("bold", style.bold.map(Any::Bool).unwrap_or(Any::Null)),
        ("italic", style.italic.map(Any::Bool).unwrap_or(Any::Null)),
        (
            "fontSize",
            style.font_size_pt.map(Any::Number).unwrap_or(Any::Null),
        ),
        (
            "color",
            style.color.as_deref().map(Any::from).unwrap_or(Any::Null),
        ),
        (
            "fontFamily",
            style
                .font_family
                .as_deref()
                .map(Any::from)
                .unwrap_or(Any::Null),
        ),
        (
            "underline",
            style
                .underline
                .as_deref()
                .map(Any::from)
                .unwrap_or(Any::Null),
        ),
        (
            "spacing",
            style.spacing_pt.map(Any::Number).unwrap_or(Any::Null),
        ),
        (
            "baseline",
            style.baseline_pct.map(Any::Number).unwrap_or(Any::Null),
        ),
        (
            "caps",
            style
                .caps
                .map(|caps| Any::from(caps.as_attribute()))
                .unwrap_or(Any::Null),
        ),
    ]
}

fn attrs_from_patch(patch: &TextStylePatch) -> Attrs {
    let mut attrs = Attrs::default();
    insert_option(&mut attrs, "bold", patch.bold.map(Any::Bool));
    insert_option(&mut attrs, "italic", patch.italic.map(Any::Bool));
    insert_option(&mut attrs, "fontSize", patch.font_size_pt.map(Any::Number));
    insert_option(&mut attrs, "color", patch.color.as_deref().map(Any::from));
    insert_option(
        &mut attrs,
        "fontFamily",
        patch.font_family.as_deref().map(Any::from),
    );
    insert_option(
        &mut attrs,
        "underline",
        patch.underline.as_deref().map(Any::from),
    );
    insert_option(&mut attrs, "spacing", patch.spacing_pt.map(Any::Number));
    insert_option(&mut attrs, "baseline", patch.baseline_pct.map(Any::Number));
    attrs
}

fn insert_option(attrs: &mut Attrs, key: &str, value: Option<Any>) {
    if let Some(value) = value {
        attrs.insert(Arc::from(key), value);
    }
}

fn style_from_run_properties(properties: &RunProperties, theme: Option<&Theme>) -> TextStyle {
    TextStyle {
        bold: properties.bold,
        italic: properties.italic,
        font_size_pt: properties.font_size_pt,
        color: resolve_color_value_to_hex_with_theme(properties.color.as_ref(), theme),
        font_family: properties.font_family.clone(),
        underline: properties.underline.clone(),
        spacing_pt: properties.spacing_pt,
        baseline_pct: properties.baseline_pct,
        caps: properties.caps,
    }
}

pub(crate) fn style_from_attrs(attrs: Option<&Attrs>) -> TextStyle {
    TextStyle {
        bold: attrs.and_then(|attrs| any_bool(attrs.get("bold"))),
        italic: attrs.and_then(|attrs| any_bool(attrs.get("italic"))),
        font_size_pt: attrs.and_then(|attrs| any_number(attrs.get("fontSize"))),
        color: attrs.and_then(|attrs| any_string(attrs.get("color"))),
        font_family: attrs.and_then(|attrs| any_string(attrs.get("fontFamily"))),
        underline: attrs.and_then(|attrs| any_string(attrs.get("underline"))),
        spacing_pt: attrs.and_then(|attrs| any_number(attrs.get("spacing"))),
        baseline_pct: attrs.and_then(|attrs| any_number(attrs.get("baseline"))),
        caps: attrs
            .and_then(|attrs| any_string(attrs.get("caps")))
            .as_deref()
            .and_then(TextCaps::from_attribute),
    }
}

fn any_bool(value: Option<&Any>) -> Option<bool> {
    match value {
        Some(Any::Bool(value)) => Some(*value),
        _ => None,
    }
}

fn any_number(value: Option<&Any>) -> Option<f64> {
    match value {
        Some(Any::Number(value)) if value.is_finite() => Some(*value),
        Some(Any::BigInt(value)) => Some(*value as f64),
        _ => None,
    }
}

fn any_string(value: Option<&Any>) -> Option<String> {
    match value {
        Some(Any::String(value)) => Some(value.to_string()),
        _ => None,
    }
}

fn map_string<T: ReadTxn>(map: &MapRef, txn: &T, key: &str) -> Option<String> {
    match map.get(txn, key) {
        Some(Out::Any(Any::String(value))) => Some(value.to_string()),
        _ => None,
    }
}

fn map_number<T: ReadTxn>(map: &MapRef, txn: &T, key: &str) -> Option<f64> {
    match map.get(txn, key) {
        Some(Out::Any(Any::Number(value))) if value.is_finite() => Some(value),
        Some(Out::Any(Any::BigInt(value))) => Some(value as f64),
        _ => None,
    }
}

fn out_len(value: &Out) -> u32 {
    match value {
        Out::Any(Any::String(value)) => value.encode_utf16().count() as u32,
        _ => 1,
    }
}