Merge pull request #495 from MisterY/img-viewer

image viewer for .org files
This commit is contained in:
Alen Siljak
2021-04-25 12:55:35 +02:00
committed by GitHub
2 changed files with 35 additions and 5 deletions

View File

@ -15,16 +15,25 @@ limitations under the License.
*/
import 'dart:developer';
//import 'dart:html';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:gitjournal/widgets/images/image_details.dart';
import 'package:gitjournal/widgets/images/themable_image.dart';
import 'package:org_flutter/org_flutter.dart';
import 'package:path/path.dart';
import 'package:url_launcher/url_launcher.dart';
/// Handles links from .org documents.
class OrgLinkHandler {
BuildContext context;
String notePath;
OrgLinkHandler(this.notePath) : super();
OrgLinkHandler(this.context, this.notePath) : super();
void launchUrl(String link) async {
// handle =file:= prefix
@ -32,8 +41,8 @@ class OrgLinkHandler {
link = link.replaceFirst('file:', '');
}
// Images
if (looksLikeImagePath(link)) {
// Images
if (looksLikeUrl(link)) {
// Remote images
if (await canLaunch(link)) {
@ -43,7 +52,7 @@ class OrgLinkHandler {
log('could not launch $link');
}
} else {
// (presumably-)Local images
// Local images
File file = File(link);
if (file.isAbsolute) {
@ -54,8 +63,16 @@ class OrgLinkHandler {
// 1. name-only
// 2. relative path
log('image ' + file.path);
//log('image ' + file.path);
Context ctx = Context();
String noteDir = ctx.dirname(notePath);
String fullPath = ctx.join(noteDir, file.path);
file = File(fullPath);
// caption is the link caption
}
_showImage(file);
}
} else {
// Other links.
@ -83,4 +100,12 @@ class OrgLinkHandler {
}
}
}
void _showImage(File file) {
ThemableImage im = ThemableImage.image(file);
Navigator.push(
context, MaterialPageRoute(builder: (context) => ImageDetails(im, "")));
// captionText(context, altText, tooltip)
}
}

View File

@ -1,5 +1,7 @@
// @dart=2.9
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
@ -135,11 +137,14 @@ class _EditorScaffoldState extends State<EditorScaffold> {
} else {
switch (note.fileFormat) {
case NoteFileFormat.OrgMode:
OrgLinkHandler handler = OrgLinkHandler(note.filePath);
OrgLinkHandler handler = OrgLinkHandler(context, note.filePath);
body = Org(
note.body,
onLinkTap: handler.launchUrl,
onLocalSectionLinkTap: (OrgSection section) {
log("local section link: " + section.toString());
},
);
break;
default: