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

Implement /flutter for flutter.new as firebase redirect #3093

Merged
merged 4 commits into from
Nov 20, 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
10 changes: 10 additions & 0 deletions pkgs/dartpad_ui/firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
"**/node_modules/**"
],
"redirects": [
{
"source": "/dart",
"destination": "/?sample=dart",
"type": 301
},
{
"source": "/flutter",
"destination": "/?sample=flutter",
"type": 301
},
{
"source": "/embed-dart?(.html)",
"destination": "/?embed=true",
Expand Down
7 changes: 1 addition & 6 deletions pkgs/dartpad_ui/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ class _DartPadAppState extends State<DartPadApp> {

Widget _homePageBuilder(BuildContext context, GoRouterState state,
{String? gist}) {
final path = state.path;
final gistId = gist ?? state.uri.queryParameters['id'];
final builtinSampleId = state.uri.queryParameters['sample'];
final flutterSampleId = state.uri.queryParameters['sample_id'];
Expand All @@ -128,7 +127,6 @@ class _DartPadAppState extends State<DartPadApp> {
final runOnLoad = state.uri.queryParameters['run'] == 'true';

return DartPadMainPage(
path: path,
initialChannel: channelParam,
embedMode: embedMode,
runOnLoad: runOnLoad,
Expand Down Expand Up @@ -207,7 +205,6 @@ class DartPadMainPage extends StatefulWidget {
final String? gistId;
final String? builtinSampleId;
final String? flutterSampleId;
final String? path;

DartPadMainPage({
required this.initialChannel,
Expand All @@ -217,7 +214,6 @@ class DartPadMainPage extends StatefulWidget {
this.gistId,
this.builtinSampleId,
this.flutterSampleId,
this.path,
}) : super(
key: ValueKey(
'sample:$builtinSampleId gist:$gistId flutter:$flutterSampleId',
Expand Down Expand Up @@ -276,14 +272,13 @@ class _DartPadMainPageState extends State<DartPadMainPage>
);

appServices.populateVersions();
final fallBackSnippetType = widget.path == '/flutter' ? 'flutter' : 'dart';
appServices
.performInitialLoad(
gistId: widget.gistId,
sampleId: widget.builtinSampleId,
flutterSampleId: widget.flutterSampleId,
channel: widget.initialChannel,
fallbackSnippet: Samples.getDefault(type: fallBackSnippetType))
fallbackSnippet: Samples.defaultSnippet())
parlough marked this conversation as resolved.
Show resolved Hide resolved
.then((value) {
// Start listening for inject code messages.
handleEmbedMessage(appServices, runOnInject: widget.runOnLoad);
Expand Down
3 changes: 2 additions & 1 deletion pkgs/dartpad_ui/lib/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ class AppServices {

void resetTo({String? type}) {
type ??= 'dart';
final source = Samples.getDefault(type: type);
final source =
Samples.defaultSnippet(forFlutter: type.toLowerCase() == 'flutter');

// Reset the source.
appModel.sourceCodeController.text = source;
Expand Down
112 changes: 52 additions & 60 deletions pkgs/dartpad_ui/lib/samples.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pkgs/dartpad_ui/test/samples_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import 'package:test/test.dart';

void main() {
group('samples', () {
test('has dart default', () {
final sample = Samples.getDefault(type: 'dart');
test('has default dart sample', () {
final sample = Samples.getById('dart');
expect(sample, isNotNull);
});

test('has flutter default', () {
final sample = Samples.getDefault(type: 'flutter');
test('has default flutter sample', () {
final sample = Samples.getById('flutter');
expect(sample, isNotNull);
});
});
Expand Down
2 changes: 2 additions & 0 deletions pkgs/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Sample code snippets for DartPad.
| --- | --- | --- | --- |
| Dart | Fibonacci | [fibonacci.dart](lib/fibonacci.dart) | `fibonacci` |
| Dart | Hello world | [hello_world.dart](lib/hello_world.dart) | `hello-world` |
| Defaults | Dart snippet | [default_dart.dart](lib/default_dart.dart) | `dart` |
| Defaults | Flutter snippet | [default_flutter.dart](lib/default_flutter.dart) | `flutter` |
| Ecosystem | Flame game | [brick_breaker.dart](lib/brick_breaker.dart) | `flame-game` |
| Ecosystem | Google AI SDK | [google_ai.dart](lib/google_ai.dart) | `google-ai-sdk` |
| Flutter | Counter | [main.dart](lib/main.dart) | `counter` |
Expand Down
6 changes: 5 additions & 1 deletion pkgs/samples/lib/default_dart.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One drawback of having the copyright header here (and for default_flutter.dart) is that they'll show up for most dartpad snippets. I think it's ok to omit here, but to keep it for the more specific samples (sunflower, ...).

Copy link
Member Author

@parlough parlough Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this PR, I ended up stripping the copyright from each snippet when loaded into DartPad, but keeping it in the source file. Is there any concern with that? As the license comments end up distracting from the samples anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I'd missed that! No real concerns.

// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

void main() {
for (int i = 0; i < 10; i++) {
for (var i = 0; i < 10; i++) {
print('hello ${i + 1}');
}
}
4 changes: 4 additions & 0 deletions pkgs/samples/lib/default_flutter.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:flutter/material.dart';

void main() {
Expand Down
Loading