mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
chore: add grid view revision struct
This commit is contained in:
parent
2623974def
commit
8da6ed9d28
@ -5,7 +5,7 @@ use crate::entities::{
|
||||
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
|
||||
use flowy_error::ErrorCode;
|
||||
use flowy_grid_data_model::parser::NotEmptyStr;
|
||||
use flowy_grid_data_model::revision::GridLayoutRevision;
|
||||
use flowy_grid_data_model::revision::LayoutRevision;
|
||||
use flowy_sync::entities::grid::GridSettingChangesetParams;
|
||||
use std::collections::HashMap;
|
||||
use std::convert::TryInto;
|
||||
@ -19,7 +19,7 @@ pub struct GridSettingPB {
|
||||
pub layouts: Vec<GridLayoutPB>,
|
||||
|
||||
#[pb(index = 2)]
|
||||
pub current_layout_type: GridLayoutType,
|
||||
pub current_layout_type: Layout,
|
||||
|
||||
#[pb(index = 3)]
|
||||
pub filter_configuration_by_field_id: HashMap<String, RepeatedGridConfigurationFilterPB>,
|
||||
@ -34,13 +34,13 @@ pub struct GridSettingPB {
|
||||
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
|
||||
pub struct GridLayoutPB {
|
||||
#[pb(index = 1)]
|
||||
ty: GridLayoutType,
|
||||
ty: Layout,
|
||||
}
|
||||
|
||||
impl GridLayoutPB {
|
||||
pub fn all() -> Vec<GridLayoutPB> {
|
||||
let mut layouts = vec![];
|
||||
for layout_ty in GridLayoutType::iter() {
|
||||
for layout_ty in Layout::iter() {
|
||||
layouts.push(GridLayoutPB { ty: layout_ty })
|
||||
}
|
||||
|
||||
@ -50,31 +50,31 @@ impl GridLayoutPB {
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, ProtoBuf_Enum, EnumIter)]
|
||||
#[repr(u8)]
|
||||
pub enum GridLayoutType {
|
||||
pub enum Layout {
|
||||
Table = 0,
|
||||
Board = 1,
|
||||
}
|
||||
|
||||
impl std::default::Default for GridLayoutType {
|
||||
impl std::default::Default for Layout {
|
||||
fn default() -> Self {
|
||||
GridLayoutType::Table
|
||||
Layout::Table
|
||||
}
|
||||
}
|
||||
|
||||
impl std::convert::From<GridLayoutRevision> for GridLayoutType {
|
||||
fn from(rev: GridLayoutRevision) -> Self {
|
||||
impl std::convert::From<LayoutRevision> for Layout {
|
||||
fn from(rev: LayoutRevision) -> Self {
|
||||
match rev {
|
||||
GridLayoutRevision::Table => GridLayoutType::Table,
|
||||
GridLayoutRevision::Board => GridLayoutType::Board,
|
||||
LayoutRevision::Table => Layout::Table,
|
||||
LayoutRevision::Board => Layout::Board,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::convert::From<GridLayoutType> for GridLayoutRevision {
|
||||
fn from(layout: GridLayoutType) -> Self {
|
||||
impl std::convert::From<Layout> for LayoutRevision {
|
||||
fn from(layout: Layout) -> Self {
|
||||
match layout {
|
||||
GridLayoutType::Table => GridLayoutRevision::Table,
|
||||
GridLayoutType::Board => GridLayoutRevision::Board,
|
||||
Layout::Table => LayoutRevision::Table,
|
||||
Layout::Board => LayoutRevision::Board,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -85,7 +85,7 @@ pub struct GridSettingChangesetPayloadPB {
|
||||
pub grid_id: String,
|
||||
|
||||
#[pb(index = 2)]
|
||||
pub layout_type: GridLayoutType,
|
||||
pub layout_type: Layout,
|
||||
|
||||
#[pb(index = 3, one_of)]
|
||||
pub insert_filter: Option<CreateGridFilterPayloadPB>,
|
||||
|
@ -108,7 +108,7 @@ pub(crate) async fn refresh_filter_cache(
|
||||
grid_pad: &Arc<RwLock<GridRevisionPad>>,
|
||||
) {
|
||||
let grid_pad = grid_pad.read().await;
|
||||
let filters_revs = grid_pad.get_filters(None, field_ids).unwrap_or_default();
|
||||
let filters_revs = grid_pad.get_filters(field_ids).unwrap_or_default();
|
||||
|
||||
for filter_rev in filters_revs {
|
||||
match grid_pad.get_field_rev(&filter_rev.field_id) {
|
||||
|
@ -447,10 +447,9 @@ impl GridRevisionEditor {
|
||||
Ok(grid_setting)
|
||||
}
|
||||
|
||||
pub async fn get_grid_filter(&self, layout_type: &GridLayoutType) -> FlowyResult<Vec<GridFilterConfiguration>> {
|
||||
pub async fn get_grid_filter(&self) -> FlowyResult<Vec<GridFilterConfiguration>> {
|
||||
let read_guard = self.grid_pad.read().await;
|
||||
let layout_rev = layout_type.clone().into();
|
||||
match read_guard.get_filters(Some(&layout_rev), None) {
|
||||
match read_guard.get_filters(None) {
|
||||
Some(filter_revs) => Ok(filter_revs
|
||||
.iter()
|
||||
.map(|filter_rev| filter_rev.as_ref().into())
|
||||
|
@ -83,8 +83,7 @@ impl GridGroupService {
|
||||
pub(crate) async fn get_group_configuration(&self, field_rev: &FieldRevision) -> GroupConfigurationRevision {
|
||||
let grid_pad = self.grid_pad.read().await;
|
||||
let setting = grid_pad.get_setting_rev();
|
||||
let layout = &setting.layout;
|
||||
let configurations = setting.get_groups(layout, &field_rev.id, &field_rev.field_type_rev);
|
||||
let configurations = setting.get_groups(&field_rev.id, &field_rev.field_type_rev);
|
||||
match configurations {
|
||||
None => default_group_configuration(field_rev),
|
||||
Some(mut configurations) => {
|
||||
|
@ -1,8 +1,8 @@
|
||||
use crate::entities::{
|
||||
GridLayoutPB, GridLayoutType, GridSettingPB, RepeatedGridConfigurationFilterPB, RepeatedGridGroupConfigurationPB,
|
||||
GridLayoutPB, GridSettingPB, Layout, RepeatedGridConfigurationFilterPB, RepeatedGridGroupConfigurationPB,
|
||||
RepeatedGridSortPB,
|
||||
};
|
||||
use flowy_grid_data_model::revision::{FieldRevision, GridSettingRevision};
|
||||
use flowy_grid_data_model::revision::{FieldRevision, SettingRevision};
|
||||
use flowy_sync::entities::grid::{CreateGridFilterParams, DeleteFilterParams, GridSettingChangesetParams};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
@ -12,7 +12,7 @@ pub struct GridSettingChangesetBuilder {
|
||||
}
|
||||
|
||||
impl GridSettingChangesetBuilder {
|
||||
pub fn new(grid_id: &str, layout_type: &GridLayoutType) -> Self {
|
||||
pub fn new(grid_id: &str, layout_type: &Layout) -> Self {
|
||||
let params = GridSettingChangesetParams {
|
||||
grid_id: grid_id.to_string(),
|
||||
layout_type: layout_type.clone().into(),
|
||||
@ -41,8 +41,8 @@ impl GridSettingChangesetBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn make_grid_setting(grid_setting_rev: &GridSettingRevision, field_revs: &[Arc<FieldRevision>]) -> GridSettingPB {
|
||||
let current_layout_type: GridLayoutType = grid_setting_rev.layout.clone().into();
|
||||
pub fn make_grid_setting(grid_setting_rev: &SettingRevision, field_revs: &[Arc<FieldRevision>]) -> GridSettingPB {
|
||||
let current_layout_type: Layout = grid_setting_rev.layout.clone().into();
|
||||
let filters_by_field_id = grid_setting_rev
|
||||
.get_all_filters(field_revs)
|
||||
.map(|filters_by_field_id| {
|
||||
|
@ -3,7 +3,7 @@
|
||||
#![allow(dead_code)]
|
||||
#![allow(unused_imports)]
|
||||
|
||||
use flowy_grid::entities::{CreateGridFilterPayloadPB, GridLayoutType, GridSettingPB};
|
||||
use flowy_grid::entities::{CreateGridFilterPayloadPB, Layout, GridSettingPB};
|
||||
use flowy_grid::services::setting::GridSettingChangesetBuilder;
|
||||
use flowy_grid_data_model::revision::{FieldRevision, FieldTypeRevision};
|
||||
use flowy_sync::entities::grid::{CreateGridFilterParams, DeleteFilterParams, GridSettingChangesetParams};
|
||||
@ -55,19 +55,18 @@ impl GridFilterTest {
|
||||
}
|
||||
FilterScript::InsertGridTableFilter { payload } => {
|
||||
let params: CreateGridFilterParams = payload.try_into().unwrap();
|
||||
let layout_type = GridLayoutType::Table;
|
||||
let layout_type = Layout::Table;
|
||||
let params = GridSettingChangesetBuilder::new(&self.grid_id, &layout_type)
|
||||
.insert_filter(params)
|
||||
.build();
|
||||
let _ = self.editor.update_grid_setting(params).await.unwrap();
|
||||
}
|
||||
FilterScript::AssertTableFilterCount { count } => {
|
||||
let layout_type = GridLayoutType::Table;
|
||||
let filters = self.editor.get_grid_filter(&layout_type).await.unwrap();
|
||||
let filters = self.editor.get_grid_filter().await.unwrap();
|
||||
assert_eq!(count as usize, filters.len());
|
||||
}
|
||||
FilterScript::DeleteGridTableFilter { filter_id, field_rev} => {
|
||||
let layout_type = GridLayoutType::Table;
|
||||
let layout_type = Layout::Table;
|
||||
let params = GridSettingChangesetBuilder::new(&self.grid_id, &layout_type)
|
||||
.delete_filter(DeleteFilterParams { field_id: field_rev.id, filter_id, field_type_rev: field_rev.field_type_rev })
|
||||
.build();
|
||||
|
@ -76,8 +76,7 @@ impl GridEditorTest {
|
||||
}
|
||||
|
||||
pub async fn grid_filters(&self) -> Vec<GridFilterConfiguration> {
|
||||
let layout_type = GridLayoutType::Table;
|
||||
self.editor.get_grid_filter(&layout_type).await.unwrap()
|
||||
self.editor.get_grid_filter().await.unwrap()
|
||||
}
|
||||
|
||||
pub fn get_field_rev(&self, field_type: FieldType) -> &Arc<FieldRevision> {
|
||||
|
@ -1,9 +0,0 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq, Hash)]
|
||||
pub struct FilterConfigurationRevision {
|
||||
pub id: String,
|
||||
pub field_id: String,
|
||||
pub condition: u8,
|
||||
pub content: Option<String>,
|
||||
}
|
61
shared-lib/flowy-grid-data-model/src/revision/grid_block.rs
Normal file
61
shared-lib/flowy-grid-data-model/src/revision/grid_block.rs
Normal file
@ -0,0 +1,61 @@
|
||||
use indexmap::IndexMap;
|
||||
use nanoid::nanoid;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub fn gen_row_id() -> String {
|
||||
nanoid!(6)
|
||||
}
|
||||
|
||||
pub const DEFAULT_ROW_HEIGHT: i32 = 42;
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
pub struct GridBlockRevision {
|
||||
pub block_id: String,
|
||||
pub rows: Vec<Arc<RowRevision>>,
|
||||
}
|
||||
|
||||
pub type FieldId = String;
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct RowRevision {
|
||||
pub id: String,
|
||||
pub block_id: String,
|
||||
/// cells contains key/value pairs.
|
||||
/// key: field id,
|
||||
/// value: CellMeta
|
||||
#[serde(with = "indexmap::serde_seq")]
|
||||
pub cells: IndexMap<FieldId, CellRevision>,
|
||||
pub height: i32,
|
||||
pub visibility: bool,
|
||||
}
|
||||
|
||||
impl RowRevision {
|
||||
pub fn new(block_id: &str) -> Self {
|
||||
Self {
|
||||
id: gen_row_id(),
|
||||
block_id: block_id.to_owned(),
|
||||
cells: Default::default(),
|
||||
height: DEFAULT_ROW_HEIGHT,
|
||||
visibility: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct RowMetaChangeset {
|
||||
pub row_id: String,
|
||||
pub height: Option<i32>,
|
||||
pub visibility: Option<bool>,
|
||||
pub cell_by_field_id: HashMap<FieldId, CellRevision>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
|
||||
pub struct CellRevision {
|
||||
pub data: String,
|
||||
}
|
||||
|
||||
impl CellRevision {
|
||||
pub fn new(data: String) -> Self {
|
||||
Self { data }
|
||||
}
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
use crate::revision::FieldTypeRevision;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
|
||||
pub struct GroupConfigurationRevision {
|
||||
pub id: String,
|
||||
pub field_id: String,
|
||||
pub field_type_rev: FieldTypeRevision,
|
||||
pub content: Option<Vec<u8>>,
|
||||
}
|
@ -1,13 +1,10 @@
|
||||
use crate::revision::GridSettingRevision;
|
||||
use crate::revision::{GridBlockRevision, SettingRevision};
|
||||
use bytes::Bytes;
|
||||
use indexmap::IndexMap;
|
||||
use nanoid::nanoid;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub const DEFAULT_ROW_HEIGHT: i32 = 42;
|
||||
|
||||
pub fn gen_grid_id() -> String {
|
||||
// nanoid calculator https://zelark.github.io/nano-id-cc/
|
||||
nanoid!(10)
|
||||
@ -17,10 +14,6 @@ pub fn gen_block_id() -> String {
|
||||
nanoid!(10)
|
||||
}
|
||||
|
||||
pub fn gen_row_id() -> String {
|
||||
nanoid!(6)
|
||||
}
|
||||
|
||||
pub fn gen_field_id() -> String {
|
||||
nanoid!(6)
|
||||
}
|
||||
@ -32,7 +25,7 @@ pub struct GridRevision {
|
||||
pub blocks: Vec<Arc<GridBlockMetaRevision>>,
|
||||
|
||||
#[serde(default)]
|
||||
pub setting: GridSettingRevision,
|
||||
pub setting: SettingRevision,
|
||||
}
|
||||
|
||||
impl GridRevision {
|
||||
@ -41,7 +34,7 @@ impl GridRevision {
|
||||
grid_id: grid_id.to_owned(),
|
||||
fields: vec![],
|
||||
blocks: vec![],
|
||||
setting: GridSettingRevision::default(),
|
||||
setting: SettingRevision::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -97,12 +90,6 @@ impl GridBlockMetaRevisionChangeset {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
pub struct GridBlockRevision {
|
||||
pub block_id: String,
|
||||
pub rows: Vec<Arc<RowRevision>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, Eq, PartialEq)]
|
||||
pub struct FieldRevision {
|
||||
pub id: String,
|
||||
@ -201,50 +188,6 @@ pub trait TypeOptionDataDeserializer {
|
||||
fn from_protobuf_bytes(bytes: Bytes) -> Self;
|
||||
}
|
||||
|
||||
pub type FieldId = String;
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct RowRevision {
|
||||
pub id: String,
|
||||
pub block_id: String,
|
||||
/// cells contains key/value pairs.
|
||||
/// key: field id,
|
||||
/// value: CellMeta
|
||||
#[serde(with = "indexmap::serde_seq")]
|
||||
pub cells: IndexMap<FieldId, CellRevision>,
|
||||
pub height: i32,
|
||||
pub visibility: bool,
|
||||
}
|
||||
|
||||
impl RowRevision {
|
||||
pub fn new(block_id: &str) -> Self {
|
||||
Self {
|
||||
id: gen_row_id(),
|
||||
block_id: block_id.to_owned(),
|
||||
cells: Default::default(),
|
||||
height: DEFAULT_ROW_HEIGHT,
|
||||
visibility: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct RowMetaChangeset {
|
||||
pub row_id: String,
|
||||
pub height: Option<i32>,
|
||||
pub visibility: Option<bool>,
|
||||
pub cell_by_field_id: HashMap<FieldId, CellRevision>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
|
||||
pub struct CellRevision {
|
||||
pub data: String,
|
||||
}
|
||||
|
||||
impl CellRevision {
|
||||
pub fn new(data: String) -> Self {
|
||||
Self { data }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Deserialize, Serialize)]
|
||||
pub struct BuildGridContext {
|
||||
pub field_revs: Vec<Arc<FieldRevision>>,
|
||||
|
@ -1,5 +1,3 @@
|
||||
use crate::revision::filter_rev::FilterConfigurationRevision;
|
||||
use crate::revision::grid_group::GroupConfigurationRevision;
|
||||
use crate::revision::{FieldRevision, FieldTypeRevision};
|
||||
use indexmap::IndexMap;
|
||||
use nanoid::nanoid;
|
||||
@ -21,26 +19,23 @@ pub fn gen_grid_sort_id() -> String {
|
||||
nanoid!(6)
|
||||
}
|
||||
|
||||
pub type FilterConfigurations = SettingConfiguration<FilterConfigurationRevision>;
|
||||
pub type FilterConfigurationRevisionMap = GridObjectRevisionMap<FilterConfigurationRevision>;
|
||||
pub type FilterConfiguration = Configuration<FilterConfigurationRevision>;
|
||||
pub type FilterConfigurationsByFieldId = HashMap<String, Vec<Arc<FilterConfigurationRevision>>>;
|
||||
//
|
||||
pub type GroupConfigurations = SettingConfiguration<GroupConfigurationRevision>;
|
||||
pub type GroupConfigurationRevisionMap = GridObjectRevisionMap<GroupConfigurationRevision>;
|
||||
pub type GroupConfiguration = Configuration<GroupConfigurationRevision>;
|
||||
pub type GroupConfigurationsByFieldId = HashMap<String, Vec<Arc<GroupConfigurationRevision>>>;
|
||||
//
|
||||
pub type SortConfigurations = SettingConfiguration<SortConfigurationRevision>;
|
||||
pub type SortConfigurationRevisionMap = GridObjectRevisionMap<SortConfigurationRevision>;
|
||||
pub type SortConfigurations = Configuration<SortConfigurationRevision>;
|
||||
pub type SortConfigurationsByFieldId = HashMap<String, Vec<Arc<SortConfigurationRevision>>>;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default, Eq, PartialEq)]
|
||||
pub struct GridSettingRevision {
|
||||
pub layout: GridLayoutRevision,
|
||||
pub struct SettingRevision {
|
||||
pub layout: LayoutRevision,
|
||||
|
||||
pub filters: FilterConfigurations,
|
||||
pub filters: FilterConfiguration,
|
||||
|
||||
#[serde(default)]
|
||||
pub groups: GroupConfigurations,
|
||||
pub groups: GroupConfiguration,
|
||||
|
||||
#[serde(skip)]
|
||||
pub sorts: SortConfigurations,
|
||||
@ -48,88 +43,83 @@ pub struct GridSettingRevision {
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize_repr, Deserialize_repr)]
|
||||
#[repr(u8)]
|
||||
pub enum GridLayoutRevision {
|
||||
pub enum LayoutRevision {
|
||||
Table = 0,
|
||||
Board = 1,
|
||||
}
|
||||
|
||||
impl ToString for GridLayoutRevision {
|
||||
impl ToString for LayoutRevision {
|
||||
fn to_string(&self) -> String {
|
||||
let layout_rev = self.clone() as u8;
|
||||
layout_rev.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::default::Default for GridLayoutRevision {
|
||||
impl std::default::Default for LayoutRevision {
|
||||
fn default() -> Self {
|
||||
GridLayoutRevision::Table
|
||||
LayoutRevision::Table
|
||||
}
|
||||
}
|
||||
|
||||
impl GridSettingRevision {
|
||||
impl SettingRevision {
|
||||
pub fn get_all_groups(&self, field_revs: &[Arc<FieldRevision>]) -> Option<GroupConfigurationsByFieldId> {
|
||||
self.groups.get_all_objects(&self.layout, field_revs)
|
||||
self.groups.get_all_objects(field_revs)
|
||||
}
|
||||
|
||||
pub fn get_groups(
|
||||
&self,
|
||||
layout: &GridLayoutRevision,
|
||||
field_id: &str,
|
||||
field_type_rev: &FieldTypeRevision,
|
||||
) -> Option<Vec<Arc<GroupConfigurationRevision>>> {
|
||||
self.groups.get_objects(layout, field_id, field_type_rev)
|
||||
self.groups.get_objects(field_id, field_type_rev)
|
||||
}
|
||||
|
||||
pub fn get_mut_groups(
|
||||
&mut self,
|
||||
layout: &GridLayoutRevision,
|
||||
field_id: &str,
|
||||
field_type: &FieldTypeRevision,
|
||||
) -> Option<&mut Vec<Arc<GroupConfigurationRevision>>> {
|
||||
self.groups.get_mut_objects(layout, field_id, field_type)
|
||||
self.groups.get_mut_objects(field_id, field_type)
|
||||
}
|
||||
|
||||
pub fn insert_group(
|
||||
&mut self,
|
||||
layout: &GridLayoutRevision,
|
||||
field_id: &str,
|
||||
field_type: &FieldTypeRevision,
|
||||
group_rev: GroupConfigurationRevision,
|
||||
) {
|
||||
self.groups.remove_all(layout);
|
||||
self.groups.insert_object(layout, field_id, field_type, group_rev);
|
||||
// only one group can be set
|
||||
self.groups.remove_all();
|
||||
self.groups.insert_object(field_id, field_type, group_rev);
|
||||
}
|
||||
|
||||
pub fn get_all_filters(&self, field_revs: &[Arc<FieldRevision>]) -> Option<FilterConfigurationsByFieldId> {
|
||||
self.filters.get_all_objects(&self.layout, field_revs)
|
||||
self.filters.get_all_objects(field_revs)
|
||||
}
|
||||
|
||||
pub fn get_filters(
|
||||
&self,
|
||||
layout: &GridLayoutRevision,
|
||||
field_id: &str,
|
||||
field_type_rev: &FieldTypeRevision,
|
||||
) -> Option<Vec<Arc<FilterConfigurationRevision>>> {
|
||||
self.filters.get_objects(layout, field_id, field_type_rev)
|
||||
self.filters.get_objects(field_id, field_type_rev)
|
||||
}
|
||||
|
||||
pub fn get_mut_filters(
|
||||
&mut self,
|
||||
layout: &GridLayoutRevision,
|
||||
field_id: &str,
|
||||
field_type: &FieldTypeRevision,
|
||||
) -> Option<&mut Vec<Arc<FilterConfigurationRevision>>> {
|
||||
self.filters.get_mut_objects(layout, field_id, field_type)
|
||||
self.filters.get_mut_objects(field_id, field_type)
|
||||
}
|
||||
|
||||
pub fn insert_filter(
|
||||
&mut self,
|
||||
layout: &GridLayoutRevision,
|
||||
field_id: &str,
|
||||
field_type: &FieldTypeRevision,
|
||||
filter_rev: FilterConfigurationRevision,
|
||||
) {
|
||||
self.filters.insert_object(layout, field_id, field_type, filter_rev);
|
||||
self.filters.insert_object(field_id, field_type, filter_rev);
|
||||
}
|
||||
|
||||
pub fn get_all_sort(&self) -> Option<SortConfigurationsByFieldId> {
|
||||
@ -145,59 +135,40 @@ pub struct SortConfigurationRevision {
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default, Eq, PartialEq)]
|
||||
#[serde(transparent)]
|
||||
pub struct SettingConfiguration<T>
|
||||
pub struct Configuration<T>
|
||||
where
|
||||
T: Debug + Clone + Default + Eq + PartialEq + serde::Serialize + serde::de::DeserializeOwned + 'static,
|
||||
{
|
||||
/// Each layout contains multiple key/value.
|
||||
/// Key: field_id
|
||||
/// Value: this value contains key/value.
|
||||
/// Key: FieldType,
|
||||
/// Value: the corresponding objects.
|
||||
#[serde(with = "indexmap::serde_seq")]
|
||||
inner: IndexMap<GridLayoutRevision, IndexMap<String, GridObjectRevisionMap<T>>>,
|
||||
inner: IndexMap<String, ObjectIndexMap<T>>,
|
||||
}
|
||||
|
||||
impl<T> SettingConfiguration<T>
|
||||
impl<T> Configuration<T>
|
||||
where
|
||||
T: Debug + Clone + Default + Eq + PartialEq + serde::Serialize + serde::de::DeserializeOwned + 'static,
|
||||
{
|
||||
pub fn get_mut_objects(
|
||||
&mut self,
|
||||
layout: &GridLayoutRevision,
|
||||
field_id: &str,
|
||||
field_type: &FieldTypeRevision,
|
||||
) -> Option<&mut Vec<Arc<T>>> {
|
||||
pub fn get_mut_objects(&mut self, field_id: &str, field_type: &FieldTypeRevision) -> Option<&mut Vec<Arc<T>>> {
|
||||
let value = self
|
||||
.inner
|
||||
.get_mut(layout)
|
||||
.and_then(|object_rev_map_by_field_id| object_rev_map_by_field_id.get_mut(field_id))
|
||||
.get_mut(field_id)
|
||||
.and_then(|object_rev_map| object_rev_map.get_mut(field_type));
|
||||
if value.is_none() {
|
||||
tracing::warn!("Can't find the {:?} with", std::any::type_name::<T>());
|
||||
}
|
||||
value
|
||||
}
|
||||
pub fn get_objects(
|
||||
&self,
|
||||
layout: &GridLayoutRevision,
|
||||
field_id: &str,
|
||||
field_type_rev: &FieldTypeRevision,
|
||||
) -> Option<Vec<Arc<T>>> {
|
||||
pub fn get_objects(&self, field_id: &str, field_type_rev: &FieldTypeRevision) -> Option<Vec<Arc<T>>> {
|
||||
self.inner
|
||||
.get(layout)
|
||||
.and_then(|object_rev_map_by_field_id| object_rev_map_by_field_id.get(field_id))
|
||||
.get(field_id)
|
||||
.and_then(|object_rev_map| object_rev_map.get(field_type_rev))
|
||||
.cloned()
|
||||
}
|
||||
|
||||
pub fn get_all_objects(
|
||||
&self,
|
||||
layout: &GridLayoutRevision,
|
||||
field_revs: &[Arc<FieldRevision>],
|
||||
) -> Option<HashMap<String, Vec<Arc<T>>>> {
|
||||
// Acquire the read lock.
|
||||
let object_rev_map_by_field_id = self.inner.get(layout)?;
|
||||
pub fn get_all_objects(&self, field_revs: &[Arc<FieldRevision>]) -> Option<HashMap<String, Vec<Arc<T>>>> {
|
||||
// Get the objects according to the FieldType, so we need iterate the field_revs.
|
||||
let objects_by_field_id = field_revs
|
||||
.iter()
|
||||
@ -205,7 +176,7 @@ where
|
||||
let field_type = &field_rev.field_type_rev;
|
||||
let field_id = &field_rev.id;
|
||||
|
||||
let object_rev_map = object_rev_map_by_field_id.get(field_id)?;
|
||||
let object_rev_map = self.inner.get(field_id)?;
|
||||
let objects: Vec<Arc<T>> = object_rev_map.get(field_type)?.clone();
|
||||
Some((field_rev.id.clone(), objects))
|
||||
})
|
||||
@ -213,17 +184,11 @@ where
|
||||
Some(objects_by_field_id)
|
||||
}
|
||||
|
||||
pub fn insert_object(
|
||||
&mut self,
|
||||
layout: &GridLayoutRevision,
|
||||
field_id: &str,
|
||||
field_type: &FieldTypeRevision,
|
||||
object: T,
|
||||
) {
|
||||
let object_rev_map_by_field_id = self.inner.entry(layout.clone()).or_insert_with(IndexMap::new);
|
||||
let object_rev_map = object_rev_map_by_field_id
|
||||
pub fn insert_object(&mut self, field_id: &str, field_type: &FieldTypeRevision, object: T) {
|
||||
let object_rev_map = self
|
||||
.inner
|
||||
.entry(field_id.to_string())
|
||||
.or_insert_with(GridObjectRevisionMap::<T>::new);
|
||||
.or_insert_with(ObjectIndexMap::<T>::new);
|
||||
|
||||
object_rev_map
|
||||
.entry(field_type.to_owned())
|
||||
@ -231,16 +196,14 @@ where
|
||||
.push(Arc::new(object))
|
||||
}
|
||||
|
||||
pub fn remove_all(&mut self, layout: &GridLayoutRevision) {
|
||||
if let Some(object_rev_map_by_field_id) = self.inner.get_mut(layout) {
|
||||
object_rev_map_by_field_id.clear()
|
||||
}
|
||||
pub fn remove_all(&mut self) {
|
||||
self.inner.clear()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default, Eq, PartialEq)]
|
||||
#[serde(transparent)]
|
||||
pub struct GridObjectRevisionMap<T>
|
||||
pub struct ObjectIndexMap<T>
|
||||
where
|
||||
T: Debug + Clone + Default + Eq + PartialEq + serde::Serialize + serde::de::DeserializeOwned + 'static,
|
||||
{
|
||||
@ -248,16 +211,16 @@ where
|
||||
pub object_by_field_type: IndexMap<FieldTypeRevision, Vec<Arc<T>>>,
|
||||
}
|
||||
|
||||
impl<T> GridObjectRevisionMap<T>
|
||||
impl<T> ObjectIndexMap<T>
|
||||
where
|
||||
T: Debug + Clone + Default + Eq + PartialEq + serde::Serialize + serde::de::DeserializeOwned + 'static,
|
||||
{
|
||||
pub fn new() -> Self {
|
||||
GridObjectRevisionMap::default()
|
||||
ObjectIndexMap::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> std::ops::Deref for GridObjectRevisionMap<T>
|
||||
impl<T> std::ops::Deref for ObjectIndexMap<T>
|
||||
where
|
||||
T: Debug + Clone + Default + Eq + PartialEq + serde::Serialize + serde::de::DeserializeOwned + 'static,
|
||||
{
|
||||
@ -268,7 +231,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> std::ops::DerefMut for GridObjectRevisionMap<T>
|
||||
impl<T> std::ops::DerefMut for ObjectIndexMap<T>
|
||||
where
|
||||
T: Debug + Clone + Default + Eq + PartialEq + serde::Serialize + serde::de::DeserializeOwned + 'static,
|
||||
{
|
||||
@ -276,3 +239,19 @@ where
|
||||
&mut self.object_by_field_type
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
|
||||
pub struct GroupConfigurationRevision {
|
||||
pub id: String,
|
||||
pub field_id: String,
|
||||
pub field_type_rev: FieldTypeRevision,
|
||||
pub content: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq, Hash)]
|
||||
pub struct FilterConfigurationRevision {
|
||||
pub id: String,
|
||||
pub field_id: String,
|
||||
pub condition: u8,
|
||||
pub content: Option<String>,
|
||||
}
|
||||
|
20
shared-lib/flowy-grid-data-model/src/revision/grid_view.rs
Normal file
20
shared-lib/flowy-grid-data-model/src/revision/grid_view.rs
Normal file
@ -0,0 +1,20 @@
|
||||
use crate::revision::SettingRevision;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
pub struct GridViewRevision {
|
||||
pub view_id: String,
|
||||
|
||||
pub grid_id: String,
|
||||
|
||||
pub setting: SettingRevision,
|
||||
// TODO: Save the rows' order.
|
||||
// For the moment, we just use the order returned from the GridRevision
|
||||
// #[serde(rename = "row")]
|
||||
// pub row_orders: Vec<RowOrderRevision>,
|
||||
}
|
||||
|
||||
// #[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
// pub struct RowOrderRevision {
|
||||
// pub row_id: String,
|
||||
// }
|
@ -1,9 +1,9 @@
|
||||
mod filter_rev;
|
||||
mod grid_group;
|
||||
mod grid_block;
|
||||
mod grid_rev;
|
||||
mod grid_setting_rev;
|
||||
mod grid_view;
|
||||
|
||||
pub use filter_rev::*;
|
||||
pub use grid_group::*;
|
||||
pub use grid_block::*;
|
||||
pub use grid_rev::*;
|
||||
pub use grid_setting_rev::*;
|
||||
pub use grid_view::*;
|
||||
|
@ -7,8 +7,8 @@ use crate::util::{cal_diff, make_delta_from_revisions};
|
||||
use bytes::Bytes;
|
||||
use flowy_grid_data_model::revision::{
|
||||
gen_block_id, gen_grid_filter_id, gen_grid_group_id, gen_grid_id, FieldRevision, FieldTypeRevision,
|
||||
FilterConfigurationRevision, GridBlockMetaRevision, GridBlockMetaRevisionChangeset, GridLayoutRevision,
|
||||
GridRevision, GridSettingRevision, GroupConfigurationRevision,
|
||||
FilterConfigurationRevision, GridBlockMetaRevision, GridBlockMetaRevisionChangeset, GridRevision,
|
||||
GroupConfigurationRevision, SettingRevision,
|
||||
};
|
||||
use lib_infra::util::move_vec_element;
|
||||
use lib_ot::core::{OperationTransform, PhantomAttributes, TextDelta, TextDeltaBuilder};
|
||||
@ -341,18 +341,13 @@ impl GridRevisionPad {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn get_setting_rev(&self) -> &GridSettingRevision {
|
||||
pub fn get_setting_rev(&self) -> &SettingRevision {
|
||||
&self.grid_rev.setting
|
||||
}
|
||||
|
||||
/// If layout is None, then the default layout will be the read from GridSettingRevision
|
||||
pub fn get_filters(
|
||||
&self,
|
||||
layout: Option<&GridLayoutRevision>,
|
||||
field_ids: Option<Vec<String>>,
|
||||
) -> Option<Vec<Arc<FilterConfigurationRevision>>> {
|
||||
pub fn get_filters(&self, field_ids: Option<Vec<String>>) -> Option<Vec<Arc<FilterConfigurationRevision>>> {
|
||||
let mut filter_revs = vec![];
|
||||
let layout_ty = layout.unwrap_or(&self.grid_rev.setting.layout);
|
||||
let field_revs = self.get_field_revs(None).ok()?;
|
||||
|
||||
field_revs.iter().for_each(|field_rev| {
|
||||
@ -365,8 +360,7 @@ impl GridRevisionPad {
|
||||
// Only return the filters for the current fields' type.
|
||||
let field_id = &field_rev.id;
|
||||
let field_type_rev = &field_rev.field_type_rev;
|
||||
if let Some(mut t_filter_revs) = self.grid_rev.setting.get_filters(layout_ty, field_id, field_type_rev)
|
||||
{
|
||||
if let Some(mut t_filter_revs) = self.grid_rev.setting.get_filters(field_id, field_type_rev) {
|
||||
filter_revs.append(&mut t_filter_revs);
|
||||
}
|
||||
}
|
||||
@ -381,40 +375,30 @@ impl GridRevisionPad {
|
||||
) -> CollaborateResult<Option<GridChangeset>> {
|
||||
self.modify_grid(|grid_rev| {
|
||||
let mut is_changed = None;
|
||||
let layout_rev = changeset.layout_type;
|
||||
if let Some(params) = changeset.insert_filter {
|
||||
grid_rev.setting.insert_filter(
|
||||
&layout_rev,
|
||||
¶ms.field_id,
|
||||
¶ms.field_type_rev,
|
||||
make_filter_revision(¶ms),
|
||||
);
|
||||
|
||||
grid_rev
|
||||
.setting
|
||||
.insert_filter(¶ms.field_id, ¶ms.field_type_rev, make_filter_revision(¶ms));
|
||||
is_changed = Some(())
|
||||
}
|
||||
if let Some(params) = changeset.delete_filter {
|
||||
if let Some(filters) =
|
||||
grid_rev
|
||||
.setting
|
||||
.get_mut_filters(&layout_rev, ¶ms.field_id, ¶ms.field_type_rev)
|
||||
if let Some(filters) = grid_rev
|
||||
.setting
|
||||
.get_mut_filters(¶ms.field_id, ¶ms.field_type_rev)
|
||||
{
|
||||
filters.retain(|filter| filter.id != params.filter_id);
|
||||
}
|
||||
}
|
||||
if let Some(params) = changeset.insert_group {
|
||||
grid_rev.setting.insert_group(
|
||||
&layout_rev,
|
||||
¶ms.field_id,
|
||||
¶ms.field_type_rev,
|
||||
make_group_revision(¶ms),
|
||||
);
|
||||
grid_rev
|
||||
.setting
|
||||
.insert_group(¶ms.field_id, ¶ms.field_type_rev, make_group_revision(¶ms));
|
||||
is_changed = Some(());
|
||||
}
|
||||
if let Some(params) = changeset.delete_group {
|
||||
if let Some(groups) =
|
||||
grid_rev
|
||||
.setting
|
||||
.get_mut_groups(&layout_rev, ¶ms.field_id, ¶ms.field_type_rev)
|
||||
if let Some(groups) = grid_rev
|
||||
.setting
|
||||
.get_mut_groups(¶ms.field_id, ¶ms.field_type_rev)
|
||||
{
|
||||
groups.retain(|filter| filter.id != params.group_id);
|
||||
}
|
||||
|
@ -1,8 +1,8 @@
|
||||
use flowy_grid_data_model::revision::{FieldTypeRevision, GridLayoutRevision};
|
||||
use flowy_grid_data_model::revision::{FieldTypeRevision, LayoutRevision};
|
||||
|
||||
pub struct GridSettingChangesetParams {
|
||||
pub grid_id: String,
|
||||
pub layout_type: GridLayoutRevision,
|
||||
pub layout_type: LayoutRevision,
|
||||
pub insert_filter: Option<CreateGridFilterParams>,
|
||||
pub delete_filter: Option<DeleteFilterParams>,
|
||||
pub insert_group: Option<CreateGridGroupParams>,
|
||||
|
Loading…
Reference in New Issue
Block a user