Skip to content

Commit

Permalink
Fix up JIPT type in depenedencies so when disabled we don't have to p…
Browse files Browse the repository at this point in the history
…rovide JIPT-support features
  • Loading branch information
jeremywiebe committed Dec 18, 2023
1 parent d6afb8c commit 71576ef
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
6 changes: 4 additions & 2 deletions packages/perseus/src/components/graphie-movables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,14 @@ const Label: any = GraphieClasses.createSimpleClass((graphie, props) => {
coord = graphie.unscalePoint(coord);
}
let elem = null;
const deps = getDependencies();

// If the label is rendered for a locale other than "en", push the label
// element to an array. This array is used to lookup the label element
// and processed with jipt('just in place translation', crowdin specific
// program) to replace the passed in crowdin string with the translated
// string. For "en" locale, the jipt processing is skipped.
if (getDependencies().JIPT.useJIPT) {
if (deps.JIPT.useJIPT) {
elem = graphie.label(
coord,
props.text,
Expand All @@ -139,7 +141,7 @@ const Label: any = GraphieClasses.createSimpleClass((graphie, props) => {
props.style,
);

getDependencies().graphieMovablesJiptLabels.addLabel(elem, props.tex);
deps.JIPT.graphieMovablesJiptLabels.addLabel(elem, props.tex);
} else {
elem = graphie.label(
coord,
Expand Down
5 changes: 1 addition & 4 deletions packages/perseus/src/components/svg-image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,7 @@ class SvgImage extends React.Component<Props, State> {
false,
);

getDependencies().svgImageJiptLabels.addLabel(
elem,
labelData.typesetAsMath,
);
JIPT.svgImageJiptLabels.addLabel(elem, labelData.typesetAsMath);
} else if (labelData.coordinates) {
// Create labels from the data
// TODO(charlie): Some erroneous labels are being sent down
Expand Down
12 changes: 6 additions & 6 deletions packages/perseus/src/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -427,13 +427,14 @@ class Renderer extends React.Component<Props, State> {
// widgetIds and the child refs of the renderer.
// See: https://phabricator.khanacademy.org/D32420.)
this.widgetIds = [];
const {JIPT} = getDependencies();

if (this.translationIndex != null) {
if (this.translationIndex != null && JIPT.useJIPT) {
// NOTE(jeremy): Since the translationIndex is simply the array
// index of each renderer, we can't remove Renderers from this
// list, rather, we simply null out the entry (which means that
// this array's growth is unbounded until a page reload).
getDependencies().rendererTranslationComponents.removeComponentAtIndex(
JIPT.rendererTranslationComponents.removeComponentAtIndex(
this.translationIndex,
);
}
Expand Down Expand Up @@ -1879,11 +1880,10 @@ class Renderer extends React.Component<Props, State> {
// before jipt has a chance to replace its contents, so this check
// will keep us from adding the component to the registry a second
// time.
if (!this.translationIndex) {
const {JIPT} = getDependencies();
if (!this.translationIndex && JIPT.useJIPT) {
this.translationIndex =
getDependencies().rendererTranslationComponents.addComponent(
this,
);
JIPT.rendererTranslationComponents.addComponent(this);
}

// For articles, we add jipt data to individual paragraphs. For
Expand Down
22 changes: 14 additions & 8 deletions packages/perseus/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,16 @@ export type DomInsertCheckFn = (
jiptString?: string,
) => string | false;

export type JIPT = {
useJIPT: boolean;
};
export type JIPT =
| {
useJIPT: false;
}
| {
useJIPT: true;
graphieMovablesJiptLabels: JiptLabelStore;
svgImageJiptLabels: JiptLabelStore;
rendererTranslationComponents: JiptTranslationComponents;
};

export type JiptLabelStore = {
addLabel: (label?: any, useMath?: any) => void;
Expand Down Expand Up @@ -303,15 +310,14 @@ export type VideoKind = "YOUTUBE_ID" | "READABLE_ID";
// could be used. Aim to shrink the footprint of PerseusDependencies and try to
// use alternative methods where possible.
export type PerseusDependencies = {
// JIPT
JIPT: JIPT;
graphieMovablesJiptLabels: JiptLabelStore;
svgImageJiptLabels: JiptLabelStore;
rendererTranslationComponents: JiptTranslationComponents;

TeX: React.ComponentType<TeXProps>;

//misc
// misc
// Provides a function to transform a relative or absolute URL into a
// request to a static hosting service (think something like an S3 or GCS
// bucket).
staticUrl: StaticUrlFn;
InitialRequestUrl: InitialRequestUrlInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ describe("graded-group-set", () => {
...testDependencies,
JIPT: {
useJIPT: true,
graphieMovablesJiptLabels: {
addLabel: (label, useMath) => {},
},
svgImageJiptLabels: {
addLabel: (label, useMath) => {},
},
rendererTranslationComponents: {
addComponent: (renderer) => 0,
removeComponentAtIndex: (index) => undefined,
},
},
});
});
Expand Down

0 comments on commit 71576ef

Please sign in to comment.