KMAnnotationLinkViewController.m 28 KB

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