import React, { Component } from 'react'; import { Redirect } from 'react-router'; import { BrowserRouter as Router, Route, Link, Switch } from "react-router-dom"; import PropTypes from 'prop-types'; import classNames from 'classnames' import { isDev } from 'educoder' import './MemoDetailEditor.css' require('codemirror/lib/codemirror.css'); const $ = window.$; class MemoDetailMDEditor extends Component { constructor(props) { super(props) this.state = { isInited: this.props.usingMockInput ? false : true, isError: false, errorMsg: '' } } componentDidUpdate(prevProps, prevState, snapshot) { if (this.props.memo && (!prevProps.memo || this.props.memo.id != prevProps.memo.id)) { // this.keEditor = window.sd_create_editor_from_data(this.props.memo.id, null, "100%", "Memo"); // window._kk = this.keEditor } } initMDEditor = () => { // 因为props.memo不存在时,本组件不会被加载,这里直接在didMount里初始化即可 const placeholder = '我要回复...' // const imageUrl = `/upload_with_markdown?container_id=${this.props.memo.id}&container_type=Memo`; const imageUrl = `/api/attachments.json`; // 执行太快了,样式不正常 window.__tt = 400; setTimeout(() => { var commentMDEditor = window.create_editorMD_4comment("memo_comment_editorMd", '', this.props.height || 240, placeholder, imageUrl, () => { commentMDEditor.focus() this.initDrag() commentMDEditor.cm.on("change", (_cm, changeObj) => { this.setState({ isError: false, errorMsg: '' }) }) }, { watch: false, dialogLockScreen: false, }); this.commentMDEditor = commentMDEditor; window.commentMDEditor = commentMDEditor; }, window.__tt) } componentDidMount() { !this.props.usingMockInput && this.initMDEditor() } initDrag = () => { window.initMDEditorDragResize(".editor__resize", this.commentMDEditor) } onCommit = () => { const content = this.commentMDEditor.getValue(); // this.props.showError == if (this.props.showError == true) { if (!content || content.trim() == "") { this.setState({ isError: true, errorMsg: '不能为空' }) return; } else if (content.length > 2000) { this.setState({ isError: true, errorMsg: '不能超过2000个字符' }) return; } this.setState({ isError: false, errorMsg: '' }) } if (this.props.replyComment) { this.props.replyComment(content, this.props.memo.id, this.commentMDEditor) } else { window.$(document).trigger("onReply", { commentContent: content , id: this.props.memo.id, editor: this.commentMDEditor } ); } } showEditor() { $("html, body").animate({ scrollTop: $('.commentInput:visible').offset().top - 100 }, 1000, () => { if (this.commentMDEditor) { this.commentMDEditor.cm.focus() } else { this.onMockInputClick() } }); } close = () => { this.setState({isInited: false}) } onMockInputClick = () => { this.setState({isInited: true}) this.initMDEditor() } render() { const { match, history, memo, placeholder, className } = this.props const { isInited, errorMsg } = this.state if (!memo) { return
} return (