KMAnnotationLinkViewController.m 30 KB

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