Refactor CommentEditor.vue to use enum for editor modes and improve type safety

This commit is contained in:
Boris Waaub
2026-02-04 16:57:35 +01:00
parent af74fd1fd8
commit 7e59cf02e1

View File

@@ -32,11 +32,16 @@ import {
EDITOR_SWITCH_TO_COMPLEX,
} from "translator";
const EDITOR_MODE_KEY = "editorMode";
const kind = ref<"simple" | "rich">("simple");
const value = defineModel({ required: true });
enum EditorKind {
SIMPLE = "simple",
RICH = "rich",
}
const isSimple = computed(() => kind.value === "simple");
const EDITOR_MODE_KEY = "editorMode";
const kind = ref<EditorKind>(EditorKind.SIMPLE);
const value = defineModel<string>({ required: true });
const isSimple = computed(() => kind.value === EditorKind.SIMPLE);
const toggleButtonClass = computed(() => {
return {
@@ -47,19 +52,20 @@ const toggleButtonClass = computed(() => {
});
const toggleEditor = () => {
let newValue;
newValue = kind.value === "simple" ? "rich" : "simple";
kind.value = "rich";
const newValue =
kind.value === EditorKind.SIMPLE ? EditorKind.RICH : EditorKind.SIMPLE;
kind.value = newValue;
window.localStorage.setItem(EDITOR_MODE_KEY, newValue);
window.dispatchEvent(new Event("toggleEditorKind"));
};
const onKindChange = function (/* event: StorageEvent | Event */) {
const newValue = window.localStorage.getItem(EDITOR_MODE_KEY);
if (null === newValue || !(newValue === "rich" || newValue === "simple")) {
if (
null === newValue ||
!(newValue === EditorKind.RICH || newValue === EditorKind.SIMPLE)
) {
throw "invalid new value: " + newValue;
}
@@ -72,7 +78,10 @@ onMounted(function () {
const storage = window.localStorage;
const savedKind = storage.getItem(EDITOR_MODE_KEY);
if (null !== kind.value && (savedKind === "simple" || savedKind === "rich")) {
if (
null !== kind.value &&
(savedKind === EditorKind.SIMPLE || savedKind === EditorKind.RICH)
) {
kind.value = savedKind;
}