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

support older gist formats #2880

Merged
merged 1 commit into from
Feb 29, 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
1 change: 1 addition & 0 deletions pkgs/sketch_pad/lib/editor/codemirror.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
library;

import 'dart:js_interop';

import 'package:web/web.dart';

extension type CodeMirrorOptions._(JSObject _) implements JSObject {
Expand Down
32 changes: 28 additions & 4 deletions pkgs/sketch_pad/lib/gists.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,22 @@ class GistLoader {
}

class Gist {
static const String defaultFileName = 'main.dart';

final String id;
final String? description;
final String? owner;
final List<GistFile> files;
final List<String> validationIssues = [];

Gist({
required this.id,
required this.description,
required this.owner,
required this.files,
});
}) {
_validateGist();
}

factory Gist.fromJson(Map<String, dynamic> json) {
/* {
Expand Down Expand Up @@ -80,9 +85,28 @@ class Gist {
}

String? get mainDartSource {
return files
.firstWhereOrNull((file) => file.fileName == 'main.dart')
?.content;
GistFile? file;

// First, try and load 'main.dart'.
file = files.firstWhereOrNull((file) => file.fileName == defaultFileName);

// Fall back on the older (unintentional) contention - loading from the
// single dart file in a gist.
file ??= files.singleWhereOrNull((file) => file.fileName.endsWith('.dart'));

return file?.content;
}

void _validateGist() {
final file =
files.singleWhereOrNull((file) => file.fileName.endsWith('.dart'));

if (file == null) {
validationIssues.add('Warning: no Dart file found in the gist');
} else if (file.fileName != defaultFileName) {
validationIssues.add('Warning: no gist content in $defaultFileName '
'(loading from ${file.fileName})');
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions pkgs/sketch_pad/lib/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ class AppServices {
appModel.sourceCodeController.text = fallbackSnippet;
} else {
appModel.sourceCodeController.text = source;

if (gist.validationIssues.isNotEmpty) {
final message = gist.validationIssues.join('\n');
appModel.editorStatus.showToast(
message,
duration: const Duration(seconds: 10),
);
}
}

appModel.appReady.value = true;
Expand Down
43 changes: 42 additions & 1 deletion pkgs/sketch_pad/test/gists_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ void main() {

expect(gist.mainDartSource, isNull);
});

test('validates main.dart missing', () {
final gist =
Gist.fromJson(jsonDecode(jsonSampleNoMain) as Map<String, dynamic>);

expect(gist.validationIssues, isNotEmpty);
});

test('validates unexpected dart content file', () {
final gist = Gist.fromJson(
jsonDecode(jsonSampleAlternativeFile) as Map<String, dynamic>);

expect(gist.validationIssues, isNotEmpty);
});
});
}

Expand Down Expand Up @@ -76,7 +90,7 @@ const String jsonSampleNoMain = '''
"user": null,
"truncated": false,
"files": {
"main.dart": {
"main.html": {
"filename": "main.html",
"type": "application/vnd.dart",
"language": "Dart",
Expand All @@ -88,3 +102,30 @@ const String jsonSampleNoMain = '''
}
}
''';

const String jsonSampleAlternativeFile = '''
{
"id": "d3bd83918d21b6d5f778bdc69c3d36d6",
"description": "Fibonacci",
"owner": {
"login": "flutterdevrelgists"
},
"public": false,
"created_at": "2021-08-23T23:27:20Z",
"updated_at": "2023-05-30T10:59:27Z",
"comments": 0,
"user": null,
"truncated": false,
"files": {
"sample_1.dart": {
"filename": "sample_1.dart",
"type": "application/vnd.dart",
"language": "Dart",
"raw_url": "https://gist.githubusercontent.com/flutterdevrelgists/d3bd83918d21b6d5f778bdc69c3d36d6/raw/5eaecf3519fe453298d077068194720c9729be62/main.dart",
"size": 369,
"truncated": false,
"content": "..."
}
}
}
''';
Loading