2022-03-01 08:05:45 +00:00
|
|
|
library docuemnt_plugin;
|
|
|
|
|
2022-05-17 18:25:35 +00:00
|
|
|
import 'package:app_flowy/generated/locale_keys.g.dart';
|
2022-08-09 02:35:27 +00:00
|
|
|
import 'package:app_flowy/startup/plugin/plugin.dart';
|
2021-10-28 13:55:22 +00:00
|
|
|
import 'package:app_flowy/startup/startup.dart';
|
2022-01-29 02:44:35 +00:00
|
|
|
import 'package:app_flowy/workspace/application/appearance.dart';
|
2022-03-01 04:06:36 +00:00
|
|
|
import 'package:app_flowy/workspace/application/view/view_listener.dart';
|
2022-08-09 02:35:27 +00:00
|
|
|
import 'package:app_flowy/plugins/doc/application/share_bloc.dart';
|
2022-03-01 08:05:45 +00:00
|
|
|
import 'package:app_flowy/workspace/presentation/home/home_stack.dart';
|
2022-06-09 08:25:38 +00:00
|
|
|
import 'package:app_flowy/workspace/presentation/home/toast.dart';
|
2022-08-09 02:35:27 +00:00
|
|
|
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';
|
2021-11-09 15:13:04 +00:00
|
|
|
import 'package:app_flowy/workspace/presentation/widgets/pop_up_action.dart';
|
2022-05-17 18:25:35 +00:00
|
|
|
import 'package:clipboard/clipboard.dart';
|
|
|
|
import 'package:dartz/dartz.dart' as dartz;
|
2021-12-07 17:31:23 +00:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2022-03-02 03:38:22 +00:00
|
|
|
import 'package:flowy_infra/notifier.dart';
|
2021-11-09 15:13:04 +00:00
|
|
|
import 'package:flowy_infra/size.dart';
|
|
|
|
import 'package:flowy_infra_ui/flowy_infra_ui.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-05-17 18:25:35 +00:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-text-block/entities.pb.dart';
|
2021-07-24 06:05:49 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2021-11-10 06:46:59 +00:00
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2022-01-29 02:44:35 +00:00
|
|
|
import 'package:provider/provider.dart';
|
2021-07-24 06:05:49 +00:00
|
|
|
|
2022-08-09 02:35:27 +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
|
2022-03-05 02:59:44 +00:00
|
|
|
PluginType get pluginType => DefaultPlugin.quill.type();
|
2022-02-28 14:38:53 +00:00
|
|
|
|
|
|
|
@override
|
2022-03-14 09:24:25 +00:00
|
|
|
ViewDataType get dataType => ViewDataType.TextBlock;
|
2022-02-28 14:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class DocumentPlugin implements Plugin {
|
2022-07-19 06:11:29 +00:00
|
|
|
late ViewPB _view;
|
2022-03-01 08:05:45 +00:00
|
|
|
ViewListener? _listener;
|
2022-02-28 14:38:53 +00:00
|
|
|
late PluginType _pluginType;
|
2021-10-28 13:55:22 +00:00
|
|
|
|
2022-08-09 02:35:27 +00:00
|
|
|
DocumentPlugin(
|
|
|
|
{required PluginType pluginType, required ViewPB view, Key? key})
|
|
|
|
: _view = view {
|
2022-02-28 14:38:53 +00:00
|
|
|
_pluginType = pluginType;
|
2022-01-31 01:49:05 +00:00
|
|
|
_listener = getIt<ViewListener>(param1: view);
|
2022-05-05 13:15:01 +00:00
|
|
|
_listener?.start(onViewUpdated: (result) {
|
2021-10-28 13:55:22 +00:00
|
|
|
result.fold(
|
|
|
|
(newView) {
|
|
|
|
_view = newView;
|
2022-03-02 03:38:22 +00:00
|
|
|
display.notifier!.value = _view.hashCode;
|
2021-10-28 13:55:22 +00:00
|
|
|
},
|
|
|
|
(error) {},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2021-10-10 07:58:57 +00:00
|
|
|
|
|
|
|
@override
|
2022-02-28 14:38:53 +00:00
|
|
|
void dispose() {
|
2022-05-05 13:15:01 +00:00
|
|
|
_listener?.stop();
|
2022-03-01 08:05:45 +00:00
|
|
|
_listener = null;
|
2022-02-28 14:38:53 +00:00
|
|
|
}
|
2021-11-09 15:13:04 +00:00
|
|
|
|
|
|
|
@override
|
2022-03-02 03:38:22 +00:00
|
|
|
PluginDisplay<int> get display => DocumentPluginDisplay(view: _view);
|
2021-11-09 15:13:04 +00:00
|
|
|
|
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
|
2022-03-02 03:38:22 +00:00
|
|
|
PluginId get id => _view.id;
|
2022-02-28 14:38:53 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 09:17:06 +00:00
|
|
|
class DocumentPluginDisplay extends PluginDisplay<int> with NavigationItem {
|
2022-03-02 03:38:22 +00:00
|
|
|
final PublishNotifier<int> _displayNotifier = PublishNotifier<int>();
|
2022-07-19 06:11:29 +00:00
|
|
|
final ViewPB _view;
|
2022-02-28 14:38:53 +00:00
|
|
|
|
2022-07-19 06:11:29 +00:00
|
|
|
DocumentPluginDisplay({required ViewPB view, Key? key}) : _view = view;
|
2021-10-10 07:58:57 +00:00
|
|
|
|
|
|
|
@override
|
2022-02-23 14:17:47 +00:00
|
|
|
Widget buildWidget() => DocumentPage(view: _view, key: ValueKey(_view.id));
|
2021-10-10 09:01:30 +00:00
|
|
|
|
|
|
|
@override
|
2022-03-20 09:17:06 +00:00
|
|
|
Widget get leftBarItem => ViewLeftBarItem(view: _view);
|
2021-10-28 13:55:22 +00:00
|
|
|
|
|
|
|
@override
|
2022-02-28 14:38:53 +00:00
|
|
|
Widget? get rightBarItem => DocumentShareButton(view: _view);
|
|
|
|
|
|
|
|
@override
|
2022-03-20 09:17:06 +00:00
|
|
|
List<NavigationItem> get navigationItems => [this];
|
2021-10-10 09:01:30 +00:00
|
|
|
|
2022-03-02 03:38:22 +00:00
|
|
|
@override
|
|
|
|
PublishNotifier<int>? get notifier => _displayNotifier;
|
2021-11-10 10:26:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 14:22:34 +00:00
|
|
|
class DocumentShareButton extends StatelessWidget {
|
2022-07-19 06:11:29 +00:00
|
|
|
final ViewPB view;
|
2022-08-09 02:35:27 +00:00
|
|
|
DocumentShareButton({Key? key, required this.view})
|
|
|
|
: super(key: ValueKey(view.hashCode));
|
2021-11-09 15:13:04 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
double buttonWidth = 60;
|
2021-11-10 06:46:59 +00:00
|
|
|
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) {
|
2022-01-29 02:44:35 +00:00
|
|
|
return ChangeNotifierProvider.value(
|
|
|
|
value: Provider.of<AppearanceSettingModel>(context, listen: true),
|
2022-02-04 20:45:06 +00:00
|
|
|
child: Selector<AppearanceSettingModel, Locale>(
|
|
|
|
selector: (ctx, notifier) => notifier.locale,
|
2022-01-29 14:22:34 +00:00
|
|
|
builder: (ctx, _, child) => ConstrainedBox(
|
2022-01-31 00:15:49 +00:00
|
|
|
constraints: const BoxConstraints.expand(
|
2022-01-29 14:47:09 +00:00
|
|
|
height: 30,
|
|
|
|
// minWidth: buttonWidth,
|
|
|
|
width: 100,
|
2022-01-29 14:22:34 +00:00
|
|
|
),
|
|
|
|
child: RoundedTextButton(
|
|
|
|
title: LocaleKeys.shareAction_buttonText.tr(),
|
|
|
|
fontSize: 12,
|
|
|
|
borderRadius: Corners.s6Border,
|
|
|
|
color: Colors.lightBlue,
|
2022-08-09 02:35:27 +00:00
|
|
|
onPressed: () => _showActionList(
|
|
|
|
context, Offset(-(buttonWidth / 2), 10)),
|
2022-01-29 14:22:34 +00:00
|
|
|
),
|
2022-01-29 02:44:35 +00:00
|
|
|
),
|
|
|
|
),
|
2021-11-10 06:46:59 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
2021-11-09 15:13:04 +00:00
|
|
|
);
|
|
|
|
}
|
2021-07-24 14:27:24 +00:00
|
|
|
|
2022-07-18 03:58:39 +00:00
|
|
|
void _handleExportData(ExportDataPB exportData) {
|
2021-11-10 06:46:59 +00:00
|
|
|
switch (exportData.exportType) {
|
|
|
|
case ExportType.Link:
|
|
|
|
break;
|
|
|
|
case ExportType.Markdown:
|
2022-08-09 02:35:27 +00:00
|
|
|
FlutterClipboard.copy(exportData.data)
|
|
|
|
.then((value) => Log.info('copied to clipboard'));
|
2021-11-10 06:46:59 +00:00
|
|
|
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) {}
|
2021-11-10 06:46:59 +00:00
|
|
|
|
|
|
|
void _showActionList(BuildContext context, Offset offset) {
|
|
|
|
final actionList = ShareActions(onSelected: (result) {
|
|
|
|
result.fold(() {}, (action) {
|
|
|
|
switch (action) {
|
|
|
|
case ShareAction.markdown:
|
2022-08-09 02:35:27 +00:00
|
|
|
context
|
|
|
|
.read<DocShareBloc>()
|
|
|
|
.add(const DocShareEvent.shareMarkdown());
|
|
|
|
showMessageToast(
|
|
|
|
'Exported to: ${LocaleKeys.notifications_export_path.tr()}');
|
2021-11-10 06:46:59 +00:00
|
|
|
break;
|
|
|
|
case ShareAction.copyLink:
|
2022-08-09 02:35:27 +00:00
|
|
|
FlowyAlertDialog(title: LocaleKeys.shareAction_workInProgress.tr())
|
|
|
|
.show(context);
|
2021-11-10 06:46:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
actionList.show(
|
|
|
|
context,
|
|
|
|
anchorDirection: AnchorDirection.bottomWithCenterAligned,
|
|
|
|
anchorOffset: offset,
|
|
|
|
);
|
2021-07-24 14:27:24 +00:00
|
|
|
}
|
2021-07-24 06:05:49 +00:00
|
|
|
}
|
2021-11-09 15:13:04 +00:00
|
|
|
|
2022-03-31 06:43:31 +00:00
|
|
|
class ShareActions with ActionList<ShareActionWrapper>, FlowyOverlayDelegate {
|
2021-11-09 15:13:04 +00:00
|
|
|
final Function(dartz.Option<ShareAction>) onSelected;
|
2022-08-09 02:35:27 +00:00
|
|
|
final _items =
|
|
|
|
ShareAction.values.map((action) => ShareActionWrapper(action)).toList();
|
2021-11-09 15:13:04 +00:00
|
|
|
|
2021-11-10 06:46:59 +00:00
|
|
|
ShareActions({required this.onSelected});
|
2021-11-09 15:13:04 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
double get maxWidth => 130;
|
|
|
|
|
|
|
|
@override
|
|
|
|
double get itemHeight => 22;
|
|
|
|
|
|
|
|
@override
|
|
|
|
List<ShareActionWrapper> get items => _items;
|
|
|
|
|
|
|
|
@override
|
2022-08-09 02:35:27 +00:00
|
|
|
void Function(dartz.Option<ShareActionWrapper> p1) get selectCallback =>
|
|
|
|
(result) {
|
2021-11-09 15:13:04 +00:00
|
|
|
result.fold(
|
|
|
|
() => onSelected(dartz.none()),
|
|
|
|
(wrapper) => onSelected(
|
|
|
|
dartz.some(wrapper.inner),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
@override
|
|
|
|
FlowyOverlayDelegate? get delegate => this;
|
|
|
|
|
|
|
|
@override
|
2021-11-10 06:46:59 +00:00
|
|
|
void didRemove() => onSelected(dartz.none());
|
2021-11-09 15:13:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum ShareAction {
|
|
|
|
markdown,
|
|
|
|
copyLink,
|
|
|
|
}
|
|
|
|
|
|
|
|
class ShareActionWrapper extends ActionItem {
|
|
|
|
final ShareAction inner;
|
|
|
|
|
|
|
|
ShareActionWrapper(this.inner);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget? get icon => 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();
|
2021-11-09 15:13:04 +00:00
|
|
|
case ShareAction.copyLink:
|
2021-12-07 17:31:23 +00:00
|
|
|
return LocaleKeys.shareAction_copyLink.tr();
|
2021-11-09 15:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|