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

Support alternative text selection #10

Merged
merged 2 commits into from
Jun 4, 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
5 changes: 4 additions & 1 deletion lib/src/format_markdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class FormatMarkdown {

/// The curently selected text
String selectedText = '',

/// An alternate data source on which to perform formatting
String? alternateData,
}) {
late String changedData;
late int replaceCursorIndex;
Expand Down Expand Up @@ -71,7 +74,7 @@ class FormatMarkdown {
break;
case MarkdownType.blockquote:
var index = 0;
final splitedData = data.substring(fromIndex, toIndex).split('\n');
final splitedData = (data.isEmpty ? (alternateData ?? '') : data.substring(fromIndex, toIndex)).split('\n');
changedData = splitedData.map((value) {
index++;
return index == splitedData.length ? '> $value' : '> $value\n';
Expand Down
5 changes: 5 additions & 0 deletions lib/src/markdown_toolbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class MarkdownToolbar extends StatelessWidget {
/// Allows overriding tap actions for each [MarkdownType]
final Map<MarkdownType, void Function()>? customTapActions;

/// A function which allows the widget's parent to provide an alternate text selection
final String? Function()? getAlternativeSelection;

/// Constructor for [MarkdownToolbar]
const MarkdownToolbar({
super.key,
Expand All @@ -43,6 +46,7 @@ class MarkdownToolbar extends StatelessWidget {
this.customImageButtonAction,
this.imageIsLoading = false,
this.customTapActions,
this.getAlternativeSelection,
});

@override
Expand Down Expand Up @@ -228,6 +232,7 @@ class MarkdownToolbar extends StatelessWidget {
titleSize: titleSize,
link: link,
selectedText: selectedText ?? controller.text.substring(fromIndex, toIndex),
alternateData: getAlternativeSelection?.call(),
);

controller.value = controller.value.copyWith(text: result.data, selection: TextSelection.collapsed(offset: fromIndex + result.cursorIndex));
Expand Down
Loading