Skip to content

feat(editor): persist keybinding preference between sessions #3125

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

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading