pub struct TextDocument { /* private fields */ }Expand description
A rich text document.
Owns the backend (database, event hub, undo/redo manager) and provides
document-level operations. All cursor-based editing goes through
TextCursor, obtained via cursor() or
cursor_at().
Internally uses Arc<Mutex<...>> so that multiple TextCursors can
coexist and edit concurrently. Cloning a TextDocument creates a new
handle to the same underlying document (like Qt’s implicit sharing).
Implementations§
Source§impl TextDocument
impl TextDocument
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new, empty document.
§Panics
Panics if the database context cannot be created (e.g. filesystem error).
Use TextDocument::try_new for a fallible alternative.
Sourcepub fn set_plain_text(&self, text: &str) -> Result<()>
pub fn set_plain_text(&self, text: &str) -> Result<()>
Replace the entire document with plain text. Clears undo history.
Sourcepub fn to_plain_text(&self) -> Result<String>
pub fn to_plain_text(&self) -> Result<String>
Export the entire document as plain text.
Sourcepub fn set_markdown(
&self,
markdown: &str,
) -> Result<Operation<MarkdownImportResult>>
pub fn set_markdown( &self, markdown: &str, ) -> Result<Operation<MarkdownImportResult>>
Replace the entire document with Markdown. Clears undo history.
This is a long operation. Returns a typed Operation handle.
Sourcepub fn to_markdown(&self) -> Result<String>
pub fn to_markdown(&self) -> Result<String>
Export the entire document as Markdown.
Sourcepub fn set_html(&self, html: &str) -> Result<Operation<HtmlImportResult>>
pub fn set_html(&self, html: &str) -> Result<Operation<HtmlImportResult>>
Replace the entire document with HTML. Clears undo history.
This is a long operation. Returns a typed Operation handle.
Sourcepub fn to_latex(
&self,
document_class: &str,
include_preamble: bool,
) -> Result<String>
pub fn to_latex( &self, document_class: &str, include_preamble: bool, ) -> Result<String>
Export the entire document as LaTeX.
Sourcepub fn to_docx(&self, output_path: &str) -> Result<Operation<DocxExportResult>>
pub fn to_docx(&self, output_path: &str) -> Result<Operation<DocxExportResult>>
Export the entire document as DOCX to a file path.
This is a long operation. Returns a typed Operation handle.
Sourcepub fn cursor(&self) -> TextCursor
pub fn cursor(&self) -> TextCursor
Create a cursor at position 0.
Sourcepub fn cursor_at(&self, position: usize) -> TextCursor
pub fn cursor_at(&self, position: usize) -> TextCursor
Create a cursor at the given position.
Sourcepub fn stats(&self) -> DocumentStats
pub fn stats(&self) -> DocumentStats
Get document statistics. O(1) — reads cached values.
Sourcepub fn character_count(&self) -> usize
pub fn character_count(&self) -> usize
Get the total character count. O(1) — reads cached value.
Sourcepub fn block_count(&self) -> usize
pub fn block_count(&self) -> usize
Get the number of blocks (paragraphs). O(1) — reads cached value.
Sourcepub fn text_at(&self, position: usize, length: usize) -> Result<String>
pub fn text_at(&self, position: usize, length: usize) -> Result<String>
Get text at a position for a given length.
Sourcepub fn block_at(&self, position: usize) -> Result<BlockInfo>
pub fn block_at(&self, position: usize) -> Result<BlockInfo>
Get info about the block at a position. O(log n).
Sourcepub fn block_format_at(&self, position: usize) -> Result<BlockFormat>
pub fn block_format_at(&self, position: usize) -> Result<BlockFormat>
Get the block format at a position.
Sourcepub fn find(
&self,
query: &str,
from: usize,
options: &FindOptions,
) -> Result<Option<FindMatch>>
pub fn find( &self, query: &str, from: usize, options: &FindOptions, ) -> Result<Option<FindMatch>>
Find the next (or previous) occurrence. Returns None if not found.
Sourcepub fn find_all(
&self,
query: &str,
options: &FindOptions,
) -> Result<Vec<FindMatch>>
pub fn find_all( &self, query: &str, options: &FindOptions, ) -> Result<Vec<FindMatch>>
Find all occurrences.
Sourcepub fn replace_text(
&self,
query: &str,
replacement: &str,
replace_all: bool,
options: &FindOptions,
) -> Result<usize>
pub fn replace_text( &self, query: &str, replacement: &str, replace_all: bool, options: &FindOptions, ) -> Result<usize>
Replace occurrences. Returns the number of replacements. Undoable.
Sourcepub fn add_resource(
&self,
resource_type: ResourceType,
name: &str,
mime_type: &str,
data: &[u8],
) -> Result<()>
pub fn add_resource( &self, resource_type: ResourceType, name: &str, mime_type: &str, data: &[u8], ) -> Result<()>
Add a resource (image, stylesheet) to the document.
Sourcepub fn resource(&self, name: &str) -> Result<Option<Vec<u8>>>
pub fn resource(&self, name: &str) -> Result<Option<Vec<u8>>>
Get a resource by name. Returns None if not found.
Uses an internal cache to avoid scanning all resources on repeated lookups.
Sourcepub fn clear_undo_redo(&self)
pub fn clear_undo_redo(&self)
Clear all undo/redo history.
Sourcepub fn is_modified(&self) -> bool
pub fn is_modified(&self) -> bool
Returns true if the document has been modified since creation or last reset.
Sourcepub fn set_modified(&self, modified: bool)
pub fn set_modified(&self, modified: bool)
Set or clear the modified flag.
Sourcepub fn text_direction(&self) -> TextDirection
pub fn text_direction(&self) -> TextDirection
Get the text direction.
Sourcepub fn set_text_direction(&self, direction: TextDirection) -> Result<()>
pub fn set_text_direction(&self, direction: TextDirection) -> Result<()>
Set the text direction.
Sourcepub fn default_wrap_mode(&self) -> WrapMode
pub fn default_wrap_mode(&self) -> WrapMode
Get the default wrap mode.
Sourcepub fn set_default_wrap_mode(&self, mode: WrapMode) -> Result<()>
pub fn set_default_wrap_mode(&self, mode: WrapMode) -> Result<()>
Set the default wrap mode.
Sourcepub fn on_change<F>(&self, callback: F) -> Subscription
pub fn on_change<F>(&self, callback: F) -> Subscription
Subscribe to document events via callback.
Callbacks are invoked outside the document lock (after the editing
operation completes and the lock is released). It is safe to call
TextDocument or TextCursor methods from within the callback without
risk of deadlock. However, keep callbacks lightweight — they run
synchronously on the calling thread and block the caller until they
return.
Drop the returned Subscription to unsubscribe.
§Breaking change (v0.0.6)
The callback bound changed from Send to Send + Sync in v0.0.6
to support Arc-based dispatch. Callbacks that capture non-Sync
types (e.g., Rc<T>, Cell<T>) must be wrapped in a Mutex.
Sourcepub fn poll_events(&self) -> Vec<DocumentEvent>
pub fn poll_events(&self) -> Vec<DocumentEvent>
Return events accumulated since the last poll_events() call.
This delivery path is independent of callback dispatch via
on_change — using both simultaneously is safe
and each path sees every event exactly once.
Trait Implementations§
Source§impl Clone for TextDocument
impl Clone for TextDocument
Source§fn clone(&self) -> TextDocument
fn clone(&self) -> TextDocument
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more