2021-12-25 23:02:20 +08:00
|
|
|
use crate::document::*;
|
2021-11-19 14:38:11 +08:00
|
|
|
use lib_ot::{
|
2021-12-07 19:59:08 +08:00
|
|
|
core::{trim, Interval},
|
2021-08-11 23:34:35 +08:00
|
|
|
errors::{ErrorBuilder, OTError, OTErrorCode},
|
2021-12-07 19:59:08 +08:00
|
|
|
rich_text::{RichTextAttribute, RichTextDelta},
|
2021-08-11 11:42:46 +08:00
|
|
|
};
|
|
|
|
|
|
2021-08-18 16:04:22 +08:00
|
|
|
pub const RECORD_THRESHOLD: usize = 400; // in milliseconds
|
|
|
|
|
|
2021-08-11 11:42:46 +08:00
|
|
|
pub struct View {
|
|
|
|
|
insert_exts: Vec<InsertExtension>,
|
2021-08-11 23:34:35 +08:00
|
|
|
format_exts: Vec<FormatExtension>,
|
|
|
|
|
delete_exts: Vec<DeleteExtension>,
|
2021-08-11 11:42:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl View {
|
|
|
|
|
pub(crate) fn new() -> Self {
|
2021-08-11 23:34:35 +08:00
|
|
|
Self {
|
2021-08-15 00:05:18 +08:00
|
|
|
insert_exts: construct_insert_exts(),
|
|
|
|
|
format_exts: construct_format_exts(),
|
|
|
|
|
delete_exts: construct_delete_exts(),
|
2021-08-11 23:34:35 +08:00
|
|
|
}
|
2021-08-11 11:42:46 +08:00
|
|
|
}
|
|
|
|
|
|
2021-12-07 10:39:01 +08:00
|
|
|
pub(crate) fn insert(
|
|
|
|
|
&self,
|
|
|
|
|
delta: &RichTextDelta,
|
|
|
|
|
text: &str,
|
|
|
|
|
interval: Interval,
|
|
|
|
|
) -> Result<RichTextDelta, OTError> {
|
2021-08-11 23:34:35 +08:00
|
|
|
let mut new_delta = None;
|
|
|
|
|
for ext in &self.insert_exts {
|
2021-11-12 21:44:26 +08:00
|
|
|
if let Some(mut delta) = ext.apply(delta, interval.size(), text, interval.start) {
|
|
|
|
|
trim(&mut delta);
|
2021-12-09 22:28:11 +08:00
|
|
|
tracing::debug!("[{}]: applied, delta: {}", ext.ext_name(), delta);
|
2021-08-11 23:34:35 +08:00
|
|
|
new_delta = Some(delta);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match new_delta {
|
|
|
|
|
None => Err(ErrorBuilder::new(OTErrorCode::ApplyInsertFail).build()),
|
|
|
|
|
Some(new_delta) => Ok(new_delta),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-07 10:39:01 +08:00
|
|
|
pub(crate) fn delete(&self, delta: &RichTextDelta, interval: Interval) -> Result<RichTextDelta, OTError> {
|
2021-08-14 16:44:39 +08:00
|
|
|
let mut new_delta = None;
|
|
|
|
|
for ext in &self.delete_exts {
|
2021-11-12 21:44:26 +08:00
|
|
|
if let Some(mut delta) = ext.apply(delta, interval) {
|
|
|
|
|
trim(&mut delta);
|
2021-11-03 15:37:38 +08:00
|
|
|
tracing::trace!("[{}]: applied, delta: {}", ext.ext_name(), delta);
|
2021-08-14 16:44:39 +08:00
|
|
|
new_delta = Some(delta);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match new_delta {
|
2021-08-15 00:05:18 +08:00
|
|
|
None => Err(ErrorBuilder::new(OTErrorCode::ApplyDeleteFail).build()),
|
2021-08-14 16:44:39 +08:00
|
|
|
Some(new_delta) => Ok(new_delta),
|
|
|
|
|
}
|
2021-08-11 23:34:35 +08:00
|
|
|
}
|
|
|
|
|
|
2021-12-07 10:39:01 +08:00
|
|
|
pub(crate) fn format(
|
|
|
|
|
&self,
|
|
|
|
|
delta: &RichTextDelta,
|
|
|
|
|
attribute: RichTextAttribute,
|
|
|
|
|
interval: Interval,
|
|
|
|
|
) -> Result<RichTextDelta, OTError> {
|
2021-08-11 23:34:35 +08:00
|
|
|
let mut new_delta = None;
|
|
|
|
|
for ext in &self.format_exts {
|
2021-11-12 21:44:26 +08:00
|
|
|
if let Some(mut delta) = ext.apply(delta, interval, &attribute) {
|
|
|
|
|
trim(&mut delta);
|
2021-11-03 15:37:38 +08:00
|
|
|
tracing::trace!("[{}]: applied, delta: {}", ext.ext_name(), delta);
|
2021-08-11 23:34:35 +08:00
|
|
|
new_delta = Some(delta);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match new_delta {
|
|
|
|
|
None => Err(ErrorBuilder::new(OTErrorCode::ApplyFormatFail).build()),
|
|
|
|
|
Some(new_delta) => Ok(new_delta),
|
|
|
|
|
}
|
2021-08-11 11:42:46 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn construct_insert_exts() -> Vec<InsertExtension> {
|
|
|
|
|
vec![
|
2021-08-13 14:13:31 +08:00
|
|
|
Box::new(InsertEmbedsExt {}),
|
|
|
|
|
Box::new(ForceNewlineForInsertsAroundEmbedExt {}),
|
2021-08-17 22:04:39 +08:00
|
|
|
Box::new(AutoExitBlock {}),
|
|
|
|
|
Box::new(PreserveBlockFormatOnInsert {}),
|
|
|
|
|
Box::new(PreserveLineFormatOnSplit {}),
|
|
|
|
|
Box::new(ResetLineFormatOnNewLine {}),
|
2021-08-16 15:02:57 +08:00
|
|
|
Box::new(AutoFormatExt {}),
|
2021-08-17 22:04:39 +08:00
|
|
|
Box::new(PreserveInlineFormat {}),
|
|
|
|
|
Box::new(DefaultInsertAttribute {}),
|
2021-08-11 23:34:35 +08:00
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn construct_format_exts() -> Vec<FormatExtension> {
|
|
|
|
|
vec![
|
2021-08-18 13:12:45 +08:00
|
|
|
// Box::new(FormatLinkAtCaretPositionExt {}),
|
2021-08-17 22:04:39 +08:00
|
|
|
Box::new(ResolveBlockFormat {}),
|
|
|
|
|
Box::new(ResolveInlineFormat {}),
|
2021-08-11 23:34:35 +08:00
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-01 19:39:08 +08:00
|
|
|
fn construct_delete_exts() -> Vec<DeleteExtension> {
|
|
|
|
|
vec![Box::new(PreserveLineFormatOnMerge {}), Box::new(DefaultDelete {})]
|
|
|
|
|
}
|