index.jsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 || e.target.innerHTML;
  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 || e.target.innerHTML;
  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 || e.target.innerHTML;
  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&&newText){
  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 {i,setSearchLocation} = this.props;
  143. const target = ev.target||ev.srcElement;
  144. let innerVal = target.innerText || target.innerHTML,ele,boxTop;
  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. let range = window.getSelection();
  155. let textIndex = range.focusOffset;
  156. let textLength = range.anchorNode.length;
  157. if(ev.keyCode==37&& i!=0){//向左
  158. let preObj = $(this.$span.current).prev();
  159. let obj = preObj[0]&&preObj[0].nodeName=="DIV"?preObj.prev():preObj;
  160. if(textIndex == 0){
  161. if(ev.preventDefault){//阻止默认事件
  162. ev.preventDefault();
  163. }else{
  164. ev.returnValue=false;
  165. }
  166. this.moveEnd(obj[0]);
  167. }
  168. }
  169. if(ev.keyCode==39){//向右
  170. let nextObj = $(this.$span.current).next();
  171. let obj = nextObj[0]&&nextObj[0].nodeName=="DIV"?nextObj.next():nextObj;
  172. if(textIndex == textLength || textLength==undefined){
  173. if(ev.preventDefault){//阻止默认事件
  174. ev.preventDefault();
  175. }else{
  176. ev.returnValue=false;
  177. }
  178. obj.focus();
  179. }
  180. }
  181. // 注释掉,放开会影响删除
  182. // ele = document.activeElement;
  183. // boxTop = target.innerText?getPageCoordinate(e).boxTop:$(ele).offset().top+30;
  184. // setSearchLocation(boxTop,ele.offsetLeft) //搜索框的位置动态获取
  185. }
  186. handleKeyup(e){
  187. const {boxMark,handleKeydown,i,value,removeId,handleClear,delSingleLable,setSearchLocation} = this.props;
  188. const {preVal,index} = this.state;
  189. const ev = e||window.event;
  190. const target = ev.target||ev.srcElement;
  191. let innerVal = target.innerText || target.innerHTML,ele,boxTop;
  192. if(ev.keyCode==8){
  193. // 主诉现病史去重:删除最后一个字的时候移除该数据(将name、id和value替换成空)并移除id
  194. // 前面是标签,内容为空时再删一次才移除标签;前面是文本,则直接移除;
  195. let preObj = $(this.$span.current).prev();
  196. if(index!==0&&preVal.trim().length==1&& !innerVal){
  197. removeId && removeId({boxMark,i:index,text:""});
  198. handleClear && handleClear({boxMark});//删除最后一个字时清空搜索结果,避免现病史搜索框不立即消失的情况
  199. if(preObj[0].nodeName !=="DIV"){
  200. this.moveEnd(preObj[0]);
  201. }
  202. }
  203. if(innerVal !== preVal){
  204. }
  205. else{
  206. // 中英文数字和下划线--单独删除标签
  207. /*const reg = new RegExp("([\u4E00-\uFA29]|[\uE7C7-\uE7F3]|[a-zA-Z0-9_])");
  208. if(index!==0 && reg.test(innerVal)){
  209. let obj = preObj[0].nodeName=="DIV"?preObj.prev():preObj;
  210. delSingleLable && delSingleLable({boxMark,i:index});
  211. this.moveEnd(obj[0]);
  212. this.setState({
  213. index: null
  214. })
  215. }*/
  216. let data = innerVal.trim();
  217. //判断是否为空、中英文:, 。、;,且不是第一位
  218. // let pattern = new RegExp(/^\,?$|^\,?$|^\.?$|^\。?$|^\、?$|^\;?$|^\;?$|^\:?$|^\:?$|\s/);
  219. // if(index!==0 && pattern.test(data)){
  220. if(index!==0 && !config.punctuationReg.test(data)){
  221. // let preObj = $(this.$span.current).prev();
  222. let obj = preObj[0].nodeName=="DIV"?preObj.prev():preObj;
  223. handleKeydown&&handleKeydown({boxMark,i:index,text:data});
  224. this.moveEnd(obj[0]);
  225. this.setState({
  226. index: null
  227. })
  228. }
  229. }
  230. ele = document.activeElement;
  231. boxTop = target.innerText?getPageCoordinate(e).boxTop:$(ele).offset().top+30;
  232. setSearchLocation(boxTop,ele.offsetLeft) //搜索框的位置动态获取
  233. }
  234. }
  235. componentWillReceiveProps(next){
  236. const isRead = this.props.isRead;
  237. if(next.isRead != isRead){
  238. this.$span.current.innerText?(this.$span.current.innerText = next.value||''):(this.$span.current.innerHTML = next.value||'');
  239. }
  240. }
  241. componentDidMount(){
  242. const {value} = this.props;
  243. if(value){
  244. this.$span.current.innerText?(this.$span.current.innerText = value||''):(this.$span.current.innerHTML = value||'');
  245. }
  246. if(isIE()){
  247. $(this.$span.current).onIe8Input(function(e){
  248. this.onChange(e)
  249. },this);
  250. }
  251. }
  252. /*cancelSelect(e){//双击不选中
  253. if(document.selection&&document.selection.empty){
  254. document.selection.empty();
  255. }else if (window.getSelection) {
  256. var sel=window.getSelection();
  257. sel.removeAllRanges();
  258. }
  259. }*/
  260. getClass(){
  261. const {full,value,saveText,i} = this.props;
  262. const preSelected = saveText[i-1];
  263. const isFull = full?' '+style['full']:''; //是否宽度设为整行宽度
  264. //有标点符号之外的字符或者前一个标签有选中值时,显示为黑色,否则灰显
  265. const unselect = value.match(config.punctuationReg)||preSelected?'':style['unselect'];
  266. return classNames(style['editable-span'],isFull,unselect);
  267. }
  268. render() {
  269. return <span className={this.getClass()}
  270. contentEditable='true'
  271. ref={this.$span}
  272. onInput={this.onChange}
  273. onFocus={this.handleFocus}
  274. onBlur={this.handleBlur}
  275. onkeydown={this.handleKeydown}
  276. onkeyup={this.handleKeyup}></span>;
  277. }
  278. }
  279. export default EditableSpan;