Skip to content

Commit 8cfbcb2

Browse files
remo5000ning-y
authored andcommitted
Fix playground links making editor un-editable (#134)
* Fix links making playground uneditable The parsed program is now updated only once, when the page loads, rather than when the component is updated. * Revert "Fix links making playground uneditable" This reverts commit 599db0f. * Standardize parameters for workspace Now, we can pass in only a value and an optional library number. This allows the Playground to handle parsing of the library program. If there is a program passed in via the hash, the parsed program will be passed. If not, the parsed program will be undefind, causing the redux store value to be passed (using the `||` operator). * Add test for playground with link Also updated the old test snapshot * Bump version 0.1.1 -> 0.1.2
1 parent 6e93ec9 commit 8cfbcb2

File tree

5 files changed

+24
-20
lines changed

5 files changed

+24
-20
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "cadet-frontend",
4-
"version": "0.1.1",
4+
"version": "0.1.2",
55
"scripts-info": {
66
"format": "Format source code",
77
"start": "Start the Webpack development server",

src/components/Playground.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Text } from '@blueprintjs/core'
22
import { IconNames } from '@blueprintjs/icons'
3+
import { decompressFromEncodedURIComponent } from 'lz-string'
34
import * as qs from 'query-string'
45
import * as React from 'react'
56
import { HotKeys } from 'react-hotkeys'
@@ -36,9 +37,8 @@ class Playground extends React.Component<IPlaygroundProps, PlaygroundState> {
3637
handlers={this.handlers}
3738
>
3839
<WorkspaceContainer
39-
libQuery={parseLibrary(this.props)}
40-
prgrmQuery={parsePrgrm(this.props)}
41-
editorValue={this.props.editorValue}
40+
library={parseLibrary(this.props)}
41+
editorValue={parsePrgrm(this.props) || this.props.editorValue}
4242
sideContentTabs={[playgroundIntroduction]}
4343
/>
4444
</HotKeys>
@@ -53,7 +53,8 @@ class Playground extends React.Component<IPlaygroundProps, PlaygroundState> {
5353
const parsePrgrm = (props: IPlaygroundProps) => {
5454
const qsParsed = qs.parse(props.location.hash)
5555
// legacy support
56-
return qsParsed.lz !== undefined ? qsParsed.lz : qsParsed.prgrm
56+
const program = qsParsed.lz !== undefined ? qsParsed.lz : qsParsed.prgrm
57+
return program !== undefined ? decompressFromEncodedURIComponent(program) : undefined
5758
}
5859

5960
const parseLibrary = (props: IPlaygroundProps) => {

src/components/__tests__/Playground.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,13 @@ test('Playground renders correctly', () => {
1313
const tree = shallow(app)
1414
expect(tree.debug()).toMatchSnapshot()
1515
})
16+
17+
test('Playground with link renders correctly', () => {
18+
const props = {
19+
...mockRouterProps('/playground#lib=2&prgrm=CYSwzgDgNghgngCgOQAsCmUoHsCESCUA3EA', {}),
20+
editorValue: 'This should not show up'
21+
}
22+
const app = <Playground {...props} />
23+
const tree = shallow(app)
24+
expect(tree.debug()).toMatchSnapshot()
25+
})

src/components/__tests__/__snapshots__/Playground.tsx.snap

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
exports[`Playground renders correctly 1`] = `
44
"<HotKeys className=\\"Playground pt-dark\\" keyMap={{...}} handlers={{...}}>
5-
<Connect(Workspace) libQuery={[undefined]} prgrmQuery={[undefined]} editorValue=\\"Test value\\" sideContentTabs={{...}} />
5+
<Connect(Workspace) library={[undefined]} editorValue=\\"Test value\\" sideContentTabs={{...}} />
6+
</HotKeys>"
7+
`;
8+
9+
exports[`Playground with link renders correctly 1`] = `
10+
"<HotKeys className=\\"Playground pt-dark\\" keyMap={{...}} handlers={{...}}>
11+
<Connect(Workspace) library={2} editorValue=\\"display(\\\\'hello!\\\\');\\" sideContentTabs={{...}} />
612
</HotKeys>"
713
`;

src/components/workspace/index.tsx

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { decompressFromEncodedURIComponent } from 'lz-string'
21
import Resizable, { ResizableProps, ResizeCallback } from 're-resizable'
32
import * as React from 'react'
43

@@ -22,8 +21,7 @@ export type DispatchProps = {
2221

2322
export type OwnProps = {
2423
controlBarOptions?: ControlBarOwnProps
25-
libQuery?: number
26-
prgrmQuery?: string
24+
library?: number
2725
editorValue?: string
2826
mcq?: IMCQQuestion
2927
sideContentTabs: SideContentTab[]
@@ -41,20 +39,9 @@ class Workspace extends React.Component<WorkspaceProps, {}> {
4139
private sideDividerDiv: HTMLDivElement
4240

4341
public componentDidMount() {
44-
this.componentDidUpdate()
4542
this.maxDividerHeight = this.sideDividerDiv.clientHeight
4643
}
4744

48-
public componentDidUpdate() {
49-
if (this.props.prgrmQuery !== undefined) {
50-
const prgrmParsed = decompressFromEncodedURIComponent(this.props.prgrmQuery)
51-
this.props.updateEditorValue(prgrmParsed)
52-
}
53-
if (this.props.libQuery !== undefined) {
54-
this.props.changeChapter(this.props.libQuery)
55-
}
56-
}
57-
5845
/**
5946
* side-content-divider gives the side content a bottom margin. I use a div
6047
* element instead of CSS so that when the user resizes the side-content all

0 commit comments

Comments
 (0)