Skip to content

Commit e84118c

Browse files
committed
First pass at hiding JS dependencies from implementation
Create script tags dynamically inside the constructor, pulling the ACE JavaScript libraries from the relative package path. Don't create an instance of the ACE editor until the dynamically created script has loaded. Add a guard in the content setter to prevent calling methods on a JS proxy that might not be up yet. Huge thanks to @santiaago for pairing on this.
1 parent 8eff287 commit e84118c

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

example/public/index.html

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<head>
2-
<script src="packages/ice_code_editor/ace/ace.js" type="text/javascript" charset="utf-8"></script>
32
<script src="packages/browser/dart.js"></script>
43
<script src="main.dart" type="application/dart"></script>
5-
64
</head>
75
<h1>Hello</h1>
8-
<div style="width:600px; height: 400px" id="ace"></div>
6+
<div style="width:600px; height: 400px" id="ice"></div>

example/public/main.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import 'package:ice_code_editor/editor.dart' as ICE;
22

33
main() {
4-
var ice = new ICE.Editor('ace');
4+
var ice = new ICE.Editor('ice');
5+
56
ice.content = '''
67
main() {
78
print("Dart rocks");

lib/editor.dart

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
11
library ice;
22

33
import 'dart:html';
4+
import 'dart:async';
45
import 'package:js/js.dart' as js;
56

67
class Editor {
78
bool edit_only, autoupdate;
89
String title;
910
var _ace;
1011

11-
Editor(el, {this.edit_only:false, this.autoupdate:true, this.title}) {
12-
_ace = js.context.ace.edit(el);
12+
Editor(id, {this.edit_only:false, this.autoupdate:true, this.title}) {
13+
var script = new ScriptElement();
14+
script.src = "packages/ice_code_editor/ace/ace.js";
15+
document.head.nodes.add(script);
16+
17+
script.onLoad.listen((event) {
18+
this._ace = js.context.ace.edit(id);
19+
js.retain(this._ace);
20+
});
1321
}
1422

1523
set content(String data) {
16-
_ace.setValue(data, -1);
24+
if (this._ace == null) {
25+
// TODO: completer?
26+
// this.waitForAce.then(updateAceValue);
27+
var wait = new Duration(milliseconds: 50);
28+
new Timer(wait, (){ this.content = data;});
29+
return;
30+
}
31+
32+
this._ace.setValue(data, -1);
33+
this._ace.focus();
1734
}
1835

1936
String get content => _ace.getValue();

test/index.html

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<html>
22
<head>
33
<title>ICE Test Suite</title>
4-
<script src="packages/ice_code_editor/ace/ace.js" type="text/javascript" charset="utf-8"></script>
54
<script type="application/dart" src="editor_test.dart"></script>
65
<script src="packages/browser/dart.js"></script>
76
</head>

0 commit comments

Comments
 (0)