Allow the default editor to be configured

This doesn't solve #41, but it does provide a good workaround.
This commit is contained in:
Vishesh Handa
2020-02-18 16:26:12 +01:00
parent 77a70a3336
commit 59d379ab62
3 changed files with 80 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import 'package:gitjournal/core/notes_folder.dart';
import 'package:gitjournal/editors/markdown_editor.dart'; import 'package:gitjournal/editors/markdown_editor.dart';
import 'package:gitjournal/editors/raw_editor.dart'; import 'package:gitjournal/editors/raw_editor.dart';
import 'package:gitjournal/editors/checklist_editor.dart'; import 'package:gitjournal/editors/checklist_editor.dart';
import 'package:gitjournal/settings.dart';
import 'package:gitjournal/state_container.dart'; import 'package:gitjournal/state_container.dart';
import 'package:gitjournal/widgets/folder_selection_dialog.dart'; import 'package:gitjournal/widgets/folder_selection_dialog.dart';
import 'package:gitjournal/widgets/rename_dialog.dart'; import 'package:gitjournal/widgets/rename_dialog.dart';
@ -53,6 +54,20 @@ class NoteEditorState extends State<NoteEditor> {
originalNoteData = MdYamlDoc.from(note.data); originalNoteData = MdYamlDoc.from(note.data);
} }
@override
void initState() {
super.initState();
switch (Settings.instance.defaultEditor) {
case SettingsEditorType.Markdown:
editorType = EditorType.Markdown;
break;
case SettingsEditorType.Raw:
editorType = EditorType.Raw;
break;
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return WillPopScope( return WillPopScope(

View File

@ -168,6 +168,19 @@ class SettingsListState extends State<SettingsList> {
enabled: remoteGitConfigured, enabled: remoteGitConfigured,
), ),
const SizedBox(height: 16.0), const SizedBox(height: 16.0),
SettingsHeader("Editor Settings"),
ListPreference(
title: "Default Editor",
currentOption: settings.defaultEditor.toPublicString(),
options:
SettingsEditorType.options.map((f) => f.toPublicString()).toList(),
onChange: (String publicStr) {
var val = SettingsEditorType.fromPublicString(publicStr);
Settings.instance.defaultEditor = val;
Settings.instance.save();
setState(() {});
},
),
SettingsHeader("Storage"), SettingsHeader("Storage"),
ListPreference( ListPreference(
title: "File Name", title: "File Name",

View File

@ -22,6 +22,7 @@ class Settings {
RemoteSyncFrequency remoteSyncFrequency = RemoteSyncFrequency.Default; RemoteSyncFrequency remoteSyncFrequency = RemoteSyncFrequency.Default;
SortingMode sortingMode = SortingMode.Default; SortingMode sortingMode = SortingMode.Default;
SettingsEditorType defaultEditor = SettingsEditorType.Default;
int version = 0; int version = 0;
void load(SharedPreferences pref) { void load(SharedPreferences pref) {
@ -44,6 +45,8 @@ class Settings {
pref.getString("remoteSyncFrequency")); pref.getString("remoteSyncFrequency"));
sortingMode = SortingMode.fromInternalString(pref.getString("sortingMode")); sortingMode = SortingMode.fromInternalString(pref.getString("sortingMode"));
defaultEditor =
SettingsEditorType.fromInternalString(pref.getString("defaultEditor"));
version = pref.getInt("settingsVersion") ?? version; version = pref.getInt("settingsVersion") ?? version;
} }
@ -60,6 +63,7 @@ class Settings {
pref.setString( pref.setString(
"remoteSyncFrequency", remoteSyncFrequency.toInternalString()); "remoteSyncFrequency", remoteSyncFrequency.toInternalString());
pref.setString("sortingMode", sortingMode.toInternalString()); pref.setString("sortingMode", sortingMode.toInternalString());
pref.setString("defaultEditor", defaultEditor.toInternalString());
pref.setInt("settingsVersion", version); pref.setInt("settingsVersion", version);
// Shouldn't we check if something has actually changed? // Shouldn't we check if something has actually changed?
@ -77,6 +81,7 @@ class Settings {
"collectCrashReports": collectCrashReports, "collectCrashReports": collectCrashReports,
"yamlModifiedKey": yamlModifiedKey, "yamlModifiedKey": yamlModifiedKey,
"defaultNewNoteFolder": defaultNewNoteFolder, "defaultNewNoteFolder": defaultNewNoteFolder,
"defaultEditor": defaultEditor.toInternalString(),
"version": version, "version": version,
}; };
} }
@ -237,3 +242,50 @@ class SortingMode {
return ""; return "";
} }
} }
class SettingsEditorType {
static const Markdown = SettingsEditorType("Markdown", "Markdown");
static const Raw = SettingsEditorType("Raw", "Raw");
static const Default = Markdown;
final String _str;
final String _publicString;
const SettingsEditorType(this._publicString, this._str);
String toInternalString() {
return _str;
}
String toPublicString() {
return _publicString;
}
static const options = <SettingsEditorType>[
Markdown,
Raw,
];
static SettingsEditorType fromInternalString(String str) {
for (var opt in options) {
if (opt.toInternalString() == str) {
return opt;
}
}
return Default;
}
static SettingsEditorType fromPublicString(String str) {
for (var opt in options) {
if (opt.toPublicString() == str) {
return opt;
}
}
return Default;
}
@override
String toString() {
assert(false, "EditorType toString should never be called");
return "";
}
}