Skip to content

Commit

Permalink
Update types based on PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Myranae committed Dec 10, 2024
1 parent c6f7d86 commit 1579702
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import * as React from "react";

import LabelImageEditor from "../label-image-editor";

import type {MarkerAnswers} from "@khanacademy/perseus";
import type {
LabelImageMarker,
LabelImageMarkerScoringData,
} from "@khanacademy/perseus";

type StoryArgs = Record<any, any>;

Expand All @@ -29,7 +32,7 @@ type State = {
imageUrl: string;
imageWidth: number;
imageHeight: number;
markers: ReadonlyArray<MarkerAnswers>;
markers: ReadonlyArray<LabelImageMarker & LabelImageMarkerScoringData>;
};

class WithState extends React.Component<Empty, State> {
Expand Down
13 changes: 9 additions & 4 deletions packages/perseus-editor/src/widgets/label-image-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import Behavior from "./label-image/behavior";
import QuestionMarkers from "./label-image/question-markers";
import SelectImage from "./label-image/select-image";

import type {MarkerAnswers} from "@khanacademy/perseus";
import type {
LabelImageMarker,
LabelImageMarkerScoringData,
} from "@khanacademy/perseus";

type Props = {
// List of answer choices to label question image with.
Expand All @@ -28,7 +31,7 @@ type Props = {
imageWidth: number;
imageHeight: number;
// The list of label markers on the question image.
markers: ReadonlyArray<MarkerAnswers>;
markers: ReadonlyArray<LabelImageMarker & LabelImageMarkerScoringData>;
// Whether multiple answer choices may be selected for markers.
multipleAnswers: boolean;
// Whether to hide answer choices from user instructions.
Expand Down Expand Up @@ -176,8 +179,10 @@ class LabelImageEditor extends React.Component<Props> {
this.props.onChange({choices});
};

handleMarkersChange: (markers: ReadonlyArray<MarkerAnswers>) => void = (
markers: ReadonlyArray<MarkerAnswers>,
handleMarkersChange: (
markers: ReadonlyArray<LabelImageMarker & LabelImageMarkerScoringData>,
) => void = (
markers: ReadonlyArray<LabelImageMarker & LabelImageMarkerScoringData>,
) => {
this.props.onChange({markers});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import * as React from "react";

import QuestionMarkers from "../question-markers";

import type {MarkerAnswers} from "@khanacademy/perseus";
import type {
LabelImageMarker,
LabelImageMarkerScoringData,
} from "@khanacademy/perseus";

type StoryArgs = Record<any, any>;

Expand Down Expand Up @@ -31,7 +34,7 @@ const Wrapper = (props) => (
class WithState extends React.Component<
Record<any, any>,
{
markers: ReadonlyArray<MarkerAnswers>;
markers: ReadonlyArray<LabelImageMarker & LabelImageMarkerScoringData>;
}
> {
state = {
Expand Down
26 changes: 16 additions & 10 deletions packages/perseus-editor/src/widgets/label-image/marker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,22 @@ import Option, {OptionGroup} from "../../components/dropdown-option";
import FormWrappedTextField from "../../components/form-wrapped-text-field";
import {gray17, gray85, gray98} from "../../styles/global-colors";

import type {MarkerAnswers} from "@khanacademy/perseus";

type Props = MarkerAnswers & {
// The list of possible answer choices.
choices: ReadonlyArray<string>;
// Callback for when any of the marker props are changed.
onChange: (marker: MarkerAnswers) => void;
// Callback to remove marker from the question image.
onRemove: () => void;
};
import type {
LabelImageMarker,
LabelImageMarkerScoringData,
} from "@khanacademy/perseus";

type Props = LabelImageMarker &
LabelImageMarkerScoringData & {
// The list of possible answer choices.
choices: ReadonlyArray<string>;
// Callback for when any of the marker props are changed.
onChange: (
marker: LabelImageMarker & LabelImageMarkerScoringData,
) => void;
// Callback to remove marker from the question image.
onRemove: () => void;
};

type State = {
// Whether answer choices dropdown is shown, controlled by the user clicking
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
* label image with answers from list of choices.
*/

import {Util, Dependencies, bodyXsmallBold} from "@khanacademy/perseus";
import {
Util,
Dependencies,
bodyXsmallBold,
type LabelImageMarker,
type LabelImageMarkerScoringData,
} from "@khanacademy/perseus";
import {StyleSheet, css} from "aphrodite";
import * as React from "react";

import {gray17, gray68} from "../../styles/global-colors";

import Marker from "./marker";

import type {MarkerAnswers} from "@khanacademy/perseus";

type Props = {
// The list of possible answers in a specific order.
choices: ReadonlyArray<string>;
Expand All @@ -21,9 +25,11 @@ type Props = {
imageWidth: number;
imageHeight: number;
// The list of markers placed on the question image.
markers: ReadonlyArray<MarkerAnswers>;
markers: ReadonlyArray<LabelImageMarker & LabelImageMarkerScoringData>;
// Callback for when any of markers change.
onChange: (markers: ReadonlyArray<MarkerAnswers>) => void;
onChange: (
markers: ReadonlyArray<LabelImageMarker & LabelImageMarkerScoringData>,
) => void;
};

export default class QuestionMarkers extends React.Component<Props> {
Expand Down
5 changes: 4 additions & 1 deletion packages/perseus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ export type {
} from "./perseus-types";
export type {UserInputMap} from "./validation.types";
export type {Coord} from "./interactive2/types";
export type {MarkerAnswers} from "./widgets/label-image/types";
export type {
LabelImageMarker,
LabelImageMarkerScoringData,
} from "./widgets/label-image/types";

/**
* Multi-items
Expand Down
7 changes: 5 additions & 2 deletions packages/perseus/src/perseus-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// TODO(FEI-4011): Use types generated by https://github.com/jaredly/generate-perseus-flowtypes

import type {Coord} from "./interactive2/types";
import type {MarkerAnswers} from "./widgets/label-image/types";
import type {
LabelImageMarker,
LabelImageMarkerScoringData,
} from "./widgets/label-image/types";
import type {Interval, vec} from "mafs";

// Range is replaced within this file with Interval, but it is used elsewhere
Expand Down Expand Up @@ -1051,7 +1054,7 @@ export type PerseusLabelImageWidgetOptions = {
// The width of the image
imageWidth: number;
// A list of markers to display on the image
markers: ReadonlyArray<MarkerAnswers>;
markers: ReadonlyArray<LabelImageMarker & LabelImageMarkerScoringData>;
// Do not display answer choices in instructions
hideChoicesFromInstructions: boolean;
// Allow multiple answers per marker
Expand Down
9 changes: 5 additions & 4 deletions packages/perseus/src/validation.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ import type {
PerseusGraphCorrectType,
} from "./perseus-types";
import type {
InteractiveMarkerType,
MarkerAnswers,
LabelImageMarker,
LabelImageMarkerScoringData,
LabelImageMarkerUserInput,
} from "./widgets/label-image/types";
import type {Relationship} from "./widgets/number-line/number-line";

Expand Down Expand Up @@ -142,11 +143,11 @@ export type PerseusInteractiveGraphRubric = {
export type PerseusInteractiveGraphUserInput = PerseusGraphType;

export type PerseusLabelImageScoringData = {
markers: ReadonlyArray<MarkerAnswers>;
markers: ReadonlyArray<LabelImageMarker & LabelImageMarkerScoringData>;
};

export type PerseusLabelImageUserInput = {
markers: ReadonlyArray<Omit<InteractiveMarkerType, "answers">>;
markers: ReadonlyArray<LabelImageMarker & LabelImageMarkerUserInput>;
};

export type PerseusMatcherRubric = PerseusMatcherWidgetOptions;
Expand Down
4 changes: 2 additions & 2 deletions packages/perseus/src/widgets/label-image/label-image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {HideAnswersToggle} from "./hide-answers-toggle";
import Marker from "./marker";
import scoreLabelImage, {scoreMarker} from "./score-label-image";

import type {InteractiveMarkerType} from "./types";
import type {LabelImageFullMarker} from "./types";
import type {DependencyProps} from "../../dependencies";
import type {ChangeableProps} from "../../mixins/changeable";
import type {PerseusLabelImageWidgetOptions} from "../../perseus-types";
Expand Down Expand Up @@ -75,7 +75,7 @@ type LabelImageProps = ChangeableProps &
Omit<PerseusLabelImageWidgetOptions, "markers"> & {
apiOptions: APIOptions;
// The list of label markers on the question image.
markers: ReadonlyArray<InteractiveMarkerType>;
markers: ReadonlyArray<LabelImageFullMarker>;
// Whether the question has been answered by the user.
questionCompleted: boolean;
// preferred placement for popover (preference, not MUST)
Expand Down
4 changes: 2 additions & 2 deletions packages/perseus/src/widgets/label-image/marker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import {iconCheck, iconChevronDown, iconMinus} from "../../icon-paths";

import {AnswerPill} from "./answer-pill";

import type {InteractiveMarkerType} from "./types";
import type {LabelImageFullMarker} from "./types";
import type {IconType} from "../../components/icon";
import type {AnalyticsEventHandlerFn} from "@khanacademy/perseus-core";
import type {CSSProperties} from "aphrodite";

type Props = InteractiveMarkerType & {
type Props = LabelImageFullMarker & {
// Whether this marker has been selected by user.
showSelected: boolean;
// Whether this marker should pulsate to draw user attention.
Expand Down
4 changes: 2 additions & 2 deletions packages/perseus/src/widgets/label-image/score-label-image.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {InteractiveMarkerType} from "./types";
import type {LabelImageFullMarker} from "./types";
import type {PerseusScore} from "../../types";
import type {
PerseusLabelImageScoringData,
Expand All @@ -14,7 +14,7 @@ type InteractiveMarkerScore = {
};

export function scoreMarker(
marker: InteractiveMarkerType,
marker: LabelImageFullMarker,
): InteractiveMarkerScore {
const score = {
hasAnswers: false,
Expand Down
22 changes: 13 additions & 9 deletions packages/perseus/src/widgets/label-image/types.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
// Base marker, with the props that are set by the editor.
export type MarkerAnswers = {
// The list of correct answers expected for the marker. Often only one but can have multiple
answers: ReadonlyArray<string>;
export type LabelImageMarker = {
// Reveal the correctness state of the user selected answers for the marker.
showCorrectness?: "correct" | "incorrect";
focused?: boolean;
// Translatable Text; The text to show for the marker. Not displayed directly to the user
label: string;
// The marker coordinates on the question image as percent of image size.
x: number;
y: number;
};

export type MarkerUserInput = {
export type LabelImageMarkerScoringData = {
// The list of correct answers expected for the marker. Often only one but can have multiple
answers: ReadonlyArray<string>;
};

export type LabelImageMarkerUserInput = {
// The user selected list of answers, used to grade the question.
selected?: ReadonlyArray<string>;
// Reveal the correctness state of the user selected answers for the marker.
showCorrectness?: "correct" | "incorrect";
focused?: boolean;
};

// Additional props that are set when user interacts with the marker.
export type InteractiveMarkerType = MarkerAnswers & MarkerUserInput;
export type LabelImageFullMarker = LabelImageMarker &
LabelImageMarkerScoringData &
LabelImageMarkerUserInput;

0 comments on commit 1579702

Please sign in to comment.