A text document structure and management for Rust
This model is thought as a backend for a text UI. [TextDocument
] can't be modified directly by the user, only for setting the whole document with set_plain_text(...)
.
The user must use a [TextCursor
] using document.create_cursor()
to make any change.
Frame
]: contains Block elements and other Frame elements, formatable with FrameFormatBlock
]: contains Text elements or Image elements, formatable with BlockFormatText
]: contains the actuel text, formatable with TextFormatImage
]: represent the position of an image, formatable with ImageFormatAll these items are encapsulated in its corresponding [Element
] for ease of storage.
raw
Frame
|- Block
|- Text
|- Block
|- Text
raw
Frame
|- Block
|- Text --> I really lo
|- Text --> ve (imagine it Formatted in bold)
|- Text --> Rust
|- Image
|- Text
|- Frame
|- Block
|- Text
|- Text
|- Text
|- Block
|- Text
|- Text
|- Text
|- Block
|- Image
Each modififcation is signaled using callbacks. [TextDocument
] offers different ways to make your code aware of any change:
- [TextDocument::add_text_change_callback()
]
Give the number of removed characters and number of added characters with the reference of a cursor position.
[TextDocument::add_element_change_callback()
]
Give the modified element with the reason. If two direct children elements changed at the same time.
```rust use text_document::{TextDocument, ChangeReason, MoveMode};
let mut document = TextDocument::new();
document.addtextchangecallback(|position, removedcharacters, addedcharacters|{ println!("position: {}, removedcharacters: {}, addedcharacters: {}", position, removedcharacters, added_characters); } );
document.addelementchangecallback(|element, reason|{ asserteq!(element.uuid(), 0); asserteq!(reason, ChangeReason::ChildrenChanged ); } ); document.setplain_text("beginningend").unwrap();
let mut cursor = document.createcursor(); cursor.setposition(9, MoveMode::MoveAnchor); cursor.insertplaintext("new\nplain_text\ntest");
```
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.