123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- 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 <Block>
- <Form>
- {getFieldDecorator('id')(<input hidden />)}
- {getFieldDecorator('chapter')(<input hidden />)}
- {getFieldDecorator('part')(<input hidden />)}
- <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='前言名称'>
- {getFieldDecorator('title')(
- <Input placeholder='请输入名称' />,
- )}
- </Form.Item>
- </Form>
- </Block>;
- }
- renderBase() {
- const { getFieldDecorator } = this.props.form;
- return <Block>
- <Form>
- {getFieldDecorator('id')(<input hidden />)}
- <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='Chapter'>
- {getFieldDecorator('chapter', {
- rules: [
- { required: true, message: '请选择章节' },
- ],
- })(
- <Select select={this.state.chapters} placeholder='请选择章节' onChange={(v) => {
- this.refreshPart(v);
- }} />,
- )}
- </Form.Item>
- <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='Part'>
- {getFieldDecorator('part', {
- rules: [
- { required: true, message: '请输入Part' },
- {
- validator: (rule, value, callback) => {
- if (this.partList.indexOf(value) >= 0) callback('该part已被使用');
- else callback();
- },
- },
- ],
- })(
- <InputNumber min={1} precision={0} formatter={(v) => parseInt(v, 10) || 1} />,
- )}
- </Form.Item>
- <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='名称'>
- {getFieldDecorator('title', {
- rules: [
- { required: true, message: '请输入名称' },
- ],
- })(
- <Input placeholder='请输入名称' />,
- )}
- </Form.Item>
- {/* <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='开放试用'>
- <Row>
- <Col span={4}>
- {getFieldDecorator('isTrail', {
- valuePropName: 'checked',
- })(
- <Switch checkedChildren='on' unCheckedChildren='off' />,
- )}
- </Col>
- <Col span={3}>
- <Form.Item>
- {getFieldDecorator('trailStart')(
- <InputNumber disabled={!getFieldValue('isTrail')} min={1} precision={0} formatter={(v) => parseInt(v, 10) || 1} />,
- )}
- </Form.Item>
- </Col>
- <Col span={1}>至</Col>
- <Col span={3}>
- <Form.Item>
- {getFieldDecorator('trailEnd')(
- <InputNumber disabled={!getFieldValue('isTrail')} min={1} precision={0} formatter={(v) => parseInt(v, 10) || 1} />,
- )}
- </Form.Item>
- </Col>
- </Row>
- </Form.Item> */}
- </Form>
- </Block>;
- }
- renderContent() {
- const { getFieldDecorator } = this.props.form;
- return <Block flex>
- <Form>
- <Form.Item label='文章录入'>
- {getFieldDecorator('content', {
- })(
- <Editor placeholder='输入内容'
- onChange={(value) => {
- this.changeContent(value);
- }} />,
- )}
- </Form.Item>
- </Form>
- <div ref={(ref) => {
- if (ref) {
- this.preview = ref;
- this.preview.style.display = 'none';
- this.changeContent(this.content);
- }
- }} className='simulation' />
- </Block>;
- }
- renderView() {
- const { mode } = this.state;
- return <div flex>
- {mode === 'introduction' ? this.renderIntroduction() : this.renderBase()}
- {this.renderContent()}
- <Row type="flex" justify="center">
- <Col>
- <Button type="primary" onClick={() => {
- this.submit();
- }}>保存</Button>
- </Col>
- </Row>
- </div>;
- }
- }
|