Chromium Code Reviews| Index: dart/site/try/src/interaction_manager.dart |
| diff --git a/dart/site/try/src/interaction_manager.dart b/dart/site/try/src/interaction_manager.dart |
| index 0f0a18c7da5b36c0a3c74d9f8818065703fb1035..8bb2f05b079544dc7865308961c6a5c579222411 100644 |
| --- a/dart/site/try/src/interaction_manager.dart |
| +++ b/dart/site/try/src/interaction_manager.dart |
| @@ -13,6 +13,9 @@ import 'dart:math' show |
| max, |
| min; |
| +import 'dart:async' show |
| + Future; |
| + |
| import 'package:compiler/implementation/scanner/scannerlib.dart' |
| show |
| EOF_TOKEN, |
| @@ -42,6 +45,10 @@ import 'decoration.dart' show |
| import 'html_to_text.dart' show |
| htmlToText; |
| + |
| +import 'compilation_unit.dart' show |
| + CompilationUnit; |
| + |
| import 'editor.dart' as editor; |
| import 'mock.dart' as mock; |
| @@ -77,6 +84,14 @@ abstract class InteractionManager { |
| void onMutation(List<MutationRecord> mutations, MutationObserver observer); |
| void onSelectionChange(Event event); |
| + |
| + /// Called when the content of a CompilationUnit changed. |
| + void onCompilationUnitChanged(CompilationUnit unit); |
| + |
| + Future<List<String>> projectFileNames(); |
| + |
| + /// Called when the user selected a new project file. |
| + void onProjectFileSelected(String projectFile); |
| } |
| /** |
| @@ -85,6 +100,10 @@ abstract class InteractionManager { |
| class InteractionContext extends InteractionManager { |
| InteractionState state; |
| + final Map<String, CompilationUnit> projectFiles = <String, CompilationUnit>{}; |
| + |
| + CompilationUnit currentCompilationUnit = new CompilationUnit('fake', ''); |
|
kasperl
2014/03/28 10:24:51
Maybe chose a fake name that isn't at all likely (
ahe
2014/03/28 11:53:14
This is a temporary hack. I'll add a todo.
|
| + |
| InteractionContext() |
| : super.internal() { |
| state = new InitialState(this); |
| @@ -99,6 +118,16 @@ class InteractionContext extends InteractionManager { |
| } |
| void onSelectionChange(Event event) => state.onSelectionChange(event); |
| + |
| + void onCompilationUnitChanged(CompilationUnit unit) { |
| + return state.onCompilationUnitChanged(unit); |
| + } |
| + |
| + Future<List<String>> projectFileNames() => state.projectFileNames(); |
| + |
| + void onProjectFileSelected(String projectFile) { |
| + return state.onProjectFileSelected(projectFile); |
| + } |
| } |
| abstract class InteractionState implements InteractionManager { |
| @@ -233,9 +262,10 @@ class InitialState extends InteractionState { |
| walk4(mainEditorPane); |
| } |
| - editor.currentSource = mainEditorPane.text; |
| + String currentText = mainEditorPane.text; |
| + context.currentCompilationUnit.content = currentText; |
| mainEditorPane.nodes.clear(); |
| - mainEditorPane.appendText(editor.currentSource); |
| + mainEditorPane.appendText(currentText); |
| if (hasSelection) { |
| selection.collapse(mainEditorPane.firstChild, anchorOffset); |
| } |
| @@ -280,9 +310,6 @@ class InitialState extends InteractionState { |
| } |
| } |
| - window.localStorage['currentSource'] = editor.currentSource; |
| - print('Saved source'); |
| - |
| // Discard highlighting mutations. |
| observer.takeRecords(); |
| } |
| @@ -294,6 +321,58 @@ class InitialState extends InteractionState { |
| super.onStateChanged(previous); |
| scheduleCompilation(); |
| } |
| + |
| + void onCompilationUnitChanged(CompilationUnit unit) { |
| + if (unit == context.currentCompilationUnit) { |
| + window.localStorage['currentSource'] = unit.content; |
|
kasperl
2014/03/28 10:24:51
Add a setter for currentSource next to the getter?
ahe
2014/03/28 11:53:14
Also a temporary hack, but encapsulating the hacks
|
| + print('Saved source'); |
| + scheduleCompilation(); |
| + } else { |
| + print("Unexpected change to compilation unit '${unit.name}'."); |
| + } |
| + } |
| + |
| + Future<List<String>> projectFileNames() { |
| + return getString('project?list').then((String response) { |
| + return new List<String>.from(JSON.decode(response)); |
| + }); |
| + } |
| + |
| + void onProjectFileSelected(String projectFile) { |
| + // Disable editing whilst fetching data. |
| + mainEditorPane.contentEditable = 'false'; |
| + |
| + CompilationUnit unit = context.projectFiles[projectFile]; |
| + Future<CompilationUnit> future; |
| + if (unit != null) { |
| + // This project file had been fetched already. |
| + future = new Future<CompilationUnit>.value(unit); |
| + } else { |
| + // This project file has to be fetched. |
| + future = getString('project/$projectFile').then((String text) { |
| + CompilationUnit unit = context.projectFiles[projectFile]; |
| + if (unit == null) { |
| + // Only create a new unit if the value hadn't arrived already. |
| + unit = new CompilationUnit(projectFile, text); |
| + context.projectFiles[projectFile] = unit; |
| + } |
| + return unit; |
| + }); |
| + } |
| + future.then((CompilationUnit unit) { |
| + mainEditorPane |
| + ..contentEditable = 'true' |
| + ..nodes.clear(); |
| + observer.takeRecords(); // Discard mutations. |
| + |
| + // Install the code, which will trigger a call to onMutation. |
| + mainEditorPane.appendText(unit.content); |
| + }); |
| + } |
| +} |
| + |
| +Future<String> getString(uri) { |
| + return new Future<String>.sync(() => HttpRequest.getString('$uri')); |
| } |
| class PendingInputState extends InitialState { |