index.jsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import React,{Component} from 'react';
  2. import style from './index.less';
  3. import config from "@config/index";
  4. import {filterArr,isIE} from '@utils/tools.js';
  5. import Notify from '../Notify/index.js';
  6. import classNames from 'classnames';
  7. import $ from 'jquery';
  8. /*****
  9. * author:zn@2018-12-10
  10. * 自由文本输入组件
  11. * 接收参数:
  12. * value:默认显示文字
  13. * handleChange:输入变化时触发函数,接收以下参数:
  14. * * boxMark:病例项标识
  15. * * i:标签index
  16. * * text:标签内文字
  17. *
  18. * ****/
  19. class EditableSpan extends Component{
  20. constructor(props){
  21. super(props);
  22. this.state={
  23. timer:null,
  24. clearTimer:null,
  25. oldText:props.value,
  26. labelVal:'', //存放标签原有的值--主诉字数限制用
  27. preVal:'',
  28. index:null,
  29. searchPre:''
  30. };
  31. this.$span = React.createRef();
  32. this.handleFocus = this.handleFocus.bind(this);
  33. this.onChange = this.onChange.bind(this);
  34. this.handleBlur = this.handleBlur.bind(this);
  35. this.handleKeydown = this.handleKeydown.bind(this);
  36. this.handleKeyup = this.handleKeyup.bind(this);
  37. this.moveEnd = this.moveEnd.bind(this);
  38. }
  39. handleFocus(e){
  40. e.stopPropagation();
  41. const {setFocusIndex,i,boxMark}= this.props;
  42. let text = e.target.innerText;
  43. setFocusIndex&&setFocusIndex({i,boxMark,dom:this.$span});
  44. this.setState({
  45. labelVal:text,
  46. index:i,
  47. searchPre:text
  48. });
  49. }
  50. onChange(e){
  51. e.stopPropagation();
  52. const {handleChange,boxMark,i,handleSearch,value,mainSaveText,mainIds} = this.props;
  53. const {labelVal,searchPre} = this.state;
  54. const text1 =e.target.innerText;
  55. let mainText = filterArr(mainSaveText);//主诉字数
  56. if(+boxMark==1){
  57. if(mainText.length >= config.limited){
  58. if(text1.length > labelVal.length){
  59. e.target.innerText = labelVal;
  60. Notify.info(config.limitText);
  61. return
  62. }else if(text1.length == labelVal.length){
  63. this.setState({
  64. labelVal:text1
  65. });
  66. }else{
  67. handleChange&&handleChange({text1,boxMark,i});
  68. }
  69. return
  70. }
  71. }
  72. this.setState({
  73. labelVal:text1
  74. });
  75. const that = this;
  76. handleChange&&handleChange({text1,boxMark,i});
  77. //延迟搜索
  78. clearTimeout(this.state.timer);
  79. const timer = setTimeout(function(){
  80. let newText = e.target.innerText;
  81. let temp = '',isEnd=false;
  82. let search='';
  83. clearTimeout(that.state.timer);
  84. temp = newText.replace(searchPre,'');
  85. isEnd = !(newText.indexOf(searchPre)>0);
  86. search = temp.replace(/[(^\s*)|(\s*$)|(^\,*)|(\,*$)]/g,'');
  87. //console.log(labelVal,'旧:',searchPre,'新:',newText,'搜索:',search);
  88. if(config.punctuationReg.test(search)){ //只有标点符号时不搜索
  89. handleSearch&&handleSearch({text:search,isEnd,boxMark,mainIds});
  90. }
  91. //搜索后保持现在的值,继续输入时要用于对比
  92. that.setState({
  93. searchPre:newText
  94. });
  95. },config.delayTime);
  96. this.setState({
  97. timer
  98. });
  99. }
  100. handleBlur(e){//为了阻止冒泡事件
  101. const {boxMark,handleClear,handleChange,i} = this.props;
  102. e.stopPropagation();
  103. // 延时清空搜索结果,不延时会影响选中
  104. clearTimeout(this.state.clearTimer);
  105. const clearTimer = setTimeout(function(){
  106. handleClear && handleClear({boxMark})
  107. },config.delayTime);
  108. this.setState({
  109. clearTimer
  110. });
  111. }
  112. moveEnd(obj) {
  113. if(window.getSelection){//ie11 10 9 ff safari
  114. obj.focus(); //解决ff不获取焦点无法定位问题
  115. var range = window.getSelection();//创建range
  116. range.selectAllChildren(obj);//range 选择obj下所有子内容
  117. range.collapseToEnd();//光标移至最后
  118. }
  119. else if (document.selection) {//ie10 9 8 7 6 5
  120. var range = document.selection.createRange();//创建选择对象
  121. range.moveToElementText(obj);//range定位到obj
  122. range.collapse(false);//光标移至最后
  123. range.select();
  124. }
  125. }
  126. handleKeydown(e){
  127. const ev = e||window.event;
  128. const target = ev.target||ev.srcElement;
  129. let innerVal = target.innerText;
  130. /*if(this.props.full){
  131. return false;
  132. }*/
  133. //禁止回车事件
  134. if(ev.keyCode==13){return false;}
  135. //backspace事件
  136. if(ev.keyCode==8){
  137. //用于对比backspace前后的值
  138. this.setState({
  139. preVal:innerVal
  140. })
  141. }
  142. }
  143. handleKeyup(e){
  144. const {boxMark,handleKeydown,i,value,removeId} = this.props;
  145. const {preVal,index} = this.state;
  146. const ev = e||window.event;
  147. const target = ev.target||ev.srcElement;
  148. let innerVal = target.innerText;
  149. if(ev.keyCode==8){
  150. // 主诉现病史去重:删除最后一个字的时候移除该数据(将name、id和value替换成空)并移除id
  151. // 前面是标签,内容为空时再删一次才移除标签;前面是文本,则直接移除;
  152. let preObj = $(this.$span.current).prev();
  153. if(index!==0&&preVal.trim().length==1){
  154. removeId && removeId({boxMark,i:index,text:""});
  155. if(preObj[0].nodeName !=="DIV"){
  156. this.moveEnd(preObj[0]);
  157. }
  158. }
  159. if(innerVal !==preVal){return false}
  160. let data = innerVal.trim();
  161. //判断是否为空、中英文:, 。、;,且不是第一位
  162. let pattern = new RegExp(/^\,?$|^\,?$|^\.?$|^\。?$|^\、?$|^\;?$|^\;?$|^\:?$|^\:?$\s/);
  163. if(index!==0 && pattern.test(data)){
  164. // let preObj = $(this.$span.current).prev();
  165. let obj = preObj[0].nodeName=="DIV"?preObj.prev():preObj;
  166. handleKeydown&&handleKeydown({boxMark,i:index,text:data});
  167. this.moveEnd(obj[0]);
  168. this.setState({
  169. index: null
  170. })
  171. }
  172. }
  173. }
  174. componentWillReceiveProps(next){
  175. const isRead = this.props.isRead;
  176. if(next.isRead != isRead){
  177. this.$span.current.innerText = next.value||'';
  178. }
  179. }
  180. componentDidMount(){
  181. const {value} = this.props;
  182. if(value){
  183. this.$span.current.innerText = value||'';
  184. }
  185. if(isIE()){
  186. $(this.$span.current).onIe8Input(function(e){
  187. this.onChange(e)
  188. },this);
  189. }
  190. }
  191. getClass(){
  192. const {full,value,saveText,i} = this.props;
  193. const preSelected = saveText[i-1];
  194. const isFull = full?' '+style['full']:''; //是否宽度设为整行宽度
  195. //有标点符号之外的字符或者前一个标签有选中值时,显示为黑色,否则灰显
  196. const unselect = value.match(config.punctuationReg)||preSelected?'':style['unselect'];
  197. return classNames(style['editable-span'],isFull,unselect);
  198. }
  199. render() {
  200. return <span className={this.getClass()}
  201. contentEditable='true'
  202. ref={this.$span}
  203. onInput={this.onChange}
  204. onFocus={this.handleFocus}
  205. onBlur={this.handleBlur}
  206. onkeydown={this.handleKeydown}
  207. onkeyup={this.handleKeyup}></span>;
  208. }
  209. }
  210. export default EditableSpan;