Skip to content

Commit 11b9be8

Browse files
committed
Moved autosave functionality to LocalStorage singleton
1 parent 6bcfbfe commit 11b9be8

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

pkgs/dartpad_ui/lib/editor/editor.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:flutter/material.dart';
1212
import 'package:flutter/services.dart';
1313
import 'package:web/web.dart' as web;
1414

15+
import '../local_storage.dart';
1516
import '../model.dart';
1617
import 'codemirror.dart';
1718

@@ -185,7 +186,7 @@ class _EditorWidgetState extends State<EditorWidget> implements EditorService {
185186
void _autosave([Timer? timer]) {
186187
final content = widget.appModel.sourceCodeController.text;
187188
if (content.isEmpty) return;
188-
web.window.localStorage.setItem('user_input', content);
189+
LocalStorage.instance.saveUserCode(content);
189190
}
190191

191192
void _platformViewCreated(int id, {required bool darkMode}) {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:web/web.dart' as web;
6+
7+
const _userInputKey = 'user_';
8+
9+
class LocalStorage {
10+
static final instance = LocalStorage._();
11+
12+
LocalStorage._();
13+
14+
void saveUserCode(String code) =>
15+
web.window.localStorage.setItem(_userInputKey, code);
16+
17+
String? getUserCode() => web.window.localStorage.getItem(_userInputKey);
18+
}

pkgs/dartpad_ui/lib/main.dart

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import 'package:provider/provider.dart';
1515
import 'package:split_view/split_view.dart';
1616
import 'package:url_launcher/url_launcher.dart' as url_launcher;
1717
import 'package:vtable/vtable.dart';
18-
import 'package:web/web.dart' as web;
1918

2019
import 'console.dart';
2120
import 'docs.dart';
@@ -24,6 +23,7 @@ import 'embed.dart';
2423
import 'execution/execution.dart';
2524
import 'extensions.dart';
2625
import 'keys.dart' as keys;
26+
import 'local_storage.dart';
2727
import 'model.dart';
2828
import 'problems.dart';
2929
import 'samples.g.dart';
@@ -273,25 +273,27 @@ class _DartPadMainPageState extends State<DartPadMainPage>
273273
);
274274

275275
appServices.populateVersions();
276-
appServices
277-
.performInitialLoad(
278-
gistId: widget.gistId,
279-
sampleId: widget.builtinSampleId,
280-
flutterSampleId: widget.flutterSampleId,
281-
channel: widget.initialChannel,
282-
fallbackSnippet: web.window.localStorage.getItem('user_input') ??
283-
Samples.defaultSnippet())
284-
.then((value) {
285-
// Start listening for inject code messages.
286-
handleEmbedMessage(appServices, runOnInject: widget.runOnLoad);
287-
if (widget.runOnLoad) {
288-
appServices.performCompileAndRun();
289-
}
290-
});
276+
_initAppServices();
291277

292278
appModel.compilingBusy.addListener(_handleRunStarted);
293279
}
294280

281+
Future<void> _initAppServices() async {
282+
await appServices.performInitialLoad(
283+
gistId: widget.gistId,
284+
sampleId: widget.builtinSampleId,
285+
flutterSampleId: widget.flutterSampleId,
286+
channel: widget.initialChannel,
287+
fallbackSnippet:
288+
LocalStorage.instance.getUserCode() ?? Samples.defaultSnippet(),
289+
);
290+
// Start listening for inject code messages.
291+
handleEmbedMessage(appServices, runOnInject: widget.runOnLoad);
292+
if (widget.runOnLoad) {
293+
appServices.performCompileAndRun();
294+
}
295+
}
296+
295297
@override
296298
void dispose() {
297299
appModel.compilingBusy.removeListener(_handleRunStarted);

0 commit comments

Comments
 (0)