document.dart 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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/startup/tasks/load_plugin.dart';
  9. import 'package:app_flowy/workspace/application/appearance.dart';
  10. import 'package:app_flowy/workspace/application/doc/share_bloc.dart';
  11. import 'package:app_flowy/workspace/infrastructure/repos/view_repo.dart';
  12. import 'package:app_flowy/workspace/presentation/home/home_stack.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/size.dart';
  17. import 'package:flowy_infra/theme.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-folder-data-model/share.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.quillEditor.type();
  44. @override
  45. ViewDataType get dataType => ViewDataType.RichText;
  46. }
  47. class DocumentPlugin implements Plugin {
  48. late View _view;
  49. ViewListener? _listener;
  50. final ValueNotifier<int> _displayNotifier = ValueNotifier<int>(0);
  51. late PluginType _pluginType;
  52. DocumentPlugin({required PluginType pluginType, required View view, Key? key}) : _view = view {
  53. _pluginType = pluginType;
  54. _listener = getIt<ViewListener>(param1: view);
  55. _listener?.updatedNotifier.addPublishListener((result) {
  56. result.fold(
  57. (newView) {
  58. _view = newView;
  59. _displayNotifier.value = _view.hashCode;
  60. },
  61. (error) {},
  62. );
  63. });
  64. _listener?.start();
  65. }
  66. @override
  67. void dispose() {
  68. _listener?.close();
  69. _listener = null;
  70. }
  71. @override
  72. PluginDisplay get pluginDisplay => DocumentPluginDisplay(view: _view);
  73. @override
  74. PluginType get pluginType => _pluginType;
  75. @override
  76. PluginId get pluginId => _view.id;
  77. @override
  78. ChangeNotifier? get displayNotifier => _displayNotifier;
  79. }
  80. class DocumentPluginDisplay extends PluginDisplay {
  81. final View _view;
  82. DocumentPluginDisplay({required View view, Key? key}) : _view = view;
  83. @override
  84. Widget buildWidget() => DocumentPage(view: _view, key: ValueKey(_view.id));
  85. @override
  86. Widget get leftBarItem => DocumentLeftBarItem(view: _view);
  87. @override
  88. Widget? get rightBarItem => DocumentShareButton(view: _view);
  89. @override
  90. List<NavigationItem> get navigationItems => _makeNavigationItems();
  91. List<NavigationItem> _makeNavigationItems() {
  92. return [
  93. this,
  94. ];
  95. }
  96. }
  97. class DocumentLeftBarItem extends StatefulWidget {
  98. final View view;
  99. DocumentLeftBarItem({required this.view, Key? key}) : super(key: ValueKey(view.hashCode));
  100. @override
  101. State<DocumentLeftBarItem> createState() => _DocumentLeftBarItemState();
  102. }
  103. class _DocumentLeftBarItemState extends State<DocumentLeftBarItem> {
  104. final _controller = TextEditingController();
  105. final _focusNode = FocusNode();
  106. late ViewRepository repo;
  107. @override
  108. void initState() {
  109. repo = ViewRepository(view: widget.view);
  110. _focusNode.addListener(_handleFocusChanged);
  111. super.initState();
  112. }
  113. @override
  114. void dispose() {
  115. _controller.dispose();
  116. _focusNode.dispose();
  117. super.dispose();
  118. }
  119. @override
  120. Widget build(BuildContext context) {
  121. _controller.text = widget.view.name;
  122. final theme = context.watch<AppTheme>();
  123. return IntrinsicWidth(
  124. key: ValueKey(_controller.text),
  125. child: TextField(
  126. controller: _controller,
  127. focusNode: _focusNode,
  128. scrollPadding: EdgeInsets.zero,
  129. decoration: const InputDecoration(
  130. contentPadding: EdgeInsets.zero,
  131. border: InputBorder.none,
  132. isDense: true,
  133. ),
  134. style: TextStyle(
  135. color: theme.textColor,
  136. fontSize: 14,
  137. fontWeight: FontWeight.w500,
  138. overflow: TextOverflow.ellipsis,
  139. ),
  140. // cursorColor: widget.cursorColor,
  141. // obscureText: widget.enableObscure,
  142. ),
  143. );
  144. }
  145. void _handleFocusChanged() {
  146. if (_controller.text.isEmpty) {
  147. _controller.text = widget.view.name;
  148. return;
  149. }
  150. if (_controller.text != widget.view.name) {
  151. repo.updateView(name: _controller.text);
  152. }
  153. }
  154. }
  155. class DocumentShareButton extends StatelessWidget {
  156. final View view;
  157. DocumentShareButton({Key? key, required this.view}) : super(key: ValueKey(view.hashCode));
  158. @override
  159. Widget build(BuildContext context) {
  160. double buttonWidth = 60;
  161. return BlocProvider(
  162. create: (context) => getIt<DocShareBloc>(param1: view),
  163. child: BlocListener<DocShareBloc, DocShareState>(
  164. listener: (context, state) {
  165. state.map(
  166. initial: (_) {},
  167. loading: (_) {},
  168. finish: (state) {
  169. state.successOrFail.fold(
  170. _handleExportData,
  171. _handleExportError,
  172. );
  173. },
  174. );
  175. },
  176. child: BlocBuilder<DocShareBloc, DocShareState>(
  177. builder: (context, state) {
  178. return ChangeNotifierProvider.value(
  179. value: Provider.of<AppearanceSettingModel>(context, listen: true),
  180. child: Selector<AppearanceSettingModel, Locale>(
  181. selector: (ctx, notifier) => notifier.locale,
  182. builder: (ctx, _, child) => ConstrainedBox(
  183. constraints: const BoxConstraints.expand(
  184. height: 30,
  185. // minWidth: buttonWidth,
  186. width: 100,
  187. ),
  188. child: RoundedTextButton(
  189. title: LocaleKeys.shareAction_buttonText.tr(),
  190. fontSize: 12,
  191. borderRadius: Corners.s6Border,
  192. color: Colors.lightBlue,
  193. onPressed: () => _showActionList(context, Offset(-(buttonWidth / 2), 10)),
  194. ),
  195. ),
  196. ),
  197. );
  198. },
  199. ),
  200. ),
  201. );
  202. }
  203. void _handleExportData(ExportData exportData) {
  204. switch (exportData.exportType) {
  205. case ExportType.Link:
  206. break;
  207. case ExportType.Markdown:
  208. FlutterClipboard.copy(exportData.data).then((value) => Log.info('copied to clipboard'));
  209. break;
  210. case ExportType.Text:
  211. break;
  212. }
  213. }
  214. void _handleExportError(FlowyError error) {}
  215. void _showActionList(BuildContext context, Offset offset) {
  216. final actionList = ShareActions(onSelected: (result) {
  217. result.fold(() {}, (action) {
  218. switch (action) {
  219. case ShareAction.markdown:
  220. context.read<DocShareBloc>().add(const DocShareEvent.shareMarkdown());
  221. break;
  222. case ShareAction.copyLink:
  223. showWorkInProgressDialog(context);
  224. break;
  225. }
  226. });
  227. });
  228. actionList.show(
  229. context,
  230. context,
  231. anchorDirection: AnchorDirection.bottomWithCenterAligned,
  232. anchorOffset: offset,
  233. );
  234. }
  235. void showWorkInProgressDialog(BuildContext context) {
  236. FlowyAlertDialog(title: LocaleKeys.shareAction_workInProgress.tr()).show(context);
  237. }
  238. }
  239. class ShareActions with ActionList<ShareActionWrapper> implements FlowyOverlayDelegate {
  240. final Function(dartz.Option<ShareAction>) onSelected;
  241. final _items = ShareAction.values.map((action) => ShareActionWrapper(action)).toList();
  242. ShareActions({required this.onSelected});
  243. @override
  244. double get maxWidth => 130;
  245. @override
  246. double get itemHeight => 22;
  247. @override
  248. List<ShareActionWrapper> get items => _items;
  249. @override
  250. void Function(dartz.Option<ShareActionWrapper> p1) get selectCallback => (result) {
  251. result.fold(
  252. () => onSelected(dartz.none()),
  253. (wrapper) => onSelected(
  254. dartz.some(wrapper.inner),
  255. ),
  256. );
  257. };
  258. @override
  259. FlowyOverlayDelegate? get delegate => this;
  260. @override
  261. void didRemove() => onSelected(dartz.none());
  262. }
  263. enum ShareAction {
  264. markdown,
  265. copyLink,
  266. }
  267. class ShareActionWrapper extends ActionItem {
  268. final ShareAction inner;
  269. ShareActionWrapper(this.inner);
  270. @override
  271. Widget? get icon => null;
  272. @override
  273. String get name => inner.name;
  274. }
  275. extension QuestionBubbleExtension on ShareAction {
  276. String get name {
  277. switch (this) {
  278. case ShareAction.markdown:
  279. return LocaleKeys.shareAction_markdown.tr();
  280. case ShareAction.copyLink:
  281. return LocaleKeys.shareAction_copyLink.tr();
  282. }
  283. }
  284. }