From 8594da65c4be2ac57a8a49c0cfe2216f1f23efbd Mon Sep 17 00:00:00 2001 From: appflowy Date: Sun, 19 Jun 2022 21:10:07 +0800 Subject: [PATCH] chore: add get grid setting handler --- .../presentation/plugins/board/board.dart | 2 +- .../rust-lib/flowy-grid/src/event_handler.rs | 13 ++- frontend/rust-lib/flowy-grid/src/event_map.rs | 4 + .../flowy-grid/src/services/grid_editor.rs | 6 +- .../{grid_info.rs => grid_setting.rs} | 59 +++++++++----- .../flowy-grid-data-model/src/entities/mod.rs | 4 +- .../src/parser/grid_info_parser.rs | 20 ++--- .../src/revision/grid_info_rev.rs | 81 ++++++++++++++----- 8 files changed, 134 insertions(+), 55 deletions(-) rename shared-lib/flowy-grid-data-model/src/entities/{grid_info.rs => grid_setting.rs} (57%) diff --git a/frontend/app_flowy/lib/workspace/presentation/plugins/board/board.dart b/frontend/app_flowy/lib/workspace/presentation/plugins/board/board.dart index 041f1c4713..fb55fc70c1 100644 --- a/frontend/app_flowy/lib/workspace/presentation/plugins/board/board.dart +++ b/frontend/app_flowy/lib/workspace/presentation/plugins/board/board.dart @@ -28,7 +28,7 @@ class BoardPluginBuilder implements PluginBuilder { class BoardPluginConfig implements PluginConfig { @override - bool get creatable => false; + bool get creatable => true; } class BoardPlugin extends Plugin { diff --git a/frontend/rust-lib/flowy-grid/src/event_handler.rs b/frontend/rust-lib/flowy-grid/src/event_handler.rs index c27e7c2175..beac8ddc0c 100644 --- a/frontend/rust-lib/flowy-grid/src/event_handler.rs +++ b/frontend/rust-lib/flowy-grid/src/event_handler.rs @@ -15,10 +15,21 @@ pub(crate) async fn get_grid_data_handler( ) -> DataResult { let grid_id: GridId = data.into_inner(); let editor = manager.open_grid(grid_id).await?; - let grid = editor.grid_data().await?; + let grid = editor.get_grid_data().await?; data_result(grid) } +#[tracing::instrument(level = "trace", skip(data, manager), err)] +pub(crate) async fn get_grid_setting_handler( + data: Data, + manager: AppData>, +) -> DataResult { + let grid_id: GridId = data.into_inner(); + let editor = manager.open_grid(grid_id).await?; + let grid_setting = editor.get_grid_setting().await?; + data_result(grid_setting) +} + #[tracing::instrument(level = "debug", skip(data, manager), err)] pub(crate) async fn get_grid_blocks_handler( data: Data, diff --git a/frontend/rust-lib/flowy-grid/src/event_map.rs b/frontend/rust-lib/flowy-grid/src/event_map.rs index 48e5fd9b6c..8e30ba6051 100644 --- a/frontend/rust-lib/flowy-grid/src/event_map.rs +++ b/frontend/rust-lib/flowy-grid/src/event_map.rs @@ -10,6 +10,7 @@ pub fn create(grid_manager: Arc) -> Module { module = module .event(GridEvent::GetGridData, get_grid_data_handler) .event(GridEvent::GetGridBlocks, get_grid_blocks_handler) + .event(GridEvent::GetGridSetting, get_grid_setting_handler) // Field .event(GridEvent::GetFields, get_fields_handler) .event(GridEvent::UpdateField, update_field_handler) @@ -49,6 +50,9 @@ pub enum GridEvent { #[event(input = "QueryGridBlocksPayload", output = "RepeatedGridBlock")] GetGridBlocks = 1, + #[event(input = "GridId", output = "GridSetting")] + GetGridSetting = 2, + #[event(input = "QueryFieldPayload", output = "RepeatedField")] GetFields = 10, diff --git a/frontend/rust-lib/flowy-grid/src/services/grid_editor.rs b/frontend/rust-lib/flowy-grid/src/services/grid_editor.rs index 88960b0202..f78c185f26 100644 --- a/frontend/rust-lib/flowy-grid/src/services/grid_editor.rs +++ b/frontend/rust-lib/flowy-grid/src/services/grid_editor.rs @@ -412,7 +412,7 @@ impl GridMetaEditor { Ok(()) } - pub async fn grid_data(&self) -> FlowyResult { + pub async fn get_grid_data(&self) -> FlowyResult { let pad_read_guard = self.grid_pad.read().await; let field_orders = pad_read_guard.get_field_orders(); let mut block_orders = vec![]; @@ -432,6 +432,10 @@ impl GridMetaEditor { }) } + pub async fn get_grid_setting(&self) -> FlowyResult { + todo!() + } + pub async fn grid_block_snapshots(&self, block_ids: Option>) -> FlowyResult> { let block_ids = match block_ids { None => self diff --git a/shared-lib/flowy-grid-data-model/src/entities/grid_info.rs b/shared-lib/flowy-grid-data-model/src/entities/grid_setting.rs similarity index 57% rename from shared-lib/flowy-grid-data-model/src/entities/grid_info.rs rename to shared-lib/flowy-grid-data-model/src/entities/grid_setting.rs index 5c532bfeb2..64b094aabf 100644 --- a/shared-lib/flowy-grid-data-model/src/entities/grid_info.rs +++ b/shared-lib/flowy-grid-data-model/src/entities/grid_setting.rs @@ -1,28 +1,42 @@ use crate::parser::{NotEmptyStr, ViewFilterParser, ViewGroupParser, ViewSortParser}; -use flowy_derive::ProtoBuf; +use flowy_derive::{ProtoBuf, ProtoBuf_Enum}; use flowy_error_code::ErrorCode; +use std::collections::HashMap; use std::convert::TryInto; #[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)] -pub struct ViewExtData { +pub struct GridSetting { #[pb(index = 1)] - pub filter: ViewFilter, + pub filter: HashMap, #[pb(index = 2)] - pub group: ViewGroup, + pub group: HashMap, #[pb(index = 3)] - pub sort: ViewSort, + pub sort: HashMap, +} + +#[derive(Debug, Clone, PartialEq, Eq, ProtoBuf_Enum)] +#[repr(u8)] +pub enum GridLayoutType { + Table = 0, + Board = 1, +} + +impl std::default::Default for GridLayoutType { + fn default() -> Self { + GridLayoutType::Table + } } #[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)] -pub struct ViewFilter { +pub struct GridFilter { #[pb(index = 1, one_of)] pub field_id: Option, } #[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)] -pub struct ViewGroup { +pub struct GridGroup { #[pb(index = 1, one_of)] pub group_field_id: Option, @@ -31,37 +45,41 @@ pub struct ViewGroup { } #[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)] -pub struct ViewSort { +pub struct GridSort { #[pb(index = 1, one_of)] pub field_id: Option, } #[derive(Default, ProtoBuf)] -pub struct GridInfoChangesetPayload { +pub struct GridSettingChangesetPayload { #[pb(index = 1)] pub grid_id: String, - #[pb(index = 2, one_of)] - pub filter: Option, + #[pb(index = 2)] + pub layout_type: GridLayoutType, #[pb(index = 3, one_of)] - pub group: Option, + pub filter: Option, #[pb(index = 4, one_of)] - pub sort: Option, + pub group: Option, + + #[pb(index = 5, one_of)] + pub sort: Option, } -pub struct GridInfoChangesetParams { +pub struct GridSettingChangesetParams { pub view_id: String, - pub filter: Option, - pub group: Option, - pub sort: Option, + pub layout_type: GridLayoutType, + pub filter: Option, + pub group: Option, + pub sort: Option, } -impl TryInto for GridInfoChangesetPayload { +impl TryInto for GridSettingChangesetPayload { type Error = ErrorCode; - fn try_into(self) -> Result { + fn try_into(self) -> Result { let view_id = NotEmptyStr::parse(self.grid_id) .map_err(|_| ErrorCode::FieldIdIsEmpty)? .0; @@ -81,8 +99,9 @@ impl TryInto for GridInfoChangesetPayload { Some(sort) => Some(ViewSortParser::parse(sort)?), }; - Ok(GridInfoChangesetParams { + Ok(GridSettingChangesetParams { view_id, + layout_type: self.layout_type, filter, group, sort, diff --git a/shared-lib/flowy-grid-data-model/src/entities/mod.rs b/shared-lib/flowy-grid-data-model/src/entities/mod.rs index 6021d90167..535e2142d8 100644 --- a/shared-lib/flowy-grid-data-model/src/entities/mod.rs +++ b/shared-lib/flowy-grid-data-model/src/entities/mod.rs @@ -1,7 +1,7 @@ mod field; mod grid; -mod grid_info; +mod grid_setting; pub use field::*; pub use grid::*; -pub use grid_info::*; +pub use grid_setting::*; diff --git a/shared-lib/flowy-grid-data-model/src/parser/grid_info_parser.rs b/shared-lib/flowy-grid-data-model/src/parser/grid_info_parser.rs index 799cfe520c..1d14582ac0 100644 --- a/shared-lib/flowy-grid-data-model/src/parser/grid_info_parser.rs +++ b/shared-lib/flowy-grid-data-model/src/parser/grid_info_parser.rs @@ -1,24 +1,24 @@ -use crate::entities::{ViewFilter, ViewGroup, ViewSort}; +use crate::entities::{GridFilter, GridGroup, GridSort}; use crate::parser::NotEmptyStr; use flowy_error_code::ErrorCode; -pub struct ViewFilterParser(pub ViewFilter); +pub struct ViewFilterParser(pub GridFilter); impl ViewFilterParser { - pub fn parse(value: ViewFilter) -> Result { + pub fn parse(value: GridFilter) -> Result { let field_id = match value.field_id { None => None, Some(field_id) => Some(NotEmptyStr::parse(field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?.0), }; - Ok(ViewFilter { field_id }) + Ok(GridFilter { field_id }) } } -pub struct ViewGroupParser(pub ViewGroup); +pub struct ViewGroupParser(pub GridGroup); impl ViewGroupParser { - pub fn parse(value: ViewGroup) -> Result { + pub fn parse(value: GridGroup) -> Result { let group_field_id = match value.group_field_id { None => None, Some(group_field_id) => Some( @@ -37,22 +37,22 @@ impl ViewGroupParser { ), }; - Ok(ViewGroup { + Ok(GridGroup { group_field_id, sub_group_field_id, }) } } -pub struct ViewSortParser(pub ViewSort); +pub struct ViewSortParser(pub GridSort); impl ViewSortParser { - pub fn parse(value: ViewSort) -> Result { + pub fn parse(value: GridSort) -> Result { let field_id = match value.field_id { None => None, Some(field_id) => Some(NotEmptyStr::parse(field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?.0), }; - Ok(ViewSort { field_id }) + Ok(GridSort { field_id }) } } diff --git a/shared-lib/flowy-grid-data-model/src/revision/grid_info_rev.rs b/shared-lib/flowy-grid-data-model/src/revision/grid_info_rev.rs index bea1272e36..24a5721cc4 100644 --- a/shared-lib/flowy-grid-data-model/src/revision/grid_info_rev.rs +++ b/shared-lib/flowy-grid-data-model/src/revision/grid_info_rev.rs @@ -1,30 +1,38 @@ -use crate::entities::{ViewFilter, ViewGroup, ViewSort}; +use crate::entities::{GridFilter, GridGroup, GridLayoutType, GridSetting, GridSort}; +use indexmap::IndexMap; use serde::{Deserialize, Serialize}; use serde_repr::*; +use std::collections::HashMap; #[derive(Debug, Clone, Serialize, Deserialize, Default)] pub struct GridInfoRevision { - pub filter: GridFilterRevision, - pub group: GridGroupRevision, - pub sort: GridSortRevision, - pub layout: GridLayoutRevision, + #[serde(with = "indexmap::serde_seq")] + pub filter: IndexMap, + + #[serde(with = "indexmap::serde_seq")] + pub group: IndexMap, + + #[serde(with = "indexmap::serde_seq")] + pub sort: IndexMap, } -#[derive(Debug, Clone, Serialize, Deserialize, Default)] -pub struct GridLayoutRevision { - pub ty: GridLayoutType, -} - -#[derive(Debug, Clone, Serialize_repr, Deserialize_repr)] +#[derive(Debug, PartialEq, Eq, Hash, Clone, Serialize_repr, Deserialize_repr)] #[repr(u8)] -pub enum GridLayoutType { +pub enum GridLayoutRevision { Table = 0, Board = 1, } -impl std::default::Default for GridLayoutType { +impl ToString for GridLayoutRevision { + fn to_string(&self) -> String { + let layout_rev = self.clone() as u8; + layout_rev.to_string() + } +} + +impl std::default::Default for GridLayoutRevision { fn default() -> Self { - GridLayoutType::Table + GridLayoutRevision::Table } } @@ -44,23 +52,56 @@ pub struct GridSortRevision { field_id: Option, } -impl std::convert::From for ViewFilter { +impl std::convert::From for GridFilter { fn from(rev: GridFilterRevision) -> Self { - ViewFilter { field_id: rev.field_id } + GridFilter { field_id: rev.field_id } } } -impl std::convert::From for ViewGroup { +impl std::convert::From for GridGroup { fn from(rev: GridGroupRevision) -> Self { - ViewGroup { + GridGroup { group_field_id: rev.group_field_id, sub_group_field_id: rev.sub_group_field_id, } } } -impl std::convert::From for ViewSort { +impl std::convert::From for GridSort { fn from(rev: GridSortRevision) -> Self { - ViewSort { field_id: rev.field_id } + GridSort { field_id: rev.field_id } + } +} + +impl std::convert::From for GridLayoutType { + fn from(rev: GridLayoutRevision) -> Self { + match rev { + GridLayoutRevision::Table => GridLayoutType::Table, + GridLayoutRevision::Board => GridLayoutType::Board, + } + } +} + +impl std::convert::From for GridSetting { + fn from(rev: GridInfoRevision) -> Self { + let filter: HashMap = rev + .filter + .into_iter() + .map(|(layout_rev, filter_rev)| (layout_rev.to_string(), filter_rev.into())) + .collect(); + + let group: HashMap = rev + .group + .into_iter() + .map(|(layout_rev, group_rev)| (layout_rev.to_string(), group_rev.into())) + .collect(); + + let sort: HashMap = rev + .sort + .into_iter() + .map(|(layout_rev, sort_rev)| (layout_rev.to_string(), sort_rev.into())) + .collect(); + + GridSetting { filter, group, sort } } }