BookmarkContentViewModel.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. using ComPDFKit.PDFDocument;
  2. using ComPDFKitViewer.PdfViewer;
  3. using ImTools;
  4. using PDF_Master.CustomControl;
  5. using PDF_Master.Helper;
  6. using PDF_Master.Model;
  7. using PDF_Master.Model.PageEdit;
  8. using Prism.Commands;
  9. using Prism.Mvvm;
  10. using Prism.Regions;
  11. using Prism.Services.Dialogs;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Collections.ObjectModel;
  15. using System.Linq;
  16. using System.Reflection;
  17. using System.Windows;
  18. using System.Windows.Controls;
  19. using System.Windows.Controls.Primitives;
  20. using System.Windows.Forms;
  21. using System.Windows.Input;
  22. using System.Windows.Markup;
  23. using System.Windows.Media;
  24. using static Dropbox.Api.Files.SearchMatchType;
  25. using static Dropbox.Api.TeamLog.AdminAlertSeverityEnum;
  26. using static System.Net.Mime.MediaTypeNames;
  27. using static System.Windows.Forms.VisualStyles.VisualStyleElement;
  28. using KeyEventArgs = System.Windows.Input.KeyEventArgs;
  29. using ListViewItem = System.Windows.Controls.ListViewItem;
  30. using TextBox = System.Windows.Controls.TextBox;
  31. using Task = System.Threading.Tasks.Task;
  32. using Microsoft.Office.Interop.Word;
  33. using System.Threading.Tasks;
  34. using static Dropbox.Api.TeamLog.TimeUnit;
  35. using System.Threading;
  36. using static Dropbox.Api.Files.SaveUrlError;
  37. using System.Windows.Documents;
  38. using System.Runtime.Remoting.Messaging;
  39. using ListView = System.Windows.Controls.ListView;
  40. using PDF_Master.EventAggregators;
  41. using Prism.Events;
  42. using System.Windows.Data;
  43. namespace PDF_Master.ViewModels.BOTA
  44. {
  45. public class BookmarkContentViewModel : BindableBase, INavigationAware
  46. {
  47. #region 文案
  48. private string T_emptyTitle;
  49. public string T_EmptyTitle
  50. {
  51. get { return T_emptyTitle; }
  52. set
  53. {
  54. SetProperty(ref T_emptyTitle, value);
  55. }
  56. }
  57. private string T_emptyContext;
  58. public string T_EmptyContext
  59. {
  60. get { return T_emptyContext; }
  61. set
  62. {
  63. SetProperty(ref T_emptyContext, value);
  64. }
  65. }
  66. private void InitString()
  67. {
  68. T_EmptyTitle = App.MainPageLoader.GetString("Bookmark_EmptyTitle");
  69. T_EmptyContext = App.MainPageLoader.GetString("Bookmark_EmptyContext");
  70. }
  71. #endregion 文案
  72. #region 属性
  73. public bool isAddBookMark = false;
  74. private IRegionManager region;
  75. private IDialogService dialogs;
  76. public CPDFViewer PDFViewer;
  77. public IEventAggregator events;
  78. private string unicode;
  79. /// <summary>
  80. /// 书签ItemSouce
  81. /// </summary>
  82. private ObservableCollection<CPDFBookmark> bookmarklist;
  83. public ObservableCollection<CPDFBookmark> Bookmarklist
  84. {
  85. get
  86. {
  87. return bookmarklist;
  88. }
  89. set
  90. {
  91. SetProperty(ref bookmarklist, value);
  92. }
  93. }
  94. /// <summary>
  95. /// 书签列表为空时,显示状态
  96. /// </summary>
  97. private Visibility isEmpty;
  98. public Visibility IsEmptyPanelVisibility
  99. {
  100. get
  101. {
  102. return isEmpty;
  103. }
  104. set
  105. {
  106. SetProperty(ref isEmpty, value);
  107. }
  108. }
  109. public CPDFBookmark AddCPDFBookmark = null;
  110. public bool IsHasBookmark = true;
  111. #endregion 属性
  112. #region 命令
  113. public DelegateCommand<object> KeyDownCommand { get; set; }
  114. public DelegateCommand<object> LostFocusCommand { get; set; }
  115. public DelegateCommand<object> AddBookmarkCommand { get; set; }
  116. public DelegateCommand<object> ListViewItemMouseDownCommand { get; set; }
  117. public DelegateCommand<object> EditPageIndexCommand { get; set; }
  118. public DelegateCommand<object> DeleteCommand { get; set; }
  119. #endregion 命令
  120. public BookmarkContentViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
  121. {
  122. region = regionManager;
  123. dialogs = dialogService;
  124. events = eventAggregator;
  125. unicode = App.mainWindowViewModel.SelectedItem.Unicode;
  126. Bookmarklist = new ObservableCollection<CPDFBookmark>();
  127. LostFocusCommand = new DelegateCommand<object>(LostFocusEvent);
  128. AddBookmarkCommand = new DelegateCommand<object>(AddBookmarkEvent);
  129. ListViewItemMouseDownCommand = new DelegateCommand<object>(ListViewItemMouseLeftButtonDownEvent);
  130. DeleteCommand = new DelegateCommand<object>(DelegateEvent);
  131. EditPageIndexCommand = new DelegateCommand<object>(EditPageIndexEvent);
  132. KeyDownCommand = new DelegateCommand<object>(KeyDownEvent);
  133. InitString();
  134. events.GetEvent<DelBookMarkEvent>().Subscribe(DelBook, e => e.Unicode == unicode && e.IsReadMode == true);
  135. events.GetEvent<AddBookMarkEvent>().Subscribe(AddBook, e => e.Unicode == unicode && e.IsReadMode == true);
  136. events.GetEvent<PageEditNotifyEvent>().Subscribe(OnNotifyEvent, e => e.Unicode == unicode && e.Type == NotifyType.PageCountChanged);
  137. }
  138. private void DelBook(BookMarkArgs obj)
  139. {
  140. if (PDFViewer.Document.GetBookmarkList().Count == 0)
  141. {
  142. return;
  143. }
  144. CPDFBookmark list = PDFViewer.Document.GetBookmarkList().FirstOrDefault(q => q.PageIndex == PDFViewer.CurrentIndex);
  145. if (list != null)
  146. {
  147. bool flag = PDFViewer.Document.RemoveBookmark(PDFViewer.CurrentIndex);
  148. if (flag)
  149. {
  150. Bookmarklist.Remove(list);
  151. }
  152. isAddBookMark = true;
  153. PDFViewer.UndoManager.CanSave = true;
  154. }
  155. }
  156. private void AddBook(BookMarkArgs args)
  157. {
  158. string info = App.MainPageLoader.GetString("Bookmark_PageN");
  159. int index = PDFViewer.CurrentIndex;
  160. string mark = string.Format(info, (index + 1).ToString());
  161. CPDFBookmark bookmark = new CPDFBookmark();
  162. bookmark.Title = mark;
  163. bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
  164. bookmark.PageIndex = PDFViewer.CurrentIndex;
  165. AddCPDFBookmark = bookmark;
  166. if (PDFViewer.Document.AddBookmark(bookmark))
  167. {
  168. PDFViewer.UndoManager.CanSave = true;
  169. Bookmarklist.Add(bookmark);
  170. Bookmarklist = new ObservableCollection<CPDFBookmark>(Bookmarklist.OrderBy(item => item.PageIndex));
  171. Bookmarklist.CollectionChanged += Bookmarklist_CollectionChanged;
  172. isAddBookMark = false;
  173. }
  174. }
  175. /// <summary>
  176. /// 页面编辑页数发生变化后触发的事件
  177. /// 更新BOTA数据源
  178. /// </summary>
  179. /// <param name="obj"></param>
  180. private void OnNotifyEvent(PageEditNotifyEventArgs obj)
  181. {
  182. InitBookMarkSource();
  183. }
  184. /// <summary>
  185. /// 检测ObservableCollection的数据变更
  186. /// </summary>
  187. /// <param name="sender"></param>
  188. /// <param name="e"></param>
  189. private void Bookmarklist_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  190. {
  191. ObservableCollection<CPDFBookmark> obsSender = sender as ObservableCollection<CPDFBookmark>;
  192. if (obsSender != null)
  193. {
  194. if (obsSender.Count < 1)
  195. {
  196. IsEmptyPanelVisibility = Visibility.Visible;
  197. }
  198. else
  199. {
  200. IsEmptyPanelVisibility = Visibility.Collapsed;
  201. }
  202. }
  203. else
  204. {
  205. IsEmptyPanelVisibility = Visibility.Visible;
  206. }
  207. }
  208. /// <summary>
  209. /// 文本框按下Enter键
  210. /// </summary>
  211. /// <param name="obj"></param>
  212. private void KeyDownEvent(object obj)
  213. {
  214. if (obj is KeyEventArgs)
  215. {
  216. KeyEventArgs keyEventArgs = (KeyEventArgs)obj;
  217. if (keyEventArgs.Key == Key.Enter)
  218. {
  219. if (keyEventArgs.OriginalSource is TextBox)
  220. {
  221. TextBox textBox = (TextBox)keyEventArgs.OriginalSource;
  222. ListBoxItem listViewItem = CommonHelper.FindVisualParent<ListBoxItem>(textBox);
  223. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listViewItem);
  224. UpdateTitle(listViewItem, textBox, textBlock);
  225. textBlock.Visibility = Visibility.Visible;
  226. }
  227. }
  228. }
  229. }
  230. /// <summary>
  231. /// 重新定位
  232. /// </summary>
  233. /// <param name="obj"></param>
  234. private void EditPageIndexEvent(object obj)
  235. {
  236. CPDFBookmark bookmark = obj as CPDFBookmark;
  237. if (bookmark != null)
  238. {
  239. AlertsMessage alertsMessage = new AlertsMessage();
  240. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Outline_ChangeDest_Tips"), App.ServiceLoader.GetString("Text_cancel"), App.ServiceLoader.GetString("Text_ok"));
  241. if (alertsMessage.result == ContentResult.Ok)
  242. {
  243. if (RemoveBookMark(bookmark))
  244. {
  245. bookmarklist.Remove(bookmark);
  246. CPDFBookmark cPDFBookmark = new CPDFBookmark();
  247. bookmark.Title = bookmark.Title;
  248. bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
  249. bookmark.PageIndex = PDFViewer.CurrentIndex;
  250. if (PDFViewer.Document.AddBookmark(bookmark))
  251. {
  252. PDFViewer.UndoManager.CanSave = true;
  253. Bookmarklist.Add(bookmark);
  254. }
  255. }
  256. }
  257. }
  258. }
  259. /// <summary>
  260. /// 右键菜单-删除
  261. /// </summary>
  262. /// <param name="obj"></param>
  263. private void DelegateEvent(object obj)
  264. {
  265. CPDFBookmark bookmark = obj as CPDFBookmark;
  266. if (bookmark != null)
  267. {
  268. if (RemoveBookMark(bookmark))
  269. {
  270. Bookmarklist.Remove(bookmark);
  271. isAddBookMark = true;
  272. PDFViewer.UndoManager.CanSave = true;
  273. }
  274. }
  275. }
  276. /// <summary>
  277. /// 删除书签
  278. /// </summary>
  279. /// <param name="item"></param>
  280. /// <returns></returns>
  281. private bool RemoveBookMark(CPDFBookmark item)
  282. {
  283. var data = item;
  284. if (data == null)
  285. {
  286. return false;
  287. }
  288. return PDFViewer.Document.RemoveBookmark(data.PageIndex);
  289. }
  290. /// <summary>
  291. /// ListViewItem失去焦点
  292. /// </summary>
  293. /// <param name="obj"></param>
  294. private void LostFocusEvent(object obj)
  295. {
  296. try
  297. {
  298. //连续快捷键Ctrl+B 两次添加书签时会报错,暂时没有好的方式判断disconnecteditem 先异常捕获处理一下
  299. TextBox textBox = null;
  300. ListBoxItem listViewItem = null;
  301. TextBlock textBlock = null;
  302. if (obj is CompositeCommandParameter parameter)
  303. {
  304. if (parameter.Parameter is TextBox textBox1)
  305. {
  306. textBox = textBox1;
  307. listViewItem = CommonHelper.FindVisualParent<ListViewItem>(textBox);
  308. textBlock = CommonHelper.FindVisualChild<TextBlock>(listViewItem);
  309. }
  310. }
  311. if (obj is ListBoxItem item)
  312. {
  313. listViewItem = item;
  314. textBox = CommonHelper.FindVisualChild<TextBox>(item);
  315. textBlock = CommonHelper.FindVisualChild<TextBlock>(item);
  316. }
  317. CPDFBookmark bookmark = (CPDFBookmark)listViewItem.DataContext;
  318. UpdateTitle(listViewItem, textBox, textBlock);
  319. }
  320. catch { }
  321. }
  322. /// <summary>
  323. /// 修改书签标题
  324. /// </summary>
  325. /// <param name="listViewItem"></param>
  326. /// <param name="textBox"></param>
  327. private void UpdateTitle(ListBoxItem listViewItem, TextBox textBox, TextBlock textBlock)
  328. {
  329. if (listViewItem != null)
  330. {
  331. var data = listViewItem.DataContext as CPDFBookmark;
  332. if (data == null)
  333. {
  334. return;
  335. }
  336. var result = PDFViewer.Document.EditBookmark(data.PageIndex, textBox.Text.Trim());
  337. if (result)
  338. {
  339. data.Title = textBox.Text.Trim();
  340. PDFViewer.UndoManager.CanSave = true;
  341. textBlock.Text = textBox.Text;
  342. }
  343. }
  344. }
  345. /// <summary>
  346. /// ListBoxItem左键单击,页面跳转
  347. /// </summary>
  348. /// <param name="obj"></param>
  349. private void ListViewItemMouseLeftButtonDownEvent(object obj)
  350. {
  351. ListBoxItem listBoxItem = (obj as ListBoxItem);
  352. if (listBoxItem != null)
  353. {
  354. if (!(listBoxItem.DataContext is CPDFBookmark))
  355. {
  356. return;
  357. }
  358. int index = (listBoxItem.DataContext as CPDFBookmark).PageIndex;
  359. PDFViewer.GoToPage(index);
  360. }
  361. }
  362. /// <summary>
  363. /// 添加书签
  364. /// </summary>
  365. /// <param name="obj"></param>
  366. private void AddBookmarkEvent(object obj)
  367. {
  368. string info = App.MainPageLoader.GetString("Bookmark_PageN");
  369. int index = PDFViewer.CurrentIndex;
  370. string mark = string.Format(info, (index + 1).ToString());
  371. System.Windows.Controls.ListBox listView = obj as System.Windows.Controls.ListBox;
  372. IsHasBookmark = IsExistBookmark(listView);// await Task.Run(() => IsExistBookmark(listView));
  373. if (IsHasBookmark == false)
  374. {
  375. listView.SelectedItems.Clear();
  376. CPDFBookmark bookmark = new CPDFBookmark();
  377. bookmark.Title = mark;
  378. bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
  379. bookmark.PageIndex = PDFViewer.CurrentIndex;
  380. AddCPDFBookmark = bookmark;
  381. if (PDFViewer.Document.AddBookmark(bookmark))
  382. {
  383. PDFViewer.UndoManager.CanSave = true;
  384. Bookmarklist.Add(bookmark);
  385. Bookmarklist = new ObservableCollection<CPDFBookmark>(Bookmarklist.OrderBy(item => item.PageIndex));
  386. Bookmarklist.CollectionChanged += Bookmarklist_CollectionChanged;
  387. isAddBookMark = false;
  388. }
  389. }
  390. }
  391. private bool IsExistBookmark(System.Windows.Controls.ListBox listView)
  392. {
  393. bool isExistBookmark = false;
  394. int index = PDFViewer.CurrentIndex;
  395. if (PDFViewer.Document == null)
  396. {
  397. isExistBookmark = true;
  398. return isExistBookmark;
  399. }
  400. //检测是否已存在相同数据
  401. var list = PDFViewer.Document.GetBookmarkList().FindAll(q => q.PageIndex == index);
  402. if (list.Count > 0)
  403. {
  404. int i = PDFViewer.Document.GetBookmarkList().FindIndex(q => q.PageIndex == index);
  405. isExistBookmark = true;
  406. if (listView != null)
  407. {
  408. listView.ScrollIntoView(list[0]);
  409. //ListBoxItem myListBoxItem = await Task.Run(() => GetListViewItem(listView, list[0]));
  410. ListBoxItem myListBoxItem = (ListBoxItem)(listView.ItemContainerGenerator.ContainerFromItem(list[0]));
  411. List<ListBoxItem> listItems = new List<ListBoxItem>();
  412. foreach (var item in listView.SelectedItems)
  413. {
  414. listView.ScrollIntoView(item);
  415. ListBoxItem listViewItem = (ListBoxItem)(listView.ItemContainerGenerator.ContainerFromItem(item));
  416. if (listItems.IndexOf(listViewItem) == -1)
  417. {
  418. listItems.Add(listViewItem);
  419. }
  420. }
  421. if (myListBoxItem.IsSelected == false)
  422. {
  423. myListBoxItem.IsSelected = true;
  424. myListBoxItem.Focus();
  425. isAddBookMark = false;
  426. foreach (var item in listItems)
  427. {
  428. item.IsSelected = false;
  429. }
  430. }
  431. return isExistBookmark;
  432. }
  433. }
  434. return isExistBookmark;
  435. }
  436. /// <summary>
  437. /// 刷新书签列表数据源
  438. /// </summary>
  439. private void InitBookMarkSource()
  440. {
  441. if (PDFViewer.Document.GetBookmarkList().Count > 0)
  442. {
  443. Bookmarklist = new ObservableCollection<CPDFBookmark>(PDFViewer.Document.GetBookmarkList().OrderBy(d => d.PageIndex));
  444. }
  445. //避免重复绑定
  446. bookmarklist.CollectionChanged -= Bookmarklist_CollectionChanged;
  447. Bookmarklist.CollectionChanged += Bookmarklist_CollectionChanged;
  448. }
  449. public void OnNavigatedTo(NavigationContext navigationContext)
  450. {
  451. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  452. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  453. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  454. if (PDFViewer == null)
  455. {
  456. IsEmptyPanelVisibility = Visibility.Visible;
  457. return;
  458. }
  459. InitBookMarkSource();
  460. if (Bookmarklist.Count < 1)
  461. {
  462. IsEmptyPanelVisibility = Visibility.Visible;
  463. return;
  464. }
  465. else
  466. {
  467. IsEmptyPanelVisibility = Visibility.Hidden;
  468. return;
  469. }
  470. }
  471. public bool IsNavigationTarget(NavigationContext navigationContext)
  472. {
  473. return true;
  474. }
  475. public void OnNavigatedFrom(NavigationContext navigationContext)
  476. {
  477. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  478. }
  479. private void ShortCut_KeyDown(object sender, KeyEventArgs e)
  480. {
  481. if (KeyEventsHelper.IsSingleKey(System.Windows.Input.Key.Escape))
  482. {
  483. this.events.GetEvent<CleanSelectAllEvent>().Publish(new CleanSelectAllArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsCleanSelectAll = true });
  484. }
  485. }
  486. }
  487. }