document.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. import 'package:shared_preferences/shared_preferences.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 DocumentStyle with ChangeNotifier {
  34. DocumentStyle() {
  35. sync();
  36. }
  37. double _fontSize = 14.0;
  38. double get fontSize => _fontSize;
  39. set fontSize(double fontSize) {
  40. _fontSize = fontSize;
  41. notifyListeners();
  42. }
  43. void sync() async {
  44. final prefs = await SharedPreferences.getInstance();
  45. fontSize = prefs.getDouble('kSelectFontSize') ?? _fontSize;
  46. }
  47. }
  48. class DocumentPlugin extends Plugin<int> {
  49. late PluginType _pluginType;
  50. late final DocumentStyle _documentStyle;
  51. @override
  52. final ViewPluginNotifier notifier;
  53. DocumentPlugin({
  54. required PluginType pluginType,
  55. required ViewPB view,
  56. Key? key,
  57. }) : notifier = ViewPluginNotifier(view: view) {
  58. _pluginType = pluginType;
  59. _documentStyle = DocumentStyle();
  60. }
  61. @override
  62. void dispose() {
  63. _documentStyle.dispose();
  64. super.dispose();
  65. }
  66. @override
  67. PluginDisplay get display {
  68. return DocumentPluginDisplay(
  69. notifier: notifier,
  70. documentStyle: _documentStyle,
  71. );
  72. }
  73. @override
  74. PluginType get ty => _pluginType;
  75. @override
  76. PluginId get id => notifier.view.id;
  77. }
  78. class DocumentPluginDisplay extends PluginDisplay with NavigationItem {
  79. final ViewPluginNotifier notifier;
  80. ViewPB get view => notifier.view;
  81. int? deletedViewIndex;
  82. DocumentStyle documentStyle;
  83. DocumentPluginDisplay({
  84. required this.notifier,
  85. required this.documentStyle,
  86. Key? key,
  87. });
  88. @override
  89. Widget buildWidget(PluginContext context) {
  90. notifier.isDeleted.addListener(() {
  91. notifier.isDeleted.value.fold(() => null, (deletedView) {
  92. if (deletedView.hasIndex()) {
  93. deletedViewIndex = deletedView.index;
  94. }
  95. });
  96. });
  97. return ChangeNotifierProvider.value(
  98. value: documentStyle,
  99. child: DocumentPage(
  100. view: view,
  101. onDeleted: () => context.onDeleted(view, deletedViewIndex),
  102. key: ValueKey(view.id),
  103. ),
  104. );
  105. }
  106. @override
  107. Widget get leftBarItem => ViewLeftBarItem(view: view);
  108. @override
  109. Widget? get rightBarItem {
  110. return Row(
  111. children: [
  112. DocumentShareButton(view: view),
  113. const SizedBox(width: 10),
  114. ChangeNotifierProvider.value(
  115. value: documentStyle,
  116. child: const DocumentMoreButton(),
  117. ),
  118. ],
  119. );
  120. }
  121. @override
  122. List<NavigationItem> get navigationItems => [this];
  123. }
  124. extension QuestionBubbleExtension on ShareAction {
  125. String get name {
  126. switch (this) {
  127. case ShareAction.markdown:
  128. return LocaleKeys.shareAction_markdown.tr();
  129. case ShareAction.copyLink:
  130. return LocaleKeys.shareAction_copyLink.tr();
  131. }
  132. }
  133. }