Add cursor types
This commit is contained in:
parent
23ba0d2c82
commit
c9c0ffecf1
2 changed files with 116 additions and 0 deletions
45
backend/reconcile/src/cursor.rs
Normal file
45
backend/reconcile/src/cursor.rs
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
|
#[cfg(feature = "serde")]
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
// CursorPosition is a wrapper around usize to represent the position of an
|
||||||
|
// identifiable cursor in a text document based on the character index.
|
||||||
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Default)]
|
||||||
|
pub struct CursorPosition {
|
||||||
|
pub id: usize,
|
||||||
|
pub char_index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
|
#[derive(Debug, Clone, PartialEq, Default)]
|
||||||
|
pub struct TextWithCursors<'a> {
|
||||||
|
pub text: Cow<'a, str>,
|
||||||
|
pub cursors: Vec<CursorPosition>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> TextWithCursors<'a> {
|
||||||
|
pub fn new(text: &'a str, cursors: Vec<CursorPosition>) -> Self {
|
||||||
|
Self {
|
||||||
|
text: text.into(),
|
||||||
|
cursors,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_owned(text: String, cursors: Vec<CursorPosition>) -> Self {
|
||||||
|
Self {
|
||||||
|
text: text.into(),
|
||||||
|
cursors,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a str> for TextWithCursors<'a> {
|
||||||
|
fn from(text: &'a str) -> Self {
|
||||||
|
Self {
|
||||||
|
text: text.into(),
|
||||||
|
cursors: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
71
backend/sync_lib/src/cursor.rs
Normal file
71
backend/sync_lib/src/cursor.rs
Normal file
|
|
@ -0,0 +1,71 @@
|
||||||
|
use reconcile::{CursorPosition, TextWithCursors};
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
/// Wrapper type to expose `TextWithCursors` to JS.
|
||||||
|
#[wasm_bindgen]
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub struct OwnedTextWithCursors {
|
||||||
|
text: String,
|
||||||
|
cursors: Vec<OwnedCursorPosition>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OwnedTextWithCursors {
|
||||||
|
pub fn new(text: impl Into<String>, cursors: Vec<OwnedCursorPosition>) -> Self {
|
||||||
|
Self {
|
||||||
|
text: text.into(),
|
||||||
|
cursors,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<OwnedTextWithCursors> for TextWithCursors<'_> {
|
||||||
|
fn from(owned: OwnedTextWithCursors) -> Self {
|
||||||
|
TextWithCursors::new_owned(
|
||||||
|
owned.text.to_string(),
|
||||||
|
owned
|
||||||
|
.cursors
|
||||||
|
.into_iter()
|
||||||
|
.map(|cursor| cursor.into())
|
||||||
|
.collect(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<TextWithCursors<'_>> for OwnedTextWithCursors {
|
||||||
|
fn from(text_with_cursors: TextWithCursors<'_>) -> Self {
|
||||||
|
OwnedTextWithCursors {
|
||||||
|
text: text_with_cursors.text.into_owned(),
|
||||||
|
cursors: text_with_cursors
|
||||||
|
.cursors
|
||||||
|
.into_iter()
|
||||||
|
.map(|cursor| cursor.into())
|
||||||
|
.collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Wrapper type to expose `CursorPosition` to JS.
|
||||||
|
#[wasm_bindgen]
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub struct OwnedCursorPosition {
|
||||||
|
pub id: usize,
|
||||||
|
pub char_index: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<OwnedCursorPosition> for CursorPosition {
|
||||||
|
fn from(owned: OwnedCursorPosition) -> Self {
|
||||||
|
CursorPosition {
|
||||||
|
id: owned.id,
|
||||||
|
char_index: owned.char_index,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<CursorPosition> for OwnedCursorPosition {
|
||||||
|
fn from(cursor: CursorPosition) -> Self {
|
||||||
|
OwnedCursorPosition {
|
||||||
|
id: cursor.id,
|
||||||
|
char_index: cursor.char_index,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue