index.jsx 9.5 KB

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