2021-08-16 15:02:57 +08:00
|
|
|
use super::extensions::*;
|
2021-08-11 11:42:46 +08:00
|
|
|
use crate::{
|
2021-08-15 22:08:03 +08:00
|
|
|
core::{Attribute, Delta, Interval},
|
2021-08-11 23:34:35 +08:00
|
|
|
errors::{ErrorBuilder, OTError, OTErrorCode},
|
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-08-14 16:44:39 +08:00
|
|
|
pub(crate) fn insert(
|
|
|
|
|
&self,
|
|
|
|
|
delta: &Delta,
|
|
|
|
|
text: &str,
|
2021-08-15 00:05:18 +08:00
|
|
|
interval: Interval,
|
2021-08-14 16:44:39 +08:00
|
|
|
) -> Result<Delta, OTError> {
|
2021-08-11 23:34:35 +08:00
|
|
|
let mut new_delta = None;
|
|
|
|
|
for ext in &self.insert_exts {
|
2021-08-15 00:05:18 +08:00
|
|
|
if let Some(delta) = ext.apply(delta, interval.size(), text, interval.start) {
|
2021-08-15 21:11:48 +08:00
|
|
|
log::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-08-14 16:44:39 +08:00
|
|
|
pub(crate) fn delete(&self, delta: &Delta, interval: Interval) -> Result<Delta, OTError> {
|
|
|
|
|
let mut new_delta = None;
|
|
|
|
|
for ext in &self.delete_exts {
|
|
|
|
|
if let Some(delta) = ext.apply(delta, interval) {
|
2021-08-15 21:11:48 +08:00
|
|
|
log::debug!("[{}]: 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn format(
|
|
|
|
|
&self,
|
|
|
|
|
delta: &Delta,
|
|
|
|
|
attribute: Attribute,
|
|
|
|
|
interval: Interval,
|
|
|
|
|
) -> Result<Delta, OTError> {
|
|
|
|
|
let mut new_delta = None;
|
|
|
|
|
for ext in &self.format_exts {
|
|
|
|
|
if let Some(delta) = ext.apply(delta, interval, &attribute) {
|
2021-08-15 21:11:48 +08:00
|
|
|
log::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::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 {}),
|
|
|
|
|
Box::new(AutoExitBlockExt {}),
|
|
|
|
|
Box::new(PreserveBlockStyleOnInsertExt {}),
|
|
|
|
|
Box::new(PreserveLineStyleOnSplitExt {}),
|
|
|
|
|
Box::new(ResetLineFormatOnNewLineExt {}),
|
2021-08-16 15:02:57 +08:00
|
|
|
Box::new(AutoFormatExt {}),
|
2021-08-13 14:13:31 +08:00
|
|
|
Box::new(PreserveInlineStylesExt {}),
|
|
|
|
|
Box::new(DefaultInsertExt {}),
|
2021-08-11 23:34:35 +08:00
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn construct_format_exts() -> Vec<FormatExtension> {
|
|
|
|
|
vec![
|
|
|
|
|
Box::new(FormatLinkAtCaretPositionExt {}),
|
2021-08-15 21:11:48 +08:00
|
|
|
Box::new(ResolveBlockFormatExt {}),
|
2021-08-11 23:34:35 +08:00
|
|
|
Box::new(ResolveInlineFormatExt {}),
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn construct_delete_exts() -> Vec<DeleteExtension> {
|
|
|
|
|
vec![
|
|
|
|
|
//
|
2021-08-14 16:44:39 +08:00
|
|
|
Box::new(DefaultDeleteExt {}),
|
2021-08-11 11:42:46 +08:00
|
|
|
]
|
|
|
|
|
}
|