TF-725 Insert image to composer on web

This commit is contained in:
dab246
2022-07-20 16:50:28 +07:00
committed by Dat H. Pham
parent 68fd99ad5e
commit da32cf9a40
@@ -1,7 +1,14 @@
import 'package:collection/collection.dart';
import 'package:dartz/dartz.dart';
import 'package:html/parser.dart' show parse;
import 'package:core/utils/app_logger.dart';
import 'package:get/get.dart';
import 'package:html_editor_enhanced/html_editor.dart';
import 'package:model/email/attachment.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/image_source.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/inline_image.dart';
import 'package:tmail_ui_user/features/composer/presentation/model/rich_text_style_type.dart';
class RichTextWebController extends GetxController {
@@ -41,4 +48,42 @@ class RichTextWebController extends GetxController {
bool isTextStyleTypeSelected(RichTextStyleType richTextStyleType) =>
listTextStyleApply.contains(richTextStyleType);
void insertImage(InlineImage image, {double? maxWithEditor}) async {
log('RichTextWebController::insertImage(): $image | maxWithEditor: $maxWithEditor');
if (image.source == ImageSource.network) {
editorController.insertNetworkImage(image.link!);
} else {
final htmlContent = await image.generateImgTagHtml(maxWithEditor: maxWithEditor);
log('RichTextWebController::insertImage(): $htmlContent');
editorController.insertHtml(htmlContent);
}
}
Future<Tuple2<String, List<Attachment>>> refactorContentHasInlineImage(
String emailContent,
Map<String, Attachment> mapInlineAttachments
) async {
final document = parse(emailContent);
final listImgTag = document.querySelectorAll('img[src^="data:image/"]');
final listInlineAttachment = await Future.wait(listImgTag.map((imgTag) async {
final cid = imgTag.attributes['id'];
log('RichTextWebController::refactorContentHasInlineImage(): cid: $cid');
imgTag.attributes['src'] = 'cid:$cid';
imgTag.attributes.remove('id');
return cid;
})).then((listCid) {
log('RichTextWebController::refactorContentHasInlineImage(): $listCid');
final listInlineAttachment = listCid
.whereNotNull()
.map((cid) => mapInlineAttachments[cid])
.whereNotNull()
.toList();
return listInlineAttachment;
});
final newContent = document.body?.innerHtml ?? emailContent;
log('RichTextWebController::refactorContentHasInlineImage(): $newContent');
log('RichTextWebController::refactorContentHasInlineImage(): listInlineAttachment: $listInlineAttachment');
return Tuple2(newContent, listInlineAttachment);
}
}