AppFlowy/shared-lib/flowy-grid-data-model/src/entities/meta.rs

237 lines
5.7 KiB
Rust
Raw Normal View History

use crate::entities::FieldType;
2022-04-01 08:38:51 +00:00
use bytes::Bytes;
use indexmap::IndexMap;
2022-04-11 07:27:03 +00:00
use nanoid::nanoid;
2022-03-10 04:01:31 +00:00
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
2022-04-04 12:47:04 +00:00
pub const DEFAULT_ROW_HEIGHT: i32 = 42;
2022-03-10 04:01:31 +00:00
2022-04-11 07:27:03 +00:00
pub fn gen_grid_id() -> String {
// nanoid calculator https://zelark.github.io/nano-id-cc/
nanoid!(10)
}
pub fn gen_block_id() -> String {
nanoid!(10)
}
pub fn gen_row_id() -> String {
nanoid!(6)
}
pub fn gen_field_id() -> String {
nanoid!(6)
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
2022-03-10 04:01:31 +00:00
pub struct GridMeta {
pub grid_id: String,
2022-03-15 03:07:18 +00:00
pub fields: Vec<FieldMeta>,
pub blocks: Vec<GridBlockMeta>,
2022-03-10 04:01:31 +00:00
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
2022-03-17 09:25:43 +00:00
pub struct GridBlockMeta {
pub block_id: String,
2022-03-10 09:14:10 +00:00
pub start_row_index: i32,
pub row_count: i32,
}
2022-03-17 09:25:43 +00:00
impl GridBlockMeta {
2022-03-15 03:07:18 +00:00
pub fn len(&self) -> i32 {
2022-03-18 09:14:46 +00:00
self.row_count
2022-03-15 03:07:18 +00:00
}
2022-03-16 02:02:37 +00:00
pub fn is_empty(&self) -> bool {
self.row_count == 0
}
2022-03-15 03:07:18 +00:00
}
2022-03-17 09:25:43 +00:00
impl GridBlockMeta {
2022-03-12 01:30:13 +00:00
pub fn new() -> Self {
2022-03-17 09:25:43 +00:00
GridBlockMeta {
2022-04-11 07:27:03 +00:00
block_id: gen_block_id(),
2022-03-12 01:30:13 +00:00
..Default::default()
}
}
}
2022-03-17 09:25:43 +00:00
pub struct GridBlockMetaChangeset {
2022-03-11 13:36:00 +00:00
pub block_id: String,
pub start_row_index: Option<i32>,
pub row_count: Option<i32>,
}
2022-03-17 09:25:43 +00:00
impl GridBlockMetaChangeset {
2022-03-13 03:06:28 +00:00
pub fn from_row_count(block_id: &str, row_count: i32) -> Self {
Self {
block_id: block_id.to_string(),
start_row_index: None,
row_count: Some(row_count),
}
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
2022-04-02 02:54:01 +00:00
pub struct GridBlockMetaData {
2022-03-10 09:14:10 +00:00
pub block_id: String,
pub rows: Vec<RowMeta>,
2022-03-10 04:01:31 +00:00
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, Eq, PartialEq)]
2022-03-15 03:07:18 +00:00
pub struct FieldMeta {
2022-03-10 04:01:31 +00:00
pub id: String,
pub name: String,
pub desc: String,
pub field_type: FieldType,
pub frozen: bool,
pub visibility: bool,
pub width: i32,
// #[pb(index = 8)]
2022-04-08 23:35:35 +00:00
/// type_options contains key/value pairs
/// key: id of the FieldType
/// value: type option data string
#[serde(with = "indexmap::serde_seq")]
pub type_options: IndexMap<String, String>,
2022-03-10 04:01:31 +00:00
}
2022-03-15 03:07:18 +00:00
impl FieldMeta {
2022-03-12 01:30:13 +00:00
pub fn new(name: &str, desc: &str, field_type: FieldType) -> Self {
2022-04-09 07:57:12 +00:00
let width = field_type.default_cell_width();
2022-03-10 04:01:31 +00:00
Self {
2022-04-11 07:27:03 +00:00
id: gen_field_id(),
2022-03-10 04:01:31 +00:00
name: name.to_string(),
desc: desc.to_string(),
field_type,
frozen: false,
visibility: true,
2022-04-09 07:57:12 +00:00
width,
2022-04-08 23:35:35 +00:00
type_options: Default::default(),
2022-04-01 08:38:51 +00:00
}
}
2022-04-10 12:21:28 +00:00
pub fn insert_type_option_entry<T>(&mut self, entry: &T)
where
T: TypeOptionDataEntry + ?Sized,
{
2022-04-08 23:35:35 +00:00
self.type_options.insert(entry.field_type().type_id(), entry.json_str());
2022-04-01 14:49:26 +00:00
}
2022-04-10 12:21:28 +00:00
pub fn get_type_option_entry<T: TypeOptionDataDeserializer>(&self, field_type: &FieldType) -> Option<T> {
2022-04-08 23:35:35 +00:00
self.type_options
2022-04-01 14:49:26 +00:00
.get(&field_type.type_id())
.map(|s| T::from_json_str(s))
}
pub fn insert_type_option_str(&mut self, field_type: &FieldType, json_str: String) {
2022-04-08 23:35:35 +00:00
self.type_options.insert(field_type.type_id(), json_str);
2022-04-01 14:49:26 +00:00
}
pub fn get_type_option_str(&self, field_type: Option<FieldType>) -> Option<String> {
let field_type = field_type.as_ref().unwrap_or(&self.field_type);
2022-04-08 23:35:35 +00:00
self.type_options.get(&field_type.type_id()).map(|s| s.to_owned())
2022-04-01 08:38:51 +00:00
}
}
pub trait TypeOptionDataEntry {
fn field_type(&self) -> FieldType;
fn json_str(&self) -> String;
fn protobuf_bytes(&self) -> Bytes;
}
2022-04-10 12:21:28 +00:00
pub trait TypeOptionDataDeserializer {
2022-04-01 08:38:51 +00:00
fn from_json_str(s: &str) -> Self;
fn from_protobuf_bytes(bytes: Bytes) -> Self;
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
2022-03-10 04:01:31 +00:00
pub struct RowMeta {
pub id: String,
2022-03-10 13:43:23 +00:00
pub block_id: String,
2022-04-08 23:35:35 +00:00
/// cells contains key/value pairs.
/// key: field id,
/// value: CellMeta
#[serde(with = "indexmap::serde_seq")]
pub cells: IndexMap<String, CellMeta>,
2022-03-10 04:01:31 +00:00
pub height: i32,
pub visibility: bool,
}
impl RowMeta {
2022-03-13 03:06:28 +00:00
pub fn new(block_id: &str) -> Self {
2022-03-10 04:01:31 +00:00
Self {
2022-04-11 07:27:03 +00:00
id: gen_row_id(),
2022-03-11 13:36:00 +00:00
block_id: block_id.to_owned(),
2022-04-08 23:35:35 +00:00
cells: Default::default(),
2022-03-10 04:01:31 +00:00
height: DEFAULT_ROW_HEIGHT,
visibility: true,
}
}
}
#[derive(Debug, Clone, Default)]
2022-03-10 13:43:23 +00:00
pub struct RowMetaChangeset {
pub row_id: String,
pub height: Option<i32>,
pub visibility: Option<bool>,
pub cell_by_field_id: HashMap<String, CellMeta>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
2022-03-10 04:01:31 +00:00
pub struct CellMeta {
2022-03-13 03:06:28 +00:00
pub data: String,
}
2022-03-10 04:01:31 +00:00
2022-03-13 03:06:28 +00:00
impl CellMeta {
2022-04-08 23:35:35 +00:00
pub fn new(data: String) -> Self {
Self { data }
2022-03-13 03:06:28 +00:00
}
2022-03-10 04:01:31 +00:00
}
2022-03-14 09:24:25 +00:00
#[derive(Clone, Deserialize, Serialize)]
pub struct BuildGridContext {
pub field_metas: Vec<FieldMeta>,
pub block_meta: GridBlockMeta,
pub block_meta_data: GridBlockMetaData,
2022-03-14 09:24:25 +00:00
}
impl std::convert::From<BuildGridContext> for Bytes {
fn from(ctx: BuildGridContext) -> Self {
let bytes = serde_json::to_vec(&ctx).unwrap_or_else(|_| vec![]);
Bytes::from(bytes)
2022-03-14 09:24:25 +00:00
}
}
2022-03-15 11:00:28 +00:00
impl std::convert::TryFrom<Bytes> for BuildGridContext {
type Error = serde_json::Error;
2022-03-15 11:00:28 +00:00
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
let ctx: BuildGridContext = serde_json::from_slice(&bytes)?;
Ok(ctx)
}
2022-03-15 11:00:28 +00:00
}
impl std::default::Default for BuildGridContext {
fn default() -> Self {
let block_meta = GridBlockMeta::new();
let block_meta_data = GridBlockMetaData {
block_id: block_meta.block_id.clone(),
rows: vec![],
2022-03-15 11:00:28 +00:00
};
Self {
field_metas: vec![],
block_meta,
block_meta_data,
2022-03-15 11:00:28 +00:00
}
}
}