fixup! Add text selection mixin
This commit is contained in:
@@ -52,6 +52,24 @@ class HtmlUtils {
|
||||
script: '''
|
||||
let lastSelectedText = '';
|
||||
|
||||
const sendSelectionChangeMessage = (data) => {
|
||||
// When iframe
|
||||
if (window.parent) {
|
||||
window.parent.postMessage(
|
||||
JSON.stringify({
|
||||
...data,
|
||||
name: 'onSelectionChange',
|
||||
}),
|
||||
"*"
|
||||
)
|
||||
}
|
||||
|
||||
// When WebView
|
||||
if (window.flutter_inappwebview) {
|
||||
window.flutter_inappwebview.callHandler('onSelectionChange', data);
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('selectionchange', function() {
|
||||
const selection = window.getSelection();
|
||||
const selectedText = selection ? selection.toString().trim() : '';
|
||||
@@ -62,33 +80,14 @@ class HtmlUtils {
|
||||
lastSelectedText = selectedText;
|
||||
|
||||
if (selectedText.length > 0 && selection.rangeCount > 0) {
|
||||
const range = selection.getRangeAt(0);
|
||||
const rects = range.getClientRects();
|
||||
try {
|
||||
const range = selection.getRangeAt(0);
|
||||
const rects = range.getClientRects();
|
||||
|
||||
if (rects.length > 0) {
|
||||
const rect = rects[rects.length - 1];
|
||||
if (rects.length > 0) {
|
||||
const rect = rects[rects.length - 1];
|
||||
|
||||
// When iframe
|
||||
if (window.parent) {
|
||||
window.parent.postMessage(
|
||||
JSON.stringify({
|
||||
name: 'onSelectionChange',
|
||||
hasSelection: true,
|
||||
selectedText: selectedText,
|
||||
coordinates: {
|
||||
x: rect.right,
|
||||
y: rect.bottom,
|
||||
width: rect.width,
|
||||
height: rect.height
|
||||
}
|
||||
}),
|
||||
"*"
|
||||
)
|
||||
}
|
||||
|
||||
// When WebView
|
||||
if (window.flutter_inappwebview) {
|
||||
window.flutter_inappwebview.callHandler('onSelectionChange', {
|
||||
sendSelectionChangeMessage({
|
||||
hasSelection: true,
|
||||
selectedText: selectedText,
|
||||
coordinates: {
|
||||
@@ -97,27 +96,17 @@ class HtmlUtils {
|
||||
width: rect.width,
|
||||
height: rect.height
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
} catch {
|
||||
sendSelectionChangeMessage({
|
||||
hasSelection: false
|
||||
})
|
||||
}
|
||||
} else {
|
||||
// When iframe
|
||||
if (window.parent) {
|
||||
window.parent.postMessage(
|
||||
JSON.stringify({
|
||||
name: 'onSelectionChange',
|
||||
hasSelection: false
|
||||
}),
|
||||
"*"
|
||||
)
|
||||
}
|
||||
|
||||
// When WebView
|
||||
if (window.flutter_inappwebview) {
|
||||
window.flutter_inappwebview.callHandler('onSelectionChange', {
|
||||
hasSelection: false
|
||||
});
|
||||
}
|
||||
sendSelectionChangeMessage({
|
||||
hasSelection: false
|
||||
})
|
||||
}
|
||||
});''',
|
||||
name: 'onSelectionChange');
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
typedef OnTextSelectionChanged = Function(TextSelectionData?);
|
||||
|
||||
class TextSelectionData {
|
||||
final bool hasSelection;
|
||||
final String? selectedText;
|
||||
@@ -68,7 +70,7 @@ class TextSelectionCoordinates {
|
||||
}
|
||||
|
||||
mixin TextSelectionMixin<T extends StatefulWidget> on State<T> {
|
||||
void Function(TextSelectionData?)? get onSelectionChanged => null;
|
||||
OnTextSelectionChanged? get onSelectionChanged => null;
|
||||
|
||||
void handleSelectionChange(Map<dynamic, dynamic> data) {
|
||||
final selectionData = TextSelectionData.fromMap(data);
|
||||
|
||||
Reference in New Issue
Block a user