document.dart 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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:clipboard/clipboard.dart';
  14. import 'package:dartz/dartz.dart' as dartz;
  15. import 'package:easy_localization/easy_localization.dart';
  16. import 'package:flowy_infra/size.dart';
  17. import 'package:flowy_infra_ui/flowy_infra_ui.dart';
  18. import 'package:flowy_infra_ui/widget/rounded_button.dart';
  19. import 'package:flowy_sdk/log.dart';
  20. import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
  21. import 'package:flowy_sdk/protobuf/flowy-folder/view.pb.dart';
  22. import 'package:flowy_sdk/protobuf/flowy-text-block/entities.pb.dart';
  23. import 'package:flutter/material.dart';
  24. import 'package:flutter_bloc/flutter_bloc.dart';
  25. import 'package:provider/provider.dart';
  26. import 'document_page.dart';
  27. class DocumentPluginBuilder extends PluginBuilder {
  28. @override
  29. Plugin build(dynamic data) {
  30. if (data is ViewPB) {
  31. return DocumentPlugin(pluginType: pluginType, view: data);
  32. } else {
  33. throw FlowyPluginException.invalidData;
  34. }
  35. }
  36. @override
  37. String get menuName => LocaleKeys.document_menuName.tr();
  38. @override
  39. PluginType get pluginType => PluginType.editor;
  40. @override
  41. ViewDataTypePB get dataType => ViewDataTypePB.Text;
  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. double buttonWidth = 60;
  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. // minWidth: buttonWidth,
  120. width: 100,
  121. ),
  122. child: RoundedTextButton(
  123. title: LocaleKeys.shareAction_buttonText.tr(),
  124. fontSize: 12,
  125. borderRadius: Corners.s6Border,
  126. color: Colors.lightBlue,
  127. onPressed: () => _showActionList(
  128. context, Offset(-(buttonWidth / 2), 10)),
  129. ),
  130. ),
  131. ),
  132. );
  133. },
  134. ),
  135. ),
  136. );
  137. }
  138. void _handleExportData(ExportDataPB exportData) {
  139. switch (exportData.exportType) {
  140. case ExportType.Link:
  141. break;
  142. case ExportType.Markdown:
  143. FlutterClipboard.copy(exportData.data)
  144. .then((value) => Log.info('copied to clipboard'));
  145. break;
  146. case ExportType.Text:
  147. break;
  148. }
  149. }
  150. void _handleExportError(FlowyError error) {}
  151. void _showActionList(BuildContext context, Offset offset) {
  152. final actionList = ShareActions(onSelected: (result) {
  153. result.fold(() {}, (action) {
  154. switch (action) {
  155. case ShareAction.markdown:
  156. context
  157. .read<DocShareBloc>()
  158. .add(const DocShareEvent.shareMarkdown());
  159. showMessageToast(
  160. 'Exported to: ${LocaleKeys.notifications_export_path.tr()}');
  161. break;
  162. case ShareAction.copyLink:
  163. NavigatorAlertDialog(
  164. title: LocaleKeys.shareAction_workInProgress.tr())
  165. .show(context);
  166. break;
  167. }
  168. });
  169. });
  170. actionList.show(
  171. context,
  172. anchorDirection: AnchorDirection.bottomWithCenterAligned,
  173. anchorOffset: offset,
  174. );
  175. }
  176. }
  177. class ShareActions with ActionList<ShareActionWrapper>, FlowyOverlayDelegate {
  178. final Function(dartz.Option<ShareAction>) onSelected;
  179. final _items =
  180. ShareAction.values.map((action) => ShareActionWrapper(action)).toList();
  181. ShareActions({required this.onSelected});
  182. @override
  183. double get itemHeight => 22;
  184. @override
  185. List<ShareActionWrapper> get items => _items;
  186. @override
  187. void Function(dartz.Option<ShareActionWrapper> p1) get selectCallback =>
  188. (result) {
  189. result.fold(
  190. () => onSelected(dartz.none()),
  191. (wrapper) => onSelected(
  192. dartz.some(wrapper.inner),
  193. ),
  194. );
  195. };
  196. @override
  197. FlowyOverlayDelegate? get delegate => this;
  198. @override
  199. void didRemove() => onSelected(dartz.none());
  200. }
  201. enum ShareAction {
  202. markdown,
  203. copyLink,
  204. }
  205. class ShareActionWrapper extends ActionItem {
  206. final ShareAction inner;
  207. ShareActionWrapper(this.inner);
  208. @override
  209. Widget? icon(Color iconColor) => null;
  210. @override
  211. String get name => inner.name;
  212. }
  213. extension QuestionBubbleExtension on ShareAction {
  214. String get name {
  215. switch (this) {
  216. case ShareAction.markdown:
  217. return LocaleKeys.shareAction_markdown.tr();
  218. case ShareAction.copyLink:
  219. return LocaleKeys.shareAction_copyLink.tr();
  220. }
  221. }
  222. }