index.jsx 8.1 KB

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