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

84 lines
3.3 KiB
Rust
Raw Normal View History

2022-03-04 14:09:16 +00:00
use crate::manager::GridManager;
2022-03-02 14:43:04 +00:00
use flowy_error::FlowyError;
2022-03-09 08:11:24 +00:00
use flowy_grid_data_model::entities::{
2022-03-18 09:14:46 +00:00
CellMetaChangeset, CreateRowPayload, Field, Grid, GridId, QueryFieldPayload, QueryGridBlocksPayload,
2022-03-18 13:04:01 +00:00
QueryRowPayload, RepeatedField, RepeatedGridBlock, Row,
2022-03-09 08:11:24 +00:00
};
2022-03-18 09:14:46 +00:00
use flowy_grid_data_model::parser::{CreateRowParams, QueryFieldParams, QueryGridBlocksParams, QueryRowParams};
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(&params.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-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(&params.grid_id)?;
match editor.get_row(&params.block_id, &params.row_id).await? {
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 get_fields_handler(
2022-03-05 09:52:25 +00:00
data: Data<QueryFieldPayload>,
2022-03-05 02:59:44 +00:00
manager: AppData<Arc<GridManager>>,
2022-03-04 00:22:49 +00:00
) -> DataResult<RepeatedField, FlowyError> {
2022-03-18 09:14:46 +00:00
let params: QueryFieldParams = data.into_inner().try_into()?;
let editor = manager.get_grid_editor(&params.grid_id)?;
let field_metas = editor.get_field_metas(Some(params.field_orders)).await?;
2022-03-16 02:02:37 +00:00
let repeated_field: RepeatedField = field_metas.into_iter().map(Field::from).collect::<Vec<_>>().into();
2022-03-05 02:59:44 +00:00
data_result(repeated_field)
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-03 14:17:07 +00:00
pub(crate) async fn create_row_handler(
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(
data: Data<CellMetaChangeset>,
manager: AppData<Arc<GridManager>>,
2022-03-09 08:11:24 +00:00
) -> Result<(), FlowyError> {
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(())
}