import React from 'react'; import { Form, Input, Button, Row, Col, InputNumber } from 'antd'; import './index.less'; import Editor from '@src/components/Editor'; import Page from '@src/containers/Page'; import Block from '@src/components/Block'; import Select from '@src/components/Select'; // import FileUpload from '@src/components/FileUpload'; import { formatFormError, getMap } from '@src/services/Tools'; import { asyncSMessage } from '@src/services/AsyncTools'; import { Sentence } from '../../../stores/sentence'; import config from './index'; export default class extends Page { constructor(props) { super(props); this.preview = null; this.structMap = {}; this.partList = []; this.pageLine = 100; const { id } = this.params; if (id) { config.title = '编辑长难句文章'; } else { config.title = '添加长难句文章'; } } init() { Sentence.getStruct().then(result => { return this.refreshStruct(result); }); } refreshStruct(result) { result = result || {}; result.chapters = result.chapters || []; const chapters = result.chapters.map((row, index) => { row.value = index + 1; return row; }).filter(row => !row.exercise); this.structMap = getMap(chapters, 'value'); this.setState({ struct: result, chapters }); } refreshPart(chapter) { const { form } = this.props; const { id } = this.params; Sentence.listArticle({ chapter }).then(result => { this.partList = result.list.map(row => row.part); if (id) { this.partList = this.partList.filter(row => row !== this.data.part); } form.validateFields(['part'], { force: true }); }); } initData() { const { id } = this.params; const { form } = this.props; let handler; if (id) { handler = Sentence.getArticle({ id }).then((result) => { if (result.chapter === 0) { // 前言模式 this.setState({ mode: 'introduction' }); } else { this.refreshPart(result.chapter); } return result; }); } else { const { i } = this.state.search; if (i) { this.setState({ mode: 'introduction' }); // 查询前言对应的id handler = Sentence.listArticle({ chapter: 0 }) .then(result => { if (result.total > 0) { return result.list[0]; } return { part: 1, chapter: 0, title: '前言' }; }); } else { handler = Promise.resolve({ part: 1 }); } } handler .then(result => { this.data = result; this.content = result.content || ''; form.setFieldsValue(result); }); } computePages() { if (!this.preview) return 0; this.preview.style.display = 'block'; const lines = this.preview.clientHeight / 20; this.preview.style.display = 'none'; return Math.ceil(lines / this.pageLine); } submit() { const { form } = this.props; form.validateFields((err) => { if (!err) { const data = form.getFieldsValue(); data.isTrail = data.isTrail ? 1 : 0; data.pages = this.computePages(); let handler; if (data.id) { handler = Sentence.editArticle(data); } else { handler = Sentence.addArticle(data); } handler.then(() => { asyncSMessage('保存成功'); goBack(); }).catch((e) => { if (e.result) form.setFields(formatFormError(data, e.result)); }); } }); } changeContent(content) { this.content = content; if (this.preview) { this.preview.innerHTML = content; } } renderIntroduction() { const { getFieldDecorator } = this.props.form; return
{getFieldDecorator('id')()} {getFieldDecorator('chapter')()} {getFieldDecorator('part')()} {getFieldDecorator('title')( , )}
; } renderBase() { const { getFieldDecorator } = this.props.form; return
{getFieldDecorator('id')()} {getFieldDecorator('chapter', { rules: [ { required: true, message: '请选择章节' }, ], })( , )} {/* {getFieldDecorator('isTrail', { valuePropName: 'checked', })( , )} {getFieldDecorator('trailStart')( parseInt(v, 10) || 1} />, )} {getFieldDecorator('trailEnd')( parseInt(v, 10) || 1} />, )} */}
; } renderContent() { const { getFieldDecorator } = this.props.form; return
{getFieldDecorator('content', { })( { this.changeContent(value); }} />, )}
{ if (ref) { this.preview = ref; this.preview.style.display = 'none'; this.changeContent(this.content); } }} className='simulation' /> ; } renderView() { const { mode } = this.state; return
{mode === 'introduction' ? this.renderIntroduction() : this.renderBase()} {this.renderContent()}
; } }