AppFlowy/frontend/app_flowy/lib/plugins/document/document.dart

236 lines
6.5 KiB
Dart
Raw Normal View History

2022-08-10 09:59:28 +00:00
library document_plugin;
2022-03-01 08:05:45 +00:00
2022-05-17 18:25:35 +00:00
import 'package:app_flowy/generated/locale_keys.g.dart';
import 'package:app_flowy/plugins/util.dart';
import 'package:app_flowy/startup/plugin/plugin.dart';
import 'package:app_flowy/startup/startup.dart';
2022-11-15 02:46:26 +00:00
import 'package:app_flowy/plugins/document/application/share_bloc.dart';
2022-03-01 08:05:45 +00:00
import 'package:app_flowy/workspace/presentation/home/home_stack.dart';
import 'package:app_flowy/workspace/presentation/home/toast.dart';
import 'package:app_flowy/workspace/presentation/widgets/left_bar_item.dart';
2021-11-10 07:09:24 +00:00
import 'package:app_flowy/workspace/presentation/widgets/dialogs.dart';
import 'package:app_flowy/workspace/presentation/widgets/pop_up_action.dart';
import 'package:appflowy_popover/appflowy_popover.dart';
2022-05-17 18:25:35 +00:00
import 'package:clipboard/clipboard.dart';
2021-12-07 17:31:23 +00:00
import 'package:easy_localization/easy_localization.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flowy_infra/size.dart';
import 'package:flowy_infra_ui/widget/rounded_button.dart';
2022-02-19 05:52:52 +00:00
import 'package:flowy_sdk/log.dart';
2021-12-14 10:04:51 +00:00
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
2022-07-04 07:00:54 +00:00
import 'package:flowy_sdk/protobuf/flowy-folder/view.pb.dart';
2022-10-13 15:29:37 +00:00
import 'package:flowy_sdk/protobuf/flowy-document/entities.pb.dart';
2021-07-24 06:05:49 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
2021-07-24 06:05:49 +00:00
import 'document_page.dart';
2022-05-17 18:25:35 +00:00
2022-03-01 03:22:39 +00:00
class DocumentPluginBuilder extends PluginBuilder {
2022-02-28 14:38:53 +00:00
@override
Plugin build(dynamic data) {
2022-07-19 06:11:29 +00:00
if (data is ViewPB) {
2022-02-28 14:38:53 +00:00
return DocumentPlugin(pluginType: pluginType, view: data);
} else {
throw FlowyPluginException.invalidData;
}
}
@override
2022-05-17 18:25:35 +00:00
String get menuName => LocaleKeys.document_menuName.tr();
2022-02-28 14:38:53 +00:00
@override
String get menuIcon => "editor/documents";
2022-02-28 14:38:53 +00:00
@override
PluginType get pluginType => PluginType.editor;
2022-02-28 14:38:53 +00:00
@override
2022-10-22 13:57:44 +00:00
ViewDataFormatPB get dataFormatType => ViewDataFormatPB.TreeFormat;
2022-02-28 14:38:53 +00:00
}
class DocumentPlugin extends Plugin<int> {
2022-02-28 14:38:53 +00:00
late PluginType _pluginType;
2021-10-10 07:58:57 +00:00
@override
final ViewPluginNotifier notifier;
DocumentPlugin({
required PluginType pluginType,
required ViewPB view,
Key? key,
}) : notifier = ViewPluginNotifier(view: view) {
_pluginType = pluginType;
2022-02-28 14:38:53 +00:00
}
@override
PluginDisplay get display => DocumentPluginDisplay(notifier: notifier);
2021-10-10 07:58:57 +00:00
@override
2022-03-02 03:38:22 +00:00
PluginType get ty => _pluginType;
2022-02-28 14:38:53 +00:00
@override
PluginId get id => notifier.view.id;
2022-02-28 14:38:53 +00:00
}
class DocumentPluginDisplay extends PluginDisplay with NavigationItem {
final ViewPluginNotifier notifier;
ViewPB get view => notifier.view;
int? deletedViewIndex;
2022-02-28 14:38:53 +00:00
DocumentPluginDisplay({required this.notifier, Key? key});
2021-10-10 07:58:57 +00:00
@override
Widget buildWidget(PluginContext context) {
notifier.isDeleted.addListener(() {
notifier.isDeleted.value.fold(() => null, (deletedView) {
if (deletedView.hasIndex()) {
deletedViewIndex = deletedView.index;
}
});
});
return DocumentPage(
view: view,
onDeleted: () => context.onDeleted(view, deletedViewIndex),
key: ValueKey(view.id),
);
}
@override
Widget get leftBarItem => ViewLeftBarItem(view: view);
@override
Widget? get rightBarItem => DocumentShareButton(view: view);
2022-02-28 14:38:53 +00:00
@override
2022-03-20 09:17:06 +00:00
List<NavigationItem> get navigationItems => [this];
}
class DocumentShareButton extends StatelessWidget {
2022-07-19 06:11:29 +00:00
final ViewPB view;
DocumentShareButton({Key? key, required this.view})
: super(key: ValueKey(view.hashCode));
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) => getIt<DocShareBloc>(param1: view),
child: BlocListener<DocShareBloc, DocShareState>(
listener: (context, state) {
state.map(
initial: (_) {},
loading: (_) {},
finish: (state) {
state.successOrFail.fold(
_handleExportData,
_handleExportError,
);
},
);
},
child: BlocBuilder<DocShareBloc, DocShareState>(
builder: (context, state) => ConstrainedBox(
constraints: const BoxConstraints.expand(
height: 30,
width: 100,
),
child: ShareActionList(view: view),
),
),
),
);
}
2021-07-24 14:27:24 +00:00
void _handleExportData(ExportDataPB exportData) {
switch (exportData.exportType) {
case ExportType.Link:
break;
case ExportType.Markdown:
FlutterClipboard.copy(exportData.data)
.then((value) => Log.info('copied to clipboard'));
break;
case ExportType.Text:
break;
}
2021-07-24 14:27:24 +00:00
}
2021-12-14 10:04:51 +00:00
void _handleExportError(FlowyError error) {}
}
class ShareActionList extends StatelessWidget {
const ShareActionList({
Key? key,
required this.view,
}) : super(key: key);
final ViewPB view;
@override
Widget build(BuildContext context) {
final docShareBloc = context.read<DocShareBloc>();
return PopoverActionList<ShareActionWrapper>(
direction: PopoverDirection.bottomWithCenterAligned,
actions: ShareAction.values
.map((action) => ShareActionWrapper(action))
.toList(),
buildChild: (controller) {
return RoundedTextButton(
title: LocaleKeys.shareAction_buttonText.tr(),
fontSize: FontSizes.s12,
borderRadius: Corners.s6Border,
color: Theme.of(context).colorScheme.primary,
onPressed: () => controller.show(),
);
},
onSelected: (action, controller) async {
switch (action.inner) {
case ShareAction.markdown:
final exportPath = await FilePicker.platform.saveFile(
dialogTitle: '',
fileName: '${view.name}.md',
);
if (exportPath != null) {
docShareBloc.add(DocShareEvent.shareMarkdown(exportPath));
showMessageToast('Exported to: $exportPath');
}
break;
case ShareAction.copyLink:
2022-09-15 08:05:55 +00:00
NavigatorAlertDialog(
title: LocaleKeys.shareAction_workInProgress.tr())
.show(context);
break;
}
controller.close();
},
);
2021-07-24 14:27:24 +00:00
}
2021-07-24 06:05:49 +00:00
}
enum ShareAction {
markdown,
copyLink,
}
class ShareActionWrapper extends ActionCell {
final ShareAction inner;
ShareActionWrapper(this.inner);
@override
2022-09-23 09:27:30 +00:00
Widget? icon(Color iconColor) => null;
@override
String get name => inner.name;
}
extension QuestionBubbleExtension on ShareAction {
String get name {
switch (this) {
case ShareAction.markdown:
2021-12-07 17:31:23 +00:00
return LocaleKeys.shareAction_markdown.tr();
case ShareAction.copyLink:
2021-12-07 17:31:23 +00:00
return LocaleKeys.shareAction_copyLink.tr();
}
}
}