| 
									
										
										
										
											2022-02-28 22:38:53 +08:00
										 |  |  | library flowy_plugin; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-14 13:34:01 -07:00
										 |  |  | import 'package:appflowy/generated/flowy_svgs.g.dart'; | 
					
						
							| 
									
										
										
										
											2023-02-26 16:27:17 +08:00
										 |  |  | import 'package:appflowy/startup/plugin/plugin.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/startup/startup.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/workspace/presentation/home/home_stack.dart'; | 
					
						
							| 
									
										
										
										
											2023-04-04 08:41:16 +08:00
										 |  |  | import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart'; | 
					
						
							| 
									
										
										
										
											2022-02-28 22:38:53 +08:00
										 |  |  | import 'package:flutter/widgets.dart'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export "./src/sandbox.dart"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-18 19:32:08 +08:00
										 |  |  | enum PluginType { | 
					
						
							| 
									
										
										
										
											2022-08-10 17:59:28 +08:00
										 |  |  |   editor, | 
					
						
							| 
									
										
										
										
											2022-03-02 11:38:22 +08:00
										 |  |  |   blank, | 
					
						
							|  |  |  |   trash, | 
					
						
							| 
									
										
										
										
											2022-03-04 08:22:49 +08:00
										 |  |  |   grid, | 
					
						
							| 
									
										
										
										
											2022-06-11 15:51:53 +08:00
										 |  |  |   board, | 
					
						
							| 
									
										
										
										
											2023-01-08 20:51:19 +08:00
										 |  |  |   calendar, | 
					
						
							| 
									
										
										
										
											2022-03-02 11:38:22 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-02-28 22:38:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-01 16:05:45 +08:00
										 |  |  | typedef PluginId = String; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-22 13:08:48 +08:00
										 |  |  | abstract class Plugin<T> { | 
					
						
							| 
									
										
										
										
											2022-03-02 11:38:22 +08:00
										 |  |  |   PluginId get id; | 
					
						
							| 
									
										
										
										
											2022-03-01 16:05:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-01 20:23:27 +08:00
										 |  |  |   PluginWidgetBuilder get widgetBuilder; | 
					
						
							| 
									
										
										
										
											2022-02-28 22:38:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-22 13:08:48 +08:00
										 |  |  |   PluginNotifier? get notifier => null; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-01 20:23:27 +08:00
										 |  |  |   PluginType get pluginType; | 
					
						
							| 
									
										
										
										
											2022-02-28 22:38:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-22 13:08:48 +08:00
										 |  |  |   void dispose() { | 
					
						
							|  |  |  |     notifier?.dispose(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-26 16:59:58 +08:00
										 |  |  | abstract class PluginNotifier<T> { | 
					
						
							| 
									
										
										
										
											2022-09-22 13:08:48 +08:00
										 |  |  |   /// Notify if the plugin get deleted
 | 
					
						
							| 
									
										
										
										
											2022-09-26 16:59:58 +08:00
										 |  |  |   ValueNotifier<T> get isDeleted; | 
					
						
							| 
									
										
										
										
											2022-09-22 13:08:48 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-02 11:38:22 +08:00
										 |  |  |   void dispose() {} | 
					
						
							| 
									
										
										
										
											2022-02-28 22:38:53 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | abstract class PluginBuilder { | 
					
						
							|  |  |  |   Plugin build(dynamic data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-01 16:05:45 +08:00
										 |  |  |   String get menuName; | 
					
						
							| 
									
										
										
										
											2022-02-28 22:38:53 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-14 13:34:01 -07:00
										 |  |  |   FlowySvgData get icon; | 
					
						
							| 
									
										
										
										
											2022-11-15 11:45:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-01 20:23:27 +08:00
										 |  |  |   /// The type of this [Plugin]. Each [Plugin] should have a unique [PluginType]
 | 
					
						
							| 
									
										
										
										
											2022-02-28 22:38:53 +08:00
										 |  |  |   PluginType get pluginType; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-01 20:23:27 +08:00
										 |  |  |   /// The layoutType is used in the backend to determine the layout of the view.
 | 
					
						
							|  |  |  |   /// Currrently, AppFlowy supports 4 layout types: Document, Grid, Board, Calendar.
 | 
					
						
							| 
									
										
										
										
											2023-04-04 14:08:50 +08:00
										 |  |  |   ViewLayoutPB? get layoutType => ViewLayoutPB.Document; | 
					
						
							| 
									
										
										
										
											2022-02-28 22:38:53 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-01 11:22:39 +08:00
										 |  |  | abstract class PluginConfig { | 
					
						
							| 
									
										
										
										
											2022-03-02 11:38:22 +08:00
										 |  |  |   // Return false will disable the user to create it. For example, a trash plugin shouldn't be created by the user,
 | 
					
						
							| 
									
										
										
										
											2022-03-01 11:22:39 +08:00
										 |  |  |   bool get creatable => true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-01 20:23:27 +08:00
										 |  |  | abstract class PluginWidgetBuilder with NavigationItem { | 
					
						
							| 
									
										
										
										
											2022-02-28 22:38:53 +08:00
										 |  |  |   List<NavigationItem> get navigationItems; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-01 20:23:27 +08:00
										 |  |  |   EdgeInsets get contentPadding => | 
					
						
							|  |  |  |       const EdgeInsets.symmetric(horizontal: 40, vertical: 28); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-02 18:07:44 +08:00
										 |  |  |   Widget buildWidget({PluginContext? context, required bool shrinkWrap}); | 
					
						
							| 
									
										
										
										
											2022-09-22 13:08:48 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class PluginContext { | 
					
						
							|  |  |  |   // calls when widget of the plugin get deleted
 | 
					
						
							| 
									
										
										
										
											2022-09-26 16:59:58 +08:00
										 |  |  |   final Function(ViewPB, int?) onDeleted; | 
					
						
							| 
									
										
										
										
											2022-03-02 11:38:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-22 13:08:48 +08:00
										 |  |  |   PluginContext({required this.onDeleted}); | 
					
						
							| 
									
										
										
										
											2022-02-28 22:38:53 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-01 11:22:39 +08:00
										 |  |  | void registerPlugin({required PluginBuilder builder, PluginConfig? config}) { | 
					
						
							| 
									
										
										
										
											2022-08-09 10:35:27 +08:00
										 |  |  |   getIt<PluginSandbox>() | 
					
						
							|  |  |  |       .registerPlugin(builder.pluginType, builder, config: config); | 
					
						
							| 
									
										
										
										
											2022-02-28 22:38:53 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-01 20:23:27 +08:00
										 |  |  | /// Make the correct plugin from the [pluginType] and [data]. If the plugin
 | 
					
						
							|  |  |  | ///  is not registered, it will return a blank plugin.
 | 
					
						
							| 
									
										
										
										
											2022-03-01 10:25:21 +08:00
										 |  |  | Plugin makePlugin({required PluginType pluginType, dynamic data}) { | 
					
						
							| 
									
										
										
										
											2022-02-28 22:38:53 +08:00
										 |  |  |   final plugin = getIt<PluginSandbox>().buildPlugin(pluginType, data); | 
					
						
							|  |  |  |   return plugin; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-01 11:22:39 +08:00
										 |  |  | List<PluginBuilder> pluginBuilders() { | 
					
						
							|  |  |  |   final pluginBuilders = getIt<PluginSandbox>().builders; | 
					
						
							|  |  |  |   final pluginConfigs = getIt<PluginSandbox>().pluginConfigs; | 
					
						
							|  |  |  |   return pluginBuilders.where( | 
					
						
							|  |  |  |     (builder) { | 
					
						
							|  |  |  |       final config = pluginConfigs[builder.pluginType]?.creatable; | 
					
						
							|  |  |  |       return config ?? true; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   ).toList(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-28 22:38:53 +08:00
										 |  |  | enum FlowyPluginException { | 
					
						
							|  |  |  |   invalidData, | 
					
						
							|  |  |  | } |