Skip to content

Commit 96d104a

Browse files
committed
added splittedMarkdownField, improved MarkdownAutoPreview
1 parent b679863 commit 96d104a

12 files changed

+520
-229
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.2.11 - 10-June-2022
2+
* Breaking: MarkdownFormField is now MarkdownAutoPreview
3+
* Added SplittedMarkdown FormField
4+
* Example updated
5+
* Added tootips in markdown toolbar
6+
17
## 0.2.10
28
* Switch between markdown and textfield using focusNode reimplemented
39
* Dependency upgrade

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,15 @@ Initialize controller and focus node. These controllers and focus nodes are opti
3838

3939
```dart
4040
TextEditingController _controller = TextEditingController();
41-
FocusNode _focusNode = FocusNode();
4241
```
4342

4443
Show widget for editor
4544

4645
```dart
4746
// editable text with toolbar by default
48-
MarkdownFormField(
47+
MarkdownAutoPreview(
4948
controller: _controller,
5049
emojiConvert: true,
51-
autoCloseAfterSelectEmoji: false,
5250
)
5351
5452
// editable text without toolbar
@@ -86,7 +84,7 @@ MarkdownParse(
8684
In order to set the colors
8785

8886
```dart
89-
MarkdownFormField(
87+
MarkdownAutoPreview(
9088
controller: _controller,
9189
enableToolBar: true,
9290
emojiConvert: true,

example/lib/main.dart

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,6 @@ class HomeScreen extends StatefulWidget {
2525
}
2626

2727
class _HomeScreenState extends State<HomeScreen> {
28-
final TextEditingController _controller = TextEditingController();
29-
30-
@override
31-
void dispose() {
32-
_controller.dispose();
33-
super.dispose();
34-
}
35-
3628
@override
3729
Widget build(BuildContext context) {
3830
return Scaffold(
@@ -41,37 +33,21 @@ class _HomeScreenState extends State<HomeScreen> {
4133
),
4234
body: Column(
4335
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
44-
children: [
45-
MarkdownFormField(
46-
decoration: const InputDecoration(
47-
hintText: 'Type your markdown string here',
36+
children: const [
37+
MarkdownAutoPreview(
38+
decoration: InputDecoration(
39+
hintText: 'Markdown Auto Preview',
4840
),
49-
controller: _controller,
5041
emojiConvert: true,
51-
autoCloseAfterSelectEmoji: false,
5242
// maxLines: 10,
5343
// minLines: 1,
5444
// expands: true,
55-
onChanged: (s) {
56-
setState(() {});
57-
},
5845
),
59-
SizedBox(
60-
height: 100,
61-
child: MarkdownParse(
62-
data: _controller.text,
63-
selectable: true,
64-
onTapHastag: (String name, String match) {
65-
// example : #hashtag
66-
// name => hashtag
67-
// match => #hashtag
68-
},
69-
onTapMention: (String name, String match) {
70-
// example : @mention
71-
// name => mention
72-
// match => #mention
73-
},
46+
SplittedMarkdownFormField(
47+
decoration: InputDecoration(
48+
hintText: 'Splitted Markdown FormField',
7449
),
50+
emojiConvert: true,
7551
),
7652
],
7753
),

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ packages:
115115
path: ".."
116116
relative: true
117117
source: path
118-
version: "0.2.10"
118+
version: "0.2.11"
119119
matcher:
120120
dependency: transitive
121121
description:

lib/markdown_editor_plus.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
library markdown_editor_plus;
22

3-
export 'widgets/markdown_form_field.dart';
3+
export 'widgets/markdown_auto_preview.dart';
44
export 'widgets/markdown_parse.dart';
55
export 'widgets/markdown_toolbar.dart';
66
export 'widgets/markdown_field.dart';
7+
export 'widgets/splitted_markdown_form_field.dart';

lib/src/toolbar.dart

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,16 @@ class ToolbarResult {
1414

1515
class Toolbar {
1616
final TextEditingController controller;
17-
final FocusNode focusNode;
18-
final ValueChanged<bool> isEditorFocused;
19-
20-
Toolbar(
21-
{required this.controller,
22-
required this.focusNode,
23-
required this.isEditorFocused});
24-
25-
// check if have selection text
26-
bool checkHasSelection() {
27-
return (controller.selection.baseOffset -
28-
controller.selection.extentOffset) !=
29-
0;
30-
}
17+
final VoidCallback? bringEditorToFocus;
18+
19+
Toolbar({required this.controller, this.bringEditorToFocus});
20+
21+
/// Returns true if controller contains selection text
22+
///
23+
/// else returns false
24+
bool get hasSelection =>
25+
(controller.selection.baseOffset - controller.selection.extentOffset) !=
26+
0;
3127

3228
// get selection text pffset
3329
TextSelection getSelection(TextSelection selection) {
@@ -40,27 +36,27 @@ class Toolbar {
4036
}
4137

4238
// toolbar action
43-
void action(
44-
String left,
45-
String right, {
46-
TextSelection? textSelection,
47-
}) {
48-
if (!focusNode.hasFocus) {
49-
print('Editor is not in focus');
50-
51-
isEditorFocused(true);
52-
focusNode.requestFocus();
53-
}
39+
void action(String left, String right, {TextSelection? textSelection}) {
40+
// Keep this as it is
41+
// Dont remove or place in the end
42+
bringEditorToFocus?.call();
43+
44+
// if (!focusNode.hasFocus) {
45+
// print('Editor is not in focus');
46+
47+
// isEditorFocused(true);
48+
// focusNode.requestFocus();
49+
// }
5450

5551
// default parameter
56-
final currentTextValue = controller.value.text;
57-
var selection = textSelection ?? controller.selection;
52+
final String currentTextValue = controller.value.text;
53+
TextSelection selection = textSelection ?? controller.selection;
5854
selection = getSelection(selection);
5955

60-
final middle = selection.textInside(currentTextValue);
61-
var selectionText = '$left$middle$right';
62-
var baseOffset = left.length + middle.length;
63-
var extentOffset = selection.extentOffset + left.length + right.length;
56+
final String middle = selection.textInside(currentTextValue);
57+
String selectionText = '$left$middle$right';
58+
int baseOffset = left.length + middle.length;
59+
int extentOffset = selection.extentOffset + left.length + right.length;
6460

6561
// check if middle text have char \n
6662
if (middle.split("\n").length > 1) {
@@ -80,7 +76,7 @@ class Toolbar {
8076
extentOffset = selection.extentOffset - (left.length + right.length);
8177
}
8278

83-
final newTextValue = selection.textBefore(currentTextValue) +
79+
final String newTextValue = selection.textBefore(currentTextValue) +
8480
selectionText +
8581
selection.textAfter(currentTextValue);
8682

@@ -106,12 +102,12 @@ class Toolbar {
106102
String right,
107103
int selection,
108104
) {
109-
final splitData = middle.split("\n");
110-
var index = 0;
111-
var resetLength = 0;
112-
var addLength = 0;
105+
final List<String> splitData = middle.split("\n");
106+
int index = 0;
107+
int resetLength = 0;
108+
int addLength = 0;
113109

114-
final selectionText = splitData.map((text) {
110+
final String selectionText = splitData.map((text) {
115111
index++;
116112
addLength += left.length + right.length;
117113

@@ -127,12 +123,12 @@ class Toolbar {
127123
addLength -= left.length + right.length;
128124
}
129125

130-
final newText = text.trim().isEmpty ? text : "$left$text$right";
126+
final String newText = text.trim().isEmpty ? text : "$left$text$right";
131127
return index == splitData.length ? newText : "$newText\n";
132128
}).join();
133129

134-
final baseOffset = addLength + (middle.length - (resetLength * 2));
135-
final extentOffset = selection + addLength - (resetLength * 2);
130+
final int baseOffset = addLength + (middle.length - (resetLength * 2));
131+
final int extentOffset = selection + addLength - (resetLength * 2);
136132

137133
return ToolbarResult(
138134
baseOffset: baseOffset,
@@ -142,19 +138,21 @@ class Toolbar {
142138
}
143139

144140
void selectSingleLine() {
145-
var currentPosition = controller.selection;
141+
TextSelection currentPosition = controller.selection;
146142
if (!currentPosition.isValid) {
147143
currentPosition = currentPosition.copyWith(
148144
baseOffset: 0,
149145
extentOffset: 0,
150146
);
151147
}
152-
var textBefore = currentPosition.textBefore(controller.text);
153-
var textAfter = currentPosition.textAfter(controller.text);
148+
String textBefore = currentPosition.textBefore(controller.text);
149+
String textAfter = currentPosition.textAfter(controller.text);
154150

155151
textBefore = textBefore.split("\n").last;
156152
textAfter = textAfter.split("\n")[0];
157-
final firstTextPosition = controller.text.indexOf(textBefore + textAfter);
153+
154+
final int firstTextPosition =
155+
controller.text.indexOf(textBefore + textAfter);
158156
controller.value = controller.value.copyWith(
159157
selection: TextSelection(
160158
baseOffset: firstTextPosition,

0 commit comments

Comments
 (0)