Skip to content

Commit

Permalink
fix line length #15
Browse files Browse the repository at this point in the history
  • Loading branch information
codenameakshay committed May 25, 2021
1 parent f8650aa commit 4eaaca6
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 212 deletions.
20 changes: 6 additions & 14 deletions lib/format_markdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ class FormatMarkdown {
/// Convert [data] part into [ResultMarkdown] from [type].
/// Use [fromIndex] and [toIndex] for converting part of [data]
/// [titleSize] is used for markdown titles
static ResultMarkdown convertToMarkdown(
MarkdownType type, String data, int fromIndex, int toIndex,
static ResultMarkdown convertToMarkdown(MarkdownType type, String data, int fromIndex, int toIndex,
{int titleSize = 1}) {
late String changedData;
late int replaceCursorIndex;
Expand All @@ -23,13 +22,11 @@ class FormatMarkdown {
replaceCursorIndex = 2;
break;
case MarkdownType.link:
changedData =
'[${data.substring(fromIndex, toIndex)}](${data.substring(fromIndex, toIndex)})';
changedData = '[${data.substring(fromIndex, toIndex)}](${data.substring(fromIndex, toIndex)})';
replaceCursorIndex = 3;
break;
case MarkdownType.title:
changedData =
"${"#" * titleSize} ${data.substring(fromIndex, toIndex)}";
changedData = "${"#" * titleSize} ${data.substring(fromIndex, toIndex)}";
replaceCursorIndex = 0;
break;
case MarkdownType.list:
Expand Down Expand Up @@ -59,20 +56,15 @@ class FormatMarkdown {
replaceCursorIndex = 0;
break;
case MarkdownType.image:
changedData =
'![${data.substring(fromIndex, toIndex)}](${data.substring(fromIndex, toIndex)})';
changedData = '![${data.substring(fromIndex, toIndex)}](${data.substring(fromIndex, toIndex)})';
replaceCursorIndex = 3;
break;
}

final cursorIndex = changedData.length;

return ResultMarkdown(
data.substring(0, fromIndex) +
changedData +
data.substring(toIndex, data.length),
cursorIndex,
replaceCursorIndex);
return ResultMarkdown(data.substring(0, fromIndex) + changedData + data.substring(toIndex, data.length),
cursorIndex, replaceCursorIndex);
}
}

Expand Down
53 changes: 17 additions & 36 deletions lib/markdown_text_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,37 @@ class MarkdownTextInput extends StatefulWidget {

/// Constructor for [MarkdownTextInput]
MarkdownTextInput(this.onTextChanged, this.initialValue,
{this.label = '',
this.validators,
this.textDirection = TextDirection.ltr,
this.maxLines = 10});
{this.label = '', this.validators, this.textDirection = TextDirection.ltr, this.maxLines = 10});

@override
_MarkdownTextInputState createState() => _MarkdownTextInputState();
}

class _MarkdownTextInputState extends State<MarkdownTextInput> {
final _controller = TextEditingController();
TextSelection textSelection =
const TextSelection(baseOffset: 0, extentOffset: 0);
TextSelection textSelection = const TextSelection(baseOffset: 0, extentOffset: 0);

void onTap(MarkdownType type, {int titleSize = 1}) {
final basePosition = textSelection.baseOffset;
var noTextSelected =
(textSelection.baseOffset - textSelection.extentOffset) == 0;
var noTextSelected = (textSelection.baseOffset - textSelection.extentOffset) == 0;

final result = FormatMarkdown.convertToMarkdown(type, _controller.text,
textSelection.baseOffset, textSelection.extentOffset,
final result = FormatMarkdown.convertToMarkdown(
type, _controller.text, textSelection.baseOffset, textSelection.extentOffset,
titleSize: titleSize);

_controller.value = _controller.value.copyWith(
text: result.data,
selection:
TextSelection.collapsed(offset: basePosition + result.cursorIndex));
_controller.value = _controller.value
.copyWith(text: result.data, selection: TextSelection.collapsed(offset: basePosition + result.cursorIndex));

if (noTextSelected) {
_controller.selection = TextSelection.collapsed(
offset: _controller.selection.end - result.replaceCursorIndex);
_controller.selection = TextSelection.collapsed(offset: _controller.selection.end - result.replaceCursorIndex);
}
}

@override
void initState() {
_controller.text = widget.initialValue;
_controller.addListener(() {
if (_controller.selection.baseOffset != -1)
textSelection = _controller.selection;
if (_controller.selection.baseOffset != -1) textSelection = _controller.selection;
widget.onTextChanged(_controller.text);
});
super.initState();
Expand Down Expand Up @@ -94,24 +85,18 @@ class _MarkdownTextInputState extends State<MarkdownTextInput> {
cursorColor: Theme.of(context).primaryColor,
textDirection: widget.textDirection,
decoration: InputDecoration(
enabledBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Theme.of(context).accentColor)),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Theme.of(context).accentColor)),
enabledBorder: UnderlineInputBorder(borderSide: BorderSide(color: Theme.of(context).accentColor)),
focusedBorder: UnderlineInputBorder(borderSide: BorderSide(color: Theme.of(context).accentColor)),
hintText: widget.label,
hintStyle:
const TextStyle(color: Color.fromRGBO(63, 61, 86, 0.5)),
contentPadding:
const EdgeInsets.symmetric(vertical: 15, horizontal: 10),
hintStyle: const TextStyle(color: Color.fromRGBO(63, 61, 86, 0.5)),
contentPadding: const EdgeInsets.symmetric(vertical: 15, horizontal: 10),
),
),
SizedBox(
height: 44,
child: Material(
color: Theme.of(context).cardColor,
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(10),
bottomRight: Radius.circular(10)),
borderRadius: const BorderRadius.only(bottomLeft: Radius.circular(10), bottomRight: Radius.circular(10)),
child: ListView(
scrollDirection: Axis.horizontal,
children: [
Expand Down Expand Up @@ -164,8 +149,7 @@ class _MarkdownTextInputState extends State<MarkdownTextInput> {
padding: EdgeInsets.all(10),
child: Text(
'H#',
style: TextStyle(
fontSize: 16, fontWeight: FontWeight.w700),
style: TextStyle(fontSize: 16, fontWeight: FontWeight.w700),
),
),
),
Expand All @@ -177,15 +161,12 @@ class _MarkdownTextInputState extends State<MarkdownTextInput> {
for (int i = 1; i <= 6; i++)
InkWell(
key: Key('H${i}_button'),
onTap: () =>
onTap(MarkdownType.title, titleSize: i),
onTap: () => onTap(MarkdownType.title, titleSize: i),
child: Padding(
padding: const EdgeInsets.all(10),
child: Text(
'H$i',
style: TextStyle(
fontSize: (18 - i).toDouble(),
fontWeight: FontWeight.w700),
style: TextStyle(fontSize: (18 - i).toDouble(), fontWeight: FontWeight.w700),
),
),
),
Expand Down
Loading

0 comments on commit 4eaaca6

Please sign in to comment.