document.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. bool listenOnViewChanged = false,
  43. Key? key,
  44. }) : notifier = ViewPluginNotifier(view: view) {
  45. _pluginType = pluginType;
  46. _documentAppearanceCubit.fetch();
  47. }
  48. @override
  49. void dispose() {
  50. _documentAppearanceCubit.close();
  51. super.dispose();
  52. }
  53. @override
  54. PluginWidgetBuilder get widgetBuilder {
  55. return DocumentPluginWidgetBuilder(
  56. notifier: notifier,
  57. documentAppearanceCubit: _documentAppearanceCubit,
  58. );
  59. }
  60. @override
  61. PluginType get pluginType => _pluginType;
  62. @override
  63. PluginId get id => notifier.view.id;
  64. }
  65. class DocumentPluginWidgetBuilder extends PluginWidgetBuilder
  66. with NavigationItem {
  67. final ViewPluginNotifier notifier;
  68. ViewPB get view => notifier.view;
  69. int? deletedViewIndex;
  70. DocumentAppearanceCubit documentAppearanceCubit;
  71. DocumentPluginWidgetBuilder({
  72. required this.notifier,
  73. required this.documentAppearanceCubit,
  74. Key? key,
  75. });
  76. @override
  77. EdgeInsets get contentPadding => EdgeInsets.zero;
  78. @override
  79. Widget buildWidget({PluginContext? context}) {
  80. notifier.isDeleted.addListener(() {
  81. notifier.isDeleted.value.fold(() => null, (deletedView) {
  82. if (deletedView.hasIndex()) {
  83. deletedViewIndex = deletedView.index;
  84. }
  85. });
  86. });
  87. return BlocProvider.value(
  88. value: documentAppearanceCubit,
  89. child: BlocBuilder<DocumentAppearanceCubit, DocumentAppearance>(
  90. builder: (_, state) {
  91. return DocumentPage(
  92. view: view,
  93. onDeleted: () => context?.onDeleted(view, deletedViewIndex),
  94. key: ValueKey(view.id),
  95. );
  96. },
  97. ),
  98. );
  99. }
  100. @override
  101. Widget get leftBarItem => ViewLeftBarItem(view: view);
  102. @override
  103. Widget? get rightBarItem {
  104. return Row(
  105. children: [
  106. DocumentShareButton(
  107. key: ValueKey(view.id),
  108. view: view,
  109. ),
  110. const SizedBox(width: 10),
  111. BlocProvider.value(
  112. value: documentAppearanceCubit,
  113. child: const DocumentMoreButton(),
  114. ),
  115. ],
  116. );
  117. }
  118. @override
  119. List<NavigationItem> get navigationItems => [this];
  120. }