Skip to content

Commit

Permalink
Add simple markdown toolbar
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Oct 30, 2023
1 parent 8c310aa commit 61275a8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
11 changes: 10 additions & 1 deletion app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -270,5 +270,14 @@
},
"deleteLabelDescription": "Are you sure you want to delete the label {name}?",
"label": "Label",
"startOfWeek": "Start of week"
"startOfWeek": "Start of week",
"headlineNumber": "Headline {number}",
"@headlineNumber": {
"placeholders": {
"number": {
"type": "int",
"example": "1"
}
}
}
}
51 changes: 49 additions & 2 deletions app/lib/pages/notes/card.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:math';

import 'package:collection/collection.dart';
import 'package:flow/helpers/sourced_paging_controller.dart';
import 'package:flow/widgets/color.dart';
import 'package:flow/widgets/markdown_field.dart';
Expand Down Expand Up @@ -36,8 +39,10 @@ class NoteCard extends StatefulWidget {
State<NoteCard> createState() => _NoteCardState();
}

enum PastePositing { line, selection }

class _NoteCardState extends State<NoteCard> {
late final TextEditingController _nameController;
late final TextEditingController _nameController, _descriptionController;
late Note _newNote;
late final FlowCubit _cubit;
late final SourceService _sourceService;
Expand All @@ -52,6 +57,8 @@ class _NoteCardState extends State<NoteCard> {
void initState() {
super.initState();
_nameController = TextEditingController(text: widget.note.name);
_descriptionController =
TextEditingController(text: widget.note.description);
_newNote = widget.note;
_cubit = context.read<FlowCubit>();
_sourceService = _cubit.getService(widget.source);
Expand Down Expand Up @@ -85,6 +92,26 @@ class _NoteCardState extends State<NoteCard> {
}
}

void _addDescription(PastePositing position, String text) {
var description = _descriptionController.text;
final selection = _descriptionController.selection;
if (!selection.isValid) return;
final start = selection.baseOffset;
final lineStart = max(0, description.lastIndexOf("\n", start));
description = switch (position) {
PastePositing.line => description.substring(0, lineStart) +
text +
description.substring(lineStart),
PastePositing.selection => description.substring(0, start) +
text +
description.substring(selection.extentOffset),
};
_descriptionController.text = description;
_descriptionController.selection = TextSelection.collapsed(
offset: start + text.length,
);
}

@override
Widget build(BuildContext context) {
return Card(
Expand Down Expand Up @@ -302,12 +329,32 @@ class _NoteCardState extends State<NoteCard> {
]),
),
const SizedBox(height: 16),
if (widget.primary)
SizedBox(
height: 50,
child: ListView(scrollDirection: Axis.horizontal, children: [
...[
PhosphorIconsLight.textHOne,
PhosphorIconsLight.textHTwo,
PhosphorIconsLight.textHThree,
PhosphorIconsLight.textHFour,
PhosphorIconsLight.textHFive,
PhosphorIconsLight.textHSix
].mapIndexed((index, element) => IconButton(
icon: PhosphorIcon(element),
onPressed: () => _addDescription(
PastePositing.line,
"${"#" * (index + 1)} ",
),
))
]),
),
MarkdownField(
decoration: InputDecoration(
labelText: AppLocalizations.of(context).description,
border: const OutlineInputBorder(),
),
value: _newNote.description,
controller: _descriptionController,
onChangeEnd: (value) {
_newNote = _newNote.copyWith(description: value);
_updateNote();
Expand Down
13 changes: 8 additions & 5 deletions app/lib/widgets/markdown_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import 'package:url_launcher/url_launcher_string.dart';
import 'package:markdown/markdown.dart' as md;

class MarkdownField extends StatefulWidget {
final String value;
final String? value;
final InputDecoration decoration;
final TextEditingController? controller;
final ValueChanged<String>? onChanged, onChangeEnd;
final List<Widget> actions;

const MarkdownField(
{super.key,
required this.value,
this.value,
this.controller,
this.onChanged,
this.onChangeEnd,
this.decoration = const InputDecoration(),
Expand All @@ -23,14 +25,15 @@ class MarkdownField extends StatefulWidget {
}

class _MarkdownFieldState extends State<MarkdownField> {
final TextEditingController _controller = TextEditingController();
late final TextEditingController _controller;
bool _editMode = false;
final FocusNode _focusNode = FocusNode();

@override
void initState() {
super.initState();
_controller.text = widget.value;
_controller =
widget.controller ?? TextEditingController(text: widget.value);
_focusNode.addListener(() {
if (!_focusNode.hasFocus) {
_exitEditMode();
Expand All @@ -42,7 +45,7 @@ class _MarkdownFieldState extends State<MarkdownField> {
void didUpdateWidget(MarkdownField oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.value != widget.value) {
_controller.text = widget.value;
_controller.text = widget.value ?? _controller.text;
}
}

Expand Down
1 change: 0 additions & 1 deletion app/lib/widgets/source_dropdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class SourceDropdown<T> extends StatelessWidget {
})
.whereNotNull()
.toList());
final remotes = services.keys.map((e) => cubit.sourcesService.getRemote(e));
return Column(
children: [
const SizedBox(height: 16),
Expand Down

0 comments on commit 61275a8

Please sign in to comment.