diff --git a/frontend/app_flowy/lib/plugin/plugin.dart b/frontend/app_flowy/lib/plugin/plugin.dart index a949d76985..b4b8ad9fd1 100644 --- a/frontend/app_flowy/lib/plugin/plugin.dart +++ b/frontend/app_flowy/lib/plugin/plugin.dart @@ -17,8 +17,6 @@ abstract class Plugin { String get pluginId; - bool get enable; - void dispose(); PluginDisplay get display; @@ -27,13 +25,17 @@ abstract class Plugin { abstract class PluginBuilder { Plugin build(dynamic data); - String get pluginName; + String get name; PluginType get pluginType; ViewDataType get dataType => ViewDataType.PlainText; } +abstract class PluginConfig { + bool get creatable => true; +} + abstract class PluginDisplay with NavigationItem { @override Widget get leftBarItem; @@ -46,8 +48,8 @@ abstract class PluginDisplay with NavigationItem { Widget buildWidget(); } -void registerPlugin({required PluginBuilder builder}) { - getIt().registerPlugin(builder.pluginType, builder); +void registerPlugin({required PluginBuilder builder, PluginConfig? config}) { + getIt().registerPlugin(builder.pluginType, builder, config: config); } Plugin makePlugin({required PluginType pluginType, dynamic data}) { @@ -55,6 +57,18 @@ Plugin makePlugin({required PluginType pluginType, dynamic data}) { return plugin; } +List pluginBuilders() { + final pluginBuilders = getIt().builders; + final pluginConfigs = getIt().pluginConfigs; + + return pluginBuilders.where( + (builder) { + final config = pluginConfigs[builder.pluginType]?.creatable; + return config ?? true; + }, + ).toList(); +} + enum FlowyPluginException { invalidData, } diff --git a/frontend/app_flowy/lib/plugin/src/sandbox.dart b/frontend/app_flowy/lib/plugin/src/sandbox.dart index cfaecb605e..5f1d63c287 100644 --- a/frontend/app_flowy/lib/plugin/src/sandbox.dart +++ b/frontend/app_flowy/lib/plugin/src/sandbox.dart @@ -6,7 +6,8 @@ import '../plugin.dart'; import 'runner.dart'; class PluginSandbox { - final LinkedHashMap _pluginMap = LinkedHashMap(); + final LinkedHashMap _pluginBuilders = LinkedHashMap(); + final Map _pluginConfigs = {}; late PluginRunner pluginRunner; PluginSandbox() { @@ -14,7 +15,7 @@ class PluginSandbox { } int indexOf(PluginType pluginType) { - final index = _pluginMap.keys.toList().indexWhere((ty) => ty == pluginType); + final index = _pluginBuilders.keys.toList().indexWhere((ty) => ty == pluginType); if (index == -1) { throw PlatformException(code: '-1', message: "Can't find the flowy plugin type: $pluginType"); } @@ -22,18 +23,24 @@ class PluginSandbox { } Plugin buildPlugin(PluginType pluginType, dynamic data) { - final plugin = _pluginMap[pluginType]!.build(data); + final plugin = _pluginBuilders[pluginType]!.build(data); return plugin; } - void registerPlugin(PluginType pluginType, PluginBuilder builder) { - if (_pluginMap.containsKey(pluginType)) { + void registerPlugin(PluginType pluginType, PluginBuilder builder, {PluginConfig? config}) { + if (_pluginBuilders.containsKey(pluginType)) { throw PlatformException(code: '-1', message: "$pluginType was registered before"); } - _pluginMap[pluginType] = builder; + _pluginBuilders[pluginType] = builder; + + if (config != null) { + _pluginConfigs[pluginType] = config; + } } - List get supportPluginTypes => _pluginMap.keys.toList(); + List get supportPluginTypes => _pluginBuilders.keys.toList(); - List get builders => _pluginMap.values.toList(); + List get builders => _pluginBuilders.values.toList(); + + Map get pluginConfigs => _pluginConfigs; } diff --git a/frontend/app_flowy/lib/startup/tasks/load_plugin.dart b/frontend/app_flowy/lib/startup/tasks/load_plugin.dart index 0542cbda29..5899a109b5 100644 --- a/frontend/app_flowy/lib/startup/tasks/load_plugin.dart +++ b/frontend/app_flowy/lib/startup/tasks/load_plugin.dart @@ -23,18 +23,14 @@ extension FlowyDefaultPluginExt on DefaultPlugin { } } -bool isDefaultPlugin(PluginType pluginType) { - return DefaultPlugin.values.map((e) => e.type()).contains(pluginType); -} - class PluginLoadTask extends LaunchTask { @override LaunchTaskType get type => LaunchTaskType.dataProcessing; @override Future initialize(LaunchContext context) async { - registerPlugin(builder: BlankPluginBuilder()); - registerPlugin(builder: TrashPluginBuilder()); + registerPlugin(builder: BlankPluginBuilder(), config: BlankPluginConfig()); + registerPlugin(builder: TrashPluginBuilder(), config: TrashPluginConfig()); registerPlugin(builder: DocumentPluginBuilder()); } } diff --git a/frontend/app_flowy/lib/workspace/application/app/app_bloc.dart b/frontend/app_flowy/lib/workspace/application/app/app_bloc.dart index 49539cb2d0..2f9cfa355c 100644 --- a/frontend/app_flowy/lib/workspace/application/app/app_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/app/app_bloc.dart @@ -22,7 +22,12 @@ class AppBloc extends Bloc { ); await _fetchViews(emit); }, createView: (CreateView value) async { - final viewOrFailed = await repo.createView(name: value.name, desc: value.desc, dataType: value.dataType); + final viewOrFailed = await repo.createView( + name: value.name, + desc: value.desc, + dataType: value.dataType, + pluginType: value.pluginType, + ); viewOrFailed.fold( (view) => emit(state.copyWith( latestCreatedView: view, @@ -96,7 +101,12 @@ class AppBloc extends Bloc { @freezed class AppEvent with _$AppEvent { const factory AppEvent.initial() = Initial; - const factory AppEvent.createView(String name, String desc, PluginDataType dataType) = CreateView; + const factory AppEvent.createView( + String name, + String desc, + PluginDataType dataType, + PluginType pluginType, + ) = CreateView; const factory AppEvent.delete() = Delete; const factory AppEvent.rename(String newName) = Rename; const factory AppEvent.didReceiveViews(List views) = ReceiveViews; diff --git a/frontend/app_flowy/lib/workspace/infrastructure/repos/app_repo.dart b/frontend/app_flowy/lib/workspace/infrastructure/repos/app_repo.dart index f443605365..d060a32b05 100644 --- a/frontend/app_flowy/lib/workspace/infrastructure/repos/app_repo.dart +++ b/frontend/app_flowy/lib/workspace/infrastructure/repos/app_repo.dart @@ -28,12 +28,14 @@ class AppRepository { required String name, required String desc, required PluginDataType dataType, + required PluginType pluginType, }) { final request = CreateViewPayload.create() ..belongToId = appId ..name = name ..desc = desc - ..dataType = dataType; + ..dataType = dataType + ..pluginType = pluginType; return FolderEventCreateView(request).send(); } diff --git a/frontend/app_flowy/lib/workspace/presentation/stack_page/blank/blank_page.dart b/frontend/app_flowy/lib/workspace/presentation/stack_page/blank/blank_page.dart index 69b8e0b9fd..05148c34cf 100644 --- a/frontend/app_flowy/lib/workspace/presentation/stack_page/blank/blank_page.dart +++ b/frontend/app_flowy/lib/workspace/presentation/stack_page/blank/blank_page.dart @@ -14,12 +14,17 @@ class BlankPluginBuilder extends PluginBuilder { } @override - String get pluginName => "Blank"; + String get name => "Blank"; @override PluginType get pluginType => DefaultPlugin.blank.type(); } +class BlankPluginConfig implements PluginConfig { + @override + bool get creatable => false; +} + class BlankPagePlugin implements Plugin { final PluginType _pluginType; BlankPagePlugin({ @@ -32,9 +37,6 @@ class BlankPagePlugin implements Plugin { @override PluginDisplay get display => BlankPagePluginDisplay(); - @override - bool get enable => true; - @override String get pluginId => "BlankStack"; diff --git a/frontend/app_flowy/lib/workspace/presentation/stack_page/doc/doc_stack_page.dart b/frontend/app_flowy/lib/workspace/presentation/stack_page/doc/doc_stack_page.dart index 1c4f9711e3..74280fa67f 100644 --- a/frontend/app_flowy/lib/workspace/presentation/stack_page/doc/doc_stack_page.dart +++ b/frontend/app_flowy/lib/workspace/presentation/stack_page/doc/doc_stack_page.dart @@ -25,7 +25,7 @@ import 'package:provider/provider.dart'; import 'document_page.dart'; -class DocumentPluginBuilder implements PluginBuilder { +class DocumentPluginBuilder extends PluginBuilder { @override Plugin build(dynamic data) { if (data is View) { @@ -36,7 +36,7 @@ class DocumentPluginBuilder implements PluginBuilder { } @override - String get pluginName => "Doc"; + String get name => "Doc"; @override PluginType get pluginType => DefaultPlugin.quillEditor.type(); @@ -74,9 +74,6 @@ class DocumentPlugin implements Plugin { @override PluginDisplay get display => DocumentPluginDisplay(view: _view); - @override - bool get enable => true; - @override PluginType get pluginType => _pluginType; diff --git a/frontend/app_flowy/lib/workspace/presentation/stack_page/trash/trash_page.dart b/frontend/app_flowy/lib/workspace/presentation/stack_page/trash/trash_page.dart index e2c71f1cda..7fcf90da5f 100644 --- a/frontend/app_flowy/lib/workspace/presentation/stack_page/trash/trash_page.dart +++ b/frontend/app_flowy/lib/workspace/presentation/stack_page/trash/trash_page.dart @@ -28,12 +28,17 @@ class TrashPluginBuilder extends PluginBuilder { } @override - String get pluginName => "Trash"; + String get name => "Trash"; @override PluginType get pluginType => DefaultPlugin.trash.type(); } +class TrashPluginConfig implements PluginConfig { + @override + bool get creatable => false; +} + class TrashPlugin implements Plugin { final PluginType _pluginType; @@ -45,9 +50,6 @@ class TrashPlugin implements Plugin { @override PluginDisplay get display => TrashPluginDisplay(); - @override - bool get enable => true; - @override String get pluginId => "TrashStack"; diff --git a/frontend/app_flowy/lib/workspace/presentation/widgets/menu/widget/app/header/add_button.dart b/frontend/app_flowy/lib/workspace/presentation/widgets/menu/widget/app/header/add_button.dart index 90dc29d6c7..4b7a00c21f 100644 --- a/frontend/app_flowy/lib/workspace/presentation/widgets/menu/widget/app/header/add_button.dart +++ b/frontend/app_flowy/lib/workspace/presentation/widgets/menu/widget/app/header/add_button.dart @@ -44,12 +44,7 @@ class ActionList { const ActionList({required this.anchorContext, required this.onSelected}); void show(BuildContext buildContext) { - final items = getIt() - .builders - .where( - (builder) => !isDefaultPlugin(builder.pluginType), - ) - .map( + final items = pluginBuilders().map( (pluginBuilder) { return CreateItem( pluginBuilder: pluginBuilder, @@ -94,7 +89,7 @@ class CreateItem extends StatelessWidget { return GestureDetector( onTap: () => onSelected(pluginBuilder), child: FlowyText.medium( - pluginBuilder.pluginName, + pluginBuilder.name, color: theme.textColor, fontSize: 12, ).padding(horizontal: 10, vertical: 6), diff --git a/frontend/app_flowy/lib/workspace/presentation/widgets/menu/widget/app/header/header.dart b/frontend/app_flowy/lib/workspace/presentation/widgets/menu/widget/app/header/header.dart index 565b6af717..53e9720f18 100644 --- a/frontend/app_flowy/lib/workspace/presentation/widgets/menu/widget/app/header/header.dart +++ b/frontend/app_flowy/lib/workspace/presentation/widgets/menu/widget/app/header/header.dart @@ -107,6 +107,7 @@ class MenuAppHeader extends StatelessWidget { LocaleKeys.menuAppHeader_defaultNewPageName.tr(), "", pluginBuilder.dataType, + pluginBuilder.pluginType, )); }, ).padding(right: MenuAppSizes.headerPadding), diff --git a/shared-lib/flowy-folder-data-model/src/entities/view.rs b/shared-lib/flowy-folder-data-model/src/entities/view.rs index 8a90f0fb9f..3af0943dc0 100644 --- a/shared-lib/flowy-folder-data-model/src/entities/view.rs +++ b/shared-lib/flowy-folder-data-model/src/entities/view.rs @@ -44,9 +44,11 @@ pub struct View { pub create_time: i64, #[pb(index = 10)] + #[serde(default)] pub ext_data: String, #[pb(index = 11)] + #[serde(default)] pub thumbnail: String, #[pb(index = 12)]