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

65 lines
2.5 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::{
CellMetaChangeset, CreateRowPayload, Field, Grid, GridId, QueryFieldPayload, QueryRowPayload, RepeatedField,
RepeatedRow, Row,
2022-03-09 08:11:24 +00:00
};
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-03 14:17:07 +00:00
pub(crate) async fn get_rows_handler(
2022-03-05 09:52:25 +00:00
data: Data<QueryRowPayload>,
2022-03-05 02:59:44 +00:00
manager: AppData<Arc<GridManager>>,
2022-03-04 00:22:49 +00:00
) -> DataResult<RepeatedRow, FlowyError> {
2022-03-05 09:52:25 +00:00
let payload: QueryRowPayload = data.into_inner();
let editor = manager.get_grid_editor(&payload.grid_id)?;
2022-03-14 09:24:25 +00:00
let repeated_row: RepeatedRow = editor.get_rows(Some(payload.row_orders)).await?.into();
2022-03-05 02:59:44 +00:00
data_result(repeated_row)
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 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-05 09:52:25 +00:00
let payload: QueryFieldPayload = data.into_inner();
let editor = manager.get_grid_editor(&payload.grid_id)?;
2022-03-15 03:07:18 +00:00
let field_metas = editor.get_field_metas(Some(payload.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>>,
) -> DataResult<Row, FlowyError> {
let payload: CreateRowPayload = data.into_inner();
let editor = manager.get_grid_editor(payload.grid_id.as_ref())?;
let row = editor.create_row(payload.upper_row_id).await?;
data_result(row)
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(())
}