-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathspotlight.mm
115 lines (92 loc) · 3.38 KB
/
spotlight.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#import <AppKit/AppKit.h>
#import <CoreSpotlight/CoreSpotlight.h>
#import <Foundation/Foundation.h>
#include <nan.h>
#include "spotlight.h"
using namespace v8;
NAN_METHOD(AddItems) {
if (info.Length() < 1) {
Nan::ThrowTypeError("Missing arguments");
return;
}
if (!info[0]->IsArray()) {
Nan::ThrowTypeError("Items must be an array");
return;
}
Local<Array> inputItems = Local<Array>::Cast(info[0]);
NSMutableArray *items = [[NSMutableArray alloc] init];
for (unsigned int i = 0; i < inputItems->Length(); i++ ) {
if (!Nan::Has(inputItems, i).FromJust()) continue;
Local<Object> inputItem = Nan::Get(inputItems, i)
.ToLocalChecked()
.As<Object>();
Nan::Utf8String idString(
Nan::Get(inputItem, Nan::New("id").ToLocalChecked()
).ToLocalChecked());
Nan::Utf8String titleString(
Nan::Get(inputItem, Nan::New("title").ToLocalChecked()
).ToLocalChecked());
NSString* identifier = [NSString stringWithUTF8String:*idString];
NSString* title = [NSString stringWithUTF8String:*titleString];
CSSearchableItemAttributeSet *attributeSet = [[CSSearchableItemAttributeSet alloc]
initWithItemContentType:(NSString *)kUTTypeData];
attributeSet.title = title;
MaybeLocal<Value> iconHandle = Nan::Get(inputItem, Nan::New("icon").ToLocalChecked());
if (!iconHandle.IsEmpty()
&& !Nan::Equals(iconHandle.ToLocalChecked(), Nan::Undefined()).FromJust()) {
Nan::Utf8String iconURLString(iconHandle.ToLocalChecked());
NSString* iconURL = [NSString stringWithUTF8String:*iconURLString];
NSURL* theURL = [[NSURL alloc] initWithString:iconURL];
attributeSet.thumbnailData = [[NSData alloc] initWithContentsOfURL:theURL];
}
CSSearchableItem *item = [[CSSearchableItem alloc]
initWithUniqueIdentifier:identifier
domainIdentifier:identifier
attributeSet:attributeSet];
[items addObject:item];
}
[[CSSearchableIndex defaultSearchableIndex] indexSearchableItems:items
completionHandler: ^(NSError * __nullable error) {
if (!error) {
NSLog(@"Search items indexed");
} else {
NSLog(@"Indexing items failed: %@", error);
}
}];
info.GetReturnValue().Set(Nan::Undefined());
}
NAN_METHOD(RemoveItems) {
if (info.Length() < 1) {
Nan::ThrowTypeError("Missing arguments");
return;
}
if (!info[0]->IsArray()) {
Nan::ThrowTypeError("Items must be an array");
return;
}
Local<Array> inputIds = Local<Array>::Cast(info[0]);
NSMutableArray *ids = [[NSMutableArray alloc] init];
for (unsigned int i = 0; i < inputIds->Length(); i++ ) {
if (!Nan::Has(inputIds, i).FromJust()) continue;
Nan::Utf8String identifier(Nan::Get(inputIds, i).ToLocalChecked());
NSString* nsIdentifier = [NSString stringWithUTF8String:*identifier];
[ids addObject:nsIdentifier];
}
[[CSSearchableIndex defaultSearchableIndex] deleteSearchableItemsWithIdentifiers:ids
completionHandler: ^(NSError * __nullable error) {
if (!error) {
NSLog(@"Removed items successfully");
} else {
NSLog(@"Removing items failed: %@", error);
}
}];
}
NAN_METHOD(RemoveAllItems) {
[[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:^(NSError * __nullable error) {
if (!error) {
NSLog(@"Removed all items");
} else {
NSLog(@"Removing items failed: %@", error);
}
}];
}