public
Created — forked from /file_chooser_app.dart

  • Download Gist
file_chooser_app.dart
Dart
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
import 'dart:async';
import 'dart:chrome';
import 'dart:html';
import 'package:js/js.dart' as js;
 
void main() {
var open = new ButtonElement()..text = 'Open';
document.body.children.add(open);
write(s) => document.body.children.add(new DivElement()..text = s);
open.onClick.listen((_) {
FileEntry.choose()
.then((entry) {
entry.displayPath.then(write);
// Not sure why this isn't working?
var reader = new js.Proxy(js.context.FileReader);
reader.onloadend = new js.Callback.once((_) {
write(reader.result);
});
reader.readAsText(entry);
});
});
}
 
class ChooseEntryType {
const ChooseEntryType(this._value);
final String _value;
toString() => _value;
}
 
const OPEN_FILE = const ChooseEntryType("openFile");
const OPEN_FILE_WRITABLE = const ChooseEntryType("openWritableFile");
const SAVE_FILE = const ChooseEntryType("saveFile");
 
class AcceptOption {
AcceptOption(this.description, this.mimeTypes, this.extensions);
final String description;
final List<String> mimeTypes;
final List<String> extensions;
}
 
class FileEntry {
final _proxy;
FileEntry._internal(this._proxy);
void release() {
js.release(_proxy);
}
static Future<FileEntry> choose(
{ChooseEntryType type: OPEN_FILE,
String suggestedName : '',
List<AcceptOption> accepts : const [],
bool acceptsAllTypes: true}) {
var cpl = new Completer<String>();
js.scoped(() {
var options = {'type': type.toString(),
'suggestedName': suggestedName,
'accepts': js.array(accepts), //TODO
'acceptsAllTypes': acceptsAllTypes};
js.context.chrome.fileSystem.chooseEntry(js.map(options),
new js.Callback.once((fileEntry) {
cpl.complete(new FileEntry._internal(fileEntry));
}));
});
return cpl.future;
}
static FileEntry getById(String id) {
var fileEntryProxy;
js.scoped(() {
fileEntryProxy = js.context.chrome.fileSystem.getEntryById(id);
js.retain(fileEntryProxy);
});
return new FileEntry._internal(fileEntryProxy);
}
String get id {
var entryId;
js.scoped(() {
entryId = js.context.chrome.fileSystem.getEntryId(_proxy);
});
return entryId;
}
Future<String> get displayPath {
var cpl = new Completer<String>();
js.scoped(() {
js.context.chrome.fileSystem.getDisplayPath(_proxy,
new js.Callback.once((path) {
cpl.complete(path);
}));
});
return cpl.future;
}
Future<FileEntry> makeWritable() {
throw new UnimplementedError();
}
Future<bool> get isWritable {
throw new UnimplementedError();
}
}
 
main2() {
var paste = new ButtonElement()..text = 'paste';
var copy = new ButtonElement()..text = 'copy';
var textarea = new TextAreaElement();
document.body.children.addAll([paste, copy, textarea]);
paste.onClick.listen((_) => textarea.value = Clipboard.text);
copy.onClick.listen((_) => Clipboard.text = 'oi!' );
}
 
class Clipboard {
static String get text {
var active = document.activeElement;
var hidden = new TextAreaElement();
document.body.append(hidden);
hidden.focus();
document.execCommand('paste', null, '');
active.focus();
hidden.remove();
return hidden.value;
}
static set text(String s) {
var active = document.activeElement;
var hidden = new TextAreaElement();
hidden.value = s;
document.body.append(hidden);
hidden.select();
document.execCommand('copy', null, '');
active.focus();
hidden.remove();
}
}

Greg, I think I see the problem here:

reader.readAsText(entry);

readAsText takes a File, not a FileEntry. I'm going to start hacking on a solution, which I think is doable, but not necessarily trivial. Here's what it needs to do (in pure js):

entry.file(function(file) {
      var reader = new FileReader();

      reader.onerror = errorHandler;
      reader.onloadend = function(e) {
        console.log(e.target.result);
      };

      reader.readAsText(file);
    });

Take a look at what I did below for class _Window implements WindowBase, I think we need to take a similar approach for File, FileEntry, etc... I'm going to take a stab at doing this, and just throw UnimplementedError for methods I don't immediately care about. If it works, I'll throw it up on Github.

https://github.com/rmsmith/webview/blob/master/lib/webview.dart

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.