Skip to content

Commit cbeeb8e

Browse files
ccyccyccyjiayushe
authored andcommitted
Sync editorValue after inviting
1 parent 51c8d08 commit cbeeb8e

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

src/components/Playground.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,15 @@ class Playground extends React.Component<IPlaygroundProps, PlaygroundState> {
8787
public render() {
8888
const workspaceProps: WorkspaceProps = {
8989
controlBarProps: {
90+
editorValue: this.props.editorValue,
9091
editorSessionId: this.props.editorSessionId,
9192
externalLibraryName: this.props.externalLibraryName,
9293
handleChapterSelect: ({ chapter }: { chapter: number }, e: any) =>
9394
this.props.handleChapterSelect(chapter),
9495
handleExternalSelect: ({ name }: { name: ExternalLibraryName }, e: any) =>
9596
this.props.handleExternalSelect(name),
9697
handleEditorEval: this.props.handleEditorEval,
98+
handleEditorValueChange: this.props.handleEditorValueChange,
9799
handleGenerateLz: this.props.handleGenerateLz,
98100
handleInterruptEval: this.props.handleInterruptEval,
99101
handleInvalidEditorSessionId: this.props.handleInvalidEditorSessionId,

src/components/workspace/ControlBar.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { externalLibraries } from '../../reducers/externalLibraries';
1818
import { sourceChapters } from '../../reducers/states';
1919
import { ExternalLibraryName } from '../assessment/assessmentShape';
2020
import { controlButton } from '../commons';
21+
import Editor from './Editor';
2122

2223
/**
2324
* @prop questionProgress a tuple of (current question number, question length) where
@@ -27,10 +28,13 @@ export type ControlBarProps = {
2728
queryString?: string;
2829
questionProgress: [number, number] | null;
2930
sourceChapter: number;
31+
editorRef?: React.RefObject<Editor>;
3032
editorSessionId?: string;
33+
editorValue?: string | null;
3134
externalLibraryName?: string;
3235
handleChapterSelect?: (i: IChapter, e: React.ChangeEvent<HTMLSelectElement>) => void;
3336
handleEditorEval: () => void;
37+
handleEditorValueChange?: (newCode: string) => void;
3438
handleExternalSelect?: (i: IExternal, e: React.ChangeEvent<HTMLSelectElement>) => void;
3539
handleGenerateLz?: () => void;
3640
handleInterruptEval: () => void;
@@ -109,6 +113,12 @@ class ControlBar extends React.PureComponent<ControlBarProps, {}> {
109113
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
110114
const id = JSON.parse(xmlhttp.responseText).id;
111115
this.props.handleSetEditorSessionId!(id);
116+
const code = this.props.editorValue
117+
? this.props.editorValue
118+
: '// Collaborative Editing Mode!';
119+
this.props.editorRef!.current!.ShareAce.on('ready', () =>
120+
this.props.handleEditorValueChange!(code)
121+
);
112122
}
113123
};
114124
xmlhttp.open('GET', 'https://api2.sourceacademy.nus.edu.sg/gists/latest', true);

src/components/workspace/Editor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Editor extends React.PureComponent<IEditorProps, {}> {
2929
private onChangeMethod: (newCode: string) => void;
3030
private onValidateMethod: (annotations: Annotation[]) => void;
3131
private AceEditor: React.RefObject<AceEditor>;
32-
private ShareAce: any;
32+
public ShareAce: any;
3333

3434
constructor(props: IEditorProps) {
3535
super(props);

src/components/workspace/index.tsx

100644100755
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ class Workspace extends React.Component<WorkspaceProps, {}> {
2727
private leftParentResizable: Resizable;
2828
private maxDividerHeight: number;
2929
private sideDividerDiv: HTMLDivElement;
30+
private editorRef: React.RefObject<Editor>;
31+
32+
public constructor(props: WorkspaceProps) {
33+
super(props);
34+
this.editorRef = React.createRef();
35+
}
3036

3137
public componentDidMount() {
3238
this.maxDividerHeight = this.sideDividerDiv.clientHeight;
@@ -67,6 +73,7 @@ class Workspace extends React.Component<WorkspaceProps, {}> {
6773
private controlBarProps() {
6874
return {
6975
...this.props.controlBarProps,
76+
editorRef: this.editorRef,
7077
hasUnsavedChanges: this.props.hasUnsavedChanges
7178
};
7279
}
@@ -154,7 +161,13 @@ class Workspace extends React.Component<WorkspaceProps, {}> {
154161
private createWorkspaceInput = (props: WorkspaceProps) => {
155162
if (props.editorProps) {
156163
// Set key to force remount of Editor component when session id changes
157-
return <Editor {...props.editorProps} key={props.editorProps.editorSessionId} />;
164+
return (
165+
<Editor
166+
{...props.editorProps}
167+
key={props.editorProps.editorSessionId}
168+
ref={this.editorRef}
169+
/>
170+
);
158171
} else {
159172
return <MCQChooser {...props.mcqProps!} />;
160173
}

0 commit comments

Comments
 (0)