plugin.dart 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. library flowy_plugin;
  2. import 'package:app_flowy/startup/plugin/plugin.dart';
  3. import 'package:app_flowy/startup/startup.dart';
  4. import 'package:app_flowy/workspace/presentation/home/home_stack.dart';
  5. import 'package:flowy_sdk/protobuf/flowy-folder/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. }
  15. typedef PluginId = String;
  16. abstract class Plugin<T> {
  17. PluginId get id;
  18. PluginDisplay get display;
  19. PluginNotifier? get notifier => null;
  20. PluginType get ty;
  21. void dispose() {
  22. notifier?.dispose();
  23. }
  24. }
  25. abstract class PluginNotifier<T> {
  26. /// Notify if the plugin get deleted
  27. ValueNotifier<T> get isDeleted;
  28. /// Notify if the [PluginDisplay]'s content was changed
  29. ValueNotifier<int> get isDisplayChanged;
  30. void dispose() {}
  31. }
  32. abstract class PluginBuilder {
  33. Plugin build(dynamic data);
  34. String get menuName;
  35. String get menuIcon;
  36. PluginType get pluginType;
  37. ViewDataFormatPB get dataFormatType => ViewDataFormatPB.TreeFormat;
  38. ViewLayoutTypePB? get layoutType => ViewLayoutTypePB.Document;
  39. }
  40. abstract class PluginConfig {
  41. // Return false will disable the user to create it. For example, a trash plugin shouldn't be created by the user,
  42. bool get creatable => true;
  43. }
  44. abstract class PluginDisplay with NavigationItem {
  45. List<NavigationItem> get navigationItems;
  46. Widget buildWidget(PluginContext context);
  47. }
  48. class PluginContext {
  49. // calls when widget of the plugin get deleted
  50. final Function(ViewPB, int?) onDeleted;
  51. PluginContext({required this.onDeleted});
  52. }
  53. void registerPlugin({required PluginBuilder builder, PluginConfig? config}) {
  54. getIt<PluginSandbox>()
  55. .registerPlugin(builder.pluginType, builder, config: config);
  56. }
  57. Plugin makePlugin({required PluginType pluginType, dynamic data}) {
  58. final plugin = getIt<PluginSandbox>().buildPlugin(pluginType, data);
  59. return plugin;
  60. }
  61. List<PluginBuilder> pluginBuilders() {
  62. final pluginBuilders = getIt<PluginSandbox>().builders;
  63. final pluginConfigs = getIt<PluginSandbox>().pluginConfigs;
  64. return pluginBuilders.where(
  65. (builder) {
  66. final config = pluginConfigs[builder.pluginType]?.creatable;
  67. return config ?? true;
  68. },
  69. ).toList();
  70. }
  71. enum FlowyPluginException {
  72. invalidData,
  73. }