index.jsx 11 KB

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