document.dart 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. library docuemnt_plugin;
  2. export './src/document_page.dart';
  3. export './src/widget/toolbar/history_button.dart';
  4. export './src/widget/toolbar/toolbar_icon_button.dart';
  5. export './src/widget/toolbar/tool_bar.dart';
  6. import 'package:app_flowy/plugin/plugin.dart';
  7. import 'package:app_flowy/startup/startup.dart';
  8. import 'package:app_flowy/workspace/application/appearance.dart';
  9. import 'package:app_flowy/workspace/application/doc/share_bloc.dart';
  10. import 'package:app_flowy/workspace/application/view/view_listener.dart';
  11. import 'package:app_flowy/workspace/presentation/home/home_stack.dart';
  12. import 'package:app_flowy/workspace/presentation/plugins/widgets/left_bar_item.dart';
  13. import 'package:app_flowy/workspace/presentation/widgets/dialogs.dart';
  14. import 'package:app_flowy/workspace/presentation/widgets/pop_up_action.dart';
  15. import 'package:easy_localization/easy_localization.dart';
  16. import 'package:flowy_infra/notifier.dart';
  17. import 'package:flowy_infra/size.dart';
  18. import 'package:flowy_infra_ui/flowy_infra_ui.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-text-block/entities.pb.dart';
  22. import 'package:flowy_sdk/protobuf/flowy-folder-data-model/view.pb.dart';
  23. import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
  24. import 'package:flutter/material.dart';
  25. import 'package:dartz/dartz.dart' as dartz;
  26. import 'package:flutter_bloc/flutter_bloc.dart';
  27. import 'package:clipboard/clipboard.dart';
  28. import 'package:app_flowy/generated/locale_keys.g.dart';
  29. import 'package:provider/provider.dart';
  30. import 'src/document_page.dart';
  31. class DocumentPluginBuilder extends PluginBuilder {
  32. @override
  33. Plugin build(dynamic data) {
  34. if (data is View) {
  35. return DocumentPlugin(pluginType: pluginType, view: data);
  36. } else {
  37. throw FlowyPluginException.invalidData;
  38. }
  39. }
  40. @override
  41. String get menuName => "Doc";
  42. @override
  43. PluginType get pluginType => DefaultPlugin.quill.type();
  44. @override
  45. ViewDataType get dataType => ViewDataType.TextBlock;
  46. }
  47. class DocumentPlugin implements Plugin {
  48. late View _view;
  49. ViewListener? _listener;
  50. late PluginType _pluginType;
  51. DocumentPlugin({required PluginType pluginType, required View view, Key? key}) : _view = view {
  52. _pluginType = pluginType;
  53. _listener = getIt<ViewListener>(param1: view);
  54. _listener?.start(onViewUpdated: (result) {
  55. result.fold(
  56. (newView) {
  57. _view = newView;
  58. display.notifier!.value = _view.hashCode;
  59. },
  60. (error) {},
  61. );
  62. });
  63. }
  64. @override
  65. void dispose() {
  66. _listener?.stop();
  67. _listener = null;
  68. }
  69. @override
  70. PluginDisplay<int> get display => DocumentPluginDisplay(view: _view);
  71. @override
  72. PluginType get ty => _pluginType;
  73. @override
  74. PluginId get id => _view.id;
  75. }
  76. class DocumentPluginDisplay extends PluginDisplay<int> with NavigationItem {
  77. final PublishNotifier<int> _displayNotifier = PublishNotifier<int>();
  78. final View _view;
  79. DocumentPluginDisplay({required View view, Key? key}) : _view = view;
  80. @override
  81. Widget buildWidget() => DocumentPage(view: _view, key: ValueKey(_view.id));
  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. @override
  89. PublishNotifier<int>? get notifier => _displayNotifier;
  90. }
  91. class DocumentShareButton extends StatelessWidget {
  92. final View view;
  93. DocumentShareButton({Key? key, required this.view}) : super(key: ValueKey(view.hashCode));
  94. @override
  95. Widget build(BuildContext context) {
  96. double buttonWidth = 60;
  97. return BlocProvider(
  98. create: (context) => getIt<DocShareBloc>(param1: view),
  99. child: BlocListener<DocShareBloc, DocShareState>(
  100. listener: (context, state) {
  101. state.map(
  102. initial: (_) {},
  103. loading: (_) {},
  104. finish: (state) {
  105. state.successOrFail.fold(
  106. _handleExportData,
  107. _handleExportError,
  108. );
  109. },
  110. );
  111. },
  112. child: BlocBuilder<DocShareBloc, DocShareState>(
  113. builder: (context, state) {
  114. return ChangeNotifierProvider.value(
  115. value: Provider.of<AppearanceSettingModel>(context, listen: true),
  116. child: Selector<AppearanceSettingModel, Locale>(
  117. selector: (ctx, notifier) => notifier.locale,
  118. builder: (ctx, _, child) => ConstrainedBox(
  119. constraints: const BoxConstraints.expand(
  120. height: 30,
  121. // minWidth: buttonWidth,
  122. width: 100,
  123. ),
  124. child: RoundedTextButton(
  125. title: LocaleKeys.shareAction_buttonText.tr(),
  126. fontSize: 12,
  127. borderRadius: Corners.s6Border,
  128. color: Colors.lightBlue,
  129. onPressed: () => _showActionList(context, Offset(-(buttonWidth / 2), 10)),
  130. ),
  131. ),
  132. ),
  133. );
  134. },
  135. ),
  136. ),
  137. );
  138. }
  139. void _handleExportData(ExportData exportData) {
  140. switch (exportData.exportType) {
  141. case ExportType.Link:
  142. break;
  143. case ExportType.Markdown:
  144. FlutterClipboard.copy(exportData.data).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.read<DocShareBloc>().add(const DocShareEvent.shareMarkdown());
  157. break;
  158. case ShareAction.copyLink:
  159. FlowyAlertDialog(title: LocaleKeys.shareAction_workInProgress.tr()).show(context);
  160. break;
  161. }
  162. });
  163. });
  164. actionList.show(
  165. context,
  166. anchorDirection: AnchorDirection.bottomWithCenterAligned,
  167. anchorOffset: offset,
  168. );
  169. }
  170. }
  171. class ShareActions with ActionList<ShareActionWrapper>, FlowyOverlayDelegate {
  172. final Function(dartz.Option<ShareAction>) onSelected;
  173. final _items = ShareAction.values.map((action) => ShareActionWrapper(action)).toList();
  174. ShareActions({required this.onSelected});
  175. @override
  176. double get maxWidth => 130;
  177. @override
  178. double get itemHeight => 22;
  179. @override
  180. List<ShareActionWrapper> get items => _items;
  181. @override
  182. void Function(dartz.Option<ShareActionWrapper> p1) get selectCallback => (result) {
  183. result.fold(
  184. () => onSelected(dartz.none()),
  185. (wrapper) => onSelected(
  186. dartz.some(wrapper.inner),
  187. ),
  188. );
  189. };
  190. @override
  191. FlowyOverlayDelegate? get delegate => this;
  192. @override
  193. void didRemove() => onSelected(dartz.none());
  194. }
  195. enum ShareAction {
  196. markdown,
  197. copyLink,
  198. }
  199. class ShareActionWrapper extends ActionItem {
  200. final ShareAction inner;
  201. ShareActionWrapper(this.inner);
  202. @override
  203. Widget? get icon => null;
  204. @override
  205. String get name => inner.name;
  206. }
  207. extension QuestionBubbleExtension on ShareAction {
  208. String get name {
  209. switch (this) {
  210. case ShareAction.markdown:
  211. return LocaleKeys.shareAction_markdown.tr();
  212. case ShareAction.copyLink:
  213. return LocaleKeys.shareAction_copyLink.tr();
  214. }
  215. }
  216. }