-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solution Explanations #15
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ca4fa4e
fix: wip adding solutions/reset code to quizzes
kennethcassel 6e0472c
fix solution previewing
malerba118 3048354
don't render solution button if no solution
malerba118 a20bb6f
init explanation
malerba118 66bf2d7
progress
malerba118 d76834c
progress
malerba118 c27e02d
line selector
malerba118 59c6e2e
refactor some stuff
malerba118 9c8c66e
working out some kinks in editor
malerba118 448d568
tweaks
malerba118 6b4cffd
refactoring
malerba118 fe37cb9
tweaks
malerba118 c24fccf
preserve state across steps
malerba118 d5da01a
tweaks
malerba118 efaec29
fix build error
malerba118 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import React, { FC, HTMLProps, useEffect, useState } from "react"; | ||
|
||
interface ComplexInputProps<T> | ||
extends Omit<HTMLProps<HTMLInputElement>, "value" | "onChange"> { | ||
stringify: (val: T) => string; | ||
parse: (val: string) => T; | ||
value: T; | ||
onChange: (value: T) => void; | ||
} | ||
|
||
function ComplexInput<T>({ | ||
stringify, | ||
parse, | ||
value, | ||
onChange, | ||
...otherProps | ||
}: ComplexInputProps<T>) { | ||
const [draft, setDraft] = useState<string>(stringify(value)); | ||
|
||
useEffect(() => { | ||
setDraft(stringify(value)); | ||
}, [value]); | ||
|
||
const process = () => { | ||
let shouldNotify = false; | ||
let parsed; | ||
try { | ||
parsed = parse(draft); | ||
shouldNotify = true; | ||
} catch (err) { | ||
setDraft(stringify(value)); | ||
} | ||
if (shouldNotify) { | ||
onChange(parsed); | ||
} | ||
}; | ||
|
||
return ( | ||
<input | ||
{...otherProps} | ||
onBlur={() => process()} | ||
onKeyDown={(e) => { | ||
if (e.key === "Enter") { | ||
e.currentTarget.blur(); | ||
} | ||
otherProps.onKeyDown?.(e); | ||
}} | ||
value={draft} | ||
onChange={(e) => { | ||
setDraft(e.target.value); | ||
}} | ||
/> | ||
); | ||
} | ||
|
||
const stringify = (value: number[]) => { | ||
return JSON.stringify(value); | ||
}; | ||
|
||
const parse = (value: string) => { | ||
const parsed = JSON.parse(value); | ||
if (!Array.isArray(parsed)) { | ||
throw new Error("Should be array"); | ||
} | ||
if (parsed.some((x) => typeof x !== "number")) { | ||
throw new Error("Should contain only numbers"); | ||
} | ||
return parsed; | ||
}; | ||
|
||
interface NumberArrayInputProps | ||
extends Omit<HTMLProps<HTMLInputElement>, "value" | "onChange"> { | ||
value: number[]; | ||
onChange: (value: number[]) => void; | ||
} | ||
|
||
export const NumberArrayInput: FC<NumberArrayInputProps> = ({ | ||
value, | ||
onChange, | ||
...otherProps | ||
}) => { | ||
return ( | ||
<ComplexInput | ||
{...otherProps} | ||
stringify={stringify} | ||
parse={parse} | ||
value={value} | ||
onChange={onChange} | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { | ||
useLayoutEffect, | ||
useRef, | ||
FC, | ||
ReactNode, | ||
useEffect, | ||
Children, | ||
} from "react"; | ||
import { useMonaco } from "@monaco-editor/react"; | ||
import { useEditor } from "./Editor"; | ||
import ReactDOM from "react-dom"; | ||
import useForceUpdate from "../hooks/useForceUpdate"; | ||
|
||
interface ContentWidgetProps { | ||
children: any; | ||
line: number; | ||
widgetId: string; | ||
} | ||
|
||
/** | ||
* This component pins its children to a specific line in the editor. | ||
* It's good for adding annotations to the editor. | ||
* @param param0 | ||
* @returns | ||
*/ | ||
const ContentWidget: FC<ContentWidgetProps> = ({ | ||
widgetId, | ||
line, | ||
children, | ||
}) => { | ||
const monaco = useMonaco(); | ||
const editor = useEditor(); | ||
const ref = useRef(null); | ||
|
||
useLayoutEffect(() => { | ||
if (editor) { | ||
// Add a content widget (scrolls inline with text) | ||
var contentWidget = { | ||
domNode: null, | ||
allowEditorOverflow: true, | ||
getId: function () { | ||
return widgetId; | ||
}, | ||
getDomNode: function () { | ||
if (!ref.current) { | ||
ref.current = document.createElement("div"); | ||
ref.current.style.position = "relative"; | ||
ref.current.style.width = 0; | ||
ref.current.classList.add("content-widget-portal"); | ||
} | ||
return ref.current; | ||
}, | ||
afterRender: function () { | ||
ReactDOM.render( | ||
<div | ||
onMouseOver={(e) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
}} | ||
> | ||
{children} | ||
</div>, | ||
ref.current | ||
); | ||
}, | ||
getPosition: function () { | ||
return { | ||
position: { | ||
lineNumber: line, | ||
column: 0, | ||
}, | ||
preference: [monaco.editor.ContentWidgetPositionPreference.EXACT], | ||
}; | ||
}, | ||
}; | ||
editor.addContentWidget(contentWidget); | ||
return () => { | ||
editor.removeContentWidget(contentWidget); | ||
}; | ||
} | ||
}, [editor, children]); | ||
|
||
return null; | ||
}; | ||
|
||
export default ContentWidget; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually unused now, but i'm kinda tempted just to leave it in cause i think it's powerful. I was using it for manual array input in my first iteration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool with leavin this in!