2022-03-04 14:09:16 +00:00
|
|
|
use crate::manager::GridManager;
|
2022-04-05 06:25:07 +00:00
|
|
|
use crate::services::field::{
|
2022-04-05 13:25:59 +00:00
|
|
|
default_type_option_builder_from_type, type_option_builder_from_json_str, MultiSelectTypeOption, SelectOption,
|
|
|
|
SelectOptionChangesetParams, SelectOptionChangesetPayload, SelectOptionContext, SingleSelectTypeOption,
|
2022-04-05 06:25:07 +00:00
|
|
|
};
|
2022-03-31 14:51:46 +00:00
|
|
|
use crate::services::grid_editor::ClientGridEditor;
|
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
2022-03-27 01:35:10 +00:00
|
|
|
use flowy_grid_data_model::entities::*;
|
2022-03-05 02:59:44 +00:00
|
|
|
use lib_dispatch::prelude::{data_result, AppData, Data, DataResult};
|
2022-03-02 14:43:04 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-03-06 03:28:24 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
2022-03-06 13:22:42 +00:00
|
|
|
pub(crate) async fn get_grid_data_handler(
|
2022-03-02 14:43:04 +00:00
|
|
|
data: Data<GridId>,
|
2022-03-05 02:59:44 +00:00
|
|
|
manager: AppData<Arc<GridManager>>,
|
2022-03-02 14:43:04 +00:00
|
|
|
) -> DataResult<Grid, FlowyError> {
|
2022-03-05 02:59:44 +00:00
|
|
|
let grid_id: GridId = data.into_inner();
|
|
|
|
let editor = manager.open_grid(grid_id).await?;
|
2022-03-15 03:07:18 +00:00
|
|
|
let grid = editor.grid_data().await?;
|
2022-03-05 02:59:44 +00:00
|
|
|
data_result(grid)
|
2022-03-03 14:17:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 03:28:24 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
2022-03-17 09:25:43 +00:00
|
|
|
pub(crate) async fn get_grid_blocks_handler(
|
|
|
|
data: Data<QueryGridBlocksPayload>,
|
2022-03-05 02:59:44 +00:00
|
|
|
manager: AppData<Arc<GridManager>>,
|
2022-03-17 09:25:43 +00:00
|
|
|
) -> DataResult<RepeatedGridBlock, FlowyError> {
|
2022-03-18 09:14:46 +00:00
|
|
|
let params: QueryGridBlocksParams = data.into_inner().try_into()?;
|
|
|
|
let editor = manager.get_grid_editor(¶ms.grid_id)?;
|
|
|
|
let block_ids = params
|
|
|
|
.block_orders
|
|
|
|
.into_iter()
|
|
|
|
.map(|block| block.block_id)
|
|
|
|
.collect::<Vec<String>>();
|
|
|
|
let repeated_grid_block = editor.get_blocks(Some(block_ids)).await?;
|
2022-03-17 09:25:43 +00:00
|
|
|
data_result(repeated_grid_block)
|
2022-03-03 14:17:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 14:10:31 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
|
|
|
pub(crate) async fn get_fields_handler(
|
|
|
|
data: Data<QueryFieldPayload>,
|
|
|
|
manager: AppData<Arc<GridManager>>,
|
|
|
|
) -> DataResult<RepeatedField, FlowyError> {
|
|
|
|
let params: QueryFieldParams = data.into_inner().try_into()?;
|
|
|
|
let editor = manager.get_grid_editor(¶ms.grid_id)?;
|
2022-04-05 06:25:07 +00:00
|
|
|
let field_orders = params.field_orders.items;
|
|
|
|
let field_metas = editor.get_field_metas(Some(field_orders)).await?;
|
2022-03-23 14:10:31 +00:00
|
|
|
let repeated_field: RepeatedField = field_metas.into_iter().map(Field::from).collect::<Vec<_>>().into();
|
|
|
|
data_result(repeated_field)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
|
|
|
pub(crate) async fn update_field_handler(
|
2022-03-27 01:35:10 +00:00
|
|
|
data: Data<FieldChangesetPayload>,
|
2022-03-23 14:10:31 +00:00
|
|
|
manager: AppData<Arc<GridManager>>,
|
|
|
|
) -> Result<(), FlowyError> {
|
2022-03-27 01:35:10 +00:00
|
|
|
let changeset: FieldChangesetParams = data.into_inner().try_into()?;
|
2022-03-23 14:10:31 +00:00
|
|
|
let editor = manager.get_grid_editor(&changeset.grid_id)?;
|
|
|
|
let _ = editor.update_field(changeset).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
|
|
|
pub(crate) async fn create_field_handler(
|
|
|
|
data: Data<CreateFieldPayload>,
|
|
|
|
manager: AppData<Arc<GridManager>>,
|
|
|
|
) -> Result<(), FlowyError> {
|
|
|
|
let params: CreateFieldParams = data.into_inner().try_into()?;
|
|
|
|
let editor = manager.get_grid_editor(¶ms.grid_id)?;
|
|
|
|
let _ = editor.create_field(params).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-03-24 09:09:05 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
2022-03-27 01:35:10 +00:00
|
|
|
pub(crate) async fn delete_field_handler(
|
2022-03-27 03:14:21 +00:00
|
|
|
data: Data<FieldIdentifierPayload>,
|
2022-03-27 01:35:10 +00:00
|
|
|
manager: AppData<Arc<GridManager>>,
|
|
|
|
) -> Result<(), FlowyError> {
|
2022-03-27 03:14:21 +00:00
|
|
|
let params: FieldIdentifierParams = data.into_inner().try_into()?;
|
2022-03-27 01:35:10 +00:00
|
|
|
let editor = manager.get_grid_editor(¶ms.grid_id)?;
|
2022-03-27 03:14:21 +00:00
|
|
|
let _ = editor.delete_field(¶ms.field_id).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-04-01 01:31:10 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
|
|
|
pub(crate) async fn switch_to_field_handler(
|
|
|
|
data: Data<EditFieldPayload>,
|
|
|
|
manager: AppData<Arc<GridManager>>,
|
|
|
|
) -> DataResult<EditFieldContext, FlowyError> {
|
|
|
|
let params: EditFieldParams = data.into_inner().try_into()?;
|
|
|
|
let editor = manager.get_grid_editor(¶ms.grid_id)?;
|
2022-04-01 14:46:01 +00:00
|
|
|
editor
|
|
|
|
.switch_to_field_type(¶ms.field_id, ¶ms.field_type)
|
|
|
|
.await?;
|
|
|
|
|
2022-04-05 13:25:59 +00:00
|
|
|
let field_meta = editor
|
|
|
|
.get_field_metas(Some(vec![params.field_id.as_str()]))
|
|
|
|
.await?
|
|
|
|
.pop();
|
2022-04-01 14:46:01 +00:00
|
|
|
let edit_context = make_field_edit_context(
|
|
|
|
¶ms.grid_id,
|
|
|
|
Some(params.field_id),
|
|
|
|
params.field_type,
|
|
|
|
editor,
|
|
|
|
field_meta,
|
|
|
|
)
|
|
|
|
.await?;
|
2022-04-01 01:31:10 +00:00
|
|
|
data_result(edit_context)
|
|
|
|
}
|
|
|
|
|
2022-03-27 03:14:21 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
|
|
|
pub(crate) async fn duplicate_field_handler(
|
|
|
|
data: Data<FieldIdentifierPayload>,
|
|
|
|
manager: AppData<Arc<GridManager>>,
|
|
|
|
) -> Result<(), FlowyError> {
|
|
|
|
let params: FieldIdentifierParams = data.into_inner().try_into()?;
|
|
|
|
let editor = manager.get_grid_editor(¶ms.grid_id)?;
|
|
|
|
let _ = editor.duplicate_field(¶ms.field_id).await?;
|
2022-03-27 01:35:10 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-03-29 14:58:38 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(data), err)]
|
|
|
|
pub(crate) async fn create_select_option_handler(
|
|
|
|
data: Data<CreateSelectOptionPayload>,
|
|
|
|
) -> DataResult<SelectOption, FlowyError> {
|
|
|
|
let params: CreateSelectOptionParams = data.into_inner().try_into()?;
|
|
|
|
data_result(SelectOption::new(¶ms.option_name))
|
|
|
|
}
|
|
|
|
|
2022-04-05 13:25:59 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
|
|
|
pub(crate) async fn get_select_option_handler(
|
|
|
|
data: Data<CellIdentifierPayload>,
|
|
|
|
manager: AppData<Arc<GridManager>>,
|
|
|
|
) -> DataResult<SelectOptionContext, FlowyError> {
|
2022-04-06 02:32:08 +00:00
|
|
|
let params: CellIdentifier = data.into_inner().try_into()?;
|
2022-04-05 13:25:59 +00:00
|
|
|
let editor = manager.get_grid_editor(¶ms.grid_id)?;
|
|
|
|
match editor
|
|
|
|
.get_field_metas(Some(vec![params.field_id.as_str()]))
|
|
|
|
.await?
|
|
|
|
.pop()
|
|
|
|
{
|
|
|
|
None => {
|
|
|
|
tracing::error!("Can't find the corresponding field with id: {}", params.field_id);
|
|
|
|
data_result(SelectOptionContext::default())
|
|
|
|
}
|
|
|
|
Some(field_meta) => {
|
|
|
|
let cell_meta = editor.get_cell_meta(¶ms.row_id, ¶ms.field_id).await?;
|
|
|
|
match field_meta.field_type {
|
|
|
|
FieldType::SingleSelect => {
|
|
|
|
let type_option = SingleSelectTypeOption::from(&field_meta);
|
|
|
|
let select_option_context = type_option.select_option_context(&cell_meta);
|
|
|
|
data_result(select_option_context)
|
|
|
|
}
|
|
|
|
FieldType::MultiSelect => {
|
|
|
|
let type_option = MultiSelectTypeOption::from(&field_meta);
|
|
|
|
let select_option_context = type_option.select_option_context(&cell_meta);
|
|
|
|
data_result(select_option_context)
|
|
|
|
}
|
|
|
|
ty => {
|
|
|
|
tracing::error!("Unsupported field type: {:?} for this handler", ty);
|
|
|
|
data_result(SelectOptionContext::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-27 01:35:10 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
2022-04-01 01:31:10 +00:00
|
|
|
pub(crate) async fn get_field_context_handler(
|
|
|
|
data: Data<GetEditFieldContextPayload>,
|
2022-03-24 09:09:05 +00:00
|
|
|
manager: AppData<Arc<GridManager>>,
|
2022-03-25 07:02:43 +00:00
|
|
|
) -> DataResult<EditFieldContext, FlowyError> {
|
2022-04-01 01:31:10 +00:00
|
|
|
let params = data.into_inner();
|
2022-03-25 07:02:43 +00:00
|
|
|
let editor = manager.get_grid_editor(¶ms.grid_id)?;
|
2022-04-01 14:46:01 +00:00
|
|
|
let edit_context =
|
|
|
|
make_field_edit_context(¶ms.grid_id, params.field_id, params.field_type, editor, None).await?;
|
|
|
|
|
|
|
|
data_result(edit_context)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn make_field_edit_context(
|
|
|
|
grid_id: &str,
|
|
|
|
field_id: Option<String>,
|
|
|
|
field_type: FieldType,
|
|
|
|
editor: Arc<ClientGridEditor>,
|
|
|
|
field_meta: Option<FieldMeta>,
|
|
|
|
) -> FlowyResult<EditFieldContext> {
|
|
|
|
let field_meta = field_meta.unwrap_or(get_or_create_field_meta(field_id, &field_type, editor).await?);
|
|
|
|
let s = field_meta
|
2022-04-01 14:49:26 +00:00
|
|
|
.get_type_option_str(None)
|
2022-04-01 14:46:01 +00:00
|
|
|
.unwrap_or_else(|| default_type_option_builder_from_type(&field_type).entry().json_str());
|
|
|
|
|
2022-04-01 08:38:51 +00:00
|
|
|
let builder = type_option_builder_from_json_str(&s, &field_meta.field_type);
|
|
|
|
let type_option_data = builder.entry().protobuf_bytes().to_vec();
|
2022-03-25 07:02:43 +00:00
|
|
|
let field: Field = field_meta.into();
|
2022-04-01 14:46:01 +00:00
|
|
|
Ok(EditFieldContext {
|
|
|
|
grid_id: grid_id.to_string(),
|
2022-03-25 07:02:43 +00:00
|
|
|
grid_field: field,
|
|
|
|
type_option_data,
|
2022-04-01 14:46:01 +00:00
|
|
|
})
|
2022-03-24 09:09:05 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 14:51:46 +00:00
|
|
|
async fn get_or_create_field_meta(
|
2022-04-01 14:46:01 +00:00
|
|
|
field_id: Option<String>,
|
|
|
|
field_type: &FieldType,
|
2022-03-31 14:51:46 +00:00
|
|
|
editor: Arc<ClientGridEditor>,
|
|
|
|
) -> FlowyResult<FieldMeta> {
|
2022-04-05 13:25:59 +00:00
|
|
|
match field_id {
|
|
|
|
None => editor.create_next_field_meta(field_type).await,
|
|
|
|
Some(field_id) => match editor.get_field_metas(Some(vec![field_id.as_str()])).await?.pop() {
|
|
|
|
None => editor.create_next_field_meta(field_type).await,
|
|
|
|
Some(field_meta) => Ok(field_meta),
|
|
|
|
},
|
2022-03-31 14:51:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-18 09:14:46 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
|
|
|
pub(crate) async fn get_row_handler(
|
|
|
|
data: Data<QueryRowPayload>,
|
|
|
|
manager: AppData<Arc<GridManager>>,
|
|
|
|
) -> DataResult<Row, FlowyError> {
|
|
|
|
let params: QueryRowParams = data.into_inner().try_into()?;
|
|
|
|
let editor = manager.get_grid_editor(¶ms.grid_id)?;
|
2022-04-05 13:25:59 +00:00
|
|
|
match editor.get_row(¶ms.row_id).await? {
|
2022-03-18 09:14:46 +00:00
|
|
|
None => Err(FlowyError::record_not_found().context("Can not find the row")),
|
|
|
|
Some(row) => data_result(row),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-06 03:28:24 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip(data, manager), err)]
|
2022-03-03 14:17:07 +00:00
|
|
|
pub(crate) async fn create_row_handler(
|
2022-03-16 08:10:35 +00:00
|
|
|
data: Data<CreateRowPayload>,
|
2022-03-05 02:59:44 +00:00
|
|
|
manager: AppData<Arc<GridManager>>,
|
2022-03-16 13:19:51 +00:00
|
|
|
) -> Result<(), FlowyError> {
|
2022-03-18 09:14:46 +00:00
|
|
|
let params: CreateRowParams = data.into_inner().try_into()?;
|
|
|
|
let editor = manager.get_grid_editor(params.grid_id.as_ref())?;
|
|
|
|
let _ = editor.create_row(params.start_row_id).await?;
|
2022-03-16 13:19:51 +00:00
|
|
|
Ok(())
|
2022-03-02 14:43:04 +00:00
|
|
|
}
|
2022-03-09 08:11:24 +00:00
|
|
|
|
2022-03-13 15:16:52 +00:00
|
|
|
#[tracing::instrument(level = "debug", skip_all, err)]
|
2022-03-09 08:11:24 +00:00
|
|
|
pub(crate) async fn update_cell_handler(
|
2022-03-16 08:10:35 +00:00
|
|
|
data: Data<CellMetaChangeset>,
|
|
|
|
manager: AppData<Arc<GridManager>>,
|
2022-03-09 08:11:24 +00:00
|
|
|
) -> Result<(), FlowyError> {
|
2022-03-16 08:10:35 +00:00
|
|
|
let changeset: CellMetaChangeset = data.into_inner();
|
|
|
|
let editor = manager.get_grid_editor(&changeset.grid_id)?;
|
|
|
|
let _ = editor.update_cell(changeset).await?;
|
2022-03-09 08:11:24 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-04-05 06:25:07 +00:00
|
|
|
|
|
|
|
#[tracing::instrument(level = "debug", skip_all, err)]
|
2022-04-05 13:25:59 +00:00
|
|
|
pub(crate) async fn apply_select_option_changeset_handler(
|
2022-04-05 06:25:07 +00:00
|
|
|
data: Data<SelectOptionChangesetPayload>,
|
|
|
|
manager: AppData<Arc<GridManager>>,
|
|
|
|
) -> Result<(), FlowyError> {
|
|
|
|
let params: SelectOptionChangesetParams = data.into_inner().try_into()?;
|
|
|
|
let editor = manager.get_grid_editor(¶ms.grid_id)?;
|
2022-04-05 13:25:59 +00:00
|
|
|
let changeset: CellMetaChangeset = params.into();
|
|
|
|
let _ = editor.update_cell(changeset).await?;
|
2022-04-05 06:25:07 +00:00
|
|
|
Ok(())
|
|
|
|
}
|