index.jsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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.handleClick = this.handleClick.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. // e.newValue IE浏览器DOMCharacterDataModified监听
  65. // const text1 = e.target.innerText || e.target.innerHTML || e.newValue;
  66. let mainText = filterDataArr(mainSaveText);//主诉字数
  67. if(+boxMark==1){
  68. if(mainText.length >= config.limited){
  69. if(text1.length > labelVal.length){
  70. e.target.innerText = labelVal;
  71. Notify.info(config.limitText);
  72. return
  73. }else if(text1.length == labelVal.length){
  74. this.setState({
  75. labelVal:text1
  76. });
  77. }else{
  78. handleChange&&handleChange({text1,boxMark,i});
  79. }
  80. return
  81. }
  82. }
  83. this.setState({
  84. labelVal:text1
  85. });
  86. const that = this;
  87. handleChange&&handleChange({text1,boxMark,i});
  88. //延迟搜索
  89. clearTimeout(this.state.timer);
  90. const timer = setTimeout(function(){
  91. let newText = e.target.innerText || e.target.innerHTML;
  92. let temp = '',isEnd=false;
  93. let search='';
  94. clearTimeout(that.state.timer);
  95. temp = newText.replace(searchPre,'');
  96. isEnd = !(newText.indexOf(searchPre)>0);
  97. // search = temp.replace(/[(^\s*)|(\s*$)|(^\,*)|(\,*$)]/g,'');
  98. search = temp.replace(config.regPreAndAft,'');
  99. // if(!search&&searchPre){
  100. if(!temp&&searchPre&&newText){
  101. search = searchPre;
  102. }
  103. //console.log(labelVal,'旧:',searchPre,'新:',newText,'搜索:',search);
  104. if(config.punctuationReg.test(search)){ //只有标点符号时不搜索
  105. handleSearch&&handleSearch({text:search,isEnd,boxMark,mainIds});
  106. }else{//只有标点符号时要清空搜索结果
  107. handleClear && handleClear({boxMark})
  108. }
  109. //搜索后保持现在的值,继续输入时要用于对比
  110. /*that.setState({
  111. searchPre:newText
  112. });*/
  113. },config.delayTime);
  114. this.setState({
  115. timer
  116. });
  117. }
  118. handleBlur(e){//为了阻止冒泡事件
  119. const {boxMark,handleClear,handleChange,i} = this.props;
  120. e.stopPropagation();
  121. // 延时清空搜索结果,不延时会影响选中
  122. /*clearTimeout(this.state.clearTimer);
  123. const clearTimer = setTimeout(function(){
  124. handleClear && handleClear({boxMark})
  125. },config.delayTime);
  126. this.setState({
  127. clearTimer
  128. });*/
  129. }
  130. moveEnd(obj) {
  131. if(window.getSelection){//ie11 10 9 ff safari
  132. obj.focus(); //解决ff不获取焦点无法定位问题
  133. var range = window.getSelection();//创建range
  134. range.selectAllChildren(obj);//range 选择obj下所有子内容
  135. range.collapseToEnd();//光标移至最后
  136. }
  137. else if (document.selection) {//ie10 9 8 7 6 5
  138. var range = document.selection.createRange();//创建选择对象
  139. range.moveToElementText(obj);//range定位到obj
  140. range.collapse(false);//光标移至最后
  141. range.select();
  142. }
  143. }
  144. handleKeydown(e){
  145. const ev = e||window.event;
  146. const {i} = this.props;
  147. const target = ev.target||ev.srcElement;
  148. let innerVal = target.innerText || target.innerHTML,ele,boxTop;
  149. //禁止回车事件
  150. if(ev.keyCode==13){return false;}
  151. //backspace事件 和delete
  152. if(ev.keyCode==8 || ev.keyCode==46){
  153. //用于对比backspace前后的值
  154. this.setState({
  155. preVal:innerVal
  156. })
  157. }
  158. let range = window.getSelection();
  159. let textIndex = range.focusOffset;
  160. let textLength = range.anchorNode.length;
  161. if(ev.keyCode==37&& i!=0){//向左
  162. let preObj = $(this.$span.current).prev();
  163. let obj = preObj[0]&&preObj[0].nodeName=="DIV"?preObj.prev():preObj;
  164. if(textIndex == 0){
  165. if(ev.preventDefault){//阻止默认事件
  166. ev.preventDefault();
  167. }else{
  168. ev.returnValue=false;
  169. }
  170. if(obj){
  171. this.moveEnd(obj[0]);
  172. }
  173. }
  174. }
  175. if(ev.keyCode==39){//向右
  176. let nextObj = $(this.$span.current).next();
  177. let obj = nextObj[0]&&nextObj[0].nodeName=="DIV"?nextObj.next():nextObj;
  178. if(textIndex == textLength || textLength==undefined || (textIndex == 0 && textLength==1)){
  179. if(ev.preventDefault){//阻止默认事件
  180. ev.preventDefault();
  181. }else{
  182. ev.returnValue=false;
  183. }
  184. if(obj){
  185. obj.focus();
  186. }
  187. }
  188. }
  189. }
  190. handleKeyup(e){
  191. const {boxMark,handleKeydown,removeId,handleClear,removeSpan} = this.props;
  192. const {preVal,index} = this.state;
  193. const ev = e||window.event;
  194. const target = ev.target||ev.srcElement;
  195. let innerVal = target.innerText || target.innerHTML,ele,boxTop;
  196. // 可编辑div不支持oninput事件,用此事件替代
  197. /*if(isIE() && innerVal != preVal){
  198. this.onChange(ev);
  199. }*/
  200. if(ev.keyCode==46){//delete
  201. //判断nexObj
  202. // let nextObj = $(this.$span.current).next();
  203. let nextObj = $(this.$span.current);
  204. if(preVal.trim().length==1&& !innerVal){
  205. removeId && removeId({boxMark,i:index,text:"",flag:'del'});
  206. handleClear && handleClear({boxMark});//删除最后一个字时清空搜索结果,避免现病史搜索框不立即消失的情况
  207. //如果后一个不是标签,则光标移到最前
  208. if(nextObj && nextObj[0].nodeName !=="DIV"){
  209. nextObj.focus();
  210. }
  211. }
  212. //action里往后删除
  213. if(innerVal == preVal){
  214. let data = innerVal.trim();
  215. if(nextObj && !config.punctuationReg.test(data) || data=='<br>'){
  216. handleKeydown&&handleKeydown({boxMark,i:index,text:data,flag:'del'});
  217. // nextObj.focus();
  218. if(nextObj && nextObj[0] && nextObj[0].nodeName !=="DIV"){
  219. // IE浏览器focus光标在最后,其他浏览器在最前
  220. nextObj.focus();
  221. }
  222. /*this.setState({
  223. index: null
  224. })*/
  225. }
  226. }
  227. }
  228. if(ev.keyCode==8){
  229. // 主诉现病史去重:删除最后一个字的时候移除该数据(将name、id和value替换成空)并移除id
  230. // 前面是标签,内容为空时再删一次才移除标签;前面是文本,则直接移除;
  231. let preObj = $(this.$span.current).prev();
  232. if(index!==0&&preVal.trim().length==1&& !innerVal){
  233. removeId && removeId({boxMark,i:index,text:""});
  234. handleClear && handleClear({boxMark});//删除最后一个字时清空搜索结果,避免现病史搜索框不立即消失的情况
  235. if(preObj[0].nodeName !=="DIV"){
  236. this.moveEnd(preObj[0]);
  237. }
  238. }
  239. if(innerVal !== preVal){
  240. }
  241. else{
  242. // 中英文数字和下划线--单独删除标签
  243. /*const reg = new RegExp("([\u4E00-\uFA29]|[\uE7C7-\uE7F3]|[a-zA-Z0-9_])");
  244. if(index!==0 && reg.test(innerVal)){
  245. let obj = preObj[0].nodeName=="DIV"?preObj.prev():preObj;
  246. delSingleLable && delSingleLable({boxMark,i:index});
  247. this.moveEnd(obj[0]);
  248. this.setState({
  249. index: null
  250. })
  251. }*/
  252. let data = innerVal.trim();
  253. //判断是否为空、中英文:, 。、;,且不是第一位
  254. // let pattern = new RegExp(/^\,?$|^\,?$|^\.?$|^\。?$|^\、?$|^\;?$|^\;?$|^\:?$|^\:?$|\s/);
  255. // if(index!==0 && pattern.test(data)){
  256. // 后半段是处理IE
  257. if(index!==0 && !config.punctuationReg.test(data) || index!==0 && data=='<br>'){
  258. // let preObj = $(this.$span.current).prev();
  259. let obj = preObj[0].nodeName=="DIV"?preObj.prev():preObj;
  260. handleKeydown&&handleKeydown({boxMark,i:index,text:data,flag:'backsp'});
  261. this.moveEnd(obj[0]);
  262. this.setState({
  263. index: null
  264. })
  265. }
  266. }
  267. // 主诉使用模板删除最后一个空span时移除
  268. if(boxMark==1 && index==0 && !innerVal){
  269. removeSpan();
  270. }
  271. }
  272. }
  273. componentWillReceiveProps(next){
  274. const isRead = this.props.isRead;
  275. if(next.isRead != isRead){
  276. this.$span.current.innerText?(this.$span.current.innerText = next.value||''):(this.$span.current.innerHTML = next.value||'');
  277. }
  278. }
  279. handleClick(e){
  280. $(this.$span.current).attr({"contentEditable":true}).focus()
  281. }
  282. componentDidMount(){
  283. const {preIsExt,afterIsExt,value} = this.props;
  284. const br = preIsExt&&!afterIsExt; //最后一个体征标签
  285. const val = br&&!config.punctuationReg.test(value)?'':value;
  286. if(value){
  287. this.$span.current.innerText?(this.$span.current.innerText = val||''):(this.$span.current.innerHTML = val||'');
  288. }
  289. }
  290. getClass(){
  291. const {full,value,saveText,i,preIsExt} = this.props;
  292. const preSelected = saveText[i-1];
  293. const isFull = full?' '+style['full']:''; //是否宽度设为整行宽度
  294. //有标点符号之外的字符或者前一个标签有选中值时,显示为黑色,否则查体中,有体征标记显示蓝色,否则灰显
  295. const ext = preIsExt?style['ext']:style['unselect'];
  296. const unselect = value.match(config.punctuationReg)||preSelected?'':ext;
  297. return classNames(style['editable-span'],isFull,unselect);
  298. }
  299. render() {
  300. const {preIsExt,afterIsExt} = this.props;
  301. const br = preIsExt&&!afterIsExt; //最后一个体征标签
  302. if(br){
  303. return <React.Fragment>
  304. <span className={this.getClass()}
  305. contentEditable='true'
  306. ref={this.$span}
  307. onInput={this.onChange}
  308. onFocus={this.handleFocus}
  309. onBlur={this.handleBlur}
  310. onKeyDown={this.handleKeydown}
  311. onClick={this.handleClick}
  312. onKeyUp={this.handleKeyup}></span>
  313. <br/>
  314. </React.Fragment>;
  315. }
  316. return <span className={this.getClass()}
  317. contentEditable='true'
  318. ref={this.$span}
  319. onInput={this.onChange}
  320. onFocus={this.handleFocus}
  321. onBlur={this.handleBlur}
  322. onkeydown={this.handleKeydown}
  323. onclick={this.handleClick}
  324. onkeyup={this.handleKeyup}></span>;
  325. }
  326. }
  327. export default EditableSpan;