MCPcopy Create free account
hub / github.com/freeCodeCamp/freeCodeCamp / Editor

Function Editor

client/src/templates/Challenges/classic/editor.tsx:319–1505  ·  view source on GitHub ↗
(props: EditorProps)

Source from the content-addressed store, hash-verified

317});
318
319const Editor = (props: EditorProps): JSX.Element => {
320 const { t } = useTranslation();
321 const {
322 editorRef,
323 initTests,
324 resetAttempts,
325 isMobileLayout,
326 challengeFiles,
327 fileKey
328 } = props;
329 // These refs are used during initialisation of the editor as well as by
330 // callbacks. Since they have to be initialised before editorWillMount and
331 // editorDidMount are called, we cannot use useState. Reason being that will
332 // only take effect during the next render, which is too late. We could use
333 // plain objects here, but useRef is shared between instances, so avoids
334 // unnecessary object creation.
335 // Each Editor instance (e.g. per file in MultifileEditor) must get its own
336 // nested state; a shallow copy would share refs across instances and cause
337 // "element was detached" and wrong-editor behavior.
338 const monacoRef: MutableRefObject<typeof monacoEditor | null> =
339 useRef<typeof monacoEditor>(null);
340 const dataRef = useRef<EditorState>(createInitialEditorState());
341 const [lowerJawContainer, setLowerJawContainer] =
342 React.useState<HTMLDivElement | null>(null);
343
344 const submitChallenge = useSubmit();
345
346 const detachUpperJawEventsRef = useRef<(() => void) | null>(null);
347 const player = useRef<{
348 // eslint-disable-next-line @typescript-eslint/no-explicit-any
349 sampler: any;
350 noteIndex: number;
351 shouldPlay: boolean | undefined;
352 }>({
353 sampler: undefined,
354 noteIndex: 0,
355 shouldPlay: store.get('fcc-sound') as boolean | undefined
356 });
357
358 // since editorDidMount runs once with the initial props object, it keeps a
359 // reference to *those* props. If we want it to use the latest props, we can
360 // use a ref, since it will be updated on every render.
361 const testRef = useRef<Test[]>([]);
362 testRef.current = props.tests;
363 const attemptsRef = useRef<number>(0);
364 attemptsRef.current = props.attempts;
365
366 useEffect(() => {
367 return () => {
368 detachUpperJawEventsRef.current?.();
369 detachUpperJawEventsRef.current = null;
370 };
371 }, []);
372
373 const challengeFile = challengeFiles?.find(
374 challengeFile => challengeFile.fileKey === fileKey
375 );
376

Callers

nothing calls this directly

Calls 15

useTranslationFunction · 0.90
useSubmitFunction · 0.90
getScrollbarWidthFunction · 0.90
createInitialEditorStateFunction · 0.85
resetEditorValuesFunction · 0.85
focusIfTargetEditorFunction · 0.85
hasEditableRegionFunction · 0.85
initializeRegionsFunction · 0.85
addWidgetsToRegionsFunction · 0.85
updateDescriptionZoneFunction · 0.85
showEditableRegionFunction · 0.85
resetMarginDecorationsFunction · 0.85

Tested by

no test coverage detected