Skip to content

Commit

Permalink
perf: open url when query easydict url scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed Dec 7, 2023
1 parent 403c7da commit d1da868
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Easydict/App/AppDelegate+EZURLScheme.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ - (void)handleURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEv
NSURL *URL = [NSURL URLWithString:[[event paramDescriptorForKeyword:keyDirectObject] stringValue]];

// easydict://query?text=good
if ([URL.scheme isEqualToString:EZEasydictScheme]) {
if ([URL.scheme containsString:EZEasydictScheme]) {
NSLog(@"handle URL: %@", URL);
}

Expand Down
1 change: 1 addition & 0 deletions Easydict/Feature/Utility/EZLinkParser/EZSchemeParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ NS_ASSUME_NONNULL_BEGIN

/// Easydict Scheme: easydict://
static NSString *const EZEasydictScheme = @"easydict";
static NSString *const EZEasydictDebugScheme = @"easydictd";

static NSString *const EZWriteKeyValueKey = @"writeKeyValue";
static NSString *const EZReadValueOfKeyKey = @"readValueOfKey";
Expand Down
11 changes: 9 additions & 2 deletions Easydict/Feature/Utility/EZLinkParser/EZSchemeParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ - (void)openURLScheme:(NSString *)URLScheme completion:(void (^)(BOOL isSuccess,
NSArray *allowedActions = actionDict.allKeys;

if (![allowedActions containsObject:action]) {
completion(NO, @"Invalid Easydict Action", nil);
if ([action isEqualToString:EZQueryKey]) {
[NSWorkspace.sharedWorkspace openURL:[NSURL URLWithString:URLScheme]];
completion(YES, nil, nil);
} else {
completion(NO, @"Invalid Easydict Action", nil);
}

return;
}

Expand Down Expand Up @@ -72,7 +78,8 @@ - (BOOL)isEasydictScheme:(NSString *)text {
NSString *urlString = [text trim];
NSURLComponents *urlComponents = [NSURLComponents componentsWithString:urlString];
NSString *scheme = urlComponents.scheme;
return [scheme isEqualToString:EZEasydictScheme];
NSArray *schemes = @[EZEasydictScheme, EZEasydictDebugScheme];
return [schemes containsObject:scheme];
}

- (BOOL)isWriteActionKey:(NSString *)actionKey {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ - (BOOL)handleEasydictScheme:(NSString *)text {
[self clearInput];

// If write, need to update.
if ([self.schemeParser isWriteActionKey:actionKey]) {
if (actionKey && [self.schemeParser isWriteActionKey:actionKey]) {
// Besides current window, other pages need to be notified, such as the settings service page.
[self postUpdateServiceNotification];
}
Expand Down

0 comments on commit d1da868

Please sign in to comment.