KMAnnotationLinkViewController.m 30 KB

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