Skip to content

Instantly share code, notes, and snippets.

@xxgreg
Forked from anonymous/file_chooser_app.dart
Created April 21, 2013 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xxgreg/5428315 to your computer and use it in GitHub Desktop.
Save xxgreg/5428315 to your computer and use it in GitHub Desktop.
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();
}
}
Copy link

ghost commented May 18, 2013

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment