document.dart 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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:flowy_infra/size.dart';
  17. import 'package:flowy_infra/theme.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-document/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. 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) {
  111. return ChangeNotifierProvider.value(
  112. value: Provider.of<AppearanceSetting>(context, listen: true),
  113. child: Selector<AppearanceSetting, Locale>(
  114. selector: (ctx, notifier) => notifier.locale,
  115. builder: (ctx, _, child) => ConstrainedBox(
  116. constraints: const BoxConstraints.expand(
  117. height: 30,
  118. width: 100,
  119. ),
  120. child: const ShareActionList(),
  121. ),
  122. ),
  123. );
  124. },
  125. ),
  126. ),
  127. );
  128. }
  129. void _handleExportData(ExportDataPB exportData) {
  130. switch (exportData.exportType) {
  131. case ExportType.Link:
  132. break;
  133. case ExportType.Markdown:
  134. FlutterClipboard.copy(exportData.data)
  135. .then((value) => Log.info('copied to clipboard'));
  136. break;
  137. case ExportType.Text:
  138. break;
  139. }
  140. }
  141. void _handleExportError(FlowyError error) {}
  142. }
  143. class ShareActionList extends StatelessWidget {
  144. const ShareActionList({Key? key}) : super(key: key);
  145. @override
  146. Widget build(BuildContext context) {
  147. final theme = context.watch<AppTheme>();
  148. return PopoverActionList<ShareActionWrapper>(
  149. direction: PopoverDirection.bottomWithCenterAligned,
  150. actions: ShareAction.values
  151. .map((action) => ShareActionWrapper(action))
  152. .toList(),
  153. buildChild: (controller) {
  154. return RoundedTextButton(
  155. title: LocaleKeys.shareAction_buttonText.tr(),
  156. fontSize: 12,
  157. borderRadius: Corners.s6Border,
  158. color: theme.main1,
  159. onPressed: () => controller.show(),
  160. );
  161. },
  162. onSelected: (action, controller) {
  163. switch (action.inner) {
  164. case ShareAction.markdown:
  165. context
  166. .read<DocShareBloc>()
  167. .add(const DocShareEvent.shareMarkdown());
  168. showMessageToast(
  169. 'Exported to: ${LocaleKeys.notifications_export_path.tr()}');
  170. break;
  171. case ShareAction.copyLink:
  172. NavigatorAlertDialog(
  173. title: LocaleKeys.shareAction_workInProgress.tr())
  174. .show(context);
  175. break;
  176. }
  177. controller.close();
  178. },
  179. );
  180. }
  181. }
  182. enum ShareAction {
  183. markdown,
  184. copyLink,
  185. }
  186. class ShareActionWrapper extends ActionCell {
  187. final ShareAction inner;
  188. ShareActionWrapper(this.inner);
  189. @override
  190. Widget? icon(Color iconColor) => null;
  191. @override
  192. String get name => inner.name;
  193. }
  194. extension QuestionBubbleExtension on ShareAction {
  195. String get name {
  196. switch (this) {
  197. case ShareAction.markdown:
  198. return LocaleKeys.shareAction_markdown.tr();
  199. case ShareAction.copyLink:
  200. return LocaleKeys.shareAction_copyLink.tr();
  201. }
  202. }
  203. }