plugin.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. library flowy_plugin;
  2. import 'package:appflowy/startup/plugin/plugin.dart';
  3. import 'package:appflowy/startup/startup.dart';
  4. import 'package:appflowy/workspace/presentation/home/home_stack.dart';
  5. import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart';
  6. import 'package:flutter/widgets.dart';
  7. export "./src/sandbox.dart";
  8. enum PluginType {
  9. editor,
  10. blank,
  11. trash,
  12. grid,
  13. board,
  14. calendar,
  15. }
  16. typedef PluginId = String;
  17. abstract class Plugin<T> {
  18. PluginId get id;
  19. PluginWidgetBuilder get widgetBuilder;
  20. PluginNotifier? get notifier => null;
  21. PluginType get pluginType;
  22. void dispose() {
  23. notifier?.dispose();
  24. }
  25. }
  26. abstract class PluginNotifier<T> {
  27. /// Notify if the plugin get deleted
  28. ValueNotifier<T> get isDeleted;
  29. void dispose() {}
  30. }
  31. abstract class PluginBuilder {
  32. Plugin build(dynamic data);
  33. String get menuName;
  34. String get menuIcon;
  35. /// The type of this [Plugin]. Each [Plugin] should have a unique [PluginType]
  36. PluginType get pluginType;
  37. /// The layoutType is used in the backend to determine the layout of the view.
  38. /// Currrently, AppFlowy supports 4 layout types: Document, Grid, Board, Calendar.
  39. ViewLayoutPB? get layoutType => ViewLayoutPB.Document;
  40. }
  41. abstract class PluginConfig {
  42. // Return false will disable the user to create it. For example, a trash plugin shouldn't be created by the user,
  43. bool get creatable => true;
  44. }
  45. abstract class PluginWidgetBuilder with NavigationItem {
  46. List<NavigationItem> get navigationItems;
  47. EdgeInsets get contentPadding =>
  48. const EdgeInsets.symmetric(horizontal: 40, vertical: 28);
  49. Widget buildWidget({PluginContext? context});
  50. }
  51. class PluginContext {
  52. // calls when widget of the plugin get deleted
  53. final Function(ViewPB, int?) onDeleted;
  54. PluginContext({required this.onDeleted});
  55. }
  56. void registerPlugin({required PluginBuilder builder, PluginConfig? config}) {
  57. getIt<PluginSandbox>()
  58. .registerPlugin(builder.pluginType, builder, config: config);
  59. }
  60. /// Make the correct plugin from the [pluginType] and [data]. If the plugin
  61. /// is not registered, it will return a blank plugin.
  62. Plugin makePlugin({required PluginType pluginType, dynamic data}) {
  63. final plugin = getIt<PluginSandbox>().buildPlugin(pluginType, data);
  64. return plugin;
  65. }
  66. List<PluginBuilder> pluginBuilders() {
  67. final pluginBuilders = getIt<PluginSandbox>().builders;
  68. final pluginConfigs = getIt<PluginSandbox>().pluginConfigs;
  69. return pluginBuilders.where(
  70. (builder) {
  71. final config = pluginConfigs[builder.pluginType]?.creatable;
  72. return config ?? true;
  73. },
  74. ).toList();
  75. }
  76. enum FlowyPluginException {
  77. invalidData,
  78. }