BookmarkContentViewModel.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. UpdateTitle(listViewItem, textBox);
  224. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listViewItem);
  225. textBlock.Text = textBox.Text;
  226. textBlock.Visibility = Visibility.Visible;
  227. }
  228. }
  229. }
  230. }
  231. /// <summary>
  232. /// 重新定位
  233. /// </summary>
  234. /// <param name="obj"></param>
  235. private void EditPageIndexEvent(object obj)
  236. {
  237. CPDFBookmark bookmark = obj as CPDFBookmark;
  238. if (bookmark != null)
  239. {
  240. AlertsMessage alertsMessage = new AlertsMessage();
  241. alertsMessage.ShowDialog("", App.MainPageLoader.GetString("Outline_ChangeDest_Tips"), App.ServiceLoader.GetString("Text_cancel"), App.ServiceLoader.GetString("Text_ok"));
  242. if (alertsMessage.result == ContentResult.Ok)
  243. {
  244. if (RemoveBookMark(bookmark))
  245. {
  246. bookmarklist.Remove(bookmark);
  247. CPDFBookmark cPDFBookmark = new CPDFBookmark();
  248. bookmark.Title = bookmark.Title;
  249. bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
  250. bookmark.PageIndex = PDFViewer.CurrentIndex;
  251. if (PDFViewer.Document.AddBookmark(bookmark))
  252. {
  253. PDFViewer.UndoManager.CanSave = true;
  254. Bookmarklist.Add(bookmark);
  255. }
  256. }
  257. }
  258. }
  259. }
  260. /// <summary>
  261. /// 右键菜单-删除
  262. /// </summary>
  263. /// <param name="obj"></param>
  264. private void DelegateEvent(object obj)
  265. {
  266. CPDFBookmark bookmark = obj as CPDFBookmark;
  267. if (bookmark != null)
  268. {
  269. if (RemoveBookMark(bookmark))
  270. {
  271. Bookmarklist.Remove(bookmark);
  272. isAddBookMark = true;
  273. PDFViewer.UndoManager.CanSave = true;
  274. }
  275. }
  276. }
  277. /// <summary>
  278. /// 删除书签
  279. /// </summary>
  280. /// <param name="item"></param>
  281. /// <returns></returns>
  282. private bool RemoveBookMark(CPDFBookmark item)
  283. {
  284. var data = item;
  285. if (data == null)
  286. {
  287. return false;
  288. }
  289. return PDFViewer.Document.RemoveBookmark(data.PageIndex);
  290. }
  291. /// <summary>
  292. /// ListViewItem失去焦点
  293. /// </summary>
  294. /// <param name="obj"></param>
  295. private void LostFocusEvent(object obj)
  296. {
  297. try
  298. {
  299. //连续快捷键Ctrl+B 两次添加书签时会报错,暂时没有好的方式判断disconnecteditem 先异常捕获处理一下
  300. TextBox textBox = null;
  301. ListViewItem listViewItem = null;
  302. if (obj is CompositeCommandParameter)
  303. {
  304. CompositeCommandParameter parameter = (CompositeCommandParameter)obj;
  305. if (parameter.Parameter is TextBox)
  306. {
  307. textBox = (TextBox)parameter.Parameter;
  308. listViewItem = CommonHelper.FindVisualParent<ListViewItem>(textBox);
  309. }
  310. }
  311. if (obj is ListBoxItem)
  312. {
  313. listViewItem = (ListViewItem)(obj);
  314. textBox = CommonHelper.FindVisualChild<TextBox>(listViewItem);
  315. }
  316. CPDFBookmark bookmark = (CPDFBookmark)listViewItem.DataContext;
  317. UpdateTitle(listViewItem, textBox);
  318. }
  319. catch { }
  320. }
  321. /// <summary>
  322. /// 修改书签标题
  323. /// </summary>
  324. /// <param name="listViewItem"></param>
  325. /// <param name="textBox"></param>
  326. private void UpdateTitle(ListBoxItem listViewItem, TextBox textBox)
  327. {
  328. if (listViewItem != null)
  329. {
  330. var data = listViewItem.DataContext as CPDFBookmark;
  331. if (data == null)
  332. {
  333. return;
  334. }
  335. var result = PDFViewer.Document.EditBookmark(data.PageIndex, textBox.Text.Trim());
  336. if (result)
  337. {
  338. data.Title = textBox.Text.Trim();
  339. PDFViewer.UndoManager.CanSave = true;
  340. }
  341. }
  342. }
  343. /// <summary>
  344. /// ListBoxItem左键单击,页面跳转
  345. /// </summary>
  346. /// <param name="obj"></param>
  347. private void ListViewItemMouseLeftButtonDownEvent(object obj)
  348. {
  349. ListBoxItem listBoxItem = (obj as ListBoxItem);
  350. if (listBoxItem != null)
  351. {
  352. if (!(listBoxItem.DataContext is CPDFBookmark))
  353. {
  354. return;
  355. }
  356. int index = (listBoxItem.DataContext as CPDFBookmark).PageIndex;
  357. PDFViewer.GoToPage(index);
  358. }
  359. }
  360. /// <summary>
  361. /// 添加书签
  362. /// </summary>
  363. /// <param name="obj"></param>
  364. private void AddBookmarkEvent(object obj)
  365. {
  366. string info = App.MainPageLoader.GetString("Bookmark_PageN");
  367. int index = PDFViewer.CurrentIndex;
  368. string mark = string.Format(info, (index + 1).ToString());
  369. System.Windows.Controls.ListBox listView = obj as System.Windows.Controls.ListBox;
  370. listView.SelectedItems.Clear();
  371. IsHasBookmark = IsExistBookmark(listView);// await Task.Run(() => IsExistBookmark(listView));
  372. if (IsHasBookmark == false)
  373. {
  374. CPDFBookmark bookmark = new CPDFBookmark();
  375. bookmark.Title = mark;
  376. bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
  377. bookmark.PageIndex = PDFViewer.CurrentIndex;
  378. AddCPDFBookmark = bookmark;
  379. if (PDFViewer.Document.AddBookmark(bookmark))
  380. {
  381. PDFViewer.UndoManager.CanSave = true;
  382. Bookmarklist.Add(bookmark);
  383. Bookmarklist = new ObservableCollection<CPDFBookmark>(Bookmarklist.OrderBy(item => item.PageIndex));
  384. Bookmarklist.CollectionChanged += Bookmarklist_CollectionChanged;
  385. isAddBookMark = false;
  386. }
  387. //DialogParameters value = new DialogParameters();
  388. //value.Add(ParameterNames.Bookmark, mark);
  389. //value.Add(ParameterNames.Title, "创建一个新的书签");
  390. //dialogs.ShowDialog(DialogNames.AddBookmarkDialog, value, e =>
  391. //{
  392. // if (e.Result == ButtonResult.OK && e.Parameters != null)
  393. // {
  394. // if (e.Parameters.ContainsKey(ParameterNames.Bookmark))
  395. // {
  396. // mark = e.Parameters.GetValue<string>(ParameterNames.Bookmark).ToString();
  397. // CPDFBookmark bookmark = new CPDFBookmark();
  398. // bookmark.Title = mark;
  399. // bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
  400. // bookmark.PageIndex = PDFViewer.CurrentIndex;
  401. // AddCPDFBookmark = bookmark;
  402. // if (PDFViewer.Document.AddBookmark(bookmark))
  403. // {
  404. // PDFViewer.UndoManager.CanSave = true;
  405. // Bookmarklist.Add(bookmark);
  406. // Bookmarklist = new ObservableCollection<CPDFBookmark>(Bookmarklist.OrderBy(item => item.PageIndex));
  407. // Bookmarklist.CollectionChanged += Bookmarklist_CollectionChanged;
  408. // }
  409. // }
  410. // isAddBookMark = false;
  411. // }
  412. // if (e.Result == ButtonResult.Cancel)
  413. // {
  414. // isAddBookMark = true;
  415. // }
  416. //});
  417. }
  418. }
  419. private bool IsExistBookmark(System.Windows.Controls.ListBox listView)
  420. {
  421. bool isExistBookmark = false;
  422. int index = PDFViewer.CurrentIndex;
  423. if (PDFViewer.Document == null)
  424. {
  425. isExistBookmark = true;
  426. return isExistBookmark;
  427. }
  428. //检测是否已存在相同数据
  429. var list = PDFViewer.Document.GetBookmarkList().FindAll(q => q.PageIndex == index);
  430. if (list.Count > 0)
  431. {
  432. int i = PDFViewer.Document.GetBookmarkList().FindIndex(q => q.PageIndex == index);
  433. isExistBookmark = true;
  434. if (listView != null)
  435. {
  436. listView.ScrollIntoView(list[0]);
  437. //ListBoxItem myListBoxItem = await Task.Run(() => GetListViewItem(listView, list[0]));
  438. ListBoxItem myListBoxItem = (ListBoxItem)(listView.ItemContainerGenerator.ContainerFromItem(list[0]));
  439. List<ListBoxItem> listItems = new List<ListBoxItem>();
  440. foreach (var item in listView.SelectedItems)
  441. {
  442. listView.ScrollIntoView(item);
  443. ListBoxItem listViewItem = (ListBoxItem)(listView.ItemContainerGenerator.ContainerFromItem(item));
  444. if (listItems.IndexOf(listViewItem) == -1)
  445. {
  446. listItems.Add(listViewItem);
  447. }
  448. }
  449. if (myListBoxItem.IsSelected == false)
  450. {
  451. myListBoxItem.IsSelected = true;
  452. myListBoxItem.Focus();
  453. isAddBookMark = false;
  454. foreach (var item in listItems)
  455. {
  456. item.IsSelected = false;
  457. }
  458. }
  459. return isExistBookmark;
  460. }
  461. }
  462. return isExistBookmark;
  463. }
  464. /// <summary>
  465. /// 刷新书签列表数据源
  466. /// </summary>
  467. private void InitBookMarkSource()
  468. {
  469. if (PDFViewer.Document.GetBookmarkList().Count > 0)
  470. {
  471. Bookmarklist = new ObservableCollection<CPDFBookmark>(PDFViewer.Document.GetBookmarkList().OrderBy(d => d.PageIndex));
  472. }
  473. //避免重复绑定
  474. bookmarklist.CollectionChanged -= Bookmarklist_CollectionChanged;
  475. Bookmarklist.CollectionChanged += Bookmarklist_CollectionChanged;
  476. }
  477. public void OnNavigatedTo(NavigationContext navigationContext)
  478. {
  479. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  480. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  481. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  482. if (PDFViewer == null)
  483. {
  484. IsEmptyPanelVisibility = Visibility.Visible;
  485. return;
  486. }
  487. InitBookMarkSource();
  488. if (Bookmarklist.Count < 1)
  489. {
  490. IsEmptyPanelVisibility = Visibility.Visible;
  491. return;
  492. }
  493. else
  494. {
  495. IsEmptyPanelVisibility = Visibility.Hidden;
  496. return;
  497. }
  498. }
  499. public bool IsNavigationTarget(NavigationContext navigationContext)
  500. {
  501. return true;
  502. }
  503. public void OnNavigatedFrom(NavigationContext navigationContext)
  504. {
  505. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  506. }
  507. private void ShortCut_KeyDown(object sender, KeyEventArgs e)
  508. {
  509. if (KeyEventsHelper.IsSingleKey(System.Windows.Input.Key.Escape))
  510. {
  511. this.events.GetEvent<CleanSelectAllEvent>().Publish(new CleanSelectAllArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsCleanSelectAll = true });
  512. }
  513. }
  514. }
  515. }