Skip to content

Commit b8f7c8e

Browse files
committed
chore: Improve input assist experience
1 parent 1b95395 commit b8f7c8e

File tree

1 file changed

+43
-7
lines changed
  • server-core/src/main/java/io/onedev/server/web/behavior/inputassist

1 file changed

+43
-7
lines changed

server-core/src/main/java/io/onedev/server/web/behavior/inputassist/input-assist.js

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,35 @@ onedev.server.inputassist = {
1212
if (e.keyCode == 75 && (e.ctrlKey || e.metaKey) && !e.shiftKey) // command palette
1313
$input.blur();
1414
});
15-
$input.on("paste click keyup assist", function(e) {
15+
$input.on("paste click keyup assist", function(e) {
1616
// For click events, defer caret reading to allow browser to update position
1717
if (e.type === "click") {
1818
setTimeout(function() {
19-
$input.trigger("keyup");
19+
$input.trigger("assist");
2020
}, 0);
2121
return;
2222
}
2323

2424
var value = $input.val();
2525
var caret;
26-
if ($input.is(":focus"))
27-
caret = $input.caret();
28-
else
26+
var selectionStart, selectionEnd;
27+
if ($input.is(":focus")) {
28+
selectionStart = $input[0].selectionStart;
29+
selectionEnd = $input[0].selectionEnd;
30+
caret = selectionEnd;
31+
} else {
2932
caret = -1;
33+
selectionStart = -1;
34+
selectionEnd = -1;
35+
}
36+
37+
// For suggestion logic, treat selections as if they are cleared
38+
if (selectionStart !== -1 && selectionStart !== selectionEnd) {
39+
// Remove the selected portion from value and set caret at selection start
40+
value = value.substring(0, selectionStart) + value.substring(selectionEnd);
41+
caret = selectionStart;
42+
}
43+
3044
if (value != $input.data("prevValue") || caret != $input.data("prevCaret") || !$input.data("dropdown")) {
3145
$input.data("prevValue", value);
3246
$input.data("prevCaret", caret);
@@ -164,11 +178,33 @@ onedev.server.inputassist = {
164178
var $form = $input.closest("form");
165179
$form.css("position", "relative");
166180
$form.find(">.input-error-mark").remove();
181+
182+
// If there's a selection, errors were calculated based on effective value
183+
// (with selection treated as deleted). Map error positions back to actual value.
184+
var selectionStart = $input[0].selectionStart;
185+
var selectionEnd = $input[0].selectionEnd;
186+
var selectionLength = selectionEnd - selectionStart;
187+
167188
if ($input.val().length != 0) {
168189
for (var i in errors) {
169190
var error = errors[i];
170-
var fromCoord = getCaretCoordinates($input[0], error.from);
171-
var toCoord = getCaretCoordinates($input[0], error.to+1);
191+
var errorFrom = error.from;
192+
var errorTo = error.to;
193+
194+
// Map error positions from effective value back to actual value
195+
if (selectionLength > 0) {
196+
// Errors at or after selectionStart need to be shifted by selectionLength
197+
// because in the actual value, the selection still exists
198+
if (errorFrom >= selectionStart) {
199+
errorFrom += selectionLength;
200+
}
201+
if (errorTo >= selectionStart) {
202+
errorTo += selectionLength;
203+
}
204+
}
205+
206+
var fromCoord = getCaretCoordinates($input[0], errorFrom);
207+
var toCoord = getCaretCoordinates($input[0], errorTo+1);
172208
var $error = $("<div class='input-error-mark'></div>");
173209
$error.appendTo($form);
174210
var inputCoord = $input.offset();

0 commit comments

Comments
 (0)