Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkgs/id/web/src/editor-compat.css
Original file line number Diff line number Diff line change
Expand Up @@ -1140,6 +1140,16 @@ img.ProseMirror-separator {
border-color: var(--color-primary);
}

.goto-line-input[aria-invalid="true"] {
border-color: var(--color-error);
}

.goto-line-error {
color: var(--color-error);
font-size: 0.75rem;
white-space: nowrap;
}

/* Hide number input spinners */
.goto-line-input::-webkit-inner-spin-button,
.goto-line-input::-webkit-outer-spin-button {
Expand Down
26 changes: 26 additions & 0 deletions pkgs/id/web/src/goto-line.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,32 @@ describe("dialog input interaction", () => {
expect(dialog.style.display).not.toBe("none");
});

it.each([
["", "Enter a line number."],
["2.0", "Enter a whole number."],
["999", "Enter a line between 1 and 3."],
])("explains invalid input %j and clears the error when corrected", (value, message) => {
const { view, container } = createViewWithGotoLine("first\nsecond\nthird");
views.push(view);
containers.push(container);

triggerGotoLine(view);
const input = container.querySelector<HTMLInputElement>(".goto-line-input")!;
const error = container.querySelector<HTMLElement>("#goto-line-error")!;
expect(input.getAttribute("aria-describedby")).toBe("goto-line-error");

input.value = value;
input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
expect(error.textContent).toBe(message);
expect(error.getAttribute("role")).toBe("alert");
expect(input.getAttribute("aria-invalid")).toBe("true");

input.value = "2";
input.dispatchEvent(new Event("input", { bubbles: true }));
expect(error.textContent).toBe("");
expect(input.hasAttribute("aria-invalid")).toBe(false);
});

it("Enter with line 1 moves cursor to the first line start", () => {
const { view, container } = createViewWithGotoLine("first\nsecond");
views.push(view);
Expand Down
38 changes: 37 additions & 1 deletion pkgs/id/web/src/goto-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { EditorView } from "prosemirror-view";
/** State for the goto-line dialog. */
let dialogEl: HTMLElement | null = null;
let dialogInput: HTMLInputElement | null = null;
let dialogError: HTMLElement | null = null;
let activeView: EditorView | null = null;

/**
Expand Down Expand Up @@ -76,12 +77,31 @@ function createDialog(container: HTMLElement): HTMLElement {
dialog.setAttribute("aria-label", "Go to line");
dialog.innerHTML = `
<label class="goto-line-label" for="goto-line-input">Go to Line:</label>
<input type="number" min="1" class="goto-line-input" id="goto-line-input" placeholder="Line #" autocomplete="off" />
<input type="number" min="1" class="goto-line-input" id="goto-line-input" placeholder="Line #" autocomplete="off" aria-describedby="goto-line-error" />
<span class="goto-line-error" id="goto-line-error" role="alert"></span>
`;
container.prepend(dialog);
return dialog;
}

/**
* Explain why the typed value was rejected, in the dialog itself.
*
* Before this, invalid input only re-selected the text: no visible or
* spoken reason, so "Go to Line" looked broken for out-of-range or
* non-integer values (#712).
*/
function showGotoLineError(message: string): void {
if (dialogInput) dialogInput.setAttribute("aria-invalid", "true");
if (dialogError) dialogError.textContent = message;
}

/** Clear the rejection message once the value changes or the dialog closes. */
function clearGotoLineError(): void {
if (dialogInput) dialogInput.removeAttribute("aria-invalid");
if (dialogError) dialogError.textContent = "";
}

/** Open the goto-line dialog. */
function openGotoLine(view: EditorView): void {
const container = view.dom.parentElement;
Expand All @@ -99,8 +119,10 @@ function openGotoLine(view: EditorView): void {
if (!dialogEl) {
dialogEl = createDialog(container);
dialogInput = dialogEl.querySelector("#goto-line-input");
dialogError = dialogEl.querySelector("#goto-line-error");

if (dialogInput) {
dialogInput.addEventListener("input", clearGotoLineError);
dialogInput.addEventListener("keydown", (e: KeyboardEvent) => {
if (e.key === "Enter") {
e.preventDefault();
Expand All @@ -124,9 +146,20 @@ function openGotoLine(view: EditorView): void {
? (lineStarts[line - 1] ?? null)
: null;
if (pos === null || !activeView) {
// #712: keep the dialog open, but say why β€” values that are
// empty, not whole numbers, or outside 1..lineCount all used
// to be rejected silently.
const message =
raw === ""
? "Enter a line number."
: !/^\d+$/.test(raw)
? "Enter a whole number."
: `Enter a line between 1 and ${lineCount}.`;
showGotoLineError(message);
dialogInput?.select();
return;
}
clearGotoLineError();
const tr = activeView.state.tr.setSelection(TextSelection.create(activeView.state.doc, pos));
activeView.dispatch(tr);
activeView.focus();
Expand All @@ -144,6 +177,7 @@ function openGotoLine(view: EditorView): void {

activeView = view;
dialogEl.style.display = "";
clearGotoLineError();

// Show line count hint
const totalLines = getLineCount(view);
Expand All @@ -157,6 +191,7 @@ function openGotoLine(view: EditorView): void {

/** Close the goto-line dialog. */
function closeGotoLine(): void {
clearGotoLineError();
if (dialogEl) {
dialogEl.style.display = "none";
}
Expand Down Expand Up @@ -190,6 +225,7 @@ export function destroyGotoLineDialog(): void {
dialogEl.remove();
dialogEl = null;
dialogInput = null;
dialogError = null;
activeView = null;
}
}
Loading