AppFlowy/frontend/rust-lib/flowy-grid/src/event_handler.rs

408 lines
16 KiB
Rust
Raw Normal View History

use crate::entities::*;
2022-03-04 14:09:16 +00:00
use crate::manager::GridManager;
use crate::services::cell::AnyCellData;
use crate::services::field::{
2022-07-13 03:09:13 +00:00
default_type_option_builder_from_type, select_option_operation, type_option_builder_from_json_str,
2022-07-17 05:38:53 +00:00
DateChangesetParams, DateChangesetPayloadPB, SelectOptionPB, SelectOptionCellChangeset,
SelectOptionCellChangesetParams, SelectOptionCellChangesetPayloadPB, SelectOptionCellDataPB, SelectOptionChangeset,
SelectOptionChangesetPayloadPB,
};
2022-07-12 07:49:14 +00:00
use crate::services::row::make_row_from_row_rev;
2022-04-07 00:33:10 +00:00
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
use flowy_grid_data_model::revision::FieldRevision;
use flowy_sync::entities::grid::{FieldChangesetParams, GridSettingChangesetParams};
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-05-26 09:28:44 +00:00
#[tracing::instrument(level = "trace", skip(data, manager), err)]
2022-06-24 10:13:40 +00:00
pub(crate) async fn get_grid_handler(
2022-07-17 05:38:53 +00:00
data: Data<GridIdPB>,
2022-03-05 02:59:44 +00:00
manager: AppData<Arc<GridManager>>,
2022-07-17 05:38:53 +00:00
) -> DataResult<GridPB, FlowyError> {
let grid_id: GridIdPB = data.into_inner();
2022-03-05 02:59:44 +00:00
let editor = manager.open_grid(grid_id).await?;
2022-06-19 13:10:07 +00:00
let grid = editor.get_grid_data().await?;
2022-03-05 02:59:44 +00:00
data_result(grid)
2022-03-03 14:17:07 +00:00
}
2022-06-19 13:10:07 +00:00
#[tracing::instrument(level = "trace", skip(data, manager), err)]
pub(crate) async fn get_grid_setting_handler(
2022-07-17 05:38:53 +00:00
data: Data<GridIdPB>,
2022-06-19 13:10:07 +00:00
manager: AppData<Arc<GridManager>>,
2022-07-17 05:38:53 +00:00
) -> DataResult<GridSettingPB, FlowyError> {
let grid_id: GridIdPB = data.into_inner();
2022-06-19 13:10:07 +00:00
let editor = manager.open_grid(grid_id).await?;
let grid_setting = editor.get_grid_setting().await?;
2022-06-22 09:11:56 +00:00
data_result(grid_setting)
2022-06-19 13:10:07 +00:00
}
2022-06-20 01:37:52 +00:00
#[tracing::instrument(level = "trace", skip(data, manager), err)]
pub(crate) async fn update_grid_setting_handler(
2022-07-17 05:38:53 +00:00
data: Data<GridSettingChangesetPayloadPB>,
2022-06-20 01:37:52 +00:00
manager: AppData<Arc<GridManager>>,
) -> Result<(), FlowyError> {
let params: GridSettingChangesetParams = data.into_inner().try_into()?;
let editor = manager.open_grid(&params.grid_id).await?;
let _ = editor.update_grid_setting(params).await?;
Ok(())
}
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(
2022-07-17 05:38:53 +00:00
data: Data<QueryGridBlocksPayloadPB>,
2022-03-05 02:59:44 +00:00
manager: AppData<Arc<GridManager>>,
2022-07-17 05:38:53 +00:00
) -> DataResult<RepeatedGridBlockPB, FlowyError> {
2022-03-18 09:14:46 +00:00
let params: QueryGridBlocksParams = data.into_inner().try_into()?;
let editor = manager.get_grid_editor(&params.grid_id)?;
2022-06-24 07:23:39 +00:00
let repeated_grid_block = editor.get_blocks(Some(params.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-05-26 09:28:44 +00:00
#[tracing::instrument(level = "trace", skip(data, manager), err)]
pub(crate) async fn get_fields_handler(
2022-07-17 05:38:53 +00:00
data: Data<QueryFieldPayloadPB>,
manager: AppData<Arc<GridManager>>,
2022-07-17 05:38:53 +00:00
) -> DataResult<RepeatedFieldPB, FlowyError> {
let params: QueryFieldParams = data.into_inner().try_into()?;
let editor = manager.get_grid_editor(&params.grid_id)?;
let field_orders = params
.field_orders
.items
.into_iter()
.map(|field_order| field_order.field_id)
.collect();
let field_revs = editor.get_field_revs(Some(field_orders)).await?;
2022-07-17 05:38:53 +00:00
let repeated_field: RepeatedFieldPB = field_revs.into_iter().map(FieldPB::from).collect::<Vec<_>>().into();
data_result(repeated_field)
}
2022-05-26 09:28:44 +00:00
#[tracing::instrument(level = "trace", skip(data, manager), err)]
pub(crate) async fn update_field_handler(
2022-07-17 05:38:53 +00:00
data: Data<FieldChangesetPayloadPB>,
manager: AppData<Arc<GridManager>>,
) -> Result<(), FlowyError> {
2022-03-27 01:35:10 +00:00
let changeset: FieldChangesetParams = data.into_inner().try_into()?;
let editor = manager.get_grid_editor(&changeset.grid_id)?;
let _ = editor.update_field(changeset).await?;
Ok(())
}
2022-05-26 09:28:44 +00:00
#[tracing::instrument(level = "trace", skip(data, manager), err)]
pub(crate) async fn insert_field_handler(
2022-07-17 05:38:53 +00:00
data: Data<InsertFieldPayloadPB>,
manager: AppData<Arc<GridManager>>,
) -> Result<(), FlowyError> {
let params: InsertFieldParams = data.into_inner().try_into()?;
let editor = manager.get_grid_editor(&params.grid_id)?;
let _ = editor.insert_field(params).await?;
Ok(())
}
2022-05-26 09:28:44 +00:00
#[tracing::instrument(level = "trace", skip(data, manager), err)]
2022-05-10 01:33:34 +00:00
pub(crate) async fn update_field_type_option_handler(
2022-07-17 05:38:53 +00:00
data: Data<UpdateFieldTypeOptionPayloadPB>,
2022-05-10 01:33:34 +00:00
manager: AppData<Arc<GridManager>>,
) -> Result<(), FlowyError> {
let params: UpdateFieldTypeOptionParams = data.into_inner().try_into()?;
let editor = manager.get_grid_editor(&params.grid_id)?;
let _ = editor
.update_field_type_option(&params.grid_id, &params.field_id, params.type_option_data)
.await?;
Ok(())
}
2022-05-26 09:28:44 +00:00
#[tracing::instrument(level = "trace", skip(data, manager), err)]
2022-03-27 01:35:10 +00:00
pub(crate) async fn delete_field_handler(
2022-07-17 05:38:53 +00:00
data: Data<FieldIdentifierPayloadPB>,
2022-03-27 01:35:10 +00:00
manager: AppData<Arc<GridManager>>,
) -> Result<(), FlowyError> {
2022-04-07 00:33:10 +00:00
let params: FieldIdentifier = data.into_inner().try_into()?;
2022-03-27 01:35:10 +00:00
let editor = manager.get_grid_editor(&params.grid_id)?;
2022-03-27 03:14:21 +00:00
let _ = editor.delete_field(&params.field_id).await?;
Ok(())
}
2022-05-26 09:28:44 +00:00
#[tracing::instrument(level = "trace", skip(data, manager), err)]
pub(crate) async fn switch_to_field_handler(
2022-07-17 05:38:53 +00:00
data: Data<EditFieldPayloadPB>,
manager: AppData<Arc<GridManager>>,
2022-07-17 05:38:53 +00:00
) -> DataResult<FieldTypeOptionDataPB, FlowyError> {
let params: EditFieldParams = data.into_inner().try_into()?;
let editor = manager.get_grid_editor(&params.grid_id)?;
2022-05-19 02:41:00 +00:00
editor
.switch_to_field_type(&params.field_id, &params.field_type)
.await?;
// Get the FieldMeta with field_id, if it doesn't exist, we create the default FieldMeta from the FieldType.
let field_rev = editor
.get_field_rev(&params.field_id)
2022-05-19 02:41:00 +00:00
.await
2022-07-01 02:36:07 +00:00
.unwrap_or(Arc::new(editor.next_field_rev(&params.field_type).await?));
2022-05-19 02:41:00 +00:00
let type_option_data = get_type_option_data(&field_rev, &params.field_type).await?;
2022-07-17 05:38:53 +00:00
let data = FieldTypeOptionDataPB {
2022-05-19 02:41:00 +00:00
grid_id: params.grid_id,
field: field_rev.into(),
2022-05-19 02:41:00 +00:00
type_option_data,
};
data_result(data)
}
2022-05-26 09:28:44 +00:00
#[tracing::instrument(level = "trace", skip(data, manager), err)]
2022-03-27 03:14:21 +00:00
pub(crate) async fn duplicate_field_handler(
2022-07-17 05:38:53 +00:00
data: Data<FieldIdentifierPayloadPB>,
2022-03-27 03:14:21 +00:00
manager: AppData<Arc<GridManager>>,
) -> Result<(), FlowyError> {
2022-04-07 00:33:10 +00:00
let params: FieldIdentifier = data.into_inner().try_into()?;
2022-03-27 03:14:21 +00:00
let editor = manager.get_grid_editor(&params.grid_id)?;
let _ = editor.duplicate_field(&params.field_id).await?;
2022-03-27 01:35:10 +00:00
Ok(())
}
2022-05-19 02:41:00 +00:00
/// Return the FieldTypeOptionData if the Field exists otherwise return record not found error.
2022-05-26 09:28:44 +00:00
#[tracing::instrument(level = "trace", skip(data, manager), err)]
pub(crate) async fn get_field_type_option_data_handler(
2022-07-17 05:38:53 +00:00
data: Data<EditFieldPayloadPB>,
manager: AppData<Arc<GridManager>>,
2022-07-17 05:38:53 +00:00
) -> DataResult<FieldTypeOptionDataPB, FlowyError> {
2022-05-09 06:59:26 +00:00
let params: EditFieldParams = data.into_inner().try_into()?;
let editor = manager.get_grid_editor(&params.grid_id)?;
match editor.get_field_rev(&params.field_id).await {
2022-05-19 02:41:00 +00:00
None => Err(FlowyError::record_not_found()),
Some(field_rev) => {
let field_type = field_rev.field_type_rev.into();
let type_option_data = get_type_option_data(&field_rev, &field_type).await?;
2022-07-17 05:38:53 +00:00
let data = FieldTypeOptionDataPB {
2022-05-19 02:41:00 +00:00
grid_id: params.grid_id,
field: field_rev.into(),
2022-05-19 02:41:00 +00:00
type_option_data,
};
data_result(data)
}
}
}
/// Create FieldMeta and save it. Return the FieldTypeOptionData.
2022-05-26 09:28:44 +00:00
#[tracing::instrument(level = "trace", skip(data, manager), err)]
2022-05-19 02:41:00 +00:00
pub(crate) async fn create_field_type_option_data_handler(
2022-07-17 05:38:53 +00:00
data: Data<EditFieldPayloadPB>,
2022-05-19 02:41:00 +00:00
manager: AppData<Arc<GridManager>>,
2022-07-17 05:38:53 +00:00
) -> DataResult<FieldTypeOptionDataPB, FlowyError> {
2022-05-19 02:41:00 +00:00
let params: CreateFieldParams = data.into_inner().try_into()?;
let editor = manager.get_grid_editor(&params.grid_id)?;
let field_rev = editor.create_next_field_rev(&params.field_type).await?;
let field_type: FieldType = field_rev.field_type_rev.into();
let type_option_data = get_type_option_data(&field_rev, &field_type).await?;
2022-07-17 05:38:53 +00:00
data_result(FieldTypeOptionDataPB {
grid_id: params.grid_id,
field: field_rev.into(),
type_option_data,
})
}
2022-05-26 09:28:44 +00:00
#[tracing::instrument(level = "trace", skip(data, manager), err)]
2022-04-13 06:24:54 +00:00
pub(crate) async fn move_item_handler(
2022-07-17 05:38:53 +00:00
data: Data<MoveItemPayloadPB>,
2022-04-13 06:24:54 +00:00
manager: AppData<Arc<GridManager>>,
) -> Result<(), FlowyError> {
let params: MoveItemParams = data.into_inner().try_into()?;
let editor = manager.get_grid_editor(&params.grid_id)?;
let _ = editor.move_item(params).await?;
Ok(())
}
/// The FieldMeta contains multiple data, each of them belongs to a specific FieldType.
async fn get_type_option_data(field_rev: &FieldRevision, field_type: &FieldType) -> FlowyResult<Vec<u8>> {
let s = field_rev
2022-05-11 03:34:13 +00:00
.get_type_option_str(field_type)
.unwrap_or_else(|| default_type_option_builder_from_type(field_type).entry().json_str());
let field_type: FieldType = field_rev.field_type_rev.into();
let builder = type_option_builder_from_json_str(&s, &field_type);
let type_option_data = builder.entry().protobuf_bytes().to_vec();
Ok(type_option_data)
}
2022-03-18 09:14:46 +00:00
#[tracing::instrument(level = "debug", skip(data, manager), err)]
pub(crate) async fn get_row_handler(
2022-07-17 05:38:53 +00:00
data: Data<GridRowIdPayloadPB>,
2022-03-18 09:14:46 +00:00
manager: AppData<Arc<GridManager>>,
2022-07-17 05:38:53 +00:00
) -> DataResult<OptionalRowPB, FlowyError> {
let params: GridRowIdPB = data.into_inner().try_into()?;
2022-03-18 09:14:46 +00:00
let editor = manager.get_grid_editor(&params.grid_id)?;
2022-07-12 07:49:14 +00:00
let row = editor
.get_row_rev(&params.row_id)
.await?
.and_then(make_row_from_row_rev);
2022-07-17 05:38:53 +00:00
data_result(OptionalRowPB { row })
2022-03-18 09:14:46 +00:00
}
2022-03-06 03:28:24 +00:00
#[tracing::instrument(level = "debug", skip(data, manager), err)]
2022-04-09 14:07:48 +00:00
pub(crate) async fn delete_row_handler(
2022-07-17 05:38:53 +00:00
data: Data<GridRowIdPayloadPB>,
2022-04-09 14:07:48 +00:00
manager: AppData<Arc<GridManager>>,
2022-04-09 14:42:42 +00:00
) -> Result<(), FlowyError> {
2022-07-17 05:38:53 +00:00
let params: GridRowIdPB = data.into_inner().try_into()?;
2022-04-09 14:42:42 +00:00
let editor = manager.get_grid_editor(&params.grid_id)?;
2022-04-10 00:25:01 +00:00
let _ = editor.delete_row(&params.row_id).await?;
2022-04-09 14:42:42 +00:00
Ok(())
2022-04-09 14:07:48 +00:00
}
#[tracing::instrument(level = "debug", skip(data, manager), err)]
pub(crate) async fn duplicate_row_handler(
2022-07-17 05:38:53 +00:00
data: Data<GridRowIdPayloadPB>,
2022-04-09 14:07:48 +00:00
manager: AppData<Arc<GridManager>>,
2022-04-09 14:42:42 +00:00
) -> Result<(), FlowyError> {
2022-07-17 05:38:53 +00:00
let params: GridRowIdPB = data.into_inner().try_into()?;
2022-04-09 14:42:42 +00:00
let editor = manager.get_grid_editor(&params.grid_id)?;
2022-04-10 00:25:01 +00:00
let _ = editor.duplicate_row(&params.row_id).await?;
2022-04-09 14:42:42 +00:00
Ok(())
2022-04-09 14:07:48 +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-07-17 05:38:53 +00:00
data: Data<CreateRowPayloadPB>,
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
// #[tracing::instrument(level = "debug", skip_all, err)]
2022-04-07 12:15:00 +00:00
pub(crate) async fn get_cell_handler(
data: Data<CellIdentifierPayload>,
manager: AppData<Arc<GridManager>>,
) -> DataResult<Cell, FlowyError> {
let params: CellIdentifier = data.into_inner().try_into()?;
let editor = manager.get_grid_editor(&params.grid_id)?;
match editor.get_cell(&params).await {
2022-05-09 08:08:27 +00:00
None => data_result(Cell::empty(&params.field_id)),
2022-04-07 12:15:00 +00:00
Some(cell) => data_result(cell),
}
}
2022-05-26 09:28:44 +00:00
#[tracing::instrument(level = "trace", skip_all, err)]
2022-03-09 08:11:24 +00:00
pub(crate) async fn update_cell_handler(
data: Data<CellChangeset>,
manager: AppData<Arc<GridManager>>,
2022-03-09 08:11:24 +00:00
) -> Result<(), FlowyError> {
let changeset: CellChangeset = 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
2022-05-26 09:28:44 +00:00
#[tracing::instrument(level = "trace", skip_all, err)]
pub(crate) async fn new_select_option_handler(
data: Data<CreateSelectOptionPayload>,
manager: AppData<Arc<GridManager>>,
2022-07-17 05:38:53 +00:00
) -> DataResult<SelectOptionPB, FlowyError> {
let params: CreateSelectOptionParams = data.into_inner().try_into()?;
let editor = manager.get_grid_editor(&params.grid_id)?;
match editor.get_field_rev(&params.field_id).await {
None => Err(ErrorCode::InvalidData.into()),
Some(field_rev) => {
let type_option = select_option_operation(&field_rev)?;
let select_option = type_option.create_option(&params.option_name);
data_result(select_option)
}
}
2022-04-07 00:33:10 +00:00
}
2022-05-26 09:28:44 +00:00
#[tracing::instrument(level = "trace", skip_all, err)]
pub(crate) async fn update_select_option_handler(
2022-07-17 05:38:53 +00:00
data: Data<SelectOptionChangesetPayloadPB>,
2022-04-05 06:25:07 +00:00
manager: AppData<Arc<GridManager>>,
) -> Result<(), FlowyError> {
2022-04-07 00:33:10 +00:00
let changeset: SelectOptionChangeset = data.into_inner().try_into()?;
let editor = manager.get_grid_editor(&changeset.cell_identifier.grid_id)?;
if let Some(mut field_rev) = editor.get_field_rev(&changeset.cell_identifier.field_id).await {
2022-07-01 02:36:07 +00:00
let mut_field_rev = Arc::make_mut(&mut field_rev);
let mut type_option = select_option_operation(mut_field_rev)?;
2022-05-11 03:34:13 +00:00
let mut cell_content_changeset = None;
2022-04-07 00:33:10 +00:00
if let Some(option) = changeset.insert_option {
2022-07-08 06:54:11 +00:00
cell_content_changeset = Some(SelectOptionCellChangeset::from_insert(&option.id).to_str());
2022-04-07 00:33:10 +00:00
type_option.insert_option(option);
}
if let Some(option) = changeset.update_option {
type_option.insert_option(option);
}
2022-04-07 00:33:10 +00:00
if let Some(option) = changeset.delete_option {
2022-07-08 06:54:11 +00:00
cell_content_changeset = Some(SelectOptionCellChangeset::from_delete(&option.id).to_str());
2022-04-07 00:33:10 +00:00
type_option.delete_option(option);
}
2022-07-01 02:36:07 +00:00
mut_field_rev.insert_type_option_entry(&*type_option);
let _ = editor.replace_field(field_rev).await?;
2022-04-07 00:33:10 +00:00
let changeset = CellChangeset {
2022-04-07 00:33:10 +00:00
grid_id: changeset.cell_identifier.grid_id,
row_id: changeset.cell_identifier.row_id,
field_id: changeset.cell_identifier.field_id,
2022-07-08 06:54:11 +00:00
content: cell_content_changeset,
2022-04-07 00:33:10 +00:00
};
let _ = editor.update_cell(changeset).await?;
}
Ok(())
}
2022-05-26 09:28:44 +00:00
#[tracing::instrument(level = "trace", skip(data, manager), err)]
2022-04-07 00:33:10 +00:00
pub(crate) async fn get_select_option_handler(
data: Data<CellIdentifierPayload>,
manager: AppData<Arc<GridManager>>,
2022-07-17 05:38:53 +00:00
) -> DataResult<SelectOptionCellDataPB, FlowyError> {
2022-04-07 00:33:10 +00:00
let params: CellIdentifier = data.into_inner().try_into()?;
let editor = manager.get_grid_editor(&params.grid_id)?;
match editor.get_field_rev(&params.field_id).await {
2022-04-07 00:33:10 +00:00
None => {
2022-05-27 11:03:48 +00:00
tracing::error!("Can't find the select option field with id: {}", params.field_id);
2022-07-17 05:38:53 +00:00
data_result(SelectOptionCellDataPB::default())
2022-04-07 00:33:10 +00:00
}
Some(field_rev) => {
//
let cell_rev = editor.get_cell_rev(&params.row_id, &params.field_id).await?;
let type_option = select_option_operation(&field_rev)?;
let any_cell_data: AnyCellData = match cell_rev {
None => AnyCellData {
data: "".to_string(),
field_type: field_rev.field_type_rev.into(),
},
Some(cell_rev) => cell_rev.try_into()?,
};
2022-07-12 14:24:01 +00:00
let option_context = type_option.selected_select_option(any_cell_data.into());
2022-04-07 00:33:10 +00:00
data_result(option_context)
}
}
}
2022-05-26 09:28:44 +00:00
#[tracing::instrument(level = "trace", skip_all, err)]
2022-05-11 03:34:13 +00:00
pub(crate) async fn update_select_option_cell_handler(
2022-07-17 05:38:53 +00:00
data: Data<SelectOptionCellChangesetPayloadPB>,
2022-04-07 00:33:10 +00:00
manager: AppData<Arc<GridManager>>,
) -> Result<(), FlowyError> {
let params: SelectOptionCellChangesetParams = data.into_inner().try_into()?;
2022-05-11 03:34:13 +00:00
let editor = manager.get_grid_editor(&params.cell_identifier.grid_id)?;
let _ = editor.update_cell(params.into()).await?;
Ok(())
}
2022-05-26 09:28:44 +00:00
#[tracing::instrument(level = "trace", skip_all, err)]
2022-05-11 03:34:13 +00:00
pub(crate) async fn update_date_cell_handler(
2022-07-17 05:38:53 +00:00
data: Data<DateChangesetPayloadPB>,
2022-05-11 03:34:13 +00:00
manager: AppData<Arc<GridManager>>,
) -> Result<(), FlowyError> {
let params: DateChangesetParams = data.into_inner().try_into()?;
let editor = manager.get_grid_editor(&params.cell_identifier.grid_id)?;
let _ = editor.update_cell(params.into()).await?;
2022-04-05 06:25:07 +00:00
Ok(())
}