Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

generic document data

appflowy48c3436ada84
diff --git a/app_flowy/packages/flowy_editor/lib/src/model/heuristic/rule.dart b/app_flowy/packages/flowy_editor/lib/src/model/heuristic/rule.dart
index 8eb52e6441..dc329a183e 100644
--- a/app_flowy/packages/flowy_editor/lib/src/model/heuristic/rule.dart
+++ b/app_flowy/packages/flowy_editor/lib/src/model/heuristic/rule.dart
@@ -43,7 +43,7 @@ class Rules {
const FormatLinkAtCaretPositionRule(),
const ResolveLineFormatRule(),
const ResolveInlineFormatRule(),
- // const InsertEmbedsRule(),
+ const InsertEmbedsRule(),
// const ForceNewlineForInsertsAroundEmbedRule(),
const AutoExitBlockRule(),
const PreserveBlockStyleOnInsertRule(),
diff --git a/rust-lib/flowy-ot/src/client/document/data.rs b/rust-lib/flowy-ot/src/client/document/data.rs
new file mode 100644
index 0000000000..046c531cd1
--- /dev/null
+++ b/rust-lib/flowy-ot/src/client/document/data.rs
@@ -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)
+ }
+}
diff --git a/rust-lib/flowy-ot/src/client/document.rs b/rust-lib/flowy-ot/src/client/document/document.rs
similarity index 89%
rename from rust-lib/flowy-ot/src/client/document.rs
rename to rust-lib/flowy-ot/src/client/document/document.rs
index b98d0919b6..538663ae92 100644
--- a/rust-lib/flowy-ot/src/client/document.rs
+++ b/rust-lib/flowy-ot/src/client/document/document.rs
@@ -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)?;
}
diff --git a/rust-lib/flowy-ot/src/client/document/mod.rs b/rust-lib/flowy-ot/src/client/document/mod.rs
new file mode 100644
index 0000000000..3eef9bee24
--- /dev/null
+++ b/rust-lib/flowy-ot/src/client/document/mod.rs
@@ -0,0 +1,5 @@
+mod data;
+mod document;
+
+pub use data::*;
+pub use document::*;
diff --git a/rust-lib/flowy-ot/src/client/view.rs b/rust-lib/flowy-ot/src/client/view.rs
index 51b005137e..0b927148ab 100644
--- a/rust-lib/flowy-ot/src/client/view.rs
+++ b/rust-lib/flowy-ot/src/client/view.rs
@@ -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>,
diff --git a/rust-lib/flowy-ot/src/errors.rs b/rust-lib/flowy-ot/src/errors.rs
index e219655025..b0c07ac424 100644
--- a/rust-lib/flowy-ot/src/errors.rs
+++ b/rust-lib/flowy-ot/src/errors.rs
@@ -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 {