Skip to content

Commit 8d7b71d

Browse files
committed
improve js code
1 parent 2709641 commit 8d7b71d

File tree

2 files changed

+42
-59
lines changed

2 files changed

+42
-59
lines changed

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
)
3030

3131
const (
32-
version = "v0.0.10"
32+
version = "v0.0.11"
3333
)
3434

3535
var (

web/static/js/vimbin.js

Lines changed: 41 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,45 @@
1-
// Desc: Main JavaScript file for the Vimbin web app
21
document.addEventListener("DOMContentLoaded", function () {
32
// Function to get the preferred theme (dark, light, or system default)
43
function getPreferredTheme() {
5-
// If the browser doesn't support the prefers-color-scheme media query, return the default theme
6-
if (!window.matchMedia) {
7-
return "default";
8-
}
4+
return window.matchMedia?.("(prefers-color-scheme: dark)")?.matches
5+
? "catppuccin"
6+
: "default";
7+
}
98

10-
if (window.matchMedia("(prefers-color-scheme: dark)").matches) {
11-
return "catppuccin";
12-
}
9+
// Function to set the theme based on the initial color scheme
10+
function setThemeBasedOnColorScheme() {
11+
const preferredTheme = getPreferredTheme();
12+
console.log(`Setting theme to '${preferredTheme}'`);
1313

14-
if (window.matchMedia("(prefers-color-scheme: light)").matches) {
15-
return "default";
16-
}
17-
18-
return "default";
14+
editor.setOption("theme", preferredTheme);
1915
}
2016

2117
// Function to update Vim mode display
2218
function updateVimMode(vimEvent, vimModeElement) {
23-
const mode = vimEvent.mode;
24-
const sub = vimEvent.subMode;
25-
19+
const { mode, subMode } = vimEvent;
20+
21+
console.log(`VIM mode '${mode}', subMode '${subMode}'`);
22+
23+
// Mapping of mode to corresponding text and class
24+
const modeMap = {
25+
normal: { text: "NORMAL", class: "normal" },
26+
insert: { text: "INSERT", class: "insert" },
27+
visual: {
28+
text: subMode === "" ? "VISUAL" : "V-LINE",
29+
class: subMode === "" ? "visual" : "visual-line",
30+
},
31+
unknown: { text: "UNKNOWN", class: "unknown" },
32+
};
33+
34+
// Remove all existing classes
2635
vimModeElement.classList.remove(
27-
"normal",
28-
"insert",
29-
"visual",
30-
"visual-line",
36+
...Object.values(modeMap).map((entry) => entry.class),
3137
);
3238

33-
switch (mode) {
34-
case "normal":
35-
vimModeElement.innerText = "NORMAL";
36-
vimModeElement.classList.add("normal");
37-
break;
38-
case "insert":
39-
vimModeElement.innerText = "INSERT";
40-
vimModeElement.classList.add("insert");
41-
break;
42-
case "visual":
43-
if (sub === "") {
44-
vimModeElement.innerText = "VISUAL";
45-
vimModeElement.classList.add("visual");
46-
break;
47-
}
48-
vimModeElement.innerText = "V-LINE";
49-
vimModeElement.classList.add("visual-line");
50-
break;
51-
default:
52-
vimModeElement.innerText = "UNKNOWN";
53-
vimModeElement.classList.add("unknown");
54-
}
39+
// Update text and add corresponding class
40+
const { text, class: modeClass } = modeMap[mode] || modeMap.unknown;
41+
vimModeElement.innerText = text;
42+
vimModeElement.classList.add(modeClass);
5543
}
5644

5745
// Function to show relative line numbers
@@ -118,7 +106,7 @@ document.addEventListener("DOMContentLoaded", function () {
118106
}
119107

120108
statusElement.innerText = status;
121-
statusElement.classList.remove("isError", "noChanges"); // Remove all classes
109+
statusElement.classList.remove("isError", "noChanges");
122110

123111
if (isError) {
124112
statusElement.classList.add("isError");
@@ -146,11 +134,10 @@ document.addEventListener("DOMContentLoaded", function () {
146134
matchBrackets: true,
147135
showCursorWhenSelecting: true,
148136
theme: getPreferredTheme(),
149-
lineWrapping: true, // Optional: enable line wrapping if desired
137+
lineWrapping: true,
150138
});
151139

152140
editor.on("cursorActivity", showRelativeLines);
153-
editor.focus();
154141

155142
// Custom vim Ex commands
156143
CodeMirror.Vim.defineEx("x", "", function () {
@@ -165,17 +152,13 @@ document.addEventListener("DOMContentLoaded", function () {
165152
CodeMirror.commands.save = saveContent;
166153

167154
// Listen for changes in the prefers-color-scheme media query
168-
window.matchMedia("(prefers-color-scheme: dark)").addListener((e) => {
169-
if (e.matches) {
170-
editor.setOption("theme", "catppuccin");
171-
} else {
172-
editor.setOption("theme", "default");
173-
}
174-
});
175-
176-
window.matchMedia("(prefers-color-scheme: light)").addListener((e) => {
177-
if (e.matches) {
178-
editor.setOption("theme", "default");
179-
}
180-
});
155+
window
156+
.matchMedia("(prefers-color-scheme: dark)")
157+
.addListener(setThemeBasedOnColorScheme);
158+
window
159+
.matchMedia("(prefers-color-scheme: light)")
160+
.addListener(setThemeBasedOnColorScheme);
161+
162+
// Focus editor
163+
editor.focus();
181164
});

0 commit comments

Comments
 (0)