2021-08-11 23:34:35 +08:00
|
|
|
use crate::{
|
2021-08-13 14:13:31 +08:00
|
|
|
client::view::{FormatExt, NEW_LINE},
|
|
|
|
|
core::{
|
|
|
|
|
Attribute,
|
2021-08-14 16:44:39 +08:00
|
|
|
AttributeKey,
|
2021-08-13 14:13:31 +08:00
|
|
|
AttributeScope,
|
|
|
|
|
Attributes,
|
|
|
|
|
CharMetric,
|
|
|
|
|
Delta,
|
|
|
|
|
DeltaBuilder,
|
|
|
|
|
DeltaIter,
|
|
|
|
|
Interval,
|
|
|
|
|
Operation,
|
|
|
|
|
},
|
2021-08-11 23:34:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub struct FormatLinkAtCaretPositionExt {}
|
|
|
|
|
|
|
|
|
|
impl FormatExt for FormatLinkAtCaretPositionExt {
|
|
|
|
|
fn apply(&self, delta: &Delta, interval: Interval, attribute: &Attribute) -> Option<Delta> {
|
2021-08-14 16:44:39 +08:00
|
|
|
if attribute.key != AttributeKey::Link || interval.size() != 0 {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 13:57:41 +08:00
|
|
|
let mut iter = DeltaIter::new(delta);
|
2021-08-13 14:13:31 +08:00
|
|
|
iter.seek::<CharMetric>(interval.start);
|
2021-08-14 16:44:39 +08:00
|
|
|
|
|
|
|
|
let (before, after) = (iter.next_op_with_len(interval.size()), iter.next());
|
|
|
|
|
let mut start = interval.end;
|
2021-08-12 13:57:41 +08:00
|
|
|
let mut retain = 0;
|
|
|
|
|
|
|
|
|
|
if let Some(before) = before {
|
|
|
|
|
if before.contain_attribute(attribute) {
|
|
|
|
|
start -= before.length();
|
|
|
|
|
retain += before.length();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(after) = after {
|
|
|
|
|
if after.contain_attribute(attribute) {
|
|
|
|
|
if retain != 0 {
|
|
|
|
|
retain += after.length();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if retain == 0 {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Some(
|
|
|
|
|
DeltaBuilder::new()
|
2021-08-15 00:05:18 +08:00
|
|
|
.retain(start)
|
|
|
|
|
.retain_with_attributes(retain, (attribute.clone()).into())
|
2021-08-12 13:57:41 +08:00
|
|
|
.build(),
|
|
|
|
|
)
|
2021-08-11 23:34:35 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct ResolveLineFormatExt {}
|
|
|
|
|
impl FormatExt for ResolveLineFormatExt {
|
|
|
|
|
fn apply(&self, delta: &Delta, interval: Interval, attribute: &Attribute) -> Option<Delta> {
|
2021-08-12 13:57:41 +08:00
|
|
|
if attribute.scope != AttributeScope::Block {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut new_delta = Delta::new();
|
|
|
|
|
new_delta.retain(interval.start, Attributes::default());
|
|
|
|
|
|
|
|
|
|
let mut iter = DeltaIter::new(delta);
|
2021-08-13 14:13:31 +08:00
|
|
|
iter.seek::<CharMetric>(interval.start);
|
2021-08-12 13:57:41 +08:00
|
|
|
|
|
|
|
|
None
|
2021-08-11 23:34:35 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct ResolveInlineFormatExt {}
|
|
|
|
|
|
|
|
|
|
impl FormatExt for ResolveInlineFormatExt {
|
|
|
|
|
fn apply(&self, delta: &Delta, interval: Interval, attribute: &Attribute) -> Option<Delta> {
|
2021-08-13 14:13:31 +08:00
|
|
|
if attribute.scope != AttributeScope::Inline {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
2021-08-15 00:05:18 +08:00
|
|
|
let mut new_delta = DeltaBuilder::new().retain(interval.start).build();
|
2021-08-13 14:13:31 +08:00
|
|
|
let mut iter = DeltaIter::new(delta);
|
|
|
|
|
iter.seek::<CharMetric>(interval.start);
|
|
|
|
|
|
|
|
|
|
let mut cur = 0;
|
|
|
|
|
let len = interval.size();
|
|
|
|
|
|
|
|
|
|
while cur < len && iter.has_next() {
|
2021-08-13 16:39:32 +08:00
|
|
|
let some_op = iter.next_op_with_len(len - cur);
|
2021-08-13 14:13:31 +08:00
|
|
|
if some_op.is_none() {
|
|
|
|
|
return Some(new_delta);
|
|
|
|
|
}
|
|
|
|
|
let op = some_op.unwrap();
|
|
|
|
|
if let Operation::Insert(insert) = &op {
|
|
|
|
|
let mut s = insert.s.as_str();
|
|
|
|
|
match s.find(NEW_LINE) {
|
|
|
|
|
None => {
|
|
|
|
|
new_delta.retain(op.length(), attribute.clone().into());
|
|
|
|
|
},
|
2021-08-14 16:44:39 +08:00
|
|
|
Some(line_break) => {
|
2021-08-13 14:13:31 +08:00
|
|
|
let mut pos = 0;
|
|
|
|
|
let mut some_line_break = Some(line_break);
|
|
|
|
|
while some_line_break.is_some() {
|
|
|
|
|
let line_break = some_line_break.unwrap();
|
|
|
|
|
new_delta.retain(line_break - pos, attribute.clone().into());
|
|
|
|
|
new_delta.retain(1, Attributes::default());
|
|
|
|
|
pos = line_break + 1;
|
|
|
|
|
|
|
|
|
|
s = &s[pos..s.len()];
|
|
|
|
|
some_line_break = s.find(NEW_LINE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if pos < op.length() {
|
|
|
|
|
new_delta.retain(op.length() - pos, attribute.clone().into());
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cur += op.length();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Some(new_delta)
|
2021-08-11 23:34:35 +08:00
|
|
|
}
|
|
|
|
|
}
|