TF-2116 Insert image at mouse cursor position

(cherry picked from commit cb347e5077eddb1366daf706aa87eb2291f71aab)
This commit is contained in:
dab246
2023-09-08 11:41:35 +07:00
committed by Dat H. Pham
parent d0744e7560
commit e63a8f7d7f
11 changed files with 164 additions and 53 deletions
@@ -10,11 +10,24 @@ class DownloadImageAsBase64Success extends UIState {
final String base64Uri;
final String cid;
final FileInfo fileInfo;
final bool fromFileShared;
DownloadImageAsBase64Success(this.base64Uri, this.cid, this.fileInfo);
DownloadImageAsBase64Success(
this.base64Uri,
this.cid,
this.fileInfo,
{
this.fromFileShared = false
}
);
@override
List<Object?> get props => [base64Uri, cid, fileInfo];
List<Object?> get props => [
base64Uri,
cid,
fileInfo,
fromFileShared,
];
}
class DownloadImageAsBase64Failure extends FeatureFailure {
@@ -7,18 +7,40 @@ class UploadAttachmentSuccess extends UIState {
final UploadAttachment uploadAttachment;
final bool isInline;
final bool fromFileShared;
UploadAttachmentSuccess(this.uploadAttachment, {this.isInline = false});
UploadAttachmentSuccess(
this.uploadAttachment,
{
this.isInline = false,
this.fromFileShared = false,
}
);
@override
List<Object?> get props => [uploadAttachment, isInline];
List<Object?> get props => [
uploadAttachment,
isInline,
fromFileShared,
];
}
class UploadAttachmentFailure extends FeatureFailure {
final bool isInline;
final bool fromFileShared;
UploadAttachmentFailure(dynamic exception, {this.isInline = false}) : super(exception: exception);
UploadAttachmentFailure(
dynamic exception,
{
this.isInline = false,
this.fromFileShared = false,
}
) : super(exception: exception);
@override
List<Object?> get props => [isInline, ...super.props];
List<Object?> get props => [
isInline,
fromFileShared,
...super.props
];
}
@@ -10,24 +10,31 @@ class DownloadImageAsBase64Interactor {
DownloadImageAsBase64Interactor(this._composerRepository);
Stream<Either<Failure, Success>> execute(
String url,
String cid,
FileInfo fileInfo,
{
double? maxWidth,
bool? compress
}
String url,
String cid,
FileInfo fileInfo,
{
double? maxWidth,
bool? compress,
bool fromFileShared = false,
}
) async* {
try {
yield Right<Failure, Success>(DownloadingImageAsBase64());
final result = await _composerRepository.downloadImageAsBase64(
url,
url,
cid,
fileInfo,
maxWidth: maxWidth,
compress: compress
);
if (result?.isNotEmpty == true) {
yield Right<Failure, Success>(DownloadImageAsBase64Success(
result!,
cid,
fileInfo,
maxWidth: maxWidth,
compress: compress);
if (result?.isNotEmpty == true) {
yield Right<Failure, Success>(DownloadImageAsBase64Success(result!, cid, fileInfo));
fromFileShared: fromFileShared
));
} else {
yield Left<Failure, Success>(DownloadImageAsBase64Failure(null));
}
@@ -15,7 +15,8 @@ class UploadAttachmentInteractor {
FileInfo fileInfo,
Uri uploadUri, {
CancelToken? cancelToken,
bool isInline = false
bool isInline = false,
bool fromFileShared = false,
}) async* {
try {
final uploadAttachment = await _composerRepository.uploadAttachment(
@@ -23,9 +24,17 @@ class UploadAttachmentInteractor {
uploadUri,
cancelToken: cancelToken
);
yield Right<Failure, Success>(UploadAttachmentSuccess(uploadAttachment, isInline: isInline));
yield Right<Failure, Success>(UploadAttachmentSuccess(
uploadAttachment,
isInline: isInline,
fromFileShared: fromFileShared
));
} catch (e) {
yield Left<Failure, Success>(UploadAttachmentFailure(e, isInline: isInline));
yield Left<Failure, Success>(UploadAttachmentFailure(
e,
isInline: isInline,
fromFileShared: fromFileShared
));
}
}
}
@@ -311,7 +311,10 @@ class ComposerController extends BaseController {
ImageSource.local,
fileInfo: success.fileInfo,
cid: success.cid,
base64Uri: success.base64Uri));
base64Uri: success.base64Uri
),
fromFileShare: success.fromFileShared
);
}
maxWithEditor = null;
}
@@ -1235,7 +1238,7 @@ class ComposerController extends BaseController {
if (listImageSharedMediaFile.isNotEmpty) {
final listInlineImage = covertListSharedMediaFileToInlineImage(listSharedMediaFile);
for (var e in listInlineImage) {
_uploadInlineAttachmentsAction(e.fileInfo!);
_uploadInlineAttachmentsAction(e.fileInfo!, fromFileShared: true);
}
}
if (listFileAttachmentSharedMediaFile.isNotEmpty) {
@@ -1681,13 +1684,18 @@ class ComposerController extends BaseController {
}
}
void _uploadInlineAttachmentsAction(FileInfo pickedFile) async {
void _uploadInlineAttachmentsAction(FileInfo pickedFile, {bool fromFileShared = false}) async {
if (uploadController.hasEnoughMaxAttachmentSize(listFiles: [pickedFile])) {
final session = mailboxDashBoardController.sessionCurrent;
final accountId = mailboxDashBoardController.accountId.value;
if (session != null && accountId != null) {
final uploadUri = session.getUploadUri(accountId, jmapUrl: _dynamicUrlInterceptors.jmapUrl);
uploadController.uploadFileAction(pickedFile, uploadUri, isInline: true);
uploadController.uploadFileAction(
pickedFile,
uploadUri,
isInline: true,
fromFileShared: fromFileShared
);
}
} else {
if (currentContext != null) {
@@ -1713,10 +1721,12 @@ class ComposerController extends BaseController {
final imageUrl = uploadState.attachment.getDownloadUrl(baseDownloadUrl, accountId);
log('ComposerController::_handleUploadInlineSuccess(): imageUrl: $imageUrl');
consumeState(_downloadImageAsBase64Interactor.execute(
imageUrl,
uploadState.attachment.cid!,
uploadState.fileInfo,
maxWidth: maxWithEditor));
imageUrl,
uploadState.attachment.cid!,
uploadState.fileInfo,
maxWidth: maxWithEditor,
fromFileShared: uploadState.fromFileShared,
));
}
}
@@ -9,12 +9,20 @@ import 'package:tmail_ui_user/features/composer/presentation/model/inline_image.
class RichTextMobileTabletController extends BaseRichTextController {
HtmlEditorApi? htmlEditorApi;
void insertImage(InlineImage image, {double? maxWithEditor}) async {
log('RichTextMobileTabletController::insertImage(): $image | maxWithEditor: $maxWithEditor');
void insertImage(
InlineImage image,
{
double? maxWithEditor,
bool fromFileShare = false
}
) async {
log('RichTextMobileTabletController::insertImage(): $image | maxWithEditor: $maxWithEditor | $fromFileShare');
if (image.source == ImageSource.network) {
htmlEditorApi?.insertImageLink(image.link!);
} else {
await htmlEditorApi?.moveCursorAtLastNode();
if (fromFileShare) {
await htmlEditorApi?.moveCursorAtLastNode();
}
await htmlEditorApi?.insertHtml(image.base64Uri ?? '');
}
}