Skip to content

Commit

Permalink
Optionally auto compile and run code on injection (#3002)
Browse files Browse the repository at this point in the history
* Optionally auto compile and run code on injection

* Fix lints
  • Loading branch information
parlough committed Jun 13, 2024
1 parent fe692e9 commit d0d5581
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 40 deletions.
7 changes: 5 additions & 2 deletions pkgs/dartpad_ui/lib/embed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'model.dart';

/// Listen to frame messages if embedded as an iFrame
/// to accept injected snippets.
void handleEmbedMessage(AppModel model) {
void handleEmbedMessage(AppServices services, {bool runOnInject = false}) {
final parent = web.window.parent;
if (parent == null) return;

Expand All @@ -16,7 +16,10 @@ void handleEmbedMessage(AppModel model) {
if (event.data case _SourceCodeMessage(:final type?, :final sourceCode?)
when type == 'sourceCode') {
if (sourceCode.isNotEmpty) {
model.sourceCodeController.text = sourceCode;
services.appModel.sourceCodeController.text = sourceCode;
if (runOnInject) {
services.performCompileAndRun();
}
}
}
}.toJS,
Expand Down
45 changes: 9 additions & 36 deletions pkgs/dartpad_ui/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ class _DartPadMainPageState extends State<DartPadMainPage>
fallbackSnippet: Samples.getDefault(type: 'dart'))
.then((value) {
// Start listening for inject code messages.
handleEmbedMessage(appModel);
handleEmbedMessage(appServices, runOnInject: widget.runOnLoad);
if (widget.runOnLoad) {
_performCompileAndRun();
appServices.performCompileAndRun();
}
});

Expand Down Expand Up @@ -320,7 +320,7 @@ class _DartPadMainPageState extends State<DartPadMainPage>
appModel: appModel,
appServices: appServices,
onFormat: _handleFormatting,
onCompileAndRun: _performCompileAndRun,
onCompileAndRun: appServices.performCompileAndRun,
key: _editorKey,
);

Expand Down Expand Up @@ -465,10 +465,14 @@ class _DartPadMainPageState extends State<DartPadMainPage>
child: CallbackShortcuts(
bindings: <ShortcutActivator, VoidCallback>{
keys.runKeyActivator1: () {
if (!appModel.compilingBusy.value) _performCompileAndRun();
if (!appModel.compilingBusy.value) {
appServices.performCompileAndRun();
}
},
keys.runKeyActivator2: () {
if (!appModel.compilingBusy.value) _performCompileAndRun();
if (!appModel.compilingBusy.value) {
appServices.performCompileAndRun();
}
},
// keys.findKeyActivator: () {
// // TODO:
Expand Down Expand Up @@ -529,37 +533,6 @@ class _DartPadMainPageState extends State<DartPadMainPage>
}
}

Future<void> _performCompileAndRun() async {
final source = appModel.sourceCodeController.text;
final progress =
appModel.editorStatus.showMessage(initialText: 'Compiling…');

try {
final response =
await appServices.compileDDC(CompileRequest(source: source));
appModel.clearConsole();
appServices.executeJavaScript(
response.result,
modulesBaseUrl: response.modulesBaseUrl,
engineVersion: appModel.runtimeVersions.value?.engineVersion,
dartSource: source,
);
} catch (error) {
appModel.clearConsole();

appModel.editorStatus.showToast('Compilation failed');

if (error is ApiRequestError) {
appModel.appendLineToConsole(error.message);
appModel.appendLineToConsole(error.body);
} else {
appModel.appendLineToConsole('$error');
}
} finally {
progress.close();
}
}

void _handleRunStarted() {
setState(() {
// Switch to the application output tab.]
Expand Down
34 changes: 32 additions & 2 deletions pkgs/dartpad_ui/lib/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,36 @@ class AppServices {
appModel.appReady.value = true;
}

Future<void> performCompileAndRun() async {
final source = appModel.sourceCodeController.text;
final progress =
appModel.editorStatus.showMessage(initialText: 'Compiling…');

try {
final response = await _compileDDC(CompileRequest(source: source));
appModel.clearConsole();
_executeJavaScript(
response.result,
modulesBaseUrl: response.modulesBaseUrl,
engineVersion: appModel.runtimeVersions.value?.engineVersion,
dartSource: source,
);
} catch (error) {
appModel.clearConsole();

appModel.editorStatus.showToast('Compilation failed');

if (error is ApiRequestError) {
appModel.appendLineToConsole(error.message);
appModel.appendLineToConsole(error.body);
} else {
appModel.appendLineToConsole('$error');
}
} finally {
progress.close();
}
}

Future<FormatResponse> format(SourceRequest request) async {
try {
appModel.formattingBusy.value = true;
Expand All @@ -330,7 +360,7 @@ class AppServices {
}
}

Future<CompileDDCResponse> compileDDC(CompileRequest request) async {
Future<CompileDDCResponse> _compileDDC(CompileRequest request) async {
try {
appModel.compilingBusy.value = true;
return await services.compileDDC(request);
Expand Down Expand Up @@ -358,7 +388,7 @@ class AppServices {
_editorService = editorService;
}

void executeJavaScript(
void _executeJavaScript(
String javaScript, {
required String dartSource,
String? modulesBaseUrl,
Expand Down

0 comments on commit d0d5581

Please sign in to comment.