document.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. library document_plugin;
  2. import 'package:appflowy/generated/locale_keys.g.dart';
  3. import 'package:appflowy/plugins/document/document_page.dart';
  4. import 'package:appflowy/plugins/document/presentation/more/cubit/document_appearance_cubit.dart';
  5. import 'package:appflowy/plugins/document/presentation/more/more_button.dart';
  6. import 'package:appflowy/plugins/document/presentation/share/share_button.dart';
  7. import 'package:appflowy/plugins/util.dart';
  8. import 'package:appflowy/startup/plugin/plugin.dart';
  9. import 'package:appflowy/workspace/presentation/home/home_stack.dart';
  10. import 'package:appflowy/workspace/presentation/widgets/left_bar_item.dart';
  11. import 'package:easy_localization/easy_localization.dart';
  12. import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart';
  13. import 'package:flutter/material.dart';
  14. import 'package:flutter_bloc/flutter_bloc.dart';
  15. class DocumentPluginBuilder extends PluginBuilder {
  16. @override
  17. Plugin build(dynamic data) {
  18. if (data is ViewPB) {
  19. return DocumentPlugin(pluginType: pluginType, view: data);
  20. } else {
  21. throw FlowyPluginException.invalidData;
  22. }
  23. }
  24. @override
  25. String get menuName => LocaleKeys.document_menuName.tr();
  26. @override
  27. String get menuIcon => "editor/documents";
  28. @override
  29. PluginType get pluginType => PluginType.editor;
  30. @override
  31. ViewLayoutPB? get layoutType => ViewLayoutPB.Document;
  32. }
  33. class DocumentPlugin extends Plugin<int> {
  34. late PluginType _pluginType;
  35. final DocumentAppearanceCubit _documentAppearanceCubit =
  36. DocumentAppearanceCubit();
  37. @override
  38. final ViewPluginNotifier notifier;
  39. DocumentPlugin({
  40. required PluginType pluginType,
  41. required ViewPB view,
  42. Key? key,
  43. }) : notifier = ViewPluginNotifier(view: view) {
  44. _pluginType = pluginType;
  45. _documentAppearanceCubit.fetch();
  46. }
  47. @override
  48. void dispose() {
  49. _documentAppearanceCubit.close();
  50. super.dispose();
  51. }
  52. @override
  53. PluginWidgetBuilder get widgetBuilder {
  54. return DocumentPluginWidgetBuilder(
  55. notifier: notifier,
  56. documentAppearanceCubit: _documentAppearanceCubit,
  57. );
  58. }
  59. @override
  60. PluginType get pluginType => _pluginType;
  61. @override
  62. PluginId get id => notifier.view.id;
  63. }
  64. class DocumentPluginWidgetBuilder extends PluginWidgetBuilder
  65. with NavigationItem {
  66. final ViewPluginNotifier notifier;
  67. ViewPB get view => notifier.view;
  68. int? deletedViewIndex;
  69. DocumentAppearanceCubit documentAppearanceCubit;
  70. DocumentPluginWidgetBuilder({
  71. required this.notifier,
  72. required this.documentAppearanceCubit,
  73. Key? key,
  74. });
  75. @override
  76. EdgeInsets get contentPadding => EdgeInsets.zero;
  77. @override
  78. Widget buildWidget({PluginContext? context}) {
  79. notifier.isDeleted.addListener(() {
  80. notifier.isDeleted.value.fold(() => null, (deletedView) {
  81. if (deletedView.hasIndex()) {
  82. deletedViewIndex = deletedView.index;
  83. }
  84. });
  85. });
  86. return BlocProvider.value(
  87. value: documentAppearanceCubit,
  88. child: BlocBuilder<DocumentAppearanceCubit, DocumentAppearance>(
  89. builder: (_, state) {
  90. return DocumentPage(
  91. view: view,
  92. onDeleted: () => context?.onDeleted(view, deletedViewIndex),
  93. key: ValueKey(view.id),
  94. );
  95. },
  96. ),
  97. );
  98. }
  99. @override
  100. Widget get leftBarItem => ViewLeftBarItem(view: view);
  101. @override
  102. Widget? get rightBarItem {
  103. return Row(
  104. children: [
  105. DocumentShareButton(view: view),
  106. const SizedBox(width: 10),
  107. BlocProvider.value(
  108. value: documentAppearanceCubit,
  109. child: const DocumentMoreButton(),
  110. ),
  111. ],
  112. );
  113. }
  114. @override
  115. List<NavigationItem> get navigationItems => [this];
  116. }