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