plugin.dart 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /// Notify if the [PluginWidgetBuilder]'s content was changed
  30. ValueNotifier<int> get isDisplayChanged;
  31. void dispose() {}
  32. }
  33. abstract class PluginBuilder {
  34. Plugin build(dynamic data);
  35. String get menuName;
  36. String get menuIcon;
  37. /// The type of this [Plugin]. Each [Plugin] should have a unique [PluginType]
  38. PluginType get pluginType;
  39. /// The layoutType is used in the backend to determine the layout of the view.
  40. /// Currrently, AppFlowy supports 4 layout types: Document, Grid, Board, Calendar.
  41. ViewLayoutPB? get layoutType => ViewLayoutPB.Document;
  42. }
  43. abstract class PluginConfig {
  44. // Return false will disable the user to create it. For example, a trash plugin shouldn't be created by the user,
  45. bool get creatable => true;
  46. }
  47. abstract class PluginWidgetBuilder with NavigationItem {
  48. List<NavigationItem> get navigationItems;
  49. EdgeInsets get contentPadding =>
  50. const EdgeInsets.symmetric(horizontal: 40, vertical: 28);
  51. Widget buildWidget({PluginContext? context});
  52. }
  53. class PluginContext {
  54. // calls when widget of the plugin get deleted
  55. final Function(ViewPB, int?) onDeleted;
  56. PluginContext({required this.onDeleted});
  57. }
  58. void registerPlugin({required PluginBuilder builder, PluginConfig? config}) {
  59. getIt<PluginSandbox>()
  60. .registerPlugin(builder.pluginType, builder, config: config);
  61. }
  62. /// Make the correct plugin from the [pluginType] and [data]. If the plugin
  63. /// is not registered, it will return a blank plugin.
  64. Plugin makePlugin({required PluginType pluginType, dynamic data}) {
  65. final plugin = getIt<PluginSandbox>().buildPlugin(pluginType, data);
  66. return plugin;
  67. }
  68. List<PluginBuilder> pluginBuilders() {
  69. final pluginBuilders = getIt<PluginSandbox>().builders;
  70. final pluginConfigs = getIt<PluginSandbox>().pluginConfigs;
  71. return pluginBuilders.where(
  72. (builder) {
  73. final config = pluginConfigs[builder.pluginType]?.creatable;
  74. return config ?? true;
  75. },
  76. ).toList();
  77. }
  78. enum FlowyPluginException {
  79. invalidData,
  80. }