2022-02-28 14:38:53 +00:00
|
|
|
library flowy_plugin;
|
|
|
|
|
2022-08-09 02:35:27 +00:00
|
|
|
import 'package:app_flowy/startup/plugin/plugin.dart';
|
2022-02-28 14:38:53 +00:00
|
|
|
import 'package:app_flowy/startup/startup.dart';
|
2022-03-01 08:05:45 +00:00
|
|
|
import 'package:app_flowy/workspace/presentation/home/home_stack.dart';
|
2022-07-04 07:00:54 +00:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-folder/view.pb.dart';
|
2022-02-28 14:38:53 +00:00
|
|
|
import 'package:flutter/widgets.dart';
|
|
|
|
|
|
|
|
export "./src/sandbox.dart";
|
|
|
|
|
2022-08-18 11:32:08 +00:00
|
|
|
enum PluginType {
|
2022-08-10 09:59:28 +00:00
|
|
|
editor,
|
2022-03-02 03:38:22 +00:00
|
|
|
blank,
|
|
|
|
trash,
|
2022-03-04 00:22:49 +00:00
|
|
|
grid,
|
2022-06-11 07:51:53 +00:00
|
|
|
board,
|
2022-03-02 03:38:22 +00:00
|
|
|
}
|
2022-02-28 14:38:53 +00:00
|
|
|
|
2022-03-01 08:05:45 +00:00
|
|
|
typedef PluginId = String;
|
|
|
|
|
2022-09-22 05:08:48 +00:00
|
|
|
abstract class Plugin<T> {
|
2022-03-02 03:38:22 +00:00
|
|
|
PluginId get id;
|
2022-03-01 08:05:45 +00:00
|
|
|
|
2022-03-02 03:38:22 +00:00
|
|
|
PluginDisplay get display;
|
2022-02-28 14:38:53 +00:00
|
|
|
|
2022-09-22 05:08:48 +00:00
|
|
|
PluginNotifier? get notifier => null;
|
|
|
|
|
2022-03-02 03:38:22 +00:00
|
|
|
PluginType get ty;
|
2022-02-28 14:38:53 +00:00
|
|
|
|
2022-09-22 05:08:48 +00:00
|
|
|
void dispose() {
|
|
|
|
notifier?.dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-26 08:59:58 +00:00
|
|
|
abstract class PluginNotifier<T> {
|
2022-09-22 05:08:48 +00:00
|
|
|
/// Notify if the plugin get deleted
|
2022-09-26 08:59:58 +00:00
|
|
|
ValueNotifier<T> get isDeleted;
|
2022-09-22 05:08:48 +00:00
|
|
|
|
|
|
|
/// Notify if the [PluginDisplay]'s content was changed
|
|
|
|
ValueNotifier<int> get isDisplayChanged;
|
|
|
|
|
2022-03-02 03:38:22 +00:00
|
|
|
void dispose() {}
|
2022-02-28 14:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
abstract class PluginBuilder {
|
|
|
|
Plugin build(dynamic data);
|
|
|
|
|
2022-03-01 08:05:45 +00:00
|
|
|
String get menuName;
|
2022-02-28 14:38:53 +00:00
|
|
|
|
2022-11-15 03:45:23 +00:00
|
|
|
String get menuIcon;
|
|
|
|
|
2022-02-28 14:38:53 +00:00
|
|
|
PluginType get pluginType;
|
|
|
|
|
2022-10-22 13:57:44 +00:00
|
|
|
ViewDataFormatPB get dataFormatType => ViewDataFormatPB.TreeFormat;
|
2022-08-10 09:59:28 +00:00
|
|
|
|
2022-10-16 08:51:21 +00:00
|
|
|
ViewLayoutTypePB? get layoutType => ViewLayoutTypePB.Document;
|
2022-02-28 14:38:53 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 03:22:39 +00:00
|
|
|
abstract class PluginConfig {
|
2022-03-02 03:38:22 +00:00
|
|
|
// Return false will disable the user to create it. For example, a trash plugin shouldn't be created by the user,
|
2022-03-01 03:22:39 +00:00
|
|
|
bool get creatable => true;
|
|
|
|
}
|
|
|
|
|
2022-09-22 05:08:48 +00:00
|
|
|
abstract class PluginDisplay with NavigationItem {
|
2022-02-28 14:38:53 +00:00
|
|
|
List<NavigationItem> get navigationItems;
|
|
|
|
|
2022-09-22 05:08:48 +00:00
|
|
|
Widget buildWidget(PluginContext context);
|
|
|
|
}
|
|
|
|
|
|
|
|
class PluginContext {
|
|
|
|
// calls when widget of the plugin get deleted
|
2022-09-26 08:59:58 +00:00
|
|
|
final Function(ViewPB, int?) onDeleted;
|
2022-03-02 03:38:22 +00:00
|
|
|
|
2022-09-22 05:08:48 +00:00
|
|
|
PluginContext({required this.onDeleted});
|
2022-02-28 14:38:53 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 03:22:39 +00:00
|
|
|
void registerPlugin({required PluginBuilder builder, PluginConfig? config}) {
|
2022-08-09 02:35:27 +00:00
|
|
|
getIt<PluginSandbox>()
|
|
|
|
.registerPlugin(builder.pluginType, builder, config: config);
|
2022-02-28 14:38:53 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 02:25:21 +00:00
|
|
|
Plugin makePlugin({required PluginType pluginType, dynamic data}) {
|
2022-02-28 14:38:53 +00:00
|
|
|
final plugin = getIt<PluginSandbox>().buildPlugin(pluginType, data);
|
|
|
|
return plugin;
|
|
|
|
}
|
|
|
|
|
2022-03-01 03:22:39 +00:00
|
|
|
List<PluginBuilder> pluginBuilders() {
|
|
|
|
final pluginBuilders = getIt<PluginSandbox>().builders;
|
|
|
|
final pluginConfigs = getIt<PluginSandbox>().pluginConfigs;
|
|
|
|
return pluginBuilders.where(
|
|
|
|
(builder) {
|
|
|
|
final config = pluginConfigs[builder.pluginType]?.creatable;
|
|
|
|
return config ?? true;
|
|
|
|
},
|
|
|
|
).toList();
|
|
|
|
}
|
|
|
|
|
2022-02-28 14:38:53 +00:00
|
|
|
enum FlowyPluginException {
|
|
|
|
invalidData,
|
|
|
|
}
|