From d0d5581c8ae112732b89552e1dc82a0e66ad9e09 Mon Sep 17 00:00:00 2001 From: Parker Lougheed Date: Thu, 13 Jun 2024 10:47:13 -0500 Subject: [PATCH] Optionally auto compile and run code on injection (#3002) * Optionally auto compile and run code on injection * Fix lints --- pkgs/dartpad_ui/lib/embed.dart | 7 ++++-- pkgs/dartpad_ui/lib/main.dart | 45 +++++++--------------------------- pkgs/dartpad_ui/lib/model.dart | 34 +++++++++++++++++++++++-- 3 files changed, 46 insertions(+), 40 deletions(-) diff --git a/pkgs/dartpad_ui/lib/embed.dart b/pkgs/dartpad_ui/lib/embed.dart index 1617c25a0..61d1b1ca8 100644 --- a/pkgs/dartpad_ui/lib/embed.dart +++ b/pkgs/dartpad_ui/lib/embed.dart @@ -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; @@ -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, diff --git a/pkgs/dartpad_ui/lib/main.dart b/pkgs/dartpad_ui/lib/main.dart index d554eab39..810459664 100644 --- a/pkgs/dartpad_ui/lib/main.dart +++ b/pkgs/dartpad_ui/lib/main.dart @@ -280,9 +280,9 @@ class _DartPadMainPageState extends State 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(); } }); @@ -320,7 +320,7 @@ class _DartPadMainPageState extends State appModel: appModel, appServices: appServices, onFormat: _handleFormatting, - onCompileAndRun: _performCompileAndRun, + onCompileAndRun: appServices.performCompileAndRun, key: _editorKey, ); @@ -465,10 +465,14 @@ class _DartPadMainPageState extends State child: CallbackShortcuts( bindings: { 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: @@ -529,37 +533,6 @@ class _DartPadMainPageState extends State } } - Future _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.] diff --git a/pkgs/dartpad_ui/lib/model.dart b/pkgs/dartpad_ui/lib/model.dart index 6c65faf25..c8bcc0771 100644 --- a/pkgs/dartpad_ui/lib/model.dart +++ b/pkgs/dartpad_ui/lib/model.dart @@ -304,6 +304,36 @@ class AppServices { appModel.appReady.value = true; } + Future 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 format(SourceRequest request) async { try { appModel.formattingBusy.value = true; @@ -330,7 +360,7 @@ class AppServices { } } - Future compileDDC(CompileRequest request) async { + Future _compileDDC(CompileRequest request) async { try { appModel.compilingBusy.value = true; return await services.compileDDC(request); @@ -358,7 +388,7 @@ class AppServices { _editorService = editorService; } - void executeJavaScript( + void _executeJavaScript( String javaScript, { required String dartSource, String? modulesBaseUrl,