Skip to content

Commit

Permalink
feat(editor): persist keybinding preference between sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
maranix committed Jan 17, 2025
1 parent 4b1ab01 commit 12b5b7c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkgs/dartpad_ui/lib/editor/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,10 @@ class _EditorWidgetState extends State<EditorWidget> implements EditorService {

if (enabled) {
cm.setKeymap('vim');
LocalStorage.instance.saveUserKeybinding('vim');
} else {
cm.setKeymap('default');
LocalStorage.instance.saveUserKeybinding('default');
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions pkgs/dartpad_ui/lib/local_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ abstract class LocalStorage {

void saveUserCode(String code);
String? getUserCode();

void saveUserKeybinding(String keybinding);
String? getUserKeybinding();
}
7 changes: 7 additions & 0 deletions pkgs/dartpad_ui/lib/local_storage/stub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ import '../utils.dart';

class LocalStorageImpl extends LocalStorage {
String? _code;
String? _keyBinding;

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

@override
void saveUserKeybinding(String keybinding) => _keyBinding = keybinding;

@override
String? getUserCode() => _code?.nullIfEmpty;

@override
String? getUserKeybinding() => _keyBinding?.nullIfEmpty;
}
9 changes: 9 additions & 0 deletions pkgs/dartpad_ui/lib/local_storage/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import '../local_storage.dart';
import '../utils.dart';

const _userInputKey = 'user_';
const _userKeybindingKey = 'user_keybinding_';

class LocalStorageImpl extends LocalStorage {
@override
Expand All @@ -17,4 +18,12 @@ class LocalStorageImpl extends LocalStorage {
@override
String? getUserCode() =>
web.window.localStorage.getItem(_userInputKey)?.nullIfEmpty;

@override
void saveUserKeybinding(String keybinding) =>
web.window.localStorage.setItem(_userKeybindingKey, keybinding);

@override
String? getUserKeybinding() =>
web.window.localStorage.getItem(_userKeybindingKey)?.nullIfEmpty;
}
1 change: 1 addition & 0 deletions pkgs/dartpad_ui/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ class _DartPadMainPageState extends State<DartPadMainPage>
sampleId: widget.builtinSampleId,
flutterSampleId: widget.flutterSampleId,
channel: widget.initialChannel,
keybinding: LocalStorage.instance.getUserKeybinding(),
getFallback: () =>
LocalStorage.instance.getUserCode() ?? Samples.defaultSnippet(),
)
Expand Down
5 changes: 5 additions & 0 deletions pkgs/dartpad_ui/lib/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ class AppServices {
String? sampleId,
String? flutterSampleId,
String? channel,
String? keybinding,
required String Function() getFallback,
}) async {
// Delay a bit for codemirror to initialize.
Expand Down Expand Up @@ -294,6 +295,10 @@ class AppServices {
return;
}

if (keybinding != null && keybinding == 'vim') {
appModel.vimKeymapsEnabled.value = true;
}

// Neither gistId nor flutterSampleId were passed in.
appModel.sourceCodeController.text = getFallback();
appModel.appReady.value = true;
Expand Down

0 comments on commit 12b5b7c

Please sign in to comment.