diff --git a/core/lib/core.dart b/core/lib/core.dart index b397e6e81..ec9be0be4 100644 --- a/core/lib/core.dart +++ b/core/lib/core.dart @@ -67,7 +67,6 @@ export 'presentation/views/dialog/edit_text_dialog_builder.dart'; export 'presentation/views/dialog/color_picker_dialog_builder.dart'; export 'presentation/views/html_viewer/html_content_viewer_widget.dart'; export 'presentation/views/html_viewer/html_content_viewer_on_web_widget.dart'; -export 'presentation/views/html_viewer/html_viewer_controller_for_web.dart'; export 'presentation/views/floating_button/scrolling_floating_button_animated.dart'; export 'presentation/views/bottom_popup/cupertino_action_sheet_action_builder.dart'; export 'presentation/views/bottom_popup/cupertino_action_sheet_no_icon_builder.dart'; diff --git a/core/lib/presentation/views/html_viewer/html_content_viewer_on_web_widget.dart b/core/lib/presentation/views/html_viewer/html_content_viewer_on_web_widget.dart index 84405bd82..b48ef5459 100644 --- a/core/lib/presentation/views/html_viewer/html_content_viewer_on_web_widget.dart +++ b/core/lib/presentation/views/html_viewer/html_content_viewer_on_web_widget.dart @@ -5,7 +5,6 @@ import 'dart:math' as math; import 'package:core/presentation/extensions/color_extension.dart'; import 'package:core/presentation/utils/html_transformer/html_template.dart'; import 'package:core/presentation/utils/html_transformer/html_utils.dart'; -import 'package:core/presentation/views/html_viewer/html_viewer_controller_for_web.dart'; import 'package:core/utils/app_logger.dart'; import 'package:flutter/cupertino.dart'; import 'package:universal_html/html.dart' as html; @@ -16,7 +15,6 @@ class HtmlContentViewerOnWeb extends StatefulWidget { final String contentHtml; final double widthContent; final double heightContent; - final HtmlViewerControllerForWeb controller; final TextDirection? direction; /// Handler for mailto: links @@ -30,7 +28,6 @@ class HtmlContentViewerOnWeb extends StatefulWidget { required this.contentHtml, required this.widthContent, required this.heightContent, - required this.controller, this.allowResizeToDocumentSize = true, this.mailtoDelegate, this.direction, @@ -42,27 +39,26 @@ class HtmlContentViewerOnWeb extends StatefulWidget { class _HtmlContentViewerOnWebState extends State { + static const double _minWidth = 300; /// The view ID for the IFrameElement. Must be unique. - late String createdViewId; + late String _createdViewId; /// The actual height of the content view, used to automatically set the height - late double actualHeight; + late double _actualHeight; /// The actual width of the content view, used to automatically set the width - late double actualWidth; + late double _actualWidth; - Future? webInit; + Future? _webInit; String? _htmlData; bool _isLoading = true; double minHeight = 100; - double minWidth = 300; - final jsonEncoder = const JsonEncoder(); + final _jsonEncoder = const JsonEncoder(); @override void initState() { super.initState(); - actualHeight = widget.heightContent; - actualWidth = widget.widthContent; - createdViewId = _getRandString(10); - widget.controller.viewId = createdViewId; + _actualHeight = widget.heightContent; + _actualWidth = widget.widthContent; + _createdViewId = _getRandString(10); _setUpWeb(); } @@ -72,17 +68,16 @@ class _HtmlContentViewerOnWebState extends State { log('_HtmlContentViewerOnWebState::didUpdateWidget():Old-Direction: ${oldWidget.direction} | Current-Direction: ${widget.direction}'); if (widget.contentHtml != oldWidget.contentHtml || widget.direction != oldWidget.direction) { - createdViewId = _getRandString(10); - widget.controller.viewId = createdViewId; + _createdViewId = _getRandString(10); _setUpWeb(); } if (widget.heightContent != oldWidget.heightContent) { - actualHeight = widget.heightContent; + _actualHeight = widget.heightContent; } if (widget.widthContent != oldWidget.widthContent) { - actualWidth = widget.widthContent; + _actualWidth = widget.widthContent; } } @@ -101,14 +96,14 @@ class _HtmlContentViewerOnWebState extends State { function handleMessage(e) { if (e && e.data && e.data.includes("toIframe:")) { var data = JSON.parse(e.data); - if (data["view"].includes("$createdViewId")) { + if (data["view"].includes("$_createdViewId")) { if (data["type"].includes("getHeight")) { var height = document.body.scrollHeight; - window.parent.postMessage(JSON.stringify({"view": "$createdViewId", "type": "toDart: htmlHeight", "height": height}), "*"); + window.parent.postMessage(JSON.stringify({"view": "$_createdViewId", "type": "toDart: htmlHeight", "height": height}), "*"); } if (data["type"].includes("getWidth")) { var width = document.body.scrollWidth; - window.parent.postMessage(JSON.stringify({"view": "$createdViewId", "type": "toDart: htmlWidth", "width": width}), "*"); + window.parent.postMessage(JSON.stringify({"view": "$_createdViewId", "type": "toDart: htmlWidth", "width": width}), "*"); } if (data["type"].includes("execCommand")) { if (data["argument"] === null) { @@ -125,10 +120,10 @@ class _HtmlContentViewerOnWebState extends State { let link = e.target; let textContent = e.target.textContent; if (link && isValidMailtoLink(link)) { - window.parent.postMessage(JSON.stringify({"view": "$createdViewId", "type": "toDart: OpenLink", "url": "" + link}), "*"); + window.parent.postMessage(JSON.stringify({"view": "$_createdViewId", "type": "toDart: OpenLink", "url": "" + link}), "*"); e.preventDefault(); } else if (textContent && isValidMailtoLink(textContent)) { - window.parent.postMessage(JSON.stringify({"view": "$createdViewId", "type": "toDart: OpenLink", "url": "" + textContent}), "*"); + window.parent.postMessage(JSON.stringify({"view": "$_createdViewId", "type": "toDart: OpenLink", "url": "" + textContent}), "*"); e.preventDefault(); } } @@ -169,7 +164,7 @@ class _HtmlContentViewerOnWebState extends State { final htmlTemplate = generateHtml(content, minHeight: minHeight, - minWidth: minWidth, + minWidth: _minWidth, styleCSS: tooltipLinkCss, javaScripts: webViewActionScripts + scriptsDisableZoom + HtmlUtils.scriptsHandleLazyLoadingBackgroundImage, direction: widget.direction); @@ -181,8 +176,8 @@ class _HtmlContentViewerOnWebState extends State { _htmlData = _generateHtmlDocument(widget.contentHtml); final iframe = html.IFrameElement() - ..width = actualWidth.toString() - ..height = actualHeight.toString() + ..width = _actualWidth.toString() + ..height = _actualHeight.toString() ..srcdoc = _htmlData ?? '' ..style.border = 'none' ..style.overflow = 'hidden' @@ -194,13 +189,13 @@ class _HtmlContentViewerOnWebState extends State { html.window.onMessage.listen((event) { var data = json.decode(event.data); - if (data['type'] != null && data['type'].contains('toDart: htmlHeight') && data['view'] == createdViewId) { - final docHeight = data['height'] ?? actualHeight; + if (data['type'] != null && data['type'].contains('toDart: htmlHeight') && data['view'] == _createdViewId) { + final docHeight = data['height'] ?? _actualHeight; if (docHeight != null && mounted) { final scrollHeightWithBuffer = docHeight + 30.0; if (scrollHeightWithBuffer > minHeight) { setState(() { - actualHeight = scrollHeightWithBuffer; + _actualHeight = scrollHeightWithBuffer; _isLoading = false; }); } @@ -212,18 +207,18 @@ class _HtmlContentViewerOnWebState extends State { } } - if (data['type'] != null && data['type'].contains('toDart: htmlWidth') && data['view'] == createdViewId) { - final docWidth = data['width'] ?? actualWidth; + if (data['type'] != null && data['type'].contains('toDart: htmlWidth') && data['view'] == _createdViewId) { + final docWidth = data['width'] ?? _actualWidth; if (docWidth != null && mounted) { - if (docWidth > minWidth && widget.allowResizeToDocumentSize) { + if (docWidth > _minWidth && widget.allowResizeToDocumentSize) { setState(() { - actualWidth = docWidth; + _actualWidth = docWidth; }); } } } - if (data['type'] != null && data['type'].contains('toDart: OpenLink') && data['view'] == createdViewId) { + if (data['type'] != null && data['type'].contains('toDart: OpenLink') && data['view'] == _createdViewId) { final link = data['url']; if (link != null && mounted) { final urlString = link as String; @@ -235,11 +230,11 @@ class _HtmlContentViewerOnWebState extends State { }); }); - ui.platformViewRegistry.registerViewFactory(createdViewId, (int viewId) => iframe); + ui.platformViewRegistry.registerViewFactory(_createdViewId, (int viewId) => iframe); if (mounted) { setState(() { - webInit = Future.value(true); + _webInit = Future.value(true); }); } } @@ -254,15 +249,15 @@ class _HtmlContentViewerOnWebState extends State { const SizedBox.shrink() else FutureBuilder( - future: webInit, + future: _webInit, builder: (context, snapshot) { if (snapshot.hasData) { return SizedBox( - height: actualHeight, - width: actualWidth, + height: _actualHeight, + width: _actualWidth, child: HtmlElementView( key: ValueKey(_htmlData), - viewType: createdViewId, + viewType: _createdViewId, ), ); } else { @@ -292,9 +287,9 @@ class _HtmlContentViewerOnWebState extends State { void _sendMessageToWebViewForGetHeight() { final dataGetHeight = { 'type': 'toIframe: getHeight', - 'view' : createdViewId + 'view' : _createdViewId }; - final jsonGetHeight = jsonEncoder.convert(dataGetHeight); + final jsonGetHeight = _jsonEncoder.convert(dataGetHeight); html.window.postMessage(jsonGetHeight, '*'); } @@ -302,9 +297,9 @@ class _HtmlContentViewerOnWebState extends State { void _sendMessageToWebViewForGetWidth() { final dataGetWidth = { 'type': 'toIframe: getWidth', - 'view' : createdViewId + 'view' : _createdViewId }; - final jsonGetWidth = jsonEncoder.convert(dataGetWidth); + final jsonGetWidth = _jsonEncoder.convert(dataGetWidth); html.window.postMessage(jsonGetWidth, '*'); } diff --git a/core/lib/presentation/views/html_viewer/html_viewer_controller_for_web.dart b/core/lib/presentation/views/html_viewer/html_viewer_controller_for_web.dart deleted file mode 100644 index ab15b7ef1..000000000 --- a/core/lib/presentation/views/html_viewer/html_viewer_controller_for_web.dart +++ /dev/null @@ -1,54 +0,0 @@ -import 'dart:convert'; -import 'package:universal_html/html.dart' as html; - -import 'package:flutter/foundation.dart'; - -class HtmlViewerControllerForWeb { - - HtmlViewerControllerForWeb(); - - /// Manages the view ID for the [HtmlViewerControllerForWeb] on web - String? _viewId; - - set viewId(String? viewId) => _viewId = viewId; - - /// Recalculates the height of the editor to remove any vertical scrolling. - void recalculateHeight() { - _evaluateJavascriptWeb(data: { - 'type': 'toIframe: getHeight', - }); - } - - /// A function to quickly call a document.execCommand function in a readable format - void execCommand(String command, {String? argument}) { - _evaluateJavascriptWeb(data: { - 'type': 'toIframe: execCommand', - 'command': command, - 'argument': argument - }); - } - - /// A function to execute JS passed as a [WebScript] to the editor. This should - /// only be used on Flutter Web. - Future evaluateJavascriptWeb(String name, - {bool hasReturnValue = false}) async { - _evaluateJavascriptWeb(data: {'type': 'toIframe: $name'}); - if (hasReturnValue) { - var e = await html.window.onMessage.firstWhere( - (element) => json.decode(element.data)['type'] == 'toDart: $name'); - return json.decode(e.data); - } - } - - /// Helper function to run javascript and check current environment - void _evaluateJavascriptWeb({required Map data}) async { - if (kIsWeb) { - data['view'] = _viewId; - const jsonEncoder = JsonEncoder(); - var json = jsonEncoder.convert(data); - html.window.postMessage(json, '*'); - } else { - throw Exception('Non-Flutter Web environment detected'); - } - } -} \ No newline at end of file diff --git a/lib/features/email/presentation/email_view.dart b/lib/features/email/presentation/email_view.dart index 2ecf5a160..846d019cc 100644 --- a/lib/features/email/presentation/email_view.dart +++ b/lib/features/email/presentation/email_view.dart @@ -2,7 +2,6 @@ import 'package:core/presentation/extensions/color_extension.dart'; import 'package:core/presentation/views/button/tmail_button_widget.dart'; import 'package:core/presentation/views/html_viewer/html_content_viewer_on_web_widget.dart'; import 'package:core/presentation/views/html_viewer/html_content_viewer_widget.dart'; -import 'package:core/presentation/views/html_viewer/html_viewer_controller_for_web.dart'; import 'package:core/utils/direction_utils.dart'; import 'package:core/utils/platform_info.dart'; import 'package:flutter/material.dart'; @@ -378,7 +377,6 @@ class EmailView extends GetWidget { widthContent: constraints.maxWidth, heightContent: constraints.maxHeight, contentHtml: allEmailContents, - controller: HtmlViewerControllerForWeb(), mailtoDelegate: controller.openMailToLink, direction: AppUtils.getCurrentDirection(context), ), diff --git a/lib/features/manage_account/presentation/profiles/identities/widgets/signature_builder.dart b/lib/features/manage_account/presentation/profiles/identities/widgets/signature_builder.dart index 1e5a046b4..1cd7a3f90 100644 --- a/lib/features/manage_account/presentation/profiles/identities/widgets/signature_builder.dart +++ b/lib/features/manage_account/presentation/profiles/identities/widgets/signature_builder.dart @@ -1,6 +1,5 @@ import 'package:core/presentation/views/html_viewer/html_content_viewer_on_web_widget.dart'; import 'package:core/presentation/views/html_viewer/html_content_viewer_widget.dart'; -import 'package:core/presentation/views/html_viewer/html_viewer_controller_for_web.dart'; import 'package:core/utils/platform_info.dart'; import 'package:flutter/material.dart'; import 'package:tmail_ui_user/main/utils/app_utils.dart'; @@ -41,7 +40,6 @@ class SignatureBuilder extends StatelessWidget { contentHtml: signatureSelected, widthContent: width, heightContent: height, - controller: HtmlViewerControllerForWeb(), allowResizeToDocumentSize: false, direction: AppUtils.getCurrentDirection(context), );