AppFlowy/frontend/appflowy_flutter/lib/plugins/document/document.dart

177 lines
5.1 KiB
Dart
Raw Normal View History

2022-08-10 09:59:28 +00:00
library document_plugin;
2022-03-01 08:05:45 +00:00
import 'package:appflowy/generated/flowy_svgs.g.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/plugins/document/application/document_appearance_cubit.dart';
import 'package:appflowy/plugins/document/document_page.dart';
import 'package:appflowy/plugins/document/presentation/document_collaborators.dart';
import 'package:appflowy/plugins/shared/share/share_button.dart';
import 'package:appflowy/plugins/util.dart';
import 'package:appflowy/shared/feature_flags.dart';
import 'package:appflowy/startup/plugin/plugin.dart';
import 'package:appflowy/workspace/application/view_info/view_info_bloc.dart';
import 'package:appflowy/workspace/presentation/home/home_stack.dart';
import 'package:appflowy/workspace/presentation/widgets/favorite_button.dart';
import 'package:appflowy/workspace/presentation/widgets/more_view_actions/more_view_actions.dart';
import 'package:appflowy/workspace/presentation/widgets/tab_bar_item.dart';
import 'package:appflowy/workspace/presentation/widgets/view_title_bar.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:easy_localization/easy_localization.dart';
2023-12-10 11:44:37 +00:00
import 'package:flowy_infra_ui/flowy_infra_ui.dart';
import 'package:flutter/material.dart';
2022-11-28 10:02:05 +00:00
import 'package:flutter_bloc/flutter_bloc.dart';
2022-05-17 18:25:35 +00:00
2022-03-01 03:22:39 +00:00
class DocumentPluginBuilder extends PluginBuilder {
2022-02-28 14:38:53 +00:00
@override
Plugin build(dynamic data) {
2022-07-19 06:11:29 +00:00
if (data is ViewPB) {
2022-02-28 14:38:53 +00:00
return DocumentPlugin(pluginType: pluginType, view: data);
}
throw FlowyPluginException.invalidData;
2022-02-28 14:38:53 +00:00
}
@override
2022-05-17 18:25:35 +00:00
String get menuName => LocaleKeys.document_menuName.tr();
2022-02-28 14:38:53 +00:00
@override
FlowySvgData get icon => FlowySvgs.icon_document_s;
2022-02-28 14:38:53 +00:00
@override
PluginType get pluginType => PluginType.document;
2022-02-28 14:38:53 +00:00
@override
ViewLayoutPB get layoutType => ViewLayoutPB.Document;
2022-02-28 14:38:53 +00:00
}
class DocumentPlugin extends Plugin {
DocumentPlugin({
required ViewPB view,
required PluginType pluginType,
this.initialSelection,
}) : notifier = ViewPluginNotifier(view: view) {
_pluginType = pluginType;
}
late PluginType _pluginType;
late final ViewInfoBloc _viewInfoBloc;
@override
final ViewPluginNotifier notifier;
final Selection? initialSelection;
@override
PluginWidgetBuilder get widgetBuilder => DocumentPluginWidgetBuilder(
bloc: _viewInfoBloc,
notifier: notifier,
initialSelection: initialSelection,
);
2021-10-10 07:58:57 +00:00
@override
PluginType get pluginType => _pluginType;
2022-02-28 14:38:53 +00:00
@override
PluginId get id => notifier.view.id;
@override
void init() {
_viewInfoBloc = ViewInfoBloc(view: notifier.view)
..add(const ViewInfoEvent.started());
}
@override
void dispose() {
_viewInfoBloc.close();
notifier.dispose();
}
2022-02-28 14:38:53 +00:00
}
class DocumentPluginWidgetBuilder extends PluginWidgetBuilder
with NavigationItem {
DocumentPluginWidgetBuilder({
required this.bloc,
required this.notifier,
this.initialSelection,
});
2021-10-10 07:58:57 +00:00
final ViewInfoBloc bloc;
final ViewPluginNotifier notifier;
ViewPB get view => notifier.view;
int? deletedViewIndex;
final Selection? initialSelection;
@override
EdgeInsets get contentPadding => EdgeInsets.zero;
2021-10-10 07:58:57 +00:00
@override
Widget buildWidget({
required PluginContext context,
required bool shrinkWrap,
Map<String, dynamic>? data,
}) {
notifier.isDeleted.addListener(() {
final deletedView = notifier.isDeleted.value;
if (deletedView != null && deletedView.hasIndex()) {
deletedViewIndex = deletedView.index;
}
});
return BlocProvider<ViewInfoBloc>.value(
value: bloc,
child: BlocBuilder<DocumentAppearanceCubit, DocumentAppearance>(
builder: (_, state) => DocumentPage(
key: ValueKey(view.id),
view: view,
onDeleted: () => context.onDeleted?.call(view, deletedViewIndex),
initialSelection: initialSelection,
),
),
);
}
@override
Widget get leftBarItem => ViewTitleBar(key: ValueKey(view.id), view: view);
@override
Widget tabBarItem(String pluginId) => ViewTabBarItem(view: notifier.view);
@override
Widget? get rightBarItem {
return BlocProvider<ViewInfoBloc>.value(
value: bloc,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
...FeatureFlag.syncDocument.isOn
? [
DocumentCollaborators(
key: ValueKey('collaborators_${view.id}'),
width: 120,
height: 32,
view: view,
),
const HSpace(16),
]
: [const HSpace(8)],
ShareButton(
key: ValueKey('share_button_${view.id}'),
view: view,
),
const HSpace(10),
ViewFavoriteButton(
key: ValueKey('favorite_button_${view.id}'),
view: view,
),
const HSpace(4),
MoreViewActions(view: view),
],
),
);
}
2021-07-24 14:27:24 +00:00
@override
List<NavigationItem> get navigationItems => [this];
}