123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- using ComPDFKit.PDFDocument;
- using ComPDFKitViewer.PdfViewer;
- using ImTools;
- using PDF_Master.CustomControl;
- using PDF_Master.Helper;
- using PDF_Master.Model;
- using PDF_Master.Model.PageEdit;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Regions;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Reflection;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Forms;
- using System.Windows.Input;
- using System.Windows.Markup;
- using System.Windows.Media;
- using static Dropbox.Api.Files.SearchMatchType;
- using static Dropbox.Api.TeamLog.AdminAlertSeverityEnum;
- using static System.Net.Mime.MediaTypeNames;
- using static System.Windows.Forms.VisualStyles.VisualStyleElement;
- using KeyEventArgs = System.Windows.Input.KeyEventArgs;
- using ListViewItem = System.Windows.Controls.ListViewItem;
- using TextBox = System.Windows.Controls.TextBox;
- using Task = System.Threading.Tasks.Task;
- using Microsoft.Office.Interop.Word;
- using System.Threading.Tasks;
- using static Dropbox.Api.TeamLog.TimeUnit;
- using System.Threading;
- using static Dropbox.Api.Files.SaveUrlError;
- using System.Windows.Documents;
- using System.Runtime.Remoting.Messaging;
- using ListView = System.Windows.Controls.ListView;
- using PDF_Master.EventAggregators;
- using Prism.Events;
- using System.Windows.Data;
- namespace PDF_Master.ViewModels.BOTA
- {
- public class BookmarkContentViewModel : BindableBase, INavigationAware
- {
- #region 文案
- private string T_emptyTitle;
- public string T_EmptyTitle
- {
- get { return T_emptyTitle; }
- set
- {
- SetProperty(ref T_emptyTitle, value);
- }
- }
- private string T_emptyContext;
- public string T_EmptyContext
- {
- get { return T_emptyContext; }
- set
- {
- SetProperty(ref T_emptyContext, value);
- }
- }
- private void InitString()
- {
- T_EmptyTitle = App.MainPageLoader.GetString("Bookmark_EmptyTitle");
- T_EmptyContext = App.MainPageLoader.GetString("Bookmark_EmptyContext");
- }
- #endregion 文案
- #region 属性
- public bool isAddBookMark = false;
- private IRegionManager region;
- private IDialogService dialogs;
- public CPDFViewer PDFViewer;
- public IEventAggregator events;
- private string unicode;
- /// <summary>
- /// 书签ItemSouce
- /// </summary>
- private ObservableCollection<CPDFBookmark> bookmarklist;
- public ObservableCollection<CPDFBookmark> Bookmarklist
- {
- get
- {
- return bookmarklist;
- }
- set
- {
- SetProperty(ref bookmarklist, value);
- }
- }
- /// <summary>
- /// 书签列表为空时,显示状态
- /// </summary>
- private Visibility isEmpty;
- public Visibility IsEmptyPanelVisibility
- {
- get
- {
- return isEmpty;
- }
- set
- {
- SetProperty(ref isEmpty, value);
- }
- }
- public CPDFBookmark AddCPDFBookmark = null;
- public bool IsHasBookmark = true;
- #endregion 属性
- #region 命令
- public DelegateCommand<object> KeyDownCommand { get; set; }
- public DelegateCommand<object> LostFocusCommand { get; set; }
- public DelegateCommand<object> AddBookmarkCommand { get; set; }
- public DelegateCommand<object> ListViewItemMouseDownCommand { get; set; }
- public DelegateCommand<object> EditPageIndexCommand { get; set; }
- public DelegateCommand<object> DeleteCommand { get; set; }
- #endregion 命令
- public BookmarkContentViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
- {
- region = regionManager;
- dialogs = dialogService;
- events = eventAggregator;
- unicode = App.mainWindowViewModel.SelectedItem.Unicode;
- Bookmarklist = new ObservableCollection<CPDFBookmark>();
- LostFocusCommand = new DelegateCommand<object>(LostFocusEvent);
- AddBookmarkCommand = new DelegateCommand<object>(AddBookmarkEvent);
- ListViewItemMouseDownCommand = new DelegateCommand<object>(ListViewItemMouseLeftButtonDownEvent);
- DeleteCommand = new DelegateCommand<object>(DelegateEvent);
- EditPageIndexCommand = new DelegateCommand<object>(EditPageIndexEvent);
- KeyDownCommand = new DelegateCommand<object>(KeyDownEvent);
- InitString();
- events.GetEvent<DelBookMarkEvent>().Subscribe(DelBook, e => e.Unicode == unicode && e.IsReadMode == true);
- events.GetEvent<AddBookMarkEvent>().Subscribe(AddBook, e => e.Unicode == unicode && e.IsReadMode == true);
- events.GetEvent<PageEditNotifyEvent>().Subscribe(OnNotifyEvent, e => e.Unicode == unicode && e.Type == NotifyType.PageCountChanged);
- }
- private void DelBook(BookMarkArgs obj)
- {
- if (PDFViewer.Document.GetBookmarkList().Count == 0)
- {
- return;
- }
- CPDFBookmark list = PDFViewer.Document.GetBookmarkList().FirstOrDefault(q => q.PageIndex == PDFViewer.CurrentIndex);
- if (list != null)
- {
- bool flag = PDFViewer.Document.RemoveBookmark(PDFViewer.CurrentIndex);
- if (flag)
- {
- Bookmarklist.Remove(list);
- }
- isAddBookMark = true;
- PDFViewer.UndoManager.CanSave = true;
- }
- }
- private void AddBook(BookMarkArgs args)
- {
- string info = App.MainPageLoader.GetString("Bookmark_PageN");
- int index = PDFViewer.CurrentIndex;
- string mark = string.Format(info, (index + 1).ToString());
- CPDFBookmark bookmark = new CPDFBookmark();
- bookmark.Title = mark;
- bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
- bookmark.PageIndex = PDFViewer.CurrentIndex;
- AddCPDFBookmark = bookmark;
- if (PDFViewer.Document.AddBookmark(bookmark))
- {
- PDFViewer.UndoManager.CanSave = true;
- Bookmarklist.Add(bookmark);
- Bookmarklist = new ObservableCollection<CPDFBookmark>(Bookmarklist.OrderBy(item => item.PageIndex));
- Bookmarklist.CollectionChanged += Bookmarklist_CollectionChanged;
- isAddBookMark = false;
- }
- }
- /// <summary>
- /// 页面编辑页数发生变化后触发的事件
- /// 更新BOTA数据源
- /// </summary>
- /// <param name="obj"></param>
- private void OnNotifyEvent(PageEditNotifyEventArgs obj)
- {
- InitBookMarkSource();
- }
- /// <summary>
- /// 检测ObservableCollection的数据变更
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Bookmarklist_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
- {
- ObservableCollection<CPDFBookmark> obsSender = sender as ObservableCollection<CPDFBookmark>;
- if (obsSender != null)
- {
- if (obsSender.Count < 1)
- {
- IsEmptyPanelVisibility = Visibility.Visible;
- }
- else
- {
- IsEmptyPanelVisibility = Visibility.Collapsed;
- }
- }
- else
- {
- IsEmptyPanelVisibility = Visibility.Visible;
- }
- }
- /// <summary>
- /// 文本框按下Enter键
- /// </summary>
- /// <param name="obj"></param>
- private void KeyDownEvent(object obj)
- {
- if (obj is KeyEventArgs)
- {
- KeyEventArgs keyEventArgs = (KeyEventArgs)obj;
- if (keyEventArgs.Key == Key.Enter)
- {
- if (keyEventArgs.OriginalSource is TextBox)
- {
- TextBox textBox = (TextBox)keyEventArgs.OriginalSource;
- ListBoxItem listViewItem = CommonHelper.FindVisualParent<ListBoxItem>(textBox);
- TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listViewItem);
- UpdateTitle(listViewItem, textBox, textBlock);
- textBlock.Visibility = Visibility.Visible;
- }
- }
- }
- }
- /// <summary>
- /// 重新定位
- /// </summary>
- /// <param name="obj"></param>
- private void EditPageIndexEvent(object obj)
- {
- CPDFBookmark bookmark = obj as CPDFBookmark;
- if (bookmark != null)
- {
- AlertsMessage alertsMessage = new AlertsMessage();
- alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Outline_ChangeDest_Tips"), App.ServiceLoader.GetString("Text_cancel"), App.ServiceLoader.GetString("Text_ok"));
- if (alertsMessage.result == ContentResult.Ok)
- {
- if (RemoveBookMark(bookmark))
- {
- bookmarklist.Remove(bookmark);
- CPDFBookmark cPDFBookmark = new CPDFBookmark();
- bookmark.Title = bookmark.Title;
- bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
- bookmark.PageIndex = PDFViewer.CurrentIndex;
- if (PDFViewer.Document.AddBookmark(bookmark))
- {
- PDFViewer.UndoManager.CanSave = true;
- Bookmarklist.Add(bookmark);
- }
- }
- }
- }
- }
- /// <summary>
- /// 右键菜单-删除
- /// </summary>
- /// <param name="obj"></param>
- private void DelegateEvent(object obj)
- {
- CPDFBookmark bookmark = obj as CPDFBookmark;
- if (bookmark != null)
- {
- if (RemoveBookMark(bookmark))
- {
- Bookmarklist.Remove(bookmark);
- isAddBookMark = true;
- PDFViewer.UndoManager.CanSave = true;
- }
- }
- }
- /// <summary>
- /// 删除书签
- /// </summary>
- /// <param name="item"></param>
- /// <returns></returns>
- private bool RemoveBookMark(CPDFBookmark item)
- {
- var data = item;
- if (data == null)
- {
- return false;
- }
- return PDFViewer.Document.RemoveBookmark(data.PageIndex);
- }
- /// <summary>
- /// ListViewItem失去焦点
- /// </summary>
- /// <param name="obj"></param>
- private void LostFocusEvent(object obj)
- {
- try
- {
- //连续快捷键Ctrl+B 两次添加书签时会报错,暂时没有好的方式判断disconnecteditem 先异常捕获处理一下
- TextBox textBox = null;
- ListBoxItem listViewItem = null;
- TextBlock textBlock = null;
- if (obj is CompositeCommandParameter parameter)
- {
- if (parameter.Parameter is TextBox textBox1)
- {
- textBox = textBox1;
- listViewItem = CommonHelper.FindVisualParent<ListViewItem>(textBox);
- textBlock = CommonHelper.FindVisualChild<TextBlock>(listViewItem);
- }
- }
- if (obj is ListBoxItem item)
- {
- listViewItem = item;
- textBox = CommonHelper.FindVisualChild<TextBox>(item);
- textBlock = CommonHelper.FindVisualChild<TextBlock>(item);
- }
- CPDFBookmark bookmark = (CPDFBookmark)listViewItem.DataContext;
- UpdateTitle(listViewItem, textBox, textBlock);
- }
- catch { }
- }
- /// <summary>
- /// 修改书签标题
- /// </summary>
- /// <param name="listViewItem"></param>
- /// <param name="textBox"></param>
- private void UpdateTitle(ListBoxItem listViewItem, TextBox textBox, TextBlock textBlock)
- {
- if (listViewItem != null)
- {
- var data = listViewItem.DataContext as CPDFBookmark;
- if (data == null)
- {
- return;
- }
- var result = PDFViewer.Document.EditBookmark(data.PageIndex, textBox.Text.Trim());
- if (result)
- {
- data.Title = textBox.Text.Trim();
- PDFViewer.UndoManager.CanSave = true;
- textBlock.Text = textBox.Text;
- }
- }
- }
- /// <summary>
- /// ListBoxItem左键单击,页面跳转
- /// </summary>
- /// <param name="obj"></param>
- private void ListViewItemMouseLeftButtonDownEvent(object obj)
- {
- ListBoxItem listBoxItem = (obj as ListBoxItem);
- if (listBoxItem != null)
- {
- if (!(listBoxItem.DataContext is CPDFBookmark))
- {
- return;
- }
- int index = (listBoxItem.DataContext as CPDFBookmark).PageIndex;
- PDFViewer.GoToPage(index);
- }
- }
- /// <summary>
- /// 添加书签
- /// </summary>
- /// <param name="obj"></param>
- private void AddBookmarkEvent(object obj)
- {
- string info = App.MainPageLoader.GetString("Bookmark_PageN");
- int index = PDFViewer.CurrentIndex;
- string mark = string.Format(info, (index + 1).ToString());
- System.Windows.Controls.ListBox listView = obj as System.Windows.Controls.ListBox;
- IsHasBookmark = IsExistBookmark(listView);// await Task.Run(() => IsExistBookmark(listView));
- if (IsHasBookmark == false)
- {
- listView.SelectedItems.Clear();
- CPDFBookmark bookmark = new CPDFBookmark();
- bookmark.Title = mark;
- bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
- bookmark.PageIndex = PDFViewer.CurrentIndex;
- AddCPDFBookmark = bookmark;
- if (PDFViewer.Document.AddBookmark(bookmark))
- {
- PDFViewer.UndoManager.CanSave = true;
- Bookmarklist.Add(bookmark);
- Bookmarklist = new ObservableCollection<CPDFBookmark>(Bookmarklist.OrderBy(item => item.PageIndex));
- Bookmarklist.CollectionChanged += Bookmarklist_CollectionChanged;
- isAddBookMark = false;
- }
- }
- }
- private bool IsExistBookmark(System.Windows.Controls.ListBox listView)
- {
- bool isExistBookmark = false;
- int index = PDFViewer.CurrentIndex;
- if (PDFViewer.Document == null)
- {
- isExistBookmark = true;
- return isExistBookmark;
- }
- //检测是否已存在相同数据
- var list = PDFViewer.Document.GetBookmarkList().FindAll(q => q.PageIndex == index);
- if (list.Count > 0)
- {
- int i = PDFViewer.Document.GetBookmarkList().FindIndex(q => q.PageIndex == index);
- isExistBookmark = true;
- if (listView != null)
- {
- listView.ScrollIntoView(list[0]);
- //ListBoxItem myListBoxItem = await Task.Run(() => GetListViewItem(listView, list[0]));
- ListBoxItem myListBoxItem = (ListBoxItem)(listView.ItemContainerGenerator.ContainerFromItem(list[0]));
- List<ListBoxItem> listItems = new List<ListBoxItem>();
- foreach (var item in listView.SelectedItems)
- {
- listView.ScrollIntoView(item);
- ListBoxItem listViewItem = (ListBoxItem)(listView.ItemContainerGenerator.ContainerFromItem(item));
- if (listItems.IndexOf(listViewItem) == -1)
- {
- listItems.Add(listViewItem);
- }
- }
- if (myListBoxItem.IsSelected == false)
- {
- myListBoxItem.IsSelected = true;
- myListBoxItem.Focus();
- isAddBookMark = false;
- foreach (var item in listItems)
- {
- item.IsSelected = false;
- }
- }
- return isExistBookmark;
- }
- }
- return isExistBookmark;
- }
- /// <summary>
- /// 刷新书签列表数据源
- /// </summary>
- private void InitBookMarkSource()
- {
- if (PDFViewer.Document.GetBookmarkList().Count > 0)
- {
- Bookmarklist = new ObservableCollection<CPDFBookmark>(PDFViewer.Document.GetBookmarkList().OrderBy(d => d.PageIndex));
- }
- //避免重复绑定
- bookmarklist.CollectionChanged -= Bookmarklist_CollectionChanged;
- Bookmarklist.CollectionChanged += Bookmarklist_CollectionChanged;
- }
- public void OnNavigatedTo(NavigationContext navigationContext)
- {
- KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
- KeyEventsHelper.KeyDown += ShortCut_KeyDown;
- navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
- if (PDFViewer == null)
- {
- IsEmptyPanelVisibility = Visibility.Visible;
- return;
- }
- InitBookMarkSource();
- if (Bookmarklist.Count < 1)
- {
- IsEmptyPanelVisibility = Visibility.Visible;
- return;
- }
- else
- {
- IsEmptyPanelVisibility = Visibility.Hidden;
- return;
- }
- }
- public bool IsNavigationTarget(NavigationContext navigationContext)
- {
- return true;
- }
- public void OnNavigatedFrom(NavigationContext navigationContext)
- {
- KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
- }
- private void ShortCut_KeyDown(object sender, KeyEventArgs e)
- {
- if (KeyEventsHelper.IsSingleKey(System.Windows.Input.Key.Escape))
- {
- this.events.GetEvent<CleanSelectAllEvent>().Publish(new CleanSelectAllArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsCleanSelectAll = true });
- }
- }
- }
- }
|