document.dart 7.0 KB

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