forked from companyzero/bisonrelay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
md_elements.dart
379 lines (333 loc) · 10.3 KB
/
md_elements.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import 'dart:convert';
import 'dart:typed_data';
// import 'package:dart_vlc/dart_vlc.dart' as vlc;
import 'package:bruig/components/snackbars.dart';
import 'package:bruig/models/downloads.dart';
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:golib_plugin/definitions.dart';
import 'package:markdown/markdown.dart' as md;
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';
class VideoInlineSyntax extends md.InlineSyntax {
/// This is a primitive example pattern
VideoInlineSyntax({
String pattern = r'--video\[(.*?)\]--',
}) : super(pattern);
@override
bool onMatch(md.InlineParser parser, Match match) {
final videoURL = match.group(1);
md.Element el = md.Element.text("video", videoURL!.toString());
parser.addNode(el);
return true;
}
}
class ImageInlineSyntax extends md.InlineSyntax {
/// This is a primitive example pattern
ImageInlineSyntax({
String pattern = r'--image\[(.*?)\]--',
}) : super(pattern);
@override
bool onMatch(md.InlineParser parser, Match match) {
final imageURL = match.group(1);
md.Element el = md.Element.text("image", imageURL!.toString());
parser.addNode(el);
return true;
}
}
class EmbedInlineSyntax extends md.InlineSyntax {
/// This is a primitive example pattern
EmbedInlineSyntax({
String pattern = r'--embed\[(.*?)\]--',
}) : super(pattern);
@override
bool onMatch(md.InlineParser parser, Match match) {
final Map<String, String> parms = {};
final rawParms = match.group(1) ?? "";
rawParms.split(",").forEach((element) {
var p = element.indexOf("=");
if (p == -1) return;
parms[element.substring(0, p)] = element.substring(p + 1);
});
// Only accept valid download FIDs.
var download = parms["download"] ?? "";
if (!RegExp(r"^[0-9a-fA-F]{64}$").hasMatch(download)) {
download = "";
}
// URL-decode alt text.
var alt = parms["alt"] ?? "";
if (alt != "") {
try {
alt = Uri.decodeComponent(alt);
} catch (exception) {
// Ignore decoding errors and just print a debug msg.
debugPrint("Unable to decode alt: $exception");
}
}
var data = parms["data"] ?? "";
// Bare link without embedded data.
if (data == "" && download != "") {
var el = md.Element.text(
"download", alt != "" ? alt : "Download file $download");
el.attributes["fid"] = download;
parser.addNode(el);
return true;
}
// Otherwise, we need data.
if (data == "") {
return true;
}
var tag = "";
switch (parms["type"]) {
case "image/jpeg":
tag = "image";
break;
case "image/png":
case "image/gif":
case "image/bmp":
tag = "image";
break;
case "text/plain":
// Decode plain text directly.
tag = "p";
try {
data = const Base64Decoder().convert(data).toString();
} catch (exception) {
data = "Unable to decode plain text contents: $exception";
}
break;
default:
return true;
}
md.Element el = md.Element.text(tag, data);
if (download != "") {
el.attributes["fid"] = download;
}
if (alt != "") {
el.attributes["alt"] = alt;
}
parser.addNode(el);
return true;
}
}
/*
class _VideoMarkdownDesktopElement extends StatefulWidget {
final String filename;
_VideoMarkdownDesktopElement(this.filename, {Key? key}) : super(key: key);
@override
__VideoMarkdownDesktopElementState createState() =>
__VideoMarkdownDesktopElementState();
}
class __VideoMarkdownDesktopElementState
extends State<_VideoMarkdownDesktopElement> {
vlc.Player player = vlc.Player(id: 69420);
vlc.Media? media;
@override
void initState() {
super.initState();
media = vlc.Media.file(File(widget.filename));
if (media != null) {
player.open(media!);
}
}
@override
void dispose() {
player.stop();
super.dispose();
}
@override
Widget build(BuildContext context) {
return vlc.Video(
player: player,
width: 320,
height: 200,
);
}
}
class _VideoMarkdownMobileElement extends StatefulWidget {
final String filename;
_VideoMarkdownMobileElement(this.filename, {Key? key}) : super(key: key);
@override
__VideoMarkdownMobileElementState createState() =>
__VideoMarkdownMobileElementState();
}
class __VideoMarkdownMobileElementState
extends State<_VideoMarkdownMobileElement> {
mbv.VideoPlayerController? controller;
void initController() async {
var f = File(widget.filename);
var newController = await mbv.VideoPlayerController.file(f);
await newController.initialize();
mounted
? setState(() {
controller = newController;
controller?.play();
})
: null;
}
@override
void initState() {
super.initState();
initController();
}
@override
void dispose() {
controller?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
var theme = Theme.of(context);
if (controller == null) {
return Container(
color: theme.cardColor,
child: Center(
child: Text("Loading..."),
),
);
}
return AspectRatio(
aspectRatio: controller!.value.aspectRatio,
child: mbv.VideoPlayer(controller!));
}
}
class VideoMarkdownElementBuilder extends MarkdownElementBuilder {
final String basedir;
VideoMarkdownElementBuilder(this.basedir);
@override
Widget visitElementAfter(md.Element element, TextStyle? preferredStyle) {
final bool useVLC =
Platform.isWindows || Platform.isLinux || Platform.isMacOS;
// Protect against trying to fetch from !basedir.
String filename = p.canonicalize(p.join(this.basedir, element.textContent));
if (!p.isWithin(basedir, filename)) {
return Container(color: Colors.amber, width: 100, height: 100);
}
return Container(
margin: EdgeInsets.symmetric(horizontal: 0, vertical: 2),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(6)),
),
child: Padding(
padding: const EdgeInsets.all(4.0),
child: useVLC
? _VideoMarkdownDesktopElement(filename)
: _VideoMarkdownMobileElement(filename)),
);
}
}
*/
class MarkdownArea extends StatelessWidget {
final String text;
const MarkdownArea(this.text, {Key? key}) : super(key: key);
Future<void> launchUrlAwait(url) async {
if (!await launchUrl(Uri.parse(url))) {
throw 'Could not launch $url';
}
}
@override
Widget build(BuildContext context) {
var theme = Theme.of(context);
var darkTextColor = theme.indicatorColor;
var textColor = theme.focusColor;
final screenWidth = MediaQuery.of(context).size.width;
return MarkdownBody(
styleSheet: MarkdownStyleSheet(
p: TextStyle(
color: textColor,
fontWeight: FontWeight.w300,
letterSpacing: 0.44),
h1: TextStyle(color: textColor),
h2: TextStyle(color: textColor),
h3: TextStyle(color: textColor),
h4: TextStyle(color: textColor),
h5: TextStyle(color: textColor),
h6: TextStyle(color: textColor),
em: TextStyle(color: textColor),
strong: TextStyle(color: textColor),
del: TextStyle(color: textColor),
listBullet: TextStyle(color: textColor),
blockquote: TextStyle(color: textColor),
checkbox: TextStyle(color: textColor),
tableBody: TextStyle(color: textColor),
tableHead: TextStyle(color: textColor),
blockquoteDecoration: BoxDecoration(color: darkTextColor),
codeblockDecoration: BoxDecoration(color: darkTextColor),
code: TextStyle(color: textColor),
),
selectable: true,
data: text.trim(),
builders: {
//"video": VideoMarkdownElementBuilder(basedir),
"image": ImageMarkdownElementBuilder(),
"download": DownloadLinkElementBuilder(),
},
onTapLink: (text, url, blah) {
launchUrlAwait(url);
},
inlineSyntaxes: [
//VideoInlineSyntax(),
//ImageInlineSyntax()
EmbedInlineSyntax(),
]);
}
}
class Downloadable extends StatelessWidget {
final String tip;
final String fid;
final Widget child;
const Downloadable(this.tip, this.fid, this.child, {Key? key})
: super(key: key);
void download(BuildContext context) async {
var downloads = Provider.of<DownloadsModel>(context, listen: false);
var post = Provider.of<PostSummary>(context, listen: false);
try {
await downloads.getUnknownUserFile(post.authorID, fid);
showSuccessSnackbar(context, "Added $fid to download queue");
} catch (exception) {
showErrorSnackbar(context, "Unable to start download: $exception");
}
}
@override
Widget build(BuildContext context) => Tooltip(
message: tip,
child: InkWell(
hoverColor: Theme.of(context).hoverColor,
onTap: fid != "" ? () => download(context) : null,
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 2, vertical: 2),
child: child,
)));
}
class DownloadLinkElementBuilder extends MarkdownElementBuilder {
@override
Widget visitElementAfter(md.Element element, TextStyle? preferredStyle) {
var download = element.attributes["fid"] ?? "";
var tip = "Click to download file $download";
return Downloadable(tip, download, Text(element.textContent));
}
}
class ImageMarkdownElementBuilder extends MarkdownElementBuilder {
@override
Widget visitElementAfter(md.Element element, TextStyle? preferredStyle) {
Uint8List imgBytes;
try {
imgBytes = const Base64Decoder().convert(element.textContent);
} catch (exception) {
return Text("Unable to decode image: $exception");
}
var alt = element.attributes["alt"] ?? "";
var download = element.attributes["fid"] ?? "";
var tip = "";
if (alt != "") {
tip = alt;
if (download != "") {
tip += "\n\n";
}
}
if (download != "") {
tip += "Click to download file $download";
}
return Downloadable(tip, download, Image.memory(imgBytes));
}
}