use canvist_model::Document;
use canvist_model::Point;
use canvist_model::Selection;
use serde_json::Value as Json;
use serde_json::json;
use yrs::Assoc;
use yrs::BranchID;
use yrs::IndexedSequence;
use yrs::StickyIndex;
use yrs::Transact;
use yrs::sync::Awareness;
use yrs::sync::AwarenessUpdate;
use yrs::sync::time::Clock;
use yrs::updates::decoder::Decode;
use yrs::updates::encoder::Encode;
use crate::Binding;
use crate::CollabError;
use crate::local::map_ref;
use crate::registry::Owner;
use crate::text::Chunks;
use crate::text::Cursor;
use crate::tree;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct User {
pub name: String,
pub color: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Collaborator {
pub client_id: u64,
pub user: Option<User>,
pub selection: Option<Selection>,
}
pub struct Presence {
awareness: Awareness,
changed: bool,
}
impl std::fmt::Debug for Presence {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Presence")
.field("client_id", &self.awareness.client_id())
.field("changed", &self.changed)
.finish_non_exhaustive()
}
}
impl Presence {
#[cfg(not(all(target_family = "wasm", target_os = "unknown")))]
pub fn new(binding: &Binding) -> Self {
Self::from_awareness(Awareness::new(binding.yrs().clone()))
}
pub fn with_clock(binding: &Binding, clock: impl Clock + 'static) -> Self {
Self::from_awareness(Awareness::with_clock(binding.yrs().clone(), clock))
}
fn from_awareness(awareness: Awareness) -> Self {
Self {
awareness,
changed: false,
}
}
pub fn client_id(&self) -> u64 {
self.awareness.client_id().get()
}
pub fn set_local(
&mut self,
(binding, doc): (&Binding, &Document),
user: &User,
selection: Option<Selection>,
) {
let cursor = selection.and_then(|selection| cursor_json(binding, doc, selection));
let state = json!({
"user": { "name": user.name, "color": user.color },
"cursor": cursor,
});
self.awareness.set_local_state_raw(state.to_string());
self.changed = true;
}
pub fn clear_local(&mut self) {
self.awareness.clean_local_state();
self.changed = true;
}
pub fn collaborators(&self, binding: &Binding, doc: &Document) -> Vec<Collaborator> {
let own = self.awareness.client_id();
let mut collaborators: Vec<Collaborator> = self
.awareness
.iter()
.filter(|(client, _)| *client != own)
.filter_map(|(client, state)| {
let state: Json = serde_json::from_str(state.data.as_deref()?).ok()?;
Some(Collaborator {
client_id: client.get(),
user: user_of(&state),
selection: selection_of(binding, doc, &state),
})
})
.collect();
collaborators.sort_by_key(|collaborator| collaborator.client_id);
collaborators
}
pub fn encode(&self) -> Result<Vec<u8>, CollabError> {
Ok(self.awareness.update()?.encode_v1())
}
pub fn take_local_change(&mut self) -> Result<Option<AwarenessUpdate>, CollabError> {
if !std::mem::take(&mut self.changed) {
return Ok(None);
}
let own = self.awareness.client_id();
Ok(Some(self.awareness.update_with_clients([own])?))
}
pub fn apply(&mut self, update: &[u8]) -> Result<(), CollabError> {
self.apply_update(AwarenessUpdate::decode_v1(update)?)
}
pub(crate) fn apply_update(&mut self, update: AwarenessUpdate) -> Result<(), CollabError> {
Ok(self.awareness.apply_update(update)?)
}
pub(crate) fn update(&self) -> Result<AwarenessUpdate, CollabError> {
Ok(self.awareness.update()?)
}
}
fn cursor_json(binding: &Binding, doc: &Document, selection: Selection) -> Option<Json> {
let Selection::Text { anchor, head } = selection else {
return None;
};
let anchor = serde_json::to_value(sticky(binding, doc, anchor)?).ok()?;
let head = serde_json::to_value(sticky(binding, doc, head)?).ok()?;
Some(json!({ "anchor": anchor, "head": head }))
}
fn user_of(state: &Json) -> Option<User> {
let user = state.get("user")?;
Some(User {
name: user.get("name")?.as_str()?.to_owned(),
color: user.get("color")?.as_str().unwrap_or_default().to_owned(),
})
}
fn selection_of(binding: &Binding, doc: &Document, state: &Json) -> Option<Selection> {
let cursor = state.get("cursor")?;
let anchor: StickyIndex = serde_json::from_value(cursor.get("anchor")?.clone()).ok()?;
let head: StickyIndex = serde_json::from_value(cursor.get("head")?.clone()).ok()?;
Some(Selection::text(
point(binding, doc, &anchor)?,
point(binding, doc, &head)?,
))
}
pub(crate) fn sticky(binding: &Binding, doc: &Document, point: Point) -> Option<StickyIndex> {
let txn = binding.yrs().transact();
let block = binding.registry().block(point.block)?;
let text = tree::text_of(&map_ref(&txn, &block.map)?, &txn)?;
doc.inline(point.block)?.check_offset(point.offset).ok()?;
let index = Chunks::read(&text, &txn).utf16_index(point.offset)?;
text.sticky_index(&txn, index, Assoc::After)
.or_else(|| Some(StickyIndex::from_type(&txn, &text, Assoc::After)))
}
pub(crate) fn point(binding: &Binding, doc: &Document, sticky: &StickyIndex) -> Option<Point> {
let txn = binding.yrs().transact();
let offset = sticky.get_offset(&txn)?;
let BranchID::Nested(text) = offset.branch.id() else {
return None;
};
let Some(Owner::Text(block)) = binding.registry().owner(&text) else {
return None;
};
let mut cursor = Cursor::default();
cursor.advance(doc.inline(block)?.as_str(), offset.index)?;
Some(Point::new(block, cursor.utf8))
}