-
Notifications
You must be signed in to change notification settings - Fork 354
/
Copy patharticle-renderer.tsx
264 lines (229 loc) · 9.14 KB
/
article-renderer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
* An article renderer. Articles are long-form pieces of content,
* composed of multiple (Renderer) sections concatenated together.
*/
import * as PerseusLinter from "@khanacademy/perseus-linter";
import classNames from "classnames";
import * as React from "react";
import {PerseusI18nContext} from "./components/i18n-context";
import {DependenciesContext, getDependencies} from "./dependencies";
import JiptParagraphs from "./jipt-paragraphs";
import {ClassNames as ApiClassNames, ApiOptions} from "./perseus-api";
import Renderer from "./renderer";
import Util from "./util";
import type {PerseusArticle, PerseusRenderer} from "./perseus-types";
import type {PerseusDependenciesV2, SharedRendererProps} from "./types";
import type {KeypadAPI} from "@khanacademy/math-input";
import type {KeypadContextRendererInterface} from "@khanacademy/perseus-core";
type Props = Partial<React.ContextType<typeof DependenciesContext>> &
SharedRendererProps & {
json: PerseusArticle;
// Whether to use the new Bibliotron styles for articles
/**
* @deprecated Does nothing
*/
useNewStyles: boolean;
legacyPerseusLint?: ReadonlyArray<string>;
keypadElement?: KeypadAPI | null | undefined;
dependencies: PerseusDependenciesV2;
};
type DefaultProps = {
apiOptions: Props["apiOptions"];
useNewStyles: Props["useNewStyles"];
linterContext: Props["linterContext"];
};
class ArticleRenderer
extends React.Component<Props>
implements KeypadContextRendererInterface
{
static contextType = PerseusI18nContext;
declare context: React.ContextType<typeof PerseusI18nContext>;
_currentFocus: any;
sectionRenderers: Array<Renderer> = [];
static defaultProps: DefaultProps = {
apiOptions: ApiOptions.defaults,
useNewStyles: false,
linterContext: PerseusLinter.linterContextDefault,
};
constructor(props: Props) {
super(props);
}
componentDidMount() {
this._currentFocus = null;
}
shouldComponentUpdate(nextProps: Props): boolean {
return nextProps !== this.props;
}
_handleFocusChange: (arg1: any, arg2: any) => void = (
newFocusPath,
oldFocusPath,
) => {
// TODO(charlie): DRY this up--some of this logic is repeated in
// ItemRenderer.
if (newFocusPath) {
this._setCurrentFocus(newFocusPath);
} else {
this._onRendererBlur(oldFocusPath);
}
};
_setCurrentFocus: (arg1: any) => void = (newFocusPath) => {
const {keypadElement, apiOptions} = this.props;
const {isMobile} = apiOptions;
const prevFocusPath = this._currentFocus;
this._currentFocus = newFocusPath;
// Use the section prefix to extract the relevant Renderer's input
// paths, so as to check whether the focused path represents an
// input.
let didFocusInput = false;
let focusedInput;
if (this._currentFocus) {
const [sectionIndex, ...focusPath] = this._currentFocus;
const inputPaths =
this.sectionRenderers[sectionIndex].getInputPaths();
didFocusInput = inputPaths.some((inputPath) => {
return Util.inputPathsEqual(inputPath, focusPath);
});
focusedInput =
this.sectionRenderers[sectionIndex].getDOMNodeForPath(
focusPath,
);
}
const {onFocusChange} = this.props.apiOptions;
if (onFocusChange) {
// Wait for the keypad to mount before getting the height
setTimeout(() => {
const keypadDomNode =
keypadElement?.getDOMNode() as HTMLElement;
const keypadHeight =
keypadDomNode && didFocusInput
? keypadDomNode.getBoundingClientRect().height
: 0;
onFocusChange(
this._currentFocus,
prevFocusPath,
keypadHeight,
didFocusInput ? focusedInput : null,
);
}, 0);
}
if (keypadElement && isMobile) {
if (didFocusInput) {
keypadElement.activate();
} else {
keypadElement.dismiss();
}
}
};
_onRendererBlur: (arg1: any) => void = (blurPath) => {
const blurringFocusPath = this._currentFocus;
// Failsafe: abort if ID is different, because focus probably happened
// before blur.
if (!Util.inputPathsEqual(blurPath, blurringFocusPath)) {
return;
}
// Wait until after any new focus events fire this tick before declaring
// that nothing is focused, since if there were a focus change across
// sections, we could receive the blur before the focus.
// TODO(jeff, CP-3128): Use Wonder Blocks Timing API
// eslint-disable-next-line no-restricted-syntax
setTimeout(() => {
if (Util.inputPathsEqual(this._currentFocus, blurringFocusPath)) {
this._setCurrentFocus(null);
}
});
};
blur: () => void = () => {
if (this._currentFocus) {
const [sectionIndex, ...inputPath] = this._currentFocus;
this.sectionRenderers[sectionIndex].blurPath(inputPath);
}
};
_sections: () => any = () => {
const sections = Array.isArray(this.props.json)
? this.props.json
: [this.props.json];
// In JIPT context we split sections to paragraphs in order match the
// translatable strings found on Crowdin when rendering articles in
// the WYSIWYG mode for translation. This is needed for the jipt.js
// integration in order to attribute the rendered strings on Crowdin.
const {JIPT} = getDependencies();
if (JIPT.useJIPT) {
const paragraphs: Array<PerseusRenderer> = [];
for (const section of sections) {
JiptParagraphs.parseToArray(section.content).forEach(
(paragraph) => {
paragraphs.push({
...section,
content: paragraph,
});
},
);
}
return paragraphs;
}
return sections;
};
render(): React.ReactNode {
const apiOptions = {
...ApiOptions.defaults,
...this.props.apiOptions,
isArticle: true,
} as const;
const classes = classNames({
"framework-perseus": true,
"perseus-article": true,
// NOTE(charlie): For exercises, this is applied outside of Perseus
// (in webapp).
[ApiClassNames.MOBILE]: apiOptions.isMobile,
});
// TODO(alex): Add mobile api functions and pass them down here
// We're using the index as the key here because we don't have a unique
// identifier for each section. This should be fine as we never remove
// or reorder sections.
const sections = this._sections().map((section, sectionIndex) => {
return (
<div key={sectionIndex} className="clearfix">
<Renderer
{...section}
ref={(elem) => {
if (elem) {
this.sectionRenderers[sectionIndex] = elem;
}
}}
key={sectionIndex}
key_={sectionIndex}
keypadElement={this.props.keypadElement}
apiOptions={{
...apiOptions,
onFocusChange: (newFocusPath, oldFocusPath) => {
// Prefix the paths with the relevant section index,
// so as to allow us to distinguish between
// equivalently-named inputs across Renderers.
this._handleFocusChange(
newFocusPath &&
[sectionIndex].concat(newFocusPath),
oldFocusPath &&
[sectionIndex].concat(oldFocusPath),
);
},
}}
linterContext={PerseusLinter.pushContextStack(
this.props.linterContext,
"article",
)}
legacyPerseusLint={this.props.legacyPerseusLint}
strings={this.context.strings}
/>
</div>
);
});
return (
<div className={classes}>
<DependenciesContext.Provider value={this.props.dependencies}>
{sections}
</DependenciesContext.Provider>
</div>
);
}
}
export default ArticleRenderer;