generic document data

This commit is contained in:
appflowy 2021-08-18 16:04:22 +08:00
parent 27681fda07
commit 48c3436ada
6 changed files with 51 additions and 7 deletions

View File

@ -43,7 +43,7 @@ class Rules {
const FormatLinkAtCaretPositionRule(),
const ResolveLineFormatRule(),
const ResolveInlineFormatRule(),
// const InsertEmbedsRule(),
const InsertEmbedsRule(),
// const ForceNewlineForInsertsAroundEmbedRule(),
const AutoExitBlockRule(),
const PreserveBlockStyleOnInsertRule(),

View File

@ -0,0 +1,19 @@
use crate::{client::DocumentData, errors::OTError};
use serde::{Deserialize, Serialize};
use serde_json::Error;
impl<T: AsRef<str>> DocumentData for T {
fn into_string(self) -> Result<String, OTError> { Ok(self.as_ref().to_string()) }
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ImageData {
image: String,
}
impl DocumentData for ImageData {
fn into_string(self) -> Result<String, OTError> {
let s = serde_json::to_string(&self)?;
Ok(s)
}
}

View File

@ -1,10 +1,12 @@
use crate::{
client::{view::View, History, RevId, UndoResult},
client::{view::View, History, RevId, UndoResult, RECORD_THRESHOLD},
core::*,
errors::{ErrorBuilder, OTError, OTErrorCode, OTErrorCode::*},
};
pub const RECORD_THRESHOLD: usize = 400; // in milliseconds
pub trait DocumentData {
fn into_string(self) -> Result<String, OTError>;
}
pub struct Document {
delta: Delta,
@ -30,10 +32,12 @@ impl Document {
}
}
pub fn insert(&mut self, index: usize, text: &str) -> Result<Delta, OTError> {
pub fn insert<T: DocumentData>(&mut self, index: usize, data: T) -> Result<Delta, OTError> {
let interval = Interval::new(index, index);
let _ = validate_interval(&self.delta, &interval)?;
let delta = self.view.insert(&self.delta, text, interval)?;
let text = data.into_string()?;
let delta = self.view.insert(&self.delta, &text, interval)?;
log::debug!("👉 receive change: {}", delta);
self.add_delta(&delta)?;
Ok(delta)
@ -63,11 +67,16 @@ impl Document {
Ok(())
}
pub fn replace(&mut self, interval: Interval, text: &str) -> Result<Delta, OTError> {
pub fn replace<T: DocumentData>(
&mut self,
interval: Interval,
data: T,
) -> Result<Delta, OTError> {
let _ = validate_interval(&self.delta, &interval)?;
let mut delta = Delta::default();
let text = data.into_string()?;
if !text.is_empty() {
delta = self.view.insert(&self.delta, text, interval)?;
delta = self.view.insert(&self.delta, &text, interval)?;
log::debug!("👉 receive change: {}", delta);
self.add_delta(&delta)?;
}

View File

@ -0,0 +1,5 @@
mod data;
mod document;
pub use data::*;
pub use document::*;

View File

@ -4,6 +4,8 @@ use crate::{
errors::{ErrorBuilder, OTError, OTErrorCode},
};
pub const RECORD_THRESHOLD: usize = 400; // in milliseconds
pub struct View {
insert_exts: Vec<InsertExtension>,
format_exts: Vec<FormatExtension>,

View File

@ -23,6 +23,14 @@ impl Error for OTError {
fn source(&self) -> Option<&(dyn Error + 'static)> { None }
}
impl std::convert::From<serde_json::Error> for OTError {
fn from(error: serde_json::Error) -> Self {
ErrorBuilder::new(OTErrorCode::SerdeError)
.error(error)
.build()
}
}
#[derive(Debug, Clone)]
pub enum OTErrorCode {
IncompatibleLength,
@ -33,6 +41,7 @@ pub enum OTErrorCode {
IntervalOutOfBound,
UndoFail,
RedoFail,
SerdeError,
}
pub struct ErrorBuilder {