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

Implemented autosave on a 5-second timer #3037

Merged
merged 18 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
11 changes: 10 additions & 1 deletion pkgs/dartpad_ui/lib/editor/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,17 @@ class _EditorWidgetState extends State<EditorWidget> 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;

Expand Down Expand Up @@ -305,6 +312,8 @@ class _EditorWidgetState extends State<EditorWidget> implements EditorService {
@override
void dispose() {
listener?.cancel();
_autosaveTimer?.cancel();
_autosave();

widget.appServices.registerEditorService(null);

Expand Down
2 changes: 1 addition & 1 deletion pkgs/dartpad_ui/lib/execution/frame.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function dartPrint(message) {
'sender': 'frame',
'type': 'stdout',
'message': message.toString()
}, '*');
}, '*');
}
''');

Expand Down
4 changes: 3 additions & 1 deletion pkgs/dartpad_ui/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -277,7 +278,8 @@ class _DartPadMainPageState extends State<DartPadMainPage>
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);
Expand Down