plugin.dart 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. library flowy_plugin;
  2. import 'package:app_flowy/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_infra/notifier.dart';
  6. import 'package:flowy_sdk/protobuf/flowy-folder-data-model/view.pb.dart';
  7. import 'package:flutter/widgets.dart';
  8. export "./src/sandbox.dart";
  9. enum DefaultPlugin {
  10. quillEditor,
  11. blank,
  12. trash,
  13. }
  14. extension FlowyDefaultPluginExt on DefaultPlugin {
  15. int type() {
  16. switch (this) {
  17. case DefaultPlugin.quillEditor:
  18. return 0;
  19. case DefaultPlugin.blank:
  20. return 1;
  21. case DefaultPlugin.trash:
  22. return 2;
  23. }
  24. }
  25. }
  26. typedef PluginType = int;
  27. typedef PluginDataType = ViewDataType;
  28. typedef PluginId = String;
  29. abstract class Plugin {
  30. PluginId get id;
  31. PluginDisplay get display;
  32. PluginType get ty;
  33. void dispose() {}
  34. }
  35. abstract class PluginBuilder {
  36. Plugin build(dynamic data);
  37. String get menuName;
  38. PluginType get pluginType;
  39. ViewDataType get dataType => ViewDataType.PlainText;
  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 PluginDisplay<T> with NavigationItem {
  46. @override
  47. Widget get leftBarItem;
  48. @override
  49. Widget? get rightBarItem;
  50. List<NavigationItem> get navigationItems;
  51. PublishNotifier<T>? get notifier => null;
  52. Widget buildWidget();
  53. }
  54. void registerPlugin({required PluginBuilder builder, PluginConfig? config}) {
  55. getIt<PluginSandbox>().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. }