BookmarkContentViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. namespace PDF_Master.ViewModels.BOTA
  43. {
  44. public class BookmarkContentViewModel : BindableBase, INavigationAware
  45. {
  46. #region 文案
  47. private string T_emptyTitle;
  48. public string T_EmptyTitle
  49. {
  50. get { return T_emptyTitle; }
  51. set
  52. {
  53. SetProperty(ref T_emptyTitle, value);
  54. }
  55. }
  56. private string T_emptyContext;
  57. public string T_EmptyContext
  58. {
  59. get { return T_emptyContext; }
  60. set
  61. {
  62. SetProperty(ref T_emptyContext, value);
  63. }
  64. }
  65. private void InitString()
  66. {
  67. T_EmptyTitle = App.MainPageLoader.GetString("Bookmark_EmptyTitle");
  68. T_EmptyContext = App.MainPageLoader.GetString("Bookmark_EmptyContext");
  69. }
  70. #endregion 文案
  71. #region 属性
  72. public bool isAddBookMark = false;
  73. private IRegionManager region;
  74. private IDialogService dialogs;
  75. public CPDFViewer PDFViewer;
  76. public IEventAggregator events;
  77. /// <summary>
  78. /// 书签ItemSouce
  79. /// </summary>
  80. private ObservableCollection<CPDFBookmark> bookmarklist;
  81. public ObservableCollection<CPDFBookmark> Bookmarklist
  82. {
  83. get
  84. {
  85. return bookmarklist;
  86. }
  87. set
  88. {
  89. SetProperty(ref bookmarklist, value);
  90. }
  91. }
  92. /// <summary>
  93. /// 书签列表为空时,显示状态
  94. /// </summary>
  95. private Visibility isEmpty;
  96. public Visibility IsEmptyPanelVisibility
  97. {
  98. get
  99. {
  100. return isEmpty;
  101. }
  102. set
  103. {
  104. SetProperty(ref isEmpty, value);
  105. }
  106. }
  107. public CPDFBookmark AddCPDFBookmark = null;
  108. public bool IsHasBookmark = true;
  109. #endregion 属性
  110. #region 命令
  111. public DelegateCommand<object> KeyDownCommand { get; set; }
  112. public DelegateCommand<object> LostFocusCommand { get; set; }
  113. public DelegateCommand<object> AddBookmarkCommand { get; set; }
  114. public DelegateCommand<object> ListViewItemMouseDownCommand { get; set; }
  115. public DelegateCommand<object> EditPageIndexCommand { get; set; }
  116. public DelegateCommand<object> DeleteCommand { get; set; }
  117. #endregion 命令
  118. public BookmarkContentViewModel(IRegionManager regionManager, IDialogService dialogService, IEventAggregator eventAggregator)
  119. {
  120. region = regionManager;
  121. dialogs = dialogService;
  122. events = eventAggregator;
  123. Bookmarklist = new ObservableCollection<CPDFBookmark>();
  124. LostFocusCommand = new DelegateCommand<object>(LostFocusEvent);
  125. AddBookmarkCommand = new DelegateCommand<object>(AddBookmarkEvent);
  126. ListViewItemMouseDownCommand = new DelegateCommand<object>(ListViewItemMouseLeftButtonDownEvent);
  127. DeleteCommand = new DelegateCommand<object>(DelegateEvent);
  128. EditPageIndexCommand = new DelegateCommand<object>(EditPageIndexEvent);
  129. KeyDownCommand = new DelegateCommand<object>(KeyDownEvent);
  130. InitString();
  131. }
  132. /// <summary>
  133. /// 检测ObservableCollection的数据变更
  134. /// </summary>
  135. /// <param name="sender"></param>
  136. /// <param name="e"></param>
  137. private void Bookmarklist_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
  138. {
  139. ObservableCollection<CPDFBookmark> obsSender = sender as ObservableCollection<CPDFBookmark>;
  140. if (obsSender != null)
  141. {
  142. if (obsSender.Count < 1)
  143. {
  144. IsEmptyPanelVisibility = Visibility.Visible;
  145. }
  146. else
  147. {
  148. IsEmptyPanelVisibility = Visibility.Collapsed;
  149. }
  150. }
  151. else
  152. {
  153. IsEmptyPanelVisibility = Visibility.Visible;
  154. }
  155. }
  156. /// <summary>
  157. /// 文本框按下Enter键
  158. /// </summary>
  159. /// <param name="obj"></param>
  160. private void KeyDownEvent(object obj)
  161. {
  162. if (obj is KeyEventArgs)
  163. {
  164. KeyEventArgs keyEventArgs = (KeyEventArgs)obj;
  165. if (keyEventArgs.Key == Key.Enter)
  166. {
  167. if (keyEventArgs.OriginalSource is TextBox)
  168. {
  169. TextBox textBox = (TextBox)keyEventArgs.OriginalSource;
  170. ListViewItem listViewItem = CommonHelper.FindVisualParent<ListViewItem>(textBox);
  171. UpdateTitle(listViewItem, textBox);
  172. TextBlock textBlock = CommonHelper.FindVisualChild<TextBlock>(listViewItem);
  173. textBlock.Text = textBox.Text;
  174. textBlock.Visibility = Visibility.Visible;
  175. }
  176. }
  177. }
  178. }
  179. /// <summary>
  180. /// 重新定位
  181. /// </summary>
  182. /// <param name="obj"></param>
  183. private void EditPageIndexEvent(object obj)
  184. {
  185. CPDFBookmark bookmark = obj as CPDFBookmark;
  186. if (bookmark != null)
  187. {
  188. AlertsMessage alertsMessage = new AlertsMessage();
  189. alertsMessage.ShowDialog("提示", "确定要将选定大纲的目标位置设置为当前位置吗?", "Cancel", "OK");
  190. if (alertsMessage.result == ContentResult.Ok)
  191. {
  192. if (RemoveBookMark(bookmark))
  193. {
  194. bookmarklist.Remove(bookmark);
  195. CPDFBookmark cPDFBookmark = new CPDFBookmark();
  196. bookmark.Title = bookmark.Title;
  197. bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
  198. bookmark.PageIndex = PDFViewer.CurrentIndex;
  199. if (PDFViewer.Document.AddBookmark(bookmark))
  200. {
  201. PDFViewer.UndoManager.CanSave = true;
  202. Bookmarklist.Add(bookmark);
  203. }
  204. }
  205. }
  206. }
  207. }
  208. /// <summary>
  209. /// 右键菜单-删除
  210. /// </summary>
  211. /// <param name="obj"></param>
  212. private void DelegateEvent(object obj)
  213. {
  214. CPDFBookmark bookmark = obj as CPDFBookmark;
  215. if (bookmark != null)
  216. {
  217. if (RemoveBookMark(bookmark))
  218. {
  219. Bookmarklist.Remove(bookmark);
  220. isAddBookMark = true;
  221. PDFViewer.UndoManager.CanSave = true;
  222. }
  223. }
  224. }
  225. /// <summary>
  226. /// 删除书签
  227. /// </summary>
  228. /// <param name="item"></param>
  229. /// <returns></returns>
  230. private bool RemoveBookMark(CPDFBookmark item)
  231. {
  232. var data = item;
  233. if (data == null)
  234. {
  235. return false;
  236. }
  237. return PDFViewer.Document.RemoveBookmark(data.PageIndex);
  238. }
  239. /// <summary>
  240. /// ListViewItem失去焦点
  241. /// </summary>
  242. /// <param name="obj"></param>
  243. private void LostFocusEvent(object obj)
  244. {
  245. TextBox textBox = null;
  246. ListViewItem listViewItem = null;
  247. if (obj is CompositeCommandParameter)
  248. {
  249. CompositeCommandParameter parameter = (CompositeCommandParameter)obj;
  250. if (parameter.Parameter is TextBox)
  251. {
  252. textBox = (TextBox)parameter.Parameter;
  253. listViewItem = CommonHelper.FindVisualParent<ListViewItem>(textBox);
  254. }
  255. }
  256. if (obj is ListBoxItem)
  257. {
  258. listViewItem = (ListViewItem)(obj);
  259. textBox = CommonHelper.FindVisualChild<TextBox>(listViewItem);
  260. }
  261. CPDFBookmark bookmark = (CPDFBookmark)listViewItem.DataContext;
  262. UpdateTitle(listViewItem, textBox);
  263. }
  264. /// <summary>
  265. /// 修改书签标题
  266. /// </summary>
  267. /// <param name="listViewItem"></param>
  268. /// <param name="textBox"></param>
  269. private void UpdateTitle(ListViewItem listViewItem, TextBox textBox)
  270. {
  271. if (listViewItem != null)
  272. {
  273. var data = listViewItem.DataContext as CPDFBookmark;
  274. if (data == null)
  275. {
  276. return;
  277. }
  278. var result = PDFViewer.Document.EditBookmark(data.PageIndex, textBox.Text.Trim());
  279. if (result)
  280. {
  281. data.Title = textBox.Text.Trim();
  282. PDFViewer.UndoManager.CanSave = true;
  283. }
  284. }
  285. }
  286. /// <summary>
  287. /// ListBoxItem左键单击,页面跳转
  288. /// </summary>
  289. /// <param name="obj"></param>
  290. private void ListViewItemMouseLeftButtonDownEvent(object obj)
  291. {
  292. ListBoxItem listBoxItem = (obj as ListBoxItem);
  293. if (listBoxItem != null)
  294. {
  295. if (!(listBoxItem.DataContext is CPDFBookmark))
  296. {
  297. return;
  298. }
  299. int index = (listBoxItem.DataContext as CPDFBookmark).PageIndex;
  300. PDFViewer.GoToPage(index);
  301. }
  302. }
  303. /// <summary>
  304. /// 添加书签
  305. /// </summary>
  306. /// <param name="obj"></param>
  307. private void AddBookmarkEvent(object obj)
  308. {
  309. string info = App.MainPageLoader.GetString("Bookmark_PageN");
  310. int index = PDFViewer.CurrentIndex;
  311. string mark = string.Format(info, (index + 1).ToString());
  312. System.Windows.Controls.ListView listView = obj as System.Windows.Controls.ListView;
  313. listView.SelectedItems.Clear();
  314. IsHasBookmark = IsExistBookmark(listView);// await Task.Run(() => IsExistBookmark(listView));
  315. if (IsHasBookmark == false)
  316. {
  317. CPDFBookmark bookmark = new CPDFBookmark();
  318. bookmark.Title = mark;
  319. bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
  320. bookmark.PageIndex = PDFViewer.CurrentIndex;
  321. AddCPDFBookmark = bookmark;
  322. if (PDFViewer.Document.AddBookmark(bookmark))
  323. {
  324. PDFViewer.UndoManager.CanSave = true;
  325. Bookmarklist.Add(bookmark);
  326. Bookmarklist = new ObservableCollection<CPDFBookmark>(Bookmarklist.OrderBy(item => item.PageIndex));
  327. Bookmarklist.CollectionChanged += Bookmarklist_CollectionChanged;
  328. isAddBookMark = false;
  329. }
  330. //DialogParameters value = new DialogParameters();
  331. //value.Add(ParameterNames.Bookmark, mark);
  332. //value.Add(ParameterNames.Title, "创建一个新的书签");
  333. //dialogs.ShowDialog(DialogNames.AddBookmarkDialog, value, e =>
  334. //{
  335. // if (e.Result == ButtonResult.OK && e.Parameters != null)
  336. // {
  337. // if (e.Parameters.ContainsKey(ParameterNames.Bookmark))
  338. // {
  339. // mark = e.Parameters.GetValue<string>(ParameterNames.Bookmark).ToString();
  340. // CPDFBookmark bookmark = new CPDFBookmark();
  341. // bookmark.Title = mark;
  342. // bookmark.Date = DateTime.Now.ToString(@"yyyyMMddHHmmsszzz\'").Replace(':', '\'') + "\n";
  343. // bookmark.PageIndex = PDFViewer.CurrentIndex;
  344. // AddCPDFBookmark = bookmark;
  345. // if (PDFViewer.Document.AddBookmark(bookmark))
  346. // {
  347. // PDFViewer.UndoManager.CanSave = true;
  348. // Bookmarklist.Add(bookmark);
  349. // Bookmarklist = new ObservableCollection<CPDFBookmark>(Bookmarklist.OrderBy(item => item.PageIndex));
  350. // Bookmarklist.CollectionChanged += Bookmarklist_CollectionChanged;
  351. // }
  352. // }
  353. // isAddBookMark = false;
  354. // }
  355. // if (e.Result == ButtonResult.Cancel)
  356. // {
  357. // isAddBookMark = true;
  358. // }
  359. //});
  360. }
  361. }
  362. private bool IsExistBookmark(System.Windows.Controls.ListView listView)
  363. {
  364. bool isExistBookmark = false;
  365. int index = PDFViewer.CurrentIndex;
  366. if (PDFViewer.Document == null)
  367. {
  368. isExistBookmark = true;
  369. return isExistBookmark;
  370. }
  371. //检测是否已存在相同数据
  372. var list = PDFViewer.Document.GetBookmarkList().FindAll(q => q.PageIndex == index);
  373. if (list.Count > 0)
  374. {
  375. int i = PDFViewer.Document.GetBookmarkList().FindIndex(q => q.PageIndex == index);
  376. isExistBookmark = true;
  377. if (listView != null)
  378. {
  379. listView.ScrollIntoView(list[0]);
  380. //ListBoxItem myListBoxItem = await Task.Run(() => GetListViewItem(listView, list[0]));
  381. ListViewItem myListBoxItem = (ListViewItem)(listView.ItemContainerGenerator.ContainerFromItem(list[0]));
  382. List<ListViewItem> listItems = new List<ListViewItem>();
  383. foreach (var item in listView.SelectedItems)
  384. {
  385. listView.ScrollIntoView(item);
  386. ListViewItem listViewItem = (ListViewItem)(listView.ItemContainerGenerator.ContainerFromItem(item));
  387. if (listItems.IndexOf(listViewItem) == -1)
  388. {
  389. listItems.Add(listViewItem);
  390. }
  391. }
  392. if (myListBoxItem.IsSelected == false)
  393. {
  394. myListBoxItem.IsSelected = true;
  395. myListBoxItem.Focus();
  396. isAddBookMark = false;
  397. foreach (var item in listItems)
  398. {
  399. item.IsSelected = false;
  400. }
  401. }
  402. return isExistBookmark;
  403. }
  404. }
  405. return isExistBookmark;
  406. }
  407. public void OnNavigatedTo(NavigationContext navigationContext)
  408. {
  409. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  410. KeyEventsHelper.KeyDown += ShortCut_KeyDown;
  411. navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
  412. if (PDFViewer == null)
  413. {
  414. IsEmptyPanelVisibility = Visibility.Visible;
  415. return;
  416. }
  417. Bookmarklist = new ObservableCollection<CPDFBookmark>(PDFViewer.Document.GetBookmarkList().OrderBy(d => d.PageIndex));
  418. Bookmarklist.CollectionChanged += Bookmarklist_CollectionChanged;
  419. if (Bookmarklist.Count < 1)
  420. {
  421. IsEmptyPanelVisibility = Visibility.Visible;
  422. return;
  423. }
  424. else
  425. {
  426. IsEmptyPanelVisibility = Visibility.Hidden;
  427. return;
  428. }
  429. }
  430. public bool IsNavigationTarget(NavigationContext navigationContext)
  431. {
  432. return true;
  433. }
  434. public void OnNavigatedFrom(NavigationContext navigationContext)
  435. {
  436. KeyEventsHelper.KeyDown -= ShortCut_KeyDown;
  437. }
  438. private void ShortCut_KeyDown(object sender, KeyEventArgs e)
  439. {
  440. if (KeyEventsHelper.IsSingleKey(System.Windows.Input.Key.Escape))
  441. {
  442. this.events.GetEvent<CleanSelectAllEvent>().Publish(new CleanSelectAllArgs() { Unicode = App.mainWindowViewModel.SelectedItem.Unicode, IsCleanSelectAll = true });
  443. }
  444. }
  445. }
  446. }