Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid onBlur method called twice when editor loses focus #46

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 7 additions & 13 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,10 +63,9 @@ 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();

@override
void initState() {
Expand Down Expand Up @@ -676,9 +674,7 @@ class _HtmlEditorWidgetWebState extends State<HtmlEditorWidget> {
),
);

return Focus(
focusNode: _focusNode,
child: child);
return child;
}

/// Adds the callbacks the user set into JavaScript
Expand Down Expand Up @@ -719,10 +715,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 @@ -816,11 +812,10 @@ class _HtmlEditorWidgetWebState extends State<HtmlEditorWidget> {
c.onEnter!.call();
}
if (data['type'].contains('onFocus')) {
_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 Expand Up @@ -996,7 +991,6 @@ class _HtmlEditorWidgetWebState extends State<HtmlEditorWidget> {
void dispose() {
_editorJSListener?.cancel();
_summernoteOnLoadListener?.cancel();
_focusNode.dispose();
super.dispose();
}
}
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
Loading