document.dart 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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?.updatedNotifier.addPublishListener((result) {
  55. result.fold(
  56. (newView) {
  57. _view = newView;
  58. display.notifier!.value = _view.hashCode;
  59. },
  60. (error) {},
  61. );
  62. });
  63. _listener?.start();
  64. }
  65. @override
  66. void dispose() {
  67. _listener?.close();
  68. _listener = null;
  69. }
  70. @override
  71. PluginDisplay<int> get display => DocumentPluginDisplay(view: _view);
  72. @override
  73. PluginType get ty => _pluginType;
  74. @override
  75. PluginId get id => _view.id;
  76. }
  77. class DocumentPluginDisplay extends PluginDisplay<int> with NavigationItem {
  78. final PublishNotifier<int> _displayNotifier = PublishNotifier<int>();
  79. final View _view;
  80. DocumentPluginDisplay({required View view, Key? key}) : _view = view;
  81. @override
  82. Widget buildWidget() => DocumentPage(view: _view, key: ValueKey(_view.id));
  83. @override
  84. Widget get leftBarItem => ViewLeftBarItem(view: _view);
  85. @override
  86. Widget? get rightBarItem => DocumentShareButton(view: _view);
  87. @override
  88. List<NavigationItem> get navigationItems => [this];
  89. @override
  90. PublishNotifier<int>? get notifier => _displayNotifier;
  91. }
  92. class DocumentShareButton extends StatelessWidget {
  93. final View view;
  94. DocumentShareButton({Key? key, required this.view}) : super(key: ValueKey(view.hashCode));
  95. @override
  96. Widget build(BuildContext context) {
  97. double buttonWidth = 60;
  98. return BlocProvider(
  99. create: (context) => getIt<DocShareBloc>(param1: view),
  100. child: BlocListener<DocShareBloc, DocShareState>(
  101. listener: (context, state) {
  102. state.map(
  103. initial: (_) {},
  104. loading: (_) {},
  105. finish: (state) {
  106. state.successOrFail.fold(
  107. _handleExportData,
  108. _handleExportError,
  109. );
  110. },
  111. );
  112. },
  113. child: BlocBuilder<DocShareBloc, DocShareState>(
  114. builder: (context, state) {
  115. return ChangeNotifierProvider.value(
  116. value: Provider.of<AppearanceSettingModel>(context, listen: true),
  117. child: Selector<AppearanceSettingModel, Locale>(
  118. selector: (ctx, notifier) => notifier.locale,
  119. builder: (ctx, _, child) => ConstrainedBox(
  120. constraints: const BoxConstraints.expand(
  121. height: 30,
  122. // minWidth: buttonWidth,
  123. width: 100,
  124. ),
  125. child: RoundedTextButton(
  126. title: LocaleKeys.shareAction_buttonText.tr(),
  127. fontSize: 12,
  128. borderRadius: Corners.s6Border,
  129. color: Colors.lightBlue,
  130. onPressed: () => _showActionList(context, Offset(-(buttonWidth / 2), 10)),
  131. ),
  132. ),
  133. ),
  134. );
  135. },
  136. ),
  137. ),
  138. );
  139. }
  140. void _handleExportData(ExportData exportData) {
  141. switch (exportData.exportType) {
  142. case ExportType.Link:
  143. break;
  144. case ExportType.Markdown:
  145. FlutterClipboard.copy(exportData.data).then((value) => Log.info('copied to clipboard'));
  146. break;
  147. case ExportType.Text:
  148. break;
  149. }
  150. }
  151. void _handleExportError(FlowyError error) {}
  152. void _showActionList(BuildContext context, Offset offset) {
  153. final actionList = ShareActions(onSelected: (result) {
  154. result.fold(() {}, (action) {
  155. switch (action) {
  156. case ShareAction.markdown:
  157. context.read<DocShareBloc>().add(const DocShareEvent.shareMarkdown());
  158. break;
  159. case ShareAction.copyLink:
  160. FlowyAlertDialog(title: LocaleKeys.shareAction_workInProgress.tr()).show(context);
  161. break;
  162. }
  163. });
  164. });
  165. actionList.show(
  166. context,
  167. anchorDirection: AnchorDirection.bottomWithCenterAligned,
  168. anchorOffset: offset,
  169. );
  170. }
  171. }
  172. class ShareActions with ActionList<ShareActionWrapper>, FlowyOverlayDelegate {
  173. final Function(dartz.Option<ShareAction>) onSelected;
  174. final _items = ShareAction.values.map((action) => ShareActionWrapper(action)).toList();
  175. ShareActions({required this.onSelected});
  176. @override
  177. double get maxWidth => 130;
  178. @override
  179. double get itemHeight => 22;
  180. @override
  181. List<ShareActionWrapper> get items => _items;
  182. @override
  183. void Function(dartz.Option<ShareActionWrapper> p1) get selectCallback => (result) {
  184. result.fold(
  185. () => onSelected(dartz.none()),
  186. (wrapper) => onSelected(
  187. dartz.some(wrapper.inner),
  188. ),
  189. );
  190. };
  191. @override
  192. FlowyOverlayDelegate? get delegate => this;
  193. @override
  194. void didRemove() => onSelected(dartz.none());
  195. }
  196. enum ShareAction {
  197. markdown,
  198. copyLink,
  199. }
  200. class ShareActionWrapper extends ActionItem {
  201. final ShareAction inner;
  202. ShareActionWrapper(this.inner);
  203. @override
  204. Widget? get icon => null;
  205. @override
  206. String get name => inner.name;
  207. }
  208. extension QuestionBubbleExtension on ShareAction {
  209. String get name {
  210. switch (this) {
  211. case ShareAction.markdown:
  212. return LocaleKeys.shareAction_markdown.tr();
  213. case ShareAction.copyLink:
  214. return LocaleKeys.shareAction_copyLink.tr();
  215. }
  216. }
  217. }