1
0
mirror of https://github.com/git-touch/git-touch synced 2025-03-25 15:40:09 +01:00

refactor: rename markdown view

This commit is contained in:
Rongjian Zhang 2020-11-04 16:41:04 +08:00
parent 78adfdfa88
commit 9db5a201a3
9 changed files with 121 additions and 121 deletions

View File

@ -79,7 +79,7 @@ class BbRepoScreen extends StatelessWidget {
Container(
padding: CommonStyle.padding,
color: theme.palette.background,
child: MarkdownView(t.item2),
child: MarkdownFlutterView(t.item2),
),
CommonStyle.verticalGap,
],

View File

@ -7,7 +7,7 @@ import 'package:git_touch/scaffolds/refresh_stateful.dart';
import 'package:git_touch/utils/utils.dart';
import 'package:git_touch/widgets/app_bar_title.dart';
import 'package:git_touch/widgets/entry_item.dart';
import 'package:git_touch/widgets/html_view.dart';
import 'package:git_touch/widgets/markdown_view.dart';
import 'package:git_touch/widgets/repo_header.dart';
import 'package:git_touch/widgets/table_view.dart';
import 'package:provider/provider.dart';
@ -101,7 +101,7 @@ class GeRepoScreen extends StatelessWidget {
],
),
CommonStyle.verticalGap,
if (t.item2 != null) MarkdownHtmlView(t.item2)
if (t.item2 != null) MarkdownWebView(t.item2)
],
);
},

View File

@ -7,7 +7,7 @@ import 'package:git_touch/scaffolds/refresh_stateful.dart';
import 'package:git_touch/utils/utils.dart';
import 'package:git_touch/widgets/app_bar_title.dart';
import 'package:git_touch/widgets/entry_item.dart';
import 'package:git_touch/widgets/html_view.dart';
import 'package:git_touch/widgets/markdown_view.dart';
import 'package:git_touch/widgets/label.dart';
import 'package:git_touch/widgets/language_bar.dart';
import 'package:git_touch/widgets/mutation_button.dart';
@ -338,7 +338,7 @@ class GhRepoScreen extends StatelessWidget {
if (snapshot.data == null) {
return Container();
} else {
return MarkdownHtmlView(snapshot.data);
return MarkdownWebView(snapshot.data);
}
},
)

View File

@ -7,7 +7,7 @@ import 'package:git_touch/scaffolds/refresh_stateful.dart';
import 'package:git_touch/utils/utils.dart';
import 'package:git_touch/widgets/app_bar_title.dart';
import 'package:git_touch/widgets/entry_item.dart';
import 'package:git_touch/widgets/html_view.dart';
import 'package:git_touch/widgets/markdown_view.dart';
import 'package:git_touch/widgets/language_bar.dart';
import 'package:git_touch/widgets/repo_header.dart';
import 'package:git_touch/widgets/table_view.dart';
@ -191,7 +191,7 @@ class GlProjectScreen extends StatelessWidget {
if (snapshot.data == null) {
return Container();
} else {
return MarkdownHtmlView(snapshot.data);
return MarkdownWebView(snapshot.data);
}
},
),

View File

@ -96,12 +96,7 @@ class GtRepoScreen extends StatelessWidget {
],
),
CommonStyle.verticalGap,
if (t.item2 != null)
Container(
padding: CommonStyle.padding,
color: theme.palette.background,
child: MarkdownView(t.item2),
),
if (t.item2 != null) MarkdownFlutterView(t.item2),
CommonStyle.verticalGap,
],
);

View File

@ -51,10 +51,7 @@ class BlobView extends StatelessWidget {
: Image.memory(base64.decode(base64Text));
case 'md':
case 'markdown':
return Padding(
padding: CommonStyle.padding,
child: MarkdownView(_text), // TODO: basePath
);
return MarkdownFlutterView(_text);
default:
return SingleChildScrollView(
scrollDirection: Axis.horizontal,

View File

@ -182,7 +182,7 @@ class CommentItem extends StatelessWidget {
),
]),
SizedBox(height: 12),
MarkdownView(body), // TODO: link
MarkdownFlutterView(body), // TODO: link
SizedBox(height: 12),
if (widgets != null) ...widgets
],

View File

@ -1,21 +1,8 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:git_touch/models/theme.dart';
import 'package:git_touch/utils/utils.dart';
import 'package:provider/provider.dart';
import 'package:webview_flutter/webview_flutter.dart';
class MarkdownHtmlView extends StatelessWidget {
final String html;
MarkdownHtmlView(this.html);
@override
Widget build(BuildContext context) {
final theme = Provider.of<ThemeModel>(context);
return HtmlView(html, cssText: theme.markdownCss);
}
}
class HtmlView extends StatefulWidget {
final String html;
HtmlView(String text, {String cssText, List<String> cssLinks = const []})

View File

@ -3,15 +3,32 @@ import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:git_touch/models/code.dart';
import 'package:git_touch/models/theme.dart';
import 'package:git_touch/utils/utils.dart';
import 'package:git_touch/widgets/html_view.dart';
import 'package:provider/provider.dart';
import 'package:uri/uri.dart';
import 'package:path/path.dart' as path;
class MarkdownView extends StatelessWidget {
class MarkdownWebView extends StatelessWidget {
final String html;
MarkdownWebView(this.html);
@override
Widget build(BuildContext context) {
final theme = Provider.of<ThemeModel>(context);
return HtmlView(html, cssText: theme.markdownCss);
}
}
class MarkdownFlutterView extends StatelessWidget {
final String text;
final List<String> basePaths;
final EdgeInsetsGeometry padding;
MarkdownView(this.text, {this.basePaths});
MarkdownFlutterView(
this.text, {
this.basePaths,
this.padding = const EdgeInsets.all(12),
});
static Map<String, String> matchPattern(String url, String pattern) {
var uri = Uri.parse(url);
@ -27,105 +44,109 @@ class MarkdownView extends StatelessWidget {
final _hStyle =
_basicStyle.copyWith(fontWeight: FontWeight.w600, height: 1.25);
return MarkdownBody(
data: text,
selectable: true,
imageBuilder: (uri, title, alt) {
if (uri.scheme == 'http' || uri.scheme == 'https') {
// TODO: svg support
// if (uri.path.endsWith('.svg')) {
// return SvgPicture.network(uri.toString());
// }
return Image.network(uri.toString());
} else {
return Container(); // TODO: relative path image
}
},
onTapLink: (url) {
final theme = context.read<ThemeModel>();
return Container(
padding: padding,
child: MarkdownBody(
data: text,
selectable: true,
imageBuilder: (uri, title, alt) {
if (uri.scheme == 'http' || uri.scheme == 'https') {
// TODO: svg support
// if (uri.path.endsWith('.svg')) {
// return SvgPicture.network(uri.toString());
// }
return Image.network(uri.toString());
} else {
return Container(); // TODO: relative path image
}
},
onTapLink: (url) {
final theme = context.read<ThemeModel>();
if (basePaths != null &&
!url.startsWith('https://') &&
!url.startsWith('http://')) {
// Treat as relative path
if (basePaths != null &&
!url.startsWith('https://') &&
!url.startsWith('http://')) {
// Treat as relative path
final x = basePaths.sublist(3).join('/');
var y = path.join(x, url);
if (y.startsWith('/')) y = y.substring(1);
final x = basePaths.sublist(3).join('/');
var y = path.join(x, url);
if (y.startsWith('/')) y = y.substring(1);
return theme.push(context,
'/${basePaths[0]}/${basePaths[1]}/${basePaths[2]}?path=${y.urlencode}');
}
return theme.push(context,
'/${basePaths[0]}/${basePaths[1]}/${basePaths[2]}?path=${y.urlencode}');
}
// TODO: Relative paths
if (url.startsWith('https://github.com')) {
const matchedPaths = [
'/{owner}/{name}/pull/{number}',
'/{owner}/{name}/issues/{number}',
'/{owner}/{name}',
'/{login}'
];
for (var p in matchedPaths) {
final m = matchPattern(url, p);
if (m != null) {
return theme.push(context,
url.replaceFirst(RegExp(r'https://github.com'), '/github'));
// TODO: Relative paths
if (url.startsWith('https://github.com')) {
const matchedPaths = [
'/{owner}/{name}/pull/{number}',
'/{owner}/{name}/issues/{number}',
'/{owner}/{name}',
'/{login}'
];
for (var p in matchedPaths) {
final m = matchPattern(url, p);
if (m != null) {
return theme.push(context,
url.replaceFirst(RegExp(r'https://github.com'), '/github'));
}
}
}
}
launchUrl(url);
},
styleSheet: MarkdownStyleSheet(
a: _basicStyle.copyWith(color: theme.palette.primary),
p: _basicStyle,
code: _basicStyle.copyWith(
backgroundColor: theme.palette.grayBackground,
fontSize: 16 * 0.85,
height: 1.45,
fontFamily: code.fontFamilyUsed,
),
h1: _hStyle.copyWith(fontSize: 32),
h2: _hStyle.copyWith(fontSize: 24),
h3: _hStyle.copyWith(fontSize: 20),
h4: _hStyle,
h5: _hStyle.copyWith(fontSize: 14),
h6: _hStyle.copyWith(
fontSize: 16 * 0.85, color: theme.palette.tertiaryText),
em: _basicStyle.copyWith(fontStyle: FontStyle.italic),
strong: _basicStyle.copyWith(fontWeight: FontWeight.w600),
del: const TextStyle(decoration: TextDecoration.lineThrough),
blockquote: _basicStyle.copyWith(color: theme.palette.tertiaryText),
img: _basicStyle,
checkbox: _basicStyle,
blockSpacing: 16,
listIndent: 32,
listBullet: _basicStyle,
// tableHead: _basicStyle,
tableBody: _basicStyle,
// tableHeadAlign: TextAlign.center,
// tableBorder: TableBorder.all(color: Colors.grey.shade300, width: 0),
// tableColumnWidth: const FlexColumnWidth(),
// tableCellsPadding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
blockquotePadding: EdgeInsets.symmetric(horizontal: 16),
blockquoteDecoration: BoxDecoration(
border: Border(left: BorderSide(color: Color(0xffdfe2e5), width: 4)),
),
codeblockPadding: EdgeInsets.all(16),
codeblockDecoration: BoxDecoration(
color: theme.palette.grayBackground,
borderRadius: BorderRadius.circular(3),
),
horizontalRuleDecoration: BoxDecoration(
border: Border(
top: BorderSide(
width: 4,
color: theme.palette.grayBackground,
launchUrl(url);
},
styleSheet: MarkdownStyleSheet(
a: _basicStyle.copyWith(color: theme.palette.primary),
p: _basicStyle,
code: _basicStyle.copyWith(
backgroundColor: theme.palette.grayBackground,
fontSize: 16 * 0.85,
height: 1.45,
fontFamily: code.fontFamilyUsed,
),
h1: _hStyle.copyWith(fontSize: 32),
h2: _hStyle.copyWith(fontSize: 24),
h3: _hStyle.copyWith(fontSize: 20),
h4: _hStyle,
h5: _hStyle.copyWith(fontSize: 14),
h6: _hStyle.copyWith(
fontSize: 16 * 0.85, color: theme.palette.tertiaryText),
em: _basicStyle.copyWith(fontStyle: FontStyle.italic),
strong: _basicStyle.copyWith(fontWeight: FontWeight.w600),
del: const TextStyle(decoration: TextDecoration.lineThrough),
blockquote: _basicStyle.copyWith(color: theme.palette.tertiaryText),
img: _basicStyle,
checkbox: _basicStyle,
blockSpacing: 16,
listIndent: 32,
listBullet: _basicStyle,
// tableHead: _basicStyle,
tableBody: _basicStyle,
// tableHeadAlign: TextAlign.center,
// tableBorder: TableBorder.all(color: Colors.grey.shade300, width: 0),
// tableColumnWidth: const FlexColumnWidth(),
// tableCellsPadding: const EdgeInsets.fromLTRB(16, 8, 16, 8),
blockquotePadding: EdgeInsets.symmetric(horizontal: 16),
blockquoteDecoration: BoxDecoration(
border:
Border(left: BorderSide(color: Color(0xffdfe2e5), width: 4)),
),
codeblockPadding: EdgeInsets.all(16),
codeblockDecoration: BoxDecoration(
color: theme.palette.grayBackground,
borderRadius: BorderRadius.circular(3),
),
horizontalRuleDecoration: BoxDecoration(
border: Border(
top: BorderSide(
width: 4,
color: theme.palette.grayBackground,
),
),
),
),
// syntaxHighlighter: , // TODO:
),
// syntaxHighlighter: , // TODO:
);
}
}