document.dart 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. library document_plugin;
  2. import 'package:app_flowy/generated/locale_keys.g.dart';
  3. import 'package:app_flowy/plugins/util.dart';
  4. import 'package:app_flowy/startup/plugin/plugin.dart';
  5. import 'package:app_flowy/startup/startup.dart';
  6. import 'package:app_flowy/plugins/document/application/share_bloc.dart';
  7. import 'package:app_flowy/workspace/presentation/home/home_stack.dart';
  8. import 'package:app_flowy/workspace/presentation/home/toast.dart';
  9. import 'package:app_flowy/workspace/presentation/widgets/left_bar_item.dart';
  10. import 'package:app_flowy/workspace/presentation/widgets/dialogs.dart';
  11. import 'package:app_flowy/workspace/presentation/widgets/pop_up_action.dart';
  12. import 'package:appflowy_popover/appflowy_popover.dart';
  13. import 'package:clipboard/clipboard.dart';
  14. import 'package:easy_localization/easy_localization.dart';
  15. import 'package:file_picker/file_picker.dart';
  16. import 'package:flowy_infra_ui/widget/rounded_button.dart';
  17. import 'package:flowy_sdk/log.dart';
  18. import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
  19. import 'package:flowy_sdk/protobuf/flowy-folder/view.pb.dart';
  20. import 'package:flowy_sdk/protobuf/flowy-document/entities.pb.dart';
  21. import 'package:flutter/material.dart';
  22. import 'package:flutter_bloc/flutter_bloc.dart';
  23. import 'document_page.dart';
  24. class DocumentPluginBuilder extends PluginBuilder {
  25. @override
  26. Plugin build(dynamic data) {
  27. if (data is ViewPB) {
  28. return DocumentPlugin(pluginType: pluginType, view: data);
  29. } else {
  30. throw FlowyPluginException.invalidData;
  31. }
  32. }
  33. @override
  34. String get menuName => LocaleKeys.document_menuName.tr();
  35. @override
  36. String get menuIcon => "editor/documents";
  37. @override
  38. PluginType get pluginType => PluginType.editor;
  39. @override
  40. ViewDataFormatPB get dataFormatType => ViewDataFormatPB.TreeFormat;
  41. }
  42. class DocumentPlugin extends Plugin<int> {
  43. late PluginType _pluginType;
  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. }
  53. @override
  54. PluginDisplay get display => DocumentPluginDisplay(notifier: notifier);
  55. @override
  56. PluginType get ty => _pluginType;
  57. @override
  58. PluginId get id => notifier.view.id;
  59. }
  60. class DocumentPluginDisplay extends PluginDisplay with NavigationItem {
  61. final ViewPluginNotifier notifier;
  62. ViewPB get view => notifier.view;
  63. int? deletedViewIndex;
  64. DocumentPluginDisplay({required this.notifier, Key? key});
  65. @override
  66. Widget buildWidget(PluginContext context) {
  67. notifier.isDeleted.addListener(() {
  68. notifier.isDeleted.value.fold(() => null, (deletedView) {
  69. if (deletedView.hasIndex()) {
  70. deletedViewIndex = deletedView.index;
  71. }
  72. });
  73. });
  74. return DocumentPage(
  75. view: view,
  76. onDeleted: () => context.onDeleted(view, deletedViewIndex),
  77. key: ValueKey(view.id),
  78. );
  79. }
  80. @override
  81. Widget get leftBarItem => ViewLeftBarItem(view: view);
  82. @override
  83. Widget? get rightBarItem => DocumentShareButton(view: view);
  84. @override
  85. List<NavigationItem> get navigationItems => [this];
  86. }
  87. class DocumentShareButton extends StatelessWidget {
  88. final ViewPB view;
  89. DocumentShareButton({Key? key, required this.view})
  90. : super(key: ValueKey(view.hashCode));
  91. @override
  92. Widget build(BuildContext context) {
  93. return BlocProvider(
  94. create: (context) => getIt<DocShareBloc>(param1: view),
  95. child: BlocListener<DocShareBloc, DocShareState>(
  96. listener: (context, state) {
  97. state.map(
  98. initial: (_) {},
  99. loading: (_) {},
  100. finish: (state) {
  101. state.successOrFail.fold(
  102. _handleExportData,
  103. _handleExportError,
  104. );
  105. },
  106. );
  107. },
  108. child: BlocBuilder<DocShareBloc, DocShareState>(
  109. builder: (context, state) => ConstrainedBox(
  110. constraints: const BoxConstraints.expand(
  111. height: 30,
  112. width: 100,
  113. ),
  114. child: ShareActionList(view: view),
  115. ),
  116. ),
  117. ),
  118. );
  119. }
  120. void _handleExportData(ExportDataPB exportData) {
  121. switch (exportData.exportType) {
  122. case ExportType.Link:
  123. break;
  124. case ExportType.Markdown:
  125. FlutterClipboard.copy(exportData.data)
  126. .then((value) => Log.info('copied to clipboard'));
  127. break;
  128. case ExportType.Text:
  129. break;
  130. }
  131. }
  132. void _handleExportError(FlowyError error) {}
  133. }
  134. class ShareActionList extends StatelessWidget {
  135. const ShareActionList({
  136. Key? key,
  137. required this.view,
  138. }) : super(key: key);
  139. final ViewPB view;
  140. @override
  141. Widget build(BuildContext context) {
  142. final docShareBloc = context.read<DocShareBloc>();
  143. return PopoverActionList<ShareActionWrapper>(
  144. direction: PopoverDirection.bottomWithCenterAligned,
  145. actions: ShareAction.values
  146. .map((action) => ShareActionWrapper(action))
  147. .toList(),
  148. buildChild: (controller) {
  149. return RoundedTextButton(
  150. title: LocaleKeys.shareAction_buttonText.tr(),
  151. onPressed: () => controller.show(),
  152. );
  153. },
  154. onSelected: (action, controller) async {
  155. switch (action.inner) {
  156. case ShareAction.markdown:
  157. final exportPath = await FilePicker.platform.saveFile(
  158. dialogTitle: '',
  159. fileName: '${view.name}.md',
  160. );
  161. if (exportPath != null) {
  162. docShareBloc.add(DocShareEvent.shareMarkdown(exportPath));
  163. showMessageToast('Exported to: $exportPath');
  164. }
  165. break;
  166. case ShareAction.copyLink:
  167. NavigatorAlertDialog(
  168. title: LocaleKeys.shareAction_workInProgress.tr())
  169. .show(context);
  170. break;
  171. }
  172. controller.close();
  173. },
  174. );
  175. }
  176. }
  177. enum ShareAction {
  178. markdown,
  179. copyLink,
  180. }
  181. class ShareActionWrapper extends ActionCell {
  182. final ShareAction inner;
  183. ShareActionWrapper(this.inner);
  184. @override
  185. Widget? icon(Color iconColor) => null;
  186. @override
  187. String get name => inner.name;
  188. }
  189. extension QuestionBubbleExtension on ShareAction {
  190. String get name {
  191. switch (this) {
  192. case ShareAction.markdown:
  193. return LocaleKeys.shareAction_markdown.tr();
  194. case ShareAction.copyLink:
  195. return LocaleKeys.shareAction_copyLink.tr();
  196. }
  197. }
  198. }