diff --git a/lib/widgets/note_viewer.dart b/lib/widgets/note_viewer.dart index e87c2860..493edb3c 100644 --- a/lib/widgets/note_viewer.dart +++ b/lib/widgets/note_viewer.dart @@ -7,6 +7,7 @@ import 'package:flutter_markdown/flutter_markdown.dart'; import 'package:gitjournal/folder_views/common.dart'; import 'package:gitjournal/utils.dart'; import 'package:gitjournal/widgets/editor_scroll_view.dart'; +import 'package:gitjournal/widgets/notes_backlinks.dart'; import 'package:provider/provider.dart'; import 'package:url_launcher/url_launcher.dart'; import 'package:path/path.dart' as p; @@ -182,86 +183,3 @@ Widget _handleDataSchemeUri(Uri uri, final double width, final double height) { } return const SizedBox(); } - -class NoteBacklinkRenderer extends StatefulWidget { - final Note note; - final NotesFolderFS rootFolder; - - NoteBacklinkRenderer({ - @required this.note, - @required this.rootFolder, - }); - - @override - _NoteBacklinkRendererState createState() => _NoteBacklinkRendererState(); -} - -class _NoteBacklinkRendererState extends State { - List linkedNotes = []; - - @override - void initState() { - super.initState(); - - _initStateAsync(); - } - - Future _initStateAsync() async { - var predicate = (Note n) async { - var links = await n.fetchLinks(); - var matchedLink = links.firstWhere( - (l) => l.filePath == widget.note.filePath, - orElse: () => null, - ); - return matchedLink != null; - }; - - var l = await widget.rootFolder.matchNotes(predicate); - if (!mounted) return; - setState(() { - linkedNotes = l; - print("linkedNote $linkedNotes"); - }); - } - - @override - Widget build(BuildContext context) { - if (linkedNotes.isEmpty) { - return Container(); - } - - var textTheme = Theme.of(context).textTheme; - var c = Column( - children: [ - Text('BackLinks', style: textTheme.headline5), - const SizedBox(height: 8.0), - for (var n in linkedNotes) _buildNoteLink(n), - ], - mainAxisSize: MainAxisSize.min, - crossAxisAlignment: CrossAxisAlignment.start, - ); - - return Padding( - padding: const EdgeInsets.fromLTRB(0.0, 16.0, 0.0, 16.0), - child: c, - ); - } - - Widget _buildNoteLink(Note note) { - var textTheme = Theme.of(context).textTheme; - var title = note.title; - if (title.isEmpty) { - title = note.fileName; - } - - return Padding( - padding: const EdgeInsets.fromLTRB(0.0, 8.0, 0, 8.0), - child: GestureDetector( - onTap: () { - openNoteEditor(context, note); - }, - child: Text('- $title', style: textTheme.bodyText1), - ), - ); - } -} diff --git a/lib/widgets/notes_backlinks.dart b/lib/widgets/notes_backlinks.dart new file mode 100644 index 00000000..0abe16fa --- /dev/null +++ b/lib/widgets/notes_backlinks.dart @@ -0,0 +1,99 @@ +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; +import 'package:gitjournal/folder_views/common.dart'; + +import 'package:gitjournal/core/note.dart'; +import 'package:gitjournal/core/notes_folder_fs.dart'; + +class NoteBacklinkRenderer extends StatefulWidget { + final Note note; + final NotesFolderFS rootFolder; + + NoteBacklinkRenderer({ + @required this.note, + @required this.rootFolder, + }); + + @override + _NoteBacklinkRendererState createState() => _NoteBacklinkRendererState(); +} + +class _NoteBacklinkRendererState extends State { + List linkedNotes = []; + + @override + void initState() { + super.initState(); + + _initStateAsync(); + } + + Future _initStateAsync() async { + var predicate = (Note n) async { + var links = await n.fetchLinks(); + var matchedLink = links.firstWhere( + (l) => l.filePath == widget.note.filePath, + orElse: () => null, + ); + return matchedLink != null; + }; + + var l = await widget.rootFolder.matchNotes(predicate); + if (!mounted) return; + setState(() { + linkedNotes = l; + }); + } + + @override + Widget build(BuildContext context) { + if (linkedNotes.isEmpty) { + return Container(); + } + + var textTheme = Theme.of(context).textTheme; + var c = Column( + children: [ + Text('BackLinks', style: textTheme.headline5), + const SizedBox(height: 8.0), + for (var n in linkedNotes) + NoteSnippet(n, () { + openNoteEditor(context, n); + }), + ], + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, + ); + + return Padding( + padding: const EdgeInsets.fromLTRB(0.0, 16.0, 0.0, 16.0), + child: c, + ); + } +} + +class NoteSnippet extends StatelessWidget { + final Note note; + final Function onTap; + + NoteSnippet(this.note, this.onTap); + + @override + Widget build(BuildContext context) { + var textTheme = Theme.of(context).textTheme; + var title = note.title; + if (title.isEmpty) { + title = note.fileName; + } + + return Padding( + padding: const EdgeInsets.fromLTRB(0.0, 8.0, 0, 8.0), + child: GestureDetector( + onTap: () { + openNoteEditor(context, note); + }, + child: Text('- $title', style: textTheme.bodyText1), + ), + ); + } +}