Skip to content

Commit

Permalink
Solution for null text returned with getText() method on mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
tneotia committed Mar 10, 2021
1 parent a012a82 commit c0bdc6e
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.4.0"
version: "1.5.0"
intl:
dependency: transitive
description:
Expand Down
3 changes: 0 additions & 3 deletions lib/html_editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,5 @@ export 'package:html_editor_enhanced/src/html_editor_controller_unsupported.dart
if (dart.library.html) 'package:html_editor_enhanced/src/html_editor_controller_web.dart'
if (dart.library.io) 'package:html_editor_enhanced/src/html_editor_controller_mobile.dart';

/// Global variable used to get the text from the Html editor
String? text = "";

/// Global variable used to get the [InAppWebViewController] of the Html editor
Map<HtmlEditorController, dynamic> controllerMap = {};
10 changes: 9 additions & 1 deletion lib/src/html_editor_controller_mobile.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:html_editor_enhanced/html_editor.dart';
Expand All @@ -10,12 +12,18 @@ class HtmlEditorController extends unsupported.HtmlEditorController {
/// outside of the package itself for endless control and customization.
InAppWebViewController? get editorController => controllerMap[this];

/// Stream to get the text once the javascript execution is complete.
/// It is *not* recommended to modify or use this property in your code, this
/// is only exposed so the [InAppWebView] can access it.
StreamController<String>? getTextStream = StreamController<String>.broadcast();

/// Gets the text from the editor and returns it as a [String].
Future<String?> getText() async {
getTextStream!.stream.drain();
await _evaluateJavascript(
source:
"var str = \$('#summernote-2').summernote('code'); console.log(str);");
return text;
return await getTextStream!.stream.first;
}

/// Sets the text of the editor. Some pre-processing is applied to convert
Expand Down
7 changes: 7 additions & 0 deletions lib/src/html_editor_controller_unsupported.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter_inappwebview/flutter_inappwebview.dart';

/// Fallback controller (should never be used)
Expand All @@ -6,6 +8,11 @@ class HtmlEditorController {
/// outside of the package itself for endless control and customization.
InAppWebViewController? get editorController => null;

/// Stream to get the text once the javascript execution is complete.
/// It is *not* recommended to modify or use this property in your code, this
/// is only exposed so the [InAppWebView] can access it.
StreamController<String>? getTextStream = null;

/// Gets the text from the editor and returns it as a [String].
Future<String?> getText() => Future.value(null);

Expand Down
4 changes: 2 additions & 2 deletions lib/src/html_editor_controller_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class HtmlEditorController extends unsupported.HtmlEditorController {
_evaluateJavascriptWeb(data: {"type": "toIframe: getText"});
html.MessageEvent e = await html.window.onMessage.firstWhere(
(element) => json.decode(element.data)["type"] == "toDart: getText");
text = json.decode(e.data)["text"];
if (text!.isEmpty ||
String text = json.decode(e.data)["text"];
if (text.isEmpty ||
text == "<p></p>" ||
text == "<p><br></p>" ||
text == "<p><br/></p>") text = "";
Expand Down
4 changes: 2 additions & 2 deletions lib/src/widgets/html_editor_widget_mobile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class HtmlEditorWidget extends StatelessWidget {
message == "<p><br/></p>") {
message = "";
}
text = message;
widgetController.getTextStream!.add(message);
},
onLoadStop: (InAppWebViewController controller, Uri? uri) async {
String url = uri.toString();
Expand Down Expand Up @@ -100,7 +100,7 @@ class HtmlEditorWidget extends StatelessWidget {
String darkCSS =
"<link href=\"summernote-lite-dark.css\" rel=\"stylesheet\">";
await controller.evaluateJavascript(
source: "\$('head').append('${darkCSS}');");
source: "\$('head').append('$darkCSS');");
}
//set the text once the editor is loaded
if (value != null) {
Expand Down

0 comments on commit c0bdc6e

Please sign in to comment.