event_map.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. use std::sync::Arc;
  2. use strum_macros::Display;
  3. use flowy_derive::{Flowy_Event, ProtoBuf_Enum};
  4. use lib_dispatch::prelude::*;
  5. use crate::event_handler::*;
  6. use crate::manager::DatabaseManager2;
  7. pub fn init(database_manager: Arc<DatabaseManager2>) -> AFPlugin {
  8. let plugin = AFPlugin::new()
  9. .name(env!("CARGO_PKG_NAME"))
  10. .state(database_manager);
  11. plugin
  12. .event(DatabaseEvent::GetDatabase, get_database_data_handler)
  13. .event(DatabaseEvent::GetDatabaseId, get_database_id_handler)
  14. .event(DatabaseEvent::GetDatabaseSetting, get_database_setting_handler)
  15. .event(DatabaseEvent::UpdateDatabaseSetting, update_database_setting_handler)
  16. .event(DatabaseEvent::GetAllFilters, get_all_filters_handler)
  17. .event(DatabaseEvent::GetAllSorts, get_all_sorts_handler)
  18. .event(DatabaseEvent::DeleteAllSorts, delete_all_sorts_handler)
  19. // Field
  20. .event(DatabaseEvent::GetFields, get_fields_handler)
  21. .event(DatabaseEvent::UpdateField, update_field_handler)
  22. .event(DatabaseEvent::UpdateFieldTypeOption, update_field_type_option_handler)
  23. .event(DatabaseEvent::DeleteField, delete_field_handler)
  24. .event(DatabaseEvent::UpdateFieldType, switch_to_field_handler)
  25. .event(DatabaseEvent::DuplicateField, duplicate_field_handler)
  26. .event(DatabaseEvent::MoveField, move_field_handler)
  27. .event(DatabaseEvent::GetTypeOption, get_field_type_option_data_handler)
  28. .event(DatabaseEvent::CreateTypeOption, create_field_type_option_data_handler)
  29. // Row
  30. .event(DatabaseEvent::CreateRow, create_row_handler)
  31. .event(DatabaseEvent::GetRow, get_row_handler)
  32. .event(DatabaseEvent::DeleteRow, delete_row_handler)
  33. .event(DatabaseEvent::DuplicateRow, duplicate_row_handler)
  34. .event(DatabaseEvent::MoveRow, move_row_handler)
  35. // Cell
  36. .event(DatabaseEvent::GetCell, get_cell_handler)
  37. .event(DatabaseEvent::UpdateCell, update_cell_handler)
  38. // SelectOption
  39. .event(DatabaseEvent::CreateSelectOption, new_select_option_handler)
  40. .event(DatabaseEvent::InsertOrUpdateSelectOption, insert_or_update_select_option_handler)
  41. .event(DatabaseEvent::DeleteSelectOption, delete_select_option_handler)
  42. .event(DatabaseEvent::GetSelectOptionCellData, get_select_option_handler)
  43. .event(DatabaseEvent::UpdateSelectOptionCell, update_select_option_cell_handler)
  44. // Checklist
  45. .event(DatabaseEvent::GetChecklistCellData, get_checklist_cell_data_handler)
  46. .event(DatabaseEvent::UpdateChecklistCell, update_checklist_cell_handler)
  47. // Date
  48. .event(DatabaseEvent::UpdateDateCell, update_date_cell_handler)
  49. // Group
  50. .event(DatabaseEvent::MoveGroup, move_group_handler)
  51. .event(DatabaseEvent::MoveGroupRow, move_group_row_handler)
  52. .event(DatabaseEvent::GetGroups, get_groups_handler)
  53. .event(DatabaseEvent::GetGroup, get_group_handler)
  54. .event(DatabaseEvent::SetGroupByField, set_group_by_field_handler)
  55. .event(DatabaseEvent::UpdateGroup, update_group_handler)
  56. // Database
  57. .event(DatabaseEvent::GetDatabases, get_databases_handler)
  58. // Calendar
  59. .event(DatabaseEvent::GetAllCalendarEvents, get_calendar_events_handler)
  60. .event(DatabaseEvent::GetNoDateCalendarEvents, get_no_date_calendar_events_handler)
  61. .event(DatabaseEvent::GetCalendarEvent, get_calendar_event_handler)
  62. .event(DatabaseEvent::MoveCalendarEvent, move_calendar_event_handler)
  63. // Layout setting
  64. .event(DatabaseEvent::SetLayoutSetting, set_layout_setting_handler)
  65. .event(DatabaseEvent::GetLayoutSetting, get_layout_setting_handler)
  66. .event(DatabaseEvent::CreateDatabaseView, create_database_view)
  67. .event(DatabaseEvent::ExportCSV, export_csv_handler)
  68. }
  69. /// [DatabaseEvent] defines events that are used to interact with the Grid. You could check [this](https://appflowy.gitbook.io/docs/essential-documentation/contribute-to-appflowy/architecture/backend/protobuf)
  70. /// out, it includes how to use these annotations: input, output, etc.
  71. #[derive(Clone, Copy, PartialEq, Eq, Debug, Display, Hash, ProtoBuf_Enum, Flowy_Event)]
  72. #[event_err = "FlowyError"]
  73. pub enum DatabaseEvent {
  74. /// [GetDatabase] event is used to get the [DatabasePB]
  75. ///
  76. /// The event handler accepts a [DatabaseViewIdPB] and returns a [DatabasePB] if there are no errors.
  77. #[event(input = "DatabaseViewIdPB", output = "DatabasePB")]
  78. GetDatabase = 0,
  79. #[event(input = "DatabaseViewIdPB", output = "DatabaseIdPB")]
  80. GetDatabaseId = 1,
  81. /// [GetDatabaseSetting] event is used to get the database's settings.
  82. ///
  83. /// The event handler accepts [DatabaseViewIdPB] and return [DatabaseViewSettingPB]
  84. /// if there is no errors.
  85. #[event(input = "DatabaseViewIdPB", output = "DatabaseViewSettingPB")]
  86. GetDatabaseSetting = 2,
  87. /// [UpdateDatabaseSetting] event is used to update the database's settings.
  88. ///
  89. /// The event handler accepts [DatabaseSettingChangesetPB] and return errors if failed to modify the grid's settings.
  90. #[event(input = "DatabaseSettingChangesetPB")]
  91. UpdateDatabaseSetting = 3,
  92. #[event(input = "DatabaseViewIdPB", output = "RepeatedFilterPB")]
  93. GetAllFilters = 4,
  94. #[event(input = "DatabaseViewIdPB", output = "RepeatedSortPB")]
  95. GetAllSorts = 5,
  96. #[event(input = "DatabaseViewIdPB")]
  97. DeleteAllSorts = 6,
  98. /// [GetFields] event is used to get the database's fields.
  99. ///
  100. /// The event handler accepts a [GetFieldPayloadPB] and returns a [RepeatedFieldPB]
  101. /// if there are no errors.
  102. #[event(input = "GetFieldPayloadPB", output = "RepeatedFieldPB")]
  103. GetFields = 10,
  104. /// [UpdateField] event is used to update a field's attributes.
  105. ///
  106. /// The event handler accepts a [FieldChangesetPB] and returns errors if failed to modify the
  107. /// field.
  108. #[event(input = "FieldChangesetPB")]
  109. UpdateField = 11,
  110. /// [UpdateFieldTypeOption] event is used to update the field's type-option data. Certain field
  111. /// types have user-defined options such as color, date format, number format, or a list of values
  112. /// for a multi-select list. These options are defined within a specialization of the
  113. /// FieldTypeOption class.
  114. ///
  115. /// Check out [this](https://appflowy.gitbook.io/docs/essential-documentation/contribute-to-appflowy/architecture/frontend/grid#fieldtype)
  116. /// for more information.
  117. ///
  118. /// The event handler accepts a [TypeOptionChangesetPB] and returns errors if failed to modify the
  119. /// field.
  120. #[event(input = "TypeOptionChangesetPB")]
  121. UpdateFieldTypeOption = 12,
  122. /// [DeleteField] event is used to delete a Field. [DeleteFieldPayloadPB] is the context that
  123. /// is used to delete the field from the Database.
  124. #[event(input = "DeleteFieldPayloadPB")]
  125. DeleteField = 14,
  126. /// [UpdateFieldType] event is used to update the current Field's type.
  127. /// It will insert a new FieldTypeOptionData if the new FieldType doesn't exist before, otherwise
  128. /// reuse the existing FieldTypeOptionData. You could check the [DatabaseRevisionPad] for more details.
  129. #[event(input = "UpdateFieldTypePayloadPB")]
  130. UpdateFieldType = 20,
  131. /// [DuplicateField] event is used to duplicate a Field. The duplicated field data is kind of
  132. /// deep copy of the target field. The passed in [DuplicateFieldPayloadPB] is the context that is
  133. /// used to duplicate the field.
  134. ///
  135. /// Return errors if failed to duplicate the field.
  136. ///
  137. #[event(input = "DuplicateFieldPayloadPB")]
  138. DuplicateField = 21,
  139. /// [MoveItem] event is used to move an item. For the moment, Item has two types defined in
  140. /// [MoveItemTypePB].
  141. #[event(input = "MoveFieldPayloadPB")]
  142. MoveField = 22,
  143. /// [TypeOptionPathPB] event is used to get the FieldTypeOption data for a specific field type.
  144. ///
  145. /// Check out the [TypeOptionPB] for more details. If the [FieldTypeOptionData] does exist
  146. /// for the target type, the [TypeOptionBuilder] will create the default data for that type.
  147. ///
  148. /// Return the [TypeOptionPB] if there are no errors.
  149. #[event(input = "TypeOptionPathPB", output = "TypeOptionPB")]
  150. GetTypeOption = 23,
  151. /// [CreateTypeOption] event is used to create a new FieldTypeOptionData.
  152. #[event(input = "CreateFieldPayloadPB", output = "TypeOptionPB")]
  153. CreateTypeOption = 24,
  154. /// [CreateSelectOption] event is used to create a new select option. Returns a [SelectOptionPB] if
  155. /// there are no errors.
  156. #[event(input = "CreateSelectOptionPayloadPB", output = "SelectOptionPB")]
  157. CreateSelectOption = 30,
  158. /// [GetSelectOptionCellData] event is used to get the select option data for cell editing.
  159. /// [CellIdPB] locate which cell data that will be read from. The return value, [SelectOptionCellDataPB]
  160. /// contains the available options and the currently selected options.
  161. #[event(input = "CellIdPB", output = "SelectOptionCellDataPB")]
  162. GetSelectOptionCellData = 31,
  163. /// [InsertOrUpdateSelectOption] event is used to update a FieldTypeOptionData whose field_type is
  164. /// FieldType::SingleSelect or FieldType::MultiSelect.
  165. ///
  166. /// This event may trigger the DatabaseNotification::DidUpdateCell event.
  167. /// For example, DatabaseNotification::DidUpdateCell will be triggered if the [SelectOptionChangesetPB]
  168. /// carries a change that updates the name of the option.
  169. #[event(input = "RepeatedSelectOptionPayload")]
  170. InsertOrUpdateSelectOption = 32,
  171. #[event(input = "RepeatedSelectOptionPayload")]
  172. DeleteSelectOption = 33,
  173. #[event(input = "CreateRowPayloadPB", output = "RowPB")]
  174. CreateRow = 50,
  175. /// [GetRow] event is used to get the row data,[RowPB]. [OptionalRowPB] is a wrapper that enables
  176. /// to return a nullable row data.
  177. #[event(input = "RowIdPB", output = "OptionalRowPB")]
  178. GetRow = 51,
  179. #[event(input = "RowIdPB")]
  180. DeleteRow = 52,
  181. #[event(input = "RowIdPB")]
  182. DuplicateRow = 53,
  183. #[event(input = "MoveRowPayloadPB")]
  184. MoveRow = 54,
  185. #[event(input = "CellIdPB", output = "CellPB")]
  186. GetCell = 70,
  187. /// [UpdateCell] event is used to update the cell content. The passed in data, [CellChangesetPB],
  188. /// carries the changes that will be applied to the cell content by calling `update_cell` function.
  189. ///
  190. /// The 'content' property of the [CellChangesetPB] is a String type. It can be used directly if the
  191. /// cell uses string data. For example, the TextCell or NumberCell.
  192. ///
  193. /// But,it can be treated as a generic type, because we can use [serde] to deserialize the string
  194. /// into a specific data type. For the moment, the 'content' will be deserialized to a concrete type
  195. /// when the FieldType is SingleSelect, DateTime, and MultiSelect. Please see
  196. /// the [UpdateSelectOptionCell] and [UpdateDateCell] events for more details.
  197. #[event(input = "CellChangesetPB")]
  198. UpdateCell = 71,
  199. /// [UpdateSelectOptionCell] event is used to update a select option cell's data. [SelectOptionCellChangesetPB]
  200. /// contains options that will be deleted or inserted. It can be cast to [CellChangesetPB] that
  201. /// will be used by the `update_cell` function.
  202. #[event(input = "SelectOptionCellChangesetPB")]
  203. UpdateSelectOptionCell = 72,
  204. #[event(input = "CellIdPB", output = "ChecklistCellDataPB")]
  205. GetChecklistCellData = 73,
  206. #[event(input = "ChecklistCellDataChangesetPB")]
  207. UpdateChecklistCell = 74,
  208. /// [UpdateDateCell] event is used to update a date cell's data. [DateChangesetPB]
  209. /// contains the date and the time string. It can be cast to [CellChangesetPB] that
  210. /// will be used by the `update_cell` function.
  211. #[event(input = "DateChangesetPB")]
  212. UpdateDateCell = 80,
  213. #[event(input = "DatabaseViewIdPB", output = "RepeatedGroupPB")]
  214. GetGroups = 100,
  215. #[event(input = "DatabaseGroupIdPB", output = "GroupPB")]
  216. GetGroup = 101,
  217. #[event(input = "MoveGroupPayloadPB")]
  218. MoveGroup = 111,
  219. #[event(input = "MoveGroupRowPayloadPB")]
  220. MoveGroupRow = 112,
  221. #[event(input = "GroupByFieldPayloadPB")]
  222. SetGroupByField = 113,
  223. #[event(input = "UpdateGroupPB")]
  224. UpdateGroup = 114,
  225. /// Returns all the databases
  226. #[event(output = "RepeatedDatabaseDescriptionPB")]
  227. GetDatabases = 120,
  228. #[event(input = "LayoutSettingChangesetPB")]
  229. SetLayoutSetting = 121,
  230. #[event(input = "DatabaseLayoutMetaPB", output = "DatabaseLayoutSettingPB")]
  231. GetLayoutSetting = 122,
  232. #[event(input = "CalendarEventRequestPB", output = "RepeatedCalendarEventPB")]
  233. GetAllCalendarEvents = 123,
  234. #[event(
  235. input = "CalendarEventRequestPB",
  236. output = "RepeatedNoDateCalendarEventPB"
  237. )]
  238. GetNoDateCalendarEvents = 124,
  239. #[event(input = "RowIdPB", output = "CalendarEventPB")]
  240. GetCalendarEvent = 125,
  241. #[event(input = "MoveCalendarEventPB")]
  242. MoveCalendarEvent = 126,
  243. #[event(input = "CreateDatabaseViewPayloadPB")]
  244. CreateDatabaseView = 130,
  245. #[event(input = "DatabaseViewIdPB", output = "DatabaseExportDataPB")]
  246. ExportCSV = 141,
  247. }