Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally auto compile and run code on injection #3002

Merged
merged 2 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading