use std::sync::Arc;
use parking_lot::Mutex;
use crate::{DocumentError, Result};
use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64;
use crate::{
DjotExportOptions, DjotImportOptions, MarkdownExportOptions, PlainTextExportOptions,
ResourceType, TextDirection, WrapMode,
};
use frontend::commands::{
block_commands, document_commands, document_inspection_commands, document_io_commands,
document_search_commands, frame_commands, list_commands, resource_commands,
table_cell_commands, table_commands, undo_redo_commands,
};
use frontend::common::format_runs::{FootnoteRefAnchor, FormatRun, ImageAnchor};
use crate::HtmlExportOptions;
use crate::convert::{self, to_i64, to_usize};
use crate::cursor::TextCursor;
use crate::events::{self, DocumentEvent, Subscription};
use crate::flow::FormatChangeKind;
use crate::inner::TextDocumentInner;
use crate::operation::{
DjotImportResult, DocxExportResult, EpubExportResult, HtmlImportResult, MarkdownImportResult,
OdtExportResult, Operation, PdfExportResult,
};
use crate::{BlockFormat, BlockInfo, DocumentStats, FindMatch, FindOptions, ReplaceRange};
#[derive(Clone)]
pub struct TextDocument {
pub(crate) inner: Arc<Mutex<TextDocumentInner>>,
}
fn clearing_history_on_success<T: 'static>(
doc: &Arc<Mutex<TextDocumentInner>>,
inner: &mut TextDocumentInner,
op_id: &str,
read: impl Fn(&frontend::AppContext, &str) -> Option<Result<T>> + Send + 'static,
) -> crate::operation::ResultFn<T> {
let cleared = Arc::new(std::sync::atomic::AtomicBool::new(false));
inner
.history_resets
.insert(op_id.to_string(), Arc::clone(&cleared));
let stack_id = inner.stack_id;
let doc = Arc::downgrade(doc);
Box::new(move |ctx, id| {
let result = read(ctx, id)?;
if result.is_ok() && !cleared.swap(true, std::sync::atomic::Ordering::SeqCst) {
undo_redo_commands::clear_stack(ctx, stack_id);
if let Some(doc) = doc.upgrade() {
doc.lock().end_whole_text_selections();
}
}
Some(result)
})
}
impl TextDocument {
#[doc(hidden)]
pub fn rope_store_for_test(&self) -> std::sync::Arc<common::database::Store> {
let inner = self.inner.lock();
std::sync::Arc::clone(inner.ctx.db_context.get_store())
}
}
impl TextDocument {
pub fn new() -> Self {
Self::try_new().expect("failed to initialize document")
}
pub fn try_new() -> Result<Self> {
let ctx = frontend::AppContext::new();
let doc_inner = TextDocumentInner::initialize(ctx)?;
let inner = Arc::new(Mutex::new(doc_inner));
Self::subscribe_long_operation_events(&inner);
Ok(Self { inner })
}
pub fn new_in(backend: &crate::DocumentBackend) -> Self {
Self::try_new_in(backend).expect("failed to initialize document")
}
pub fn try_new_in(backend: &crate::DocumentBackend) -> Result<Self> {
let doc_inner = TextDocumentInner::initialize_in(backend)?;
let inner = Arc::new(Mutex::new(doc_inner));
Self::subscribe_long_operation_events(&inner);
Ok(Self { inner })
}
fn subscribe_long_operation_events(inner: &Arc<Mutex<TextDocumentInner>>) {
use frontend::common::event::{LongOperationEvent as LOE, Origin};
let weak = Arc::downgrade(inner);
let mut locked = inner.lock();
let client = match &locked.backend {
Some(backend) => backend.client().clone(),
None => locked.event_client.clone(),
};
let w = weak.clone();
let progress_tok = client.subscribe(Origin::LongOperation(LOE::Progress), move |event| {
if let Some(inner) = w.upgrade() {
let (op_id, percent, message) = parse_progress_data(&event.data);
let mut inner = inner.lock();
if !inner.owns_operation(&op_id) {
return;
}
inner.queue_event(DocumentEvent::LongOperationProgress {
operation_id: op_id,
percent,
message,
});
}
});
let w = weak.clone();
let completed_tok = client.subscribe(Origin::LongOperation(LOE::Completed), move |event| {
if let Some(inner) = w.upgrade() {
let op_id = parse_id_data(&event.data);
let mut inner = inner.lock();
if !inner.owns_operation(&op_id) {
return;
}
inner.own_operations.remove(&op_id);
inner.queue_event(DocumentEvent::DocumentReset);
inner.check_block_count_changed();
inner.reset_cached_child_order();
if let Some(cleared) = inner.history_resets.remove(&op_id) {
if !cleared.swap(true, std::sync::atomic::Ordering::SeqCst) {
undo_redo_commands::clear_stack(&inner.ctx, inner.stack_id);
}
let stack = Some(inner.stack_id);
let can_undo = undo_redo_commands::can_undo(&inner.ctx, stack);
let can_redo = undo_redo_commands::can_redo(&inner.ctx, stack);
inner.queue_event(DocumentEvent::UndoRedoChanged { can_undo, can_redo });
}
inner.queue_event(DocumentEvent::LongOperationFinished {
operation_id: op_id,
success: true,
error: None,
});
}
});
let w = weak.clone();
let cancelled_tok = client.subscribe(Origin::LongOperation(LOE::Cancelled), move |event| {
if let Some(inner) = w.upgrade() {
let op_id = parse_id_data(&event.data);
let mut inner = inner.lock();
if !inner.owns_operation(&op_id) {
return;
}
inner.own_operations.remove(&op_id);
inner.history_resets.remove(&op_id);
inner.queue_event(DocumentEvent::LongOperationFinished {
operation_id: op_id,
success: false,
error: Some("cancelled".into()),
});
}
});
let failed_tok = client.subscribe(Origin::LongOperation(LOE::Failed), move |event| {
if let Some(inner) = weak.upgrade() {
let (op_id, error) = parse_failed_data(&event.data);
let mut inner = inner.lock();
if !inner.owns_operation(&op_id) {
return;
}
inner.own_operations.remove(&op_id);
inner.history_resets.remove(&op_id);
inner.queue_event(DocumentEvent::LongOperationFinished {
operation_id: op_id,
success: false,
error: Some(error),
});
}
});
locked.long_op_subscriptions.extend([
progress_tok,
completed_tok,
cancelled_tok,
failed_tok,
]);
}
pub fn set_plain_text(&self, text: &str) -> Result<()> {
let _settled = self.settle_transient_edits();
let queued = {
let mut inner = self.inner.lock();
let dto = frontend::document_io::ImportPlainTextDto {
plain_text: text.into(),
};
document_io_commands::import_plain_text(&inner.ctx, &dto)?;
undo_redo_commands::clear_stack(&inner.ctx, inner.stack_id);
inner.invalidate_text_cache();
inner.rehighlight_all();
inner.queue_event(DocumentEvent::DocumentReset);
inner.check_block_count_changed();
inner.reset_cached_child_order();
inner.queue_event(DocumentEvent::UndoRedoChanged {
can_undo: false,
can_redo: false,
});
inner.take_queued_events()
};
crate::inner::dispatch_queued_events(queued);
Ok(())
}
pub fn to_plain_text(&self) -> Result<String> {
let mut inner = self.inner.lock();
Ok(inner.plain_text()?.into_owned())
}
pub fn to_plain_text_indented(&self) -> Result<String> {
let inner = self.inner.lock();
let dto = document_io_commands::export_plain_text_indented(&inner.ctx)?;
Ok(dto.plain_text)
}
pub fn to_plain_text_with(&self, options: PlainTextExportOptions) -> Result<String> {
let inner = self.inner.lock();
let dto = document_io_commands::export_plain_text_with(&inner.ctx, options)?;
Ok(dto.plain_text)
}
pub fn to_addressable_text(&self) -> Result<String> {
let inner = self.inner.lock();
let dto = document_search_commands::addressable_text(&inner.ctx)?;
Ok(dto.text)
}
pub fn set_markdown(&self, markdown: &str) -> Result<Operation<MarkdownImportResult>> {
let _settled = self.settle_transient_edits();
let mut inner = self.inner.lock();
inner.invalidate_text_cache();
let dto = frontend::document_io::ImportMarkdownDto {
markdown_text: markdown.into(),
};
let op_id = document_io_commands::import_markdown(&inner.ctx, &dto)?;
inner.own_operations.insert(op_id.clone());
let read = clearing_history_on_success(&self.inner, &mut inner, &op_id, |ctx, id| {
document_io_commands::get_import_markdown_result(ctx, id)
.ok()
.flatten()
.map(|r| {
Ok(MarkdownImportResult {
block_count: to_usize(r.block_count),
})
})
});
Ok(Operation::new(op_id, &inner.ctx, read))
}
pub fn to_markdown(&self) -> Result<String> {
let inner = self.inner.lock();
let dto = document_io_commands::export_markdown(&inner.ctx)?;
Ok(dto.markdown_text)
}
pub fn to_markdown_with(&self, options: MarkdownExportOptions) -> Result<String> {
let inner = self.inner.lock();
let dto = document_io_commands::export_markdown_with(&inner.ctx, options)?;
Ok(dto.markdown_text)
}
pub fn set_djot(&self, djot: &str) -> Result<Operation<DjotImportResult>> {
self.set_djot_with_options(djot, DjotImportOptions::default())
}
pub fn set_djot_with_options(
&self,
djot: &str,
options: DjotImportOptions,
) -> Result<Operation<DjotImportResult>> {
let _settled = self.settle_transient_edits();
let mut inner = self.inner.lock();
inner.invalidate_text_cache();
let dto = frontend::document_io::ImportDjotDto {
djot_text: djot.into(),
options,
};
let op_id = document_io_commands::import_djot(&inner.ctx, &dto)?;
inner.own_operations.insert(op_id.clone());
let read = clearing_history_on_success(&self.inner, &mut inner, &op_id, |ctx, id| {
document_io_commands::get_import_djot_result(ctx, id)
.ok()
.flatten()
.map(|r| {
Ok(DjotImportResult {
block_count: to_usize(r.block_count),
})
})
});
Ok(Operation::new(op_id, &inner.ctx, read))
}
pub fn set_djot_sync(&self, djot: &str) -> Result<DjotImportResult> {
self.set_djot_sync_with_options(djot, DjotImportOptions::default())
}
pub fn set_djot_sync_with_options(
&self,
djot: &str,
options: DjotImportOptions,
) -> Result<DjotImportResult> {
let _settled = self.settle_transient_edits();
let (queued, block_count) = {
let mut inner = self.inner.lock();
inner.invalidate_text_cache();
let dto = frontend::document_io::ImportDjotDto {
djot_text: djot.into(),
options,
};
let result = document_io_commands::import_djot_sync(&inner.ctx, &dto)?;
undo_redo_commands::clear_stack(&inner.ctx, inner.stack_id);
inner.queue_event(DocumentEvent::DocumentReset);
inner.check_block_count_changed();
inner.reset_cached_child_order();
inner.queue_event(DocumentEvent::UndoRedoChanged {
can_undo: false,
can_redo: false,
});
(inner.take_queued_events(), result.block_count)
};
crate::inner::dispatch_queued_events(queued);
Ok(DjotImportResult {
block_count: to_usize(block_count),
})
}
pub fn to_djot(&self) -> Result<String> {
self.to_djot_with_options(DjotExportOptions::default())
}
pub fn to_djot_with_options(&self, options: DjotExportOptions) -> Result<String> {
let inner = self.inner.lock();
let dto = document_io_commands::export_djot(&inner.ctx, &options)?;
Ok(dto.djot_text)
}
pub fn set_html(&self, html: &str) -> Result<Operation<HtmlImportResult>> {
let _settled = self.settle_transient_edits();
let mut inner = self.inner.lock();
inner.invalidate_text_cache();
let dto = frontend::document_io::ImportHtmlDto {
html_text: html.into(),
};
let op_id = document_io_commands::import_html(&inner.ctx, &dto)?;
inner.own_operations.insert(op_id.clone());
let read = clearing_history_on_success(&self.inner, &mut inner, &op_id, |ctx, id| {
document_io_commands::get_import_html_result(ctx, id)
.ok()
.flatten()
.map(|r| {
Ok(HtmlImportResult {
block_count: to_usize(r.block_count),
})
})
});
Ok(Operation::new(op_id, &inner.ctx, read))
}
pub fn to_html(&self) -> Result<String> {
let inner = self.inner.lock();
let dto = document_io_commands::export_html(&inner.ctx)?;
Ok(dto.html_text)
}
pub fn to_html_with_options(&self, options: HtmlExportOptions) -> Result<String> {
let inner = self.inner.lock();
let dto = document_io_commands::export_html_with_options(&inner.ctx, options)?;
Ok(dto.html_text)
}
pub fn to_latex(&self, document_class: &str, include_preamble: bool) -> Result<String> {
self.to_latex_with_options(crate::LatexExportOptions {
document_class: document_class.into(),
include_preamble,
omit_images: false,
})
}
pub fn to_latex_with_options(&self, options: crate::LatexExportOptions) -> Result<String> {
let inner = self.inner.lock();
let dto = frontend::document_io::ExportLatexDto { options };
let result = document_io_commands::export_latex(&inner.ctx, &dto)?;
Ok(result.latex_text)
}
pub fn to_docx(&self, output_path: &str) -> Result<Operation<DocxExportResult>> {
self.to_docx_with_options(output_path, crate::DocxExportOptions::default())
}
#[cfg(feature = "docx")]
pub fn to_docx_with_options(
&self,
output_path: &str,
options: crate::DocxExportOptions,
) -> Result<Operation<DocxExportResult>> {
let mut inner = self.inner.lock();
let dto = frontend::document_io::ExportDocxDto {
output_path: output_path.into(),
options,
};
let op_id = document_io_commands::export_docx(&inner.ctx, &dto)?;
inner.own_operations.insert(op_id.clone());
Ok(Operation::new(
op_id,
&inner.ctx,
Box::new(|ctx, id| {
document_io_commands::get_export_docx_result(ctx, id)
.ok()
.flatten()
.map(|r| {
Ok(DocxExportResult {
file_path: r.file_path,
paragraph_count: to_usize(r.paragraph_count),
})
})
}),
))
}
#[cfg(not(feature = "docx"))]
pub fn to_docx_with_options(
&self,
_output_path: &str,
_options: crate::DocxExportOptions,
) -> Result<Operation<DocxExportResult>> {
Err(DocumentError::Unsupported(
"DOCX export requires the `docx` cargo feature on the `text-document` crate".into(),
))
}
pub fn to_epub(&self, output_path: &str) -> Result<Operation<EpubExportResult>> {
self.to_epub_with_options(output_path, crate::EpubExportOptions::default())
}
#[cfg(feature = "epub")]
pub fn to_epub_with_options(
&self,
output_path: &str,
options: crate::EpubExportOptions,
) -> Result<Operation<EpubExportResult>> {
let mut inner = self.inner.lock();
let dto = frontend::document_io::ExportEpubDto {
output_path: output_path.into(),
options,
};
let op_id = document_io_commands::export_epub(&inner.ctx, &dto)?;
inner.own_operations.insert(op_id.clone());
Ok(Operation::new(
op_id,
&inner.ctx,
Box::new(|ctx, id| {
document_io_commands::get_export_epub_result(ctx, id)
.ok()
.flatten()
.map(|r| {
Ok(EpubExportResult {
file_path: r.file_path,
chapter_count: to_usize(r.chapter_count),
})
})
}),
))
}
#[cfg(not(feature = "epub"))]
pub fn to_epub_with_options(
&self,
_output_path: &str,
_options: crate::EpubExportOptions,
) -> Result<Operation<EpubExportResult>> {
Err(DocumentError::Unsupported(
"EPUB export requires the `epub` cargo feature on the `text-document` crate".into(),
))
}
pub fn to_odt(&self, output_path: &str) -> Result<Operation<OdtExportResult>> {
self.to_odt_with_options(output_path, crate::OdtExportOptions::default())
}
#[cfg(feature = "odt")]
pub fn to_odt_with_options(
&self,
output_path: &str,
options: crate::OdtExportOptions,
) -> Result<Operation<OdtExportResult>> {
let mut inner = self.inner.lock();
let dto = frontend::document_io::ExportOdtDto {
output_path: output_path.into(),
options,
};
let op_id = document_io_commands::export_odt(&inner.ctx, &dto)?;
inner.own_operations.insert(op_id.clone());
Ok(Operation::new(
op_id,
&inner.ctx,
Box::new(|ctx, id| {
document_io_commands::get_export_odt_result(ctx, id)
.ok()
.flatten()
.map(|r| {
Ok(OdtExportResult {
file_path: r.file_path,
paragraph_count: to_usize(r.paragraph_count),
})
})
}),
))
}
#[cfg(not(feature = "odt"))]
pub fn to_odt_with_options(
&self,
_output_path: &str,
_options: crate::OdtExportOptions,
) -> Result<Operation<OdtExportResult>> {
Err(DocumentError::Unsupported(
"ODT export requires the `odt` cargo feature on the `text-document` crate".into(),
))
}
pub fn to_pdf(
&self,
output_path: &str,
options: crate::PdfExportOptions,
) -> Result<Operation<PdfExportResult>> {
self.to_pdf_with_options(output_path, options)
}
#[cfg(feature = "pdf")]
pub fn to_pdf_with_options(
&self,
output_path: &str,
options: crate::PdfExportOptions,
) -> Result<Operation<PdfExportResult>> {
let mut inner = self.inner.lock();
let dto = frontend::document_io::ExportPdfDto {
output_path: output_path.into(),
options,
};
let op_id = document_io_commands::export_pdf(&inner.ctx, &dto)?;
inner.own_operations.insert(op_id.clone());
Ok(Operation::new(
op_id,
&inner.ctx,
Box::new(|ctx, id| {
document_io_commands::get_export_pdf_result(ctx, id)
.ok()
.flatten()
.map(|r| {
Ok(PdfExportResult {
file_path: r.file_path,
page_count: to_usize(r.page_count),
})
})
}),
))
}
#[cfg(not(feature = "pdf"))]
pub fn to_pdf_with_options(
&self,
_output_path: &str,
_options: crate::PdfExportOptions,
) -> Result<Operation<PdfExportResult>> {
Err(DocumentError::Unsupported(
"PDF export requires the `pdf` cargo feature on the `text-document` crate".into(),
))
}
pub fn clear(&self) -> Result<()> {
let _settled = self.settle_transient_edits();
let queued = {
let mut inner = self.inner.lock();
let dto = frontend::document_io::ImportPlainTextDto {
plain_text: String::new(),
};
document_io_commands::import_plain_text(&inner.ctx, &dto)?;
undo_redo_commands::clear_stack(&inner.ctx, inner.stack_id);
inner.invalidate_text_cache();
inner.rehighlight_all();
inner.queue_event(DocumentEvent::DocumentReset);
inner.check_block_count_changed();
inner.reset_cached_child_order();
inner.queue_event(DocumentEvent::UndoRedoChanged {
can_undo: false,
can_redo: false,
});
inner.take_queued_events()
};
crate::inner::dispatch_queued_events(queued);
Ok(())
}
pub fn cursor(&self) -> TextCursor {
self.cursor_at(0)
}
pub fn cursor_at(&self, position: usize) -> TextCursor {
let data = {
let mut inner = self.inner.lock();
inner.register_cursor(position)
};
let cursor = TextCursor {
doc: self.inner.clone(),
data,
};
cursor.snap_position_to_grapheme_boundary();
cursor
}
pub fn stats(&self) -> DocumentStats {
let inner = self.inner.lock();
let dto = document_inspection_commands::get_document_stats(&inner.ctx)
.expect("get_document_stats should not fail");
DocumentStats::from(&dto)
}
pub fn set_footnote_markers(&self, markers: std::collections::HashMap<String, String>) {
let queued = {
let mut inner = self.inner.lock();
{
let store = inner.ctx.db_context.get_store();
let mut current = store.footnote_markers.write();
if *current == markers {
return;
}
*current = markers;
}
inner.queue_event(DocumentEvent::FormatChanged {
position: 0,
length: 0,
kind: crate::flow::FormatChangeKind::Character,
});
inner.take_queued_events()
};
crate::inner::dispatch_queued_events(queued);
}
pub fn footnote_references(&self) -> Vec<(usize, String)> {
let inner = self.inner.lock();
let store = inner.ctx.db_context.get_store();
let refs = store.block_footnote_refs.read();
if refs.is_empty() {
return Vec::new();
}
let mut blocks: Vec<(i64, u64)> = store
.blocks
.read()
.values()
.map(|b| (b.document_position, b.id))
.collect();
frontend::common::database::rope_helpers::refresh_positions_from_rope(
store,
blocks.iter_mut().map(|(position, id)| (*id, position)),
);
blocks.sort_unstable();
let mut out = Vec::new();
for (position, block_id) in blocks {
let Some(anchors) = refs.get(&block_id) else {
continue;
};
let Some(block) = store.blocks.read().get(&block_id).cloned() else {
continue;
};
let text =
frontend::common::database::rope_helpers::block_content_via_store(&block, store);
let mut ordered: Vec<_> = anchors.iter().collect();
ordered.sort_by_key(|a| a.byte_offset);
for anchor in ordered {
let chars_before = text
.get(..anchor.byte_offset as usize)
.map(|s| s.chars().count())
.unwrap_or(0);
out.push((position as usize + chars_before, anchor.label.clone()));
}
}
out
}
pub fn footnote_reference_at(&self, position: usize) -> Option<String> {
self.footnote_references()
.into_iter()
.find(|(at, _)| *at == position)
.map(|(_, label)| label)
}
pub fn shares_a_backend(&self) -> bool {
self.inner.lock().backend.is_some()
}
pub fn character_count(&self) -> usize {
let inner = self.inner.lock();
crate::inner::document_counts(&inner).map_or(0, |(chars, _)| chars)
}
pub fn block_count(&self) -> usize {
let inner = self.inner.lock();
crate::inner::document_counts(&inner).map_or(0, |(_, blocks)| blocks)
}
pub fn is_empty(&self) -> bool {
self.character_count() == 0
}
pub fn text_at(&self, position: usize, length: usize) -> Result<String> {
let inner = self.inner.lock();
let dto = frontend::document_inspection::GetTextAtPositionDto {
position: to_i64(position),
length: to_i64(length),
};
let result = document_inspection_commands::get_text_at_position(&inner.ctx, &dto)?;
Ok(result.text)
}
pub fn find_element_at_position(&self, position: usize) -> Option<(u64, usize, usize)> {
let block_info = self.block_at_caret(position).ok()?;
let block_start = block_info.start;
let offset_in_block = position.checked_sub(block_start)?;
let block = crate::text_block::TextBlock {
doc: std::sync::Arc::clone(&self.inner),
block_id: block_info.block_id,
};
let frags = block.fragments();
let mut last_text: Option<(u64, usize, usize, usize)> = None; for frag in &frags {
match frag {
crate::flow::FragmentContent::Text {
offset,
length,
element_id,
..
} => {
let frag_start = *offset;
let frag_end = frag_start + *length;
if offset_in_block >= frag_start && offset_in_block < frag_end {
let abs_start = block_start + frag_start;
let offset_within = offset_in_block - frag_start;
return Some((*element_id, abs_start, offset_within));
}
if offset_in_block == frag_end {
last_text =
Some((*element_id, block_start + frag_start, frag_start, *length));
}
}
crate::flow::FragmentContent::Image {
offset, element_id, ..
}
| crate::flow::FragmentContent::FootnoteReference {
offset, element_id, ..
} => {
if offset_in_block == *offset {
return Some((*element_id, block_start + offset, 0));
}
}
}
}
last_text.map(|(id, abs_start, _, length)| (id, abs_start, length))
}
pub fn block_at(&self, position: usize) -> Result<BlockInfo> {
let inner = self.inner.lock();
let dto = frontend::document_inspection::GetBlockAtPositionDto {
position: to_i64(position),
};
let result = document_inspection_commands::get_block_at_position(&inner.ctx, &dto)?;
Ok(BlockInfo::from(&result))
}
pub fn block_at_caret(&self, position: usize) -> Result<BlockInfo> {
let inner = self.inner.lock();
let info = crate::inner::block_at_caret_dto(&inner.ctx, position)?;
Ok(BlockInfo::from(&info))
}
pub fn sentence_at(
&self,
position: usize,
content_locale: Option<&str>,
) -> Option<(usize, usize)> {
let block = self.block_at_caret(position).ok()?;
let inner = self.inner.lock();
let block_start = block.start;
let block_length = block.length;
if block_length == 0 {
return None;
}
let text_dto = frontend::document_inspection::GetTextAtPositionDto {
position: to_i64(block_start),
length: to_i64(block_length),
};
let text = document_inspection_commands::get_text_at_position(&inner.ctx, &text_dto)
.ok()?
.text;
drop(inner);
let offset = position.saturating_sub(block_start);
let (start, end) =
frontend::common::parser_tools::sentence_bounds(&text, offset, content_locale)?;
Some((block_start + start, block_start + end))
}
pub fn block_format_at(&self, position: usize) -> Result<BlockFormat> {
let inner = self.inner.lock();
let block_info = crate::inner::block_at_caret_dto(&inner.ctx, position)?;
let block_id = block_info.block_id;
let block_id = block_id as u64;
let block_dto = frontend::commands::block_commands::get_block(&inner.ctx, &block_id)?
.ok_or_else(|| DocumentError::NotFound("block not found".into()))?;
Ok(BlockFormat::from(&block_dto))
}
pub fn flow(&self) -> Vec<crate::flow::FlowElement> {
let inner = self.inner.lock();
let main_frame_id = get_main_frame_id(&inner);
crate::text_frame::build_flow_elements(&inner, &self.inner, main_frame_id)
}
pub fn block_by_id(&self, block_id: usize) -> Option<crate::text_block::TextBlock> {
let inner = self.inner.lock();
let exists = frontend::commands::block_commands::get_block(&inner.ctx, &(block_id as u64))
.ok()
.flatten()
.is_some();
if exists {
Some(crate::text_block::TextBlock {
doc: self.inner.clone(),
block_id,
})
} else {
None
}
}
pub fn snapshot_block_at_position(
&self,
position: usize,
) -> Option<crate::flow::BlockSnapshot> {
self.snapshot_block_at_position_masked(position, &crate::highlight::HighlightMask::all())
}
pub fn snapshot_block_at_position_without_highlights(
&self,
position: usize,
) -> Option<crate::flow::BlockSnapshot> {
self.snapshot_block_at_position_masked(position, &crate::highlight::HighlightMask::none())
}
pub fn snapshot_block_at_position_masked(
&self,
position: usize,
mask: &crate::highlight::HighlightMask,
) -> Option<crate::flow::BlockSnapshot> {
block_snapshot_at(&self.inner.lock(), position, mask)
}
pub fn block_at_position(&self, position: usize) -> Option<crate::text_block::TextBlock> {
let inner = self.inner.lock();
let dto = frontend::document_inspection::GetBlockAtPositionDto {
position: to_i64(position),
};
let result = document_inspection_commands::get_block_at_position(&inner.ctx, &dto).ok()?;
Some(crate::text_block::TextBlock {
doc: self.inner.clone(),
block_id: result.block_id as usize,
})
}
pub fn block_by_number(&self, block_number: usize) -> Option<crate::text_block::TextBlock> {
let inner = self.inner.lock();
let all_blocks = frontend::commands::block_commands::get_all_block(&inner.ctx).ok()?;
let mut sorted: Vec<_> = all_blocks.into_iter().collect();
let store = inner.ctx.db_context.get_store();
crate::inner::refresh_block_positions(&mut sorted, store);
sorted.sort_by_key(|b| b.document_position);
sorted
.get(block_number)
.map(|b| crate::text_block::TextBlock {
doc: self.inner.clone(),
block_id: b.id as usize,
})
}
pub fn blocks(&self) -> Vec<crate::text_block::TextBlock> {
let inner = self.inner.lock();
let all_blocks =
frontend::commands::block_commands::get_all_block(&inner.ctx).unwrap_or_default();
let mut sorted: Vec<_> = all_blocks.into_iter().collect();
let store = inner.ctx.db_context.get_store();
crate::inner::refresh_block_positions(&mut sorted, store);
sorted.sort_by_key(|b| b.document_position);
sorted
.iter()
.map(|b| crate::text_block::TextBlock {
doc: self.inner.clone(),
block_id: b.id as usize,
})
.collect()
}
pub fn blocks_in_range(
&self,
position: usize,
length: usize,
) -> Vec<crate::text_block::TextBlock> {
let inner = self.inner.lock();
let all_blocks =
frontend::commands::block_commands::get_all_block(&inner.ctx).unwrap_or_default();
let mut sorted: Vec<_> = all_blocks.into_iter().collect();
let store = inner.ctx.db_context.get_store();
crate::inner::refresh_block_positions(&mut sorted, store);
sorted.sort_by_key(|b| b.document_position);
let range_start = position;
let range_end = position + length;
sorted
.iter()
.filter(|b| {
let block_start = b.document_position.max(0) as usize;
let entity: common::entities::Block = (*b).clone().into();
let block_end = block_start
+ common::database::rope_helpers::block_char_length(&entity, store).max(0)
as usize;
if length == 0 {
range_start >= block_start && range_start < block_end
} else {
block_start < range_end && block_end > range_start
}
})
.map(|b| crate::text_block::TextBlock {
doc: self.inner.clone(),
block_id: b.id as usize,
})
.collect()
}
pub fn snapshot_flow(&self) -> crate::flow::FlowSnapshot {
self.snapshot_flow_masked(&crate::highlight::HighlightMask::all())
}
pub fn snapshot_flow_without_highlights(&self) -> crate::flow::FlowSnapshot {
self.snapshot_flow_masked(&crate::highlight::HighlightMask::none())
}
pub fn snapshot_flow_masked(
&self,
mask: &crate::highlight::HighlightMask,
) -> crate::flow::FlowSnapshot {
flow_snapshot_of(&self.inner.lock(), mask, false)
}
pub fn snapshot_flow_masked_no_paint(
&self,
mask: &crate::highlight::HighlightMask,
) -> crate::flow::FlowSnapshot {
flow_snapshot_of(&self.inner.lock(), mask, true)
}
pub fn find(
&self,
query: &str,
from: usize,
options: &FindOptions,
) -> Result<Option<FindMatch>> {
let inner = self.inner.lock();
let dto = options.to_find_text_dto(query, from);
let result = document_search_commands::find_text(&inner.ctx, &dto)?;
Ok(convert::find_result_to_match(&result))
}
pub fn find_all(&self, query: &str, options: &FindOptions) -> Result<Vec<FindMatch>> {
let inner = self.inner.lock();
let dto = options.to_find_all_dto(query);
let result = document_search_commands::find_all(&inner.ctx, &dto)?;
Ok(convert::find_all_to_matches(&result))
}
pub fn replace_text(
&self,
query: &str,
replacement: &str,
replace_all: bool,
options: &crate::ReplaceOptions,
) -> Result<usize> {
let _settled = self.settle_transient_edits();
let (count, queued) = {
let mut inner = self.inner.lock();
let dto = options.to_replace_dto(query, replacement, replace_all);
let result =
document_search_commands::replace_text(&inner.ctx, Some(inner.stack_id), &dto)?;
let count = to_usize(result.replacements_count);
inner.invalidate_text_cache();
if count > 0 {
inner.modified = true;
inner.rehighlight_all();
inner.queue_event(DocumentEvent::ContentsChanged {
position: 0,
chars_removed: 0,
chars_added: 0,
blocks_affected: count,
});
inner.check_block_count_changed();
inner.check_flow_changed();
let can_undo = undo_redo_commands::can_undo(&inner.ctx, Some(inner.stack_id));
let can_redo = undo_redo_commands::can_redo(&inner.ctx, Some(inner.stack_id));
inner.queue_event(DocumentEvent::UndoRedoChanged { can_undo, can_redo });
}
(count, inner.take_queued_events())
};
crate::inner::dispatch_queued_events(queued);
Ok(count)
}
pub fn replace_ranges(
&self,
ranges: &[ReplaceRange],
options: &crate::ReplaceOptions,
) -> Result<usize> {
let (result, rest) = {
let mut inner = self.inner.lock();
let reverted = inner.revert_transient_edits();
let mapped: Vec<ReplaceRange>;
let ranges = match &reverted {
None => ranges,
Some(reverted) => {
mapped = ranges
.iter()
.filter_map(|range| {
let (start, end) =
reverted.map_range(range.position, range.position + range.length);
(range.length == 0 || end > start).then(|| ReplaceRange {
position: start,
length: end - start,
replacement: range.replacement.clone(),
})
})
.collect();
&mapped
}
};
let result = Self::replace_ranges_locked(&mut inner, ranges, options);
(result, inner.take_queued_events())
};
crate::inner::dispatch_queued_events(rest);
result
}
pub fn find_and_replace(
&self,
query: &str,
options: &crate::ReplaceOptions,
mut decide: impl FnMut(&str, usize) -> Option<String>,
) -> Result<usize> {
let _settled = self.settle_transient_edits();
let (count, queued) = {
let mut inner = self.inner.lock();
let found = {
let dto = options.find.to_find_all_dto(query);
document_search_commands::find_all(&inner.ctx, &dto)?
};
let mut ranges: Vec<ReplaceRange> = Vec::new();
for (i, ((&position, &length), matched)) in found
.positions
.iter()
.zip(found.lengths.iter())
.zip(found.matched_texts.iter())
.enumerate()
{
if let Some(replacement) = decide(matched, i) {
ranges.push(ReplaceRange {
position: to_usize(position),
length: to_usize(length),
replacement,
});
}
}
let count = if ranges.is_empty() {
0
} else {
Self::replace_ranges_locked(&mut inner, &ranges, options)?
};
(count, inner.take_queued_events())
};
crate::inner::dispatch_queued_events(queued);
Ok(count)
}
fn replace_ranges_locked(
inner: &mut crate::inner::TextDocumentInner,
ranges: &[ReplaceRange],
options: &crate::ReplaceOptions,
) -> Result<usize> {
let dto = options.to_replace_ranges_dto(ranges);
let result =
document_search_commands::replace_ranges(&inner.ctx, Some(inner.stack_id), &dto)?;
let count = to_usize(result.replacements_count);
inner.invalidate_text_cache();
if count > 0 {
inner.modified = true;
inner.rehighlight_all();
inner.queue_event(DocumentEvent::ContentsChanged {
position: 0,
chars_removed: 0,
chars_added: 0,
blocks_affected: count,
});
inner.check_block_count_changed();
inner.check_flow_changed();
let can_undo = undo_redo_commands::can_undo(&inner.ctx, Some(inner.stack_id));
let can_redo = undo_redo_commands::can_redo(&inner.ctx, Some(inner.stack_id));
inner.queue_event(DocumentEvent::UndoRedoChanged { can_undo, can_redo });
}
Ok(count)
}
pub fn add_resource(
&self,
resource_type: ResourceType,
name: &str,
mime_type: &str,
data: &[u8],
) -> Result<()> {
let _settled = self.settle_transient_edits();
let mut inner = self.inner.lock();
let dto = frontend::resource::dtos::CreateResourceDto {
created_at: Default::default(),
updated_at: Default::default(),
resource_type,
name: name.into(),
url: String::new(),
mime_type: mime_type.into(),
data_base64: BASE64.encode(data),
};
let created = resource_commands::create_resource(
&inner.ctx,
Some(inner.stack_id),
&dto,
inner.document_id,
-1,
)?;
inner.resource_cache.insert(name.to_string(), created.id);
Ok(())
}
pub fn resource(&self, name: &str) -> Result<Option<Vec<u8>>> {
let mut inner = self.inner.lock();
if let Some(&id) = inner.resource_cache.get(name) {
if let Some(r) = resource_commands::get_resource(&inner.ctx, &id)? {
let bytes = BASE64
.decode(&r.data_base64)
.map_err(|e| DocumentError::Internal(e.into()))?;
return Ok(Some(bytes));
}
inner.resource_cache.remove(name);
}
let all = resource_commands::get_all_resource(&inner.ctx)?;
for r in &all {
if r.name == name {
inner.resource_cache.insert(name.to_string(), r.id);
let bytes = BASE64
.decode(&r.data_base64)
.map_err(|e| DocumentError::Internal(e.into()))?;
return Ok(Some(bytes));
}
}
Ok(None)
}
pub fn undo(&self) -> Result<()> {
let _settled = self.settle_transient_edits();
let queued = {
let mut inner = self.inner.lock();
let before = capture_block_state(&inner);
let before_outside = capture_outside_blocks(&inner);
let entry = undo_redo_commands::head_seq(&inner.ctx, Some(inner.stack_id));
let (edited_with, left_at) = inner.edit_caret_cursor(entry).unzip();
let stepped = undo_redo_commands::can_undo(&inner.ctx, Some(inner.stack_id));
let result = undo_redo_commands::undo(&inner.ctx, Some(inner.stack_id));
inner.invalidate_text_cache();
if stepped {
inner.modified = true;
}
result?;
inner.rehighlight_all();
let changed_text_end = emit_content_change_events_near(&mut inner, &before, left_at);
emit_changes_outside_blocks(&mut inner, &before_outside, changed_text_end.is_some());
if let (Some(cursor), Some(end)) = (edited_with, changed_text_end) {
let end =
crate::cursor::max_cursor_position_of(&inner).map_or(end, |max| end.min(max));
let mut data = cursor.lock();
data.position = end;
data.anchor = end;
data.anchor_in_table = None;
data.cell_selection_override = None;
}
inner.check_block_count_changed();
inner.check_flow_changed();
let can_undo = undo_redo_commands::can_undo(&inner.ctx, Some(inner.stack_id));
let can_redo = undo_redo_commands::can_redo(&inner.ctx, Some(inner.stack_id));
inner.queue_event(DocumentEvent::UndoRedoChanged { can_undo, can_redo });
inner.take_queued_events()
};
crate::inner::dispatch_queued_events(queued);
Ok(())
}
pub fn redo(&self) -> Result<()> {
let _settled = self.settle_transient_edits();
let queued = {
let mut inner = self.inner.lock();
let before = capture_block_state(&inner);
let before_outside = capture_outside_blocks(&inner);
let stepped = undo_redo_commands::can_redo(&inner.ctx, Some(inner.stack_id));
let result = undo_redo_commands::redo(&inner.ctx, Some(inner.stack_id));
inner.invalidate_text_cache();
if stepped {
inner.modified = true;
}
result?;
inner.rehighlight_all();
let changed_text_end = emit_content_change_events_near(&mut inner, &before, None);
emit_changes_outside_blocks(&mut inner, &before_outside, changed_text_end.is_some());
if stepped {
let entry = undo_redo_commands::head_seq(&inner.ctx, Some(inner.stack_id));
inner.restore_edit_caret(entry, crate::cursor::max_cursor_position_of(&inner));
}
inner.check_block_count_changed();
inner.check_flow_changed();
let can_undo = undo_redo_commands::can_undo(&inner.ctx, Some(inner.stack_id));
let can_redo = undo_redo_commands::can_redo(&inner.ctx, Some(inner.stack_id));
inner.queue_event(DocumentEvent::UndoRedoChanged { can_undo, can_redo });
inner.take_queued_events()
};
crate::inner::dispatch_queued_events(queued);
Ok(())
}
pub fn break_undo_merge(&self) {
let inner = self.inner.lock();
undo_redo_commands::seal_head(&inner.ctx, Some(inner.stack_id));
}
pub fn set_undo_limit(&self, limit: Option<usize>) {
let inner = self.inner.lock();
undo_redo_commands::set_undo_limit(&inner.ctx, limit);
}
pub fn undo_limit(&self) -> Option<usize> {
let inner = self.inner.lock();
undo_redo_commands::undo_limit(&inner.ctx)
}
pub fn can_undo(&self) -> bool {
let inner = self.inner.lock();
undo_redo_commands::can_undo(&inner.ctx, Some(inner.stack_id))
}
pub fn can_redo(&self) -> bool {
let inner = self.inner.lock();
undo_redo_commands::can_redo(&inner.ctx, Some(inner.stack_id))
}
pub fn clear_undo_redo(&self) {
let inner = self.inner.lock();
undo_redo_commands::clear_stack(&inner.ctx, inner.stack_id);
}
pub fn is_modified(&self) -> bool {
self.inner.lock().modified
}
pub fn set_modified(&self, modified: bool) {
let queued = {
let mut inner = self.inner.lock();
if inner.modified != modified {
inner.modified = modified;
inner.queue_event(DocumentEvent::ModificationChanged(modified));
}
inner.take_queued_events()
};
crate::inner::dispatch_queued_events(queued);
}
pub fn content_revision(&self) -> u64 {
self.inner.lock().content_revision
}
pub fn title(&self) -> String {
let inner = self.inner.lock();
document_commands::get_document(&inner.ctx, &inner.document_id)
.ok()
.flatten()
.map(|d| d.title)
.unwrap_or_default()
}
pub fn set_title(&self, title: &str) -> Result<()> {
let _settled = self.settle_transient_edits();
let inner = self.inner.lock();
let doc = document_commands::get_document(&inner.ctx, &inner.document_id)?
.ok_or_else(|| DocumentError::NotFound("document not found".into()))?;
let mut update: frontend::document::dtos::UpdateDocumentDto = doc.into();
update.title = title.into();
document_commands::update_document(&inner.ctx, Some(inner.stack_id), &update)?;
Ok(())
}
pub fn text_direction(&self) -> TextDirection {
let inner = self.inner.lock();
document_commands::get_document(&inner.ctx, &inner.document_id)
.ok()
.flatten()
.map(|d| d.text_direction)
.unwrap_or(TextDirection::LeftToRight)
}
pub fn set_text_direction(&self, direction: TextDirection) -> Result<()> {
let _settled = self.settle_transient_edits();
let inner = self.inner.lock();
let doc = document_commands::get_document(&inner.ctx, &inner.document_id)?
.ok_or_else(|| DocumentError::NotFound("document not found".into()))?;
let mut update: frontend::document::dtos::UpdateDocumentDto = doc.into();
update.text_direction = direction;
document_commands::update_document(&inner.ctx, Some(inner.stack_id), &update)?;
Ok(())
}
pub fn default_wrap_mode(&self) -> WrapMode {
let inner = self.inner.lock();
document_commands::get_document(&inner.ctx, &inner.document_id)
.ok()
.flatten()
.map(|d| d.default_wrap_mode)
.unwrap_or(WrapMode::WordWrap)
}
pub fn set_default_wrap_mode(&self, mode: WrapMode) -> Result<()> {
let _settled = self.settle_transient_edits();
let inner = self.inner.lock();
let doc = document_commands::get_document(&inner.ctx, &inner.document_id)?
.ok_or_else(|| DocumentError::NotFound("document not found".into()))?;
let mut update: frontend::document::dtos::UpdateDocumentDto = doc.into();
update.default_wrap_mode = mode;
document_commands::update_document(&inner.ctx, Some(inner.stack_id), &update)?;
Ok(())
}
pub fn default_language(&self) -> String {
let inner = self.inner.lock();
document_commands::get_document(&inner.ctx, &inner.document_id)
.ok()
.flatten()
.and_then(|d| d.default_language)
.unwrap_or_else(|| "en".to_string())
}
pub fn set_default_language(&self, language: &str) -> Result<()> {
let _settled = self.settle_transient_edits();
let inner = self.inner.lock();
let doc = document_commands::get_document(&inner.ctx, &inner.document_id)?
.ok_or_else(|| DocumentError::NotFound("document not found".into()))?;
let mut update: frontend::document::dtos::UpdateDocumentDto = doc.into();
update.default_language = Some(language.to_string());
document_commands::update_document(&inner.ctx, Some(inner.stack_id), &update)?;
Ok(())
}
pub fn on_change<F>(&self, callback: F) -> Subscription
where
F: Fn(DocumentEvent) + Send + Sync + 'static,
{
let mut inner = self.inner.lock();
events::subscribe_inner(&mut inner, callback)
}
pub fn poll_events(&self) -> Vec<DocumentEvent> {
let mut inner = self.inner.lock();
inner.drain_poll_events()
}
pub fn hold_events(&self) -> crate::EventHold {
self.inner.lock().event_holds += 1;
crate::EventHold {
doc: Arc::clone(&self.inner),
}
}
pub fn set_syntax_highlighter(&self, highlighter: Option<Arc<dyn crate::SyntaxHighlighter>>) {
let queued = {
let mut inner = self.inner.lock();
let prev_kind = inner.highlight_kind;
let installed = highlighter.is_some();
inner.highlights.set_shim(highlighter);
if installed {
inner.rehighlight_all(); } else {
inner.recompute_highlight_kind();
}
Self::queue_highlight_changed(&mut inner, 0, 0, prev_kind);
inner.take_queued_events()
};
crate::inner::dispatch_queued_events(queued);
}
pub fn add_syntax_session(
&self,
highlighter: Arc<dyn crate::SyntaxHighlighter>,
) -> crate::highlight::SessionId {
self.add_syntax_session_with_priority(highlighter, 0)
}
pub fn add_syntax_session_with_priority(
&self,
highlighter: Arc<dyn crate::SyntaxHighlighter>,
priority: i32,
) -> crate::highlight::SessionId {
let (id, queued) = {
let mut inner = self.inner.lock();
let prev_kind = inner.highlight_kind;
let id = inner.highlights.add_syntax(highlighter, priority);
inner.rehighlight_all();
Self::queue_highlight_changed(&mut inner, 0, 0, prev_kind);
(id, inner.take_queued_events())
};
crate::inner::dispatch_queued_events(queued);
id
}
pub fn add_range_session(&self) -> crate::highlight::SessionId {
self.add_range_session_with_priority(0)
}
pub fn add_opt_in_range_session(&self) -> crate::highlight::SessionId {
self.add_opt_in_range_session_with_priority(0)
}
pub fn opt_in_session_ids(&self) -> Vec<crate::highlight::SessionId> {
let inner = self.inner.lock();
inner
.highlights
.sessions
.iter()
.filter(|s| s.visibility == crate::highlight::SessionVisibility::OptIn)
.map(|s| s.id)
.collect()
}
pub fn add_opt_in_range_session_with_priority(
&self,
priority: i32,
) -> crate::highlight::SessionId {
let mut inner = self.inner.lock();
inner
.highlights
.add_range(priority, crate::highlight::SessionVisibility::OptIn)
}
pub fn add_range_session_with_priority(&self, priority: i32) -> crate::highlight::SessionId {
let mut inner = self.inner.lock();
inner
.highlights
.add_range(priority, crate::highlight::SessionVisibility::Shared)
}
pub fn set_session_ranges(
&self,
id: crate::highlight::SessionId,
ranges: Vec<crate::highlight::RangeHighlight>,
) -> bool {
let (ok, queued) = {
let mut inner = self.inner.lock();
let prev_kind = inner.highlight_kind;
let block_positions = crate::highlight::ordered_block_positions(&inner);
let changed = inner.highlights.set_ranges(id, ranges, &block_positions);
if let Some((position, length)) = changed {
inner.recompute_highlight_kind();
Self::queue_highlight_changed(&mut inner, position, length, prev_kind);
}
(changed.is_some(), inner.take_queued_events())
};
crate::inner::dispatch_queued_events(queued);
ok
}
pub fn remove_session(&self, id: crate::highlight::SessionId) -> bool {
let (existed, queued) = {
let mut inner = self.inner.lock();
let prev_kind = inner.highlight_kind;
let existed = inner.highlights.remove(id);
if existed {
inner.recompute_highlight_kind();
Self::queue_highlight_changed(&mut inner, 0, 0, prev_kind);
}
(existed, inner.take_queued_events())
};
crate::inner::dispatch_queued_events(queued);
existed
}
pub fn rehighlight(&self) {
let queued = {
let mut inner = self.inner.lock();
let prev_kind = inner.highlight_kind;
inner.rehighlight_all();
Self::queue_highlight_changed(&mut inner, 0, 0, prev_kind);
inner.take_queued_events()
};
crate::inner::dispatch_queued_events(queued);
}
pub fn rehighlight_block(&self, block_id: usize) {
let queued = {
let mut inner = self.inner.lock();
let prev_kind = inner.highlight_kind;
inner.rehighlight_from_block(block_id);
Self::queue_highlight_changed(&mut inner, 0, 0, prev_kind);
inner.take_queued_events()
};
crate::inner::dispatch_queued_events(queued);
}
fn queue_highlight_changed(
inner: &mut TextDocumentInner,
position: usize,
length: usize,
prev_kind: crate::highlight::HighlighterKind,
) {
use crate::highlight::HighlighterKind::{Metric, None as KNone, PaintOnly};
let new_kind = inner.highlight_kind;
let event = match (prev_kind, new_kind) {
(KNone, KNone) => return,
(PaintOnly, PaintOnly) | (KNone, PaintOnly) | (PaintOnly, KNone) => {
DocumentEvent::HighlightPaintChanged { position, length }
}
(KNone, Metric)
| (Metric, Metric)
| (Metric, PaintOnly)
| (Metric, KNone)
| (PaintOnly, Metric) => DocumentEvent::FormatChanged {
position,
length,
kind: crate::flow::FormatChangeKind::Character,
},
};
inner.queue_event(event);
}
}
impl Default for TextDocument {
fn default() -> Self {
Self::new()
}
}
pub(crate) struct UndoBlockState {
id: u64,
position: i64,
text_length: i64,
plain_text: String,
format: BlockFormat,
list: Option<u64>,
inline: (Vec<FormatRun>, Vec<ImageAnchor>, Vec<FootnoteRefAnchor>),
}
pub(crate) struct BlockStates {
blocks: Vec<UndoBlockState>,
text: String,
}
pub(crate) fn capture_block_state(inner: &TextDocumentInner) -> BlockStates {
let mut all_blocks =
frontend::commands::block_commands::get_all_block(&inner.ctx).unwrap_or_default();
let store = inner.ctx.db_context.get_store();
crate::inner::refresh_block_positions(&mut all_blocks, store);
let (runs, images, notes) = (
store.format_runs.read(),
store.block_images.read(),
store.block_footnote_refs.read(),
);
let mut states: Vec<UndoBlockState> = all_blocks
.into_iter()
.map(|b| {
let format = BlockFormat::from(&b);
let entity: common::entities::Block = b.clone().into();
let plain_text =
common::database::rope_helpers::block_content_via_store(&entity, store);
let text_length = common::database::rope_helpers::block_char_length(&entity, store);
UndoBlockState {
id: b.id,
position: b.document_position,
text_length,
plain_text,
format,
list: b.list,
inline: (
runs.get(&b.id).cloned().unwrap_or_default(),
images.get(&b.id).cloned().unwrap_or_default(),
notes.get(&b.id).cloned().unwrap_or_default(),
),
}
})
.collect();
drop((runs, images, notes));
states.sort_by_key(|s| s.position);
let text = addressable_text_of(store, &states);
BlockStates {
blocks: states,
text,
}
}
fn addressable_text_of(store: &common::database::Store, blocks: &[UndoBlockState]) -> String {
let text = common::database::rope_helpers::rope_full_text_if_flow_matches(store)
.unwrap_or_else(|| build_doc_text(blocks));
let end = blocks.last().map_or(0, |last| {
last.position.max(0) as usize + last.text_length.max(0) as usize
});
debug_assert_eq!(
end,
text.chars().count(),
"the blocks' positions are not positions in the addressable text"
);
text
}
fn build_doc_text(states: &[UndoBlockState]) -> String {
let mut text = String::new();
let mut length = 0usize;
for (i, state) in states.iter().enumerate() {
if i > 0 {
text.push('\n');
length += 1;
}
let position = state.position.max(0) as usize;
debug_assert!(
position >= length && (position - length).is_multiple_of(2),
"a gap of {} characters before the block at {position} is not tables' anchors",
position.saturating_sub(length)
);
while length + 2 <= position {
text.push_str(crate::TABLE_ANCHOR);
text.push('\n');
length += 2;
}
text.push_str(&state.plain_text);
length += state.plain_text.chars().count();
}
text
}
fn compute_text_edit(before: &str, after: &str) -> (usize, usize, usize) {
let before_chars: Vec<char> = before.chars().collect();
let after_chars: Vec<char> = after.chars().collect();
let prefix_len = before_chars
.iter()
.zip(after_chars.iter())
.take_while(|(a, b)| a == b)
.count();
let before_remaining = before_chars.len() - prefix_len;
let after_remaining = after_chars.len() - prefix_len;
let suffix_len = before_chars
.iter()
.rev()
.zip(after_chars.iter().rev())
.take(before_remaining.min(after_remaining))
.take_while(|(a, b)| a == b)
.count();
let removed = before_remaining - suffix_len;
let added = after_remaining - suffix_len;
(prefix_len, removed, added)
}
fn compute_text_edit_left_at(
before: &str,
after: &str,
minimal: (usize, usize, usize),
left_at: usize,
) -> (usize, usize, usize) {
let (mut offset, removed, added) = minimal;
if offset + removed < left_at {
return minimal;
}
let before_chars: Vec<char> = before.chars().collect();
let after_chars: Vec<char> = after.chars().collect();
loop {
if offset + removed == left_at {
return (offset, removed, added);
}
if offset == 0 || before_chars[offset - 1 + removed] != after_chars[offset - 1 + added] {
return minimal;
}
offset -= 1;
}
}
pub(crate) fn emit_content_change_events(inner: &mut TextDocumentInner, before: &BlockStates) {
emit_content_change_events_near(inner, before, None);
}
fn emit_content_change_events_near(
inner: &mut TextDocumentInner,
before: &BlockStates,
left_at: Option<usize>,
) -> Option<usize> {
let after = capture_block_state(inner);
let (before_text, after_text) = (&before.text, &after.text);
let (before, after) = (&before.blocks, &after.blocks);
let mut changed_text_end = None;
let before_map: std::collections::HashMap<u64, &UndoBlockState> =
before.iter().map(|s| (s.id, s)).collect();
let after_map: std::collections::HashMap<u64, &UndoBlockState> =
after.iter().map(|s| (s.id, s)).collect();
let mut content_changed = false;
let mut earliest_pos: Option<usize> = None;
let mut old_end: usize = 0;
let mut blocks_affected: usize = 0;
let mut block_format_changes: Vec<(usize, usize)> = Vec::new();
let mut list_changes: Vec<(usize, usize)> = Vec::new();
let mut inline_changes: Vec<(usize, usize)> = Vec::new();
for after_state in after {
if let Some(before_state) = before_map.get(&after_state.id) {
let text_changed = before_state.plain_text != after_state.plain_text
|| before_state.text_length != after_state.text_length;
let extent = (
after_state.position.max(0) as usize,
after_state.text_length.max(0) as usize,
);
if text_changed {
content_changed = true;
blocks_affected += 1;
let pos = after_state.position.max(0) as usize;
earliest_pos = Some(earliest_pos.map_or(pos, |p: usize| p.min(pos)));
old_end = old_end.max(
before_state.position.max(0) as usize
+ before_state.text_length.max(0) as usize,
);
} else {
if before_state.format != after_state.format {
block_format_changes.push(extent);
}
if before_state.list != after_state.list {
list_changes.push(extent);
}
if before_state.inline != after_state.inline {
inline_changes.push(extent);
}
}
} else {
content_changed = true;
blocks_affected += 1;
let pos = after_state.position.max(0) as usize;
earliest_pos = Some(earliest_pos.map_or(pos, |p: usize| p.min(pos)));
}
}
for before_state in before {
if !after_map.contains_key(&before_state.id) {
content_changed = true;
blocks_affected += 1;
let pos = before_state.position.max(0) as usize;
earliest_pos = Some(earliest_pos.map_or(pos, |p: usize| p.min(pos)));
old_end = old_end.max(pos + before_state.text_length.max(0) as usize);
}
}
if content_changed {
let minimal = compute_text_edit(before_text, after_text);
let (edit_offset, precise_removed, precise_added) = match left_at {
Some(left_at) => compute_text_edit_left_at(before_text, after_text, minimal, left_at),
None => minimal,
};
let before_len = before_text.chars().count();
let after_len = after_text.chars().count();
let region_start = earliest_pos.unwrap_or(0).min(before_len);
let region_end = old_end.clamp(region_start, before_len);
let precise = precise_removed > 0 || precise_added > 0;
let (position, chars_removed, chars_added) = if !precise
|| (region_start <= edit_offset && edit_offset + precise_removed <= region_end)
{
let removed = region_end - region_start;
(
region_start,
removed,
(removed + after_len).saturating_sub(before_len),
)
} else {
(edit_offset, precise_removed, precise_added)
};
if precise_removed > 0 || precise_added > 0 {
inner.adjust_cursors(edit_offset, precise_removed, precise_added);
changed_text_end = Some(edit_offset + precise_added);
}
inner.queue_event(DocumentEvent::ContentsChanged {
position,
chars_removed,
chars_added,
blocks_affected,
});
let before_len: i64 = before.iter().map(|b| b.text_length.max(0)).sum();
let after_len: i64 = after.iter().map(|a| a.text_length.max(0)).sum();
if after_len > before_len {
let text_start = after
.iter()
.map(|state| state.position.max(0) as usize)
.zip(after.iter().map(|state| state.text_length.max(0) as usize))
.find(|&(start, length)| start + length >= edit_offset)
.map_or(edit_offset, |(start, _)| start.max(edit_offset));
inner.queue_event(DocumentEvent::TextInserted {
position: text_start,
chars_inserted: (after_len - before_len) as usize,
origin: crate::InsertionOrigin::Replayed,
});
}
}
for (changes, kind) in [
(block_format_changes, FormatChangeKind::Block),
(list_changes, FormatChangeKind::List),
(inline_changes, FormatChangeKind::Character),
] {
for (position, length) in join_adjacent_blocks(changes) {
inner.queue_event(DocumentEvent::FormatChanged {
position,
length,
kind,
});
}
}
changed_text_end
}
fn join_adjacent_blocks(extents: Vec<(usize, usize)>) -> Vec<(usize, usize)> {
let mut joined: Vec<(usize, usize)> = Vec::new();
for (position, length) in extents {
match joined.last_mut() {
Some((start, len)) if *start + *len + 1 == position => *len += 1 + length,
_ => joined.push((position, length)),
}
}
joined
}
struct OutsideBlocks {
lists: Vec<frontend::list::dtos::ListDto>,
tables: Vec<frontend::table::dtos::TableDto>,
cells: Vec<frontend::table_cell::dtos::TableCellDto>,
frames: Vec<frontend::frame::dtos::FrameDto>,
}
fn capture_outside_blocks(inner: &TextDocumentInner) -> OutsideBlocks {
let mut lists = list_commands::get_all_list(&inner.ctx).unwrap_or_default();
let mut tables = table_commands::get_all_table(&inner.ctx).unwrap_or_default();
let mut cells = table_cell_commands::get_all_table_cell(&inner.ctx).unwrap_or_default();
let mut frames = frame_commands::get_all_frame(&inner.ctx).unwrap_or_default();
lists.sort_by_key(|list| list.id);
tables.sort_by_key(|table| table.id);
cells.sort_by_key(|cell| cell.id);
frames.sort_by_key(|frame| frame.id);
OutsideBlocks {
lists,
tables,
cells,
frames,
}
}
fn format_differs<T, F: PartialEq>(
before: &[T],
after: &[T],
id: impl Fn(&T) -> u64,
format: impl Fn(&T) -> F,
) -> bool {
after.iter().any(|entity| {
before
.binary_search_by_key(&id(entity), &id)
.is_ok_and(|index| format(&before[index]) != format(entity))
})
}
fn emit_changes_outside_blocks(
inner: &mut TextDocumentInner,
before: &OutsideBlocks,
text_changed: bool,
) {
let after = capture_outside_blocks(inner);
let list_format_changed = format_differs(
&before.lists,
&after.lists,
|list| list.id,
|list| {
(
list.style.clone(),
list.indent,
list.prefix.clone(),
list.suffix.clone(),
list.start,
)
},
);
let block_format_changed = format_differs(
&before.tables,
&after.tables,
|table| table.id,
|table| {
(
table.column_widths.clone(),
table.fmt_border,
table.fmt_cell_spacing,
table.fmt_cell_padding,
table.fmt_width,
table.fmt_alignment.clone(),
)
},
) || format_differs(
&before.cells,
&after.cells,
|cell| cell.id,
|cell| {
(
cell.fmt_padding,
cell.fmt_border,
cell.fmt_vertical_alignment.clone(),
cell.fmt_background_color.clone(),
)
},
) || format_differs(
&before.frames,
&after.frames,
|frame| frame.id,
|frame| {
(
(frame.fmt_height, frame.fmt_width, frame.fmt_padding),
(frame.fmt_top_margin, frame.fmt_bottom_margin),
(frame.fmt_left_margin, frame.fmt_right_margin),
frame.fmt_border,
frame.fmt_position.clone(),
frame.fmt_is_blockquote,
frame.fmt_semantic_role.clone(),
)
},
);
let shape_changed = !text_changed && !same_shape(before, &after);
if list_format_changed {
inner.queue_event(DocumentEvent::FormatChanged {
position: 0,
length: 0,
kind: FormatChangeKind::List,
});
}
if block_format_changed || shape_changed {
inner.queue_event(DocumentEvent::FormatChanged {
position: 0,
length: 0,
kind: FormatChangeKind::Block,
});
}
}
fn same_shape(before: &OutsideBlocks, after: &OutsideBlocks) -> bool {
fn all_same<T, S: PartialEq>(before: &[T], after: &[T], shape: impl Fn(&T) -> S) -> bool {
before.iter().map(&shape).eq(after.iter().map(&shape))
}
all_same(&before.frames, &after.frames, |frame| {
(
frame.id,
frame.parent_frame,
frame.blocks.clone(),
frame.child_order.clone(),
frame.table,
)
}) && all_same(&before.tables, &after.tables, |table| {
(table.id, table.cells.clone(), table.rows, table.columns)
}) && all_same(&before.cells, &after.cells, |cell| {
(
cell.id,
cell.row,
cell.column,
cell.row_span,
cell.column_span,
cell.cell_frame,
)
})
}
fn collect_frame_block_ids(
inner: &TextDocumentInner,
frame_id: frontend::common::types::EntityId,
) -> Option<Vec<u64>> {
let frame_dto = frame_commands::get_frame(&inner.ctx, &frame_id)
.ok()
.flatten()?;
if !frame_dto.child_order.is_empty() {
let mut block_ids = Vec::new();
for &entry in &frame_dto.child_order {
if entry > 0 {
block_ids.push(entry as u64);
} else if entry < 0 {
let sub_frame_id = (-entry) as u64;
let sub_frame = frame_commands::get_frame(&inner.ctx, &sub_frame_id)
.ok()
.flatten();
if let Some(ref sf) = sub_frame {
if let Some(table_id) = sf.table {
if let Some(table_dto) = table_commands::get_table(&inner.ctx, &table_id)
.ok()
.flatten()
{
let mut cell_dtos: Vec<_> = table_dto
.cells
.iter()
.filter_map(|&cid| {
table_cell_commands::get_table_cell(&inner.ctx, &cid)
.ok()
.flatten()
})
.collect();
cell_dtos
.sort_by(|a, b| a.row.cmp(&b.row).then(a.column.cmp(&b.column)));
for cell_dto in &cell_dtos {
if let Some(cf_id) = cell_dto.cell_frame
&& let Some(cf_ids) = collect_frame_block_ids(inner, cf_id)
{
block_ids.extend(cf_ids);
}
}
}
} else if let Some(sub_ids) = collect_frame_block_ids(inner, sub_frame_id) {
block_ids.extend(sub_ids);
}
}
}
}
Some(block_ids)
} else {
Some(frame_dto.blocks.to_vec())
}
}
pub(crate) fn flow_snapshot_of(
inner: &TextDocumentInner,
mask: &crate::highlight::HighlightMask,
suppress_paint: bool,
) -> crate::flow::FlowSnapshot {
let main_frame_id = get_main_frame_id(inner);
let hl = crate::highlight::SnapshotHighlights {
kind: inner.highlights.effective_kind(mask),
mask,
suppress_paint,
};
let elements = crate::text_frame::build_flow_snapshot(inner, main_frame_id, hl);
crate::flow::FlowSnapshot { elements }
}
pub(crate) fn block_snapshot_at(
inner: &TextDocumentInner,
position: usize,
mask: &crate::highlight::HighlightMask,
) -> Option<crate::flow::BlockSnapshot> {
let hl = crate::highlight::SnapshotHighlights {
kind: inner.highlights.effective_kind(mask),
mask,
suppress_paint: false,
};
let main_frame_id = get_main_frame_id(inner);
let store = inner.ctx.db_context.get_store();
if common::database::rope_helpers::rope_positions_match_flow(store)
&& let Some((block_id, _, _)) =
common::database::rope_helpers::find_block_at_char_position(store, position as i64)
{
return crate::text_block::build_block_snapshot(inner, block_id, hl);
}
let ordered_block_ids = collect_frame_block_ids(inner, main_frame_id)?;
let pos = position as i64;
let mut running_pos: i64 = 0;
for &block_id in &ordered_block_ids {
let block_dto = block_commands::get_block(&inner.ctx, &block_id)
.ok()
.flatten()?;
let entity: common::entities::Block = block_dto.clone().into();
let block_end =
running_pos + common::database::rope_helpers::block_char_length(&entity, store);
if pos >= running_pos && pos <= block_end {
return crate::text_block::build_block_snapshot_with_position(
inner,
block_id,
Some(running_pos as usize),
hl,
&crate::text_block::SnapshotLookups::default(),
);
}
running_pos = block_end + 1;
}
if let Some(&last_id) = ordered_block_ids.last() {
return crate::text_block::build_block_snapshot(inner, last_id, hl);
}
None
}
pub(crate) fn get_main_frame_id(inner: &TextDocumentInner) -> frontend::common::types::EntityId {
let frames = frontend::commands::document_commands::get_document_relationship(
&inner.ctx,
&inner.document_id,
&frontend::document::dtos::DocumentRelationshipField::Frames,
)
.unwrap_or_default();
frames.first().copied().unwrap_or(0)
}
fn parse_progress_data(data: &Option<String>) -> (String, f64, String) {
let Some(json) = data else {
return (String::new(), 0.0, String::new());
};
let v: serde_json::Value = serde_json::from_str(json).unwrap_or_default();
let id = v["id"].as_str().unwrap_or_default().to_string();
let pct = v["percentage"].as_f64().unwrap_or(0.0);
let msg = v["message"].as_str().unwrap_or_default().to_string();
(id, pct, msg)
}
fn parse_id_data(data: &Option<String>) -> String {
let Some(json) = data else {
return String::new();
};
let v: serde_json::Value = serde_json::from_str(json).unwrap_or_default();
v["id"].as_str().unwrap_or_default().to_string()
}
fn parse_failed_data(data: &Option<String>) -> (String, String) {
let Some(json) = data else {
return (String::new(), "unknown error".into());
};
let v: serde_json::Value = serde_json::from_str(json).unwrap_or_default();
let id = v["id"].as_str().unwrap_or_default().to_string();
let error = v["error"].as_str().unwrap_or("unknown error").to_string();
(id, error)
}