diff --git a/pkgs/dartpad_ui/lib/editor/editor.dart b/pkgs/dartpad_ui/lib/editor/editor.dart index d06a19ef2..a9c3d3910 100644 --- a/pkgs/dartpad_ui/lib/editor/editor.dart +++ b/pkgs/dartpad_ui/lib/editor/editor.dart @@ -177,10 +177,17 @@ class _EditorWidgetState extends State implements EditorService { @override void initState() { super.initState(); - + _autosaveTimer = Timer.periodic(const Duration(seconds: 5), _autosave); widget.appModel.appReady.addListener(_updateEditableStatus); } + Timer? _autosaveTimer; + void _autosave([Timer? timer]) { + final content = widget.appModel.sourceCodeController.text; + if (content.isEmpty) return; + web.window.localStorage.setItem('user_input', content); + } + void _platformViewCreated(int id, {required bool darkMode}) { codeMirror = codeMirrorInstance; @@ -305,6 +312,8 @@ class _EditorWidgetState extends State implements EditorService { @override void dispose() { listener?.cancel(); + _autosaveTimer?.cancel(); + _autosave(); widget.appServices.registerEditorService(null); diff --git a/pkgs/dartpad_ui/lib/execution/frame.dart b/pkgs/dartpad_ui/lib/execution/frame.dart index 99db80804..c9ced1c92 100644 --- a/pkgs/dartpad_ui/lib/execution/frame.dart +++ b/pkgs/dartpad_ui/lib/execution/frame.dart @@ -63,7 +63,7 @@ function dartPrint(message) { 'sender': 'frame', 'type': 'stdout', 'message': message.toString() - }, '*'); + }, '*'); } '''); diff --git a/pkgs/dartpad_ui/lib/main.dart b/pkgs/dartpad_ui/lib/main.dart index 9dc4f3328..f03814143 100644 --- a/pkgs/dartpad_ui/lib/main.dart +++ b/pkgs/dartpad_ui/lib/main.dart @@ -15,6 +15,7 @@ import 'package:provider/provider.dart'; import 'package:split_view/split_view.dart'; import 'package:url_launcher/url_launcher.dart' as url_launcher; import 'package:vtable/vtable.dart'; +import 'package:web/web.dart' as web; import 'console.dart'; import 'docs.dart'; @@ -277,7 +278,8 @@ class _DartPadMainPageState extends State sampleId: widget.builtinSampleId, flutterSampleId: widget.flutterSampleId, channel: widget.initialChannel, - fallbackSnippet: Samples.getDefault(type: 'dart')) + fallbackSnippet: web.window.localStorage.getItem('user_input') ?? + Samples.getDefault(type: 'dart')) .then((value) { // Start listening for inject code messages. handleEmbedMessage(appServices, runOnInject: widget.runOnLoad);