document.dart 6.5 KB

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