Added cover page

This commit is contained in:
Marco Gavelli
2018-07-16 16:29:46 +02:00
parent 3e251c35ec
commit d407c971ee
2 changed files with 41 additions and 18 deletions

View File

@@ -38,10 +38,20 @@ namespace QuickLook.Plugin.EpubViewer
protected override async void OnImageLoad(HtmlImageLoadEventArgs args) protected override async void OnImageLoad(HtmlImageLoadEventArgs args)
{ {
string imageFilePath = GetFullPath(ChapterRef.ContentFileName, args.Src); string imageFilePath = ChapterRef != null ? GetFullPath(ChapterRef.ContentFileName, args.Src) : null;
if (EpubBook.Content.Images.TryGetValue(imageFilePath, out EpubByteContentFileRef imageContent)) byte[] imageBytes = null;
if (args.Src == "COVER")
{ {
using (MemoryStream imageStream = new MemoryStream(await imageContent.ReadContentAsBytesAsync())) imageBytes = await EpubBook.ReadCoverAsync();
}
else if (EpubBook.Content.Images.TryGetValue(imageFilePath, out EpubByteContentFileRef imageContent))
{
imageBytes = await imageContent.ReadContentAsBytesAsync();
}
if (imageBytes != null)
{
using (MemoryStream imageStream = new MemoryStream(imageBytes))
{ {
try try
{ {
@@ -58,7 +68,7 @@ namespace QuickLook.Plugin.EpubViewer
{ {
Debug.WriteLine($"Failed to load image: {args.Src}"); Debug.WriteLine($"Failed to load image: {args.Src}");
} }
} }
} }
} }

View File

@@ -24,7 +24,7 @@ namespace QuickLook.Plugin.EpubViewer
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
public string Chapter => chapterRefs != null ? $"{chapterRefs?[currChapter].Title} ({currChapter + 1}/{chapterRefs?.Count})" : ""; public string Chapter => chapterRefs != null && currChapter >= 0 ? $"{chapterRefs?[currChapter].Title} ({currChapter + 1}/{chapterRefs?.Count})" : "";
public EpubViewerControl() public EpubViewerControl()
{ {
@@ -42,7 +42,7 @@ namespace QuickLook.Plugin.EpubViewer
this.chapterRefs = Flatten(epubBook.GetChapters()); this.chapterRefs = Flatten(epubBook.GetChapters());
this.currChapter = -1; this.currChapter = -1;
this.pagePanel.EpubBook = epubBook; this.pagePanel.EpubBook = epubBook;
this.NextChapter(); this.UpdateChapter();
} }
private List<EpubChapterRef> Flatten(List<EpubChapterRef> list) private List<EpubChapterRef> Flatten(List<EpubChapterRef> list)
@@ -60,16 +60,12 @@ namespace QuickLook.Plugin.EpubViewer
try try
{ {
this.currChapter = Math.Min(this.currChapter + 1, chapterRefs.Count - 1); this.currChapter = Math.Min(this.currChapter + 1, chapterRefs.Count - 1);
this.pagePanel.ChapterRef = chapterRefs[currChapter]; this.UpdateChapter();
if (chapterRefs[currChapter].Anchor != null)
{
this.pagePanel.ScrollToElement(chapterRefs[currChapter].Anchor);
}
} }
catch (Exception ex) catch (Exception ex)
{ {
Debug.WriteLine(ex); Debug.WriteLine(ex);
this.pagePanel.Text = "<div>Invalid chapter.</div>"; this.pagePanel.Text = "<div>Invalid chapter.</div>";
} }
OnPropertyChanged("Chapter"); OnPropertyChanged("Chapter");
OnChapterChanged(); OnChapterChanged();
@@ -84,12 +80,8 @@ namespace QuickLook.Plugin.EpubViewer
{ {
try try
{ {
this.currChapter = Math.Max(this.currChapter - 1, 0); this.currChapter = Math.Max(this.currChapter - 1, -1);
this.pagePanel.ChapterRef = chapterRefs[currChapter]; this.UpdateChapter();
if (chapterRefs[currChapter].Anchor != null)
{
this.pagePanel.ScrollToElement(chapterRefs[currChapter].Anchor);
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -138,5 +130,26 @@ namespace QuickLook.Plugin.EpubViewer
public int NewChapter { get; set; } public int NewChapter { get; set; }
} }
private void UpdateChapter()
{
if (currChapter < 0)
{
this.pagePanel.ChapterRef = null;
this.pagePanel.Text = string.Format(@"
<div style=""margin:4pt; text-align: center;"">
<img src=""COVER"" alt=""COVER"" style=""height:500px;""/>
<div style=""word-wrap: break-word;"">{0}</div>
</div>", epubBook.Title);
}
else
{
this.pagePanel.ChapterRef = chapterRefs[currChapter];
if (chapterRefs[currChapter].Anchor != null)
{
this.pagePanel.ScrollToElement(chapterRefs[currChapter].Anchor);
}
}
}
} }
} }