document.dart 6.5 KB

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