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
33 changes: 33 additions & 0 deletions src/common/markedText.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe, expect, it } from "vitest";
import { parseMarkedText } from "./markedText";

describe("parseMarkedText", () => {
it("cuts a text where it marks names, values and controls", () => {
expect(parseMarkedText("Turn {Start} [ON] and press <check>.")).toEqual([
{ type: "text", text: "Turn " },
{ type: "name", name: "Start" },
{ type: "text", text: " " },
{ type: "value", value: [true], notation: "onOff" },
{ type: "text", text: " and press " },
{ type: "control", control: "check" },
{ type: "text", text: "." },
]);
});

it("reads a value of 0s and 1s bit by bit", () => {
expect(parseMarkedText("[0011]")).toEqual([
{
type: "value",
value: [false, false, true, true],
notation: "digits",
},
]);
});

it.each(["<unknown>", "[2]", "{Start", "Start}"])(
"refuses a mark it does not know: %s",
(text) => {
expect(() => parseMarkedText(text)).toThrow();
},
);
});
28 changes: 15 additions & 13 deletions src/tutorial/stageText.ts → src/common/markedText.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
import type { BitNotation } from "../common/bitLabel";
import type { SimulationValue } from "../store/simulation";
import type { BitNotation } from "./bitLabel";

/** Things on the screen a stage text points at, drawn as they look there */
export const stageTextControls = [
/** Things on the screen a text points at, drawn as they look there */
export const markedTextControls = [
"check",
"constApply",
"pinApply",
"pinAdd",
"pinRemove",
"auto",
"play",
"edit",
"previousStep",
"nextStep",
"enter",
] as const;
export type StageTextControl = (typeof stageTextControls)[number];
export type MarkedTextControl = (typeof markedTextControls)[number];

export type StageTextSegment =
export type MarkedTextSegment =
| { type: "text"; text: string }
/** a pin or a part, by the name the editor shows */
| { type: "name"; name: string }
| { type: "value"; value: SimulationValue; notation: BitNotation }
| { type: "control"; control: StageTextControl };
| { type: "control"; control: MarkedTextControl };

function isControl(key: string): key is StageTextControl {
return stageTextControls.some((control) => control === key);
function isControl(key: string): key is MarkedTextControl {
return markedTextControls.some((control) => control === key);
}

function parseValue(token: string): StageTextSegment {
function parseValue(token: string): MarkedTextSegment {
if (token === "ON" || token === "OFF") {
return { type: "value", value: [token === "ON"], notation: "onOff" };
}
Expand All @@ -43,12 +45,12 @@ function parseValue(token: string): StageTextSegment {
const markup = /\{([^{}]+)\}|\[([^[\]]+)\]|<([A-Za-z]+)>/g;

/**
* A stage text cut where it marks a name as `{Start}`, a value as `[ON]` or `[0011]`,
* A text cut where it marks a name as `{Start}`, a value as `[ON]` or `[0011]`,
* or a control as `<check>`
* @throws if a mark is not one of these, so that a typo shows in the tests
*/
export function parseStageText(text: string): StageTextSegment[] {
const segments: StageTextSegment[] = [];
export function parseMarkedText(text: string): MarkedTextSegment[] {
const segments: MarkedTextSegment[] = [];
let rest = 0;
const pushText = (end: number) => {
const plain = text.slice(rest, end);
Expand All @@ -64,7 +66,7 @@ export function parseStageText(text: string): StageTextSegment[] {
else if (value !== undefined) segments.push(parseValue(value));
else if (control !== undefined && isControl(control)) {
segments.push({ type: "control", control });
} else throw new Error(`${whole} is not a control a stage text can show`);
} else throw new Error(`${whole} is not a control a text can show`);
rest = match.index + whole.length;
}
pushText(text.length);
Expand Down
14 changes: 14 additions & 0 deletions src/common/markedTextTestUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { type MarkedTextSegment, parseMarkedText } from "./markedText";

/** What a text marks other than names, which a language may translate */
export function marksOf(text: string) {
return parseMarkedText(text)
.flatMap((segment: MarkedTextSegment) => {
if (segment.type === "value") {
return [segment.value.map((bit) => (bit ? 1 : 0)).join("")];
}
if (segment.type === "control") return [`<${segment.control}>`];
return [];
})
.toSorted();
}
43 changes: 29 additions & 14 deletions src/pages/tutorial/StageText.tsx → src/components/MarkedText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@ import {
} from "@mui/icons-material";
import { Box, Button, type ButtonProps, ToggleButton } from "@mui/material";
import type { ReactNode } from "react";
import {
type MarkedTextControl,
type MarkedTextSegment,
parseMarkedText,
} from "../common/markedText";
import { useMessages } from "../i18n";
import {
autoToggleButtonLook,
checkButtonLook,
pinApplyButtonLook,
pinFieldButtonLook,
valueApplyButtonLook,
withLookSx,
} from "../../components/controlLooks";
import { ValueView } from "../../components/ValueView";
import { visuallyHiddenSx } from "../../components/visuallyHidden";
import { useMessages } from "../../i18n";
import {
parseStageText,
type StageTextControl,
type StageTextSegment,
} from "../../tutorial/stageText";
} from "./controlLooks";
import { ValueView } from "./ValueView";
import { visuallyHiddenSx } from "./visuallyHidden";

// The real controls with their looks, smaller and not to be pressed. The label is read
// out from a hidden copy, as the control itself is hidden from screen readers.
Expand Down Expand Up @@ -77,7 +78,7 @@ function InlineButton({ look, label }: { look: ButtonProps; label: string }) {
);
}

function ControlView({ control }: { control: StageTextControl }) {
function ControlView({ control }: { control: MarkedTextControl }) {
const messages = useMessages();
const roundIcon = (icon: ReactNode) => (
<Box
Expand Down Expand Up @@ -128,6 +129,20 @@ function ControlView({ control }: { control: StageTextControl }) {
return (
<InlineButton look={pinApplyButtonLook} label={messages.common.apply} />
);
case "pinAdd":
return (
<InlineButton
look={pinFieldButtonLook}
label={messages.editor.nodePinBitWidth.add}
/>
);
case "pinRemove":
return (
<InlineButton
look={pinFieldButtonLook}
label={messages.editor.nodePinBitWidth.remove}
/>
);
case "auto":
return (
<InlineControl label={messages.editor.frameControl.auto}>
Expand Down Expand Up @@ -171,7 +186,7 @@ function ControlView({ control }: { control: StageTextControl }) {
}
}

function SegmentView({ segment }: { segment: StageTextSegment }) {
function SegmentView({ segment }: { segment: MarkedTextSegment }) {
switch (segment.type) {
case "text":
return segment.text;
Expand All @@ -194,9 +209,9 @@ function SegmentView({ segment }: { segment: StageTextSegment }) {
}
}

/** A stage text with the names, values and controls it marks drawn as on the screen */
export function StageText({ text }: { text: string }) {
return parseStageText(text).map((segment, index) => (
/** A text with the names, values and controls it marks drawn as on the screen */
export function MarkedText({ text }: { text: string }) {
return parseMarkedText(text).map((segment, index) => (
// biome-ignore lint/suspicious/noArrayIndexKey: the segments of a text keep their order
<SegmentView key={index} segment={segment} />
));
Expand Down
75 changes: 59 additions & 16 deletions src/components/PartDescription.test.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,77 @@
import { describe, expect, it } from "vitest";
import { messagesByLanguage } from "../i18n/messages";
import { parseMarkedText } from "../common/markedText";
import { marksOf } from "../common/markedTextTestUtils";
import { type Language, languages, messagesByLanguage } from "../i18n/messages";
import {
and,
and_n,
broadcast,
decompose,
definitions,
display,
} from "../store/intrinsics/definitions";
import { pinNotesOf } from "./PartDescription";

const { en, ja } = messagesByLanguage;
const allDefinitions = Object.values(definitions);

/** Each text describing a part, with the part it describes */
function textsOf(language: Language) {
const messages = messagesByLanguage[language];
return allDefinitions.flatMap((definition) =>
[
messages.parts.summaries[definition.type],
...pinNotesOf(messages, definition),
].map((text) => ({ definition, text })),
);
}

describe("pinNotesOf", () => {
it("tells how to set a pin whose bit width the player chooses", () => {
expect(pinNotesOf(en, broadcast)).toEqual([
'The number in the Out pin is its number of bits. To change it, click the pin while editing, change the field and press "Apply".',
]);
});
it.each(languages)(
"tells in %s to press Apply after setting a pin's bit width",
(language) => {
expect(
pinNotesOf(messagesByLanguage[language], broadcast).map(marksOf),
).toEqual([["<pinApply>"]]);
},
);

it("tells how to add pins where a pin can be split", () => {
expect(pinNotesOf(en, and_n)).toEqual([
'The number in the In pin is its number of bits. To change it, click the pin while editing, change the field and press "Apply". Each field added with "Add" becomes another In pin.',
]);
expect(pinNotesOf(ja, decompose)).toEqual([
"Out のピンの中の数字は、そのピンのビット数。編集中にピンをクリックし、出てきた欄で変えて「適用」を押す。「追加」で欄を増やすと、Out のピンも増える。",
]);
});
it.each(languages)(
"tells in %s how to add and remove pins where a pin can be split",
(language) => {
expect(
pinNotesOf(messagesByLanguage[language], decompose).map(marksOf),
).toEqual([["<pinAdd>", "<pinApply>", "<pinRemove>"]]);
},
);

it("says nothing of pins whose bit widths follow from elsewhere", () => {
const { en } = messagesByLanguage;
expect(pinNotesOf(en, and)).toEqual([]);
expect(pinNotesOf(en, display)).toEqual([]);
});
});

describe("the part descriptions", () => {
it("mark the same values and controls in every language", () => {
const english = textsOf("en");
const japanese = textsOf("ja");
expect(japanese).toHaveLength(english.length);
for (const [index, { text }] of english.entries()) {
expect(marksOf(japanese[index]?.text ?? "")).toEqual(marksOf(text));
}
});

it.each(languages)("name only the part and its pins in %s", (language) => {
const strangers = textsOf(language).flatMap(({ definition, text }) => {
const known = new Set([
definition.name,
...definition.allPins.map((pin) => pin.name),
]);
return parseMarkedText(text).flatMap((segment) =>
segment.type === "name" && !known.has(segment.name)
? [`${definition.name}: ${segment.name}`]
: [],
);
});
expect(strangers).toEqual([]);
});
});
9 changes: 7 additions & 2 deletions src/components/PartDescription.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { CCComponent } from "../store/component";
import { IntrinsicComponentDefinition } from "../store/intrinsics/base";
import { definitionByComponentId } from "../store/intrinsics/definitions";
import { type TruthTable, truthTableOf } from "../store/intrinsics/truthTable";
import { MarkedText } from "./MarkedText";
import { TruthTableView } from "./TruthTableView";

export function partSummary(
Expand Down Expand Up @@ -62,10 +63,14 @@ export function PartDescriptionView({
>
{name}
</Typography>
{summary && <Typography variant="body2">{summary}</Typography>}
{summary && (
<Typography variant="body2">
<MarkedText text={summary} />
</Typography>
)}
{notes.map((note) => (
<Typography key={note} variant="body2" sx={{ mt: 0.5 }}>
{note}
<MarkedText text={note} />
</Typography>
))}
{table && (
Expand Down
8 changes: 7 additions & 1 deletion src/components/controlLooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
} from "@mui/material";
import { theme } from "../style";

// The looks of controls a stage text draws too, so that the two cannot drift apart
// The looks of controls a marked text draws too, so that the two cannot drift apart

export const checkButtonLook = {
variant: "contained",
Expand All @@ -24,6 +24,12 @@ export const pinApplyButtonLook = {
size: "small",
} as const satisfies ButtonProps;

/** The look of "Add" and "Remove" in the pin editor */
export const pinFieldButtonLook = {
variant: "outlined",
size: "small",
} as const satisfies ButtonProps;

export const autoToggleButtonLook = {
size: "small",
} as const satisfies Partial<ToggleButtonProps>;
Expand Down
Loading
Loading