event_map.rs 14 KB

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