plugin.dart 2.7 KB

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