plugin.dart 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. PluginType get pluginType;
  36. ViewDataTypePB get dataType => ViewDataTypePB.Text;
  37. ViewLayoutTypePB? get layoutType => ViewLayoutTypePB.Document;
  38. }
  39. abstract class PluginConfig {
  40. // Return false will disable the user to create it. For example, a trash plugin shouldn't be created by the user,
  41. bool get creatable => true;
  42. }
  43. abstract class PluginDisplay with NavigationItem {
  44. List<NavigationItem> get navigationItems;
  45. Widget buildWidget(PluginContext context);
  46. }
  47. class PluginContext {
  48. // calls when widget of the plugin get deleted
  49. final Function(ViewPB, int?) onDeleted;
  50. PluginContext({required this.onDeleted});
  51. }
  52. void registerPlugin({required PluginBuilder builder, PluginConfig? config}) {
  53. getIt<PluginSandbox>()
  54. .registerPlugin(builder.pluginType, builder, config: config);
  55. }
  56. Plugin makePlugin({required PluginType pluginType, dynamic data}) {
  57. final plugin = getIt<PluginSandbox>().buildPlugin(pluginType, data);
  58. return plugin;
  59. }
  60. List<PluginBuilder> pluginBuilders() {
  61. final pluginBuilders = getIt<PluginSandbox>().builders;
  62. final pluginConfigs = getIt<PluginSandbox>().pluginConfigs;
  63. return pluginBuilders.where(
  64. (builder) {
  65. final config = pluginConfigs[builder.pluginType]?.creatable;
  66. return config ?? true;
  67. },
  68. ).toList();
  69. }
  70. enum FlowyPluginException {
  71. invalidData,
  72. }