Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export class CodeBlockBehavior {
public readonly partResources: typeof CodeBlockResources = CodeBlockResources;
public readonly runStates: typeof ShellRunState = ShellRunState;
public readonly advanceRatio: WritableSignal<string | null> = signal(null);
public readonly scrollbarHeight: WritableSignal<string | null> = signal(null);
public readonly isCopied: Signal<boolean> = computed(() => this.copyState() === CopyState.Copied);
public readonly copyGlyph: Signal<IconName> = computed(() => Resources.copyGlyphs[this.copyState()]);
public readonly copyLabel: Signal<string> = computed(() => Resources.copyLabels[this.copyState()]);
Expand Down Expand Up @@ -122,11 +123,15 @@ export class CodeBlockBehavior {
afterNextRender(() => {
const measureAfterLayout = this.observers.create(() => {
this.measureAdvance();
this.measureScrollbar();
this.followEnd();
});
this.destroyRef.onDestroy(() => measureAfterLayout.disconnect());
for (const element of [this.elements.text(), this.elements.advanceOne(), this.elements.advanceRun()])
measureAfterLayout.observe(element.nativeElement);
// A sideways scrollbar that takes room grows the body's border box as the line starts to scroll, and shrinks it as
// the line stops, so the block measures the bar again each time.
measureAfterLayout.observe(this.elements.body().nativeElement, { box: "border-box" });
});
}

Expand Down Expand Up @@ -168,6 +173,13 @@ export class CodeBlockBehavior {
this.advanceRatio.set(String(this.elements.advanceOne().nativeElement.getBoundingClientRect().width * CodeBlockResources.codeAdvanceSample.length / run));
}

// The room the body's sideways scrollbar takes: none while the line fits, or where the scrollbar lies over the code, as
// a phone's does. The code's bottom padding gives up as much, so a block is as tall whether its line fits or scrolls.
private measureScrollbar(): void {
const body = this.elements.body().nativeElement;
this.scrollbarHeight.set(`${String(body.offsetHeight - body.clientHeight)}px`);
}

private followEnd(): void {
if (this.source.code().length > 0 && (this.grewStreaming || this.source.streaming()))
this.follower.follow(this.elements.body().nativeElement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
.nol-code-block-body {
margin: 0;
overflow-x: auto;
container-type: scroll-state;
font-family: var(--nol-font-mono);
font-size: var(--nol-text-code);
line-height: var(--nol-line-code);
Expand All @@ -66,6 +65,7 @@
width: max-content;
min-width: 100%;
padding: var(--nol-code-block-padding);
padding-bottom: max(0px, var(--nol-code-block-padding) - var(--nol-code-scrollbar-height, 0px));
Comment thread
rostislavrotaru marked this conversation as resolved.
Resolved
font: inherit;
letter-spacing: calc((var(--nol-code-advance-ratio, 1) - 1) * 1ch);
overflow: clip;
Expand Down Expand Up @@ -98,12 +98,6 @@
}
}

@container scroll-state(scrollable: inline) {
.nol-code-block-body > code {
padding-bottom: max(0px, var(--nol-code-block-padding) - var(--nol-scrollbar-size));
}
}

.nol-code-block-body > code::highlight(nol-code-comment) {
color: var(--nol-code-comment);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import { CodeBlockBehavior } from "./code-block-behavior.js";
"class": "nol-code-block",
"ngSkipHydration": "true",
"[class.nol-code-block-wrapped]": "wrapped()",
"[style.--nol-code-advance-ratio]": "view.advanceRatio()"
"[style.--nol-code-advance-ratio]": "view.advanceRatio()",
"[style.--nol-code-scrollbar-height]": "view.scrollbarHeight()"
}
})
export class CodeBlockComponent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ describe("CodeBlockComponent", () => {
range.selectNodeContents(find(".nol-code-block-body > code"));
const lines = [...range.getClientRects()];
const outer = body().getBoundingClientRect();
const scrollbar = body().scrollWidth > body().clientWidth ? AppearanceFixture.measureLook("scrollbar-size") - (body().offsetHeight - body().clientHeight) : 0;
const over = (lines[0] as DOMRect).top - outer.top;
const under = outer.bottom - (lines.at(-1) as DOMRect).bottom + scrollbar;
const under = outer.bottom - (lines.at(-1) as DOMRect).bottom;
const space = AppearanceFixture.toPixels(0.875);

expect([Math.abs(over - under) <= 0.5, Math.abs(over - space) <= 0.5, body().scrollHeight - body().clientHeight], `${over} px over, ${under} px under, ${space} px expected`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ describe("CodeBlockComponent", () => {
range.selectNodeContents(find(".nol-code-block-body > code"));
const lines = [...range.getClientRects()];
const outer = body().getBoundingClientRect();
const scrollbar = body().scrollWidth > body().clientWidth ? AppearanceFixture.measureLook("scrollbar-size") - (body().offsetHeight - body().clientHeight) : 0;
const over = (lines[0] as DOMRect).top - outer.top;
const under = outer.bottom - (lines.at(-1) as DOMRect).bottom + scrollbar;
const under = outer.bottom - (lines.at(-1) as DOMRect).bottom;
const space = AppearanceFixture.toPixels(0.875);

expect([Math.abs(over - under) <= 0.5, Math.abs(over - space) <= 0.5, body().scrollHeight - body().clientHeight], `${over} px over, ${under} px under, ${space} px expected`)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* @license
* Copyright (c) Noldova.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/

import { page } from "vitest/browser";

import { DefaultTheme } from "../../../../src/app/models/default-theme";
import { AppearanceFixture } from "../../../../src/testing/services/appearance-fixture";
import { CodeBlockHarness } from "../../../fixtures/code-block-harness.fixture";

// A one-line block's line, short enough to fit in a phone's column, and one that scrolls there.
const FITS = "sha256sum TeamRun-0.0.5-linux-x64.deb";
const SCROLLS = "Get-FileHash .\\TeamRun-0.0.5-windows-x64.exe -Algorithm SHA256";

// A classic sideways scrollbar's height, as the kit draws one. The test browser runs headless, where no scrollbar takes
// room, so its block measures none; a classic bar's case sets the measured height on the body as the block would.
const CLASSIC_BAR = 0.375;

describe("CodeBlockComponent", () => {
const harness = new CodeBlockHarness();
const { renderAsync, changeAsync, find, body, settledAsync } = harness;
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;

beforeEach(harness.configure);

afterEach(async () => {
await harness.resetAsync();
await page.viewport(windowWidth, windowHeight);
});

const rem = (value: number): number => AppearanceFixture.toPixels(value);
const bar = (): number => body().offsetHeight - body().clientHeight;
const height = (): number => find("nol-code-block").getBoundingClientRect().height;
const padding = (): number => Number.parseFloat(getComputedStyle(find(".nol-code-block-body > code")).paddingBottom);
const shape = (): string => JSON.stringify([height(), body().scrollWidth, body().clientWidth, bar(), padding()]);

async function renderLineAsync(width: number, mode: (typeof AppearanceFixture.modes)[number], code: string): Promise<void> {
await page.viewport(width, 800);
AppearanceFixture.apply(DefaultTheme.theme, mode);
await renderAsync();
await changeAsync(() => {
harness.host.width.set("calc(100vw - 2.5rem)");
harness.host.language.set(null);
harness.host.code.set(code);
});
await settledAsync("the line", shape);
}

for (const width of [375, 1280])
for (const mode of AppearanceFixture.modes)
it(`is as tall with one line that scrolls as with one that fits, where the scrollbar lies over the code, at ${String(width)} pixels, in ${mode.toLowerCase()} mode`, async () => {
await renderLineAsync(width, mode, FITS);
const fits = height();
const fitsBar = bar();
await changeAsync(() => harness.host.code.set(SCROLLS));
await settledAsync("the line that scrolls", shape);

expect([fitsBar, body().scrollWidth > body().clientWidth, bar()]).toEqual([0, width < 600, 0]);
AppearanceFixture.expectPixels(padding(), rem(0.875));
AppearanceFixture.expectPixels(height(), fits);
});

for (const mode of AppearanceFixture.modes)
it(`gives up a classic scrollbar's measured height from its bottom padding, so with the bar's room it is as tall as a block whose line fits, at 375 pixels, in ${mode.toLowerCase()} mode`, async () => {
await renderLineAsync(375, mode, FITS);
const fits = height();
await changeAsync(() => harness.host.code.set(SCROLLS));
body().style.setProperty("--nol-code-scrollbar-height", `${String(CLASSIC_BAR)}rem`);
await settledAsync("the line that scrolls over a classic bar", shape);

expect(body().scrollWidth).toBeGreaterThan(body().clientWidth);
AppearanceFixture.expectPixels(padding(), rem(0.875 - CLASSIC_BAR));
AppearanceFixture.expectPixels(height() + rem(CLASSIC_BAR), fits);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("CodeBlockComponent", () => {
afterEach(harness.resetAsync);

for (const language of [null, "TypeScript"])
it(`takes a sideways scrollbar's height from its bottom padding only while its lines scroll, and keeps its end padding once scrolled to the end, in ${language ?? "plain"} code`, async () => {
it(`takes the room its sideways scrollbar takes from its bottom padding only while its lines scroll, and keeps its end padding once scrolled to the end, in ${language ?? "plain"} code`, async () => {
AppearanceFixture.apply();
controlTokens(() => [new CodeToken(0, "const", CodeTokenKind.Keyword)]);
await renderAsync();
Expand All @@ -41,7 +41,7 @@ describe("CodeBlockComponent", () => {
expect(body().scrollWidth).toBeGreaterThan(body().clientWidth);
expect(body().scrollWidth).toBe(Math.round(code().left + code().width) - Math.round(code().left));
AppearanceFixture.expectPixels(code().right - end(), start);
AppearanceFixture.expectPixels(bottom(), AppearanceFixture.toPixels(0.875) - AppearanceFixture.measureLook("scrollbar-size"));
AppearanceFixture.expectPixels(bottom(), AppearanceFixture.toPixels(0.875) - (body().offsetHeight - body().clientHeight));

body().scrollLeft = body().scrollWidth;

Expand Down
Loading