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)
{
string imageFilePath = GetFullPath(ChapterRef.ContentFileName, args.Src);
if (EpubBook.Content.Images.TryGetValue(imageFilePath, out EpubByteContentFileRef imageContent))
string imageFilePath = ChapterRef != null ? GetFullPath(ChapterRef.ContentFileName, args.Src) : null;
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
{
@@ -58,7 +68,7 @@ namespace QuickLook.Plugin.EpubViewer
{
Debug.WriteLine($"Failed to load image: {args.Src}");
}
}
}
}
}

View File

@@ -24,7 +24,7 @@ namespace QuickLook.Plugin.EpubViewer
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()
{
@@ -42,7 +42,7 @@ namespace QuickLook.Plugin.EpubViewer
this.chapterRefs = Flatten(epubBook.GetChapters());
this.currChapter = -1;
this.pagePanel.EpubBook = epubBook;
this.NextChapter();
this.UpdateChapter();
}
private List<EpubChapterRef> Flatten(List<EpubChapterRef> list)
@@ -60,16 +60,12 @@ namespace QuickLook.Plugin.EpubViewer
try
{
this.currChapter = Math.Min(this.currChapter + 1, chapterRefs.Count - 1);
this.pagePanel.ChapterRef = chapterRefs[currChapter];
if (chapterRefs[currChapter].Anchor != null)
{
this.pagePanel.ScrollToElement(chapterRefs[currChapter].Anchor);
}
this.UpdateChapter();
}
catch (Exception ex)
{
Debug.WriteLine(ex);
this.pagePanel.Text = "<div>Invalid chapter.</div>";
this.pagePanel.Text = "<div>Invalid chapter.</div>";
}
OnPropertyChanged("Chapter");
OnChapterChanged();
@@ -84,12 +80,8 @@ namespace QuickLook.Plugin.EpubViewer
{
try
{
this.currChapter = Math.Max(this.currChapter - 1, 0);
this.pagePanel.ChapterRef = chapterRefs[currChapter];
if (chapterRefs[currChapter].Anchor != null)
{
this.pagePanel.ScrollToElement(chapterRefs[currChapter].Anchor);
}
this.currChapter = Math.Max(this.currChapter - 1, -1);
this.UpdateChapter();
}
catch (Exception ex)
{
@@ -138,5 +130,26 @@ namespace QuickLook.Plugin.EpubViewer
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);
}
}
}
}
}