Merge pull request #654 from pangeachat/html-message-toolbar
enable toolbar on click for html messagespull/1384/head
commit
2a83d3099f
@ -1,37 +1,41 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart';
|
/// Contains information about the text currently being shown in a
|
||||||
import 'package:flutter/services.dart';
|
/// toolbar overlay message and any selection within that text.
|
||||||
|
/// The ChatController contains one instance of this class, and it's values
|
||||||
|
/// should be updated each time an overlay is openned or closed, or when
|
||||||
|
/// an overlay's text selection changes.
|
||||||
class MessageTextSelection {
|
class MessageTextSelection {
|
||||||
|
/// The currently selected text in the overlay message.
|
||||||
String? selectedText;
|
String? selectedText;
|
||||||
String messageText = "";
|
|
||||||
|
/// The full text displayed in the overlay message.
|
||||||
|
String? messageText;
|
||||||
|
|
||||||
|
/// A stream that emits the currently selected text whenever it changes.
|
||||||
final StreamController<String?> selectionStream =
|
final StreamController<String?> selectionStream =
|
||||||
StreamController<String?>.broadcast();
|
StreamController<String?>.broadcast();
|
||||||
|
|
||||||
void setMessageText(String text) {
|
/// Sets messageText to match the text currently being displayed in the overlay.
|
||||||
messageText = text;
|
/// Text in messages is displayed in a variety of ways, i.e., direct message content,
|
||||||
}
|
/// translation, HTML rendered message, etc. This method should be called wherever the
|
||||||
|
/// text displayed in the overlay is determined.
|
||||||
|
void setMessageText(String text) => messageText = text;
|
||||||
|
|
||||||
void onTextSelection(TextSelection selection) => selection.isCollapsed == true
|
/// Clears the messageText value. Called when the message selection overlay is closed.
|
||||||
? clearTextSelection()
|
void clearMessageText() => messageText = null;
|
||||||
: setTextSelection(selection);
|
|
||||||
|
|
||||||
void setTextSelection(TextSelection selection) {
|
/// Updates the selectedText value and emits it to the selectionStream.
|
||||||
selectedText = selection.textInside(messageText);
|
void onSelection(String? text) {
|
||||||
if (BrowserContextMenu.enabled && kIsWeb) {
|
text == null || text.isEmpty ? selectedText = null : selectedText = text;
|
||||||
BrowserContextMenu.disableContextMenu();
|
|
||||||
}
|
|
||||||
selectionStream.add(selectedText);
|
selectionStream.add(selectedText);
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearTextSelection() {
|
/// Returns the index of the selected text within the message text.
|
||||||
selectedText = null;
|
/// If the selected text is not found, returns null.
|
||||||
if (kIsWeb && !BrowserContextMenu.enabled) {
|
int? get offset {
|
||||||
BrowserContextMenu.enableContextMenu();
|
if (selectedText == null || messageText == null) return null;
|
||||||
}
|
final index = messageText!.indexOf(selectedText!);
|
||||||
selectionStream.add(selectedText);
|
return index > -1 ? index : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get offset => messageText.indexOf(selectedText!);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue