text-document-formatting 1.4.0

Undoable text and block formatting use cases for text-document
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
//! Complex mixed scenarios -- tables, lists, frames, images interacting
//! with multiple formatting operations and undo chains.

extern crate text_document_formatting as document_formatting;

use anyhow::Result;
use common::entities::InlineContent;

use document_formatting::document_formatting_controller;
use document_formatting::{
    Alignment, CellVerticalAlignment, SetBlockFormatDto, SetFrameFormatDto, SetListFormatDto,
    SetTableCellFormatDto, SetTableFormatDto, SetTextFormatDto,
};

use test_harness::{
    BlockRelationshipField, block_controller, create_list, export_text, frame_controller,
    get_all_block_ids, get_block_ids, get_frame_id, get_sorted_cells, inline_element_controller,
    insert_frame, insert_image, insert_table, list_controller, setup_with_text,
    table_cell_controller, table_controller,
};

#[test]
fn test_mixed_list_block_and_text_formatting() -> Result<()> {
    let (db, hub, mut urm) = setup_with_text("Heading\nItem A\nItem B\nItem C")?;

    // create_list clears the undo stack, so call it before undoable formatting ops
    let list_result = create_list(
        &db,
        &hub,
        &mut urm,
        8,
        28,
        common::entities::ListStyle::Decimal,
    )?;

    document_formatting_controller::set_block_format(
        &db,
        &hub,
        &mut urm,
        None,
        &SetBlockFormatDto {
            position: 0,
            anchor: 7,
            heading_level: Some(1),
            alignment: Some(Alignment::Center),
            ..Default::default()
        },
    )?;

    document_formatting_controller::set_list_format(
        &db,
        &hub,
        &mut urm,
        None,
        &SetListFormatDto {
            list_id: list_result.list_id as i64,
            style: Some(common::entities::ListStyle::UpperAlpha),
            indent: Some(2),
            prefix: None,
            suffix: Some(".".into()),
        },
    )?;

    document_formatting_controller::set_text_format(
        &db,
        &hub,
        &mut urm,
        None,
        &SetTextFormatDto {
            position: 8,
            anchor: 28,
            font_bold: Some(true),
            ..Default::default()
        },
    )?;

    let block_ids = get_block_ids(&db)?;
    let heading_block = block_controller::get(&db, &block_ids[0])?.unwrap();
    assert_eq!(heading_block.fmt_heading_level, Some(1));
    assert_eq!(
        heading_block.fmt_alignment,
        Some(common::entities::Alignment::Center)
    );

    let list = list_controller::get(&db, &list_result.list_id)?.unwrap();
    assert_eq!(list.style, common::entities::ListStyle::UpperAlpha);
    assert_eq!(list.indent, 2);
    assert_eq!(list.suffix, ".");

    for (i, bid) in block_ids.iter().enumerate().take(4).skip(1) {
        let elem_ids =
            block_controller::get_relationship(&db, bid, &BlockRelationshipField::Elements)?;
        for eid in &elem_ids {
            let elem = inline_element_controller::get(&db, eid)?.unwrap();
            if let InlineContent::Text(ref t) = elem.content
                && !t.is_empty()
            {
                assert_eq!(
                    elem.fmt_font_bold,
                    Some(true),
                    "Text '{}' in block {} should be bold",
                    t,
                    i
                );
            }
        }
    }

    // Undo the 3 formatting operations (set_text_format, set_list_format, set_block_format)
    urm.undo(None)?;
    urm.undo(None)?;
    urm.undo(None)?;

    let heading_block = block_controller::get(&db, &block_ids[0])?.unwrap();
    assert_eq!(heading_block.fmt_heading_level, None);
    assert_eq!(heading_block.fmt_alignment, None);
    Ok(())
}

#[test]
fn test_mixed_table_with_formatted_cells() -> Result<()> {
    let (db, hub, mut urm) = setup_with_text("Report")?;
    let table_result = insert_table(&db, &hub, &mut urm, 6, 2, 3)?;
    let table_id = table_result.table_id;

    document_formatting_controller::set_table_format(
        &db,
        &hub,
        &mut urm,
        None,
        &SetTableFormatDto {
            table_id: table_id as i64,
            border: Some(1),
            cell_padding: Some(5),
            width: Some(500),
            alignment: Some(Alignment::Center),
            ..Default::default()
        },
    )?;

    let cells = get_sorted_cells(&db, &table_id)?;
    for c in cells.iter().take(3) {
        document_formatting_controller::set_table_cell_format(
            &db,
            &hub,
            &mut urm,
            None,
            &SetTableCellFormatDto {
                cell_id: c.id as i64,
                background_color: Some("#333333".into()),
                vertical_alignment: Some(CellVerticalAlignment::Middle),
                ..Default::default()
            },
        )?;
    }

    for c in cells.iter().take(3) {
        let cell = table_cell_controller::get(&db, &c.id)?.unwrap();
        assert_eq!(cell.fmt_background_color, Some("#333333".into()));
        assert_eq!(
            cell.fmt_vertical_alignment,
            Some(common::entities::CellVerticalAlignment::Middle)
        );
    }
    for c in cells.iter().take(6).skip(3) {
        let cell = table_cell_controller::get(&db, &c.id)?.unwrap();
        assert_eq!(cell.fmt_background_color, None);
    }

    let table = table_controller::get(&db, &table_id)?.unwrap();
    assert_eq!(table.fmt_border, Some(1));
    assert_eq!(table.fmt_cell_padding, Some(5));
    Ok(())
}

#[test]
fn test_mixed_sub_frame_with_list_and_formatting() -> Result<()> {
    let (db, hub, mut urm) = setup_with_text("Main text")?;
    let frame_result = insert_frame(&db, &hub, &mut urm, 9)?;
    let sub_frame_id = frame_result.frame_id;

    document_formatting_controller::set_frame_format(
        &db,
        &hub,
        &mut urm,
        None,
        &SetFrameFormatDto {
            position: 0,
            anchor: 0,
            frame_id: sub_frame_id as i64,
            is_blockquote: Some(true),
            left_margin: Some(20),
            padding: Some(10),
            border: Some(1),
            ..Default::default()
        },
    )?;

    let sub_frame = frame_controller::get(&db, &sub_frame_id)?.unwrap();
    assert_eq!(sub_frame.fmt_is_blockquote, Some(true));
    assert_eq!(sub_frame.fmt_left_margin, Some(20));
    assert_eq!(sub_frame.fmt_padding, Some(10));
    assert_eq!(sub_frame.fmt_border, Some(1));

    let main_frame_id = get_frame_id(&db)?;
    let main_frame = frame_controller::get(&db, &main_frame_id)?.unwrap();
    assert_eq!(main_frame.fmt_is_blockquote, None);
    Ok(())
}

#[test]
fn test_mixed_full_document_formatting_undo_chain() -> Result<()> {
    let (db, hub, mut urm) = setup_with_text("Title\nParagraph one\nParagraph two")?;

    document_formatting_controller::set_block_format(
        &db,
        &hub,
        &mut urm,
        None,
        &SetBlockFormatDto {
            position: 0,
            anchor: 5,
            heading_level: Some(1),
            ..Default::default()
        },
    )?;
    document_formatting_controller::set_text_format(
        &db,
        &hub,
        &mut urm,
        None,
        &SetTextFormatDto {
            position: 0,
            anchor: 5,
            font_bold: Some(true),
            font_family: Some("Helvetica".into()),
            ..Default::default()
        },
    )?;
    document_formatting_controller::set_text_format(
        &db,
        &hub,
        &mut urm,
        None,
        &SetTextFormatDto {
            position: 16,
            anchor: 19,
            font_italic: Some(true),
            ..Default::default()
        },
    )?;

    let frame_id = get_frame_id(&db)?;
    document_formatting_controller::set_frame_format(
        &db,
        &hub,
        &mut urm,
        None,
        &SetFrameFormatDto {
            position: 0,
            anchor: 0,
            frame_id: frame_id as i64,
            padding: Some(20),
            ..Default::default()
        },
    )?;

    let block_ids = get_block_ids(&db)?;
    assert_eq!(
        block_controller::get(&db, &block_ids[0])?
            .unwrap()
            .fmt_heading_level,
        Some(1)
    );
    assert_eq!(
        frame_controller::get(&db, &frame_id)?.unwrap().fmt_padding,
        Some(20)
    );

    urm.undo(None)?;
    assert_eq!(
        frame_controller::get(&db, &frame_id)?.unwrap().fmt_padding,
        None
    );
    urm.undo(None)?;
    urm.undo(None)?;
    let title_elems =
        block_controller::get_relationship(&db, &block_ids[0], &BlockRelationshipField::Elements)?;
    assert_eq!(
        inline_element_controller::get(&db, &title_elems[0])?
            .unwrap()
            .fmt_font_bold,
        None
    );
    urm.undo(None)?;
    assert_eq!(
        block_controller::get(&db, &block_ids[0])?
            .unwrap()
            .fmt_heading_level,
        None
    );

    urm.redo(None)?;
    urm.redo(None)?;
    urm.redo(None)?;
    urm.redo(None)?;
    assert_eq!(
        block_controller::get(&db, &block_ids[0])?
            .unwrap()
            .fmt_heading_level,
        Some(1)
    );
    assert_eq!(
        frame_controller::get(&db, &frame_id)?.unwrap().fmt_padding,
        Some(20)
    );
    Ok(())
}

#[test]
fn test_mixed_table_and_list_same_document_formatting() -> Result<()> {
    let (db, hub, mut urm) = setup_with_text("Header\nBullet A\nBullet B")?;
    let list_result = create_list(
        &db,
        &hub,
        &mut urm,
        7,
        24,
        common::entities::ListStyle::Disc,
    )?;
    let table_result = insert_table(&db, &hub, &mut urm, 24, 2, 2)?;

    let table_id = table_result.table_id;
    let list_id = list_result.list_id;

    document_formatting_controller::set_list_format(
        &db,
        &hub,
        &mut urm,
        None,
        &SetListFormatDto {
            list_id: list_id as i64,
            style: Some(common::entities::ListStyle::Circle),
            indent: Some(1),
            prefix: Some("- ".into()),
            suffix: None,
        },
    )?;
    document_formatting_controller::set_table_format(
        &db,
        &hub,
        &mut urm,
        None,
        &SetTableFormatDto {
            table_id: table_id as i64,
            border: Some(2),
            alignment: Some(Alignment::Left),
            ..Default::default()
        },
    )?;
    document_formatting_controller::set_block_format(
        &db,
        &hub,
        &mut urm,
        None,
        &SetBlockFormatDto {
            position: 0,
            anchor: 6,
            heading_level: Some(2),
            alignment: Some(Alignment::Center),
            ..Default::default()
        },
    )?;

    assert_eq!(
        list_controller::get(&db, &list_id)?.unwrap().style,
        common::entities::ListStyle::Circle
    );
    assert_eq!(list_controller::get(&db, &list_id)?.unwrap().prefix, "- ");
    assert_eq!(
        table_controller::get(&db, &table_id)?.unwrap().fmt_border,
        Some(2)
    );
    assert_eq!(
        block_controller::get(&db, &get_block_ids(&db)?[0])?
            .unwrap()
            .fmt_heading_level,
        Some(2)
    );

    for _ in 0..3 {
        urm.undo(None)?;
    }
    assert_eq!(
        table_controller::get(&db, &table_id)?.unwrap().fmt_border,
        None,
        "Table format should be undone"
    );
    assert_eq!(
        list_controller::get(&db, &list_id)?.unwrap().style,
        common::entities::ListStyle::Disc,
        "List style should be reverted"
    );
    Ok(())
}

#[test]
fn test_mixed_text_format_with_image_and_list() -> Result<()> {
    let (db, hub, mut urm) = setup_with_text("Start\nList item")?;
    insert_image(&db, &hub, &mut urm, 3, "icon.png", 16, 16)?;
    create_list(
        &db,
        &hub,
        &mut urm,
        7,
        16,
        common::entities::ListStyle::Disc,
    )?;

    document_formatting_controller::set_text_format(
        &db,
        &hub,
        &mut urm,
        None,
        &SetTextFormatDto {
            position: 0,
            anchor: 6,
            font_bold: Some(true),
            ..Default::default()
        },
    )?;

    let block_ids = get_block_ids(&db)?;
    let elem_ids =
        block_controller::get_relationship(&db, &block_ids[0], &BlockRelationshipField::Elements)?;
    let mut text_bold_count = 0;
    let mut image_bold = false;
    for eid in &elem_ids {
        let elem = inline_element_controller::get(&db, eid)?.unwrap();
        if elem.fmt_font_bold == Some(true) {
            match &elem.content {
                InlineContent::Text(_) => text_bold_count += 1,
                InlineContent::Image { .. } => image_bold = true,
                _ => {}
            }
        }
    }
    assert!(
        text_bold_count > 0,
        "At least one text element should be bold"
    );
    assert!(image_bold, "Image should be bold when fully in range");

    let text = export_text(&db, &hub)?;
    assert!(text.starts_with("Sta"), "Text should start with 'Sta'");
    Ok(())
}

#[test]
fn test_format_blocks_across_multiple_frames() -> Result<()> {
    let (db, hub, mut urm) = setup_with_text("Frame 1 content")?;
    insert_frame(&db, &hub, &mut urm, 15)?;

    let all_block_ids = get_all_block_ids(&db)?;
    assert!(
        all_block_ids.len() >= 2,
        "Should have blocks in both frames"
    );

    let mut min_pos = i64::MAX;
    let mut max_end = 0;
    for bid in &all_block_ids {
        let b = block_controller::get(&db, bid)?.unwrap();
        min_pos = min_pos.min(b.document_position);
        max_end = max_end.max(b.document_position + b.text_length);
    }

    document_formatting_controller::set_block_format(
        &db,
        &hub,
        &mut urm,
        None,
        &SetBlockFormatDto {
            position: min_pos,
            anchor: max_end,
            direction: Some(common::entities::TextDirection::RightToLeft),
            ..Default::default()
        },
    )?;

    for bid in &all_block_ids {
        let b = block_controller::get(&db, bid)?.unwrap();
        assert_eq!(
            b.fmt_direction,
            Some(common::entities::TextDirection::RightToLeft),
            "Block at position {} should have RTL direction",
            b.document_position
        );
    }
    Ok(())
}