Skip to content

Commit bbe658d

Browse files
committed
add relative line numbers
1 parent 67b99e9 commit bbe658d

File tree

2 files changed

+14
-56
lines changed

2 files changed

+14
-56
lines changed

web/static/js/vimbin.js

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ document.addEventListener("DOMContentLoaded", function () {
5353
vimModeElement.classList.add("unknown");
5454
}
5555
}
56+
// Function to show relative line numbers
57+
function showRelativeLines(cm) {
58+
const lineNum = cm.getCursor().line + 1;
59+
if (cm.state.curLineNum === lineNum) {
60+
return;
61+
}
62+
cm.state.curLineNum = lineNum;
63+
cm.setOption("lineNumberFormatter", (l) =>
64+
l === lineNum ? lineNum : Math.abs(lineNum - l),
65+
);
66+
}
5667

5768
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
5869
lineNumbers: true,
@@ -64,60 +75,7 @@ document.addEventListener("DOMContentLoaded", function () {
6475
lineWrapping: true, // Optional: enable line wrapping if desired
6576
});
6677

67-
editor.setOption("extraKeys", {
68-
"Ctrl-Y": function () {
69-
const selectedText = editor.getSelection();
70-
71-
if (!selectedText) {
72-
document.getElementById("vim-command-line").innerText =
73-
"No text selected to yank";
74-
setClearMessageTimer();
75-
return;
76-
}
77-
78-
navigator.clipboard
79-
.writeText(selectedText)
80-
.then(function () {
81-
document.getElementById("vim-command-line").innerText =
82-
"Yanked to clipboard";
83-
})
84-
.catch(function (error) {
85-
document.getElementById("vim-command-line").innerText =
86-
"Error yanking to clipboard: " + error.message;
87-
});
88-
89-
setClearMessageTimer();
90-
editor.focus();
91-
},
92-
"Ctrl-P": function () {
93-
navigator.clipboard
94-
.readText()
95-
.then(function (clipboardText) {
96-
if (clipboardText) {
97-
const cursor = editor.getCursor();
98-
editor.replaceRange(clipboardText, cursor);
99-
document.getElementById("vim-command-line").innerText =
100-
"Pasted from clipboard";
101-
} else {
102-
document.getElementById("vim-command-line").innerText =
103-
"Clipboard is empty";
104-
}
105-
})
106-
.catch(function (error) {
107-
document.getElementById("vim-command-line").innerText =
108-
"Error pasting from clipboard: " + error.message;
109-
});
110-
111-
setClearMessageTimer();
112-
editor.focus();
113-
},
114-
});
115-
116-
function setClearMessageTimer() {
117-
setTimeout(function () {
118-
document.getElementById("vim-command-line").innerText = "";
119-
}, 3000);
120-
}
78+
editor.on("cursorActivity", showRelativeLines);
12179

12280
editor.focus();
12381

@@ -162,7 +120,7 @@ document.addEventListener("DOMContentLoaded", function () {
162120
} catch (error) {
163121
status = "Error saving: " + error.message;
164122
}
165-
document.getElementById("vim-command-line").innerText = status;
123+
document.getElementById("status").innerText = status;
166124
};
167125

168126
// Listen for changes in the prefers-color-scheme media query

web/templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ <h2>{{.Title}}</h2>
3030
<footer>
3131
<div class="vim-info-container">
3232
<span id="vim-mode" class="vim-mode normal">NORMAL</span>
33-
<span id="vim-command-line" class="custom-width nowrap"></span>
33+
<span id="status" class="custom-width nowrap"></span>
3434
</div>
3535
<script src="static/js/vimbin.js"></script>
3636
<script>

0 commit comments

Comments
 (0)