index.jsx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import React,{Component} from 'react';
  2. import style from './index.less';
  3. import config from "@config/index";
  4. import {filterArr,isIE,getPageCoordinate,filterDataArr} 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 {mainSaveText,full,setFocusIndex,i,boxMark,value}= this.props;
  42. let mainText = filterDataArr(mainSaveText);//主诉字数
  43. if(+boxMark==3||+boxMark==4){ //主诉为空,且第一次聚焦其他史查体时提示且不可输入
  44. if(!mainText&&full&&(value===''||value===undefined)){
  45. Notify.error("无法操作,请先输入主诉");
  46. e.target.blur();
  47. return ;
  48. }
  49. }
  50. let text = e.target.innerText;
  51. setFocusIndex&&setFocusIndex({i,boxMark,dom:this.$span});
  52. this.setState({
  53. labelVal:text,
  54. index:i,
  55. searchPre:text
  56. });
  57. }
  58. onChange(e){
  59. e.stopPropagation();
  60. const {handleChange,boxMark,i,handleSearch,value,mainSaveText,mainIds,handleClear} = this.props;
  61. const {labelVal,searchPre} = this.state;
  62. const text1 =e.target.innerText;
  63. let mainText = filterDataArr(mainSaveText);//主诉字数
  64. if(+boxMark==1){
  65. if(mainText.length >= config.limited){
  66. if(text1.length > labelVal.length){
  67. e.target.innerText = labelVal;
  68. Notify.info(config.limitText);
  69. return
  70. }else if(text1.length == labelVal.length){
  71. this.setState({
  72. labelVal:text1
  73. });
  74. }else{
  75. handleChange&&handleChange({text1,boxMark,i});
  76. }
  77. return
  78. }
  79. }
  80. this.setState({
  81. labelVal:text1
  82. });
  83. const that = this;
  84. handleChange&&handleChange({text1,boxMark,i});
  85. //延迟搜索
  86. clearTimeout(this.state.timer);
  87. const timer = setTimeout(function(){
  88. let newText = e.target.innerText;
  89. let temp = '',isEnd=false;
  90. let search='';
  91. clearTimeout(that.state.timer);
  92. temp = newText.replace(searchPre,'');
  93. isEnd = !(newText.indexOf(searchPre)>0);
  94. search = temp.replace(/[(^\s*)|(\s*$)|(^\,*)|(\,*$)]/g,'');
  95. if(!search&&searchPre){
  96. search = searchPre;
  97. }
  98. //console.log(labelVal,'旧:',searchPre,'新:',newText,'搜索:',search);
  99. if(config.punctuationReg.test(search)){ //只有标点符号时不搜索
  100. handleSearch&&handleSearch({text:search,isEnd,boxMark,mainIds});
  101. }else{//只有标点符号时要清空搜索结果
  102. handleClear && handleClear({boxMark})
  103. }
  104. //搜索后保持现在的值,继续输入时要用于对比
  105. /*that.setState({
  106. searchPre:newText
  107. });*/
  108. },config.delayTime);
  109. this.setState({
  110. timer
  111. });
  112. }
  113. handleBlur(e){//为了阻止冒泡事件
  114. const {boxMark,handleClear,handleChange,i} = this.props;
  115. e.stopPropagation();
  116. // 延时清空搜索结果,不延时会影响选中
  117. clearTimeout(this.state.clearTimer);
  118. const clearTimer = setTimeout(function(){
  119. handleClear && handleClear({boxMark})
  120. },config.delayTime);
  121. this.setState({
  122. clearTimer
  123. });
  124. }
  125. moveEnd(obj) {
  126. if(window.getSelection){//ie11 10 9 ff safari
  127. obj.focus(); //解决ff不获取焦点无法定位问题
  128. var range = window.getSelection();//创建range
  129. range.selectAllChildren(obj);//range 选择obj下所有子内容
  130. range.collapseToEnd();//光标移至最后
  131. }
  132. else if (document.selection) {//ie10 9 8 7 6 5
  133. var range = document.selection.createRange();//创建选择对象
  134. range.moveToElementText(obj);//range定位到obj
  135. range.collapse(false);//光标移至最后
  136. range.select();
  137. }
  138. }
  139. handleKeydown(e){
  140. const ev = e||window.event;
  141. const target = ev.target||ev.srcElement;
  142. let innerVal = target.innerText;
  143. //禁止回车事件
  144. if(ev.keyCode==13){return false;}
  145. //backspace事件
  146. if(ev.keyCode==8){
  147. //用于对比backspace前后的值
  148. this.setState({
  149. preVal:innerVal
  150. })
  151. }
  152. }
  153. handleKeyup(e){
  154. const {boxMark,handleKeydown,i,value,removeId,handleClear,delSingleLable} = this.props;
  155. const {preVal,index} = this.state;
  156. const ev = e||window.event;
  157. const target = ev.target||ev.srcElement;
  158. let innerVal = target.innerText;
  159. if(ev.keyCode==8){
  160. // 主诉现病史去重:删除最后一个字的时候移除该数据(将name、id和value替换成空)并移除id
  161. // 前面是标签,内容为空时再删一次才移除标签;前面是文本,则直接移除;
  162. // console.log(77,innerVal,preVal);
  163. let preObj = $(this.$span.current).prev();
  164. if(index!==0&&preVal.trim().length==1&& !innerVal){
  165. removeId && removeId({boxMark,i:index,text:""});
  166. handleClear && handleClear({boxMark});//删除最后一个字时清空搜索结果,避免现病史搜索框不立即消失的情况
  167. if(preObj[0].nodeName !=="DIV"){
  168. this.moveEnd(preObj[0]);
  169. }
  170. }
  171. if(innerVal !==preVal){return false}
  172. // 中英文数字和下划线--单独删除标签
  173. /*const reg = new RegExp("([\u4E00-\uFA29]|[\uE7C7-\uE7F3]|[a-zA-Z0-9_])");
  174. if(index!==0 && reg.test(innerVal)){
  175. let obj = preObj[0].nodeName=="DIV"?preObj.prev():preObj;
  176. delSingleLable && delSingleLable({boxMark,i:index});
  177. this.moveEnd(obj[0]);
  178. this.setState({
  179. index: null
  180. })
  181. }*/
  182. let data = innerVal.trim();
  183. //判断是否为空、中英文:, 。、;,且不是第一位
  184. let pattern = new RegExp(/^\,?$|^\,?$|^\.?$|^\。?$|^\、?$|^\;?$|^\;?$|^\:?$|^\:?$\s/);
  185. if(index!==0 && pattern.test(data)){
  186. // let preObj = $(this.$span.current).prev();
  187. let obj = preObj[0].nodeName=="DIV"?preObj.prev():preObj;
  188. handleKeydown&&handleKeydown({boxMark,i:index,text:data});
  189. this.moveEnd(obj[0]);
  190. this.setState({
  191. index: null
  192. })
  193. }
  194. }
  195. }
  196. componentWillReceiveProps(next){
  197. const isRead = this.props.isRead;
  198. if(next.isRead != isRead){
  199. this.$span.current.innerText = next.value||'';
  200. }
  201. }
  202. componentDidMount(){
  203. const {value} = this.props;
  204. if(value){
  205. this.$span.current.innerText = value||'';
  206. }
  207. if(isIE()){
  208. $(this.$span.current).onIe8Input(function(e){
  209. this.onChange(e)
  210. },this);
  211. }
  212. }
  213. getClass(){
  214. const {full,value,saveText,i} = this.props;
  215. const preSelected = saveText[i-1];
  216. const isFull = full?' '+style['full']:''; //是否宽度设为整行宽度
  217. //有标点符号之外的字符或者前一个标签有选中值时,显示为黑色,否则灰显
  218. const unselect = value.match(config.punctuationReg)||preSelected?'':style['unselect'];
  219. return classNames(style['editable-span'],isFull,unselect);
  220. }
  221. render() {
  222. return <span className={this.getClass()}
  223. contentEditable='true'
  224. ref={this.$span}
  225. onInput={this.onChange}
  226. onFocus={this.handleFocus}
  227. onBlur={this.handleBlur}
  228. onkeydown={this.handleKeydown}
  229. onkeyup={this.handleKeyup}></span>;
  230. }
  231. }
  232. export default EditableSpan;