From 76e88c7817183f83bf9362222e5f6b0c01468ff5 Mon Sep 17 00:00:00 2001 From: Levi Lesches Date: Wed, 7 Aug 2024 21:44:35 -0400 Subject: [PATCH 1/4] Implemented autosave on a 5-second timer --- pkgs/dartpad_ui/lib/editor/editor.dart | 11 ++++++++++- pkgs/dartpad_ui/lib/execution/frame.dart | 2 +- pkgs/dartpad_ui/lib/main.dart | 4 +++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pkgs/dartpad_ui/lib/editor/editor.dart b/pkgs/dartpad_ui/lib/editor/editor.dart index d06a19ef2..c24a85e8e 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 = codeMirror?.getDoc().getValue(); + if (content == null) 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..c8cdf2ad5 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); From 283fac2f327759ec947855cf8b5a18c9277ac5d0 Mon Sep 17 00:00:00 2001 From: Levi Lesches Date: Wed, 7 Aug 2024 21:47:41 -0400 Subject: [PATCH 2/4] Documented --- pkgs/dartpad_ui/lib/main.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkgs/dartpad_ui/lib/main.dart b/pkgs/dartpad_ui/lib/main.dart index c8cdf2ad5..06898ed42 100644 --- a/pkgs/dartpad_ui/lib/main.dart +++ b/pkgs/dartpad_ui/lib/main.dart @@ -278,8 +278,8 @@ class _DartPadMainPageState extends State sampleId: widget.builtinSampleId, flutterSampleId: widget.flutterSampleId, channel: widget.initialChannel, - fallbackSnippet: web.window.localStorage.getItem('user_input') - ?? 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); From eb4d49ca6e8938e83b6c3e45c10eefccb39425cf Mon Sep 17 00:00:00 2001 From: Levi Lesches Date: Thu, 15 Aug 2024 18:51:45 -0400 Subject: [PATCH 3/4] Removed double quotes --- pkgs/dartpad_ui/lib/main.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/dartpad_ui/lib/main.dart b/pkgs/dartpad_ui/lib/main.dart index 06898ed42..f03814143 100644 --- a/pkgs/dartpad_ui/lib/main.dart +++ b/pkgs/dartpad_ui/lib/main.dart @@ -15,7 +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 'package:web/web.dart' as web; import 'console.dart'; import 'docs.dart'; From d60a500214fa13ec1016d1285ba628f585ee9cde Mon Sep 17 00:00:00 2001 From: Levi Lesches Date: Fri, 16 Aug 2024 16:06:37 -0400 Subject: [PATCH 4/4] Use appModel instead of codeMirror --- pkgs/dartpad_ui/lib/editor/editor.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkgs/dartpad_ui/lib/editor/editor.dart b/pkgs/dartpad_ui/lib/editor/editor.dart index c24a85e8e..a9c3d3910 100644 --- a/pkgs/dartpad_ui/lib/editor/editor.dart +++ b/pkgs/dartpad_ui/lib/editor/editor.dart @@ -183,8 +183,8 @@ class _EditorWidgetState extends State implements EditorService { Timer? _autosaveTimer; void _autosave([Timer? timer]) { - final content = codeMirror?.getDoc().getValue(); - if (content == null) return; + final content = widget.appModel.sourceCodeController.text; + if (content.isEmpty) return; web.window.localStorage.setItem('user_input', content); }