Skip to content

Commit

Permalink
Avoid onBlur being called twice when editor loses focus
Browse files Browse the repository at this point in the history
Signed-off-by: dab246 <[email protected]>
  • Loading branch information
dab246 committed Sep 30, 2024
1 parent 3274d7f commit 158e2b7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
4 changes: 2 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class _HtmlEditorExampleState extends State<HtmlEditorExample> {
debugPrint('enter/return pressed');
}, onFocus: () {
debugPrint('editor focused');
}, onBlur: () {
}, onUnFocus: () {
debugPrint('editor unfocused');
}, onBlurCodeview: () {
debugPrint('codeview either focused or unfocused');
Expand Down Expand Up @@ -207,7 +207,7 @@ class _HtmlEditorExampleState extends State<HtmlEditorExample> {
Theme.of(context).colorScheme.secondary),
onPressed: () async {
var txt = await controller.getText();
if (txt.contains('src=\"data:')) {
if (txt.contains('src=\\"data:')) {
txt =
'<text removed due to base-64 data, displaying the text could cause the app to crash>';
}
Expand Down
10 changes: 5 additions & 5 deletions lib/src/widgets/html_editor_widget_mobile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -601,10 +601,10 @@ class _HtmlEditorWidgetMobileState extends State<HtmlEditorWidget> {
});
""");
}
if (c.onBlur != null) {
if (c.onUnFocus != null) {
widget.controller.editorController!.evaluateJavascript(source: """
\$('#summernote-2').on('summernote.blur', function() {
window.flutter_inappwebview.callHandler('onBlur', 'fired');
window.flutter_inappwebview.callHandler('onUnFocus', 'fired');
});
""");
}
Expand Down Expand Up @@ -697,11 +697,11 @@ class _HtmlEditorWidgetMobileState extends State<HtmlEditorWidget> {
c.onFocus!.call();
});
}
if (c.onBlur != null) {
if (c.onUnFocus != null) {
widget.controller.editorController!.addJavaScriptHandler(
handlerName: 'onBlur',
handlerName: 'onUnFocus',
callback: (_) {
c.onBlur!.call();
c.onUnFocus!.call();
});
}
if (c.onBlurCodeview != null) {
Expand Down
13 changes: 6 additions & 7 deletions lib/src/widgets/html_editor_widget_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export 'dart:html';

import 'dart:async';
import 'dart:convert';
import 'dart:html';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -64,8 +63,8 @@ class _HtmlEditorWidgetWebState extends State<HtmlEditorWidget> {

final _jsonEncoder = const JsonEncoder();

StreamSubscription<MessageEvent>? _editorJSListener;
StreamSubscription<MessageEvent>? _summernoteOnLoadListener;
StreamSubscription<html.MessageEvent>? _editorJSListener;
StreamSubscription<html.MessageEvent>? _summernoteOnLoadListener;
static const String _summernoteLoadedMessage = '_HtmlEditorWidgetWebState::summernoteLoaded';
final _focusNode = FocusNode();

Expand Down Expand Up @@ -719,10 +718,10 @@ class _HtmlEditorWidgetWebState extends State<HtmlEditorWidget> {
});\n
""";
}
if (c.onBlur != null) {
if (c.onUnFocus != null) {
callbacks =
"""$callbacks \$('#summernote-2').on('summernote.blur', function() {
window.parent.postMessage(JSON.stringify({"view": "$createdViewId", "type": "toDart: onBlur"}), "*");
window.parent.postMessage(JSON.stringify({"view": "$createdViewId", "type": "toDart: onUnFocus"}), "*");
});\n
""";
}
Expand Down Expand Up @@ -819,8 +818,8 @@ class _HtmlEditorWidgetWebState extends State<HtmlEditorWidget> {
_focusNode.requestFocus();
c.onFocus!.call();
}
if (data['type'].contains('onBlur')) {
c.onBlur!.call();
if (data['type'].contains('onUnFocus')) {
c.onUnFocus!.call();
}
if (data['type'].contains('onBlurCodeview')) {
c.onBlurCodeview!.call();
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/callbacks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Callbacks {
this.onDialogShown,
this.onEnter,
this.onFocus,
this.onBlur,
this.onUnFocus,
this.onBlurCodeview,
this.onImageLinkInsert,
this.onImageUpload,
Expand Down Expand Up @@ -92,7 +92,7 @@ class Callbacks {
/// the webview or dismissing the keyboard does not trigger this callback.
/// This callback will only be triggered if the user taps on an empty space
/// in the toolbar or switches the view mode of the editor.
void Function()? onBlur;
void Function()? onUnFocus;

/// Called whenever the code view either gains or loses focus (the Summernote
/// docs say this will only be called when the code view loses focus but
Expand Down

0 comments on commit 158e2b7

Please sign in to comment.