document.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/more_button.dart';
  5. import 'package:app_flowy/plugins/document/presentation/share/share_button.dart';
  6. import 'package:app_flowy/plugins/util.dart';
  7. import 'package:app_flowy/startup/plugin/plugin.dart';
  8. import 'package:app_flowy/workspace/presentation/home/home_stack.dart';
  9. import 'package:app_flowy/workspace/presentation/widgets/left_bar_item.dart';
  10. import 'package:easy_localization/easy_localization.dart';
  11. import 'package:flowy_sdk/protobuf/flowy-folder/view.pb.dart';
  12. import 'package:flutter/material.dart';
  13. import 'package:provider/provider.dart';
  14. class DocumentPluginBuilder extends PluginBuilder {
  15. @override
  16. Plugin build(dynamic data) {
  17. if (data is ViewPB) {
  18. return DocumentPlugin(pluginType: pluginType, view: data);
  19. } else {
  20. throw FlowyPluginException.invalidData;
  21. }
  22. }
  23. @override
  24. String get menuName => LocaleKeys.document_menuName.tr();
  25. @override
  26. String get menuIcon => "editor/documents";
  27. @override
  28. PluginType get pluginType => PluginType.editor;
  29. @override
  30. ViewDataFormatPB get dataFormatType => ViewDataFormatPB.TreeFormat;
  31. }
  32. class DocumentStyle with ChangeNotifier {
  33. DocumentStyle();
  34. double _fontSize = 14.0;
  35. double get fontSize => _fontSize;
  36. set fontSize(double fontSize) {
  37. _fontSize = fontSize;
  38. notifyListeners();
  39. }
  40. }
  41. class DocumentPlugin extends Plugin<int> {
  42. late PluginType _pluginType;
  43. late final DocumentStyle _documentStyle;
  44. @override
  45. final ViewPluginNotifier notifier;
  46. DocumentPlugin({
  47. required PluginType pluginType,
  48. required ViewPB view,
  49. Key? key,
  50. }) : notifier = ViewPluginNotifier(view: view) {
  51. _pluginType = pluginType;
  52. _documentStyle = DocumentStyle();
  53. }
  54. @override
  55. void dispose() {
  56. _documentStyle.dispose();
  57. super.dispose();
  58. }
  59. @override
  60. PluginDisplay get display {
  61. return DocumentPluginDisplay(
  62. notifier: notifier,
  63. documentStyle: _documentStyle,
  64. );
  65. }
  66. @override
  67. PluginType get ty => _pluginType;
  68. @override
  69. PluginId get id => notifier.view.id;
  70. }
  71. class DocumentPluginDisplay extends PluginDisplay with NavigationItem {
  72. final ViewPluginNotifier notifier;
  73. ViewPB get view => notifier.view;
  74. int? deletedViewIndex;
  75. DocumentStyle documentStyle;
  76. DocumentPluginDisplay({
  77. required this.notifier,
  78. required this.documentStyle,
  79. Key? key,
  80. });
  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 ChangeNotifierProvider.value(
  91. value: documentStyle,
  92. child: DocumentPage(
  93. view: view,
  94. onDeleted: () => context.onDeleted(view, deletedViewIndex),
  95. key: ValueKey(view.id),
  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. ChangeNotifierProvider.value(
  108. value: documentStyle,
  109. child: const DocumentMoreButton(),
  110. ),
  111. ],
  112. );
  113. }
  114. @override
  115. List<NavigationItem> get navigationItems => [this];
  116. }
  117. extension QuestionBubbleExtension on ShareAction {
  118. String get name {
  119. switch (this) {
  120. case ShareAction.markdown:
  121. return LocaleKeys.shareAction_markdown.tr();
  122. case ShareAction.copyLink:
  123. return LocaleKeys.shareAction_copyLink.tr();
  124. }
  125. }
  126. }