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

Add requestFocusNode method to support insert image when editor unfocus #27

Merged
merged 1 commit into from
Feb 2, 2024
Merged
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
40 changes: 28 additions & 12 deletions lib/src/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -415,23 +415,39 @@ class HtmlEditorState extends State<HtmlEditor> {
}
}

function moveCursorAtLastNode() {
var nodeSignature = document.getElementsByClassName('tmail-signature');
var editor = document.getElementById('editor');
const textnode = document.createTextNode('');
editor.appendChild(textnode);
var lastChild;
function requestFocusLastNode() {
const nodeSignature = document.getElementsByClassName('tmail-signature');
const editor = document.getElementById('editor');
const textNode = document.createTextNode('');
editor.appendChild(textNode);
editor.focus();

const range = document.createRange();
const sel = window.getSelection();

var lastChild;
if (nodeSignature.length <= 0) {
lastChild = editor.lastChild;
lastChild = editor.lastElementChild;
range.setStartAfter(lastChild);
} else {
var position = editor.childNodes.length - 1;
lastChild = editor.childNodes[position];
lastChild = nodeSignature[0];
range.setStart(lastChild, 0);
}
range.collapse(true);

sel.removeAllRanges();
sel.addRange(range);
}

function requestFocusFirstNode() {
const editor = document.getElementById('editor');
const textNode = document.createTextNode('');
editor.appendChild(textNode);
editor.focus();

var range = document.createRange();
var sel = window.getSelection();
range.setStart(lastChild, 0);
const range = document.createRange();
const sel = window.getSelection();
range.setStart(editor.firstElementChild, 0);
range.collapse(true);

sel.removeAllRanges();
Expand Down
27 changes: 20 additions & 7 deletions lib/src/editor_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,6 @@ class HtmlEditorApi {
Future<void> replaceSignatureContent() => _webViewController
.evaluateJavascript(source: 'replaceSignatureContent();');

/// MoveCursorAtLastNode
Future<void> moveCursorAtLastNode() => _webViewController
.evaluateJavascript(source: 'moveCursorAtLastNode();');

/// checkHasFocus
Future<bool?> hasFocus() async {
final check = await _webViewController
Expand All @@ -456,9 +452,26 @@ class HtmlEditorApi {
return signature;
}

/// Focus to editor - ONLY FOR IOS
/// Focus to editor at first child
Future<void> requestFocus() async {
await _webViewController
.evaluateJavascript(source: "document.getElementById('editor').focus();");
if (Platform.isIOS) {
await _webViewController
.evaluateJavascript(source: "document.getElementById('editor').focus();");
} else if (Platform.isAndroid) {
await _webViewController
.evaluateJavascript(source: 'requestFocusFirstNode();');
} else {
log('NOT SUPPORTED PLATFORM');
}
}

/// Focus to editor at last child
Future<void> requestFocusLastChild() async {
if (Platform.isIOS || Platform.isAndroid) {
await _webViewController
.evaluateJavascript(source: 'requestFocusLastNode();');
} else {
log('NOT SUPPORTED PLATFORM');
}
}
}
Loading