document.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. PluginDisplay get display {
  54. return DocumentPluginDisplay(
  55. notifier: notifier,
  56. documentAppearanceCubit: _documentAppearanceCubit,
  57. );
  58. }
  59. @override
  60. PluginType get ty => _pluginType;
  61. @override
  62. PluginId get id => notifier.view.id;
  63. }
  64. class DocumentPluginDisplay extends PluginDisplay with NavigationItem {
  65. final ViewPluginNotifier notifier;
  66. ViewPB get view => notifier.view;
  67. int? deletedViewIndex;
  68. DocumentAppearanceCubit documentAppearanceCubit;
  69. DocumentPluginDisplay({
  70. required this.notifier,
  71. required this.documentAppearanceCubit,
  72. Key? key,
  73. });
  74. @override
  75. Widget buildWidget(PluginContext context) {
  76. notifier.isDeleted.addListener(() {
  77. notifier.isDeleted.value.fold(() => null, (deletedView) {
  78. if (deletedView.hasIndex()) {
  79. deletedViewIndex = deletedView.index;
  80. }
  81. });
  82. });
  83. return BlocProvider.value(
  84. value: documentAppearanceCubit,
  85. child: BlocBuilder<DocumentAppearanceCubit, DocumentAppearance>(
  86. builder: (_, state) {
  87. return DocumentPage(
  88. view: view,
  89. onDeleted: () => context.onDeleted(view, deletedViewIndex),
  90. key: ValueKey(view.id),
  91. );
  92. },
  93. ),
  94. );
  95. }
  96. @override
  97. Widget get leftBarItem => ViewLeftBarItem(view: view);
  98. @override
  99. Widget? get rightBarItem {
  100. return Row(
  101. children: [
  102. DocumentShareButton(view: view),
  103. const SizedBox(width: 10),
  104. BlocProvider.value(
  105. value: documentAppearanceCubit,
  106. child: const DocumentMoreButton(),
  107. ),
  108. ],
  109. );
  110. }
  111. @override
  112. List<NavigationItem> get navigationItems => [this];
  113. }