document.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. library document_plugin;
  2. import 'package:app_flowy/generated/locale_keys.g.dart';
  3. import 'package:app_flowy/plugins/document/document_page.dart';
  4. import 'package:app_flowy/plugins/document/presentation/more/cubit/document_appearance_cubit.dart';
  5. import 'package:app_flowy/plugins/document/presentation/more/more_button.dart';
  6. import 'package:app_flowy/plugins/document/presentation/share/share_button.dart';
  7. import 'package:app_flowy/plugins/util.dart';
  8. import 'package:app_flowy/startup/plugin/plugin.dart';
  9. import 'package:app_flowy/workspace/presentation/home/home_stack.dart';
  10. import 'package:app_flowy/workspace/presentation/widgets/left_bar_item.dart';
  11. import 'package:easy_localization/easy_localization.dart';
  12. import 'package:flowy_sdk/protobuf/flowy-folder/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. ViewDataFormatPB get dataFormatType => ViewDataFormatPB.TreeFormat;
  32. }
  33. class DocumentPlugin extends Plugin<int> {
  34. late PluginType _pluginType;
  35. late final DocumentAppearanceCubit _documentAppearanceCubit;
  36. @override
  37. final ViewPluginNotifier notifier;
  38. DocumentPlugin({
  39. required PluginType pluginType,
  40. required ViewPB view,
  41. Key? key,
  42. }) : notifier = ViewPluginNotifier(view: view) {
  43. _pluginType = pluginType;
  44. _documentAppearanceCubit = DocumentAppearanceCubit();
  45. }
  46. @override
  47. void dispose() {
  48. _documentAppearanceCubit.close();
  49. super.dispose();
  50. }
  51. @override
  52. PluginDisplay get display {
  53. return DocumentPluginDisplay(
  54. notifier: notifier,
  55. documentAppearanceCubit: _documentAppearanceCubit,
  56. );
  57. }
  58. @override
  59. PluginType get ty => _pluginType;
  60. @override
  61. PluginId get id => notifier.view.id;
  62. }
  63. class DocumentPluginDisplay extends PluginDisplay with NavigationItem {
  64. final ViewPluginNotifier notifier;
  65. ViewPB get view => notifier.view;
  66. int? deletedViewIndex;
  67. DocumentAppearanceCubit documentAppearanceCubit;
  68. DocumentPluginDisplay({
  69. required this.notifier,
  70. required this.documentAppearanceCubit,
  71. Key? key,
  72. });
  73. @override
  74. Widget buildWidget(PluginContext context) {
  75. notifier.isDeleted.addListener(() {
  76. notifier.isDeleted.value.fold(() => null, (deletedView) {
  77. if (deletedView.hasIndex()) {
  78. deletedViewIndex = deletedView.index;
  79. }
  80. });
  81. });
  82. return BlocProvider.value(
  83. value: documentAppearanceCubit,
  84. child: BlocBuilder<DocumentAppearanceCubit, DocumentAppearance>(
  85. builder: (_, state) {
  86. return DocumentPage(
  87. view: view,
  88. onDeleted: () => context.onDeleted(view, deletedViewIndex),
  89. key: ValueKey(view.id),
  90. );
  91. },
  92. ),
  93. );
  94. }
  95. @override
  96. Widget get leftBarItem => ViewLeftBarItem(view: view);
  97. @override
  98. Widget? get rightBarItem {
  99. return Row(
  100. children: [
  101. DocumentShareButton(view: view),
  102. const SizedBox(width: 10),
  103. BlocProvider.value(
  104. value: documentAppearanceCubit,
  105. child: const DocumentMoreButton(),
  106. ),
  107. ],
  108. );
  109. }
  110. @override
  111. List<NavigationItem> get navigationItems => [this];
  112. }
  113. extension QuestionBubbleExtension on ShareAction {
  114. String get name {
  115. switch (this) {
  116. case ShareAction.markdown:
  117. return LocaleKeys.shareAction_markdown.tr();
  118. case ShareAction.copyLink:
  119. return LocaleKeys.shareAction_copyLink.tr();
  120. }
  121. }
  122. }