event_map.rs 14 KB

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