Skip to content

Commit

Permalink
Revert "Removed local_storage conditional import"
Browse files Browse the repository at this point in the history
This reverts commit c952120.
  • Loading branch information
Levi-Lesches committed Jan 15, 2025
1 parent 53b4ce1 commit 94f97e8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
3 changes: 1 addition & 2 deletions pkgs/dartpad_ui/lib/editor/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ class _EditorWidgetState extends State<EditorWidget> implements EditorService {
}

Timer? _autosaveTimer;
void _autosave([Timer? _]) {
// (Callbacks to [Timer.periodic] must accept a Timer argument)
void _autosave([Timer? timer]) {
final content = widget.appModel.sourceCodeController.text;
if (content.isEmpty) return;
LocalStorage.instance.saveUserCode(content);
Expand Down
18 changes: 6 additions & 12 deletions pkgs/dartpad_ui/lib/local_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:web/web.dart' as web;
import 'local_storage/stub.dart'
if (dart.library.js_util) 'local_storage/web.dart';

import '../utils.dart';
abstract class LocalStorage {
static LocalStorage instance = LocalStorageImpl();

const _userInputKey = 'user_';

class LocalStorage {
static final instance = LocalStorage();

void saveUserCode(String code) =>
web.window.localStorage.setItem(_userInputKey, code);

String? getUserCode() =>
web.window.localStorage.getItem(_userInputKey)?.nullIfEmpty;
void saveUserCode(String code);
String? getUserCode();
}
16 changes: 16 additions & 0 deletions pkgs/dartpad_ui/lib/local_storage/stub.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import '../local_storage.dart';
import '../utils.dart';

class LocalStorageImpl extends LocalStorage {
String? _code;

@override
void saveUserCode(String code) => _code = code;

@override
String? getUserCode() => _code?.nullIfEmpty;
}
20 changes: 20 additions & 0 deletions pkgs/dartpad_ui/lib/local_storage/web.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:web/web.dart' as web;

import '../local_storage.dart';
import '../utils.dart';

const _userInputKey = 'user_';

class LocalStorageImpl extends LocalStorage {
@override
void saveUserCode(String code) =>
web.window.localStorage.setItem(_userInputKey, code);

@override
String? getUserCode() =>
web.window.localStorage.getItem(_userInputKey)?.nullIfEmpty;
}
2 changes: 1 addition & 1 deletion pkgs/dartpad_ui/test/autosave_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Never throwingFallback() =>

void main() {
const channel = Channel.stable;
group('Autosave', () {
group('Autosave:', () {
test('empty content is treated as null', () {
expect(''.nullIfEmpty, isNull);

Expand Down

0 comments on commit 94f97e8

Please sign in to comment.