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 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
/// Contains information about the text currently being shown in a
|
||||
/// 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 {
|
||||
/// The currently selected text in the overlay message.
|
||||
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 =
|
||||
StreamController<String?>.broadcast();
|
||||
|
||||
void setMessageText(String text) {
|
||||
messageText = text;
|
||||
}
|
||||
/// Sets messageText to match the text currently being displayed in the overlay.
|
||||
/// 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
|
||||
? clearTextSelection()
|
||||
: setTextSelection(selection);
|
||||
/// Clears the messageText value. Called when the message selection overlay is closed.
|
||||
void clearMessageText() => messageText = null;
|
||||
|
||||
void setTextSelection(TextSelection selection) {
|
||||
selectedText = selection.textInside(messageText);
|
||||
if (BrowserContextMenu.enabled && kIsWeb) {
|
||||
BrowserContextMenu.disableContextMenu();
|
||||
}
|
||||
/// Updates the selectedText value and emits it to the selectionStream.
|
||||
void onSelection(String? text) {
|
||||
text == null || text.isEmpty ? selectedText = null : selectedText = text;
|
||||
selectionStream.add(selectedText);
|
||||
}
|
||||
|
||||
void clearTextSelection() {
|
||||
selectedText = null;
|
||||
if (kIsWeb && !BrowserContextMenu.enabled) {
|
||||
BrowserContextMenu.enableContextMenu();
|
||||
}
|
||||
selectionStream.add(selectedText);
|
||||
/// Returns the index of the selected text within the message text.
|
||||
/// If the selected text is not found, returns null.
|
||||
int? get offset {
|
||||
if (selectedText == null || messageText == null) return null;
|
||||
final index = messageText!.indexOf(selectedText!);
|
||||
return index > -1 ? index : null;
|
||||
}
|
||||
|
||||
int get offset => messageText.indexOf(selectedText!);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue