From c8100a180050f09f1b7f81e7fbe480a73383c714 Mon Sep 17 00:00:00 2001 From: james Date: Sun, 16 Feb 2020 21:37:12 +0000 Subject: [PATCH 1/3] extract PDF bookmark with priority --- PDF/PDFElement.cs | 11 ++++++++-- PDF/PDFWindow.xaml.cs | 32 ++++++++++++++++++++++++++++++ PDF/Viewer/IPDFViewer.SuperMemo.cs | 8 +++++--- PDF/Viewer/IPDFViewer.cs | 5 +++-- 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/PDF/PDFElement.cs b/PDF/PDFElement.cs index 3d59615..90d671f 100644 --- a/PDF/PDFElement.cs +++ b/PDF/PDFElement.cs @@ -291,7 +291,8 @@ public static CreationResult Create( int pageMargin = PDFConst.DefaultPageMargin, float zoom = PDFConst.DefaultZoom, bool shouldDisplay = true, - string subtitle = null) + string subtitle = null, + double priority = -1) { PDFElement pdfEl; string title; @@ -299,6 +300,12 @@ public static CreationResult Create( string creationDate; string filePath; + + if (priority < 0 || priority > 100) + { + priority = PDFState.Instance.Config.PDFExtractPriority; + } + try { filePath = binMem.GetFilePath("pdf"); @@ -348,7 +355,7 @@ public static CreationResult Create( elementHtml) .WithParent(parentElement) .WithTitle(subtitle ?? title) - .WithPriority(PDFState.Instance.Config.PDFExtractPriority) + .WithPriority(priority) .WithReference( r => r.WithTitle(title) .WithAuthor(author) diff --git a/PDF/PDFWindow.xaml.cs b/PDF/PDFWindow.xaml.cs index acb8758..682b335 100644 --- a/PDF/PDFWindow.xaml.cs +++ b/PDF/PDFWindow.xaml.cs @@ -41,6 +41,8 @@ using Patagames.Pdf.Net; using SuperMemoAssistant.Extensions; using SuperMemoAssistant.Plugins.PDF.Models; +using System.Threading.Tasks; +using Forge.Forms; using SuperMemoAssistant.Services.IO.HotKeys; using SuperMemoAssistant.Sys.IO.Devices; using SuperMemoAssistant.Sys.Threading; @@ -342,6 +344,28 @@ private void TvBookmark_MenuItem_PDFExtract(object sender, IPDFViewer.ExtractBookmark(bookmark); } + private async void TvBookmark_MenuItem_PDFExtractWithPriority(object sender, + RoutedEventArgs e) + { + + var result = await Forge.Forms.Show.Window() + .For(new Prompt { Title = "Bookmark Priority?", Value = -1 }); + + if (!result.Model.Confirmed) + { + return; + } + + PdfBookmark bookmark = (PdfBookmark)tvBookmarks.SelectedItem; + if (bookmark == null) + { + return; + } + + IPDFViewer.ExtractBookmark(bookmark, result.Model.Value); + } + + private void TvBookmarks_PreviewKeyDown(object sender, KeyEventArgs e) { @@ -364,6 +388,14 @@ private void TvBookmarks_PreviewKeyDown(object sender, e.Handled = true; } + + else if (kbMod == (KeyModifiers.Ctrl | KeyModifiers.Shift) + && e.Key == Key.X) + { + TvBookmark_MenuItem_PDFExtractWithPriority(sender, + null); + e.Handled = true; + } } private void BtnExpandAll_Click(object sender, diff --git a/PDF/Viewer/IPDFViewer.SuperMemo.cs b/PDF/Viewer/IPDFViewer.SuperMemo.cs index a584a11..1c7a2fc 100644 --- a/PDF/Viewer/IPDFViewer.SuperMemo.cs +++ b/PDF/Viewer/IPDFViewer.SuperMemo.cs @@ -225,7 +225,7 @@ protected ContentBase CreateImageContent(Image image, string title) // PDF Extracts - protected bool CreatePDFExtract() + protected bool CreatePDFExtract(double priority = -1) { bool ret = false; @@ -263,7 +263,8 @@ protected bool CreatePDFExtract() } protected bool CreatePDFExtract(SelectInfo selInfo, - string title = null) + string title = null, + double priority = -1) { Save(false); @@ -282,7 +283,8 @@ protected bool CreatePDFExtract(SelectInfo selInfo, PDFElement.PageMargin, PDFElement.Zoom, false, - title) == PDFElement.CreationResult.Ok; + title, + priority) == PDFElement.CreationResult.Ok; if (ret) { diff --git a/PDF/Viewer/IPDFViewer.cs b/PDF/Viewer/IPDFViewer.cs index 9dd1766..459adaa 100644 --- a/PDF/Viewer/IPDFViewer.cs +++ b/PDF/Viewer/IPDFViewer.cs @@ -253,7 +253,7 @@ public void LoadDocument(PDFElement pdfElement) OnDocumentLoaded(null); } - public void ExtractBookmark(PdfBookmark bookmark) + public void ExtractBookmark(PdfBookmark bookmark, double priority = -1) { var selInfo = bookmark.GetSelection(Document); @@ -261,7 +261,8 @@ public void ExtractBookmark(PdfBookmark bookmark) return; CreatePDFExtract(selInfo.Value, - bookmark.Title); + bookmark.Title, + priority); } public void ProcessBookmark(PdfBookmark bookmark) From 5db553cdd646154466b24a7229af5e47fb0e17ca Mon Sep 17 00:00:00 2001 From: james Date: Sun, 16 Feb 2020 22:03:37 +0000 Subject: [PATCH 2/3] Added a menu item --- PDF/PDFWindow.xaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/PDF/PDFWindow.xaml b/PDF/PDFWindow.xaml index b19f187..74fedc5 100644 --- a/PDF/PDFWindow.xaml +++ b/PDF/PDFWindow.xaml @@ -153,6 +153,8 @@ + From ff8591f61153c6974a52611581edbf65ba36e137 Mon Sep 17 00:00:00 2001 From: james Date: Sun, 15 Mar 2020 16:24:33 +0000 Subject: [PATCH 3/3] Add default priority, priority checks and alert, cleaned up code --- PDF/PDFElement.cs | 1 + PDF/PDFWindow.xaml.cs | 13 +++++++++++-- PDF/Viewer/IPDFViewer.SuperMemo.cs | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/PDF/PDFElement.cs b/PDF/PDFElement.cs index 90d671f..24d4d36 100644 --- a/PDF/PDFElement.cs +++ b/PDF/PDFElement.cs @@ -303,6 +303,7 @@ public static CreationResult Create( if (priority < 0 || priority > 100) { + LogTo.Error("Attempted to call PDFElement.Create with invalid priority value. Resorting to user's config PDF Extract Priority."); priority = PDFState.Instance.Config.PDFExtractPriority; } diff --git a/PDF/PDFWindow.xaml.cs b/PDF/PDFWindow.xaml.cs index 682b335..45bffb8 100644 --- a/PDF/PDFWindow.xaml.cs +++ b/PDF/PDFWindow.xaml.cs @@ -349,20 +349,29 @@ private async void TvBookmark_MenuItem_PDFExtractWithPriority(object sender, { var result = await Forge.Forms.Show.Window() - .For(new Prompt { Title = "Bookmark Priority?", Value = -1 }); + .For(new Prompt { Title = "Bookmark Priority?", Value = Config.PDFExtractPriority }); if (!result.Model.Confirmed) { return; } + double priority = result.Model.Value; + + if (priority < 0 || priority > 100) + { + await Forge.Forms.Show.Window() + .For(new Alert("Priority value must be between 0 and 100.")); + return; + } + PdfBookmark bookmark = (PdfBookmark)tvBookmarks.SelectedItem; if (bookmark == null) { return; } - IPDFViewer.ExtractBookmark(bookmark, result.Model.Value); + IPDFViewer.ExtractBookmark(bookmark, priority); } diff --git a/PDF/Viewer/IPDFViewer.SuperMemo.cs b/PDF/Viewer/IPDFViewer.SuperMemo.cs index 1c7a2fc..b73b3bf 100644 --- a/PDF/Viewer/IPDFViewer.SuperMemo.cs +++ b/PDF/Viewer/IPDFViewer.SuperMemo.cs @@ -225,7 +225,7 @@ protected ContentBase CreateImageContent(Image image, string title) // PDF Extracts - protected bool CreatePDFExtract(double priority = -1) + protected bool CreatePDFExtract() { bool ret = false;