Skip to content

Commit

Permalink
fix(ios): properly handle Ti.UI.Clipboard.setData with Files
Browse files Browse the repository at this point in the history
  • Loading branch information
sgtcoolguy committed Apr 8, 2021
1 parent 2185a8f commit 485f5cf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
2 changes: 1 addition & 1 deletion iphone/Classes/TiUIClipboardProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ READONLY_PROPERTY(bool, unique, Unique);
JSExportAs(setData,
-(void)setData
: (NSString *)type withData
: (id)data);
: (JSValue *)data);
- (void)setText:(NSString *)text;
- (void)setItems:(NSDictionary<NSString *, id> *)items;
- (void)remove;
Expand Down
30 changes: 13 additions & 17 deletions iphone/Classes/TiUIClipboardProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -361,25 +361,15 @@ - (void)setItems:(NSDictionary<NSString *, id> *)args
return [result autorelease];
}

- (void)setData:(NSString *)mimeType withData:(id)data
- (void)setData:(NSString *)mimeType withData:(JSValue *)data
{
if (data == nil) {
if (data == nil) { // what about undefined?
DebugLog(@"[WARN] setData: data object was nil.");
return;
}
// FIXME: data doesn't get converted properly if it's a TiFile, because trhat's not one of the "new" proxies
// Can we convert id to JSValue* in the signature and handle it?\
// Or do we need ot keep this ugly code?
if ([data isKindOfClass:[NSDictionary class]]) {
NSDictionary *dict = (NSDictionary *)data;
if (dict.count == 0) {
id whatever = JSContext.currentArguments[1];
id converted = [self JSValueToNative:whatever];
if ([converted isKindOfClass:[TiFile class]]) {
data = converted;
}
}
}

data = [self JSValueToNative:data];
// TODO: If value is NSNull or undefined, should we throw or just clear the data for the given mime type?

UIPasteboard *board = [self pasteboard];
ClipboardType dataType = mimeTypeToDataType(mimeType);
Expand All @@ -393,7 +383,12 @@ - (void)setData:(NSString *)mimeType withData:(id)data
break;
}
case CLIPBOARD_IMAGE: {
board.image = [TiUtils toImage:data proxy:self];
UIImage *image = [TiUtils toImage:data proxy:self];
if (image) {
board.image = image;
} else {
board.image = nil;
}
break;
}
case CLIPBOARD_COLOR: {
Expand All @@ -418,7 +413,8 @@ - (void)setData:(NSString *)mimeType withData:(id)data

- (void)setText:(NSString *)text
{
[self setData:@"text/plain" withData:text];
UIPasteboard *board = [self pasteboard];
board.string = text;
}

@end
Expand Down

0 comments on commit 485f5cf

Please sign in to comment.