sandbox.dart 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import 'dart:collection';
  2. import 'package:appflowy/plugins/blank/blank.dart';
  3. import 'package:flutter/services.dart';
  4. import '../plugin.dart';
  5. import 'runner.dart';
  6. class PluginSandbox {
  7. final LinkedHashMap<PluginType, PluginBuilder> _pluginBuilders =
  8. LinkedHashMap();
  9. final Map<PluginType, PluginConfig> _pluginConfigs =
  10. <PluginType, PluginConfig>{};
  11. late PluginRunner pluginRunner;
  12. PluginSandbox() {
  13. pluginRunner = PluginRunner();
  14. }
  15. int indexOf(PluginType pluginType) {
  16. final index =
  17. _pluginBuilders.keys.toList().indexWhere((ty) => ty == pluginType);
  18. if (index == -1) {
  19. throw PlatformException(
  20. code: '-1',
  21. message: "Can't find the flowy plugin type: $pluginType",
  22. );
  23. }
  24. return index;
  25. }
  26. /// Build a plugin from [data] with [pluginType]
  27. /// If the [pluginType] is not registered, it will return a blank plugin
  28. Plugin buildPlugin(PluginType pluginType, dynamic data) {
  29. final builder = _pluginBuilders[pluginType] ?? BlankPluginBuilder();
  30. return builder.build(data);
  31. }
  32. void registerPlugin(
  33. PluginType pluginType,
  34. PluginBuilder builder, {
  35. PluginConfig? config,
  36. }) {
  37. if (_pluginBuilders.containsKey(pluginType)) {
  38. throw PlatformException(
  39. code: '-1',
  40. message: "$pluginType was registered before",
  41. );
  42. }
  43. _pluginBuilders[pluginType] = builder;
  44. if (config != null) {
  45. _pluginConfigs[pluginType] = config;
  46. }
  47. }
  48. List<PluginType> get supportPluginTypes => _pluginBuilders.keys.toList();
  49. List<PluginBuilder> get builders => _pluginBuilders.values.toList();
  50. Map<PluginType, PluginConfig> get pluginConfigs => _pluginConfigs;
  51. }