plugin.dart 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. quill,
  11. blank,
  12. trash,
  13. grid,
  14. }
  15. extension FlowyDefaultPluginExt on DefaultPlugin {
  16. int type() {
  17. switch (this) {
  18. case DefaultPlugin.quill:
  19. return 0;
  20. case DefaultPlugin.blank:
  21. return 1;
  22. case DefaultPlugin.trash:
  23. return 2;
  24. case DefaultPlugin.grid:
  25. return 3;
  26. }
  27. }
  28. }
  29. typedef PluginType = int;
  30. typedef PluginDataType = ViewDataType;
  31. typedef PluginId = String;
  32. abstract class Plugin {
  33. PluginId get id;
  34. PluginDisplay get display;
  35. PluginType get ty;
  36. void dispose() {}
  37. }
  38. abstract class PluginBuilder {
  39. Plugin build(dynamic data);
  40. String get menuName;
  41. PluginType get pluginType;
  42. ViewDataType get dataType => ViewDataType.TextBlock;
  43. }
  44. abstract class PluginConfig {
  45. // Return false will disable the user to create it. For example, a trash plugin shouldn't be created by the user,
  46. bool get creatable => true;
  47. }
  48. abstract class PluginDisplay<T> with NavigationItem {
  49. @override
  50. Widget get leftBarItem;
  51. @override
  52. Widget? get rightBarItem;
  53. List<NavigationItem> get navigationItems;
  54. PublishNotifier<T>? get notifier => null;
  55. Widget buildWidget();
  56. }
  57. void registerPlugin({required PluginBuilder builder, PluginConfig? config}) {
  58. getIt<PluginSandbox>().registerPlugin(builder.pluginType, builder, config: config);
  59. }
  60. Plugin makePlugin({required PluginType pluginType, dynamic data}) {
  61. final plugin = getIt<PluginSandbox>().buildPlugin(pluginType, data);
  62. return plugin;
  63. }
  64. List<PluginBuilder> pluginBuilders() {
  65. final pluginBuilders = getIt<PluginSandbox>().builders;
  66. final pluginConfigs = getIt<PluginSandbox>().pluginConfigs;
  67. return pluginBuilders.where(
  68. (builder) {
  69. final config = pluginConfigs[builder.pluginType]?.creatable;
  70. return config ?? true;
  71. },
  72. ).toList();
  73. }
  74. enum FlowyPluginException {
  75. invalidData,
  76. }