grid_rev.rs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. use crate::entities::{CellChangeset, FieldType, RowOrder};
  2. use crate::revision::GridSettingRevision;
  3. use bytes::Bytes;
  4. use indexmap::IndexMap;
  5. use nanoid::nanoid;
  6. use serde::{Deserialize, Serialize};
  7. use std::collections::HashMap;
  8. use std::sync::Arc;
  9. pub const DEFAULT_ROW_HEIGHT: i32 = 42;
  10. pub fn gen_grid_id() -> String {
  11. // nanoid calculator https://zelark.github.io/nano-id-cc/
  12. nanoid!(10)
  13. }
  14. pub fn gen_block_id() -> String {
  15. nanoid!(10)
  16. }
  17. pub fn gen_row_id() -> String {
  18. nanoid!(6)
  19. }
  20. pub fn gen_field_id() -> String {
  21. nanoid!(6)
  22. }
  23. #[derive(Debug, Clone, Default, Serialize, Deserialize)]
  24. pub struct GridRevision {
  25. pub grid_id: String,
  26. pub fields: Vec<Arc<FieldRevision>>,
  27. pub blocks: Vec<Arc<GridBlockMetaRevision>>,
  28. #[serde(default, skip)]
  29. pub setting: GridSettingRevision,
  30. }
  31. impl GridRevision {
  32. pub fn new(grid_id: &str) -> Self {
  33. Self {
  34. grid_id: grid_id.to_owned(),
  35. fields: vec![],
  36. blocks: vec![],
  37. setting: GridSettingRevision::default(),
  38. }
  39. }
  40. pub fn from_build_context(grid_id: &str, context: BuildGridContext) -> Self {
  41. Self {
  42. grid_id: grid_id.to_owned(),
  43. fields: context.field_revs.into_iter().map(Arc::new).collect(),
  44. blocks: context.blocks.into_iter().map(Arc::new).collect(),
  45. setting: Default::default(),
  46. }
  47. }
  48. }
  49. #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
  50. pub struct GridBlockMetaRevision {
  51. pub block_id: String,
  52. pub start_row_index: i32,
  53. pub row_count: i32,
  54. }
  55. impl GridBlockMetaRevision {
  56. pub fn len(&self) -> i32 {
  57. self.row_count
  58. }
  59. pub fn is_empty(&self) -> bool {
  60. self.row_count == 0
  61. }
  62. }
  63. impl GridBlockMetaRevision {
  64. pub fn new() -> Self {
  65. GridBlockMetaRevision {
  66. block_id: gen_block_id(),
  67. ..Default::default()
  68. }
  69. }
  70. }
  71. pub struct GridBlockMetaRevisionChangeset {
  72. pub block_id: String,
  73. pub start_row_index: Option<i32>,
  74. pub row_count: Option<i32>,
  75. }
  76. impl GridBlockMetaRevisionChangeset {
  77. pub fn from_row_count(block_id: &str, row_count: i32) -> Self {
  78. Self {
  79. block_id: block_id.to_string(),
  80. start_row_index: None,
  81. row_count: Some(row_count),
  82. }
  83. }
  84. }
  85. #[derive(Debug, Clone, Default, Serialize, Deserialize)]
  86. pub struct GridBlockRevision {
  87. pub block_id: String,
  88. pub rows: Vec<Arc<RowRevision>>,
  89. }
  90. #[derive(Debug, Clone, Default, Serialize, Deserialize, Eq, PartialEq)]
  91. pub struct FieldRevision {
  92. pub id: String,
  93. pub name: String,
  94. pub desc: String,
  95. pub field_type: FieldType,
  96. pub frozen: bool,
  97. pub visibility: bool,
  98. pub width: i32,
  99. /// type_options contains key/value pairs
  100. /// key: id of the FieldType
  101. /// value: type option data that can be parsed into specified TypeOptionStruct.
  102. /// For example, CheckboxTypeOption, MultiSelectTypeOption etc.
  103. #[serde(with = "indexmap::serde_seq")]
  104. pub type_options: IndexMap<String, String>,
  105. #[serde(default = "DEFAULT_IS_PRIMARY")]
  106. pub is_primary: bool,
  107. }
  108. impl AsRef<FieldRevision> for FieldRevision {
  109. fn as_ref(&self) -> &FieldRevision {
  110. self
  111. }
  112. }
  113. const DEFAULT_IS_PRIMARY: fn() -> bool = || false;
  114. impl FieldRevision {
  115. pub fn new(name: &str, desc: &str, field_type: FieldType, is_primary: bool) -> Self {
  116. let width = field_type.default_cell_width();
  117. Self {
  118. id: gen_field_id(),
  119. name: name.to_string(),
  120. desc: desc.to_string(),
  121. field_type,
  122. frozen: false,
  123. visibility: true,
  124. width,
  125. type_options: Default::default(),
  126. is_primary,
  127. }
  128. }
  129. pub fn insert_type_option_entry<T>(&mut self, entry: &T)
  130. where
  131. T: TypeOptionDataEntry + ?Sized,
  132. {
  133. self.type_options.insert(entry.field_type().type_id(), entry.json_str());
  134. }
  135. pub fn get_type_option_entry<T: TypeOptionDataDeserializer>(&self, field_type: &FieldType) -> Option<T> {
  136. self.type_options
  137. .get(&field_type.type_id())
  138. .map(|s| T::from_json_str(s))
  139. }
  140. pub fn insert_type_option_str(&mut self, field_type: &FieldType, json_str: String) {
  141. self.type_options.insert(field_type.type_id(), json_str);
  142. }
  143. pub fn get_type_option_str(&self, field_type: &FieldType) -> Option<String> {
  144. self.type_options.get(&field_type.type_id()).map(|s| s.to_owned())
  145. }
  146. }
  147. pub trait TypeOptionDataEntry {
  148. fn field_type(&self) -> FieldType;
  149. fn json_str(&self) -> String;
  150. fn protobuf_bytes(&self) -> Bytes;
  151. }
  152. pub trait TypeOptionDataDeserializer {
  153. fn from_json_str(s: &str) -> Self;
  154. fn from_protobuf_bytes(bytes: Bytes) -> Self;
  155. }
  156. #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
  157. pub struct RowRevision {
  158. pub id: String,
  159. pub block_id: String,
  160. /// cells contains key/value pairs.
  161. /// key: field id,
  162. /// value: CellMeta
  163. #[serde(with = "indexmap::serde_seq")]
  164. pub cells: IndexMap<String, CellRevision>,
  165. pub height: i32,
  166. pub visibility: bool,
  167. }
  168. impl RowRevision {
  169. pub fn new(block_id: &str) -> Self {
  170. Self {
  171. id: gen_row_id(),
  172. block_id: block_id.to_owned(),
  173. cells: Default::default(),
  174. height: DEFAULT_ROW_HEIGHT,
  175. visibility: true,
  176. }
  177. }
  178. }
  179. impl std::convert::From<&RowRevision> for RowOrder {
  180. fn from(row: &RowRevision) -> Self {
  181. Self {
  182. row_id: row.id.clone(),
  183. block_id: row.block_id.clone(),
  184. height: row.height,
  185. }
  186. }
  187. }
  188. impl std::convert::From<&Arc<RowRevision>> for RowOrder {
  189. fn from(row: &Arc<RowRevision>) -> Self {
  190. Self {
  191. row_id: row.id.clone(),
  192. block_id: row.block_id.clone(),
  193. height: row.height,
  194. }
  195. }
  196. }
  197. #[derive(Debug, Clone, Default)]
  198. pub struct RowMetaChangeset {
  199. pub row_id: String,
  200. pub height: Option<i32>,
  201. pub visibility: Option<bool>,
  202. pub cell_by_field_id: HashMap<String, CellRevision>,
  203. }
  204. impl std::convert::From<CellChangeset> for RowMetaChangeset {
  205. fn from(changeset: CellChangeset) -> Self {
  206. let mut cell_by_field_id = HashMap::with_capacity(1);
  207. let field_id = changeset.field_id;
  208. let cell_rev = CellRevision {
  209. data: changeset.cell_content_changeset.unwrap_or_else(|| "".to_owned()),
  210. };
  211. cell_by_field_id.insert(field_id, cell_rev);
  212. RowMetaChangeset {
  213. row_id: changeset.row_id,
  214. height: None,
  215. visibility: None,
  216. cell_by_field_id,
  217. }
  218. }
  219. }
  220. #[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
  221. pub struct CellRevision {
  222. pub data: String,
  223. }
  224. impl CellRevision {
  225. pub fn new(data: String) -> Self {
  226. Self { data }
  227. }
  228. }
  229. #[derive(Clone, Default, Deserialize, Serialize)]
  230. pub struct BuildGridContext {
  231. pub field_revs: Vec<FieldRevision>,
  232. pub blocks: Vec<GridBlockMetaRevision>,
  233. pub blocks_meta_data: Vec<GridBlockRevision>,
  234. }
  235. impl BuildGridContext {
  236. pub fn new() -> Self {
  237. Self::default()
  238. }
  239. }
  240. impl std::convert::From<BuildGridContext> for Bytes {
  241. fn from(ctx: BuildGridContext) -> Self {
  242. let bytes = serde_json::to_vec(&ctx).unwrap_or_else(|_| vec![]);
  243. Bytes::from(bytes)
  244. }
  245. }
  246. impl std::convert::TryFrom<Bytes> for BuildGridContext {
  247. type Error = serde_json::Error;
  248. fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
  249. let ctx: BuildGridContext = serde_json::from_slice(&bytes)?;
  250. Ok(ctx)
  251. }
  252. }