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
29 changes: 29 additions & 0 deletions pkgs/id/web/src/goto-line.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,35 @@ describe("go-to-line across textblocks (#745)", () => {
// Trailing paragraph sits at pos 12; its text starts at pos 13.
expect(view.state.selection.from).toBe(13);
});

it("rich doc with hard_break counts as multiple lines and jumps correctly", () => {
const doc = richSchema.node("doc", null, [
richSchema.node("paragraph", null, [
richSchema.text("foo"),
richSchema.node("hard_break"),
richSchema.text("bar"),
]),
richSchema.node("paragraph", null, [richSchema.text("baz")]),
]);
const { view, container } = createViewFromDoc(richSchema, doc);

triggerGotoLine(view);
const input = container.querySelector(".goto-line-input") as HTMLInputElement;
// 3 lines total: "foo", "bar", "baz"
expect(input.placeholder).toContain("3");
expect(input.max).toBe("3");

gotoLine(container, "2");
// First paragraph starts at pos 0, content at pos 1
// "foo" text size = 3, so hard_break starts at pos 4
// hard_break size = 1, so "bar" starts at pos 5
expect(view.state.selection.from).toBe(5);

triggerGotoLine(view);
gotoLine(container, "3");
// Second paragraph starts at pos 9, text "baz" at pos 10
expect(view.state.selection.from).toBe(10);
});
});

// ── Editor recreation (#718) ───────────────────────────────────────
Expand Down
21 changes: 17 additions & 4 deletions pkgs/id/web/src/goto-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,23 @@ function getLineStarts(view: EditorView): number[] {
if (!node.isTextblock) return true; // descend into block containers (lists, quotes, cells…)
const contentStart = pos + 1;
starts.push(contentStart);
const text = node.textContent;
for (let i = 0; i < text.length; i++) {
if (text[i] === "\n") starts.push(contentStart + i + 1);
}

let currentPos = contentStart;
node.forEach((child) => {
if (child.type.name === "hard_break") {
starts.push(currentPos + child.nodeSize);
} else if (child.isText) {
const text = child.text;
if (text) {
for (let i = 0; i < text.length; i++) {
if (text[i] === "\n") {
starts.push(currentPos + i + 1);
}
}
}
}
currentPos += child.nodeSize;
});
return false; // inline content cannot contain another textblock
});
return starts;
Expand Down
Loading