Skip to main content

TextDocument

Struct TextDocument 

Source
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

Source

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.

Source

pub fn try_new() -> Result<Self>

Create a new, empty document, returning an error on failure.

Source

pub fn set_plain_text(&self, text: &str) -> Result<()>

Replace the entire document with plain text. Clears undo history.

Source

pub fn to_plain_text(&self) -> Result<String>

Export the entire document as plain text.

Source

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.

Source

pub fn to_markdown(&self) -> Result<String>

Export the entire document as Markdown.

Source

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.

Source

pub fn to_html(&self) -> Result<String>

Export the entire document as HTML.

Source

pub fn to_latex( &self, document_class: &str, include_preamble: bool, ) -> Result<String>

Export the entire document as LaTeX.

Source

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.

Source

pub fn clear(&self) -> Result<()>

Clear all document content and reset to an empty state.

Source

pub fn cursor(&self) -> TextCursor

Create a cursor at position 0.

Source

pub fn cursor_at(&self, position: usize) -> TextCursor

Create a cursor at the given position.

Source

pub fn stats(&self) -> DocumentStats

Get document statistics. O(1) — reads cached values.

Source

pub fn character_count(&self) -> usize

Get the total character count. O(1) — reads cached value.

Source

pub fn block_count(&self) -> usize

Get the number of blocks (paragraphs). O(1) — reads cached value.

Source

pub fn is_empty(&self) -> bool

Returns true if the document has no text content.

Source

pub fn text_at(&self, position: usize, length: usize) -> Result<String>

Get text at a position for a given length.

Source

pub fn block_at(&self, position: usize) -> Result<BlockInfo>

Get info about the block at a position. O(log n).

Source

pub fn block_format_at(&self, position: usize) -> Result<BlockFormat>

Get the block format at a position.

Source

pub fn find( &self, query: &str, from: usize, options: &FindOptions, ) -> Result<Option<FindMatch>>

Find the next (or previous) occurrence. Returns None if not found.

Source

pub fn find_all( &self, query: &str, options: &FindOptions, ) -> Result<Vec<FindMatch>>

Find all occurrences.

Source

pub fn replace_text( &self, query: &str, replacement: &str, replace_all: bool, options: &FindOptions, ) -> Result<usize>

Replace occurrences. Returns the number of replacements. Undoable.

Source

pub fn add_resource( &self, resource_type: ResourceType, name: &str, mime_type: &str, data: &[u8], ) -> Result<()>

Add a resource (image, stylesheet) to the document.

Source

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.

Source

pub fn undo(&self) -> Result<()>

Undo the last operation.

Source

pub fn redo(&self) -> Result<()>

Redo the last undone operation.

Source

pub fn can_undo(&self) -> bool

Returns true if there are operations that can be undone.

Source

pub fn can_redo(&self) -> bool

Returns true if there are operations that can be redone.

Source

pub fn clear_undo_redo(&self)

Clear all undo/redo history.

Source

pub fn is_modified(&self) -> bool

Returns true if the document has been modified since creation or last reset.

Source

pub fn set_modified(&self, modified: bool)

Set or clear the modified flag.

Source

pub fn title(&self) -> String

Get the document title.

Source

pub fn set_title(&self, title: &str) -> Result<()>

Set the document title.

Source

pub fn text_direction(&self) -> TextDirection

Get the text direction.

Source

pub fn set_text_direction(&self, direction: TextDirection) -> Result<()>

Set the text direction.

Source

pub fn default_wrap_mode(&self) -> WrapMode

Get the default wrap mode.

Source

pub fn set_default_wrap_mode(&self, mode: WrapMode) -> Result<()>

Set the default wrap mode.

Source

pub fn on_change<F>(&self, callback: F) -> Subscription
where F: Fn(DocumentEvent) + Send + Sync + 'static,

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.

Source

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

Source§

fn clone(&self) -> TextDocument

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for TextDocument

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.