From b551bcd3a9ecc356d8e97a5c35bb66bee8b80a2a Mon Sep 17 00:00:00 2001 From: Micah Morrison Date: Tue, 21 May 2024 15:40:30 -0400 Subject: [PATCH 1/2] Support alternative text selection --- lib/src/format_markdown.dart | 5 ++++- lib/src/markdown_toolbar.dart | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/src/format_markdown.dart b/lib/src/format_markdown.dart index a178a74..2eae30e 100644 --- a/lib/src/format_markdown.dart +++ b/lib/src/format_markdown.dart @@ -11,6 +11,9 @@ class FormatMarkdown { /// String to convert String data, + /// An alternate data source on which to perform formatting + String? alternateData, + /// Start index when converting part of [data] int fromIndex, @@ -71,7 +74,7 @@ class FormatMarkdown { break; case MarkdownType.blockquote: var index = 0; - final splitedData = data.substring(fromIndex, toIndex).split('\n'); + final splitedData = (alternateData ?? data.substring(fromIndex, toIndex)).split('\n'); changedData = splitedData.map((value) { index++; return index == splitedData.length ? '> $value' : '> $value\n'; diff --git a/lib/src/markdown_toolbar.dart b/lib/src/markdown_toolbar.dart index 65d3aa1..e460c9b 100644 --- a/lib/src/markdown_toolbar.dart +++ b/lib/src/markdown_toolbar.dart @@ -33,6 +33,9 @@ class MarkdownToolbar extends StatelessWidget { /// Allows overriding tap actions for each [MarkdownType] final Map? 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, @@ -43,6 +46,7 @@ class MarkdownToolbar extends StatelessWidget { this.customImageButtonAction, this.imageIsLoading = false, this.customTapActions, + this.getAlternativeSelection, }); @override @@ -223,6 +227,7 @@ class MarkdownToolbar extends StatelessWidget { ResultMarkdown result = FormatMarkdown.convertToMarkdown( type, controller.text, + getAlternativeSelection?.call(), fromIndex, toIndex, titleSize: titleSize, From 1e39dd72cce89457ece47bdddf88e0dfe82984f0 Mon Sep 17 00:00:00 2001 From: Micah Morrison Date: Tue, 21 May 2024 16:32:29 -0400 Subject: [PATCH 2/2] Fix tests --- lib/src/format_markdown.dart | 8 ++++---- lib/src/markdown_toolbar.dart | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/src/format_markdown.dart b/lib/src/format_markdown.dart index 2eae30e..4c307ca 100644 --- a/lib/src/format_markdown.dart +++ b/lib/src/format_markdown.dart @@ -11,9 +11,6 @@ class FormatMarkdown { /// String to convert String data, - /// An alternate data source on which to perform formatting - String? alternateData, - /// Start index when converting part of [data] int fromIndex, @@ -27,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; @@ -74,7 +74,7 @@ class FormatMarkdown { break; case MarkdownType.blockquote: var index = 0; - final splitedData = (alternateData ?? 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'; diff --git a/lib/src/markdown_toolbar.dart b/lib/src/markdown_toolbar.dart index e460c9b..71edd30 100644 --- a/lib/src/markdown_toolbar.dart +++ b/lib/src/markdown_toolbar.dart @@ -227,12 +227,12 @@ class MarkdownToolbar extends StatelessWidget { ResultMarkdown result = FormatMarkdown.convertToMarkdown( type, controller.text, - getAlternativeSelection?.call(), fromIndex, toIndex, 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));