KMAnnotationLinkViewController.m 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. //
  2. // KMAnnotationLinkViewController.m
  3. // SignFlow
  4. //
  5. // Created by wanjun on 2021/6/23.
  6. //
  7. #import "KMAnnotationLinkViewController.h"
  8. #import "NSButton+TitleColor.h"
  9. #import "TextFieldFormatter.h"
  10. #import "KMBox.h"
  11. #import <ComPDFKit/ComPDFKit.h>
  12. #import "KMCoverButton.h"
  13. #define URLPlaceholder @"https://www.pdfreaderpro.com"
  14. #define EmailPlaceholder @"support@pdfreaderpro.com"
  15. typedef NS_ENUM(NSUInteger, KMAnnotationLinkType) {
  16. KMAnnotationLinkType_Page = 0,
  17. KMAnnotationLinkType_URL,
  18. KMAnnotationLinkType_Email
  19. };
  20. typedef NS_ENUM(NSUInteger, KMAnnotationLinkState) {
  21. KMAnnotationLinkStateNormal = 0,
  22. KMAnnotationLinkStateHover,
  23. KMAnnotationLinkStateSelected
  24. };
  25. @interface KMAnnotationLinkViewController () <NSTextFieldDelegate>
  26. @property (weak) IBOutlet NSView *linkStyleView;
  27. @property (assign) IBOutlet KMBox *linkPageBox;
  28. @property (assign) IBOutlet NSImageView *linkPageImageView;
  29. @property (weak) IBOutlet NSImageView *pageImageThumible;
  30. @property (weak) IBOutlet KMCoverButton *targetButton;
  31. @property (weak) IBOutlet NSTextField *targetLabel;
  32. @property (weak) IBOutlet NSBox *tagrgetBox;
  33. @property (assign) IBOutlet KMBox *linkUrlBox;
  34. @property (assign) IBOutlet NSImageView *linkUrlImageView;
  35. @property (assign) IBOutlet KMBox *linkEmailBox;
  36. @property (assign) IBOutlet NSImageView *linkEmailImageView;
  37. @property (assign) IBOutlet NSBox *contentBox;
  38. @property (assign) IBOutlet NSView *urlView;
  39. @property (assign) IBOutlet NSTextField *urlLabel;
  40. @property (assign) IBOutlet NSTextField *inputUrlTextField;
  41. @property (weak) IBOutlet NSBox *inputUrlBox;
  42. @property (assign) IBOutlet NSButton *goButton;
  43. @property (assign) IBOutlet NSTextField *errorLabel;
  44. @property (weak) IBOutlet NSLayoutConstraint *goButtonTopComstraint;
  45. @property (nonatomic, retain) CPDFLinkAnnotation *annotation;
  46. @property (nonatomic, assign) KMAnnotationLinkType linkType;
  47. @property (nonatomic, retain) KMBox *mouseDownBox;
  48. @property (nonatomic, copy) NSString *pageRecord;
  49. @property (nonatomic, copy) NSString *urlRecord;
  50. @property (nonatomic, copy) NSString *emailRecord;
  51. @property (nonatomic, copy) NSString *startPage;
  52. @property (nonatomic, assign) BOOL isGo;
  53. @property (nonatomic, strong) NSColor *boxNormalBorderColor;
  54. @property (nonatomic, strong) NSColor *boxErrorBorderColor;
  55. @property (nonatomic, assign) KMAnnotationLinkState targetButtonState;
  56. @end
  57. @implementation KMAnnotationLinkViewController
  58. #pragma mark - Init Methods
  59. - (instancetype)init {
  60. if (self = [super init]) {
  61. self.boxNormalBorderColor = [NSColor colorWithRed:223.0/255.0 green:225.0/255.0 blue:229.0/255.0 alpha:1];
  62. self.boxErrorBorderColor = [NSColor colorWithRed:243/255.f green:70/255.f blue:91/255.f alpha:1.f];
  63. }
  64. return self;
  65. }
  66. - (void)dealloc {
  67. [[NSNotificationCenter defaultCenter] removeObserver:self];
  68. _inputUrlTextField.delegate = nil;
  69. NSLog(@"%s", __func__);
  70. }
  71. #pragma mark - View Methods
  72. - (void)viewDidLoad {
  73. [super viewDidLoad];
  74. self.annotation = (CPDFLinkAnnotation *)self.annotationModel.annotation;
  75. self.pageRecord = @"";
  76. self.urlRecord = @"";
  77. self.emailRecord = @"";
  78. _linkStyleView.wantsLayer =
  79. _linkPageImageView.wantsLayer =
  80. _linkUrlImageView.wantsLayer =
  81. _linkEmailImageView.wantsLayer = YES;
  82. _linkStyleView.layer.backgroundColor = [NSColor colorWithRed:223.0/255.0 green:225.0/255.0 blue:229.0/255.0 alpha:1].CGColor;
  83. _linkStyleView.layer.cornerRadius = 6.0;
  84. _linkStyleView.layer.masksToBounds = YES;
  85. NSArray <KMBox *>*boxArr = @[_linkPageBox, _linkUrlBox, _linkEmailBox];
  86. __weak typeof(self) weakSelf = self;
  87. for (KMBox *box in boxArr) {
  88. box.mouseDownCallback = ^(BOOL downEntered, KMBox *mouseBox) {
  89. if (downEntered) {
  90. if ([weakSelf.mouseDownBox isEqual:mouseBox]) {
  91. return;
  92. }
  93. NSInteger mouseDownInt = 0;
  94. if ([weakSelf.linkPageBox isEqual:mouseBox]) {
  95. weakSelf.inputUrlTextField.stringValue = @"";
  96. weakSelf.linkType = KMAnnotationLinkType_Page;
  97. mouseDownInt = 0;
  98. } else if ([weakSelf.linkUrlBox isEqual:mouseBox]) {
  99. weakSelf.inputUrlTextField.stringValue = @"";
  100. weakSelf.linkType = KMAnnotationLinkType_URL;
  101. mouseDownInt = 1;
  102. } else if ([weakSelf.linkEmailBox isEqual:mouseBox]) {
  103. weakSelf.inputUrlTextField.stringValue = @"";
  104. weakSelf.linkType = KMAnnotationLinkType_Email;
  105. mouseDownInt = 2;
  106. }
  107. [[NSUserDefaults standardUserDefaults] setObject:@(mouseDownInt) forKey:@"kmLinkSelectIndex"];
  108. [[NSUserDefaults standardUserDefaults] synchronize];
  109. weakSelf.mouseDownBox = mouseBox;
  110. }
  111. };
  112. }
  113. self.isGo = YES;
  114. self.targetLabel.stringValue = NSLocalizedStringFromTable(@"Locate the target page", @"MainMenu", nil);
  115. self.inputUrlTextField.wantsLayer = YES;
  116. self.inputUrlTextField.focusRingType = NSFocusRingTypeNone;
  117. self.linkPageImageView.layer.cornerRadius =
  118. self.linkUrlImageView.layer.cornerRadius =
  119. self.linkEmailImageView.layer.cornerRadius = 4.0f;
  120. [[self.inputUrlTextField cell] setAllowedInputSourceLocales:@[NSAllRomanInputSourcesLocaleIdentifier]];
  121. if (_linkType == KMAnnotationLinkType_Email) {
  122. [self setLinkType:KMAnnotationLinkType_Email];
  123. self.mouseDownBox = _linkEmailBox;
  124. } else if (_linkType == KMAnnotationLinkType_URL) {
  125. [self setLinkType:KMAnnotationLinkType_URL];
  126. self.mouseDownBox = _linkUrlBox;
  127. } else if (_linkType == KMAnnotationLinkType_Page) {
  128. [self setLinkType:KMAnnotationLinkType_Page];
  129. self.mouseDownBox = _linkPageBox;
  130. }
  131. self.targetButton.coverAction = ^(KMCoverButton * _Nonnull button, KMCoverAction action) {
  132. if (weakSelf.targetButtonState == KMAnnotationLinkStateSelected) {
  133. return;
  134. }
  135. if (action == KMCoverActionEnter) {
  136. [weakSelf updateTargetBoxState:KMAnnotationLinkStateHover];
  137. } else if (action == KMCoverActionExit) {
  138. [weakSelf updateTargetBoxState:KMAnnotationLinkStateNormal];
  139. }
  140. };
  141. [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown handler:^NSEvent * _Nullable(NSEvent * _Nonnull event) {
  142. if (event.keyCode == 53) {
  143. if ([self.inputUrlTextField isEditable]) {
  144. [self.pdfview setAnnotationType:CAnnotationTypeUnkown];
  145. }
  146. }
  147. return event;
  148. }];
  149. }
  150. - (void)viewDidAppear {
  151. [super viewDidAppear];
  152. self.inputUrlTextField.delegate = self;
  153. [_inputUrlTextField becomeFirstResponder];
  154. }
  155. - (void)setupUI {
  156. [super setupUI];
  157. NSString *fontName = @"SFProText-Regular";
  158. self.urlLabel.font = [NSFont fontWithName:fontName size:12];
  159. self.urlLabel.textColor= [NSColor colorWithRed:97.0/255.0 green:100.0/255.0 blue:105.0/255.0 alpha:1];
  160. self.inputUrlBox.borderColor = self.boxNormalBorderColor;
  161. self.inputUrlBox.fillColor = [NSColor whiteColor];
  162. self.errorLabel.textColor = self.boxErrorBorderColor;
  163. self.errorLabel.font = [NSFont fontWithName:fontName size:12];
  164. self.tagrgetBox.fillColor = [NSColor whiteColor];
  165. [self updateTargetBoxState:KMAnnotationLinkStateNormal];
  166. self.targetLabel.font = [NSFont fontWithName:fontName size:14];
  167. self.targetLabel.textColor = [NSColor colorWithRed:37/255.f green:38/255.f blue:41/255.f alpha:1.f];
  168. [self.goButton setTitle:NSLocalizedString(@"Go", nil)];
  169. [self.goButton setTitleColor:[NSColor colorWithRed:23.0/255.0 green:112.0/255.0 blue:244.0/255.0 alpha:1]];
  170. }
  171. - (void)setLinkType:(KMAnnotationLinkType)linkType {
  172. _linkType = linkType;
  173. switch (_linkType) {
  174. case KMAnnotationLinkType_Page:
  175. {
  176. [self createLinkPageView];
  177. [_inputUrlTextField becomeFirstResponder];
  178. }
  179. break;
  180. case KMAnnotationLinkType_URL:
  181. {
  182. [self createLinkURLView];
  183. [_inputUrlTextField becomeFirstResponder];
  184. }
  185. break;
  186. case KMAnnotationLinkType_Email:
  187. {
  188. [self createLinkEmailView];
  189. [_inputUrlTextField becomeFirstResponder];
  190. }
  191. break;
  192. default:
  193. break;
  194. }
  195. }
  196. - (void)setContent:(NSString *)content
  197. {
  198. if (_content) {
  199. _content = nil;
  200. }
  201. if (![content isEqualToString:@""]){
  202. if ([content length] > 0) {
  203. unsigned char typelocal = [content characterAtIndex:0];
  204. if (typelocal == '0') {
  205. _content = [content substringFromIndex:1];
  206. [self setLinkType:KMAnnotationLinkType_Page];
  207. self.inputUrlTextField.stringValue = _content;
  208. self.mouseDownBox = _linkPageBox;
  209. }else{
  210. if ([[content substringFromIndex:1] hasPrefix:@"mailto:"]) {
  211. _content = [content substringFromIndex:8];
  212. [self setLinkType:KMAnnotationLinkType_Email];
  213. self.mouseDownBox = _linkEmailBox;
  214. } else {
  215. _content = [content substringFromIndex:1];
  216. [self setLinkType:KMAnnotationLinkType_URL];
  217. self.mouseDownBox = _linkUrlBox;
  218. }
  219. }
  220. }
  221. } else {
  222. _content = [NSString stringWithFormat:@"http://"];
  223. [self setLinkType:KMAnnotationLinkType_URL];
  224. self.mouseDownBox = _linkUrlBox;
  225. }
  226. }
  227. - (void)setPdfview:(CPDFListView *)pdfview {
  228. [super setPdfview:pdfview];
  229. self.startPage = [NSString stringWithFormat:@"%ld", (self.pdfview.currentPageIndex + 1)];
  230. }
  231. - (void)setIsCreateLink:(BOOL)isCreateLink {
  232. _isCreateLink = isCreateLink;
  233. if (isCreateLink) {
  234. [self setLinkType:KMAnnotationLinkType_Page];
  235. self.mouseDownBox = _linkPageBox;
  236. }
  237. }
  238. #pragma mark - private
  239. - (void)createLinkPageView {
  240. [self annotationLinkSelectBox:_linkPageBox];
  241. _urlLabel.stringValue = NSLocalizedString(@"Page", nil);
  242. [self.inputUrlTextField setFormatter:[[TextFieldFormatter alloc] init]];
  243. if (self.pdfview.document.pageCount > 1) {
  244. // self.inputUrlTextField.placeholderString = [NSString stringWithFormat:@"1-%ld",_pageCount];
  245. self.inputUrlTextField.placeholderString = NSLocalizedString(@"Enter target page", nil);
  246. } else {
  247. self.inputUrlTextField.placeholderString = @"1";
  248. }
  249. if (_annotation.destination) {
  250. NSInteger pageIndex = self.annotation.destination.pageIndex;
  251. self.inputUrlTextField.stringValue = [NSString stringWithFormat:@"%@",@(pageIndex+1)];
  252. self.pageRecord = [NSString stringWithFormat:@"%@",@(pageIndex+1)];
  253. }
  254. if (self.inputUrlTextField.stringValue.length == 0) {
  255. self.inputUrlTextField.stringValue = self.pageRecord;
  256. }
  257. self.tagrgetBox.hidden = NO;
  258. self.pageImageThumible.hidden = YES;
  259. self.goButton.hidden = YES;
  260. self.goButtonTopComstraint.constant = 20;
  261. self.errorLabel.stringValue = NSLocalizedString(@"Page number out of range", nil);
  262. if (self.inputUrlTextField.stringValue.length == 0) {
  263. } else {
  264. NSUInteger dexPage = [self.inputUrlTextField.stringValue integerValue];
  265. [self dealThumibleImage:dexPage];
  266. }
  267. }
  268. - (void)createLinkURLView {
  269. [self annotationLinkSelectBox:_linkUrlBox];
  270. _urlLabel.stringValue = NSLocalizedString(@"URL:", nil);
  271. [self.inputUrlTextField setFormatter:nil];
  272. self.inputUrlTextField.placeholderString = @"https://www.pdfreaderpro.com";
  273. if (_annotation.URL) {
  274. NSString *urlString = self.annotation.URL;
  275. if ([urlString hasPrefix:@"http://"]) {
  276. urlString = [urlString substringFromIndex:7];
  277. self.inputUrlTextField.stringValue = urlString ? : @"";
  278. self.urlRecord = urlString ? : @"";
  279. } else if ([urlString hasPrefix:@"https://"]) {
  280. urlString = [urlString substringFromIndex:8];
  281. self.inputUrlTextField.stringValue = urlString ? : @"";
  282. self.urlRecord = urlString ? : @"";
  283. }
  284. }
  285. if (self.inputUrlTextField.stringValue.length == 0) {
  286. self.inputUrlTextField.stringValue = self.urlRecord;
  287. }
  288. self.tagrgetBox.hidden = YES;
  289. self.pageImageThumible.hidden = YES;
  290. self.goButton.hidden = YES;
  291. self.goButtonTopComstraint.constant = -390;
  292. self.errorLabel.stringValue = NSLocalizedString(@"nvalid Email. Please re-enter correct email.", nil);
  293. [self.goButton setTitle:NSLocalizedString(@"Go", nil)];
  294. [self.goButton setTitleColor:[NSColor colorWithRed:23.0/255.0 green:112.0/255.0 blue:244.0/255.0 alpha:1]];
  295. if(self.inputUrlTextField.stringValue.length > 0) {
  296. self.goButton.hidden = NO;
  297. }
  298. }
  299. - (void)createLinkEmailView {
  300. [self annotationLinkSelectBox:_linkEmailBox];
  301. _urlLabel.stringValue = NSLocalizedString(@"Email:", nil);
  302. [self.inputUrlTextField setFormatter:nil];
  303. self.inputUrlTextField.placeholderString = @"support@pdfreaderpro.com";
  304. if ([self.annotation.URL containsString:@"mailto"]) {
  305. NSString *urlString = self.annotation.URL;
  306. if ([urlString hasPrefix:@"mailto:"]) {
  307. urlString = [urlString substringFromIndex:7];
  308. self.inputUrlTextField.stringValue = urlString ? : @"";
  309. self.emailRecord = urlString ? : @"";
  310. }
  311. if (self.inputUrlTextField.stringValue.length == 0) {
  312. self.inputUrlTextField.stringValue = self.emailRecord;
  313. }
  314. }
  315. self.tagrgetBox.hidden = YES;
  316. self.pageImageThumible.hidden = YES;
  317. self.goButton.hidden = YES;
  318. self.goButtonTopComstraint.constant = -390;
  319. self.errorLabel.stringValue = NSLocalizedString(@"nvalid Email. Please re-enter correct email.", nil);
  320. [self.goButton setTitle:NSLocalizedString(@"Go", nil)];
  321. [self.goButton setTitleColor:[NSColor colorWithRed:23.0/255.0 green:112.0/255.0 blue:244.0/255.0 alpha:1]];
  322. if(self.inputUrlTextField.stringValue.length > 0) {
  323. self.goButton.hidden = NO;
  324. }
  325. }
  326. - (BOOL)isValidateEmail:(NSString *)email {
  327. NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
  328. NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegex];
  329. return [emailTest evaluateWithObject:email];
  330. }
  331. - (NSString *)judgeWebURL:(NSString *)urlString {
  332. if (![urlString hasPrefix:@"http://"] && ![urlString hasPrefix:@"https://"]) {
  333. urlString = [NSString stringWithFormat:@"https://%@",urlString];
  334. }
  335. if ([urlString isEqualToString:@"https://"]) {
  336. urlString = @"";
  337. }
  338. return urlString;
  339. }
  340. - (NSString *)judgeEmailURL:(NSString *)urlString {
  341. if (![urlString hasPrefix:@"mailto:"]) {
  342. urlString = [NSString stringWithFormat:@"mailto:%@",urlString];
  343. }
  344. if ([urlString isEqualToString:@"mailto:"]) {
  345. urlString = @"";
  346. }
  347. return urlString;
  348. }
  349. - (void)annotationLinkSelectBox:(KMBox *)box {
  350. if ([box isEqual:_linkPageBox]) {
  351. _linkPageBox.fillColor = [NSColor whiteColor];
  352. _linkEmailBox.fillColor =
  353. _linkUrlBox.fillColor = [NSColor clearColor];
  354. _linkPageImageView.image = [NSImage imageNamed:@"KMImageNamePropertybarLinkPageSel"];
  355. _linkUrlImageView.image = [NSImage imageNamed:@"KMImageNamePropertybarLinkUrlNor"];
  356. _linkEmailImageView.image = [NSImage imageNamed:@"KMImageNamePropertybarLinkEmailNor"];
  357. NSUInteger dexPage = [_pageRecord integerValue];
  358. if(_pageRecord.length < 1) return;
  359. if (dexPage < 1 || dexPage > _pageCount) {
  360. _errorLabel.hidden = NO;
  361. } else {
  362. _errorLabel.hidden = YES;
  363. }
  364. } else if ([box isEqual:_linkUrlBox]) {
  365. _linkUrlBox.fillColor = [NSColor whiteColor];
  366. _linkEmailBox.fillColor =
  367. _linkPageBox.fillColor = [NSColor clearColor];
  368. _linkPageImageView.image = [NSImage imageNamed:@"KMImageNamePropertybarLinkPageNor"];
  369. _linkUrlImageView.image = [NSImage imageNamed:@"KMImageNamePropertybarLinkUrlSel"];
  370. _linkEmailImageView.image = [NSImage imageNamed:@"KMImageNamePropertybarLinkEmailNor"];
  371. _errorLabel.hidden = YES;
  372. } else if ([box isEqual:_linkEmailBox]) {
  373. _linkEmailBox.fillColor = [NSColor whiteColor];
  374. _linkUrlBox.fillColor =
  375. _linkPageBox.fillColor = [NSColor clearColor];
  376. _linkPageImageView.image = [NSImage imageNamed:@"KMImageNamePropertybarLinkPageNor"];
  377. _linkUrlImageView.image = [NSImage imageNamed:@"KMImageNamePropertybarLinkUrlNor"];
  378. _linkEmailImageView.image = [NSImage imageNamed:@"KMImageNamePropertybarLinkEmailSel"];
  379. _errorLabel.hidden = YES;
  380. }
  381. [self updateInputUrlBoxState];
  382. }
  383. - (void)updateInputUrlBoxState {
  384. dispatch_async(dispatch_get_main_queue(), ^{
  385. if (self.errorLabel.isHidden) {
  386. self.inputUrlBox.borderColor = self.boxNormalBorderColor;
  387. } else {
  388. self.inputUrlBox.borderColor = self.boxErrorBorderColor;
  389. }
  390. });
  391. }
  392. - (void)updateTargetBoxState:(KMAnnotationLinkState)state {
  393. self.targetButtonState = state;
  394. if ([NSThread isMainThread]) {
  395. if (state == KMAnnotationLinkStateNormal) {
  396. self.tagrgetBox.borderColor = self.boxNormalBorderColor;
  397. } else if (state == KMAnnotationLinkStateHover) {
  398. self.tagrgetBox.borderColor = [NSColor colorWithRed:104/255.f green:172/255.f blue:248/255.f alpha:1.f];
  399. } else if (state == KMAnnotationLinkStateSelected) {
  400. self.tagrgetBox.borderColor = [NSColor colorWithRed:23/255.f green:112/255.f blue:244/255.f alpha:1.f];
  401. }
  402. } else {
  403. dispatch_async(dispatch_get_main_queue(), ^{
  404. if (state == KMAnnotationLinkStateNormal) {
  405. self.tagrgetBox.borderColor = self.boxNormalBorderColor;
  406. } else if (state == KMAnnotationLinkStateHover) {
  407. self.tagrgetBox.borderColor = [NSColor colorWithRed:104/255.f green:172/255.f blue:248/255.f alpha:1.f];
  408. } else if (state == KMAnnotationLinkStateSelected) {
  409. self.tagrgetBox.borderColor = [NSColor colorWithRed:23/255.f green:112/255.f blue:244/255.f alpha:1.f];
  410. }
  411. });
  412. }
  413. }
  414. - (void)dealThumibleImage:(NSUInteger)dexPage {
  415. if ((dexPage >0 || dexPage < _pageCount) && _pageRecord.length > 0) {
  416. self.pageImageThumible.hidden = NO;
  417. self.goButton.hidden = NO;
  418. NSInteger goPage = [self.inputUrlTextField.stringValue integerValue] - 1;
  419. CPDFPage *page = [self.pdfview.document pageAtIndex:goPage];
  420. self.pageImageThumible.image = [page thumbnailOfSize:[page boundsForBox:CPDFDisplayMediaBox].size];
  421. } else {
  422. self.pageImageThumible.hidden = YES;
  423. self.goButton.hidden = YES;
  424. }
  425. }
  426. #pragma mark - Action
  427. - (IBAction)goButtonAction:(NSButton *)sender {
  428. switch (_linkType) {
  429. case KMAnnotationLinkType_Page:
  430. {
  431. if(sender == self.goButton) {
  432. [(CPDFLinkAnnotation *)self.pdfview.activeAnnotation setURL:nil];
  433. NSUInteger dexPage;
  434. if (_isGo) {
  435. _isGo = NO;
  436. self.startPage = [NSString stringWithFormat:@"%ld", (self.pdfview.currentPageIndex + 1)];
  437. dexPage = [self.inputUrlTextField.stringValue integerValue];
  438. [self.goButton setTitle:NSLocalizedString(@"Go Back", nil)];
  439. [self.goButton setTitleColor:[NSColor colorWithRed:23.0/255.0 green:112.0/255.0 blue:244.0/255.0 alpha:1]];
  440. } else {
  441. _isGo = YES;
  442. dexPage = [self.startPage integerValue];
  443. [self.goButton setTitle:NSLocalizedString(@"Go", nil)];
  444. [self.goButton setTitleColor:[NSColor colorWithRed:23.0/255.0 green:112.0/255.0 blue:244.0/255.0 alpha:1]];
  445. }
  446. if (dexPage < 1 || dexPage > _pageCount) {
  447. NSAlert *alert = [[NSAlert alloc] init];
  448. [alert setAlertStyle:NSAlertStyleCritical];
  449. [alert setMessageText:NSLocalizedString(@"Invalid page range or the page number is out of range. Please try again.", nil)];
  450. [alert runModal];
  451. return;
  452. }
  453. NSInteger goPage = [self.inputUrlTextField.stringValue integerValue];
  454. CPDFDestination *destination = self.targetDestination;
  455. if (!destination) {
  456. destination = [[CPDFDestination alloc] initWithDocument:self.pdfview.document pageIndex:(goPage-1)];
  457. }
  458. [(CPDFLinkAnnotation *)self.pdfview.activeAnnotation setDestination:destination];
  459. [self.pdfview setNeedsDisplayAnnotation:_annotation];
  460. if (_isGo) {
  461. if (self.startDestination) {
  462. [self.pdfview goToDestination:self.startDestination];
  463. return;
  464. }
  465. } else {
  466. if (self.targetDestination) { // 目标定位模式
  467. [self.pdfview goToDestination:self.targetDestination];
  468. return;
  469. }
  470. }
  471. CPDFDestination *dest = [[CPDFDestination alloc] initWithDocument:self.pdfview.document pageIndex:(dexPage-1)];
  472. if (dest){
  473. [self.pdfview goToPageIndex:dest.pageIndex animated:YES];
  474. }
  475. return;
  476. }
  477. [self updateTargetBoxState:KMAnnotationLinkStateSelected];
  478. [self.pdfview setIsSetLinkDestinationArea:YES];
  479. }
  480. break;
  481. case KMAnnotationLinkType_URL:
  482. {
  483. [(CPDFLinkAnnotation *)self.pdfview.activeAnnotation setDestination:nil];
  484. NSString *linkUrlPath = [self judgeWebURL:self.inputUrlTextField.stringValue];
  485. [(CPDFLinkAnnotation *)self.pdfview.activeAnnotation setURL:linkUrlPath];
  486. [self.pdfview setNeedsDisplayAnnotation:_annotation];
  487. NSURL *url = [NSURL URLWithString:[(CPDFLinkAnnotation *)self.pdfview.activeAnnotation URL]];
  488. [[NSWorkspace sharedWorkspace] openURL:url];
  489. }
  490. break;
  491. case KMAnnotationLinkType_Email:
  492. {
  493. [(CPDFLinkAnnotation *)self.pdfview.activeAnnotation setDestination:nil];
  494. if (![self isValidateEmail:self.inputUrlTextField.stringValue]) {
  495. NSAlert *alert = [[NSAlert alloc] init];
  496. [alert setAlertStyle:NSAlertStyleCritical];
  497. [alert setMessageText:NSLocalizedString(@"Invalid email address. Please enter a valid email address.", nil)];
  498. [alert runModal];
  499. return;
  500. }
  501. NSString *linkUrlPath = [self judgeEmailURL:self.inputUrlTextField.stringValue];
  502. [(CPDFLinkAnnotation *)self.pdfview.activeAnnotation setURL:linkUrlPath];
  503. [self.pdfview setNeedsDisplayAnnotation:_annotation];
  504. NSURL *url = [NSURL URLWithString:[(CPDFLinkAnnotation *)self.pdfview.activeAnnotation URL]];
  505. [[NSWorkspace sharedWorkspace] openURL:url];
  506. }
  507. break;
  508. default:
  509. break;
  510. }
  511. }
  512. #pragma mark - NSTextFieldDelegate Methods
  513. - (void)controlTextDidChange:(NSNotification *)obj {
  514. NSTextField *textField = (NSTextField *)obj.object;
  515. _errorLabel.hidden = YES;
  516. if ([_mouseDownBox isEqual:_linkPageBox]) {
  517. _pageRecord = textField.stringValue;
  518. } else if ([_mouseDownBox isEqual:_linkUrlBox]) {
  519. _urlRecord = textField.stringValue;
  520. } else if ([_mouseDownBox isEqual:_linkEmailBox]) {
  521. _emailRecord = textField.stringValue;
  522. }
  523. if (_linkType == KMAnnotationLinkType_Page) {
  524. [(CPDFLinkAnnotation *)self.pdfview.activeAnnotation setURL:nil];
  525. NSUInteger dexPage = [self.inputUrlTextField.stringValue integerValue];
  526. if ((dexPage < 1 || dexPage > _pageCount) && _pageRecord.length > 0) {
  527. [(CPDFLinkAnnotation *)self.pdfview.activeAnnotation setDestination:nil];
  528. _errorLabel.hidden = NO;
  529. [self updateInputUrlBoxState];
  530. return;
  531. } else if(_pageRecord.length > 0){
  532. _errorLabel.hidden = YES;
  533. }
  534. CPDFPage *page = [self.pdfview.document pageAtIndex:(dexPage-1)];
  535. CPDFDestination *destination = [[CPDFDestination alloc] initWithDocument:self.pdfview.document pageIndex:(dexPage-1) atPoint:CGPointMake(0, page.bounds.size.height) zoom:self.pdfview.scaleFactor];
  536. [(CPDFLinkAnnotation *)self.pdfview.activeAnnotation setDestination:destination];
  537. [self.pdfview setNeedsDisplayAnnotationViewForPage:self.pdfview.activeAnnotation.page];
  538. ///显示预览图
  539. [self dealThumibleImage:dexPage];
  540. [self updateInputUrlBoxState];
  541. } else if (_linkType == KMAnnotationLinkType_URL) {
  542. [(CPDFLinkAnnotation *)self.pdfview.activeAnnotation setDestination:nil];
  543. // NSString *linkUrlPath = [self judgeWebURL:_urlRecord];
  544. NSString *linkUrlPath = _urlRecord;
  545. if ([linkUrlPath isEqualToString:@""]) {
  546. [(CPDFLinkAnnotation *)self.pdfview.activeAnnotation setURL:nil];
  547. } else {
  548. [(CPDFLinkAnnotation *)self.pdfview.activeAnnotation setURL:linkUrlPath];
  549. }
  550. [self.pdfview setNeedsDisplayAnnotationViewForPage:self.pdfview.activeAnnotation.page];
  551. } else if (_linkType == KMAnnotationLinkType_Email) {
  552. [(CPDFLinkAnnotation *)self.pdfview.activeAnnotation setDestination:nil];
  553. NSString *linkUrlPath = [self judgeEmailURL:_emailRecord];
  554. if ([linkUrlPath isEqualToString:@""]) {
  555. [(CPDFLinkAnnotation *)self.pdfview.activeAnnotation setURL:nil];
  556. } else {
  557. [(CPDFLinkAnnotation *)self.pdfview.activeAnnotation setURL:linkUrlPath];
  558. }
  559. [self.pdfview setNeedsDisplayAnnotationViewForPage:self.pdfview.activeAnnotation.page];
  560. }
  561. }
  562. - (BOOL)control:(NSControl *)control textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector {
  563. if (commandSelector == @selector(insertNewline:)) {
  564. [self.view.window makeFirstResponder:self.pdfview];
  565. return YES;
  566. }
  567. return NO;
  568. }
  569. - (void)mouseDown:(NSEvent *)event {
  570. [super mouseDown:event];
  571. [self.view.window makeFirstResponder:self.pdfview];
  572. }
  573. //- (void)controlTextDidEndEditing:(NSNotification *)obj {
  574. // switch (_linkType) {
  575. // case KMAnnotationLinkType_Page: {
  576. // NSUInteger dexPage = [self.inputUrlTextField.stringValue integerValue];
  577. // if ((dexPage <0 || dexPage > _pageCount) && _pageRecord.length > 0) {
  578. // self.errorLabel.hidden = NO;
  579. // } else {
  580. // self.errorLabel.hidden = YES;
  581. // NSInteger goPage = [self.inputUrlTextField.stringValue integerValue];
  582. // CPDFDestination *destination = [[CPDFDestination alloc] initWithDocument:_pdfview.document pageIndex:(goPage-1)];
  583. //
  584. // [(CPDFLinkAnnotation *)_pdfview.activeAnnotation setDestination:destination];
  585. // [_pdfview setNeedsDisplayAnnotationViewForPage:_pdfview.activeAnnotation.page];
  586. // }
  587. // self.goButton.hidden = YES;
  588. // }
  589. // break;
  590. // case KMAnnotationLinkType_URL:
  591. // {
  592. // if (self.urlRecord.length > 0) {
  593. // [(CPDFLinkAnnotation *)_pdfview.activeAnnotation setDestination:nil];
  594. //
  595. // NSString *linkUrlPath = [self judgeWebURL:self.urlRecord];
  596. //
  597. // [(CPDFLinkAnnotation *)_pdfview.activeAnnotation setURL:linkUrlPath];
  598. // [_pdfview setNeedsDisplayAnnotationViewForPage:_pdfview.activeAnnotation.page];
  599. // self.goButton.hidden = NO;
  600. // } else {
  601. // self.goButton.hidden = YES;
  602. // }
  603. // }
  604. // break;
  605. // case KMAnnotationLinkType_Email:
  606. // {
  607. // if (self.emailRecord.length == 0) return;
  608. // if (![self isValidateEmail:self.emailRecord]) {
  609. // self.errorLabel.hidden = NO;
  610. // self.goButton.hidden = YES;
  611. // return;
  612. // }
  613. // self.errorLabel.hidden = YES;
  614. // self.goButton.hidden = NO;
  615. // [(CPDFLinkAnnotation *)_pdfview.activeAnnotation setDestination:nil];
  616. // NSString *linkUrlPath = [self judgeEmailURL:self.emailRecord];
  617. // [(CPDFLinkAnnotation *)_pdfview.activeAnnotation setURL:linkUrlPath];
  618. // [_pdfview setNeedsDisplayAnnotationViewForPage:_pdfview.activeAnnotation.page];
  619. // }
  620. // break;
  621. // default:
  622. // break;
  623. // }
  624. //}
  625. @end