DraftEditorCom.js 20.0 KB
Newer Older
chscls@163.com's avatar
chscls@163.com committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
import React from 'react'
import styles from './DraftEditor.less';
import { Modal, Form, Input, Button, message, Upload, Icon } from 'antd'
import b from '../assets/专家经验系统切图/智能报告/B.png'
import ii from '../assets/专家经验系统切图/智能报告/_.png'
import it from '../assets/专家经验系统切图/智能报告/I.png'
import h from '../assets/专家经验系统切图/智能报告/H.png'
import blockQu from '../assets/专家经验系统切图/智能报告/blockQu.png'
import code from '../assets/专家经验系统切图/智能报告/___.png'
import prjectNum from '../assets/专家经验系统切图/智能报告/prjectNum.png'
import num from '../assets/专家经验系统切图/智能报告/num.png'
import t from '../assets/专家经验系统切图/智能报告/T.png'
import lk from '../assets/专家经验系统切图/智能报告/lk.png'
import pic from '../assets/专家经验系统切图/智能报告/pic.png'
import video from '../assets/专家经验系统切图/智能报告/video.png'
import sum from '../assets/专家经验系统切图/智能报告/sum.png'
import attrs from '../assets/专家经验系统切图/智能报告/attr.png'
import config from '@/webPublic/one_stop_public/config'
import MyBlockRender from './MyBlockRender'
import { changeToDraftState2, changeFromDraftState2,changeToDraftState } from '../utils/myutils'
import {
  Editor, EditorState, AtomicBlockUtils, convertFromRaw, convertToRaw, CompositeDecorator, RichUtils
} from 'draft-js';
const FormItem = Form.Item;

function getBlockStyle(block) {
  switch (block.getType()) {
    case 'blockquote': return 'RichEditor-blockquote';
    default: return null;
  }
}



const StyleControls = (props) => {
  const { editorState } = props;
  const selection = editorState.getSelection();
  const blockType = editorState
    .getCurrentContent()
    .getBlockForKey(selection.getStartKey())
    .getType();
  var currentStyle = props.editorState.getCurrentInlineStyle();

  return (
    <div style={{ borderBottom: "1px solid gray", borderTop: "1px solid gray" ,height:50,paddingTop:10}}>
      {props.btns.map((fn) =>
        <StyleButton
          key={fn.label}
          active={fn.type == "block" ? fn.style === blockType : currentStyle.has(fn.style)}
          label={fn}
          onToggle={fn.type == "block" ? props.toggleBlockType : props.toggleInlineStyle}
          style={fn.style}
        />
      )}
    </div>
  );
};





export default class DraftEditorCom extends React.Component {
  constructor(props) {
    super(props);
    const value = props.value || {};
tb53863844's avatar
tb53863844 committed
67
    console.log(value);
chscls@163.com's avatar
chscls@163.com committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    const editorState = changeToDraftState(value)
    this.state = {

      editorState: editorState,
      modalVisible: false,
      fnKey: "",
      styleMap: {
        'RED': {
          color: 'red',
        },
      }
    };

    this.setEditor = (editor) => {
      this.editor = editor;
    };
    this.onChange = (editorState,callback) => {


87

chscls@163.com's avatar
chscls@163.com committed
88 89 90
        this.setState({ editorState },()=>{
          if(callback)callback()
        });
91

chscls@163.com's avatar
chscls@163.com committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
      this.triggerChange({ editorState },()=>{
        if(callback)callback()
      });
    }

    this.focusEditor = () => {
      if (this.editor) {
        this.editor.focus();
      }
    };
    this.triggerChange = (changedValue,callback) => {
      // Should provide an event to pass value to Form.
      const onChange = this.props.onChange;
      const x = changeFromDraftState2(changedValue.editorState)
      if (onChange) {
        onChange(JSON.stringify(x));
      }
     if(callback) callback();
    };

  }
tb53863844's avatar
tb53863844 committed
113 114 115 116 117 118
  componentWillReceiveProps(nextProps) {
    // Should be a controlled component.
    if ('value' in nextProps) {
        const value = nextProps.value;
       if(value instanceof Object){
        const editorState = changeToDraftState2(value)
119

tb53863844's avatar
tb53863844 committed
120 121 122
        this.setState({editorState});  }
    }
}
chscls@163.com's avatar
chscls@163.com committed
123 124
  exchange=(data,editKey,callback)=>{
    const blocks = changeFromDraftState2(this.state.editorState)
125

chscls@163.com's avatar
chscls@163.com committed
126
    const bs =blocks.blocks
127

chscls@163.com's avatar
chscls@163.com committed
128 129
    var b;
    for(var i=0;i<bs.length;i++){
130

chscls@163.com's avatar
chscls@163.com committed
131 132 133 134 135 136
      if(bs[i].key==editKey){
        b=bs[i];
        break;
      }
    }
    const entityKey=b.entityRanges[0].key+"";
137

chscls@163.com's avatar
chscls@163.com committed
138 139 140 141 142
    blocks.entityMap[entityKey].data=data
    const editorState = changeToDraftState2(blocks)
    this.onChange(editorState,callback)
  }
  extends=(data,type,text,callback)=>{
143

chscls@163.com's avatar
chscls@163.com committed
144
    var editorState = this.state.editorState
145

chscls@163.com's avatar
chscls@163.com committed
146 147 148 149

    const contentState = editorState.getCurrentContent();
    const contentStateWithEntity = contentState.createEntity(type, 'IMMUTABLE', data);
    const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
150

chscls@163.com's avatar
chscls@163.com committed
151 152 153 154
    const newEditorState = EditorState.set(editorState, { currentContent: contentStateWithEntity });
    const xx = AtomicBlockUtils.insertAtomicBlock(newEditorState, entityKey, text);


155

chscls@163.com's avatar
chscls@163.com committed
156
      this.setState({ editorState: xx }, () => {
157

chscls@163.com's avatar
chscls@163.com committed
158 159
        callback()
      });
160

chscls@163.com's avatar
chscls@163.com committed
161 162 163 164
    this.triggerChange({ editorState: xx}, () => {

      callback()
    });
165 166


chscls@163.com's avatar
chscls@163.com committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
  }
  urlChange(event) {
    const target = event.target;
    this.setState({
      url: target.value
    });
  }
  toggleBlockType = (blockType) => {
    this.onChange(
      RichUtils.toggleBlockType(
        this.state.editorState,
        blockType
      )
    );
  }
  toggleInlineStyle = (inlineStyle) => {
    this.onChange(
      RichUtils.toggleInlineStyle(
        this.state.editorState,
        inlineStyle
      )
    );
  }

chscls@163.com's avatar
chscls@163.com committed
191
/*   componentWillReceiveProps(nextProps) {
chscls@163.com's avatar
chscls@163.com committed
192 193 194
    // Should be a controlled component.
    if ('value' in nextProps) {
      const value = nextProps.value;
195

chscls@163.com's avatar
chscls@163.com committed
196
    }
chscls@163.com's avatar
chscls@163.com committed
197
  } */
chscls@163.com's avatar
chscls@163.com committed
198 199 200 201 202 203 204



  okHandle = () => {

  }
  insertAttr = () => {
205

chscls@163.com's avatar
chscls@163.com committed
206
      this.setState({ modalVisible: true, fnKey: "attr" });
207

chscls@163.com's avatar
chscls@163.com committed
208 209 210
    this.triggerChange({ modalVisible: true, fnKey: "attr" });
  }
  insertPic = () => {
211

chscls@163.com's avatar
chscls@163.com committed
212
      this.setState({ modalVisible: true, fnKey: "image" });
213

chscls@163.com's avatar
chscls@163.com committed
214 215 216
    this.triggerChange({ modalVisible: true, fnKey: "image" });
  }
  insertFormula = () => {
217

chscls@163.com's avatar
chscls@163.com committed
218
      this.setState({ modalVisible: true, fnKey: "formula" });
219

chscls@163.com's avatar
chscls@163.com committed
220 221 222
    this.triggerChange({ modalVisible: true, fnKey: "formula" });
  }
  insertVideo = () => {
223

chscls@163.com's avatar
chscls@163.com committed
224
      this.setState({ modalVisible: true, fnKey: "video" });
225

chscls@163.com's avatar
chscls@163.com committed
226 227 228 229 230 231 232 233 234
    this.triggerChange({ modalVisible: true, fnKey: "video" });
  }
  insertLink = () => {
    const x = this.state.editorState.getSelection().getStartOffset()
    const y = this.state.editorState.getSelection().getEndOffset()
    if ((y - x) == 0) {
      message.error("请选择需要插入链接的内容")
      return
    }
235

chscls@163.com's avatar
chscls@163.com committed
236
      this.setState({ modalVisible: true, fnKey: "link" });
237

chscls@163.com's avatar
chscls@163.com committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    this.triggerChange({ modalVisible: true, fnKey: "link" });

  }
  callback3 = (content) => {


    const { editorState } = this.state


    const contentState = editorState.getCurrentContent();
    const contentStateWithEntity = contentState.createEntity('video', 'IMMUTABLE', content);
    const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
    const newEditorState = EditorState.set(editorState, { currentContent: contentStateWithEntity });
    const xx = AtomicBlockUtils.insertAtomicBlock(newEditorState, entityKey, '[视频]');






258

chscls@163.com's avatar
chscls@163.com committed
259
      this.setState({ editorState: xx, modalVisible: false, });
260

chscls@163.com's avatar
chscls@163.com committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    this.triggerChange({ editorState: xx, modalVisible: false, });

  }
  callback4 = (content) => {


    const { editorState } = this.state


    const contentState = editorState.getCurrentContent();
    const contentStateWithEntity = contentState.createEntity('attr', 'IMMUTABLE', content);
    const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
    const newEditorState = EditorState.set(editorState, { currentContent: contentStateWithEntity });
    const xx = AtomicBlockUtils.insertAtomicBlock(newEditorState, entityKey, '[附件]');






281

chscls@163.com's avatar
chscls@163.com committed
282
      this.setState({ editorState: xx, modalVisible: false, });
283

chscls@163.com's avatar
chscls@163.com committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
    this.triggerChange({ editorState: xx, modalVisible: false, });

  }
  callback2 = (content) => {


    const { editorState } = this.state


    const contentState = editorState.getCurrentContent();
    const contentStateWithEntity = contentState.createEntity('image', 'IMMUTABLE', content);
    const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
    const newEditorState = EditorState.set(editorState, { currentContent: contentStateWithEntity });
    const xx = AtomicBlockUtils.insertAtomicBlock(newEditorState, entityKey, '[图片]');


      this.setState({ editorState: xx, modalVisible: false, });
301

chscls@163.com's avatar
chscls@163.com committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    this.triggerChange({ editorState: xx, modalVisible: false, });

  }
  callback = (content) => {

    const { editorState } = this.state;

    // 获取contentState
    const contentState = editorState.getCurrentContent();
    // 在contentState上新建entity
    const contentStateWithEntity = contentState.createEntity(
      'LINK',
      // 'MUTABLE',
      // 'IMMUTABLE',
      'SEGMENTED',
      { url: content.url },
    );
    // 获取到刚才新建的entity
    const entityKey = contentStateWithEntity.getLastCreatedEntityKey();
    // 把带有entity的contentState设置到editorState上
    const newEditorState = EditorState.set(editorState, { currentContent: contentStateWithEntity });
    // 把entity和选中的内容对应
    const xx = RichUtils.toggleLink(
      newEditorState,
      newEditorState.getSelection(),
      entityKey
    )

330

chscls@163.com's avatar
chscls@163.com committed
331
      this.setState({ editorState: xx, modalVisible: false});
332

chscls@163.com's avatar
chscls@163.com committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346
    this.triggerChange({ editorState: xx, modalVisible: false});

  }
  modelContent = () => {
    if (this.state.fnKey == "link") {
      return <LinkForm callback={this.callback} />
    } else if (this.state.fnKey == "image") {
      return <PicForm callback={this.callback2} />
    } else if (this.state.fnKey == "video") {
      return <VideoForm callback={this.callback3} />
    } else if (this.state.fnKey == "formula") {
      return <FormulaForm callback={this.callback2} />
    }else if (this.state.fnKey == "attr") {
      return <AttrForm callback={this.callback4} />
347
    }
chscls@163.com's avatar
chscls@163.com committed
348 349 350
  }
 deleteBlock=(editKey)=>{
  const oldObj = changeFromDraftState2(this.state.editorState)
351

chscls@163.com's avatar
chscls@163.com committed
352 353
   const blocks = JSON.parse(JSON.stringify(oldObj));
  const bs =blocks.blocks
354

chscls@163.com's avatar
chscls@163.com committed
355 356 357
  var b;
  var j;
  for(var i=0;i<bs.length;i++){
358

chscls@163.com's avatar
chscls@163.com committed
359 360 361 362 363 364 365 366 367 368 369
    if(bs[i].key==editKey){
      b=bs[i];
      j=i;
      break;
    }
  }
  const entityKey=b.entityRanges[0].key+"";
  delete blocks.entityMap[entityKey]
  blocks.blocks.splice(j,1)

  const editorState = changeToDraftState2(blocks)
370

chscls@163.com's avatar
chscls@163.com committed
371
    this.setState({ editorState });
372

chscls@163.com's avatar
chscls@163.com committed
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
  this.triggerChange({ editorState });

 }
  render() {
    const btns = [
      { label: 'Bold', style: 'BOLD', type: "inline", icon: b },
      { label: 'Italic', style: 'ITALIC', type: "inline", icon: it },

      { label: 'line1', type: "line", icon: ii },
      { label: 'H1', style: 'header-one', type: "block", icon: h },
      /*  {label: 'H2', style: 'header-two',type:"block",icon:b},
       {label: 'H3', style: 'header-three',type:"block",icon:b},
       {label: 'H4', style: 'header-four',type:"block",icon:b},
       {label: 'H5', style: 'header-five',type:"block",icon:b},
       {label: 'H6', style: 'header-six',type:"block",icon:b}, */
      { label: 'Blockquote', style: 'blockquote', type: "block", icon: blockQu },
      { label: 'Code Block', style: 'code-block', type: "block", icon: code },
      { label: 'UL', style: 'unordered-list-item', type: "block", icon: prjectNum },
      { label: 'OL', style: 'ordered-list-item', type: "block", icon: num },
      { label: 'line2', type: "line", icon: ii },
      { label: 'lk', type: "fn", icon: lk, fn: this.insertLink },
      { label: 'pic', type: "fn", icon: pic, fn: this.insertPic },
395
    /*  { label: 'video', type: "fn", icon: video, fn: this.insertVideo },*/
chscls@163.com's avatar
chscls@163.com committed
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
    /*   { label: 'sum', type: "fn", icon: sum, fn: this.insertFormula }, */
      { label: 'att', type: "fn", icon: attrs, fn: this.insertAttr },
      { label: 'line3', type: "line", icon: ii },
      { label: 'Del', style: 'STRIKETHROUGH', type: "inline", icon: t },


    ];
    const { modalVisible } = this.state




    return (


      <div>

        <StyleControls
          btns={btns}
          editorState={this.state.editorState}
          toggleInlineStyle={this.toggleInlineStyle} toggleBlockType={this.toggleBlockType}
        />
        <div className={styles.basic} style={{

          height: '400px',

          overflowY: 'auto'
        }} onClick={this.focusEditor}>
          <Editor
            ref={this.setEditor}
            blockStyleFn={getBlockStyle}
            customStyleMap={this.state.styleMap}
            blockRendererFn={MyBlockRender.bind(this,false,null,this.props.editBlock,this.deleteBlock)}
            editorState={this.state.editorState}

            placeholder={this.props.placeholder || "请输入"}
            onChange={this.onChange} />
        </div>
        <Modal

          width='700px'
          maskClosable={false}

          destroyOnClose
          title={"插入对象"}
          visible={modalVisible}
          footer={null}
          onCancel={() => this.setState({ modalVisible: false })}>
          {this.modelContent()}
        </Modal>
      </div>
    )

  }
}
class StyleButton extends React.Component {
  constructor() {
    super();
    this.onToggle = (e) => {
      e.preventDefault();
      this.props.onToggle(this.props.style);
    };
  }
  render() {

    const { label } = this.props
    return (
      <a key={label.label} onMouseDown={this.onToggle} onClick={label.fn} style={{ marginLeft: 20 }}>
        <img src={label.icon} />
      </a>
    );
  }
}

@Form.create()
class LinkForm extends React.Component {
  constructor(props) {
    super(props);

  }
  submit = (e) => {
    e.preventDefault();

    const { form } = this.props;

    form.validateFields((err, fieldsValue) => {
      if (err) return;

      this.props.callback(fieldsValue)

    })
  }
  render() {

    const { form } = this.props;
    return (
      <Form onSubmit={this.submit}>

        <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="链接地址">
          {form.getFieldDecorator('url', {
            rules: [{ required: true, message: '请输入链接名称' }],
          })(<Input placeholder="请输入链接名称" />)}
        </FormItem>

        <Button type="primary" htmlType="submit">
          确定
            </Button>

      </Form>
    );
  }
}
@Form.create()
class AttrForm extends React.Component {
  constructor(props) {
    super(props);
    this.state={
      url:null,
      name:null,
    }

  }
  submit = (e) => {
    e.preventDefault();

    const { form } = this.props;

    form.validateFields((err, fieldsValue) => {
      if (err) return;
      var src = fieldsValue.src;


      src = src[src.length - 1].response
      const params = {
        ...fieldsValue,
        src,
        name:this.state.name
      }

      this.props.callback(params)

    })
  }
  normFile = (e) => {

    if (Array.isArray(e)) {
      return e;
    }
    return e && e.fileList;
  }
546 547


chscls@163.com's avatar
chscls@163.com committed
548
  onChange=(info) =>{
549

chscls@163.com's avatar
chscls@163.com committed
550 551 552 553 554 555
    if (info.file.status === 'done') {
      message.success(`文件上传成功`);
      this.setState({url:info.file.response,name:info.file.name})
    } else if (info.file.status === 'error') {
      message.error(`文件上传失败`);
    }
556

chscls@163.com's avatar
chscls@163.com committed
557 558 559 560 561 562 563
};
  render() {

    const { form } = this.props;
    const {url,name} =this.state
    return (
      <Form onSubmit={this.submit}>
564

chscls@163.com's avatar
chscls@163.com committed
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
        <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="上传附件">
          {form.getFieldDecorator('src', {
            valuePropName: 'fileList',
            getValueFromEvent: this.normFile,
          })(<Upload.Dragger onChange={this.onChange} showUploadList={false} name="file" action={config.uploadUrl} onChangemultiple={false} style={{ padding: 0 }}>
             {url?<a href={config.httpServer + url} target="_blank" >{name}</a>:<p className="ant-upload-drag-icon" style={{ marginBottom: 0,height:400 }}>
              <Icon type="video" />
            </p>}

          </Upload.Dragger>)}
        </FormItem>

        <Button type="primary" htmlType="submit">
          确定
            </Button>

      </Form>
    );
  }
}
@Form.create()
class VideoForm extends React.Component {
  constructor(props) {
    super(props);
    this.state={
      url:null
    }

  }
  submit = (e) => {
    e.preventDefault();

    const { form } = this.props;

    form.validateFields((err, fieldsValue) => {
      if (err) return;
      var src = fieldsValue.src;


      src = src[src.length - 1].response
      const params = {
        ...fieldsValue,
        src
      }

      this.props.callback(params)

    })
  }
  normFile = (e) => {

    if (Array.isArray(e)) {
      return e;
    }
    return e && e.fileList;
  }
   beforeUpload=(file)=> {
    const isJPG = file.type === 'video/mp4';
    if (!isJPG) {
      message.error('请上传mp4格式文件!');
625

chscls@163.com's avatar
chscls@163.com committed
626
    }
627

chscls@163.com's avatar
chscls@163.com committed
628 629
    return isJPG ;
  }
630

chscls@163.com's avatar
chscls@163.com committed
631
  onChange=(info) =>{
632

chscls@163.com's avatar
chscls@163.com committed
633 634 635 636 637 638
    if (info.file.status === 'done') {
      message.success(`视频上传成功`);
      this.setState({url:info.file.response})
    } else if (info.file.status === 'error') {
      message.error(`视频上传失败`);
    }
639

chscls@163.com's avatar
chscls@163.com committed
640 641 642 643 644 645 646
};
  render() {

    const { form } = this.props;
    const {url} =this.state
    return (
      <Form onSubmit={this.submit}>
647

chscls@163.com's avatar
chscls@163.com committed
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
        <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="上传视频">
          {form.getFieldDecorator('src', {
            valuePropName: 'fileList',
            getValueFromEvent: this.normFile,
          })(<Upload.Dragger beforeUpload={this.beforeUpload} onChange={this.onChange} showUploadList={false} name="file" action={config.uploadUrl} onChangemultiple={false} style={{ padding: 0 }}>
             {url?<video src={config.httpServer + url} controls="controls">您的浏览器不支持 video 标签。
      </video>:<p className="ant-upload-drag-icon" style={{ marginBottom: 0,height:400 }}>
              <Icon type="video" />
            </p>}

          </Upload.Dragger>)}
        </FormItem>

        <Button type="primary" htmlType="submit">
          确定
            </Button>

      </Form>
    );
  }
}
@Form.create()
class PicForm extends React.Component {
  constructor(props) {
    super(props);
    this.state={
      url:null
    }


  }
  beforeUpload=(file)=> {
    const isJPG = (file.type === 'image/jpeg'||file.type === 'image/png') ;
    if (!isJPG) {
      message.error('请上传jpg或者png格式文件!');
    }
684

chscls@163.com's avatar
chscls@163.com committed
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
    return isJPG ;
  }
  submit = (e) => {
    e.preventDefault();

    const { form } = this.props;

    form.validateFields((err, fieldsValue) => {
      if (err) return;
      var src = fieldsValue.src;


      src = src[src.length - 1].response
      const params = {
        ...fieldsValue,
        src
      }

      this.props.callback(params)

    })
  }
  normFile = (e) => {
708

chscls@163.com's avatar
chscls@163.com committed
709 710 711 712 713 714
    if (Array.isArray(e)) {
      return e;
    }
    return e && e.fileList;
  }
  onChange=(info) =>{
715

chscls@163.com's avatar
chscls@163.com committed
716 717 718 719 720 721
    if (info.file.status === 'done') {
      message.success(`图片上传成功`);
      this.setState({url:info.file.response})
    } else if (info.file.status === 'error') {
      message.error(`图片上传失败`);
    }
722

chscls@163.com's avatar
chscls@163.com committed
723 724 725 726 727 728 729
};
  render() {

    const { form } = this.props;
    const {url} =this.state
    return (
      <Form onSubmit={this.submit}>
730

chscls@163.com's avatar
chscls@163.com committed
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
        <FormItem labelCol={{ span: 5 }} wrapperCol={{ span: 15 }} label="上传图片">
          {form.getFieldDecorator('src', {
            valuePropName: 'fileList',
            getValueFromEvent: this.normFile,
          })(<Upload.Dragger beforeUpload={this.beforeUpload}  showUploadList={false} name="file" action={config.uploadUrl} onChange={this.onChange} multiple={false} style={{ padding: 0 }}>
           {url?<img src={config.httpServer+url} style={{height:400,width:"100%"}}/>:<p className="ant-upload-drag-icon" style={{ marginBottom: 0,height:400 }}>
              <Icon type="video" />
            </p>}

          </Upload.Dragger>)}
        </FormItem>

        <Button type="primary" htmlType="submit">
          确定
            </Button>

      </Form>
    );
  }
}

@Form.create()
class FormulaForm extends React.Component {
  constructor(props) {
    super(props);
    window.latex = (gs) => {
      this.props.callback({ src: '/latex?code=' + encodeURI(gs), description: "" })
    }

  }


  render() {

    const { form } = this.props;
    return (
      <iframe src="/gongshi/gongshi.html" style={{ width: '100%', minHeight: '400px', border: 'solid 1px #0062d5' }} ></iframe>
    );
  }
}