Lucas.Xu 2202326278
feat: integrate new editor (#2536)
* feat: update editor

* feat: integrate new editor

* feat: integrate the document2 into folder2

* feat: integrate the new editor

* chore: cargo fix

* chore: active document feature for flowy-error

* feat: convert the editor action to collab action

* feat: migrate the grid and board

* feat: migrate the callout block

* feat: migrate the divider

* chore: migrate the callout and math equation

* feat: migrate the code block

* feat: add shift + enter command in code block

* feat: support tab and shift+tab in code block

* fix: cursor error after inserting divider

* feat: migrate the grid and board

* feat: migrate the emoji picker

* feat: migrate openai

* feat: integrate floating toolbar

* feat: migrate the smart editor

* feat: migrate the cover

* feat: add option block action

* chore: implement block selection and delete option

* feat: support background color

* feat: dismiss color picker afer setting color

* feat: migrate the cover block

* feat: resize the font

* chore: cutomsize the padding

* chore: wrap the option button with ignore widget

* feat: customize the heading style

* chore: optimize the divider line height

* fix: the option button can't dismiss

* ci: rust test

* chore: flutter analyze

* fix: code block selection

* fix: dismiss the emoji picker after selecting one

* chore: optimize the transaction adapter

* fix: can't save the new content

* feat: show export page when some errors happen

* feat: implement get_view_data for document

---------

Co-authored-by: nathan <nathan@appflowy.io>
2023-05-16 14:58:24 +08:00

98 lines
3.0 KiB
Rust

use std::{collections::HashMap, sync::Arc};
use appflowy_integrate::collab_builder::AppFlowyCollabBuilder;
use appflowy_integrate::RocksCollabDB;
use parking_lot::RwLock;
use flowy_error::{FlowyError, FlowyResult};
use crate::document_data::DocumentDataWrapper;
use crate::{
document::Document,
entities::DocEventPB,
notification::{send_notification, DocumentNotification},
};
pub trait DocumentUser: Send + Sync {
fn user_id(&self) -> Result<i64, FlowyError>;
fn token(&self) -> Result<String, FlowyError>; // unused now.
fn collab_db(&self) -> Result<Arc<RocksCollabDB>, FlowyError>;
}
pub struct DocumentManager {
user: Arc<dyn DocumentUser>,
collab_builder: Arc<AppFlowyCollabBuilder>,
documents: Arc<RwLock<HashMap<String, Arc<Document>>>>,
}
impl DocumentManager {
pub fn new(user: Arc<dyn DocumentUser>, collab_builder: Arc<AppFlowyCollabBuilder>) -> Self {
Self {
user,
collab_builder,
documents: Default::default(),
}
}
pub fn create_document(
&self,
doc_id: String,
data: DocumentDataWrapper,
) -> FlowyResult<Arc<Document>> {
tracing::debug!("create a document: {:?}", &doc_id);
let uid = self.user.user_id()?;
let db = self.user.collab_db()?;
let collab = self.collab_builder.build(uid, &doc_id, db);
let document = Arc::new(Document::create_with_data(collab, data.0)?);
Ok(document)
}
pub fn open_document(&self, doc_id: String) -> FlowyResult<Arc<Document>> {
tracing::debug!("open a document: {:?}", &doc_id);
if let Some(doc) = self.documents.read().get(&doc_id) {
return Ok(doc.clone());
}
tracing::debug!("open_document: {:?}", &doc_id);
let uid = self.user.user_id()?;
let db = self.user.collab_db()?;
let collab = self.collab_builder.build(uid, &doc_id, db);
// read the existing document from the disk.
let document = Arc::new(Document::new(collab)?);
// save the document to the memory and read it from the memory if we open the same document again.
// and we don't want to subscribe to the document changes if we open the same document again.
self
.documents
.write()
.insert(doc_id.clone(), document.clone());
// subscribe to the document changes.
document.lock().open(move |events, is_remote| {
tracing::debug!(
"document changed: {:?}, from remote: {}",
&events,
is_remote
);
// send notification to the client.
send_notification(&doc_id, DocumentNotification::DidReceiveUpdate)
.payload::<DocEventPB>((events, is_remote).into())
.send();
})?;
Ok(document)
}
pub fn get_document(&self, doc_id: String) -> FlowyResult<Arc<Document>> {
let uid = self.user.user_id()?;
let db = self.user.collab_db()?;
let collab = self.collab_builder.build(uid, &doc_id, db);
// read the existing document from the disk.
let document = Arc::new(Document::new(collab)?);
Ok(document)
}
pub fn close_document(&self, doc_id: String) -> FlowyResult<()> {
self.documents.write().remove(&doc_id);
Ok(())
}
}