Skip to content

Mission editing slang #500

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

Merged
merged 3 commits into from
Mar 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';

import { IAssessment } from '../../assessment/assessmentShape';
import { limitNumberRange } from './';
import TextareaContent from './TextareaContent';

interface IProps {
Expand All @@ -18,12 +19,13 @@ export class GradingTab extends React.Component<IProps, {}> {
return this.gradingTab();
}

private textareaContent = (path: Array<string | number>, isNumber: boolean = false) => {
private textareaContent = (path: Array<string | number>) => {
return (
<TextareaContent
assessment={this.props.assessment}
isNumber={isNumber}
isNumber={true}
path={path}
processResults={limitNumberRange(0)}
updateAssessment={this.props.updateAssessment}
/>
);
Expand All @@ -32,10 +34,10 @@ export class GradingTab extends React.Component<IProps, {}> {
private gradingTab = () => (
<div>
Max Grade:
{this.textareaContent(this.props.path.concat(['maxGrade']), true)}
{this.textareaContent(this.props.path.concat(['maxGrade']))}
<br />
Max Xp:
{this.textareaContent(this.props.path.concat(['maxXp']), true)}
{this.textareaContent(this.props.path.concat(['maxXp']))}
</div>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as React from 'react';
import AceEditor from 'react-ace';

import { IAssessment, IMCQQuestion } from '../../assessment/assessmentShape';
import { assignToPath, getValueFromPath } from './';
import { assignToPath, getValueFromPath, limitNumberRange } from './';
import TextareaContent from './TextareaContent';

interface IProps {
Expand All @@ -12,9 +12,18 @@ interface IProps {
updateAssessment: (assessment: IAssessment) => void;
}

export class QuestionTemplateTab extends React.Component<IProps, {}> {
interface IState {
templateValue: string;
templateFocused: boolean;
}

export class QuestionTemplateTab extends React.Component<IProps, IState> {
public constructor(props: IProps) {
super(props);
this.state = {
templateValue: '',
templateFocused: false
};
}

public render() {
Expand All @@ -34,30 +43,60 @@ export class QuestionTemplateTab extends React.Component<IProps, {}> {
const path = ['questions', this.props.questionId, 'answer'];

const handleTemplateChange = (newCode: string) => {
const assessmentVal = this.props.assessment;
assignToPath(path, newCode, assessmentVal);
this.props.updateAssessment(assessmentVal);
this.setState({
templateValue: newCode
});
};

const value = this.state.templateFocused
? this.state.templateValue
: getValueFromPath(path, this.props.assessment);

const display = (
<AceEditor
className="react-ace"
editorProps={{
$blockScrolling: Infinity
}}
fontSize={14}
highlightActiveLine={false}
mode="javascript"
onChange={handleTemplateChange}
theme="cobalt"
value={getValueFromPath(path, this.props.assessment)}
width="100%"
/>
<div onClick={this.focusEditor(path)} onBlur={this.unFocusEditor(path)}>
<AceEditor
className="react-ace"
editorProps={{
$blockScrolling: Infinity
}}
fontSize={14}
highlightActiveLine={false}
mode="javascript"
onChange={handleTemplateChange}
theme="cobalt"
value={value}
width="100%"
/>
</div>
);

return display;
};

private focusEditor = (path: Array<string | number>) => (e: any): void => {
if (!this.state.templateFocused) {
this.setState({
templateValue: getValueFromPath(path, this.props.assessment),
templateFocused: true
});
}
};

private unFocusEditor = (path: Array<string | number>) => (e: any): void => {
if (this.state.templateFocused) {
const value = getValueFromPath(path, this.props.assessment);
if (value !== this.state.templateValue) {
const assessmentVal = this.props.assessment;
assignToPath(path, this.state.templateValue, assessmentVal);
this.props.updateAssessment(assessmentVal);
}
this.setState({
templateValue: '',
templateFocused: false
});
}
};

private mcqTab = () => {
const questionId = this.props.questionId;
const question = this.props.assessment!.questions[questionId] as IMCQQuestion;
Expand Down Expand Up @@ -87,17 +126,27 @@ export class QuestionTemplateTab extends React.Component<IProps, {}> {
private textareaContent = (
path: Array<string | number>,
isNumber: boolean = false,
numberRange: number[] = [0]
range: number[] = [0]
) => {
return (
<TextareaContent
assessment={this.props.assessment}
isNumber={isNumber}
numberRange={numberRange}
path={path}
updateAssessment={this.props.updateAssessment}
/>
);
if (isNumber) {
return (
<TextareaContent
assessment={this.props.assessment}
isNumber={true}
path={path}
processResults={limitNumberRange(range[0], range[1])}
updateAssessment={this.props.updateAssessment}
/>
);
} else {
return (
<TextareaContent
assessment={this.props.assessment}
path={path}
updateAssessment={this.props.updateAssessment}
/>
);
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { assignToPath, getValueFromPath } from './';
interface IProps {
assessment: IAssessment;
isNumber?: boolean;
numberRange?: number[];
path: Array<string | number>;
useRawValue?: boolean;
processResults?: (newVal: string | number) => string | number;
Expand All @@ -35,31 +34,28 @@ export class TextareaContent extends React.Component<IProps, IState> {
}

public render() {
const filler = 'Please enter value (if applicable)';
let display;
if (this.state.isEditing) {
display = <div onClick={this.toggleEditField()}>{this.makeEditingTextarea()}</div>;
display = this.makeEditingTextarea();
} else {
let value = getValueFromPath(this.props.path, this.props.assessment) || filler;
value = value.match('^(\n| )*$') ? filler : value;
display = (
<div onClick={this.toggleEditField()}>
{this.state.useRawValue ? value : <Markdown content={value} />}
</div>
);
if (this.state.useRawValue) {
display = getValueFromPath(this.props.path, this.props.assessment);
} else {
const filler = 'Please enter value (if applicable)';
let value = getValueFromPath(this.props.path, this.props.assessment) || '';
value = value.match('^(\n| )*$') ? filler : value;
display = <Markdown content={value} />;
}
}
return display;
return <div onClick={this.toggleEditField()}>{display}</div>;
}

private saveEditAssessment = (e: any) => {
let fieldValue: number | string;
if (this.state.isNumber) {
const range = this.props.numberRange || [0];
fieldValue = parseInt(this.state.fieldValue, 10);
if (isNaN(fieldValue) || fieldValue < range[0]) {
fieldValue = range[0];
} else if (range.length > 1 && fieldValue > range[1]) {
fieldValue = range[1];
if (isNaN(fieldValue)) {
fieldValue = getValueFromPath(this.props.path, this.props.assessment);
}
} else {
fieldValue = this.state.fieldValue;
Expand Down
14 changes: 14 additions & 0 deletions src/components/incubator/editingWorkspaceSideContent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,17 @@ export const assignToPath = (path: Array<string | number>, value: any, obj: any)
}
obj[path[i]] = value;
};

export const limitNumberRange = (min: number | null = 0, max: number | null = null) => (
value: number
): number => {
let result;
if (min !== null && value < min) {
result = min;
} else if (max !== null && value > max) {
result = max;
} else {
result = value;
}
return result;
};
19 changes: 14 additions & 5 deletions src/utils/xmlParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,23 @@ export const exportXml = () => {
};
let xmlStr = builder.buildObject(xml);
xmlStr = xmlStr.replace(/(&#xD;)+/g, '');
const element = document.createElement('a');
const file = new Blob([xmlStr], { endings: 'native', type: 'text/xml;charset=UTF-8' });
element.href = URL.createObjectURL(file);
element.download = title + '.xml';
element.click();
download(title + '.xml', xmlStr);
}
};

const download = (filename: string, text: string) => {
const element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);

element.style.display = 'none';
document.body.appendChild(element);

element.click();

document.body.removeChild(element);
};

const exportLibrary = (library: Library) => {
const deployment = {
$: {
Expand Down