index.jsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import React, { Component } from "react";
  2. import style from "./index.less";
  3. import config from '@config/index';
  4. import $ from 'jquery';
  5. import { setFontColorSize,moveEnd} from '@utils/tools';
  6. import store from '@store';
  7. import { embedPush } from '../../../store/async-actions/pushMessage'
  8. class Textarea extends Component {
  9. constructor(props) {
  10. super(props);
  11. this.state = {
  12. timer:null,
  13. val:'报告描述或意见'
  14. };
  15. this.textInput = React.createRef();
  16. this.$dom = React.createRef();
  17. this.$domW = React.createRef();
  18. this.handleInput = this.handleInput.bind(this);
  19. this.handleFocus = this.handleFocus.bind(this);
  20. }
  21. handleInput(e){
  22. const {handleChangeAssistValue,idx,handlePush} = this.props;
  23. const text = (e.target.innerText || e.target.innerHTML||e.target.textContent);
  24. $(e.target).find('img').remove();
  25. this.context.scrollArea.scrollBottom(); //避免滚动条上移不见
  26. // e.target.innerHTML = e.target.textContent
  27. const stimer = this.state.timer;
  28. handleChangeAssistValue&&handleChangeAssistValue(text,idx);
  29. //右侧推送--延时推送
  30. clearTimeout(stimer);
  31. let timer = setTimeout(function(){
  32. handlePush&&handlePush({mode:9});
  33. clearTimeout(stimer);
  34. },config.delayPushTime);
  35. if(text.trim() != '' && text != '<br>'){
  36. e.target.nextSibling.innerText?(e.target.nextSibling.innerText = ''):(e.target.nextSibling.innerHTML = '')
  37. }else{
  38. e.target.nextSibling.innerText?(e.target.nextSibling.innerText = '报告描述或意见'):(e.target.nextSibling.innerHTML = '报告描述或意见')
  39. }
  40. $(".TextareaRsize").css({marginTop:0});
  41. this.setState({
  42. timer
  43. });
  44. }
  45. shouldComponentUpdate(next){
  46. if(JSON.stringify(next) == JSON.stringify(this.props)){
  47. return false;
  48. }
  49. return true;
  50. }
  51. componentWillReceiveProps(next){
  52. const isRead = this.props.isRead;
  53. if(next.isRead != isRead){
  54. this.$dom.current.innerText?(this.$dom.current.innerText = next.value||'') : (this.$dom.current.innerHTML = next.value||'')
  55. if(next.value && next.value.trim()){
  56. this.$dom.current.nextSibling.innerText = ''
  57. }else{
  58. this.$dom.current.nextSibling.innerHTML = '报告描述或意见'
  59. }
  60. }
  61. }
  62. componentDidMount(){
  63. const {value} = this.props;
  64. if(value && value.trim()){
  65. this.$dom.current.innerText?(this.$dom.current.innerText = value) : (this.$dom.current.innerHTML = value)
  66. this.$dom.current.nextSibling.innerText?(this.$dom.current.nextSibling.innerText = ''):(this.$dom.current.nextSibling.innerHTML = '')
  67. }else{
  68. this.$dom.current.nextSibling.innerText?(this.$dom.current.nextSibling.innerText = '报告描述或意见'):(this.$dom.current.nextSibling.innerHTML = '报告描述或意见')
  69. }
  70. const that = this;
  71. let txt = '';
  72. //黏贴时去掉html格式
  73. $(this.$dom.current).on("paste",function(e){
  74. setTimeout(function(){
  75. txt = that.$dom.current.innerText||that.$dom.current.innerHTML;
  76. that.$dom.current.innerHTML = txt;
  77. moveEnd($(that.$dom.current)[0]); //光标落到最后去
  78. });
  79. })
  80. }
  81. componentWillUnmount(){
  82. $(this.$dom.current).off("paste");
  83. }
  84. handleFocus(){ //ie8下提示语挡住输入框,导致输入框无法聚焦
  85. store.dispatch(embedPush({
  86. action: "pacs",
  87. mode: 1
  88. }))
  89. this.textInput.current.previousSibling.focus();
  90. }
  91. render() {
  92. const {idx,disabled} = this.props;
  93. return (
  94. <div className={style.textWap}>
  95. <div className={`${style.divTextarea} ${setFontColorSize(2)}`}
  96. contenteditable={disabled?false:true}
  97. ref={this.$dom}
  98. onInput={this.handleInput}
  99. onKeyUp={this.handleInput}
  100. ></div>
  101. <p ref={this.textInput} onClick={this.handleFocus} className={`${style.textareaWarring} ${setFontColorSize(2)}`}></p>
  102. </div>
  103. );
  104. }
  105. }
  106. Textarea.contextTypes = {
  107. scrollArea: React.PropTypes.object
  108. };
  109. export default Textarea;