KMAnnotationLinkViewController.m 30 KB

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